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