]> Dogcows Code - chaz/homebank/blobdiff - src/homebank.c
import homebank-5.2.6
[chaz/homebank] / src / homebank.c
index 421d0eff7a45fe62367dad4fc9493503a1e43af9..a80d305ca87e293eb4b7a890be911b648b4f06f2 100644 (file)
@@ -1,5 +1,5 @@
 /*  HomeBank -- Free, easy, personal accounting for everyone.
 /*  HomeBank -- Free, easy, personal accounting for everyone.
- *  Copyright (C) 1995-2016 Maxime DOYEN
+ *  Copyright (C) 1995-2019 Maxime DOYEN
  *
  *  This file is part of HomeBank.
  *
  *
  *  This file is part of HomeBank.
  *
  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+
 #include "homebank.h"
 
 #include "homebank.h"
 
-#include "dsp_mainwindow.h"
+#include "dsp-mainwindow.h"
 #include "hb-preferences.h"
 #include "language.h"
 
 
 #include "hb-preferences.h"
 #include "language.h"
 
 
-
 #ifdef G_OS_WIN32
 #include <windows.h>
 #endif
 
 #define APPLICATION_NAME "HomeBank"
 
 #ifdef G_OS_WIN32
 #include <windows.h>
 #endif
 
 #define APPLICATION_NAME "HomeBank"
 
+
 /****************************************************************************/
 /* Debug macros                                                             */
 /****************************************************************************/
 /****************************************************************************/
 /* Debug macros                                                             */
 /****************************************************************************/
@@ -75,115 +76,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<4;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, 10, "OFX") != 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;
-}
-
 
 /* = = = = = = = = = = = = = = = = = = = = */
 
 
 /* = = = = = = = = = = = = = = = = = = = = */
 
@@ -199,7 +91,7 @@ void homebank_file_ensure_xhb(gchar *filename)
        {
        gchar *newfilename;
        
        {
        gchar *newfilename;
        
-               newfilename = hb_util_filename_new_with_extension(filename, "xhb");
+               newfilename = hb_filename_new_with_extension(filename, "xhb");
                hbfile_change_filepath(newfilename);
                DB( g_print(" - changed to: '%s'\n", GLOBALS->xhb_filepath) );
        }
                hbfile_change_filepath(newfilename);
                DB( g_print(" - changed to: '%s'\n", GLOBALS->xhb_filepath) );
        }
@@ -211,13 +103,15 @@ void homebank_file_ensure_xhb(gchar *filename)
 }
 
 
 }
 
 
-static gboolean homebank_copy_file(gchar *srcfile, gchar *dstfile)
+static gboolean homebank_file_copy(gchar *srcfile, gchar *dstfile)
 {
 gchar *buffer;
 gsize length;
 //GError *error = NULL;
 gboolean retval = FALSE;
 
 {
 gchar *buffer;
 gsize length;
 //GError *error = NULL;
 gboolean retval = FALSE;
 
+       DB( g_print("\n[homebank] file copy\n") );
+
        if (g_file_get_contents (srcfile, &buffer, &length, NULL))
        {
                if(g_file_set_contents(dstfile, buffer, length, NULL))
        if (g_file_get_contents (srcfile, &buffer, &length, NULL))
        {
                if(g_file_set_contents(dstfile, buffer, length, NULL))
@@ -226,32 +120,93 @@ gboolean retval = FALSE;
                }
                g_free(buffer);
        }
                }
                g_free(buffer);
        }
+
+       DB( g_print(" - copied '%s' => '%s' :: %d\n", srcfile, dstfile, retval) );
        return retval;
 }
 
 
        return retval;
 }
 
 
