]> git.deb.at Git - pkg/abook.git/commitdiff
Attempt to improve --datafile behavior
authorJaakko Heinonen <jheinonen@users.sourceforge.net>
Thu, 7 Feb 2002 12:33:13 +0000 (12:33 +0000)
committerJaakko Heinonen <jheinonen@users.sourceforge.net>
Thu, 7 Feb 2002 12:33:13 +0000 (12:33 +0000)
ChangeLog
abook.c
database.c
ui.c

index ea55e01543a3efc96c9c7da2400d5ca6dc837461..2b65755e37dc080cab3ef2eee86aa93bc0efb9f8 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,6 @@
 2002-xx-xx
  - abook can be compiled with g++
+ - attempt to improve --datafile behavior
 
 2002-02-04
  - use getopt to parse command line (also new options added)
diff --git a/abook.c b/abook.c
index 7b119b9b6c8b9174fbd0316e1660ede2c0881450..45eb83d54bc28abf0aadb95f97b66e4ec1de168d 100644 (file)
--- a/abook.c
+++ b/abook.c
@@ -33,7 +33,6 @@
 static void             init_abook();
 static void            quit_abook_sig(int i);
 static void             set_filenames();
-static void            free_filenames();
 static void             parse_command_line(int argc, char **argv);
 static void             show_usage();
 static void             mutt_query(char *str);
@@ -45,6 +44,24 @@ static void          add_email(int);
 char *datafile = NULL;
 char *rcfile = NULL;
 
+int alternative_datafile = FALSE;
+int alternative_rcfile = FALSE;
+
+static int
+datafile_writeable()
+{
+       FILE *f;
+
+       assert(datafile != NULL);
+
+       if( (f = fopen(datafile, "a")) == NULL)
+               return FALSE;
+
+       fclose(f);
+
+       return TRUE;
+}
+
 static void
 init_abook()
 {
@@ -59,26 +76,21 @@ init_abook()
        
        umask(DEFAULT_UMASK);
 
-       /*
-        * this is very ugly for now
-        */
-       /*if( options_get_int("datafile", "autosave") )*/
-
-       if( load_database(datafile) == 2 ) {
-               char *tmp = strconcat(getenv("HOME"),
-                               "/" DATAFILE, NULL);
-
-               if( safe_strcmp(tmp, datafile) ) {
-                       refresh_screen();
-                       statusline_msg("Sorry, the specified file does "
-                               "not appear to be a valid abook addressbook");
-                       statusline_msg("Will open default addressbook...");
-                       free(datafile);
-                       datafile = tmp;
-                       load_database(datafile);
-               } else
-                       free(tmp);
-       }
+       if(!datafile_writeable()) {
+               char *s = mkstr("File %s is not writeable", datafile);
+               refresh_screen();
+               statusline_msg(s);
+               free(s);
+               if(load_database(datafile) || !statusline_ask_boolean(
+                                       "If you continue all changes will "
+                               "be lost. Do you want to continue?", FALSE)) {
+                       close_config();
+                       close_database();
+                       close_ui();
+                       exit(1);
+               }
+       } else
+               load_database(datafile);
 
        refresh_screen();
 }
@@ -123,6 +135,14 @@ main(int argc, char **argv)
        return 0;
 }
 
+static void
+free_filenames()
+{
+       my_free(rcfile);
+       my_free(datafile);
+}
+
+
 static void
 set_filenames()
 {
@@ -142,13 +162,6 @@ set_filenames()
        atexit(free_filenames);
 }
 
-static void
-free_filenames()
-{
-       my_free(rcfile);
-       my_free(datafile);
-}
-
 /*
  * CLI
  */
@@ -261,6 +274,7 @@ parse_command_line(int argc, char **argv)
                                break;
                        case 'f':
                                set_filename(&datafile, optarg);
+                               alternative_datafile = TRUE;
                                break;
                        case OPT_MUTT_QUERY:
                                query_string = optarg;
@@ -268,6 +282,7 @@ parse_command_line(int argc, char **argv)
                                break;
                        case 'C':
                                set_filename(&rcfile, optarg);
+                               alternative_rcfile = TRUE;
                                break;
                        case OPT_CONVERT:
                                change_mode(&mode, MODE_CONVERT);
index 2a5ce8be107f8479ec7e07784259f51cf008c35b..8b22602e03b6a2a60121904a6cf2ae05f2fb22ad 100644 (file)
@@ -136,10 +136,7 @@ load_database(char *filename)
        
        parse_database(in);
 
-       if ( items == 0 )
-               return 2;
-
-       return 0;
+       return (items == 0) ? 2 : 0;
 }
 
 int
diff --git a/ui.c b/ui.c
index 695577e5d7fcd560b632e38fb73815698eac6ecb..1630a82f8545bff46712bf720ae1a0be56208c5d 100644 (file)
--- a/ui.c
+++ b/ui.c
@@ -44,6 +44,8 @@
 extern int items, curitem;
 extern char *datafile;
 
+extern int alternative_datafile;
+
 /*
  * internal variables
  */
@@ -629,4 +631,6 @@ ui_open_datafile()
 
        refresh_screen();
        free(filename);
+
+       alternative_datafile = TRUE;
 }