Fix changelog
[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, FALSE, 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 && prefs_common.session_passwords) {
278                 if (*keep != NULL) {
279                         pass = g_strdup (*keep);
280                 }
281                 else {
282                         gboolean state = FALSE;
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                 }
292         }
293         else {
294                 pass = input_dialog_with_invisible(_("Input password"), message, NULL);
295         }               
296         g_free(message);
297
298         return pass;
299 }
300
301 static void input_dialog_create(gboolean is_password)
302 {
303         static PangoFontDescription *font_desc;
304         GtkWidget *w_hbox;
305         GtkWidget *hbox;
306         GtkWidget *vbox;
307         GtkWidget *cancel_button;
308         GtkWidget *confirm_area;
309
310         dialog = gtk_dialog_new();
311
312         gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE);
313         gtk_window_set_default_size(GTK_WINDOW(dialog), 375, 100);
314         gtk_window_set_title(GTK_WINDOW(dialog), "");
315         gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
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         gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (dialog)->vbox), 14);
324         hbox = gtk_hbox_new (FALSE, 12);
325         gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
326         gtk_widget_show (hbox);
327         gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), hbox,
328                             FALSE, FALSE, 0);
329
330         /* for title label */
331         w_hbox = gtk_hbox_new(FALSE, 0);
332         
333         icon_q = gtk_image_new_from_stock(GTK_STOCK_DIALOG_QUESTION,
334                                 GTK_ICON_SIZE_DIALOG); 
335         gtk_misc_set_alignment (GTK_MISC (icon_q), 0.5, 0.0);
336         gtk_box_pack_start (GTK_BOX (hbox), icon_q, FALSE, FALSE, 0);
337         icon_p = gtk_image_new_from_stock(GTK_STOCK_DIALOG_AUTHENTICATION,
338                                 GTK_ICON_SIZE_DIALOG); 
339         gtk_misc_set_alignment (GTK_MISC (icon_p), 0.5, 0.0);
340         gtk_box_pack_start (GTK_BOX (hbox), icon_p, FALSE, FALSE, 0);
341         
342         vbox = gtk_vbox_new (FALSE, 12);
343         gtk_box_pack_start (GTK_BOX (hbox), vbox, TRUE, TRUE, 0);
344         gtk_widget_show (vbox);
345         
346         msg_title = gtk_label_new("");
347         gtk_misc_set_alignment(GTK_MISC(msg_title), 0, 0.5);
348         gtk_label_set_justify(GTK_LABEL(msg_title), GTK_JUSTIFY_LEFT);
349         gtk_label_set_use_markup (GTK_LABEL (msg_title), TRUE);
350         gtk_box_pack_start(GTK_BOX(vbox), msg_title, FALSE, FALSE, 0);
351         gtk_label_set_line_wrap(GTK_LABEL(msg_title), TRUE);
352         if (!font_desc) {
353                 gint size;
354
355                 size = pango_font_description_get_size
356                         (msg_title->style->font_desc);
357                 font_desc = pango_font_description_new();
358                 pango_font_description_set_weight
359                         (font_desc, PANGO_WEIGHT_BOLD);
360                 pango_font_description_set_size
361                         (font_desc, size * PANGO_SCALE_LARGE);
362         }
363         if (font_desc)
364                 gtk_widget_modify_font(msg_title, font_desc);
365         
366         msg_label = gtk_label_new("");
367         gtk_misc_set_alignment(GTK_MISC(msg_label), 0, 0.5);
368         gtk_label_set_justify(GTK_LABEL(msg_label), GTK_JUSTIFY_LEFT);
369         gtk_box_pack_start(GTK_BOX(vbox), msg_label, FALSE, FALSE, 0);
370         gtk_widget_show(msg_label);
371                 
372         entry = gtk_entry_new();
373         gtk_box_pack_start(GTK_BOX(vbox), entry, FALSE, FALSE, 0);
374         g_signal_connect(G_OBJECT(entry), "activate",
375                          G_CALLBACK(entry_activated), NULL);
376
377         combo = gtk_combo_box_entry_new_text();
378         gtk_box_pack_start(GTK_BOX(vbox), combo, FALSE, FALSE, 0);
379         g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((combo)))), "activate",
380                          G_CALLBACK(combo_activated), NULL);
381
382         remember_checkbtn = gtk_check_button_new_with_label(_("Remember this"));
383         gtk_box_pack_start(GTK_BOX(vbox), remember_checkbtn, FALSE, FALSE, 0);
384
385         hbox = gtk_hbox_new(TRUE, 0);
386
387         gtkut_stock_button_set_create(&confirm_area,
388                                       &cancel_button, GTK_STOCK_CANCEL,
389                                       &ok_button, GTK_STOCK_OK,
390                                       NULL, NULL);
391
392         gtk_box_pack_end(GTK_BOX(GTK_DIALOG(dialog)->action_area),
393                          confirm_area, FALSE, FALSE, 0);
394         gtk_container_set_border_width(GTK_CONTAINER(confirm_area), 5);
395
396         gtk_widget_show_all(GTK_DIALOG(dialog)->vbox);
397         
398         gtk_widget_hide(remember_checkbtn);
399
400         if (is_password)
401                 gtk_widget_hide(icon_q);
402         else
403                 gtk_widget_hide(icon_p);
404
405         is_pass = is_password;
406
407         gtk_widget_grab_default(ok_button);
408
409         g_signal_connect(G_OBJECT(ok_button), "clicked",
410                          G_CALLBACK(ok_clicked), NULL);
411         g_signal_connect(G_OBJECT(cancel_button), "clicked",
412                          G_CALLBACK(cancel_clicked), NULL);
413 }
414
415 static gchar *input_dialog_open(const gchar *title, const gchar *message,
416                                 const gchar *checkbtn_label,
417                                 const gchar *default_string,
418                                 gboolean default_checkbtn_state,
419                                 gboolean *remember)
420 {
421         gchar *str;
422
423         if (dialog && GTK_WIDGET_VISIBLE(dialog)) return NULL;
424
425         if (!dialog)
426                 input_dialog_create(FALSE);
427
428         if (checkbtn_label)
429                 gtk_button_set_label(GTK_BUTTON(remember_checkbtn), checkbtn_label);
430         else
431                 gtk_button_set_label(GTK_BUTTON(remember_checkbtn), _("Remember this"));
432
433         input_dialog_set(title, message, default_string);
434         gtk_widget_show(dialog);
435
436         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(remember_checkbtn),
437                                      default_checkbtn_state);
438         if (remember)
439                 gtk_widget_show(remember_checkbtn);
440         else
441                 gtk_widget_hide(remember_checkbtn);
442
443         gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
444         manage_window_set_transient(GTK_WINDOW(dialog));
445
446         ack = fin = FALSE;
447
448         while (fin == FALSE)
449                 gtk_main_iteration();
450
451         manage_window_focus_out(dialog, NULL, NULL);
452         gtk_widget_hide(dialog);
453
454         if (ack) {
455                 GtkEditable *editable;
456
457                 if (type == INPUT_DIALOG_COMBO)
458                         editable = GTK_EDITABLE(gtk_bin_get_child(GTK_BIN((combo))));
459                 else
460                         editable = GTK_EDITABLE(entry);
461
462                 str = gtk_editable_get_chars(editable, 0, -1);
463                 if (str && *str == '\0' && !is_pass) {
464                         g_free(str);
465                         str = NULL;
466                 }
467         } else
468                 str = NULL;
469
470         GTK_EVENTS_FLUSH();
471
472         if (remember) {
473                 *remember = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(remember_checkbtn));
474         }
475         if (is_pass)
476                 debug_print("return string = %s\n", str ? "********": ("none"));
477         else
478                 debug_print("return string = %s\n", str ? str : "(none)");
479         return str;
480 }
481
482 static void input_dialog_set(const gchar *title, const gchar *message,
483                              const gchar *default_string)
484 {
485         GtkWidget *entry_;
486
487         if (type == INPUT_DIALOG_COMBO)
488                 entry_ = gtk_bin_get_child(GTK_BIN((combo)));
489         else
490                 entry_ = entry;
491
492         gtk_window_set_title(GTK_WINDOW(dialog), title);
493         gtk_label_set_text(GTK_LABEL(msg_title), title);
494         gtk_label_set_text(GTK_LABEL(msg_label), message);
495         if (default_string && *default_string) {
496                 gtk_entry_set_text(GTK_ENTRY(entry_), default_string);
497                 gtk_editable_set_position(GTK_EDITABLE(entry_), 0);
498                 gtk_editable_select_region(GTK_EDITABLE(entry_), 0, -1);
499         } else
500                 gtk_entry_set_text(GTK_ENTRY(entry_), "");
501
502         gtk_widget_grab_focus(ok_button);
503         gtk_widget_grab_focus(entry_);
504 }
505
506 static void ok_clicked(GtkWidget *widget, gpointer data)
507 {
508         ack = TRUE;
509         fin = TRUE;
510 }
511
512 static void cancel_clicked(GtkWidget *widget, gpointer data)
513 {
514         ack = FALSE;
515         fin = TRUE;
516 }
517
518 static gint delete_event(GtkWidget *widget, GdkEventAny *event, gpointer data)
519 {
520         ack = FALSE;
521         fin = TRUE;
522
523         return TRUE;
524 }
525
526 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
527 {
528         if (event && event->keyval == GDK_Escape) {
529                 ack = FALSE;
530                 fin = TRUE;
531         } else if (event && event->keyval == GDK_Return) {
532                 ack = TRUE;
533                 fin = TRUE;
534                 return TRUE; /* do not let Return pass - it
535                               * pops up the combo on validating */
536         }
537
538         return FALSE;
539 }
540
541 static void entry_activated(GtkEditable *editable)
542 {
543         ack = TRUE;
544         fin = TRUE;
545 }
546
547 static void combo_activated(GtkEditable *editable)
548 {
549         ack = TRUE;
550         fin = TRUE;
551 }