]> Dogcows Code - chaz/homebank/blobdiff - src/homebank.c
Merge branch 'master' into ext-perl
[chaz/homebank] / src / homebank.c
index 3b65ca1e57043713de1d2195a37be971e29c03ae..a1b9146f6368f1d229b7d980d8ce2e8c19bcdf6f 100644 (file)
@@ -1,5 +1,5 @@
 /*  HomeBank -- Free, easy, personal accounting for everyone.
- *  Copyright (C) 1995-2018 Maxime DOYEN
+ *  Copyright (C) 1995-2019 Maxime DOYEN
  *
  *  This file is part of HomeBank.
  *
@@ -19,8 +19,9 @@
 
 
 #include "homebank.h"
+#include "ext.h"
 
-#include "dsp_mainwindow.h"
+#include "dsp-mainwindow.h"
 #include "hb-preferences.h"
 #include "language.h"
 
@@ -55,6 +56,7 @@ static gchar *pixmaps_dir  = NULL;
 static gchar *locale_dir   = NULL;
 static gchar *help_dir     = NULL;
 static gchar *datas_dir    = NULL;
+static gchar *pkglib_dir   = NULL;
 
 
 //#define MARKUP_STRING "<span size='small'>%s</span>"
@@ -76,118 +78,6 @@ static GOptionEntry option_entries[] =
 };
 
 
-/*
-** try to determine the file type (if supported for import by homebank)
-**
-**
-*/
-gint homebank_alienfile_recognize(gchar *filename)
-{
-GIOChannel *io;
-gint i, retval = FILETYPE_UNKNOW;
-gchar *tmpstr;
-gint io_stat;
-GError *err = NULL;
-static gint csvtype[7] = {
-                                       CSV_DATE,
-                                       CSV_INT,
-                                       CSV_STRING,
-                                       CSV_STRING,
-                                       CSV_STRING,
-                                       CSV_DOUBLE,
-                                       CSV_STRING,
-                                       };
-
-
-       DB( g_print("\n[homebank] alienfile_recognize\n") );
-
-
-       io = g_io_channel_new_file(filename, "r", NULL);
-       if(io != NULL)
-       {
-               g_io_channel_set_encoding(io, NULL, NULL);      /* set to binary mode */
-
-               for(i=0;i<25;i++)
-               {
-                       if( retval != FILETYPE_UNKNOW )
-                               break;
-
-                       io_stat = g_io_channel_read_line(io, &tmpstr, NULL, NULL, &err);
-                       if( io_stat == G_IO_STATUS_EOF)
-                               break;
-                       if( io_stat == G_IO_STATUS_ERROR )
-                       {
-                               DB (g_print(" + ERROR %s\n",err->message));
-                               break;
-                       }
-                       if( io_stat == G_IO_STATUS_NORMAL)
-                       {
-                               if( *tmpstr != '\0' )
-                               {
-                                       DB( g_print(" line %d: '%s' retval=%d\n", i, tmpstr, retval) );
-
-                                       /* native homebank file */
-                                       if( g_str_has_prefix(tmpstr, "<homebank v="))
-                                       {
-                                               DB( g_print(" type is HomeBank\n") );
-                                               retval = FILETYPE_HOMEBANK;
-                                       }
-                                       else
-
-                                       // QIF file ?
-                                       if( g_str_has_prefix(tmpstr, "!Type") ||
-                                           g_str_has_prefix(tmpstr, "!type") ||
-                                           g_str_has_prefix(tmpstr, "!Option") ||
-                                           g_str_has_prefix(tmpstr, "!option") ||
-                                           g_str_has_prefix(tmpstr, "!Account") ||
-                                           g_str_has_prefix(tmpstr, "!account")
-                                         )
-                                       {
-                                               DB( g_print(" type is QIF\n") );
-                                               retval = FILETYPE_QIF;
-                                       }
-                                       else
-
-                                       /* is it OFX ? */
-                                       if( g_strstr_len(tmpstr, -1, "<OFX>") != NULL 
-                                        || g_strstr_len(tmpstr, -1, "<ofx>") != NULL
-                                        /*||   strcasestr(tmpstr, "<OFC>") != NULL*/
-                                         )
-                                       {
-                                               DB( g_print(" type is OFX\n") );
-                                               retval = FILETYPE_OFX;
-                                       }
-
-                                       /* is it csv homebank ? */
-                                       else
-                                       {
-                                       gchar **str_array;
-                                       gboolean isvalid = FALSE;
-
-                                               hb_string_strip_crlf(tmpstr);
-                                               str_array = hb_csv_row_get(tmpstr, ";", 8);
-                                               isvalid = hb_csv_row_valid(str_array, 8, csvtype);
-
-                                               DB( g_print(" hbcsv %d\n", isvalid) );
-
-                                               if( isvalid == TRUE  )
-                                               {
-                                                       DB( g_print(" type is CSV homebank\n") );
-                                                       retval = FILETYPE_CSV_HB;
-                                               }
-
-                                               g_strfreev (str_array);
-                                       }
-                                       g_free(tmpstr);
-                               }
-                       }
-               }
-               g_io_channel_unref (io);
-       }
-
-       return retval;
-}
-
 
 /* = = = = = = = = = = = = = = = = = = = = */
 
