2007-11-09 [colin] 3.0.2cvs129
[claws.git] / src / addressbook.c
index 2eaadfdf84e36ac9e81f0fd880d3237d90d40e61..f308bcc386ebd0772e180f148f9c893ee9f8f943 100644 (file)
@@ -44,6 +44,8 @@
 #include <gtk/gtkitemfactory.h>
 #include <string.h>
 #include <setjmp.h>
+#include <sys/types.h>
+#include <dirent.h>
 
 #include "main.h"
 #include "addressbook.h"
@@ -549,7 +551,7 @@ static ErrMsgTableEntry _lutErrorsLDAP_[] = {
        { LDAPRC_NOENTRIES,                     N_("No LDAP entries found for search criteria") },
        { LDAPRC_STOP_FLAG,                     N_("LDAP search terminated on request") },
        { LDAPRC_TLS,                           N_("Error starting TLS connection") },
-       { LDAPRC_NODN,                          N_("Distinguised Name (dn) is missing") },
+       { LDAPRC_NODN,                          N_("Distinguished Name (dn) is missing") },
        { LDAPRC_NAMING_VIOLATION,              N_("Missing required information") },
        { LDAPRC_ALREADY_EXIST,                 N_("Another contact exists with that key") },
        { LDAPRC_STRONG_AUTH,                   N_("Strong(er) authentication required") },
@@ -1450,6 +1452,8 @@ static void addressbook_del_clicked(GtkButton *button, gpointer data)
                        item = node->data;
                        node = g_list_next( node );
                        aio = ( AddrItemObject * ) item->addressItem;
+                       if (!aio)
+                               continue;
                        if( aio->type == ADDR_ITEM_GROUP ) {
                                ItemGroup *item = ( ItemGroup * ) aio;
                                GtkCTreeNode *nd = NULL;
@@ -1478,6 +1482,10 @@ static void addressbook_del_clicked(GtkButton *button, gpointer data)
                                }
 #endif
                                if( item ) {
+                                       gchar *filename = addritem_person_get_picture(item);
+                                       if (filename && is_file_exist(filename))
+                                               g_unlink(filename);
+                                       g_free(filename);
                                        addritem_free_item_person( item );
                                }
                        }
