00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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
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 g_free(context);
00089 }
00090
00091 void
00092 gpe_expense_error (QofSession * session)
00093 {
00094 if (qof_error_check (session))
00095 gpe_error_box (qof_error_get_message (session));
00096 }
00097
00098 static struct gpe_icon my_icons[] = {
00099 { "icon", EXPENSE_ICON, 0 },
00100 { NULL, NULL, NULL }
00101 };
00102
00103 static void
00104 gpe_gui_start(int argc, char *argv[], GpeExpenseData *context)
00105 {
00106 g_return_if_fail(context);
00107 g_return_if_fail(gpe_application_init (&argc, &argv));
00108 g_return_if_fail(gpe_load_icons (my_icons));
00109 ENTER (" file=%s", context->qof.write_file);
00110 if(!context->qof.write_file)
00111 {
00112 gint test;
00113 gboolean gpe_home_exists;
00114
00115
00116 test = 0;
00117 context->qof.write_file = g_strconcat (g_get_home_dir(),
00118 "/", SQLITE_DIR, NULL);
00119 gpe_home_exists = g_file_test (context->qof.write_file, G_FILE_TEST_IS_DIR);
00120 if (!gpe_home_exists)
00121 test = g_mkdir (context->qof.write_file, 0700);
00122 g_free (context->qof.write_file);
00123 context->qof.write_file = g_strconcat (ACCESS_METHOD,
00124 ":", g_get_home_dir(), "/", SQLITE_DIR, DEFAULT_FILE, NULL);
00125 if (test)
00126 context->qof.write_file = g_strconcat (ACCESS_METHOD,
00127 ":", g_get_tmp_dir(), "/", DEFAULT_FILE, NULL);
00128 }
00129 qof_session_begin(context->qof.input_session,
00130 context->qof.write_file, TRUE, FALSE);
00131 gpe_expense_error (context->qof.input_session);
00132 qof_session_load(context->qof.input_session, NULL);
00133 context->book = qof_session_get_book(context->qof.input_session);
00134 gtk_set_locale ();
00135 gtk_init (&argc, &argv);
00136
00137 open_expenses_window (context);
00138
00139 gtk_main ();
00140 qof_session_save(context->qof.input_session, NULL);
00141 LEAVE (" ");
00142 }
00143
00144 int
00145 main (int argc, char *argv[])
00146 {
00147 QOF_OP_VARS
00148 const gchar *help_header_text, *input_file;
00149 GpeExpenseData *gpe_expense_context;
00150 gboolean debug_on;
00151 poptContext pc;
00152 gint optc;
00153 qof_op_type exp_command;
00154
00155 struct poptOption options[] = {
00156 { NULL, '\0', POPT_ARG_INCLUDE_TABLE, poptHelpOptionsI18N,
00157 0, _("Help options:"), NULL },
00158 {"list", 'l', POPT_ARG_NONE, NULL, qof_op_list,
00159 _("List all databases supported by the current QOF framework and exit."), NULL},
00160 {"explain", 0, POPT_ARG_NONE, NULL, qof_op_explain,
00161 _("List the fields within the specified database and exit, requires -d."), NULL},
00162 {"input-file", 'i', POPT_ARG_STRING, &filename, qof_op_input,
00163 _("Query the QSF XML data in <filename>"), _("filename")},
00164 {"date", 't', POPT_ARG_STRING, &date_time, qof_op_time,
00165 _("Shorthand to only query objects that contain the specified date."), _("string")},
00166 {"sql", 's', POPT_ARG_STRING, &sql_query, qof_op_sql,
00167 _("Specify a SQL query on the command line."), _("string")},
00168 {"sql-file", 'f', POPT_ARG_STRING, &sql_file, qof_op_sql_file,
00169 _("Specify one or more SQL queries contained in a file."), _("filename")},
00170 {"write", 'w', POPT_ARG_STRING, &write_file, qof_op_write,
00171 _("Write the results of any query to the file"), _("filename")},
00172 {"gui", 0, POPT_ARG_NONE, NULL, qof_op_gui,
00173 _("Use the Gtk graphic interface"), NULL},
00174 {"debug", 0, POPT_ARG_NONE, NULL, qof_op_debug,
00175 _("Print debugging information to a temporary file."), NULL},
00176 {"version", 0, POPT_ARG_NONE, NULL, qof_op_vers,
00177 _("Display version information"), NULL},
00178 {"category", 'c', POPT_ARG_STRING, &category, qof_op_category,
00179 _("Shorthand to only query objects that are set to the specified category."),
00180 _("string")},
00181 POPT_TABLEEND
00182 };
00183 exp_command = qof_op_noop;
00184 debug_on = FALSE;
00185 QOF_OP_INIT
00186 input_file = NULL;
00187 database = g_strdup("gpe_expenses");
00188
00189 #ifdef ENABLE_NLS
00190 setlocale (LC_ALL, "");
00191 bindtextdomain (GETTEXT_PACKAGE, LOCALE_DIR);
00192 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
00193 textdomain (GETTEXT_PACKAGE);
00194 #endif
00195 help_header_text = _(
00196
00197
00198
00199
00200
00201 "\n"
00202 " Expenses applet for GPE using QOF - \n"
00203 " the Query Object Framework.\n"
00204 " Supports writing iPAQ data to SQLite.\n"
00205 " SQL-type queries on the live data or SQLite file.\n"
00206 " SQLite data can be imported into other QOF applications.\n\n"
00207 " Use exactly one of -i -l --gui or --explain;\n"
00208 " options are -c -t -w, -s or -f.\n\n");
00209
00210 pc = poptGetContext (PACKAGE, argc, (const char **)argv, options, 0);
00211
00212 poptSetOtherOptionHelp (pc, help_header_text);
00213
00214 if (argc < 2)
00215 {
00216 poptPrintUsage (pc, stderr, 0);
00217 return EXIT_FAILURE;
00218 }
00219 gpe_expense_context = gpe_expense_init();
00220 g_return_val_if_fail (gpe_expense_context, 1);
00221 while ((optc = poptGetNextOpt (pc)) >= 0)
00222 {
00223 switch (optc)
00224 {
00225
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
00248 fprintf (stdout, _(" Build target.........: %s\n"), HOST_OS);
00249
00250 fprintf (stdout, _(" Build date...........: %s %s\n"), __DATE__, __TIME__);
00251
00252 fprintf (stdout, _(" --debug logs to......: %s\n\n"), GPE_EXPENSE_LOG);
00253
00254 fprintf (stdout, _(" Please use --help for more detailed options.\n\n"));
00255 return EXIT_SUCCESS;
00256 }
00257
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
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
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 qof_mod_database (database, &gpe_expense_context->qof);
00336 if(!gpe_expense_context->qof.database)
00337 {
00338
00339 fprintf (stderr, _("%s: Error: please specify which "
00340 "database you would like explained.\n\n"), PACKAGE);
00341 break;
00342 }
00343 qof_cmd_explain(&gpe_expense_context->qof);
00344 break;
00345 }
00346 default:
00347 {
00348
00349 break;
00350 }
00351 }
00352 poptFreeContext(pc);
00353 if(debug_on) { qof_log_shutdown(); }
00354 gpe_expense_close(gpe_expense_context);
00355 return EXIT_SUCCESS;
00356 }