@@ -246,11 +136,15 @@ gboolean retval = FALSE;
 
        if( g_file_test(filepath, G_FILE_TEST_EXISTS) )
        {
+               DB( g_print(" - deleting: '%s'\n", filepath) );
                g_remove(filepath);
                retval = TRUE;
        }
-
-       DB( g_print(" - deleted: '%s' :: %d\n", filepath, retval) );
+       else
+       {
+               DB( g_print(" - cannot delete: '%s'\n", filepath) );
+       }
+       
        return retval;
 }
 
@@ -258,6 +152,8 @@ gboolean retval = FALSE;
 void homebank_backup_current_file(void)
 {
 gchar *bakfilename;
+GPtrArray *array;
+gint i;
 
        DB( g_print("\n[homebank] backup_current_file\n") );
 
@@ -269,6 +165,47 @@ gchar *bakfilename;
        //retval = g_rename(pathname, newname);
        homebank_file_copy (GLOBALS->xhb_filepath, bakfilename);
        g_free(bakfilename);
+
+       //do safe backup according to user preferences
+       DB( g_print(" user pref backup\n") );   
+       if( PREFS->bak_is_automatic == TRUE )
+       {
+               bakfilename = hb_filename_new_for_backup(GLOBALS->xhb_filepath);
+               if( g_file_test(bakfilename, G_FILE_TEST_EXISTS) == FALSE )
+               {
+                       homebank_file_copy (GLOBALS->xhb_filepath, bakfilename);
+               }
+               g_free(bakfilename);
+
+               //delete any offscale backup
+               DB( g_print(" clean old backup\n") );
+               array = hb_filename_backup_list(GLOBALS->xhb_filepath);
+
+               DB( g_print(" found %d match\n", array->len) );
+
+               gchar *dirname = g_path_get_dirname(GLOBALS->xhb_filepath);
+               
+               for(i=0;i<(gint)array->len;i++)
+               {
+               gchar *offscalefilename = g_ptr_array_index(array, i);
+       
+                       DB( g_print(" %d : '%s'\n", i, offscalefilename) );
+                       if( i >= PREFS->bak_max_num_copies )
+                       {
+                       gchar *bakdelfilepath = g_build_filename(dirname, offscalefilename, NULL);
+
+                               DB( g_print(" - should delete '%s'\n", bakdelfilepath) );
+                       
+                               homebank_file_delete_existing(bakdelfilepath);
+
+                               g_free(bakdelfilepath);
+                       }
+               }
+               g_ptr_array_free(array, TRUE);
+
+               g_free(dirname);
+       }
+
 }
 
 