-void homebank_backup_current_file(void)
+static gboolean homebank_file_delete_existing(gchar *filepath)
 {
 {
-gchar *bakfilename;
+gboolean retval = FALSE;
 
 
-       DB( g_print("\n[homebank] backup_current_file\n") );
+       DB( g_print("\n[homebank] file delete existing\n") );
 
 
-       bakfilename = hb_util_filename_new_with_extension (GLOBALS->xhb_filepath, "xhb~");
-       if( g_file_test(bakfilename, G_FILE_TEST_EXISTS) )
+       if( g_file_test(filepath, G_FILE_TEST_EXISTS) )
+       {
+               DB( g_print(" - deleting: '%s'\n", filepath) );
+               g_remove(filepath);
+               retval = TRUE;
+       }
+       else
        {
        {
-               DB( g_print(" - delete existing: '%s'\n", bakfilename) );
-               g_remove(bakfilename);
+               DB( g_print(" - cannot delete: '%s'\n", filepath) );
        }
        }
+       
+       return retval;
+}
+
+
+void homebank_backup_current_file(void)
+{
+gchar *bakfilename;
+GPtrArray *array;
+gint i;
 
 
-       DB( g_print(" - copy '%s' => '%s'\n", GLOBALS->xhb_filepath, bakfilename) );
+       DB( g_print("\n[homebank] backup_current_file\n") );
 
 
+       //do normal linux backup file
+       DB( g_print(" normal backup with ~\n") );
+       bakfilename = hb_filename_new_with_extension (GLOBALS->xhb_filepath, "xhb~");
+       homebank_file_delete_existing(bakfilename);
        //#512046 copy file not to broke potential links
        //retval = g_rename(pathname, newname);
        //#512046 copy file not to broke potential links
        //retval = g_rename(pathname, newname);
-       homebank_copy_file (GLOBALS->xhb_filepath, bakfilename);
-       
+       homebank_file_copy (GLOBALS->xhb_filepath, bakfilename);
        g_free(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);
+       }
+
 }
 
 }
 
+
 /* = = = = = = = = = = = = = = = = = = = = */
 /* url open */
 
 /* = = = = = = = = = = = = = = = = = = = = */
 /* url open */
 
@@ -330,53 +285,45 @@ homebank_util_url_show (const gchar *url)
 /*
 ** load lastopenedfiles from homedir/.homebank
 */
 /*
 ** load lastopenedfiles from homedir/.homebank
 */
-gboolean homebank_lastopenedfiles_load(void)
+gchar *homebank_lastopenedfiles_load(void)
 {
 GKeyFile *keyfile;
 {
 GKeyFile *keyfile;
-gboolean retval = FALSE;
-gchar *group, *filename, *lastfilename;
+gchar *group, *filename, *tmpfilename;
+gchar *lastfilename = NULL;
+GError *error = NULL;
 
        DB( g_print("\n[homebank] lastopenedfiles load\n") );
 
        keyfile = g_key_file_new();
        if(keyfile)
        {
 
        DB( g_print("\n[homebank] lastopenedfiles load\n") );
 
        keyfile = g_key_file_new();
        if(keyfile)
        {
-
                filename = g_build_filename(homebank_app_get_config_dir(), "lastopenedfiles", NULL );
                filename = g_build_filename(homebank_app_get_config_dir(), "lastopenedfiles", NULL );
-
-               DB( g_print(" - filename: %s\n", filename) );
-
-               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";
 
                {
                        group = "HomeBank";
 
-            DB( g_print(" - load keyfile ok\n") );
-
                        if(g_key_file_has_key(keyfile, group, "LastOpenedFile", NULL))
                        {
                        if(g_key_file_has_key(keyfile, group, "LastOpenedFile", NULL))
                        {
-                DB( g_print(" - keyfile has key ok\n") );
-
-                               lastfilename = g_key_file_get_string  (keyfile, group, "LastOpenedFile", NULL);
-
-                               DB( g_print(" - lastfile loaded: %s\n", lastfilename ) );
+                               tmpfilename = g_key_file_get_string  (keyfile, group, "LastOpenedFile", NULL);
                                // #593082
                                // #593082
-                               if (g_file_test (lastfilename, G_FILE_TEST_EXISTS) != FALSE)
+                               if (g_file_test (tmpfilename, G_FILE_TEST_EXISTS) != FALSE)
                                {
                                {
-                                       DB( g_print(" - file exists\n") );
-
-                                       hbfile_change_filepath(lastfilename);
-
-                                       retval = TRUE;
+                                       lastfilename = tmpfilename;
                                }
                        }
                }
                                }
                        }
                }
+
+               if( error )
+               {
+                       g_print("failed: %s\n", error->message);
+                       g_error_free (error);
+               }
+
                g_free(filename);
                g_key_file_free (keyfile);
        }
 
                g_free(filename);
                g_key_file_free (keyfile);
        }
 
-       DB( g_print(" - return: %d\n", retval) );
-
-       return retval;
+       return lastfilename;
 }
 
 
 }
 
 