@@ -3191,7 +3199,10 @@ static void addressbook_move_nodes_up( GtkCTree *ctree, GtkCTreeNode *node ) {
 static void addressbook_edit_address_post_cb( ItemPerson *person )
 {
        if( person ) {
-
+#ifdef USE_LDAP
+               if (strcmp2(person->nickName, ADDRITEM_NAME(person)))
+                       addritem_person_set_nick_name( person, ADDRITEM_NAME(person));
+#endif
                addressbook_folder_refresh_one_person( GTK_CTREE(addrbook.clist), person );
                invalidate_address_completion();
        }
@@ -3915,9 +3926,61 @@ static gboolean addressbook_convert( AddressIndex *addrIndex ) {
        return retVal;
 }
 
+static gboolean migrate_addrbook(const gchar *origdir, const gchar *destdir)
+{
+       DIR *dp;
+       struct dirent *d;
+       gboolean failed = FALSE;
+
+       if( ( dp = opendir( origdir ) ) == NULL ) {
+               return FALSE;
+       }
+       
+       while( ( d = readdir( dp ) ) != NULL ) {
+               if (strncmp(d->d_name, "addrbook-", strlen("addrbook-")))
+                       continue;
+               else {
+                       gchar *orig_file = g_strconcat(origdir, G_DIR_SEPARATOR_S, 
+                                       d->d_name, NULL);
+                       gchar *dest_file = g_strconcat(destdir, G_DIR_SEPARATOR_S, 
+                                       d->d_name, NULL);
+                       if (copy_file(orig_file, dest_file, FALSE) < 0) {
+                               failed = TRUE;
+                       }
+                       g_free(orig_file);
+                       g_free(dest_file);
+                       if (failed) {
+                               break;
+                       }
+               }
+       }
+
+       closedir( dp );
+       if (!failed) {
+               /* all copies succeeded, we can remove source files */
+               if( ( dp = opendir( origdir ) ) == NULL ) {
+                       return FALSE;
+               }
+               while( ( d = readdir( dp ) ) != NULL ) {
+                       if (strncmp(d->d_name, "addrbook-", strlen("addrbook-")))
+                               continue;
+                       else {
+                               gchar *orig_file = g_strconcat(origdir, G_DIR_SEPARATOR_S, 
+                                               d->d_name, NULL);
+                               g_unlink(orig_file);
+                               g_free(orig_file);
+                       }
+               }
+               closedir( dp );
+       }
+       
+       return !failed;
+}
+
 void addressbook_read_file( void ) {
        AddressIndex *addrIndex = NULL;
-
+       gchar *indexdir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ADDRBOOK_DIR, NULL);
+       
        debug_print( "Reading address index...\n" );
        if( _addressIndex_ ) {
                debug_print( "address book already read!!!\n" );
@@ -3928,7 +3991,24 @@ void addressbook_read_file( void ) {
        addrindex_initialize();
 
        /* Use new address book index. */
-       addrindex_set_file_path( addrIndex, get_rc_dir() );
+       
+       if ( !is_dir_exist(indexdir) ) {
+               if ( make_dir(indexdir) < 0 ) {
+                       addrindex_set_file_path( addrIndex, get_rc_dir() );
+                       g_warning( "couldn't create dir %s\n", indexdir);
+               } else {
+                       if (!migrate_addrbook(get_rc_dir(), indexdir)) {
+                               remove_dir_recursive(indexdir);
+                               addrindex_set_file_path( addrIndex, get_rc_dir() );
+                               g_error("couldn't migrate dir %s", indexdir);
+                       } else {
+                               addrindex_set_file_path( addrIndex, indexdir);
+                       }
+               }
+       } else {
+               addrindex_set_file_path( addrIndex, indexdir);
+       }
+       g_free(indexdir);
        addrindex_set_file_name( addrIndex, ADDRESSBOOK_INDEX_FILE );
        addrindex_read_data( addrIndex );
        if( addrIndex->retVal == MGU_NO_FILE ) {
@@ -4583,7 +4663,7 @@ static void addrbookctl_build_map( GtkWidget *window ) {
        atci->showInTree = FALSE;
        atci->treeExpand = FALSE;
        atci->treeLeaf = TRUE;
-       atci->displayName = _( "EMail Address" );
+       atci->displayName = _( "Email Address" );
        atci->iconXpm = addressxpm;
        atci->maskXpm = addressxpmmask;
        atci->iconXpmOpen = addressxpm;
@@ -4872,10 +4952,11 @@ static void addrbookctl_build_ifselect( void ) {
  * This function is used by the Add sender to address book function.
  */
 gboolean addressbook_add_contact(
-               const gchar *name, const gchar *address, const gchar *remarks )
+               const gchar *name, const gchar *address, const gchar *remarks,
+               GdkPixbuf *picture )
 {
        debug_print( "addressbook_add_contact: name/address: %s - %s\n", name, address );
-       if( addressadd_selection( _addressIndex_, name, address, remarks ) ) {
+       if( addressadd_selection( _addressIndex_, name, address, remarks, picture ) ) {
                debug_print( "addressbook_add_contact - added\n" );
                addressbook_refresh();
        }
@@ -5350,11 +5431,11 @@ static void addressbook_drag_data_get(GtkWidget        *widget,
                if( ds && ds->interface && ds->interface->readOnly)
                        gtk_selection_data_set(selection_data,
                                       selection_data->target, 8,
-                                      "Dummy_addr_copy", 15);
+                                      (const guchar *)"Dummy_addr_copy", 15);
                else
                        gtk_selection_data_set(selection_data,
                                       selection_data->target, 8,
-                                      "Dummy_addr_move", 15);
+                                      (const guchar *)"Dummy_addr_move", 15);
        } 
 }