Allow custom window title for the folder selection dialog.
[claws.git] / src / export.c
index 7104490a706d4fc87e3032f4ac117d5bd6273c4d..74e465ee22e3a040e1eb8eb4a3740b457e4b62ba 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-2012 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,
  * 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
 #  include "config.h"
+#include "claws-features.h"
 #endif
 
 #include "defs.h"
 #include <glib.h>
 #include <glib/gi18n.h>
 #include <gdk/gdkkeysyms.h>
-#include <gtk/gtkmain.h>
-#include <gtk/gtkwidget.h>
-#include <gtk/gtkwindow.h>
-#include <gtk/gtkvbox.h>
-#include <gtk/gtktable.h>
-#include <gtk/gtklabel.h>
-#include <gtk/gtkentry.h>
-#include <gtk/gtkhbbox.h>
-#include <gtk/gtkbutton.h>
-#include <gtk/gtkfilesel.h>
-#include <gtk/gtksignal.h>
-
-#include "sylpheed.h"
+#include <gtk/gtk.h>
+
+#include "claws.h"
 #include "main.h"
 #include "inc.h"
 #include "mbox.h"
@@ -49,6 +40,7 @@
 #include "folder.h"
 #include "utils.h"
 #include "codeconv.h"
+#include "alertpanel.h"
 
 static GtkWidget *window;
 static GtkWidget *src_entry;
@@ -57,7 +49,7 @@ static GtkWidget *src_button;
 static GtkWidget *file_button;
 static GtkWidget *ok_button;
 static GtkWidget *cancel_button;
-static gboolean export_ack;
+static gboolean export_ok; /* see export_mbox() return values */
 
 static void export_create(void);
 static void export_ok_cb(GtkWidget *widget, gpointer data);
@@ -68,25 +60,32 @@ static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data);
 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data);
 
 gint export_mbox(FolderItem *default_src)
+/* return values: -2 skipped/cancelled, -1 error, 0 OK */
 {
-       gint ok = 0;
        gchar *src_id = NULL;
 
-       if (!window)
+       export_ok = -2; // skipped or cancelled
+
+       if (!window) {
                export_create();
-       else
+       }
+       else {
                gtk_widget_show(window);
+       }
 
-       change_dir(sylpheed_get_startup_dir());
+       gtk_window_set_modal(GTK_WINDOW(window), TRUE);
+       change_dir(claws_get_startup_dir());
 
-       if (default_src && default_src->path)
+       if (default_src && default_src->path) {
                src_id = folder_item_get_identifier(default_src);
+       }
 
        if (src_id) {
                gtk_entry_set_text(GTK_ENTRY(src_entry), src_id);
                g_free(src_id);
-       } else
+       } else {
                gtk_entry_set_text(GTK_ENTRY(src_entry), "");
+       }
        gtk_entry_set_text(GTK_ENTRY(file_entry), "");
        gtk_widget_grab_focus(file_entry);
 
@@ -94,36 +93,10 @@ gint export_mbox(FolderItem *default_src)
 
        gtk_main();
 
-       if (export_ack) {
-               const gchar *srcdir, *utf8mbox;
-               FolderItem *src;
-
-               srcdir = gtk_entry_get_text(GTK_ENTRY(src_entry));
-               utf8mbox = gtk_entry_get_text(GTK_ENTRY(file_entry));
-               if (utf8mbox && *utf8mbox) {
-                       const gchar *src_codeset = CS_UTF_8;
-                       const gchar *dest_codeset = conv_get_locale_charset_str();
-                       gchar *mbox;
-
-                       mbox = conv_codeset_strdup(utf8mbox, src_codeset, dest_codeset);
-                       if (!mbox) {
-                               g_warning("faild to convert character set\n");
-                               mbox = g_strdup(utf8mbox);
-                       }
-
-                       src = folder_find_item_from_identifier(srcdir);
-                       if (!src)
-                               g_warning("Can't find the folder.\n");
-                       else
-                               ok = export_to_mbox(src, mbox);
-
-                       g_free(mbox);
-               }
-       }
-
        gtk_widget_hide(window);
+       gtk_window_set_modal(GTK_WINDOW(window), FALSE);
 
-       return ok;
+       return export_ok;
 }
 
 static void export_create(void)
@@ -136,11 +109,10 @@ static void export_create(void)
        GtkWidget *src_label;
        GtkWidget *confirm_area;
 
