2007-10-10 [colin] 3.0.2cvs55
[claws.git] / src / editaddress.c
index b182b181620b6cfdd923e1a3ecec0af5fd2dd78c..ef17f600cd5fba48cf2f4f0badfd8c5b546b241d 100644 (file)
@@ -33,6 +33,7 @@
 #include <gtk/gtkentry.h>
 #include <gtk/gtktable.h>
 
+#include "alertpanel.h"
 #include "mgutils.h"
 #include "addressbook.h"
 #include "addressitem.h"
@@ -699,10 +700,11 @@ void addressbook_edit_person_widgetset_hide( void )
 
 static void addressbook_edit_person_unset_picture (void *obj, guint action, void *data)
 {
-       personeditdlg.pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 48, 48);
-       gdk_pixbuf_fill(personeditdlg.pixbuf, 0xffffff00);
+       GdkPixbuf *pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 48, 48);
+       gdk_pixbuf_fill(pixbuf, 0xffffff00);
        personeditdlg.picture_set = FALSE;
-       gtk_image_set_from_pixbuf(GTK_IMAGE(personeditdlg.image), personeditdlg.pixbuf);
+       gtk_image_set_from_pixbuf(GTK_IMAGE(personeditdlg.image), pixbuf);
+       g_object_unref(pixbuf);
 }
 
 static GtkItemFactoryEntry editaddr_popup_entries[] =