@@ -355,6 +292,7 @@ gchar *homebank_lastopenedfiles_load(void)
 GKeyFile *keyfile;
 gchar *group, *filename, *tmpfilename;
 gchar *lastfilename = NULL;
+GError *error = NULL;
 
        DB( g_print("\n[homebank] lastopenedfiles load\n") );
 
@@ -362,7 +300,7 @@ gchar *lastfilename = NULL;
        if(keyfile)
        {
                filename = g_build_filename(homebank_app_get_config_dir(), "lastopenedfiles", NULL );
-               if(g_key_file_load_from_file (keyfile, filename, G_KEY_FILE_NONE, NULL))
+               if(g_key_file_load_from_file (keyfile, filename, G_KEY_FILE_NONE, &error))
                {
                        group = "HomeBank";
 
@@ -376,6 +314,13 @@ gchar *lastfilename = NULL;
                                }
                        }
                }
+
+               if( error )
+               {
+                       g_print("failed: %s\n", error->message);
+                       g_error_free (error);
+               }
+
                g_free(filename);
                g_key_file_free (keyfile);
        }
@@ -393,29 +338,41 @@ GKeyFile *keyfile;
 gboolean retval = FALSE;
 gchar *group, *filename;
 gsize length;
+GError *error = NULL;
 
        DB( g_print("\n[homebank] lastopenedfiles save\n") );
 
        if( GLOBALS->xhb_filepath != NULL )
        {
-               keyfile = g_key_file_new();
-               if(keyfile )
+               //don't save bakup files
+               if( hbfile_file_isbackup(GLOBALS->xhb_filepath) == FALSE )
                {
-                       DB( g_print(" - saving '%s'\n", GLOBALS->xhb_filepath) );
+                       keyfile = g_key_file_new();
+                       if(keyfile )
+                       {
+                               DB( g_print(" - saving '%s'\n", GLOBALS->xhb_filepath) );
 
-                       group = "HomeBank";
-                       g_key_file_set_string  (keyfile, group, "LastOpenedFile", GLOBALS->xhb_filepath);
+                               group = "HomeBank";
+                               g_key_file_set_string  (keyfile, group, "LastOpenedFile", GLOBALS->xhb_filepath);
+
+                               gchar *contents = g_key_file_to_data( keyfile, &length, NULL);
 
-                       gchar *contents = g_key_file_to_data( keyfile, &length, NULL);
+                               //DB( g_print(" keyfile:\n%s\nlen=%d\n", contents, length) );
 
-                       //DB( g_print(" keyfile:\n%s\nlen=%d\n", contents, length) );
+                               filename = g_build_filename(homebank_app_get_config_dir(), "lastopenedfiles", NULL );
 
-                       filename = g_build_filename(homebank_app_get_config_dir(), "lastopenedfiles", NULL );
-                       g_file_set_contents(filename, contents, length, NULL);
-                       g_free(filename);
+                               g_file_set_contents(filename, contents, length, &error);
+                               g_free(filename);
 
-                       g_free(contents);
-                       g_key_file_free (keyfile);
+                               if( error )
+                               {
+                                       g_print("failed: %s\n", error->message);
+                                       g_error_free (error);
+                               }
+                               
+                               g_free(contents);
+                               g_key_file_free (keyfile);
+                       }
                }
        }
 
@@ -563,6 +520,12 @@ homebank_app_get_datas_dir (void)
        return datas_dir;
 }
 
+const gchar *
+homebank_app_get_pkglib_dir (void)
+{
+       return pkglib_dir;
+}
+
 
 /* build package paths at runtime */
 static void
@@ -579,6 +542,7 @@ build_package_paths (void)
        pixmaps_dir  = g_build_filename (prefix, "share", PACKAGE, "icons", NULL);
        help_dir     = g_build_filename (prefix, "share", PACKAGE, "help", NULL);
        datas_dir    = g_build_filename (prefix, "share", PACKAGE, "datas", NULL);
