2012-11-18 [colin] 3.9.0cvs6-stable
[claws.git] / src / gtk / inputdialog.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2012 Hiroyuki Yamamoto and the Claws Mail team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #include "claws-features.h"
23 #endif
24
25 #include <glib.h>
26 #include <glib/gi18n.h>
27 #include <gdk/gdkkeysyms.h>
28 #include <gtk/gtk.h>
29
30 #include "inputdialog.h"
31 #include "manage_window.h"
32 #include "gtkutils.h"
33 #include "utils.h"
34 #include "combobox.h"
35 #include "prefs_common.h"
36
37
38 #define INPUT_DIALOG_WIDTH      420
39
40 typedef enum
41 {
42         INPUT_DIALOG_NORMAL,
43         INPUT_DIALOG_INVISIBLE,
44         INPUT_DIALOG_COMBO
45 } InputDialogType;
46
47 static gboolean ack;
48 static gboolean fin;
49
50 static InputDialogType type;
51
52 static GtkWidget *dialog;
53 static GtkWidget *msg_title;
54 static GtkWidget *msg_label;
55 static GtkWidget *entry;
56 static GtkWidget *combo;
57 static GtkWidget *remember_checkbtn;
58 static GtkWidget *ok_button;
59 static GtkWidget *icon_q, *icon_p;
60 static gboolean is_pass = FALSE;
61 static void input_dialog_create (gboolean is_password);
62 static gchar *input_dialog_open (const gchar    *title,
63                                  const gchar    *message,
64                                  const gchar  *checkbtn_label,
65                                  const gchar    *default_string,
66                                  gboolean default_checkbtn_state,
67                                  gboolean       *remember);
68 static void input_dialog_set    (const gchar    *title,
69                                  const gchar    *message,
70                                  const gchar    *default_string);
71
72 static void ok_clicked          (GtkWidget      *widget,
73                                  gpointer        data);
74 static void cancel_clicked      (GtkWidget      *widget,
75                                  gpointer        data);
76 static gint delete_event        (GtkWidget      *widget,
77                                  GdkEventAny    *event,
78                                  gpointer        data);
79 static gboolean key_pressed     (GtkWidget      *widget,
80                                  GdkEventKey    *event,
81                                  gpointer        data);
82 static void entry_activated     (GtkEditable    *editable);
83 static void combo_activated     (GtkEditable    *editable);
84
85
86 gchar *input_dialog(const gchar *title, const gchar *message,
87                     const gchar *default_string)
88 {
89         if (dialog && gtk_widget_get_visible(dialog)) return NULL;
90
91         if (!dialog)
92                 input_dialog_create(FALSE);
93
94         type = INPUT_DIALOG_NORMAL;
95         gtk_widget_hide(combo);
96         gtk_widget_show(entry);
97
98         gtk_widget_hide(remember_checkbtn);
99
100         gtk_widget_show(icon_q);
101         gtk_widget_hide(icon_p);
102         is_pass = FALSE;
103         gtk_entry_set_visibility(GTK_ENTRY(entry), TRUE);
104 #ifdef MAEMO
105         hildon_gtk_entry_set_input_mode(GTK_ENTRY(entry), 
106                 HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_AUTOCAP);
107 #endif
108
109         return input_dialog_open(title, message, NULL, default_string, FALSE, NULL);
110 }
111
112 gchar *input_dialog_with_invisible(const gchar *title, const gchar *message,
113                                    const gchar *default_string)
114 {
115         if (dialog && gtk_widget_get_visible(dialog)) return NULL;
116
117         if (!dialog)
118                 input_dialog_create(TRUE);
119
120         type = INPUT_DIALOG_INVISIBLE;
121         gtk_widget_hide(combo);
122         gtk_widget_show(entry);
123         gtk_widget_hide(remember_checkbtn);
124
125         gtk_widget_hide(icon_q);
126         gtk_widget_show(icon_p);
127         is_pass = TRUE;
128         gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
129 #ifdef MAEMO
130         hildon_gtk_entry_set_input_mode(GTK_ENTRY(entry), 
131                 HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_INVISIBLE);
132 #endif
133
134         return input_dialog_open(title, message, NULL, default_string, FALSE, NULL);
135 }
136
137 gchar *input_dialog_with_invisible_checkbtn(const gchar *title, const gchar *message,
138                                    const gchar *default_string, const gchar *checkbtn_label,
139                                    gboolean *checkbtn_state)
140 {
141         if (dialog && gtk_widget_get_visible(dialog)) return NULL;
142
143         if (!dialog)
144                 input_dialog_create(TRUE);
145
146         type = INPUT_DIALOG_INVISIBLE;
147         gtk_widget_hide(combo);
148         gtk_widget_show(entry);
149
150         if (checkbtn_label && checkbtn_state) {
151                 gtk_widget_show(remember_checkbtn);
152                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(remember_checkbtn), *checkbtn_state);
153         }
154         else {
155                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(remember_checkbtn), FALSE);
156                 gtk_widget_hide(remember_checkbtn);
157         }
158
159         gtk_widget_hide(icon_q);
160         gtk_widget_show(icon_p);
161         is_pass = TRUE;
162         gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
163 #ifdef MAEMO
164         hildon_gtk_entry_set_input_mode(GTK_ENTRY(entry), 
165                 HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_INVISIBLE);
166 #endif
167
168         return input_dialog_open(title, message, checkbtn_label, default_string, (checkbtn_state? *checkbtn_state:FALSE), checkbtn_state);
169 }
170
171 gchar *input_dialog_combo(const gchar *title, const gchar *message,
172                           const gchar *default_string, GList *list)
173 {
174         return input_dialog_combo_remember(title, message, 
175                 default_string, list, FALSE);
176 }
177
178 gchar *input_dialog_combo_remember(const gchar *title, const gchar *message,
179                           const gchar *default_string, GList *list,
180                           gboolean *remember)
181 {
182         if (dialog && gtk_widget_get_visible(dialog)) return NULL;
183
184         if (!dialog)
185                 input_dialog_create(FALSE);
186
187         type = INPUT_DIALOG_COMBO;
188         gtk_widget_hide(entry);
189         gtk_widget_show(combo);
190
191         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(remember_checkbtn), FALSE);
192         if (remember)
193                 gtk_widget_show(remember_checkbtn);
194         else
195                 gtk_widget_hide(remember_checkbtn);
196
197         gtk_widget_show(icon_q);
198         gtk_widget_hide(icon_p);
199         is_pass = FALSE;
200
201         combobox_unset_popdown_strings(GTK_COMBO_BOX(combo));
202         combobox_set_popdown_strings(GTK_COMBO_BOX(combo), list);
203
204         return input_dialog_open(title, message, NULL, default_string, FALSE, remember);
205 }
206
207 gchar *input_dialog_with_checkbtn(const gchar   *title,
208                                    const gchar  *message,
209                                    const gchar  *default_string,
210                                    const gchar  *checkbtn_label,
211                                    gboolean *checkbtn_state)
212 {
213         if (dialog && gtk_widget_get_visible(dialog)) return NULL;
214
215         if (!dialog)
216                 input_dialog_create(FALSE);
217
218         type = INPUT_DIALOG_NORMAL;
219         gtk_widget_hide(combo);
220         gtk_widget_show(entry);
221
222         if (checkbtn_label && checkbtn_state)
223                 gtk_widget_show(remember_checkbtn);
224         else
225                 gtk_widget_hide(remember_checkbtn);
226
227         gtk_widget_show(icon_q);
228         gtk_widget_hide(icon_p);
229         is_pass = FALSE;
230         gtk_entry_set_visibility(GTK_ENTRY(entry), TRUE);
231 #ifdef MAEMO
232         hildon_gtk_entry_set_input_mode(GTK_ENTRY(entry), 
233                 HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_AUTOCAP);
234 #endif
235
236         return input_dialog_open(title, message, checkbtn_label, default_string, 
237                                  prefs_common.inherit_folder_props, checkbtn_state);
238 }
239
240 gchar *input_dialog_query_password(const gchar *server, const gchar *user)
241 {
242         gchar *message;
243         gchar *pass;
244
245         if (server && user)
246                 message = g_strdup_printf(_("Input password for %s on %s:"),
247                                   user, server);
248         else if (server)
249                 message = g_strdup_printf(_("Input password for %s:"),
250                                   server);
251         else if (user)
252                 message = g_strdup_printf(_("Input password for %s:"),
253                                   user);
254         else
255                 message = g_strdup_printf(_("Input password:"));
256         pass = input_dialog_with_invisible(_("Input password"), message, NULL);
257         g_free(message);
258
259         return pass;
260 }
261
262 gchar *input_dialog_query_password_keep(const gchar *server, const gchar *user, gchar **keep)
263 {
264         gchar *message;
265         gchar *pass;
266
267         if (server && user)
268                 message = g_strdup_printf(_("Input password for %s on %s:"),
269                                   user, server);
270         else if (server)
271                 message = g_strdup_printf(_("Input password for %s:"),
272                                   server);
273         else if (user)
274                 message = g_strdup_printf(_("Input password for %s:"),
275                                   user);
276         else
277                 message = g_strdup_printf(_("Input password:"));
278         if (keep) {
279                 if (*keep != NULL) {
280                         pass = g_strdup (*keep);
281                 }
282                 else {
283                         gboolean state = prefs_common.session_passwords;
284                         pass = input_dialog_with_invisible_checkbtn(_("Input password"), 
285                                         message, NULL,
286                                         _("Remember password for this session"), 
287                                         &state);
288                         if (state) {
289                                 *keep = g_strdup (pass);
290                                 debug_print("keeping session password for account\n");
291                         }
292                         prefs_common.session_passwords = state;
293                 }
294         }
295         else {
296                 pass = input_dialog_with_invisible(_("Input password"), message, NULL);
297         }               
298         g_free(message);
299
300         return pass;
301 }
302
303 static void input_dialog_create(gboolean is_password)
304 {
305         static PangoFontDescription *font_desc;
306         GtkWidget *hbox;
307         GtkWidget *vbox;
308         GtkWidget *cancel_button;
309         GtkWidget *confirm_area;
310
311         dialog = gtk_dialog_new();
312
313         gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE);
314         gtk_window_set_default_size(GTK_WINDOW(dialog), 375, 100);
315         gtk_window_set_title(GTK_WINDOW(dialog), "");
316
317         g_signal_connect(G_OBJECT(dialog), "delete_event",
318                          G_CALLBACK(delete_event), NULL);
319         g_signal_connect(G_OBJECT(dialog), "key_press_event",
320                          G_CALLBACK(key_pressed), NULL);
321         MANAGE_WINDOW_SIGNALS_CONNECT(dialog);
322
323         vbox = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
324         gtk_box_set_spacing (GTK_BOX (vbox), 14);
325         hbox = gtk_hbox_new (FALSE, 12);
326         gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
327         gtk_widget_show (hbox);
328         gtk_box_pack_start (GTK_BOX (vbox), hbox,
329                             FALSE, FALSE, 0);
330
331         /* for title label */
332         icon_q = gtk_image_new_from_stock(GTK_STOCK_DIALOG_QUESTION,
333                                 GTK_ICON_SIZE_DIALOG); 
334         gtk_misc_set_alignment (GTK_MISC (icon_q), 0.5, 0.0);
335         gtk_box_pack_start (GTK_BOX (hbox), icon_q, FALSE, FALSE, 0);
336         icon_p = gtk_image_new_from_stock(GTK_STOCK_DIALOG_AUTHENTICATION,
337                                 GTK_ICON_SIZE_DIALOG); 
338         gtk_misc_set_alignment (GTK_MISC (icon_p), 0.5, 0.0);
339         gtk_box_pack_start (GTK_BOX (hbox), icon_p, FALSE, FALSE, 0);
340         
341         vbox = gtk_vbox_new (FALSE, 12);
342         gtk_box_pack_start (GTK_BOX (hbox), vbox, TRUE, TRUE, 0);
343         gtk_widget_show (vbox);
344         
345         msg_title = gtk_label_new("");
346         gtk_misc_set_alignment(GTK_MISC(msg_title), 0, 0.5);
347         gtk_label_set_justify(GTK_LABEL(msg_title), GTK_JUSTIFY_LEFT);
348         gtk_label_set_use_markup (GTK_LABEL (msg_title), TRUE);
349         gtk_box_pack_start(GTK_BOX(vbox), msg_title, FALSE, FALSE, 0);
350         gtk_label_set_line_wrap(GTK_LABEL(msg_title), TRUE);
351         if (!font_desc) {
352                 gint size;
353
354                 size = pango_font_description_get_size
355                         (gtk_widget_get_style(msg_title)->font_desc);
356                 font_desc = pango_font_description_new();
357                 pango_font_description_set_weight
358                         (font_desc, PANGO_WEIGHT_BOLD);
359                 pango_font_description_set_size
360                         (font_desc, size * PANGO_SCALE_LARGE);
361         }
362         if (font_desc)
363                 gtk_widget_modify_font(msg_title, font_desc);
364         
365         msg_label = gtk_label_new("");
366         gtk_misc_set_alignment(GTK_MISC(msg_label), 0, 0.5);
367         gtk_label_set_justify(GTK_LABEL(msg_label), GTK_JUSTIFY_LEFT);
368         gtk_box_pack_start(GTK_BOX(vbox), msg_label, FALSE, FALSE, 0);
369         gtk_widget_show(msg_label);
370                 
371         entry = gtk_entry_new();
372         gtk_box_pack_start(GTK_BOX(vbox), entry, FALSE, FALSE, 0);
373         g_signal_connect(G_OBJECT(entry), "activate",
374                          G_CALLBACK(entry_activated), NULL);
375
376         combo = gtk_combo_box_entry_new_text();
377         gtk_box_pack_start(GTK_BOX(vbox), combo, FALSE, FALSE, 0);
378         g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((combo)))), "activate",
379                          G_CALLBACK(combo_activated), NULL);
380
381         remember_checkbtn = gtk_check_button_new_with_label(_("Remember this"));
382         gtk_box_pack_start(GTK_BOX(vbox), remember_checkbtn, FALSE, FALSE, 0);
383
384         hbox = gtk_hbox_new(TRUE, 0);
385
386         gtkut_stock_button_set_create(&confirm_area,
387                                       &cancel_button, GTK_STOCK_CANCEL,
388                                       &ok_button, GTK_STOCK_OK,
389                                       NULL, NULL);
390
391         gtk_box_pack_end(GTK_BOX(gtk_dialog_get_action_area(GTK_DIALOG(dialog))),
392                          confirm_area, FALSE, FALSE, 0);
393         gtk_container_set_border_width(GTK_CONTAINER(confirm_area), 5);
394
395         gtk_widget_show_all(gtk_dialog_get_content_area(GTK_DIALOG(dialog)));
396         
397         gtk_widget_hide(remember_checkbtn);
398
399         if (is_password)
400                 gtk_widget_hide(icon_q);
401         else
402                 gtk_widget_hide(icon_p);
403
404         is_pass = is_password;
405
406         gtk_widget_grab_default(ok_button);
407
408         g_signal_connect(G_OBJECT(ok_button), "clicked",
409                          G_CALLBACK(ok_clicked), NULL);
410         g_signal_connect(G_OBJECT(cancel_button), "clicked",
411                          G_CALLBACK(cancel_clicked), NULL);
412 }
413
414 static gchar *input_dialog_open(const gchar *title, const gchar *message,
415                                 const gchar *checkbtn_label,
416                                 const gchar *default_string,
417                                 gboolean default_checkbtn_state,
418                                 gboolean *remember)
419 {
420         gchar *str;
421
422         if (dialog && gtk_widget_get_visible(dialog)) return NULL;
423
424         if (!dialog)
425                 input_dialog_create(FALSE);
426
427         if (checkbtn_label)
428                 gtk_button_set_label(GTK_BUTTON(remember_checkbtn), checkbtn_label);
429         else
430                 gtk_button_set_label(GTK_BUTTON(remember_checkbtn), _("Remember this"));
431
432         input_dialog_set(title, message, default_string);
433         gtk_window_present(GTK_WINDOW(dialog));
434
435         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(remember_checkbtn),
436                                      default_checkbtn_state);
437         if (remember)
438                 gtk_widget_show(remember_checkbtn);
439         else
440                 gtk_widget_hide(remember_checkbtn);
441
442         gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
443         manage_window_set_transient(GTK_WINDOW(dialog));
444
445         ack = fin = FALSE;
446
447         while (fin == FALSE)
448                 gtk_main_iteration();
449
450         manage_window_focus_out(dialog, NULL, NULL);
451
452         if (ack) {
453                 GtkEditable *editable;
454
455                 if (type == INPUT_DIALOG_COMBO)
456                         editable = GTK_EDITABLE(gtk_bin_get_child(GTK_BIN((combo))));
457                 else
458                         editable = GTK_EDITABLE(entry);
459
460                 str = gtk_editable_get_chars(editable, 0, -1);
461                 if (str && *str == '\0' && !is_pass) {
462                         g_free(str);
463                         str = NULL;
464                 }
465         } else
466                 str = NULL;
467
468         GTK_EVENTS_FLUSH();
469
470         if (remember) {
471                 *remember = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(remember_checkbtn));
472         }
473
474         gtk_widget_destroy(dialog);
475         dialog = NULL;
476
477         if (is_pass)
478                 debug_print("return string = %s\n", str ? "********": ("none"));
479         else
480                 debug_print("return string = %s\n", str ? str : "(none)");
481         return str;
482 }
483
484 static void input_dialog_set(const gchar *title, const gchar *message,
485                              const gchar *default_string)
486 {
487         GtkWidget *entry_;
488
489         if (type == INPUT_DIALOG_COMBO)
490                 entry_ = gtk_bin_get_child(GTK_BIN((combo)));
491         else
492                 entry_ = entry;
493
494         gtk_window_set_title(GTK_WINDOW(dialog), title);
495         gtk_label_set_text(GTK_LABEL(msg_title), title);
496         gtk_label_set_text(GTK_LABEL(msg_label), message);
497         if (default_string && *default_string) {
498                 gtk_entry_set_text(GTK_ENTRY(entry_), default_string);
499                 gtk_editable_set_position(GTK_EDITABLE(entry_), 0);
500                 gtk_editable_select_region(GTK_EDITABLE(entry_), 0, -1);
501         } else
502                 gtk_entry_set_text(GTK_ENTRY(entry_), "");
503
504         gtk_widget_grab_focus(ok_button);
505         gtk_widget_grab_focus(entry_);
506 }
507
508 static void ok_clicked(GtkWidget *widget, gpointer data)
509 {
510         ack = TRUE;
511         fin = TRUE;
512 }
513
514 static void cancel_clicked(GtkWidget *widget, gpointer data)
515 {
516         ack = FALSE;
517         fin = TRUE;
518 }
519
520 static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data)
521 {
522         ack = FALSE;
523         fin = TRUE;
524
525         return TRUE;
526 }
527
528 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
529 {
530         if (event && event->keyval == GDK_KEY_Escape) {
531                 ack = FALSE;
532                 fin = TRUE;
533         } else if (event && event->keyval == GDK_KEY_Return) {
534                 ack = TRUE;
535                 fin = TRUE;
536                 return TRUE; /* do not let Return pass - it
537                               * pops up the combo on validating */
538         }
539
540         return FALSE;
541 }
542
543 static void entry_activated(GtkEditable *editable)
544 {
545         ack = TRUE;
546         fin = TRUE;
547 }
548
549 static void combo_activated(GtkEditable *editable)
550 {
551         ack = TRUE;
552         fin = TRUE;
553 }