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