00c273307107ca1b6c709a0122ca0f8f7dec001b
[claws.git] / src / gtk / inputdialog.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2013 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
105         return input_dialog_open(title, message, NULL, default_string, FALSE, NULL);
106 }
107
108 gchar *input_dialog_with_invisible(const gchar *title, const gchar *message,
109                                    const gchar *default_string)
110 {
111         if (dialog && gtk_widget_get_visible(dialog)) return NULL;
112
113         if (!dialog)
114                 input_dialog_create(TRUE);
115
116         type = INPUT_DIALOG_INVISIBLE;
117         gtk_widget_hide(combo);
118         gtk_widget_show(entry);
119         gtk_widget_hide(remember_checkbtn);
120
121         gtk_widget_hide(icon_q);
122         gtk_widget_show(icon_p);
123         is_pass = TRUE;
124         gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
125
126         return input_dialog_open(title, message, NULL, default_string, FALSE, NULL);
127 }
128
129 gchar *input_dialog_with_invisible_checkbtn(const gchar *title, const gchar *message,
130                                    const gchar *default_string, const gchar *checkbtn_label,
131                                    gboolean *checkbtn_state)
132 {
133         if (dialog && gtk_widget_get_visible(dialog)) return NULL;
134
135         if (!dialog)
136                 input_dialog_create(TRUE);
137
138         type = INPUT_DIALOG_INVISIBLE;
139         gtk_widget_hide(combo);
140         gtk_widget_show(entry);
141
142         if (checkbtn_label && checkbtn_state) {
143                 gtk_widget_show(remember_checkbtn);
144                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(remember_checkbtn), *checkbtn_state);
145         }
146         else {
147                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(remember_checkbtn), FALSE);
148                 gtk_widget_hide(remember_checkbtn);
149         }
150
151         gtk_widget_hide(icon_q);
152         gtk_widget_show(icon_p);
153         is_pass = TRUE;
154         gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
155
156         return input_dialog_open(title, message, checkbtn_label, default_string, (checkbtn_state? *checkbtn_state:FALSE), checkbtn_state);
157 }
158
159 gchar *input_dialog_combo(const gchar *title, const gchar *message,
160                           const gchar *default_string, GList *list)
161 {
162         return input_dialog_combo_remember(title, message, 
163                 default_string, list, FALSE);
164 }
165
166 gchar *input_dialog_combo_remember(const gchar *title, const gchar *message,
167                           const gchar *default_string, GList *list,
168                           gboolean *remember)
169 {
170         if (dialog && gtk_widget_get_visible(dialog)) return NULL;
171
172         if (!dialog)
173                 input_dialog_create(FALSE);
174
175         type = INPUT_DIALOG_COMBO;
176         gtk_widget_hide(entry);
177         gtk_widget_show(combo);
178
179         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(remember_checkbtn), FALSE);
180         if (remember)
181                 gtk_widget_show(remember_checkbtn);
182         else
183                 gtk_widget_hide(remember_checkbtn);
184
185         gtk_widget_show(icon_q);
186         gtk_widget_hide(icon_p);
187         is_pass = FALSE;
188 #if !GTK_CHECK_VERSION(2, 24, 0)
189         combobox_unset_popdown_strings(GTK_COMBO_BOX(combo));
190         combobox_set_popdown_strings(GTK_COMBO_BOX(combo), list);
191 #else
192         combobox_unset_popdown_strings(GTK_COMBO_BOX_TEXT(combo));
193         combobox_set_popdown_strings(GTK_COMBO_BOX_TEXT(combo), list);
194 #endif
195         return input_dialog_open(title, message, NULL, default_string, FALSE, remember);
196 }
197
198 gchar *input_dialog_with_checkbtn(const gchar   *title,
199                                    const gchar  *message,
200                                    const gchar  *default_string,
201                                    const gchar  *checkbtn_label,
202                                    gboolean *checkbtn_state)
203 {
204         if (dialog && gtk_widget_get_visible(dialog)) return NULL;
205
206         if (!dialog)
207                 input_dialog_create(FALSE);
208
209         type = INPUT_DIALOG_NORMAL;
210         gtk_widget_hide(combo);
211         gtk_widget_show(entry);
212
213         if (checkbtn_label && checkbtn_state)
214                 gtk_widget_show(remember_checkbtn);
215         else
216                 gtk_widget_hide(remember_checkbtn);
217
218         gtk_widget_show(icon_q);
219         gtk_widget_hide(icon_p);
220         is_pass = FALSE;
221         gtk_entry_set_visibility(GTK_ENTRY(entry), TRUE);
222
223         return input_dialog_open(title, message, checkbtn_label, default_string, 
224                                  prefs_common.inherit_folder_props, checkbtn_state);
225 }
226
227 gchar *input_dialog_query_password(const gchar *server, const gchar *user)
228 {
229         gchar *message;
230         gchar *pass;
231
232         if (server && user)
233                 message = g_strdup_printf(_("Input password for %s on %s:"),
234                                   user, server);
235         else if (server)
236                 message = g_strdup_printf(_("Input password for %s:"),
237                                   server);
238         else if (user)
239                 message = g_strdup_printf(_("Input password for %s:"),
240                                   user);
241         else
242                 message = g_strdup_printf(_("Input password:"));
243         pass = input_dialog_with_invisible(_("Input password"), message, NULL);
244         g_free(message);
245
246         return pass;
247 }
248
249 gchar *input_dialog_query_password_keep(const gchar *server, const gchar *user, gchar **keep)
250 {
251         gchar *message;
252         gchar *pass;
253
254         if (server && user)
255                 message = g_strdup_printf(_("Input password for %s on %s:"),
256                                   user, server);
257         else if (server)
258                 message = g_strdup_printf(_("Input password for %s:"),
259                                   server);
260         else if (user)
261                 message = g_strdup_printf(_("Input password for %s:"),
262                                   user);
263         else
264                 message = g_strdup_printf(_("Input password:"));
265         if (keep) {
266                 if (*keep != NULL) {
267                         pass = g_strdup (*keep);
268                 }
269                 else {
270                         gboolean state = prefs_common.session_passwords;
271                         pass = input_dialog_with_invisible_checkbtn(_("Input password"), 
272                                         message, NULL,
273                                         _("Remember password for this session"), 
274                                         &state);
275                         if (state) {
276                                 *keep = g_strdup (pass);
277                                 debug_print("keeping session password for account\n");
278                         }
279                         prefs_common.session_passwords = state;
280                 }
281         }
282         else {
283                 pass = input_dialog_with_invisible(_("Input password"), message, NULL);
284         }               
285         g_free(message);
286
287         return pass;
288 }
289
290 static void input_dialog_create(gboolean is_password)
291 {
292         static PangoFontDescription *font_desc;
293         GtkWidget *hbox;
294         GtkWidget *vbox;
295         GtkWidget *cancel_button;
296         GtkWidget *confirm_area;
297
298         dialog = gtk_dialog_new();
299
300         gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE);
301         gtk_window_set_default_size(GTK_WINDOW(dialog), 375, 100);
302         gtk_window_set_title(GTK_WINDOW(dialog), "");
303
304         g_signal_connect(G_OBJECT(dialog), "delete_event",
305                          G_CALLBACK(delete_event), NULL);
306         g_signal_connect(G_OBJECT(dialog), "key_press_event",
307                          G_CALLBACK(key_pressed), NULL);
308         MANAGE_WINDOW_SIGNALS_CONNECT(dialog);
309
310         vbox = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
311         gtk_box_set_spacing (GTK_BOX (vbox), 14);
312         hbox = gtk_hbox_new (FALSE, 12);
313         gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
314         gtk_widget_show (hbox);
315         gtk_box_pack_start (GTK_BOX (vbox), hbox,
316                             FALSE, FALSE, 0);
317
318         /* for title label */
319         icon_q = gtk_image_new_from_stock(GTK_STOCK_DIALOG_QUESTION,
320                                 GTK_ICON_SIZE_DIALOG); 
321         gtk_misc_set_alignment (GTK_MISC (icon_q), 0.5, 0.0);
322         gtk_box_pack_start (GTK_BOX (hbox), icon_q, FALSE, FALSE, 0);
323         icon_p = gtk_image_new_from_stock(GTK_STOCK_DIALOG_AUTHENTICATION,
324                                 GTK_ICON_SIZE_DIALOG); 
325         gtk_misc_set_alignment (GTK_MISC (icon_p), 0.5, 0.0);
326         gtk_box_pack_start (GTK_BOX (hbox), icon_p, FALSE, FALSE, 0);
327         
328         vbox = gtk_vbox_new (FALSE, 12);
329         gtk_box_pack_start (GTK_BOX (hbox), vbox, TRUE, TRUE, 0);
330         gtk_widget_show (vbox);
331         
332         msg_title = gtk_label_new("");
333         gtk_misc_set_alignment(GTK_MISC(msg_title), 0, 0.5);
334         gtk_label_set_justify(GTK_LABEL(msg_title), GTK_JUSTIFY_LEFT);
335         gtk_label_set_use_markup (GTK_LABEL (msg_title), TRUE);
336         gtk_box_pack_start(GTK_BOX(vbox), msg_title, FALSE, FALSE, 0);
337         gtk_label_set_line_wrap(GTK_LABEL(msg_title), TRUE);
338         if (!font_desc) {
339                 gint size;
340
341                 size = pango_font_description_get_size
342                         (gtk_widget_get_style(msg_title)->font_desc);
343                 font_desc = pango_font_description_new();
344                 pango_font_description_set_weight
345                         (font_desc, PANGO_WEIGHT_BOLD);
346                 pango_font_description_set_size
347                         (font_desc, size * PANGO_SCALE_LARGE);
348         }
349         if (font_desc)
350                 gtk_widget_modify_font(msg_title, font_desc);
351         
352         msg_label = gtk_label_new("");
353         gtk_misc_set_alignment(GTK_MISC(msg_label), 0, 0.5);
354         gtk_label_set_justify(GTK_LABEL(msg_label), GTK_JUSTIFY_LEFT);
355         gtk_box_pack_start(GTK_BOX(vbox), msg_label, FALSE, FALSE, 0);
356         gtk_widget_show(msg_label);
357                 
358         entry = gtk_entry_new();
359         gtk_box_pack_start(GTK_BOX(vbox), entry, FALSE, FALSE, 0);
360         g_signal_connect(G_OBJECT(entry), "activate",
361                          G_CALLBACK(entry_activated), NULL);
362
363 #if !GTK_CHECK_VERSION(2, 24, 0)
364         combo = gtk_combo_box_entry_new_text();
365 #else
366         combo = gtk_combo_box_text_new_with_entry();
367 #endif
368         gtk_box_pack_start(GTK_BOX(vbox), combo, FALSE, FALSE, 0);
369         g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((combo)))), "activate",
370                          G_CALLBACK(combo_activated), NULL);
371
372         remember_checkbtn = gtk_check_button_new_with_label(_("Remember this"));
373         gtk_box_pack_start(GTK_BOX(vbox), remember_checkbtn, FALSE, FALSE, 0);
374
375         gtkut_stock_button_set_create(&confirm_area,
376                                       &cancel_button, GTK_STOCK_CANCEL,
377                                       &ok_button, GTK_STOCK_OK,
378                                       NULL, NULL);
379
380         gtk_box_pack_end(GTK_BOX(gtk_dialog_get_action_area(GTK_DIALOG(dialog))),
381                          confirm_area, FALSE, FALSE, 0);
382         gtk_container_set_border_width(GTK_CONTAINER(confirm_area), 5);
383
384         gtk_widget_show_all(gtk_dialog_get_content_area(GTK_DIALOG(dialog)));
385         
386         gtk_widget_hide(remember_checkbtn);
387
388         if (is_password)
389                 gtk_widget_hide(icon_q);
390         else
391                 gtk_widget_hide(icon_p);
392
393         is_pass = is_password;
394
395         gtk_widget_grab_default(ok_button);
396
397         g_signal_connect(G_OBJECT(ok_button), "clicked",
398                          G_CALLBACK(ok_clicked), NULL);
399         g_signal_connect(G_OBJECT(cancel_button), "clicked",
400                          G_CALLBACK(cancel_clicked), NULL);
401 }
402
403 static gchar *input_dialog_open(const gchar *title, const gchar *message,
404                                 const gchar *checkbtn_label,
405                                 const gchar *default_string,
406                                 gboolean default_checkbtn_state,
407                                 gboolean *remember)
408 {
409         gchar *str;
410
411         if (dialog && gtk_widget_get_visible(dialog)) return NULL;
412
413         if (!dialog)
414                 input_dialog_create(FALSE);
415
416         if (checkbtn_label)
417                 gtk_button_set_label(GTK_BUTTON(remember_checkbtn), checkbtn_label);
418         else
419                 gtk_button_set_label(GTK_BUTTON(remember_checkbtn), _("Remember this"));
420
421         input_dialog_set(title, message, default_string);
422         gtk_window_present(GTK_WINDOW(dialog));
423
424         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(remember_checkbtn),
425                                      default_checkbtn_state);
426         if (remember)
427                 gtk_widget_show(remember_checkbtn);
428         else
429                 gtk_widget_hide(remember_checkbtn);
430
431         gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
432         manage_window_set_transient(GTK_WINDOW(dialog));
433
434         ack = fin = FALSE;
435
436         while (fin == FALSE)
437                 gtk_main_iteration();
438
439         manage_window_focus_out(dialog, NULL, NULL);
440
441         if (ack) {
442                 GtkEditable *editable;
443
444                 if (type == INPUT_DIALOG_COMBO)
445                         editable = GTK_EDITABLE(gtk_bin_get_child(GTK_BIN((combo))));
446                 else
447                         editable = GTK_EDITABLE(entry);
448
449                 str = gtk_editable_get_chars(editable, 0, -1);
450                 if (str && *str == '\0' && !is_pass) {
451                         g_free(str);
452                         str = NULL;
453                 }
454         } else
455                 str = NULL;
456
457         GTK_EVENTS_FLUSH();
458
459         if (remember) {
460                 *remember = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(remember_checkbtn));
461         }
462
463         gtk_widget_destroy(dialog);
464         dialog = NULL;
465
466         if (is_pass)
467                 debug_print("return string = %s\n", str ? "********": ("none"));
468         else
469                 debug_print("return string = %s\n", str ? str : "(none)");
470         return str;
471 }
472
473 static void input_dialog_set(const gchar *title, const gchar *message,
474                              const gchar *default_string)
475 {
476         GtkWidget *entry_;
477
478         if (type == INPUT_DIALOG_COMBO)
479                 entry_ = gtk_bin_get_child(GTK_BIN((combo)));
480         else
481                 entry_ = entry;
482
483         gtk_window_set_title(GTK_WINDOW(dialog), title);
484         gtk_label_set_text(GTK_LABEL(msg_title), title);
485         gtk_label_set_text(GTK_LABEL(msg_label), message);
486         if (default_string && *default_string) {
487                 gtk_entry_set_text(GTK_ENTRY(entry_), default_string);
488                 gtk_editable_set_position(GTK_EDITABLE(entry_), 0);
489                 gtk_editable_select_region(GTK_EDITABLE(entry_), 0, -1);
490         } else
491                 gtk_entry_set_text(GTK_ENTRY(entry_), "");
492
493         gtk_widget_grab_focus(ok_button);
494         gtk_widget_grab_focus(entry_);
495 }
496
497 static void ok_clicked(GtkWidget *widget, gpointer data)
498 {
499         ack = TRUE;
500         fin = TRUE;
501 }
502
503 static void cancel_clicked(GtkWidget *widget, gpointer data)
504 {
505         ack = FALSE;
506         fin = TRUE;
507 }
508
509 static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data)
510 {
511         ack = FALSE;
512         fin = TRUE;
513
514         return TRUE;
515 }
516
517 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
518 {
519         if (event && event->keyval == GDK_KEY_Escape) {
520                 ack = FALSE;
521                 fin = TRUE;
522         } else if (event && event->keyval == GDK_KEY_Return) {
523                 ack = TRUE;
524                 fin = TRUE;
525                 return TRUE; /* do not let Return pass - it
526                               * pops up the combo on validating */
527         }
528
529         return FALSE;
530 }
531
532 static void entry_activated(GtkEditable *editable)
533 {
534         ack = TRUE;
535         fin = TRUE;
536 }
537
538 static void combo_activated(GtkEditable *editable)
539 {
540         ack = TRUE;
541         fin = TRUE;
542 }