-       window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
-       gtk_window_set_title(GTK_WINDOW(window), _("Export"));
+       window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "export");
+       gtk_window_set_title(GTK_WINDOW(window), _("Export to 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);
        gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
        g_signal_connect(G_OBJECT(window), "delete_event",
                         G_CALLBACK(delete_event), NULL);
@@ -156,7 +128,8 @@ static void export_create(void)
        gtk_container_set_border_width(GTK_CONTAINER(hbox), 4);
 
        desc_label = gtk_label_new
-               (_("Specify target folder and mbox file."));
+               (_("Locate the folder to export and specify the mbox file."));
+       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);
@@ -166,39 +139,45 @@ static void export_create(void)
        gtk_table_set_col_spacings(GTK_TABLE(table), 8);
        gtk_widget_set_size_request(table, 420, -1);
 
-       src_label = gtk_label_new(_("Source dir:"));
+       src_label = gtk_label_new(_("Source folder:"));
        gtk_table_attach(GTK_TABLE(table), src_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(src_label), 1, 0.5);
 
-       file_label = gtk_label_new(_("Exporting file:"));
+       file_label = gtk_label_new(_("Mbox file:"));
        gtk_table_attach(GTK_TABLE(table), file_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(file_label), 1, 0.5);
 
        src_entry = gtk_entry_new();
        gtk_table_attach(GTK_TABLE(table), src_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);
 
        file_entry = gtk_entry_new();
        gtk_table_attach(GTK_TABLE(table), file_entry, 1, 2, 1, 2,
-                        GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
+                        (GtkAttachOptions) (GTK_EXPAND|GTK_SHRINK|GTK_FILL),
+                        (GtkAttachOptions) (0), 0, 0);
 
-       src_button = gtk_button_new_from_stock(GTK_STOCK_OPEN);
+       src_button = gtkut_get_browse_directory_btn(_("_Browse"));
        gtk_table_attach(GTK_TABLE(table), src_button, 2, 3, 0, 1,
-                        0, 0, 0, 0);
+                        (GtkAttachOptions) (0),
+                        (GtkAttachOptions) (0), 0, 0);
        g_signal_connect(G_OBJECT(src_button), "clicked",
                         G_CALLBACK(export_srcsel_cb), NULL);
 
-       file_button = gtk_button_new_from_stock(GTK_STOCK_OPEN);
+       file_button = gtkut_get_browse_file_btn(_("B_rowse"));
        gtk_table_attach(GTK_TABLE(table), file_button, 2, 3, 1, 2,
-                        0, 0, 0, 0);
+                        (GtkAttachOptions) (0),
+                        (GtkAttachOptions) (0), 0, 0);
        g_signal_connect(G_OBJECT(file_button), "clicked",
                         G_CALLBACK(export_filesel_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);
@@ -213,14 +192,48 @@ static void export_create(void)
 
 static void export_ok_cb(GtkWidget *widget, gpointer data)
 {
-       export_ack = TRUE;
+       const gchar *srcdir, *utf8mbox;
+       FolderItem *src;
+       gchar *mbox;
+
+       srcdir = gtk_entry_get_text(GTK_ENTRY(src_entry));
+       utf8mbox = gtk_entry_get_text(GTK_ENTRY(file_entry));
+
+       if (utf8mbox && !*utf8mbox) {
+               alertpanel_error(_("Target mbox filename can't be left empty."));
+               gtk_widget_grab_focus(file_entry);
+               return;
+       }
+       if (srcdir && !*srcdir) {
+               alertpanel_error(_("Source folder can't be left empty."));
+               gtk_widget_grab_focus(src_entry);
+               return;
+       }
+
+       mbox = g_filename_from_utf8(utf8mbox, -1, NULL, NULL, NULL);
+       if (!mbox) {
+               g_warning("export_ok_cb(): failed to convert character set.");
+               mbox = g_strdup(utf8mbox);
+       }
+
+       src = folder_find_item_from_identifier(srcdir);
+       if (!src) {
+               alertpanel_error(_("Couldn't find the source folder."));
+               gtk_widget_grab_focus(src_entry);
+               g_free(mbox);
+               return;
+       } else {
+               export_ok = export_to_mbox(src, mbox);
+       }
+
+       g_free(mbox);
+
        if (gtk_main_level() > 1)
                gtk_main_quit();
 }
 
 static void export_cancel_cb(GtkWidget *widget, gpointer data)
 {
-       export_ack = FALSE;
        if (gtk_main_level() > 1)
                gtk_main_quit();
 }
@@ -238,7 +251,7 @@ static void export_filesel_cb(GtkWidget *widget, gpointer data)
                                                conv_get_locale_charset_str(),
                                                CS_UTF_8);
                if (!filename) {
-                       g_warning("export_filesel_cb(): faild to convert character set.");
+                       g_warning("export_filesel_cb(): failed to convert character set.");
                        filename = g_strdup(oldstr);
                }
                gtk_entry_set_text(GTK_ENTRY(file_entry), filename);
@@ -251,7 +264,8 @@ static void export_srcsel_cb(GtkWidget *widget, gpointer data)
 {
        FolderItem *src;
 
-       src = foldersel_folder_sel(NULL, FOLDER_SEL_ALL, NULL);
+       src = foldersel_folder_sel(NULL, FOLDER_SEL_ALL, NULL, FALSE,
+                       _("Select folder which to export"));
        if (src && src->path)
                gtk_entry_set_text(GTK_ENTRY(src_entry), src->path);
 }
@@ -264,7 +278,7 @@ static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data)
 
 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
 {
-       if (event && event->keyval == GDK_Escape)
+       if (event && event->keyval == GDK_KEY_Escape)
                export_cancel_cb(NULL, NULL);
        return FALSE;
 }