]> git.deb.at Git - pkg/abook.git/blobdiff - database.c
Imported Upstream version 0.5.5
[pkg/abook.git] / database.c
index 07fe9abb7393d7d04b42d63af9ed0d6798ee2823..cabd3305f2c0079a4e2387d74c44ab59b885b9e6 100644 (file)
@@ -1,6 +1,6 @@
 
 /*
- * $Id: database.c,v 1.27 2005/08/13 10:49:25 jheinonen Exp $
+ * $Id: database.c,v 1.29.2.1 2005/10/26 19:45:23 jheinonen Exp $
  *
  * by JH <jheinonen@users.sourceforge.net>
  *
@@ -15,6 +15,7 @@
 #include "abook.h"
 #include <assert.h>
 #include "database.h"
+#include "gettext.h"
 #include "list.h"
 #include "misc.h"
 #include "options.h"
@@ -53,26 +54,26 @@ extern char *datafile;
  */
 
 struct abook_field abook_fields[ITEM_FIELDS] = {
-       {"Name",        "name",         TAB_CONTACT},/* NAME */
-       {"E-mails",     "email",        TAB_CONTACT},/* EMAIL */
-       {"Address",     "address",      TAB_ADDRESS},/* ADDRESS */
-       {"Address2",    "address2",     TAB_ADDRESS},/* ADDRESS2 */
-       {"City",        "city",         TAB_ADDRESS},/* CITY */
-       {"State/Province","state",      TAB_ADDRESS},/* STATE */
-       {"ZIP/Postal Code","zip",       TAB_ADDRESS},/* ZIP */
-       {"Country",     "country",      TAB_ADDRESS},/* COUNTRY */
-       {"Home Phone",  "phone",        TAB_PHONE},/* PHONE */
-       {"Work Phone",  "workphone",    TAB_PHONE},/* WORKPHONE */
-       {"Fax",         "fax",          TAB_PHONE},/* FAX */
-       {"Mobile",      "mobile",       TAB_PHONE},/* MOBILEPHONE */
-       {"Nickname/Alias", "nick",      TAB_OTHER},/* NICK */
-       {"URL",         "url",          TAB_OTHER},/* URL */
-       {"Notes",       "notes",        TAB_OTHER},/* NOTES */
-       {"Custom1",     "custom1",      TAB_CUSTOM},/* CUSTOM1 */
-       {"Custom2",     "custom2",      TAB_CUSTOM},/* CUSTOM2 */
-       {"Custom3",     "custom3",      TAB_CUSTOM},/* CUSTOM3 */
-       {"Custom4",     "custom4",      TAB_CUSTOM},/* CUSTOM4 */
-       {"Custom5",     "custom5",      TAB_CUSTOM},/* CUSTOM5 */
+       {N_("Name"),    "name",         TAB_CONTACT},/* NAME */
+       {N_("E-mails"), "email",        TAB_CONTACT},/* EMAIL */
+       {N_("Address"), "address",      TAB_ADDRESS},/* ADDRESS */
+       {N_("Address2"),        "address2",     TAB_ADDRESS},/* ADDRESS2 */
+       {N_("City"),    "city",         TAB_ADDRESS},/* CITY */
+       {N_("State/Province"),"state",  TAB_ADDRESS},/* STATE */
+       {N_("ZIP/Postal Code"),"zip",   TAB_ADDRESS},/* ZIP */
+       {N_("Country"), "country",      TAB_ADDRESS},/* COUNTRY */
+       {N_("Home Phone"),      "phone",        TAB_PHONE},/* PHONE */
+       {N_("Work Phone"),      "workphone",    TAB_PHONE},/* WORKPHONE */
+       {N_("Fax"),             "fax",          TAB_PHONE},/* FAX */
+       {N_("Mobile"),  "mobile",       TAB_PHONE},/* MOBILEPHONE */
+       {N_("Nickname/Alias"), "nick",      TAB_OTHER},/* NICK */
+       {N_("URL"),             "url",          TAB_OTHER},/* URL */
+       {N_("Notes"),   "notes",        TAB_OTHER},/* NOTES */
+       {N_("Custom1"), "custom1",      TAB_CUSTOM},/* CUSTOM1 */
+       {N_("Custom2"), "custom2",      TAB_CUSTOM},/* CUSTOM2 */
+       {N_("Custom3"), "custom3",      TAB_CUSTOM},/* CUSTOM3 */
+       {N_("Custom4"), "custom4",      TAB_CUSTOM},/* CUSTOM4 */
+       {N_("Custom5"), "custom5",      TAB_CUSTOM},/* CUSTOM5 */
 };
 
 
@@ -188,23 +189,41 @@ int
 save_database()
 {
        FILE *out;
+       int ret = 0;
        struct db_enumerator e = init_db_enumerator(ENUM_ALL);
+       char *datafile_new = strconcat(datafile, ".new", NULL);
+       char *datafile_old = strconcat(datafile, "~", NULL);
 
-       if( (out = abook_fopen(datafile, "w")) == NULL )
-               return -1;
+       if( (out = abook_fopen(datafile_new, "w")) == NULL ) {
+               ret = -1;
+               goto out;
+       }
 
        if(list_is_empty()) {
                fclose(out);
                unlink(datafile);
-               return 1;
+               ret = 1;
+               goto out;
        }
 
-
+       /*
+        * Possibly should check if write_database failed.
+        * Currently it returns always zero.
+        */
        write_database(out, e);
-
        fclose(out);
 
-       return 0;
+       if(access(datafile, F_OK) == 0 &&
+                       (rename(datafile, datafile_old)) == -1)
+               ret = -1;
+       
+       if((rename(datafile_new, datafile)) == -1)
+               ret = -1;
+
+out:
+       free(datafile_new);
+       free(datafile_old);
+       return ret;
 }
 
 static void
@@ -402,8 +421,8 @@ sort_by_field(int field)
        if(field < 0) {
                field = name2field(opt_get_str(STR_SORT_FIELD));
                if(field < 0) {
-                       statusline_msg("Not valid field value defined "
-                               "in configuration");
+                       statusline_msg(_("Invalid field value defined "
+                               "in configuration"));
                        return;
                }
        }