gpe-expenses.c

00001 /******************************************************************
00002  *            gpe-expenses.c
00003  *
00004  *  Sun Nov 13 14:54:18 2005
00005  *  Copyright  2005  Neil Williams
00006  *  linux@codehelp.co.uk
00007  ******************************************************************/
00008 /*
00009     This package is free software; you can redistribute it and/or modify
00010     it under the terms of the GNU General Public License as published by
00011     the Free Software Foundation; either version 3 of the License, or
00012     (at your option) any later version.
00013 
00014     This program is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017     GNU General Public License for more details.
00018 
00019     You should have received a copy of the GNU General Public License
00020     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00021  */
00022 
00023 #define _GNU_SOURCE
00024 #include "config.h"
00025 #include <stdio.h>
00026 #include <stdlib.h>
00027 #include <glib.h>
00028 #include <glib/gi18n.h>
00029 #include <glib/gprintf.h>
00030 #include <glib/gstdio.h>
00031 #include <qof.h>
00032 #include <gtk/gtk.h>
00033 #include <gpe/init.h>
00034 #include <gtk/gtkmain.h>
00035 #include <gpe/pixmaps.h>
00036 #include <gpe/pim-categories.h>
00037 #include <gpe/errorbox.h>
00038 #include <regex.h>
00039 #include <popt.h>
00040 #include <locale.h>
00041 #include "qof-main.h"
00042 #include "gpe-expenses.h"
00043 #include "expenses-gtk.h"
00044 
00045 /* used to print debug logs. */
00046 static QofLogModule log_module = GPE_MOD_CLI;
00047 #define EXPENSE_ICON PREFIX "/share/pixmaps/gpe-expenses.xpm"
00048 #define GPE_EXPENSE_LOG "/tmp/gpe-expense.trace"
00049 #define ARGUMENT_BAD_OPTION 17227
00050 #define QOF_MOD_SQLITE "qof-sqlite-module"
00051 #define SQLITE_DIR ".gpe/"
00052 #define DEFAULT_FILE "expenses"
00053 #define ACCESS_METHOD "sqlite"
00054 
00055 #define ENUM_LIST_Q(_)  \
00056         _(qof_op_noop, = 0) \
00057         _(qof_op_list,)     \
00058         _(qof_op_category,) \
00059         _(qof_op_time,) \
00060         _(qof_op_sql,)      \
00061         _(qof_op_sql_file,) \
00062         _(qof_op_write, )   \
00063         _(qof_op_explain,)  \
00064         _(qof_op_vers,)     \
00065         _(qof_op_compress,) \
00066         _(qof_op_debug,)    \
00067         _(qof_op_input, ) \
00068         _(qof_op_gui, )
00069 
00070         DEFINE_ENUM(qof_op_type, ENUM_LIST_Q)
00071 
00072 GpeExpenseData*
00073 gpe_expense_init (void)
00074 {
00075         GpeExpenseData *context;
00076 
00077         qof_init();
00078         g_return_val_if_fail(ExpensesRegister (), NULL);
00079         context = g_new0(GpeExpenseData, 1);
00080         return context;
00081 }
00082 
00083 void
00084 gpe_expense_close(GpeExpenseData *context)
00085 {
00086         qof_main_free(&context->qof);
00087         qof_close();
00088         if(context->category_list)
00089                 g_list_free(context->category_list);
00090         g_free(context);
00091 }
00092 
00093 void
00094 gpe_expense_error (QofSession * session)
00095 {
00096         if (qof_error_check (session))
00097                 gpe_error_box (qof_error_get_message (session));
00098 }
00099 
00100 static struct gpe_icon my_icons[] = {
00101         { "icon", EXPENSE_ICON, 0 },
00102   { NULL, NULL, NULL }
00103 };
00104 
00105 static void
00106 gpe_gui_start(int argc, char *argv[], GpeExpenseData *context)
00107 {
00108         g_return_if_fail(context);
00109         g_return_if_fail(gpe_application_init (&argc, &argv));
00110         g_return_if_fail(gpe_load_icons (my_icons));
00111         ENTER (" file=%s", context->qof.write_file);
00112         if(!context->qof.write_file)
00113         {
00114                 gint test;
00115                 gboolean gpe_home_exists;
00116 
00117                 /* use a fixed file location. */
00118                 test = 0;
00119                 context->qof.write_file = g_strconcat (g_get_home_dir(), 
00120                         "/", SQLITE_DIR, NULL);
00121                 gpe_home_exists = g_file_test (context->qof.write_file, G_FILE_TEST_IS_DIR);
00122                 if (!gpe_home_exists)
00123                         test = g_mkdir (context->qof.write_file, 0700);         
00124                 g_free (context->qof.write_file);
00125                 context->qof.write_file = g_strconcat (ACCESS_METHOD, 
00126                         ":", g_get_home_dir(), "/", SQLITE_DIR, DEFAULT_FILE, NULL);
00127                 if (test)
00128                         context->qof.write_file = g_strconcat (ACCESS_METHOD,
00129                         ":", g_get_tmp_dir(), "/", DEFAULT_FILE, NULL);
00130         }
00131         qof_session_begin(context->qof.input_session, 
00132                 context->qof.write_file, TRUE, FALSE);
00133         gpe_expense_error (context->qof.input_session);
00134         qof_session_load(context->qof.input_session, NULL);
00135         context->book = qof_session_get_book(context->qof.input_session);
00136         gtk_set_locale ();
00137         gtk_init (&argc, &argv);
00138 
00139         open_expenses_window (context);
00140 
00141         gtk_main ();
00142         qof_session_save(context->qof.input_session, NULL);
00143         LEAVE (" ");
00144 }
00145 
00146 int
00147 main (int argc, char *argv[])
00148 {
00149         QOF_OP_VARS
00150         const gchar *help_header_text, *input_file;
00151         GpeExpenseData *gpe_expense_context;
00152         gboolean debug_on;
00153         poptContext pc;
00154         gint optc;
00155         qof_op_type exp_command;
00156 
00157         struct poptOption options[] = {
00158                 { NULL, '\0', POPT_ARG_INCLUDE_TABLE, poptHelpOptionsI18N,
00159                         0, _("Help options:"), NULL },
00160                 {"list", 'l', POPT_ARG_NONE, NULL, qof_op_list,
00161                  _("List all databases supported by the current QOF framework and exit."), NULL},
00162                 {"explain", 0, POPT_ARG_NONE, NULL, qof_op_explain,
00163                  _("List the fields within the specified database and exit, requires -d."), NULL},
00164                 {"input-file", 'i', POPT_ARG_STRING, &filename, qof_op_input,
00165                  _("Query the QSF XML data in <filename>"), _("filename")},
00166                 {"date", 't', POPT_ARG_STRING, &date_time, qof_op_time,
00167                  _("Shorthand to only query objects that contain the specified date."), _("string")},
00168                 {"sql", 's', POPT_ARG_STRING, &sql_query, qof_op_sql,
00169                  _("Specify a SQL query on the command line."), _("string")},
00170                 {"sql-file", 'f', POPT_ARG_STRING, &sql_file, qof_op_sql_file,
00171                  _("Specify one or more SQL queries contained in a file."), _("filename")},
00172                 {"write", 'w', POPT_ARG_STRING, &write_file, qof_op_write,
00173                  _("Write the results of any query to the file"), _("filename")},
00174                 {"gui", 0, POPT_ARG_NONE, NULL, qof_op_gui,
00175                  _("Use the Gtk graphic interface"), NULL},
00176                 {"debug", 0, POPT_ARG_NONE, NULL, qof_op_debug,
00177                  _("Print debugging information to a temporary file."), NULL},
00178                 {"version", 0, POPT_ARG_NONE, NULL, qof_op_vers,
00179                  _("Display version information"), NULL},
00180                 {"category", 'c', POPT_ARG_STRING, &category, qof_op_category,
00181                  _("Shorthand to only query objects that are set to the specified category."),
00182                  _("string")},
00183                 POPT_TABLEEND
00184         };
00185         exp_command = qof_op_noop;
00186         debug_on = FALSE;
00187         QOF_OP_INIT
00188         input_file = NULL;
00189 
00190 #ifdef ENABLE_NLS
00191         setlocale (LC_ALL, "");
00192         bindtextdomain (GETTEXT_PACKAGE, LOCALE_DIR);
00193         bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
00194         textdomain (GETTEXT_PACKAGE);
00195 #endif
00196         help_header_text = _(
00197         /* Translators: please retain the line endings
00198         and punctuation. -i -l --gui or --explain are commands,
00199         options are as specified. Please retain 'or' in all cases,
00200         options and commands can only be combined in specific ways.
00201         */
00202                 "\n"
00203                 "   Expenses applet for GPE using QOF - \n"
00204                 "   the Query Object Framework.\n"
00205                 "   Supports writing iPAQ data to SQLite.\n"
00206                 "   SQL-type queries on the live data or SQLite file.\n"
00207                 "   SQLite data can be imported into other QOF applications.\n\n"
00208                 "   Use exactly one of -i -l --gui or --explain;\n"
00209                 "   options are -c -t -w, -s or -f.\n\n");
00210 
00211         pc = poptGetContext (PACKAGE, argc, (const char **)argv, options, 0);
00212 
00213         poptSetOtherOptionHelp (pc, help_header_text);
00214 
00215         if (argc < 2)
00216         {
00217                 poptPrintUsage (pc, stderr, 0);
00218                 return EXIT_FAILURE;
00219         }
00220         gpe_expense_context = gpe_expense_init();
00221         while ((optc = poptGetNextOpt (pc)) >= 0)
00222         {
00223                 switch (optc)
00224                 {
00225                         /* commands - mutually exclusive */
00226                         case qof_op_input:
00227                         case qof_op_list:
00228                         case qof_op_explain:
00229                         case qof_op_gui:
00230                         {
00231                                 if (qof_op_noop != exp_command)
00232                                 {
00233                                         fprintf (stderr, _("%s: ERROR: specify only one of"
00234                                                 "-i, -l, --gui or --explain.\n"), PACKAGE);
00235                                         return 1;
00236                                 }
00237                                 exp_command = optc;
00238                                 break;
00239                         }
00240                         case qof_op_vers :
00241                         {
00242                                 fprintf (stdout, "\n Copyright (c) 2005-2007 Neil Williams <linux@codehelp.co.uk>\n");
00243                                 fprintf (stdout, _(" For gpe-expenses support, join the QOF-devel mailing list at\n"));
00244                                 fprintf (stdout, " http://lists.sourceforge.net/mailman/listinfo/qof-devel\n");
00245                                 fprintf (stdout, _("\n This is gpe-expenses v%s\n"), VERSION);
00246                                 fprintf (stdout, _(" Expenses applet for GPE on iPAQ .\n"));
00247                                 /* Translators: Add or subtract dots to keep the translated lines aligned vertically */
00248                                 fprintf (stdout, _(" Build target.........: %s\n"), HOST_OS);
00249                                 /* Translators: Add or subtract dots to keep the translated lines aligned vertically */
00250                                 fprintf (stdout, _(" Build date...........: %s %s\n"), __DATE__, __TIME__);
00251                                 /* Translators: Add or subtract dots to keep the translated lines aligned vertically */
00252                                 fprintf (stdout, _(" --debug logs to......: %s\n\n"), GPE_EXPENSE_LOG);
00253                                 /* Translators: Add or subtract dots to keep the translated lines aligned vertically */
00254                                 fprintf (stdout, _(" Please use --help for more detailed options.\n\n"));
00255                                 return EXIT_SUCCESS;
00256                         }
00257                         /* optional modifiers - store to act on later. */
00258                         case qof_op_category:
00259                         {
00260                                 qof_mod_category (category, &gpe_expense_context->qof);
00261                                 break;
00262                         }
00263                         case qof_op_time:
00264                         {
00265                                 qof_mod_time (date_time, &gpe_expense_context->qof);
00266                                 break;
00267                         }
00268                         case qof_op_sql:
00269                         {
00270                                 qof_mod_sql (sql_query, &gpe_expense_context->qof);
00271                                 break;
00272                         }
00273                         case qof_op_sql_file:
00274                         {
00275                                 qof_mod_sql_file (sql_file, &gpe_expense_context->qof);
00276                                 break;
00277                         }
00278                         case qof_op_write:
00279                         {
00280                                 qof_mod_write (write_file, &gpe_expense_context->qof);
00281                                 break;
00282                         }
00283                         case qof_op_debug:
00284                         {
00285                                 qof_log_init_filename(GPE_EXPENSE_LOG);
00286                                 qof_log_set_default(QOF_LOG_DETAIL);
00287                                 qof_log_set_level (GPE_MOD_CLI, QOF_LOG_DETAIL);
00288                                 qof_log_set_level (QOF_MAIN_CLI, QOF_LOG_DETAIL);
00289                                 qof_log_set_level (QOF_MOD_SQLITE, QOF_LOG_DETAIL);
00290                                 qof_log_set_level (GPE_MOD_GUI, QOF_LOG_DETAIL);
00291                                 debug_on = TRUE;
00292                                 break;
00293                         }
00294                         default:
00295                         {
00296                                 fprintf (stderr, _("%s: ERROR: got option %d, arg %s\n"), PACKAGE,
00297                                          optc, poptGetOptArg (pc));
00298                                 return EXIT_FAILURE;
00299                         }
00300                 }
00301         }
00302         if (optc < -1)
00303         {
00304                 fprintf(stderr, "%s: %s %s\n\n", PACKAGE,
00305                 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
00306                 poptStrerror(optc));
00307                 poptPrintUsage(pc, stderr, 0);
00308                 return EXIT_FAILURE;
00309         }
00310         if (gpe_expense_context->qof.error)
00311                 return EXIT_FAILURE;
00312         /* If we get this far, we should have sensible options: start the work. */
00313         gpe_expense_context->qof.input_session = qof_session_new();
00314         switch (exp_command)
00315         {
00316                 case qof_op_input:
00317                 {
00318                         gpe_expense_context->qof.filename = g_strdup(filename);
00319                         /* despite the name, this is for any supported file */
00320                         qof_cmd_xmlfile (&gpe_expense_context->qof);
00321                         break;
00322                 }
00323                 case qof_op_list:
00324                 {
00325                         qof_cmd_list ();
00326                         break;
00327                 }
00328                 case qof_op_gui :
00329                 {
00330                         gpe_gui_start(argc, argv, gpe_expense_context);
00331                         break;
00332                 }
00333                 case qof_op_explain:
00334                 {
00335                         if(!gpe_expense_context->qof.database) 
00336                         { 
00337                                 /* Translators: capitalise only the initial letter of error. */
00338                                 fprintf (stderr, _("%s: Error: please specify which "
00339                                         "database you would like explained.\n\n"), PACKAGE);
00340                                 break;
00341                         }
00342                         qof_cmd_explain(&gpe_expense_context->qof);
00343                         break;
00344                 }
00345                 default:
00346                 {
00347                         /* should be impossible */
00348                         break;
00349                 }
00350         }
00351         poptFreeContext(pc);
00352         if(debug_on) { qof_log_shutdown(); }
00353         gpe_expense_close(gpe_expense_context);
00354         return EXIT_SUCCESS;
00355 }

Generated on Tue Dec 11 05:37:30 2007 for gpe-expenses by  doxygen 1.5.4