2007-10-18 [mones] 3.0.2cvs86
[claws.git] / src / import.c
index d055feee1095ecfc8221a2bd9fa6fbaf88660cf6..389f795620b56e064f4b64e91bab9b2dcfe39a4e 100644 (file)
@@ -1,10 +1,10 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2005 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2007 Hiroyuki Yamamoto and the Claws Mail team
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation; either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
@@ -13,8 +13,8 @@
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * 
  */
 
 #ifdef HAVE_CONFIG_H
@@ -38,7 +38,7 @@
 #include <gtk/gtkfilesel.h>
 #include <gtk/gtksignal.h>
 
-#include "sylpheed.h"
+#include "claws.h"
 #include "main.h"
 #include "inc.h"
 #include "mbox.h"
@@ -49,6 +49,7 @@
 #include "manage_window.h"
 #include "folder.h"
 #include "codeconv.h"
+#include "alertpanel.h"
 
 static GtkWidget *window;
 static GtkWidget *file_entry;
@@ -57,7 +58,7 @@ static GtkWidget *file_button;
 static GtkWidget *dest_button;
 static GtkWidget *ok_button;
 static GtkWidget *cancel_button;
-static gboolean import_ack;
+static gboolean import_ok; /* see import_mbox() return values */
 
 static void import_create(void);
 static void import_ok_cb(GtkWidget *widget, gpointer data);
@@ -68,10 +69,12 @@ static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data);
 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data);
 
 gint import_mbox(FolderItem *default_dest)
+/* return values: -2 skipped/cancelled, -1 error, 0 OK */
 {
-       gint ok = 0;
        gchar *dest_id = NULL;
 
+       import_ok = -2; // skipped or cancelled
+
        if (!window) {
                import_create();
        }
@@ -79,7 +82,7 @@ gint import_mbox(FolderItem *default_dest)
                gtk_widget_show(window);
        }
 
-       change_dir(sylpheed_get_startup_dir());
+       change_dir(claws_get_startup_dir());
 
        if (default_dest && default_dest->path) {
                dest_id = folder_item_get_identifier(default_dest);
@@ -98,41 +101,9 @@ gint import_mbox(FolderItem *default_dest)
 
        gtk_main();
 
-       if (import_ack) {
-               const gchar *utf8mbox, *destdir;
-               FolderItem *dest;
-
-               utf8mbox = gtk_entry_get_text(GTK_ENTRY(file_entry));
-               destdir = gtk_entry_get_text(GTK_ENTRY(dest_entry));
-               if (utf8mbox && *utf8mbox) {
-                       gchar *mbox;
-
-                       mbox = g_filename_from_utf8(utf8mbox, -1, NULL, NULL, NULL);
-                       if (!mbox) {
-                               g_warning("Failed to convert character set.\n");
-                               mbox = g_strdup(utf8mbox);
-                       }
-
-                       if (!destdir || !*destdir) {
-                               dest = folder_find_item_from_path(INBOX_DIR);
-                       } else {
-                               dest = folder_find_item_from_identifier
-                                       (destdir);
-                       }
-
-                       if (!dest) {
-                               g_warning("Can't find the folder.\n");
-                       } else {
-                               ok = proc_mbox(dest, mbox, FALSE);
-                       }
-
-                       g_free(mbox);
-               }
-       }
-
        gtk_widget_hide(window);
 
-       return ok;
+       return import_ok;
 }
 
 static void import_create(void)
@@ -145,8 +116,8 @@ static void import_create(void)
        GtkWidget *dest_label;
        GtkWidget *confirm_area;
 
-       window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
-       gtk_window_set_title(GTK_WINDOW(window), _("Import"));
+       window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "import");
+       gtk_window_set_title(GTK_WINDOW(window), _("Import mbox file"));
        gtk_container_set_border_width(GTK_CONTAINER(window), 5);
        gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
        gtk_window_set_modal(GTK_WINDOW(window), TRUE);
@@ -165,7 +136,8 @@ static void import_create(void)
        gtk_container_set_border_width(GTK_CONTAINER(hbox), 4);
 
        desc_label = gtk_label_new
-               (_("Specify target mbox file and destination folder."));
+               (_("Locate the mbox file and specify the destination folder."));
+       gtk_label_set_line_wrap(GTK_LABEL(desc_label), TRUE);
        gtk_box_pack_start(GTK_BOX(hbox), desc_label, FALSE, FALSE, 0);
 
        table = gtk_table_new(2, 3, FALSE);
@@ -175,44 +147,44 @@ static void import_create(void)
        gtk_table_set_col_spacings(GTK_TABLE(table), 8);
        gtk_widget_set_size_request(table, 420, -1);
 
-       file_label = gtk_label_new(_("Importing file:"));
+       file_label = gtk_label_new(_("Mbox file:"));
        gtk_table_attach(GTK_TABLE(table), file_label, 0, 1, 0, 1,
-                        GTK_FILL, GTK_EXPAND|GTK_FILL, 0, 0);
+                        (GtkAttachOptions) (GTK_FILL),
+                        (GtkAttachOptions) (GTK_EXPAND|GTK_FILL), 0, 0);
        gtk_misc_set_alignment(GTK_MISC(file_label), 1, 0.5);
 