@@ -389,29 +336,41 @@ GKeyFile *keyfile;
 gboolean retval = FALSE;
 gchar *group, *filename;
 gsize length;
 gboolean retval = FALSE;
 gchar *group, *filename;
 gsize length;
+GError *error = NULL;
 
        DB( g_print("\n[homebank] lastopenedfiles save\n") );
 
        if( GLOBALS->xhb_filepath != 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 );
-                       g_file_set_contents(filename, contents, length, NULL);
-                       g_free(filename);
+                               filename = g_build_filename(homebank_app_get_config_dir(), "lastopenedfiles", NULL );
 
 
-                       g_free(contents);
-                       g_key_file_free (keyfile);
+                               g_file_set_contents(filename, contents, length, &error);
+                               g_free(filename);
+
+                               if( error )
+                               {
+                                       g_print("failed: %s\n", error->message);
+                                       g_error_free (error);
+                               }
+                               
+                               g_free(contents);
+                               g_key_file_free (keyfile);
+                       }
                }
        }
 
                }
        }
 
@@ -1038,19 +997,42 @@ gboolean openlast;
 
                        if( openlast )
                        {
 
                        if( openlast )
                        {
-                               if( homebank_lastopenedfiles_load() == TRUE )
+                       gchar *lastfilepath;
+                       
+                               lastfilepath = homebank_lastopenedfiles_load();
+                               if( lastfilepath != NULL )
+                               {
+                                       //#1710955 test for backup open
+                                       if( hbfile_file_isbackup(lastfilepath) )
+                                       {
+                                               if( ui_mainwindow_open_backup_check_confirm(lastfilepath) == TRUE )
+                                               {
+                                                       GLOBALS->hbfile_is_bak = TRUE;
+                                               }
+                                               else
+                                               {
+                                                       g_free(lastfilepath);
+                                                       goto nobak;
+                                               }
+                                       }
+
+                                       hbfile_change_filepath(lastfilepath);
                                        ui_mainwindow_open_internal(mainwin, NULL);
                                        ui_mainwindow_open_internal(mainwin, NULL);
+
+                               }
                        }
 
                        /* -- hack to generate a big file -- */
                        }
 
                        /* -- hack to generate a big file -- */
-
+nobak:
 
                        /* update the mainwin display */
                        ui_mainwindow_update(mainwin, GINT_TO_POINTER(UF_TITLE+UF_SENSITIVE+UF_BALANCE+UF_VISUAL));
 
 
                        /* update the mainwin display */
                        ui_mainwindow_update(mainwin, GINT_TO_POINTER(UF_TITLE+UF_SENSITIVE+UF_BALANCE+UF_VISUAL));
 
-               DB( g_print(" - gtk_main()\n" ) );
-
+                       DB( g_print(" - gtk_main()\n" ) );
                        gtk_main ();
                        gtk_main ();
+       
+                       DB( g_print(" - call destroy mainwin\n" ) );
+                       gtk_widget_destroy(mainwin);
                }
 
        }
                }
 
        }
@@ -1062,7 +1044,7 @@ gboolean openlast;
 }
 
 #ifdef G_OS_WIN32
 }
 
 #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))
 
 #ifdef __GNUC__
 #define _stdcall  __attribute__((stdcall))
This page took 0.028883 seconds and 4 git commands to generate.