GnuCash 2.4.99
hello3.c
00001 /*
00002  * FILE:
00003  * hello3.c
00004  *
00005  * FUNCTION:
00006  * experimental gnucash server
00007  * written as a demo, not real code.
00008  * this file is here mostly as a simple intro to what
00009  * the server is doing.
00010  */
00011 
00012 #include <stdio.h>
00013 #include <stdlib.h>
00014 #include <string.h>
00015 #include <unistd.h>
00016 
00017 #include "gnc-engine.h"
00018 #include "io-gncxml.h"
00019 
00020 #include <fcgi_stdio.h>
00021 
00022 
00023 int
00024 main (int argc, char *argv[])
00025 {
00026     int err, fake_argc = 1;
00027     char * fake_argv[] = {"hello", 0};
00028     QofBook *book;
00029     Account *root;
00030     char *bufp;
00031     int rc, sz;
00032 
00033     /* intitialize the engine */
00034     gnc_engine_init (fake_argc, fake_argv);
00035 
00036     /* contact the database, which is a flat file for this demo */
00037     book = qof_book_new ();
00038 
00039     rc = gnc_book_begin (book, "file:/tmp/demo.gnucash", FALSE);
00040     if (!rc) goto bookerrexit;
00041 
00042     rc = gnc_book_load (book);
00043     if (!rc) goto bookerrexit;
00044 
00045     /* the root pointer points to our local cache of the data */
00046     root = gnc_book_get_root_account (book);
00047 
00048     /* --------------------------------------------------- */
00049     /* done with initialization, go into event loop */
00050 
00051     while (FCGI_Accept() >= 0)
00052     {
00053         GList *split_list;
00054         Query *q = NULL;
00055         char *request_method;
00056         int read_len = 0;
00057 
00058         /* get the request method */
00059         request_method = getenv ("REQUEST_METHOD");
00060 
00061         /* Lets pretend that method=get means user has logged
00062          * in.  Send the user the accounts and currencies,
00063          * but not the transactions/splits. */
00064         if (!strcmp ("GET", request_method))
00065         {
00066             gncxml_write_account_tree_to_buf(root, &bufp, &sz);
00067 
00068             /* print the HTTP header */
00069             printf("Content-type: text/gnc-xml\r\n"
00070                    "Content-Length: %d\r\n"
00071                    "\r\n", sz);
00072 
00073             /* send the xml to the client */
00074             printf ("%s", bufp);
00075             free (bufp);
00076 
00077             /* wait for the next request */
00078             continue;
00079         }
00080 
00081 
00082         if (!strcmp ("POST", request_method))
00083         {
00084             char * content_length = getenv("CONTENT_LENGTH");
00085             read_len = atoi (content_length);
00086 
00087             /* read 'read_len' bytes from stdin ... */
00088             bufp = (char *) malloc (read_len);
00089             fread (bufp, read_len, 1, stdin);
00090 
00091             /* conver the xml input into a gnucash query structure... */
00092             q = gncxml_read_query (bufp, read_len);
00093             xaccQuerySetGroup (q, root);
00094 
00095             /* hack -- limit to 30 splits ... */
00096             qof_query_set_max_results (q, 30);
00097             split_list = qof_query_run (q);
00098 
00099             qof_query_destroy (q);
00100 
00101             /* wait for the next request */
00102             continue;
00103         }
00104 
00105         /* if we got to here, an error -- unknown method */
00106         printf("Content-type: text/plain\r\n"
00107                "\r\n"
00108                "unknown request type \n");
00109 
00110 
00111     }
00112 
00113 bookerrexit:
00114 
00115     err = gnc_book_get_error (book);
00116 
00117     /* 500 Server Error */
00118     FCGI_SetExitStatus (500);
00119 
00120     printf("Content-type: text/plain\r\n\r\n"
00121            "error was %s\n", strerror (err));
00122 
00123     FCGI_Finish();
00124 
00125     /* close the book */
00126     qof_book_destroy (book);
00127 
00128     /* shut down the engine */
00129     gnc_engine_shutdown ();
00130 
00131     sleep (1);
00132 
00133     return 0;
00134 }
00135 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines