2010-10-17 [colin] 3.7.6cvs56
[claws.git] / src / gtk / inputdialog.c
index 2161211732712ff09470d40fc1bec016bb8b2901..65e38e70bc4f0d59e7ef52a8ab6bc6b4cb733a3a 100644 (file)
@@ -1,10 +1,10 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2006 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2009 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
 #include <glib.h>
 #include <glib/gi18n.h>
 #include <gdk/gdkkeysyms.h>
-#include <gtk/gtkmain.h>
-#include <gtk/gtkwidget.h>
-#include <gtk/gtkdialog.h>
-#include <gtk/gtkwindow.h>
-#include <gtk/gtksignal.h>
-#include <gtk/gtkvbox.h>
-#include <gtk/gtkhbox.h>
-#include <gtk/gtklabel.h>
-#include <gtk/gtkentry.h>
-#include <gtk/gtkcombo.h>
-#include <gtk/gtkbutton.h>
-#include <gtk/gtkhbbox.h>
-#include <gtk/gtkstock.h>
-#include <gtk/gtkimage.h>
-#include <gtk/gtktogglebutton.h>
-#include <gtk/gtkcheckbutton.h>
+#include <gtk/gtk.h>
 
 #include "inputdialog.h"
 #include "manage_window.h"
 #include "gtkutils.h"
 #include "utils.h"
+#include "combobox.h"
+#include "prefs_common.h"
+
 
 #define INPUT_DIALOG_WIDTH     420
 
@@ -65,14 +53,16 @@ static GtkWidget *msg_title;
 static GtkWidget *msg_label;
 static GtkWidget *entry;
 static GtkWidget *combo;
-static GtkWidget *remember_chkbtn;
+static GtkWidget *remember_checkbtn;
 static GtkWidget *ok_button;
 static GtkWidget *icon_q, *icon_p;
 static gboolean is_pass = FALSE;
 static void input_dialog_create        (gboolean is_password);
 static gchar *input_dialog_open        (const gchar    *title,
                                 const gchar    *message,
+                                const gchar  *checkbtn_label,
                                 const gchar    *default_string,
+                                gboolean default_checkbtn_state,
                                 gboolean       *remember);
 static void input_dialog_set   (const gchar    *title,
                                 const gchar    *message,
@@ -95,7 +85,7 @@ static void combo_activated   (GtkEditable    *editable);
 gchar *input_dialog(const gchar *title, const gchar *message,
                    const gchar *default_string)
 {
-       if (dialog && GTK_WIDGET_VISIBLE(dialog)) return NULL;
+       if (dialog && gtkut_widget_get_visible(dialog)) return NULL;
 
        if (!dialog)
                input_dialog_create(FALSE);
@@ -104,18 +94,50 @@ gchar *input_dialog(const gchar *title, const gchar *message,
        gtk_widget_hide(combo);
        gtk_widget_show(entry);
 
+       gtk_widget_hide(remember_checkbtn);
+
        gtk_widget_show(icon_q);
        gtk_widget_hide(icon_p);
        is_pass = FALSE;
        gtk_entry_set_visibility(GTK_ENTRY(entry), TRUE);
+#ifdef MAEMO
+       hildon_gtk_entry_set_input_mode(GTK_ENTRY(entry), 
+               HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_AUTOCAP);
+#endif
 
-       return input_dialog_open(title, message, default_string, NULL);
+       return input_dialog_open(title, message, NULL, default_string, FALSE, NULL);
 }
 
 gchar *input_dialog_with_invisible(const gchar *title, const gchar *message,
                                   const gchar *default_string)
 {
-       if (dialog && GTK_WIDGET_VISIBLE(dialog)) return NULL;
+       if (dialog && gtkut_widget_get_visible(dialog)) return NULL;
+
+       if (!dialog)
+               input_dialog_create(TRUE);
+
+       type = INPUT_DIALOG_INVISIBLE;
+       gtk_widget_hide(combo);
+       gtk_widget_show(entry);
+       gtk_widget_hide(remember_checkbtn);
+
+       gtk_widget_hide(icon_q);
+       gtk_widget_show(icon_p);
+       is_pass = TRUE;
+       gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
+#ifdef MAEMO
+       hildon_gtk_entry_set_input_mode(GTK_ENTRY(entry), 
+               HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_INVISIBLE);
+#endif
+
+       return input_dialog_open(title, message, NULL, default_string, FALSE, NULL);
+}
+
+gchar *input_dialog_with_invisible_checkbtn(const gchar *title, const gchar *message,
+                                  const gchar *default_string, const gchar *checkbtn_label,
+                                  gboolean *checkbtn_state)
+{
+       if (dialog && gtkut_widget_get_visible(dialog)) return NULL;
 
        if (!dialog)
                input_dialog_create(TRUE);
@@ -124,27 +146,39 @@ gchar *input_dialog_with_invisible(const gchar *title, const gchar *message,
        gtk_widget_hide(combo);
        gtk_widget_show(entry);
 
+       if (checkbtn_label && checkbtn_state) {
+               gtk_widget_show(remember_checkbtn);
+               gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(remember_checkbtn), *checkbtn_state);
+       }
+       else {
+               gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(remember_checkbtn), FALSE);
+               gtk_widget_hide(remember_checkbtn);
+       }
+
        gtk_widget_hide(icon_q);
        gtk_widget_show(icon_p);
        is_pass = TRUE;
        gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
+#ifdef MAEMO
+       hildon_gtk_entry_set_input_mode(GTK_ENTRY(entry), 
+               HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_INVISIBLE);
+#endif
 
-       return input_dialog_open(title, message, default_string, NULL);
+       return input_dialog_open(title, message, checkbtn_label, default_string, (checkbtn_state? *checkbtn_state:FALSE), checkbtn_state);
 }
 
 gchar *input_dialog_combo(const gchar *title, const gchar *message,
-                         const gchar *default_string, GList *list,
-                         gboolean case_sensitive)
+                         const gchar *default_string, GList *list)
 {
        return input_dialog_combo_remember(title, message, 
-               default_string, list, case_sensitive, NULL);
+               default_string, list, FALSE);
 }
 
 gchar *input_dialog_combo_remember(const gchar *title, const gchar *message,
                          const gchar *default_string, GList *list,
-                         gboolean case_sensitive, gboolean *remember)
+                         gboolean *remember)
 {
-       if (dialog && GTK_WIDGET_VISIBLE(dialog)) return NULL;
+       if (dialog && gtkut_widget_get_visible(dialog)) return NULL;
 
        if (!dialog)
                input_dialog_create(FALSE);
@@ -153,29 +187,53 @@ gchar *input_dialog_combo_remember(const gchar *title, const gchar *message,
        gtk_widget_hide(entry);
        gtk_widget_show(combo);
 
-       gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(remember_chkbtn), FALSE);
+       gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(remember_checkbtn), FALSE);
        if (remember)
-               gtk_widget_show(remember_chkbtn);
+               gtk_widget_show(remember_checkbtn);
        else
-               gtk_widget_hide(remember_chkbtn);
+               gtk_widget_hide(remember_checkbtn);
 
        gtk_widget_show(icon_q);
        gtk_widget_hide(icon_p);
        is_pass = FALSE;
 
-       if (!list) {
-               GList empty_list;
+       combobox_unset_popdown_strings(GTK_COMBO_BOX(combo));
+       combobox_set_popdown_strings(GTK_COMBO_BOX(combo), list);
 
-               empty_list.data = (gpointer)"";
-               empty_list.next = NULL;
-               empty_list.prev = NULL;
-               gtk_combo_set_popdown_strings(GTK_COMBO(combo), &empty_list);
-       } else
-               gtk_combo_set_popdown_strings(GTK_COMBO(combo), list);
+       return input_dialog_open(title, message, NULL, default_string, FALSE, remember);
+}
+
+gchar *input_dialog_with_checkbtn(const gchar  *title,
+                                  const gchar  *message,
+                                  const gchar  *default_string,
+                                  const gchar  *checkbtn_label,
+                                  gboolean *checkbtn_state)
+{
+       if (dialog && gtkut_widget_get_visible(dialog)) return NULL;
+
+       if (!dialog)
+               input_dialog_create(FALSE);
 
-       gtk_combo_set_case_sensitive(GTK_COMBO(combo), case_sensitive);
+       type = INPUT_DIALOG_NORMAL;
+       gtk_widget_hide(combo);
+       gtk_widget_show(entry);
+
+       if (checkbtn_label && checkbtn_state)
+               gtk_widget_show(remember_checkbtn);
+       else
+               gtk_widget_hide(remember_checkbtn);
+
+       gtk_widget_show(icon_q);
+       gtk_widget_hide(icon_p);
+       is_pass = FALSE;
+       gtk_entry_set_visibility(GTK_ENTRY(entry), TRUE);
+#ifdef MAEMO
+       hildon_gtk_entry_set_input_mode(GTK_ENTRY(entry), 
+               HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_AUTOCAP);
+#endif
 
-       return input_dialog_open(title, message, default_string, remember);
+       return input_dialog_open(title, message, checkbtn_label, default_string, 
+                                prefs_common.inherit_folder_props, checkbtn_state);
 }
 
 gchar *input_dialog_query_password(const gchar *server, const gchar *user)
@@ -183,14 +241,64 @@ gchar *input_dialog_query_password(const gchar *server, const gchar *user)
        gchar *message;
        gchar *pass;
 
-       message = g_strdup_printf(_("Input password for %s on %s:"),
+       if (server && user)
+               message = g_strdup_printf(_("Input password for %s on %s:"),
                                  user, server);
+       else if (server)
+               message = g_strdup_printf(_("Input password for %s:"),
+                                 server);
+       else if (user)
+               message = g_strdup_printf(_("Input password for %s:"),
+                                 user);
+       else
+               message = g_strdup_printf(_("Input password:"));
        pass = input_dialog_with_invisible(_("Input password"), message, NULL);
        g_free(message);
 
        return pass;
 }
 
+gchar *input_dialog_query_password_keep(const gchar *server, const gchar *user, gchar **keep)
+{
+       gchar *message;
+       gchar *pass;
+
+       if (server && user)
+               message = g_strdup_printf(_("Input password for %s on %s:"),
+                                 user, server);
+       else if (server)
+               message = g_strdup_printf(_("Input password for %s:"),
+                                 server);
+       else if (user)
+               message = g_strdup_printf(_("Input password for %s:"),
+                                 user);
+       else
+               message = g_strdup_printf(_("Input password:"));
+        if (keep) {
+               if (*keep != NULL) {
+                       pass = g_strdup (*keep);
+               }
+               else {
+                       gboolean state = prefs_common.session_passwords;
+                       pass = input_dialog_with_invisible_checkbtn(_("Input password"), 
+                                       message, NULL,
+                                       _("Remember password for this session"), 
+                                       &state);
+                       if (state) {
+                               *keep = g_strdup (pass);
+                               debug_print("keeping session password for account\n");
+                       }
+                       prefs_common.session_passwords = state;
+               }
+       }
+       else {
+               pass = input_dialog_with_invisible(_("Input password"), message, NULL);
+       }               
+       g_free(message);
+
+       return pass;
+}
+
 static void input_dialog_create(gboolean is_password)
 {
        static PangoFontDescription *font_desc;
@@ -205,7 +313,6 @@ static void input_dialog_create(gboolean is_password)
        gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE);
        gtk_window_set_default_size(GTK_WINDOW(dialog), 375, 100);
        gtk_window_set_title(GTK_WINDOW(dialog), "");
-       gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
 
        g_signal_connect(G_OBJECT(dialog), "delete_event",
                         G_CALLBACK(delete_event), NULL);
@@ -267,13 +374,13 @@ static void input_dialog_create(gboolean is_password)
        g_signal_connect(G_OBJECT(entry), "activate",
                         G_CALLBACK(entry_activated), NULL);
 
-       combo = gtk_combo_new();
+       combo = gtk_combo_box_entry_new_text();
        gtk_box_pack_start(GTK_BOX(vbox), combo, FALSE, FALSE, 0);
-       g_signal_connect(G_OBJECT(GTK_COMBO(combo)->entry), "activate",
+       g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((combo)))), "activate",
                         G_CALLBACK(combo_activated), NULL);
 
-       remember_chkbtn = gtk_check_button_new_with_label(_("Remember this"));
-       gtk_box_pack_start(GTK_BOX(vbox), remember_chkbtn, FALSE, FALSE, 0);
+       remember_checkbtn = gtk_check_button_new_with_label(_("Remember this"));
+       gtk_box_pack_start(GTK_BOX(vbox), remember_checkbtn, FALSE, FALSE, 0);
 
        hbox = gtk_hbox_new(TRUE, 0);
 
@@ -288,7 +395,7 @@ static void input_dialog_create(gboolean is_password)
 
        gtk_widget_show_all(GTK_DIALOG(dialog)->vbox);
        
-       gtk_widget_hide(remember_chkbtn);
+       gtk_widget_hide(remember_checkbtn);
 
        if (is_password)
                gtk_widget_hide(icon_q);
@@ -306,18 +413,33 @@ static void input_dialog_create(gboolean is_password)
 }
 
 static gchar *input_dialog_open(const gchar *title, const gchar *message,
-                               const gchar *default_string, gboolean *remember)
+                               const gchar *checkbtn_label,
+                               const gchar *default_string,
+                               gboolean default_checkbtn_state,
+                               gboolean *remember)
 {
        gchar *str;
 
-       if (dialog && GTK_WIDGET_VISIBLE(dialog)) return NULL;
+       if (dialog && gtkut_widget_get_visible(dialog)) return NULL;
 
        if (!dialog)
                input_dialog_create(FALSE);
 
+       if (checkbtn_label)
+               gtk_button_set_label(GTK_BUTTON(remember_checkbtn), checkbtn_label);
+       else
+               gtk_button_set_label(GTK_BUTTON(remember_checkbtn), _("Remember this"));
+
        input_dialog_set(title, message, default_string);
        gtk_widget_show(dialog);
 
+       gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(remember_checkbtn),
+                                    default_checkbtn_state);
+       if (remember)
+               gtk_widget_show(remember_checkbtn);
+       else
+               gtk_widget_hide(remember_checkbtn);
+
        gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
        manage_window_set_transient(GTK_WINDOW(dialog));
 
@@ -333,7 +455,7 @@ static gchar *input_dialog_open(const gchar *title, const gchar *message,
                GtkEditable *editable;
 
                if (type == INPUT_DIALOG_COMBO)
-                       editable = GTK_EDITABLE(GTK_COMBO(combo)->entry);
+                       editable = GTK_EDITABLE(gtk_bin_get_child(GTK_BIN((combo))));
                else
                        editable = GTK_EDITABLE(entry);
 
@@ -348,9 +470,12 @@ static gchar *input_dialog_open(const gchar *title, const gchar *message,
        GTK_EVENTS_FLUSH();
 
        if (remember) {
-               *remember = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(remember_chkbtn));
+               *remember = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(remember_checkbtn));
        }
-       debug_print("return string = %s\n", str ? str : "(none)");
+       if (is_pass)
+               debug_print("return string = %s\n", str ? "********": ("none"));
+       else
+               debug_print("return string = %s\n", str ? str : "(none)");
        return str;
 }
 
@@ -360,7 +485,7 @@ static void input_dialog_set(const gchar *title, const gchar *message,
        GtkWidget *entry_;
 
        if (type == INPUT_DIALOG_COMBO)
-               entry_ = GTK_COMBO(combo)->entry;
+               entry_ = gtk_bin_get_child(GTK_BIN((combo)));
        else
                entry_ = entry;