-       dest_label = gtk_label_new(_("Destination dir:"));
+       dest_label = gtk_label_new(_("Destination folder:"));
        gtk_table_attach(GTK_TABLE(table), dest_label, 0, 1, 1, 2,
-                        GTK_FILL, GTK_EXPAND|GTK_FILL, 0, 0);
+                        (GtkAttachOptions) (GTK_FILL),
+                        (GtkAttachOptions) (GTK_EXPAND|GTK_FILL), 0, 0);
        gtk_misc_set_alignment(GTK_MISC(dest_label), 1, 0.5);
 
        file_entry = gtk_entry_new();
        gtk_table_attach(GTK_TABLE(table), file_entry, 1, 2, 0, 1,
-                        GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
+                        (GtkAttachOptions) (GTK_EXPAND|GTK_SHRINK|GTK_FILL),
+                        (GtkAttachOptions) (0), 0, 0);
 
        dest_entry = gtk_entry_new();
        gtk_table_attach(GTK_TABLE(table), dest_entry, 1, 2, 1, 2,
                         GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
 
-       file_button = gtk_button_new_with_mnemonic(_("_Browse"));
-       gtk_button_set_image((GtkButton*)file_button,
-               gtk_image_new_from_stock(GTK_STOCK_OPEN, GTK_ICON_SIZE_BUTTON));
+       file_button = gtkut_get_browse_file_btn(_("_Browse"));
        gtk_table_attach(GTK_TABLE(table), file_button, 2, 3, 0, 1,
-                        0, 0, 0, 0);
+                        (GtkAttachOptions) (GTK_FILL),
+                        (GtkAttachOptions) (0), 0, 0);
        g_signal_connect(G_OBJECT(file_button), "clicked",
                         G_CALLBACK(import_filesel_cb), NULL);
 
-       /* XXX GTK 2.6: GTK_STOCK_DIRECTORY */
-       dest_button = gtk_button_new_with_mnemonic(_("B_rowse"));
-       gtk_button_set_image((GtkButton*)dest_button,
-               gtk_image_new_from_stock(GTK_STOCK_OPEN, GTK_ICON_SIZE_BUTTON));
+       dest_button = gtkut_get_browse_directory_btn(_("B_rowse"));
        gtk_table_attach(GTK_TABLE(table), dest_button, 2, 3, 1, 2,
-                        0, 0, 0, 0);
+                        (GtkAttachOptions) (GTK_FILL),
+                        (GtkAttachOptions) (0), 0, 0);
        g_signal_connect(G_OBJECT(dest_button), "clicked",
                         G_CALLBACK(import_destsel_cb), NULL);
 
        gtkut_stock_button_set_create(&confirm_area,
-                                     &ok_button, GTK_STOCK_OK,
                                      &cancel_button, GTK_STOCK_CANCEL,
+                                     &ok_button, GTK_STOCK_OK,
                                      NULL, NULL);
        gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
        gtk_widget_grab_default(ok_button);
@@ -227,14 +199,56 @@ static void import_create(void)
 
 static void import_ok_cb(GtkWidget *widget, gpointer data)
 {
-       import_ack = TRUE;
+       const gchar *utf8mbox, *destdir;
+       FolderItem *dest;
+       gchar *mbox;
+
+       utf8mbox = gtk_entry_get_text(GTK_ENTRY(file_entry));
+       destdir = gtk_entry_get_text(GTK_ENTRY(dest_entry));
+
+       if (utf8mbox && !*utf8mbox) {
+               alertpanel_error(_("Source mbox filename can't be left empty."));
+               gtk_widget_grab_focus(file_entry);
+               return;
+       }
+       if (destdir && !*destdir) {
+               if (alertpanel(_("Import mbox file"), _("Destination folder is not set.\nImport mbox file to the Inbox folder?"),
+                                               GTK_STOCK_OK, GTK_STOCK_CANCEL, NULL)
+                       == G_ALERTALTERNATE) {
+                       gtk_widget_grab_focus(dest_entry);
+                       return;
+               }
+       }
+
+       mbox = g_filename_from_utf8(utf8mbox, -1, NULL, NULL, NULL);
+       if (!mbox) {
+               g_warning("import_ok_cb(): failed to convert character set.\n");
+               mbox = g_strdup(utf8mbox);
+       }
+
+       if (!destdir || !*destdir) {
+               dest = folder_find_item_from_path(INBOX_DIR);
+       } else {
+               dest = folder_find_item_from_identifier
+                       (destdir);
+       }
+
+       if (!dest) {
+               alertpanel_error(_("Can't find the destination folder."));
+               gtk_widget_grab_focus(dest_entry);
+               return;
+       } else {
+               import_ok = proc_mbox(dest, mbox, FALSE, NULL);
+       }
+
+       g_free(mbox);
+
        if (gtk_main_level() > 1)
                gtk_main_quit();
 }
 
 static void import_cancel_cb(GtkWidget *widget, gpointer data)
 {
-       import_ack = FALSE;
        if (gtk_main_level() > 1)
                gtk_main_quit();
 }
@@ -249,7 +263,7 @@ static void import_filesel_cb(GtkWidget *widget, gpointer data)
 
        utf8_filename = g_filename_to_utf8(filename, -1, NULL, NULL, NULL);
        if (!utf8_filename) {
-               g_warning("import_filesel_cb(): faild to convert characer set.");
+               g_warning("import_filesel_cb(): failed to convert character set.");
                utf8_filename = g_strdup(filename);
        }
        gtk_entry_set_text(GTK_ENTRY(file_entry), utf8_filename);