+       pkglib_dir   = g_build_filename (prefix, "lib", PACKAGE, NULL);
        #ifdef PORTABLE_APP
                DB( g_print(" - app is portable under windows\n") );
                config_dir   = g_build_filename(prefix, "config", NULL);
@@ -592,6 +556,7 @@ build_package_paths (void)
        pixmaps_dir  = g_build_filename (DATA_DIR, PACKAGE, "icons", NULL);
        help_dir     = g_build_filename (DATA_DIR, PACKAGE, "help", NULL);
        datas_dir    = g_build_filename (DATA_DIR, PACKAGE, "datas", NULL);
+       pkglib_dir   = g_build_filename (PKGLIB_DIR, NULL);
        config_dir   = g_build_filename(g_get_user_config_dir(), HB_DATA_PATH, NULL);
 
        //#870023 Ubuntu packages the help files in "/usr/share/doc/homebank-data/help/" for some strange reason
@@ -608,6 +573,7 @@ build_package_paths (void)
        DB( g_print(" - locale_dir : %s\n", locale_dir) );
        DB( g_print(" - help_dir   : %s\n", help_dir) );
        DB( g_print(" - datas_dir  : %s\n", datas_dir) );
+       DB( g_print(" - pkglib_dir : %s\n", pkglib_dir) );
 
 }
 
@@ -749,6 +715,7 @@ static void homebank_cleanup()
        g_free (pixmaps_dir);
        g_free (locale_dir);
        g_free (help_dir);
+       g_free (pkglib_dir);
 
 }
 
@@ -879,7 +846,7 @@ homebank_init_i18n (void)
 
 
 int
-main (int argc, char *argv[])
+main (int argc, char *argv[], char *env[])
 {
 GOptionContext *option_context;
 GOptionGroup *option_group;
@@ -951,6 +918,22 @@ gboolean openlast;
                /*  change the locale if a language is specified  */
                language_init (PREFS->language);
 
+               DB( g_print(" - loading plugins\n") );
+               ext_init(&argc, &argv, &env);
+
+               GList* it;
+               for (it = PREFS->ext_whitelist; it; it = g_list_next(it)) {
+                       ext_load_plugin(it->data);
+               }
+
+               gchar** plugins = ext_list_plugins();
+               gchar** plugins_it;
+               for (plugins_it = plugins; *plugins_it; ++plugins_it) {
+                       gboolean loaded = ext_is_plugin_loaded(*plugins_it);
+                       g_print("found plugin: %s, loaded: %d\n", *plugins_it, loaded);
+               }
+               g_strfreev(plugins);
+
                if( PREFS->showsplash == TRUE )
                {
                        splash = homebank_construct_splash();
@@ -969,6 +952,9 @@ gboolean openlast;
 
                mainwin = (GtkWidget *)create_hbfile_window (NULL);
 
+               GValue mainwin_val = G_VALUE_INIT;
+               ext_hook("create_main_window", EXT_OBJECT(&mainwin_val, mainwin), NULL);
+
                if(mainwin)
                {
 
@@ -1073,13 +1059,20 @@ nobak:
                        /* update the mainwin display */
                        ui_mainwindow_update(mainwin, GINT_TO_POINTER(UF_TITLE+UF_SENSITIVE+UF_BALANCE+UF_VISUAL));
 
+                       ext_hook("enter_main_loop", NULL);
+
                        DB( g_print(" - gtk_main()\n" ) );
                        gtk_main ();
        
+                       ext_hook("exit_main_loop", NULL);
+
                        DB( g_print(" - call destroy mainwin\n" ) );
                        gtk_widget_destroy(mainwin);
                }
 
+               DB( g_print(" - unloading plugins\n") );
+               ext_term();
+
        }
 
 
@@ -1089,7 +1082,7 @@ nobak:
 }
 
 #ifdef G_OS_WIN32
-/* In case we build this as a windowed application */
+/* In case we build this as a windows application */
 
 #ifdef __GNUC__
 #define _stdcall  __attribute__((stdcall))
This page took 0.029983 seconds and 4 git commands to generate.