GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
hello3.c
1 /*
2  * FILE:
3  * hello3.c
4  *
5  * FUNCTION:
6  * experimental gnucash server
7  * written as a demo, not real code.
8  * this file is here mostly as a simple intro to what
9  * the server is doing.
10  */
11 
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <unistd.h>
16 
17 #include "gnc-engine.h"
18 #include "io-gncxml.h"
19 
20 #include <fcgi_stdio.h>
21 
22 
23 int
24 main (int argc, char *argv[])
25 {
26  int err, fake_argc = 1;
27  char * fake_argv[] = {"hello", 0};
28  QofBook *book;
29  Account *root;
30  char *bufp;
31  int rc, sz;
32 
33  /* intitialize the engine */
34  gnc_engine_init (fake_argc, fake_argv);
35 
36  /* contact the database, which is a flat file for this demo */
37  book = qof_book_new ();
38 
39  rc = gnc_book_begin (book, "file:/tmp/demo.gnucash", FALSE);
40  if (!rc) goto bookerrexit;
41 
42  rc = gnc_book_load (book);
43  if (!rc) goto bookerrexit;
44 
45  /* the root pointer points to our local cache of the data */
46  root = gnc_book_get_root_account (book);
47 
48  /* --------------------------------------------------- */
49  /* done with initialization, go into event loop */
50 
51  while (FCGI_Accept() >= 0)
52  {
53  GList *split_list;
54  Query *q = NULL;
55  char *request_method;
56  int read_len = 0;
57 
58  /* get the request method */
59  request_method = getenv ("REQUEST_METHOD");
60 
61  /* Lets pretend that method=get means user has logged
62  * in. Send the user the accounts and currencies,
63  * but not the transactions/splits. */
64  if (!strcmp ("GET", request_method))
65  {
66  gncxml_write_account_tree_to_buf(root, &bufp, &sz);
67 
68  /* print the HTTP header */
69  printf("Content-type: text/gnc-xml\r\n"
70  "Content-Length: %d\r\n"
71  "\r\n", sz);
72 
73  /* send the xml to the client */
74  printf ("%s", bufp);
75  free (bufp);
76 
77  /* wait for the next request */
78  continue;
79  }
80 
81 
82  if (!strcmp ("POST", request_method))
83  {
84  char * content_length = getenv("CONTENT_LENGTH");
85  read_len = atoi (content_length);
86 
87  /* read 'read_len' bytes from stdin ... */
88  bufp = (char *) malloc (read_len);
89  fread (bufp, read_len, 1, stdin);
90 
91  /* conver the xml input into a gnucash query structure... */
92  q = gncxml_read_query (bufp, read_len);
93  xaccQuerySetGroup (q, root);
94 
95  /* hack -- limit to 30 splits ... */
97  split_list = qof_query_run (q);
98 
100 
101  /* wait for the next request */
102  continue;
103  }
104 
105  /* if we got to here, an error -- unknown method */
106  printf("Content-type: text/plain\r\n"
107  "\r\n"
108  "unknown request type \n");
109 
110 
111  }
112 
113 bookerrexit:
114 
115  err = gnc_book_get_error (book);
116 
117  /* 500 Server Error */
118  FCGI_SetExitStatus (500);
119 
120  printf("Content-type: text/plain\r\n\r\n"
121  "error was %s\n", strerror (err));
122 
123  FCGI_Finish();
124 
125  /* close the book */
126  qof_book_destroy (book);
127 
128  /* shut down the engine */
130 
131  sleep (1);
132 
133  return 0;
134 }
135 
void gnc_engine_shutdown(void)
Definition: gnc-engine.c:160
QofBook * qof_book_new(void)
api for Version 1 XML-based file format
void qof_query_set_max_results(QofQuery *q, int n)
void qof_query_destroy(QofQuery *q)
void gnc_engine_init(int argc, char **argv)
Definition: gnc-engine.c:139
All type declarations for the whole Gnucash engine.
GList * qof_query_run(QofQuery *query)
void qof_book_destroy(QofBook *book)