replace deprecated gtk_window_set_policy
[claws.git] / src / import.c
index aee97880dd1dd80f4a142c074d633411cdd42f47..b6bc4186be8598c453a1bf2e6e23c03644c185d4 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999,2000 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2002 Hiroyuki Yamamoto
  *
  * 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
@@ -47,6 +47,7 @@
 #include "gtkutils.h"
 #include "manage_window.h"
 #include "folder.h"
+#include "codeconv.h"
 
 static GtkWidget *window;
 static GtkWidget *file_entry;
@@ -62,40 +63,67 @@ static void import_ok_cb(GtkWidget *widget, gpointer data);
 static void import_cancel_cb(GtkWidget *widget, gpointer data);
 static void import_filesel_cb(GtkWidget *widget, gpointer data);
 static void import_destsel_cb(GtkWidget *widget, gpointer data);
-static void key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data);
+static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data);
+static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data);
 
-gint import_mbox(void)
+gint import_mbox(FolderItem *default_dest)
 {
        gint ok = 0;
+       gchar *dest_id = NULL;
 
        if (!window)
                import_create();
        else
                gtk_widget_show(window);
 
+       gtk_entry_set_text(GTK_ENTRY(file_entry), "");
+       if (default_dest && default_dest->path)
+               dest_id = folder_item_get_identifier(default_dest);
+
+       if (dest_id) {
+               gtk_entry_set_text(GTK_ENTRY(dest_entry), dest_id);
+               g_free(dest_id);
+       } else
+               gtk_entry_set_text(GTK_ENTRY(dest_entry), "");
+       gtk_widget_grab_focus(file_entry);
+
        manage_window_set_transient(GTK_WINDOW(window));
 
        gtk_main();
 
        if (import_ack) {
-               gchar *filename, *destdir;
+               const gchar *utf8filename, *destdir;
                FolderItem *dest;
 
-               filename = gtk_entry_get_text(GTK_ENTRY(file_entry));
+               utf8filename = gtk_entry_get_text(GTK_ENTRY(file_entry));
                destdir = gtk_entry_get_text(GTK_ENTRY(dest_entry));
-               if (filename && *filename) {
+               if (utf8filename && *utf8filename) {
+                       const gchar *src_codeset = CS_UTF_8;
+                       const gchar *dest_codeset = conv_get_current_charset_str();
+                       gchar *filename;
+
+#warning FIXME_GTK2 /* should we use g_filename_from_utf8? */
+                       filename = conv_codeset_strdup(utf8filename,
+                                                      src_codeset,
+                                                      dest_codeset);
+                       if (!filename) {
+                               g_warning("faild to convert character set\n");
+                               filename = g_strdup(utf8filename);
+                       }
+
                        if (!destdir || !*destdir) {
                                dest = folder_find_item_from_path(INBOX_DIR);
                        } else
-                               dest = folder_find_item_from_path(destdir);
+                               dest = folder_find_item_from_identifier
+                                       (destdir);
 
                        if (!dest) {
                                g_warning("Can't find the folder.\n");
                        } else {
-                               ok = proc_mbox(dest, filename, NULL);
-                               folder_item_scan(dest);
-                               folderview_update_item(dest, TRUE);
+                               ok = proc_mbox(dest, filename, FALSE);
                        }
+
+                       g_free(filename);
                }
        }
 
@@ -107,35 +135,42 @@ gint import_mbox(void)
 static void import_create(void)
 {
        GtkWidget *vbox;
+       GtkWidget *hbox;
+       GtkWidget *desc_label;
        GtkWidget *table;
        GtkWidget *file_label;
        GtkWidget *dest_label;
        GtkWidget *confirm_area;
 
-       window = gtk_window_new(GTK_WINDOW_DIALOG);
+       window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        gtk_window_set_title(GTK_WINDOW(window), _("Import"));
-       gtk_widget_set_usize(window, 450, -1);
-       gtk_container_set_border_width(GTK_CONTAINER(window), 4);
+       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_policy(GTK_WINDOW(window), FALSE, FALSE, FALSE);
-       gtk_signal_connect(GTK_OBJECT(window), "delete_event",
-                          GTK_SIGNAL_FUNC(import_cancel_cb), NULL);
-       gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
-                          GTK_SIGNAL_FUNC(key_pressed), NULL);
-       gtk_signal_connect(GTK_OBJECT(window), "focus_in_event",
-                          GTK_SIGNAL_FUNC(manage_window_focus_in), NULL);
-       gtk_signal_connect(GTK_OBJECT(window), "focus_out_event",
-                          GTK_SIGNAL_FUNC(manage_window_focus_out), NULL);
-
-       vbox = gtk_vbox_new(FALSE, 8);
+       gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
+       g_signal_connect(G_OBJECT(window), "delete_event",
+                        G_CALLBACK(delete_event), NULL);
+       g_signal_connect(G_OBJECT(window), "key_press_event",
+                        G_CALLBACK(key_pressed), NULL);
+       MANAGE_WINDOW_SIGNALS_CONNECT(window);
+
+       vbox = gtk_vbox_new(FALSE, 4);
        gtk_container_add(GTK_CONTAINER(window), vbox);
 
+       hbox = gtk_hbox_new(FALSE, 0);
+       gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
+       gtk_container_set_border_width(GTK_CONTAINER(hbox), 4);
+
+       desc_label = gtk_label_new
+               (_("Specify target mbox file and destination folder."));
+       gtk_box_pack_start(GTK_BOX(hbox), desc_label, FALSE, FALSE, 0);
+
        table = gtk_table_new(2, 3, FALSE);
        gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
        gtk_container_set_border_width(GTK_CONTAINER(table), 8);
-       gtk_table_set_row_spacings(GTK_TABLE(table), 12);
+       gtk_table_set_row_spacings(GTK_TABLE(table), 8);
        gtk_table_set_col_spacings(GTK_TABLE(table), 8);
+       gtk_widget_set_size_request(table, 420, -1);
 
        file_label = gtk_label_new(_("Importing file:"));
        gtk_table_attach(GTK_TABLE(table), file_label, 0, 1, 0, 1,
@@ -158,14 +193,14 @@ static void import_create(void)
        file_button = gtk_button_new_with_label(_(" Select... "));
        gtk_table_attach(GTK_TABLE(table), file_button, 2, 3, 0, 1,
                         0, 0, 0, 0);
-       gtk_signal_connect(GTK_OBJECT(file_button), "clicked",
-                          GTK_SIGNAL_FUNC(import_filesel_cb), NULL);
+       g_signal_connect(G_OBJECT(file_button), "clicked",
+                        G_CALLBACK(import_filesel_cb), NULL);
 
        dest_button = gtk_button_new_with_label(_(" Select... "));
        gtk_table_attach(GTK_TABLE(table), dest_button, 2, 3, 1, 2,
                         0, 0, 0, 0);
-       gtk_signal_connect(GTK_OBJECT(dest_button), "clicked",
-                          GTK_SIGNAL_FUNC(import_destsel_cb), NULL);
+       g_signal_connect(G_OBJECT(dest_button), "clicked",
+                        G_CALLBACK(import_destsel_cb), NULL);
 
        gtkut_button_set_create(&confirm_area,
                                &ok_button,     _("OK"),
@@ -174,10 +209,10 @@ static void import_create(void)
        gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
        gtk_widget_grab_default(ok_button);
 
-       gtk_signal_connect(GTK_OBJECT(ok_button), "clicked",
-                          GTK_SIGNAL_FUNC(import_ok_cb), NULL);
-       gtk_signal_connect(GTK_OBJECT(cancel_button), "clicked",
-                          GTK_SIGNAL_FUNC(import_cancel_cb), NULL);
+       g_signal_connect(G_OBJECT(ok_button), "clicked",
+                        G_CALLBACK(import_ok_cb), NULL);
+       g_signal_connect(G_OBJECT(cancel_button), "clicked",
+                        G_CALLBACK(import_cancel_cb), NULL);
 
        gtk_widget_show_all(window);
 }
@@ -185,35 +220,61 @@ static void import_create(void)
 static void import_ok_cb(GtkWidget *widget, gpointer data)
 {
        import_ack = TRUE;
-       gtk_main_quit();
+       if (gtk_main_level() > 1)
+               gtk_main_quit();
 }
 
 static void import_cancel_cb(GtkWidget *widget, gpointer data)
 {
        import_ack = FALSE;
-       gtk_main_quit();
+       if (gtk_main_level() > 1)
+               gtk_main_quit();
 }
 
 static void import_filesel_cb(GtkWidget *widget, gpointer data)
 {
        gchar *filename;
 
-       filename = filesel_select_file(_("Select importing file"), NULL);
-       if (filename)
+       filename = filesel_select_file_open(_("Select importing file"), NULL);
+       if (!filename) return;
+
+       if (g_getenv ("G_BROKEN_FILENAMES")) {
+               const gchar *oldstr = filename;
+               filename = conv_codeset_strdup (filename,
+                                               conv_get_current_charset_str(),
+                                               CS_UTF_8);
+               if (!filename) {
+                       g_warning("import_filesel_cb(): faild to convert character set.");
+                       filename = g_strdup(oldstr);
+               }
+               gtk_entry_set_text(GTK_ENTRY(file_entry), filename);
+               g_free(filename);
+       } else
                gtk_entry_set_text(GTK_ENTRY(file_entry), filename);
 }
 
 static void import_destsel_cb(GtkWidget *widget, gpointer data)
 {
        FolderItem *dest;
+       gchar *path;
+
+       dest = foldersel_folder_sel(NULL, FOLDER_SEL_COPY, NULL);
+       if (!dest)
+                return;
+       path = folder_item_get_identifier(dest);
+       gtk_entry_set_text(GTK_ENTRY(dest_entry), path);
+       g_free(path);
+}
 
-       dest = foldersel_folder_sel(NULL);
-       if (dest && dest->path)
-               gtk_entry_set_text(GTK_ENTRY(dest_entry), dest->path);
+static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data)
+{
+       import_cancel_cb(NULL, NULL);
+       return TRUE;
 }
 
-static void key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
+static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
 {
        if (event && event->keyval == GDK_Escape)
                import_cancel_cb(NULL, NULL);
+       return FALSE;
 }