@@ -719,11 +721,8 @@ static void addressbook_edit_person_set_picture(GtkWidget *widget,
        
        if (event->button == 1) {
                if ( (filename = filesel_select_file_open(_("Choose a picture"), NULL)) ) {
-                       personeditdlg.pixbuf = gdk_pixbuf_new_from_file(filename, &error);
-                       personeditdlg.picture_set = TRUE;
-                       g_free(filename);
-                       width = gdk_pixbuf_get_width(personeditdlg.pixbuf);
-                       height = gdk_pixbuf_get_height(personeditdlg.pixbuf);
+                       GdkPixbuf *pixbuf = NULL;
+                       gdk_pixbuf_get_file_info(filename, &width, &height);
 
                        if ( width > 128 || height > 128 ) {
                                if (width > height) {
@@ -734,9 +733,21 @@ static void addressbook_edit_person_set_picture(GtkWidget *widget,
                                        scalewidth = (width * 128) / height;
                                        scaleheight = 128;
                                }
-                               personeditdlg.pixbuf = gdk_pixbuf_scale_simple(personeditdlg.pixbuf, scalewidth, scaleheight, GDK_INTERP_BILINEAR);
+                               pixbuf = gdk_pixbuf_new_from_file_at_scale(filename, 
+                                               scalewidth, scaleheight, TRUE, &error);
+                       } else {
+                               pixbuf = gdk_pixbuf_new_from_file(filename, &error);
+                       }
+                       if (error) {
+                               alertpanel_error(_("Failed to import image: \n%s"),
+                                               error->message);
+                               g_error_free(error);
+                               error = NULL;
                        }
-               gtk_image_set_from_pixbuf(GTK_IMAGE(personeditdlg.image), personeditdlg.pixbuf);
+                       personeditdlg.picture_set = TRUE;
+                       g_free(filename);
+                       gtk_image_set_from_pixbuf(GTK_IMAGE(personeditdlg.image), pixbuf);
+                       g_object_unref(pixbuf);
                }
        } else {
                gtk_menu_popup(GTK_MENU(personeditdlg.editaddr_popupmenu), 
@@ -759,7 +770,7 @@ static void addressbook_edit_person_page_basic( gint pageNum, gchar *pageLbl ) {
        const gchar *locale;
        gint top = 0;
        gint n_entries;
-
+       GdkPixbuf *pixbuf;
        vbox = gtk_vbox_new( FALSE, 20 );
        hbox = gtk_hbox_new( FALSE, 8 );
 
@@ -770,11 +781,12 @@ static void addressbook_edit_person_page_basic( gint pageNum, gchar *pageLbl ) {
        frame_picture = gtk_frame_new("Photo");
        
        /* Room for a photo */
-       personeditdlg.pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 48, 48);
-       gdk_pixbuf_fill(personeditdlg.pixbuf, 0xffffff00);
+       pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 48, 48);
+       gdk_pixbuf_fill(pixbuf, 0xffffff00);
        personeditdlg.picture_set = FALSE;
-       personeditdlg.image = gtk_image_new_from_pixbuf(personeditdlg.pixbuf);
-       
+       personeditdlg.image = gtk_image_new_from_pixbuf(pixbuf);
+       g_object_unref(pixbuf);
+
        gtk_container_add(GTK_CONTAINER(ebox_picture), personeditdlg.image);
        gtk_container_add(GTK_CONTAINER(frame_picture), ebox_picture);  
        gtk_container_add(GTK_CONTAINER( personeditdlg.notebook ), hbox );
@@ -1372,7 +1384,7 @@ static gboolean addressbook_edit_person_close( gboolean cancelled )
 {
        GList *listEMail = NULL;
        GList *listAttrib = NULL;
-       GError *error;
+       GError *error = NULL;
        gchar *cn = NULL;
 
        listEMail = edit_person_build_email_list();
@@ -1431,14 +1443,20 @@ static gboolean addressbook_edit_person_close( gboolean cancelled )
                gchar *name;
                addritem_person_set_common_name( current_person, cn );
                if (personeditdlg.picture_set) {
+                       GdkPixbuf * pixbuf = gtk_image_get_pixbuf(GTK_IMAGE(personeditdlg.image));
                        name = g_strconcat( get_rc_dir(), G_DIR_SEPARATOR_S, ADDRBOOK_DIR, G_DIR_SEPARATOR_S, 
-                                                       ADDRITEM_ID(current_person), NULL );
-                       gdk_pixbuf_save(personeditdlg.pixbuf, name, "png", &error, NULL);
+                                                       ADDRITEM_ID(current_person), ".png", NULL );
+                       gdk_pixbuf_save(pixbuf, name, "png", &error, NULL);
+                       if (error) {
+                               alertpanel_error(_("Failed to save image: \n%s"),
+                                               error->message);
+                               g_error_free(error);
+                       }
                        addritem_person_set_picture( current_person, ADDRITEM_ID(current_person) ) ;
                        g_free( name );
                } else {
                        name = g_strconcat( get_rc_dir(), G_DIR_SEPARATOR_S, ADDRBOOK_DIR, G_DIR_SEPARATOR_S, 
-                                                       ADDRITEM_ID(current_person), NULL );
+                                                       ADDRITEM_ID(current_person), ".png", NULL );
                        g_unlink(name);
                        g_free(name);
                }
@@ -1479,15 +1497,19 @@ ItemPerson *addressbook_edit_person( AddressBookFile *abf, ItemFolder *parent_fo
                                                                         gboolean get_focus) {
        static gboolean cancelled;
        GError *error = NULL;
-
+       GdkPixbuf *pixbuf;
        /* set transient data */
        current_abf = abf;
        current_person = person;
        current_parent_folder = parent_folder;
        edit_person_close_post_update_cb = post_update_cb;
        personeditdlg.ldap = (abf && abf->type == ADBOOKTYPE_LDAP)? TRUE : FALSE;
-       if( !personeditdlg.container )
-               addressbook_edit_person_create(parent_container, &cancelled);
+
+       if( personeditdlg.container ) {
+               gtk_widget_destroy(personeditdlg.container);
+               personeditdlg.container = NULL;
+       }
+       addressbook_edit_person_create(parent_container, &cancelled);
 
        /* typically, get focus when dialog mode is enabled, or when editing a new address */
        if( get_focus ) {
@@ -1523,22 +1545,29 @@ ItemPerson *addressbook_edit_person( AddressBookFile *abf, ItemFolder *parent_fo
 
                if( current_person->picture ) { 
                        gchar *filename = g_strconcat( get_rc_dir(), G_DIR_SEPARATOR_S, ADDRBOOK_DIR, G_DIR_SEPARATOR_S, 
-                                                       current_person->picture, NULL );
+                                                       current_person->picture, ".png", NULL );
                        if (is_file_exist(filename)) {
-                               personeditdlg.pixbuf = gdk_pixbuf_new_from_file(filename, &error);
+                               pixbuf = gdk_pixbuf_new_from_file(filename, &error);
+                               if (error) {
+                                       debug_print("Failed to import image: \n%s",
+                                                       error->message);
+                                       g_error_free(error);
+                                       goto no_img;
+                               }
                                personeditdlg.picture_set = TRUE;
                        } else {
-                               personeditdlg.picture_set = FALSE;
-                               personeditdlg.pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 48, 48);
-                               gdk_pixbuf_fill(personeditdlg.pixbuf, 0xffffff00);
+                               goto no_img;
                        }
                        g_free(filename);
                } else {
+no_img:
                        personeditdlg.picture_set = FALSE;
-                       personeditdlg.pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 48, 48);
-                       gdk_pixbuf_fill(personeditdlg.pixbuf, 0xffffff00);
+                       pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 48, 48);
+                       gdk_pixbuf_fill(pixbuf, 0xffffff00);
                }
-               gtk_image_set_from_pixbuf(GTK_IMAGE(personeditdlg.image), personeditdlg.pixbuf);
+               gtk_image_set_from_pixbuf(GTK_IMAGE(personeditdlg.image), pixbuf);
+               g_object_unref(pixbuf);
+               pixbuf = NULL;
 
                if( current_person->firstName )
                        gtk_entry_set_text(GTK_ENTRY(personeditdlg.entry_first), current_person->firstName );