]> Dogcows Code - chaz/homebank/blob - src/homebank.c
Merge branch 'master' into ext-perl
[chaz/homebank] / src / homebank.c
1 /* HomeBank -- Free, easy, personal accounting for everyone.
2 * Copyright (C) 1995-2016 Maxime DOYEN
3 *
4 * This file is part of HomeBank.
5 *
6 * HomeBank is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * HomeBank is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "homebank.h"
21 #include "ext.h"
22
23 #include "dsp_mainwindow.h"
24 #include "hb-preferences.h"
25 #include "language.h"
26
27
28
29 #ifdef G_OS_WIN32
30 #include <windows.h>
31 #endif
32
33 #define APPLICATION_NAME "HomeBank"
34
35 /****************************************************************************/
36 /* Debug macros */
37 /****************************************************************************/
38 #define MYDEBUG 0
39
40 #if MYDEBUG
41 #define DB(x) (x);
42 #else
43 #define DB(x);
44 #endif
45
46 /* our global datas */
47 struct HomeBank *GLOBALS;
48 struct Preferences *PREFS;
49
50
51 /* installation paths */
52 static gchar *config_dir = NULL;
53 static gchar *images_dir = NULL;
54 static gchar *pixmaps_dir = NULL;
55 static gchar *locale_dir = NULL;
56 static gchar *help_dir = NULL;
57 static gchar *datas_dir = NULL;
58 static gchar *pkglib_dir = NULL;
59
60
61 //#define MARKUP_STRING "<span size='small'>%s</span>"
62
63
64 /* Application arguments */
65 static gboolean arg_version = FALSE;
66 static gchar **files = NULL;
67
68 static GOptionEntry option_entries[] =
69 {
70 { "version", '\0', 0, G_OPTION_ARG_NONE, &arg_version,
71 N_("Output version information and exit"), NULL },
72
73 { G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_FILENAME_ARRAY, &files,
74 NULL, N_("[FILE]") },
75
76 { NULL }
77 };
78
79
80 /*
81 ** try to determine the file type (if supported for import by homebank)
82 **
83 **
84 */
85 gint homebank_alienfile_recognize(gchar *filename)
86 {
87 GIOChannel *io;
88 gint i, retval = FILETYPE_UNKNOW;
89 gchar *tmpstr;
90 gint io_stat;
91 GError *err = NULL;
92 static gint csvtype[7] = {
93 CSV_DATE,
94 CSV_INT,
95 CSV_STRING,
96 CSV_STRING,
97 CSV_STRING,
98 CSV_DOUBLE,
99 CSV_STRING,
100 };
101
102
103 DB( g_print("\n[homebank] alienfile_recognize\n") );
104
105
106 io = g_io_channel_new_file(filename, "r", NULL);
107 if(io != NULL)
108 {
109 g_io_channel_set_encoding(io, NULL, NULL); /* set to binary mode */
110
111 for(i=0;i<4;i++)
112 {
113 if( retval != FILETYPE_UNKNOW )
114 break;
115
116 io_stat = g_io_channel_read_line(io, &tmpstr, NULL, NULL, &err);
117 if( io_stat == G_IO_STATUS_EOF)
118 break;
119 if( io_stat == G_IO_STATUS_ERROR )
120 {
121 DB (g_print(" + ERROR %s\n",err->message));
122 break;
123 }
124 if( io_stat == G_IO_STATUS_NORMAL)
125 {
126 if( *tmpstr != '\0' )
127 {
128 DB( g_print(" line %d: '%s' retval=%d\n", i, tmpstr, retval) );
129
130 /* native homebank file */
131 if( g_str_has_prefix(tmpstr, "<homebank v="))
132 {
133 DB( g_print(" type is HomeBank\n") );
134 retval = FILETYPE_HOMEBANK;
135 }
136 else
137
138 // QIF file ?
139 if( g_str_has_prefix(tmpstr, "!Type") ||
140 g_str_has_prefix(tmpstr, "!type") ||
141 g_str_has_prefix(tmpstr, "!Option") ||
142 g_str_has_prefix(tmpstr, "!option") ||
143 g_str_has_prefix(tmpstr, "!Account") ||
144 g_str_has_prefix(tmpstr, "!account")
145 )
146 {
147 DB( g_print(" type is QIF\n") );
148 retval = FILETYPE_QIF;
149 }
150 else
151
152 /* is it OFX ? */
153 if( g_strstr_len(tmpstr, 10, "OFX") != NULL)
154 {
155 DB( g_print(" type is OFX\n") );
156 retval = FILETYPE_OFX;
157 }
158
159 /* is it csv homebank ? */
160 else
161 {
162 gchar **str_array;
163 gboolean isvalid = FALSE;
164
165 hb_string_strip_crlf(tmpstr);
166 str_array = hb_csv_row_get(tmpstr, ";", 8);
167 isvalid = hb_csv_row_valid(str_array, 8, csvtype);
168
169 DB( g_print(" hbcsv %d\n", isvalid) );
170
171 if( isvalid == TRUE )
172 {
173 DB( g_print(" type is CSV homebank\n") );
174 retval = FILETYPE_CSV_HB;
175 }
176
177 g_strfreev (str_array);
178 }
179 g_free(tmpstr);
180 }
181 }
182 }
183 g_io_channel_unref (io);
184 }
185
186 return retval;
187 }
188
189
190 /* = = = = = = = = = = = = = = = = = = = = */
191
192
193 /*
194 ** ensure the filename ends with '.xhb'
195 */
196 void homebank_file_ensure_xhb(gchar *filename)
197 {
198 DB( g_print("\n[homebank] file_ensure_xhb\n") );
199 filename = (filename == NULL) ? g_strdup(GLOBALS->xhb_filepath) : filename;
200 if( g_str_has_suffix (filename, ".xhb") == FALSE )
201 {
202 gchar *newfilename;
203
204 newfilename = hb_util_filename_new_with_extension(filename, "xhb");
205 hbfile_change_filepath(newfilename);
206 DB( g_print(" - changed to: '%s'\n", GLOBALS->xhb_filepath) );
207 }
208 //#1460390
209 else
210 {
211 hbfile_change_filepath(filename);
212 }
213 }
214
215
216 static gboolean homebank_copy_file(gchar *srcfile, gchar *dstfile)
217 {
218 gchar *buffer;
219 gsize length;
220 //GError *error = NULL;
221 gboolean retval = FALSE;
222
223 if (g_file_get_contents (srcfile, &buffer, &length, NULL))
224 {
225 if(g_file_set_contents(dstfile, buffer, length, NULL))
226 {
227 retval = TRUE;
228 }
229 g_free(buffer);
230 }
231 return retval;
232 }
233
234
235 void homebank_backup_current_file(void)
236 {
237 gchar *bakfilename;
238
239 DB( g_print("\n[homebank] backup_current_file\n") );
240
241 bakfilename = hb_util_filename_new_with_extension (GLOBALS->xhb_filepath, "xhb~");
242 if( g_file_test(bakfilename, G_FILE_TEST_EXISTS) )
243 {
244 DB( g_print(" - delete existing: '%s'\n", bakfilename) );
245 g_remove(bakfilename);
246 }
247
248 DB( g_print(" - copy '%s' => '%s'\n", GLOBALS->xhb_filepath, bakfilename) );
249
250 //#512046 copy file not to broke potential links
251 //retval = g_rename(pathname, newname);
252 homebank_copy_file (GLOBALS->xhb_filepath, bakfilename);
253
254 g_free(bakfilename);
255 }
256
257 /* = = = = = = = = = = = = = = = = = = = = */
258 /* url open */
259
260
261 #ifdef G_OS_WIN32
262 #define SW_NORMAL 1
263
264 static gboolean
265 homebank_util_url_show_win32 (const gchar *url)
266 {
267 int retval;
268 gchar *errmsg;
269
270 /* win32 API call */
271 retval = ShellExecuteA (NULL, "open", url, NULL, NULL, SW_NORMAL);
272
273 if (retval < 0 || retval > 32)
274 return TRUE;
275
276 errmsg = g_win32_error_message(retval);
277 DB( g_print ("%s\n", errmsg) );
278 g_free(errmsg);
279
280 return FALSE;
281 }
282
283 #else
284
285 static gboolean
286 homebank_util_url_show_unix (const gchar *url)
287 {
288 gboolean retval;
289 GError *err = NULL;
290
291 retval = gtk_show_uri (gtk_widget_get_screen (GTK_WIDGET (GLOBALS->mainwindow)), url, GDK_CURRENT_TIME, &err);
292
293 if (!retval)
294 {
295 ui_dialog_msg_infoerror(GTK_WINDOW(GLOBALS->mainwindow), GTK_MESSAGE_ERROR,
296 _("Browser error."),
297 _("Could not display the URL '%s'"),
298 url
299 );
300 }
301
302 if(err != NULL)
303 {
304 g_print ("%s\n", err->message);
305 g_error_free (err);
306 }
307
308 return retval;
309 }
310
311 #endif
312
313 gboolean
314 homebank_util_url_show (const gchar *url)
315 {
316
317 if(url == NULL)
318 return FALSE;
319
320
321 #ifdef G_OS_WIN32
322 return homebank_util_url_show_win32 (url);
323 #else
324 return homebank_util_url_show_unix (url);
325 #endif
326 }
327
328
329 /* = = = = = = = = = = = = = = = = = = = = */
330 /* lastopenedfiles */
331
332 /*
333 ** load lastopenedfiles from homedir/.homebank
334 */
335 gboolean homebank_lastopenedfiles_load(void)
336 {
337 GKeyFile *keyfile;
338 gboolean retval = FALSE;
339 gchar *group, *filename, *lastfilename;
340
341 DB( g_print("\n[homebank] lastopenedfiles load\n") );
342
343 keyfile = g_key_file_new();
344 if(keyfile)
345 {
346
347 filename = g_build_filename(homebank_app_get_config_dir(), "lastopenedfiles", NULL );
348
349 DB( g_print(" - filename: %s\n", filename) );
350
351 if(g_key_file_load_from_file (keyfile, filename, G_KEY_FILE_NONE, NULL))
352 {
353 group = "HomeBank";
354
355 DB( g_print(" - load keyfile ok\n") );
356
357 if(g_key_file_has_key(keyfile, group, "LastOpenedFile", NULL))
358 {
359 DB( g_print(" - keyfile has key ok\n") );
360
361 lastfilename = g_key_file_get_string (keyfile, group, "LastOpenedFile", NULL);
362
363 DB( g_print(" - lastfile loaded: %s\n", lastfilename ) );
364 // #593082
365 if (g_file_test (lastfilename, G_FILE_TEST_EXISTS) != FALSE)
366 {
367 DB( g_print(" - file exists\n") );
368
369 hbfile_change_filepath(lastfilename);
370
371 retval = TRUE;
372 }
373 }
374 }
375 g_free(filename);
376 g_key_file_free (keyfile);
377 }
378
379 DB( g_print(" - return: %d\n", retval) );
380
381 return retval;
382 }
383
384
385 /*
386 ** save lastopenedfiles to homedir/.homebank (HB_DATA_PATH)
387 */
388 gboolean homebank_lastopenedfiles_save(void)
389 {
390 GKeyFile *keyfile;
391 gboolean retval = FALSE;
392 gchar *group, *filename;
393 gsize length;
394
395 DB( g_print("\n[homebank] lastopenedfiles save\n") );
396
397 if( GLOBALS->xhb_filepath != NULL )
398 {
399 keyfile = g_key_file_new();
400 if(keyfile )
401 {
402 DB( g_print(" - saving '%s'\n", GLOBALS->xhb_filepath) );
403
404 group = "HomeBank";
405 g_key_file_set_string (keyfile, group, "LastOpenedFile", GLOBALS->xhb_filepath);
406
407 gchar *contents = g_key_file_to_data( keyfile, &length, NULL);
408
409 //DB( g_print(" keyfile:\n%s\nlen=%d\n", contents, length) );
410
411 filename = g_build_filename(homebank_app_get_config_dir(), "lastopenedfiles", NULL );
412 g_file_set_contents(filename, contents, length, NULL);
413 g_free(filename);
414
415 g_free(contents);
416 g_key_file_free (keyfile);
417 }
418 }
419
420 return retval;
421 }
422
423
424
425 /* = = = = = = = = = = = = = = = = = = = = */
426 /* Main homebank */
427 #ifdef G_OS_WIN32
428 static GtkCssProvider *provider;
429
430 static void
431 homebank_theme_changed (GtkSettings *settings, GParamSpec *pspec, gpointer data)
432 {
433
434 if (pspec == NULL || g_str_equal (pspec->name, "gtk-theme-name"))
435 {
436 gchar *theme;
437 GdkScreen *screen;
438
439 g_object_get (settings, "gtk-theme-name", &theme, NULL);
440 screen = gdk_screen_get_default ();
441
442 DB( g_print("theme %s\n", theme) );
443
444 if (g_str_equal (theme, "gtk-win32"))
445 {
446 if (provider == NULL)
447 {
448 gchar *filename;
449
450 filename = g_build_filename(homebank_app_get_datas_dir(), "homebank-gtk-win32.css", NULL );
451 DB( g_print("tweak file %s\n", filename) );
452
453 if( g_file_test(filename, G_FILE_TEST_EXISTS) )
454 {
455 provider = gtk_css_provider_new ();
456 gtk_css_provider_load_from_path (provider, filename, NULL);
457 }
458 g_free (filename);
459 }
460
461 if(provider != NULL)
462 {
463 DB( g_print(" assign provider %p to screen %p\n", provider, screen) );
464
465 gtk_style_context_add_provider_for_screen (screen,
466 GTK_STYLE_PROVIDER (provider),
467 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
468 }
469 }
470 else if (provider != NULL)
471 {
472 gtk_style_context_remove_provider_for_screen (screen,
473 GTK_STYLE_PROVIDER (provider));
474 g_clear_object (&provider);
475 }
476
477 g_free (theme);
478 }
479 }
480
481 static void
482 homebank_setup_theme_extensions (void)
483 {
484 GtkSettings *settings;
485
486 settings = gtk_settings_get_default ();
487 provider = NULL;
488 g_signal_connect (settings, "notify", G_CALLBACK (homebank_theme_changed), NULL);
489 homebank_theme_changed (settings, NULL, NULL);
490 }
491 #endif
492
493
494 static void
495 homebank_icon_theme_setup()
496 {
497 DB( g_print("\n[homebank] icon_theme_setup\n") );
498
499 GLOBALS->icontheme = gtk_icon_theme_get_default();
500
501 DB( g_print(" - prepend theme search path: %s\n", homebank_app_get_pixmaps_dir()) );
502 gtk_icon_theme_prepend_search_path (GLOBALS->icontheme, homebank_app_get_pixmaps_dir());
503 //DB( g_print(" - append theme search path: %s\n", homebank_app_get_pixmaps_dir()) );
504 //gtk_icon_theme_append_search_path (GLOBALS->icontheme, homebank_app_get_pixmaps_dir());
505
506
507 #if MYDEBUG == 1
508 GtkIconTheme *ic = gtk_icon_theme_get_default();
509 guint i;
510 gchar **paths;
511
512 DB( g_print(" - get default icon theme\n") );
513
514 gtk_icon_theme_get_search_path(ic, &paths, NULL);
515 for(i=0;i<g_strv_length(paths);i++)
516 {
517 g_print(" - path %d: %s\n", i, paths[i]);
518 }
519
520 g_strfreev(paths);
521
522 #endif
523
524
525 }
526
527
528 const gchar *
529 homebank_app_get_config_dir (void)
530 {
531 return config_dir;
532 }
533
534 const gchar *
535 homebank_app_get_images_dir (void)
536 {
537 return images_dir;
538 }
539
540 const gchar *
541 homebank_app_get_pixmaps_dir (void)
542 {
543 return pixmaps_dir;
544 }
545
546 const gchar *
547 homebank_app_get_locale_dir (void)
548 {
549 return locale_dir;
550 }
551
552 const gchar *
553 homebank_app_get_help_dir (void)
554 {
555 return help_dir;
556 }
557
558 const gchar *
559 homebank_app_get_datas_dir (void)
560 {
561 return datas_dir;
562 }
563
564 const gchar *
565 homebank_app_get_pkglib_dir (void)
566 {
567 return pkglib_dir;
568 }
569
570
571 /* build package paths at runtime */
572 static void
573 build_package_paths (void)
574 {
575 DB( g_print("\n[homebank] build_package_paths\n") );
576
577 #ifdef G_OS_WIN32
578 gchar *prefix;
579
580 prefix = g_win32_get_package_installation_directory_of_module (NULL);
581 locale_dir = g_build_filename (prefix, "share", "locale", NULL);
582 images_dir = g_build_filename (prefix, "share", PACKAGE, "images", NULL);
583 pixmaps_dir = g_build_filename (prefix, "share", PACKAGE, "icons", NULL);
584 help_dir = g_build_filename (prefix, "share", PACKAGE, "help", NULL);
585 datas_dir = g_build_filename (prefix, "share", PACKAGE, "datas", NULL);
586 pkglib_dir = g_build_filename (prefix, "lib", PACKAGE, NULL);
587 #ifdef PORTABLE_APP
588 DB( g_print(" - app is portable under windows\n") );
589 config_dir = g_build_filename(prefix, "config", NULL);
590 #else
591 config_dir = g_build_filename(g_get_user_config_dir(), HB_DATA_PATH, NULL);
592 #endif
593 g_free (prefix);
594 #else
595 locale_dir = g_build_filename (DATA_DIR, "locale", NULL);
596 images_dir = g_build_filename (SHARE_DIR, "images", NULL);
597 pixmaps_dir = g_build_filename (DATA_DIR, PACKAGE, "icons", NULL);
598 help_dir = g_build_filename (DATA_DIR, PACKAGE, "help", NULL);
599 datas_dir = g_build_filename (DATA_DIR, PACKAGE, "datas", NULL);
600 pkglib_dir = g_build_filename (PKGLIB_DIR, NULL);
601 config_dir = g_build_filename(g_get_user_config_dir(), HB_DATA_PATH, NULL);
602
603 //#870023 Ubuntu packages the help files in "/usr/share/doc/homebank-data/help/" for some strange reason
604 if(! g_file_test(help_dir, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
605 {
606 g_free (help_dir);
607 help_dir = g_build_filename ("/usr", "share", "doc", "homebank-data", "help", NULL);
608 }
609 #endif
610
611 DB( g_print(" - config_dir : %s\n", config_dir) );
612 DB( g_print(" - images_dir : %s\n", images_dir) );
613 DB( g_print(" - pixmaps_dir: %s\n", pixmaps_dir) );
614 DB( g_print(" - locale_dir : %s\n", locale_dir) );
615 DB( g_print(" - help_dir : %s\n", help_dir) );
616 DB( g_print(" - datas_dir : %s\n", datas_dir) );
617 DB( g_print(" - pkglib_dir : %s\n", pkglib_dir) );
618
619 }
620
621
622 guint32 homebank_app_date_get_julian(void)
623 {
624 GDate *date;
625 //init global default value
626 date = g_date_new();
627 g_date_set_time_t(date, time(NULL));
628 GLOBALS->today = g_date_get_julian(date);
629 g_date_free(date);
630 return GLOBALS->today;
631 }
632
633
634 static gboolean homebank_check_app_dir_migrate_file(gchar *srcdir, gchar *dstdir, gchar *filename)
635 {
636 gchar *srcpath;
637 gchar *dstpath;
638 gchar *buffer;
639 gsize length;
640 //GError *error = NULL;
641 gboolean retval = FALSE;
642
643 DB( g_print("\n[homebank] check_app_dir_migrate_file\n") );
644
645 srcpath = g_build_filename(srcdir, filename, NULL );
646 dstpath = g_build_filename(dstdir, filename, NULL );
647
648 if (g_file_get_contents (srcpath, &buffer, &length, NULL))
649 {
650 if(g_file_set_contents(dstpath, buffer, length, NULL))
651 {
652 //g_print("sould delete %s\n", srcpath);
653 g_remove(srcpath);
654 retval = TRUE;
655 }
656 }
657
658 g_free(dstpath);
659 g_free(srcpath);
660
661 return retval;
662 }
663
664 /*
665 * check/create user home directory for .homebank (HB_DATA_PATH) directory
666 */
667 static void homebank_check_app_dir()
668 {
669 gchar *homedir;
670 const gchar *configdir;
671 gboolean exists;
672
673 DB( g_print("\n[homebank] check_app_dir\n") );
674
675 /* check if <userdir>/.config exist */
676 #ifndef G_OS_WIN32
677 configdir = g_get_user_config_dir();
678 DB( g_print(" - check '%s' exists\n", configdir) );
679 if(!g_file_test(configdir, G_FILE_TEST_IS_DIR))
680 {
681 DB( g_print(" - creating dir\n") );
682 g_mkdir(configdir, 0755);
683 }
684 #endif
685
686 /* check for XDG .config/homebank */
687 configdir = homebank_app_get_config_dir();
688 DB( g_print(" - config_dir is: '%s'\n", configdir) );
689 exists = g_file_test(configdir, G_FILE_TEST_IS_DIR);
690 if(exists)
691 {
692 /* just update folder security */
693 DB( g_print(" - chmod 0700\n") );
694 g_chmod(configdir, 0700);
695 GLOBALS->first_run = FALSE;
696 }
697 else
698 {
699 /* create the config dir */
700 DB( g_print(" - create config_dir\n") );
701 g_mkdir(configdir, 0755);
702 g_chmod(configdir, 0700);
703
704 /* any old homedir configuration out there ? */
705 homedir = g_build_filename(g_get_home_dir (), ".homebank", NULL );
706 DB( g_print(" - homedir is: '%s'\n", homedir) );
707
708 exists = g_file_test(homedir, G_FILE_TEST_IS_DIR);
709 if(exists)
710 {
711 gboolean f1, f2;
712 /* we must do the migration properly */
713 DB( g_print(" - migrate old 2 files\n") );
714 f1 = homebank_check_app_dir_migrate_file(homedir, config_dir, "preferences");
715 f2 = homebank_check_app_dir_migrate_file(homedir, config_dir, "lastopenedfiles");
716 if(f1 && f2)
717 {
718 DB( g_print(" - removing old dir\n") );
719 g_rmdir(homedir);
720 }
721 }
722 g_free(homedir);
723 GLOBALS->first_run = TRUE;
724 }
725
726 }
727
728
729 /*
730 ** application cleanup: icons, GList, memory
731 */
732 static void homebank_cleanup()
733 {
734
735 DB( g_print("\n[homebank] cleanup\n") );
736
737 //v3.4 save windows size/position
738 homebank_pref_save();
739
740 hbfile_cleanup(TRUE);
741
742 /* free our global datas */
743 if( PREFS )
744 {
745 homebank_pref_free();
746 g_free(PREFS);
747 }
748
749 if(GLOBALS)
750 {
751 g_free(GLOBALS);
752 }
753
754 g_free (config_dir);
755 g_free (images_dir);
756 g_free (pixmaps_dir);
757 g_free (locale_dir);
758 g_free (help_dir);
759 g_free (pkglib_dir);
760
761 }
762
763
764
765 /*
766 ** application setup: icons, GList, memory
767 */
768 static gboolean homebank_setup()
769 {
770
771 DB( g_print("\n[homebank] setup\n") );
772
773 GLOBALS = g_malloc0(sizeof(struct HomeBank));
774 if(!GLOBALS) return FALSE;
775 PREFS = g_malloc0(sizeof(struct Preferences));
776 if(!PREFS) return FALSE;
777
778 // check homedir for .homebank dir
779 homebank_check_app_dir();
780
781 homebank_pref_setdefault();
782 homebank_pref_load();
783
784 hbfile_setup(TRUE);
785
786 homebank_icon_theme_setup();
787
788 #ifdef G_OS_WIN32
789 homebank_setup_theme_extensions();
790 #endif
791
792 homebank_app_date_get_julian();
793
794
795 #if MYDEBUG == 1
796
797 g_print(" - user_name: %s\n", g_get_user_name ());
798 g_print(" - real_name: %s\n", g_get_real_name ());
799 g_print(" - user_cache_dir: %s\n", g_get_user_cache_dir());
800 g_print(" - user_data_dir: %s\n", g_get_user_data_dir ());
801 g_print(" - user_config_dir: %s\n", g_get_user_config_dir ());
802 //g_print(" - system_data_dirs: %s\n", g_get_system_data_dirs ());
803 //g_print(" - system_config_dirs: %s\n", g_get_system_config_dirs ());
804
805 g_print(" - home_dir: %s\n", g_get_home_dir ());
806 g_print(" - tmp_dir: %s\n", g_get_tmp_dir ());
807 g_print(" - current_dir: %s\n", g_get_current_dir ());
808
809 #endif
810
811 return TRUE;
812 }
813
814
815 /* = = = = = = = = = = = = = = = = = = = = */
816 /* Main homebank */
817
818 static GtkWidget *
819 homebank_construct_splash()
820 {
821 GtkWidget *window;
822 GtkWidget *frame, *vbox, *image;
823 //gchar *ver_string, *markup, *version;
824 gchar *pathfilename;
825
826 DB( g_print("\n[homebank_construct_splash]\n") );
827
828 window = gtk_window_new(GTK_WINDOW_POPUP); //TOPLEVEL DONT WORK
829 gtk_window_set_type_hint (GTK_WINDOW (window), GDK_WINDOW_TYPE_HINT_SPLASHSCREEN);
830 gtk_window_set_skip_taskbar_hint (GTK_WINDOW (window), TRUE);
831
832 gtk_window_set_title (GTK_WINDOW (window), "HomeBank");
833 gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
834
835 pathfilename = g_build_filename(homebank_app_get_images_dir(), "splash.png", NULL);
836 image = gtk_image_new_from_file((const gchar *)pathfilename);
837 g_free(pathfilename);
838
839 frame = gtk_frame_new (NULL);
840 gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_OUT);
841 gtk_container_add (GTK_CONTAINER (window), frame);
842
843 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
844 gtk_container_add (GTK_CONTAINER (frame), vbox);
845
846 /*
847 ver_string = g_strdup_printf(_("Version: HomeBank-%s"), VERSION);
848
849 version = gtk_label_new(NULL);
850 markup = g_markup_printf_escaped(MARKUP_STRING, ver_string);
851 gtk_label_set_markup(GTK_LABEL(version), markup);
852 g_free(markup);
853 g_free(ver_string);
854 */
855
856 gtk_box_pack_start (GTK_BOX (vbox), image, FALSE, FALSE, 0);
857 //gtk_box_pack_start (GTK_BOX (vbox), version, FALSE, FALSE, 0);
858
859 return window;
860 }
861
862 static void
863 homebank_init_i18n (void)
864 {
865 /* We may change the locale later if the user specifies a language
866 * in the gimprc file. Here we are just initializing the locale
867 * according to the environment variables and set up the paths to
868 * the message catalogs.
869 */
870
871 setlocale (LC_ALL, "");
872
873 bindtextdomain (GETTEXT_PACKAGE, homebank_app_get_locale_dir ());
874 //#ifdef HAVE_BIND_TEXTDOMAIN_CODESET
875 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
876 //#endif
877
878 textdomain (GETTEXT_PACKAGE);
879
880 /*#ifdef G_OS_WIN32
881 gchar *wl = g_win32_getlocale ();
882 DB( g_print(" - win32 locale is '%s'\n", wl) );
883 g_free(wl);
884 #endif*/
885
886 }
887
888
889 int
890 main (int argc, char *argv[], char *env[])
891 {
892 GOptionContext *option_context;
893 GOptionGroup *option_group;
894 GError *error = NULL;
895 GtkWidget *mainwin;
896 GtkWidget *splash = NULL;
897 gboolean openlast;
898
899 DB( g_print("\n--------------------------------" ) );
900 DB( g_print("\nhomebank starting...\n" ) );
901
902 build_package_paths();
903
904 homebank_init_i18n ();
905
906 /* Set up option groups */
907 option_context = g_option_context_new (NULL);
908
909 //g_option_context_set_summary (option_context, _(""));
910
911 option_group = g_option_group_new ("homebank",
912 N_("HomeBank options"),
913 N_("HomeBank options"),
914 NULL, NULL);
915 g_option_group_add_entries (option_group, option_entries);
916 g_option_context_set_main_group (option_context, option_group);
917 g_option_group_set_translation_domain (option_group, GETTEXT_PACKAGE);
918
919 /* Add Gtk option group */
920 g_option_context_add_group (option_context, gtk_get_option_group (FALSE));
921
922 /* Parse command line */
923 if (!g_option_context_parse (option_context, &argc, &argv, &error))
924 {
925 g_option_context_free (option_context);
926
927 if (error)
928 {
929 g_print ("%s\n", error->message);
930 g_error_free (error);
931 }
932 else
933 g_print ("An unknown error occurred\n");
934
935 return -1;
936 }
937
938 g_option_context_free (option_context);
939 option_context = NULL;
940
941 if (arg_version != FALSE)
942 {
943 /* Print version information and exit */
944 g_print ("%s\n", PACKAGE " " VERSION);
945 return 0;
946 }
947
948 /* Pass NULL here since we parsed the gtk+ args already...
949 * from this point all we need a DISPLAY variable to be set.
950 */
951 gtk_init (NULL, NULL);
952
953 //todo: sanity check gtk version here ?
954
955 g_set_application_name (APPLICATION_NAME);
956
957 if( homebank_setup() )
958 {
959 /* change the locale if a language is specified */
960 language_init (PREFS->language);
961
962 DB( g_print(" - loading plugins\n") );
963 ext_init(&argc, &argv, &env);
964
965 GList* it;
966 for (it = PREFS->ext_whitelist; it; it = g_list_next(it)) {
967 ext_load_plugin(it->data);
968 }
969
970 gchar** plugins = ext_list_plugins();
971 gchar** plugins_it;
972 for (plugins_it = plugins; *plugins_it; ++plugins_it) {
973 gboolean loaded = ext_is_plugin_loaded(*plugins_it);
974 g_print("found plugin: %s, loaded: %d\n", *plugins_it, loaded);
975 }
976 g_strfreev(plugins);
977
978 if( PREFS->showsplash == TRUE )
979 {
980 splash = homebank_construct_splash();
981 gtk_window_set_auto_startup_notification (FALSE);
982 gtk_widget_show_all (splash);
983 gtk_window_set_auto_startup_notification (TRUE);
984
985 // make sure splash is up
986 while (gtk_events_pending ())
987 gtk_main_iteration ();
988 }
989
990 gtk_window_set_default_icon_name ("homebank");
991
992 DB( g_print(" - creating window\n" ) );
993
994 mainwin = (GtkWidget *)create_hbfile_window (NULL);
995
996 GValue mainwin_val = G_VALUE_INIT;
997 ext_hook("create_main_window", EXT_OBJECT(&mainwin_val, mainwin), NULL);
998
999 if(mainwin)
1000 {
1001
1002 //todo: pause on splash
1003 if( PREFS->showsplash == TRUE )
1004 {
1005 //g_usleep( G_USEC_PER_SEC * 1 );
1006 gtk_widget_hide(splash);
1007 gtk_widget_destroy(splash);
1008 }
1009
1010
1011 #if HB_UNSTABLE == TRUE
1012 /* GtkWidget *dialog = gtk_message_dialog_new (GTK_WINDOW(mainwin),
1013 GTK_DIALOG_DESTROY_WITH_PARENT,
1014 GTK_MESSAGE_WARNING,
1015 GTK_BUTTONS_CLOSE,
1016 "This is a beta version of HomeBank (UNSTABLE)"
1017 );
1018
1019 gtk_message_dialog_format_secondary_markup (GTK_MESSAGE_DIALOG (dialog),
1020 "DO NOT USE with important files or do a backup first.\n"
1021 "This kind of release is for <b>TESTING ONLY</b>.\n"
1022 "<u>It may be buggy, crash, or lose your data</u>.\n\n"
1023
1024 "For unstable bugs report, questions, suggestions:\n"
1025 " - <b>DO NOT USE LaunchPad</b>\n"
1026 " - direct email to: &lt;homebank@free.fr&gt;\n\n"
1027
1028 "<i>Thanks !</i>"
1029 );
1030
1031 gtk_dialog_run (GTK_DIALOG (dialog));
1032 gtk_widget_destroy (dialog);*/
1033 #endif
1034
1035 if(GLOBALS->first_run)
1036 {
1037 ui_mainwindow_action_help_welcome();
1038 }
1039
1040
1041 while (gtk_events_pending ()) /* make sure splash is gone */
1042 gtk_main_iteration ();
1043
1044
1045 DB( g_print(" - open last file ?\n" ) );
1046
1047 // load a file ?
1048 /* load 1st file specified on commandline */
1049 openlast = PREFS->loadlast;
1050 if (files != NULL)
1051 {
1052 if (g_file_test (files[0], G_FILE_TEST_EXISTS) != FALSE)
1053 {
1054 DB( g_print(" - should load %s\n", files[0] ) );
1055 hbfile_change_filepath(g_strdup(files[0]));
1056 ui_mainwindow_open_internal(mainwin, NULL);
1057 openlast = FALSE;
1058 }
1059 else
1060 {
1061 g_warning (_("Unable to open '%s', the file does not exist.\n"), files[0]);
1062
1063 }
1064 g_strfreev (files);
1065 }
1066
1067
1068 DB( g_print(" - GLOBALS->xhb_filepath: '%s'\n", GLOBALS->xhb_filepath ) );
1069
1070 if( openlast )
1071 {
1072 if( homebank_lastopenedfiles_load() == TRUE )
1073 ui_mainwindow_open_internal(mainwin, NULL);
1074 }
1075
1076 /* -- hack to generate a big file -- */
1077
1078
1079 /* update the mainwin display */
1080 ui_mainwindow_update(mainwin, GINT_TO_POINTER(UF_TITLE+UF_SENSITIVE+UF_BALANCE+UF_VISUAL));
1081
1082 ext_hook("enter_main_loop", NULL);
1083
1084 DB( g_print(" - gtk_main()\n" ) );
1085
1086 gtk_main ();
1087
1088 ext_hook("exit_main_loop", NULL);
1089 }
1090
1091 DB( g_print(" - unloading plugins\n") );
1092 ext_term();
1093
1094 }
1095
1096
1097 homebank_cleanup();
1098
1099 return EXIT_SUCCESS;
1100 }
1101
1102 #ifdef G_OS_WIN32
1103 /* In case we build this as a windowed application */
1104
1105 #ifdef __GNUC__
1106 #define _stdcall __attribute__((stdcall))
1107 #endif
1108
1109 int _stdcall
1110 WinMain (struct HINSTANCE__ *hInstance,
1111 struct HINSTANCE__ *hPrevInstance,
1112 char *lpszCmdLine,
1113 int nCmdShow)
1114 {
1115 return main (__argc, __argv);
1116 }
1117 #endif
1118
This page took 0.091544 seconds and 4 git commands to generate.