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