added 'recieve at get all' to 'Edit accounts' window
[claws.git] / src / account.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 Hiroyuki Yamamoto
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 2 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, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27 #include <gtk/gtk.h>
28 #include <gdk/gdkkeysyms.h>
29 #include <stdio.h>
30 #include <errno.h>
31
32 #include "intl.h"
33 #include "main.h"
34 #include "mainwindow.h"
35 #include "folderview.h"
36 #include "folder.h"
37 #include "account.h"
38 #include "prefs.h"
39 #include "prefs_account.h"
40 #include "compose.h"
41 #include "manage_window.h"
42 #include "inc.h"
43 #include "gtkutils.h"
44 #include "utils.h"
45 #include "alertpanel.h"
46
47 typedef enum
48 {
49         COL_DEFAULT     = 0,
50         COL_NAME        = 1,
51         COL_PROTOCOL    = 2,
52         COL_SERVER      = 3,
53         COL_GETALL      = 4
54 } EditAccountColumnPos;
55
56 # define N_EDIT_ACCOUNT_COLS    5
57
58 #define PREFSBUFSIZE            1024
59
60 PrefsAccount *cur_account;
61
62 static GList *account_list = NULL;
63
64 static struct EditAccount {
65         GtkWidget *window;
66         GtkWidget *clist;
67         GtkWidget *close_btn;
68 } edit_account;
69
70 static void account_edit_create         (void);
71
72 static void account_edit_prefs          (void);
73 static void account_delete              (void);
74
75 static void account_up                  (void);
76 static void account_down                (void);
77
78 static void account_set_default         (void);
79
80 static void account_set_recv_at_get_all (void);
81
82 static void account_edit_close          (void);
83 static gint account_delete_event        (GtkWidget      *widget,
84                                          GdkEventAny    *event,
85                                          gpointer        data);
86 static void account_selected            (GtkCList       *clist,
87                                          gint            row,
88                                          gint            column,
89                                          GdkEvent       *event,
90                                          gpointer        data);
91 static void account_key_pressed         (GtkWidget      *widget,
92                                          GdkEventKey    *event,
93                                          gpointer        data);
94
95 static gint account_clist_set_row       (PrefsAccount   *ac_prefs,
96                                          gint            row);
97 static void account_clist_set           (void);
98
99 static void account_list_set            (void);
100
101 void account_read_config_all(void)
102 {
103         GSList *ac_label_list = NULL, *cur;
104         gchar *rcpath;
105         FILE *fp;
106         gchar buf[PREFSBUFSIZE];
107         PrefsAccount *ac_prefs;
108
109         debug_print(_("Reading all config for each account...\n"));
110
111         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACCOUNT_RC, NULL);
112         if ((fp = fopen(rcpath, "r")) == NULL) {
113                 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
114                 g_free(rcpath);
115                 return;
116         }
117         g_free(rcpath);
118
119         while (fgets(buf, sizeof(buf), fp) != NULL) {
120                 if (!strncmp(buf, "[Account: ", 10)) {
121                         strretchomp(buf);
122                         memmove(buf, buf + 1, strlen(buf));
123                         buf[strlen(buf) - 1] = '\0';
124                         debug_print(_("Found label: %s\n"), buf);
125                         ac_label_list = g_slist_append(ac_label_list,
126                                                        g_strdup(buf));
127                 }
128         }
129         fclose(fp);
130
131         /* read config data from file */
132         cur_account = NULL;
133         for (cur = ac_label_list; cur != NULL; cur = cur->next) {
134                 ac_prefs = g_new0(PrefsAccount, 1);
135                 prefs_account_read_config(ac_prefs, (gchar *)cur->data);
136                 account_list = g_list_append(account_list, ac_prefs);
137                 if (ac_prefs->is_default)
138                         cur_account = ac_prefs;
139         }
140         /* if default is not set, assume first account as default */
141         if (!cur_account && account_list) {
142                 ac_prefs = (PrefsAccount *)account_list->data;
143                 account_set_as_default(ac_prefs);
144                 cur_account = ac_prefs;
145         }
146
147         account_set_menu();
148         main_window_reflect_prefs_all();
149
150         while (ac_label_list) {
151                 g_free(ac_label_list->data);
152                 ac_label_list = g_slist_remove(ac_label_list,
153                                                ac_label_list->data);
154         }
155 }
156
157 void account_save_config_all(void)
158 {
159         prefs_account_save_config_all(account_list);
160 }
161
162 PrefsAccount *account_find_from_smtp_server(const gchar *address,
163                                             const gchar *smtp_server)
164 {
165         GList *cur;
166         PrefsAccount *ac;
167
168         for (cur = account_list; cur != NULL; cur = cur->next) {
169                 ac = (PrefsAccount *)cur->data;
170                 if (!strcmp2(address, ac->address) &&
171                     !strcmp2(smtp_server, ac->smtp_server))
172                         return ac;
173         }
174
175         return NULL;
176 }
177
178 /*
179  * account_find_from_address:
180  * @address: Email address string.
181  *
182  * Find a mail (not news) account with the specified email address.
183  *
184  * Return value: The found account, or NULL if not found.
185  */
186 PrefsAccount *account_find_from_address(const gchar *address)
187 {
188         GList *cur;
189         PrefsAccount *ac;
190
191         for (cur = account_list; cur != NULL; cur = cur->next) {
192                 ac = (PrefsAccount *)cur->data;
193                 if (ac->protocol != A_NNTP && !strcmp2(address, ac->address))
194                         return ac;
195         }
196
197         return NULL;
198 }
199
200 PrefsAccount *account_find_from_id(gint id)
201 {
202         GList *cur;
203         PrefsAccount *ac;
204
205         for (cur = account_list; cur != NULL; cur = cur->next) {
206                 ac = (PrefsAccount *)cur->data;
207                 if (id == ac->account_id)
208                         return ac;
209         }
210
211         return NULL;
212 }
213
214 void account_set_menu(void)
215 {
216         main_window_set_account_menu(account_list);
217 }
218
219 void account_foreach(AccountFunc func, gpointer user_data)
220 {
221         GList *cur;
222
223         for (cur = account_list; cur != NULL; cur = cur->next)
224                 if (func((PrefsAccount *)cur->data, user_data) != 0)
225                         return;
226 }
227
228 GList *account_get_list(void)
229 {
230         return account_list;
231 }
232
233 void account_edit_open(void)
234 {
235         inc_autocheck_timer_remove();
236
237         if (compose_get_compose_list()) {
238                 alertpanel_notice(_("Some composing windows are open.\n"
239                                     "Please close all the composing windows before editing the accounts."));
240                 inc_autocheck_timer_set();
241                 return;
242         }
243
244         debug_print(_("Opening account edit window...\n"));
245
246         if (!edit_account.window)
247                 account_edit_create();
248
249         account_clist_set();
250
251         manage_window_set_transient(GTK_WINDOW(edit_account.window));
252         gtk_widget_grab_focus(edit_account.close_btn);
253         gtk_widget_show(edit_account.window);
254
255         manage_window_focus_in(edit_account.window, NULL, NULL);
256 }
257
258 void account_add(void)
259 {
260         PrefsAccount *ac_prefs;
261
262         ac_prefs = prefs_account_open(NULL);
263         inc_autocheck_timer_remove();
264
265         if (!ac_prefs) return;
266
267         account_list = g_list_append(account_list, ac_prefs);
268
269         if (ac_prefs->is_default)
270                 account_set_as_default(ac_prefs);
271
272         if (ac_prefs->recv_at_getall)
273                 account_set_as_recv_at_get_all(ac_prefs);
274
275         account_clist_set();
276
277         if (ac_prefs->protocol == A_IMAP4 || ac_prefs->protocol == A_NNTP) {
278                 Folder *folder;
279
280                 if (ac_prefs->protocol == A_IMAP4) {
281                         folder = folder_new(F_IMAP, ac_prefs->account_name,
282                                             ac_prefs->recv_server);
283                 } else {
284                         folder = folder_new(F_NEWS, ac_prefs->account_name,
285                                             ac_prefs->nntp_server);
286                 }
287
288                 folder->account = ac_prefs;
289                 ac_prefs->folder = REMOTE_FOLDER(folder);
290                 folder_add(folder);
291                 if (ac_prefs->protocol == A_IMAP4)
292                         folder->create_tree(folder);
293                 folderview_set_all();
294         }
295 }
296
297 void account_set_as_default(PrefsAccount *ac_prefs)
298 {
299         PrefsAccount *ap;
300         GList *cur;
301
302         for (cur = account_list; cur != NULL; cur = cur->next) {
303                 ap = (PrefsAccount *)cur->data;
304                 if (ap->is_default)
305                         ap->is_default = FALSE;
306         }
307
308         ac_prefs->is_default = TRUE;
309 }
310
311 PrefsAccount *account_get_default(void)
312 {
313         PrefsAccount *ap;
314         GList *cur;
315
316         for (cur = account_list; cur != NULL; cur = cur->next) {
317                 ap = (PrefsAccount *)cur->data;
318                 if (ap->is_default)
319                         return ap;
320         }
321
322         return NULL;
323 }
324
325 void account_set_as_recv_at_get_all(PrefsAccount *ac_prefs)
326 {
327         PrefsAccount *ap;
328         GList *cur;
329
330         for (cur = account_list; cur != NULL; cur = cur->next) {
331                 ap = (PrefsAccount *)cur->data;
332                 if (ap->account_name == ac_prefs->account_name) {
333                         if (ap->recv_at_getall == 0)
334                                 ap->recv_at_getall = 1;
335                         else
336                                 ap->recv_at_getall = 0;
337                 }
338         }
339
340 }
341
342
343 void account_set_missing_folder(void)
344 {
345         PrefsAccount *ap;
346         GList *cur;
347
348         for (cur = account_list; cur != NULL; cur = cur->next) {
349                 ap = (PrefsAccount *)cur->data;
350                 if ((ap->protocol == A_IMAP4 || ap->protocol == A_NNTP) &&
351                     !ap->folder) {
352                         Folder *folder;
353
354                         if (ap->protocol == A_IMAP4) {
355                                 folder = folder_new(F_IMAP, ap->account_name,
356                                                     ap->recv_server);
357                         } else {
358                                 folder = folder_new(F_NEWS, ap->account_name,
359                                                     ap->nntp_server);
360                         }
361
362                         folder->account = ap;
363                         ap->folder = REMOTE_FOLDER(folder);
364                         folder_add(folder);
365                         if (ap->protocol == A_IMAP4)
366                                 folder->create_tree(folder);
367                 }
368         }
369 }
370
371 void account_destroy(PrefsAccount *ac_prefs)
372 {
373         g_return_if_fail(ac_prefs != NULL);
374
375         prefs_account_free(ac_prefs);
376         account_list = g_list_remove(account_list, ac_prefs);
377
378         if (cur_account == ac_prefs) cur_account = NULL;
379         if (!cur_account && account_list) {
380                 cur_account = account_get_default();
381                 if (!cur_account) {
382                         ac_prefs = (PrefsAccount *)account_list->data;
383                         account_set_as_default(ac_prefs);
384                         cur_account = ac_prefs;
385                 }
386         }
387 }
388
389
390 static void account_edit_create(void)
391 {
392         GtkWidget *window;
393         GtkWidget *vbox;
394         GtkWidget *hbox;
395         GtkWidget *scrolledwin;
396         GtkWidget *clist;
397         gchar *titles[N_EDIT_ACCOUNT_COLS];
398         gint i;
399
400         GtkWidget *vbox2;
401         GtkWidget *add_btn;
402         GtkWidget *edit_btn;
403         GtkWidget *del_btn;
404         GtkWidget *up_btn;
405         GtkWidget *down_btn;
406
407         GtkWidget *default_btn;
408
409         GtkWidget *recvatgetall_btn;
410
411         GtkWidget *hbbox;
412         GtkWidget *close_btn;
413
414         debug_print(_("Creating account edit window...\n"));
415
416         window = gtk_window_new (GTK_WINDOW_DIALOG);
417         gtk_widget_set_usize (window, 500, 320);
418         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
419         gtk_window_set_title (GTK_WINDOW (window), _("Edit accounts"));
420         gtk_window_set_modal (GTK_WINDOW (window), TRUE);
421         gtk_signal_connect (GTK_OBJECT (window), "delete_event",
422                             GTK_SIGNAL_FUNC (account_delete_event), NULL);
423         gtk_signal_connect (GTK_OBJECT (window), "key_press_event",
424                             GTK_SIGNAL_FUNC (account_key_pressed), NULL);
425         gtk_signal_connect (GTK_OBJECT (window), "focus_in_event",
426                             GTK_SIGNAL_FUNC (manage_window_focus_in), NULL);
427         gtk_signal_connect (GTK_OBJECT (window), "focus_out_event",
428                             GTK_SIGNAL_FUNC (manage_window_focus_out), NULL);
429
430         vbox = gtk_vbox_new (FALSE, 12);
431         gtk_widget_show (vbox);
432         gtk_container_add (GTK_CONTAINER (window), vbox);
433
434         hbox = gtk_hbox_new (FALSE, 8);
435         gtk_widget_show (hbox);
436         gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
437         gtk_container_set_border_width (GTK_CONTAINER (hbox), 2);
438
439         scrolledwin = gtk_scrolled_window_new (NULL, NULL);
440         gtk_widget_show (scrolledwin);
441         gtk_box_pack_start (GTK_BOX (hbox), scrolledwin, TRUE, TRUE, 0);
442         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwin),
443                                         GTK_POLICY_AUTOMATIC,
444                                         GTK_POLICY_AUTOMATIC);
445
446         titles[COL_DEFAULT]  = "";
447         titles[COL_NAME]     = _("Name");
448         titles[COL_PROTOCOL] = _("Protocol");
449         titles[COL_SERVER]   = _("Server");
450         titles[COL_GETALL]   = _("Get all");
451
452         clist = gtk_clist_new_with_titles (N_EDIT_ACCOUNT_COLS, titles);
453         gtk_widget_show (clist);
454         gtk_container_add (GTK_CONTAINER (scrolledwin), clist);
455         gtk_clist_set_column_width (GTK_CLIST(clist), COL_DEFAULT , 16);
456         gtk_clist_set_column_width (GTK_CLIST(clist), COL_NAME    , 100);
457         gtk_clist_set_column_width (GTK_CLIST(clist), COL_PROTOCOL, 70);
458         gtk_clist_set_column_width (GTK_CLIST(clist), COL_SERVER  , 100);
459         gtk_clist_set_column_width (GTK_CLIST(clist), COL_GETALL  , 20);
460         gtk_clist_set_selection_mode (GTK_CLIST(clist), GTK_SELECTION_BROWSE);
461
462         for (i = 0; i < N_EDIT_ACCOUNT_COLS; i++)
463                 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(clist)->column[i].button,
464                                        GTK_CAN_FOCUS);
465
466         gtk_signal_connect (GTK_OBJECT (clist), "select_row",
467                             GTK_SIGNAL_FUNC (account_selected), NULL);
468
469         vbox2 = gtk_vbox_new (FALSE, 0);
470         gtk_widget_show (vbox2);
471         gtk_box_pack_start (GTK_BOX (hbox), vbox2, FALSE, FALSE, 0);
472
473         add_btn = gtk_button_new_with_label (_("Add"));
474         gtk_widget_show (add_btn);
475         gtk_box_pack_start (GTK_BOX (vbox2), add_btn, FALSE, FALSE, 4);
476         gtk_signal_connect (GTK_OBJECT(add_btn), "clicked",
477                             GTK_SIGNAL_FUNC (account_add), NULL);
478
479         edit_btn = gtk_button_new_with_label (_("Edit"));
480         gtk_widget_show (edit_btn);
481         gtk_box_pack_start (GTK_BOX (vbox2), edit_btn, FALSE, FALSE, 4);
482         gtk_signal_connect (GTK_OBJECT(edit_btn), "clicked",
483                             GTK_SIGNAL_FUNC (account_edit_prefs), NULL);
484
485         del_btn = gtk_button_new_with_label (_(" Delete "));
486         gtk_widget_show (del_btn);
487         gtk_box_pack_start (GTK_BOX (vbox2), del_btn, FALSE, FALSE, 4);
488         gtk_signal_connect (GTK_OBJECT(del_btn), "clicked",
489                             GTK_SIGNAL_FUNC (account_delete), NULL);
490
491         down_btn = gtk_button_new_with_label (_("Down"));
492         gtk_widget_show (down_btn);
493         gtk_box_pack_end (GTK_BOX (vbox2), down_btn, FALSE, FALSE, 4);
494         gtk_signal_connect (GTK_OBJECT(down_btn), "clicked",
495                             GTK_SIGNAL_FUNC (account_down), NULL);
496
497         up_btn = gtk_button_new_with_label (_("Up"));
498         gtk_widget_show (up_btn);
499         gtk_box_pack_end (GTK_BOX (vbox2), up_btn, FALSE, FALSE, 4);
500         gtk_signal_connect (GTK_OBJECT(up_btn), "clicked",
501                             GTK_SIGNAL_FUNC (account_up), NULL);
502
503         hbox = gtk_hbox_new (FALSE, 8);
504         gtk_widget_show (hbox);
505         gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
506
507         vbox2 = gtk_vbox_new(FALSE, 0);
508         gtk_widget_show (vbox2);
509         gtk_box_pack_start (GTK_BOX (hbox), vbox2, FALSE, FALSE, 0);
510
511         default_btn = gtk_button_new_with_label (_(" Set as default account "));
512         gtk_widget_show (default_btn);
513         gtk_box_pack_start (GTK_BOX (vbox2), default_btn, TRUE, FALSE, 0);
514         gtk_signal_connect (GTK_OBJECT(default_btn), "clicked",
515                             GTK_SIGNAL_FUNC (account_set_default), NULL);
516
517         recvatgetall_btn = gtk_button_new_with_label (_(" Enable/Disable 'Receive at Get all' "));
518         gtk_widget_show (recvatgetall_btn);
519         gtk_box_pack_start (GTK_BOX (vbox2), recvatgetall_btn, TRUE, FALSE, 0);
520         gtk_signal_connect (GTK_OBJECT(recvatgetall_btn), "clicked",
521                             GTK_SIGNAL_FUNC (account_set_recv_at_get_all), NULL);
522
523         gtkut_button_set_create(&hbbox, &close_btn, _("Close"),
524                                 NULL, NULL, NULL, NULL);
525         gtk_widget_show(hbbox);
526         gtk_box_pack_end (GTK_BOX (hbox), hbbox, FALSE, FALSE, 0);
527         gtk_widget_grab_default (close_btn);
528
529         gtk_signal_connect (GTK_OBJECT (close_btn), "clicked",
530                             GTK_SIGNAL_FUNC (account_edit_close),
531                             NULL);
532
533         edit_account.window    = window;
534         edit_account.clist     = clist;
535         edit_account.close_btn = close_btn;
536 }
537
538 static void account_edit_prefs(void)
539 {
540         GtkCList *clist = GTK_CLIST(edit_account.clist);
541         PrefsAccount *ac_prefs;
542         gint row;
543         gboolean prev_default;
544         gchar *ac_name;
545
546         if (!clist->selection) return;
547
548         row = GPOINTER_TO_INT(clist->selection->data);
549         ac_prefs = gtk_clist_get_row_data(clist, row);
550         prev_default = ac_prefs->is_default;
551         Xstrdup_a(ac_name, ac_prefs->account_name, return);
552
553         prefs_account_open(ac_prefs);
554         inc_autocheck_timer_remove();
555
556         if (!prev_default && ac_prefs->is_default)
557                 account_set_as_default(ac_prefs);
558
559         if ((ac_prefs->protocol == A_IMAP4 || ac_prefs->protocol == A_NNTP) &&
560             ac_prefs->folder && strcmp(ac_name, ac_prefs->account_name) != 0) {
561                 folder_set_name(FOLDER(ac_prefs->folder),
562                                 ac_prefs->account_name);
563                 folderview_update_all();
564         }
565
566         account_clist_set();
567 }
568
569 static void account_delete(void)
570 {
571         GtkCList *clist = GTK_CLIST(edit_account.clist);
572         PrefsAccount *ac_prefs;
573         gint row;
574
575         if (!clist->selection) return;
576
577         if (alertpanel(_("Delete account"),
578                        _("Do you really want to delete this account?"),
579                        _("Yes"), _("+No"), NULL) != G_ALERTDEFAULT)
580                 return;
581
582         row = GPOINTER_TO_INT(clist->selection->data);
583         ac_prefs = gtk_clist_get_row_data(clist, row);
584         if (ac_prefs->folder) {
585                 folder_destroy(FOLDER(ac_prefs->folder));
586                 folderview_update_all();
587         }
588         account_destroy(ac_prefs);
589         account_clist_set();
590 }
591
592 static void account_up(void)
593 {
594         GtkCList *clist = GTK_CLIST(edit_account.clist);
595         gint row;
596
597         if (!clist->selection) return;
598
599         row = GPOINTER_TO_INT(clist->selection->data);
600         if (row > 0) {
601                 gtk_clist_row_move(clist, row, row - 1);
602                 account_list_set();
603         }
604 }
605
606 static void account_down(void)
607 {
608         GtkCList *clist = GTK_CLIST(edit_account.clist);
609         gint row;
610
611         if (!clist->selection) return;
612
613         row = GPOINTER_TO_INT(clist->selection->data);
614         if (row < clist->rows - 1) {
615                 gtk_clist_row_move(clist, row, row + 1);
616                 account_list_set();
617         }
618 }
619
620 static void account_set_default(void)
621 {
622         GtkCList *clist = GTK_CLIST(edit_account.clist);
623         gint row;
624         PrefsAccount *ac_prefs;
625
626         if (!clist->selection) return;
627
628         row = GPOINTER_TO_INT(clist->selection->data);
629         ac_prefs = gtk_clist_get_row_data(clist, row);
630         account_set_as_default(ac_prefs);
631         account_clist_set();
632
633         cur_account = ac_prefs;
634         account_set_menu();
635         main_window_reflect_prefs_all();
636 }
637
638 static void account_set_recv_at_get_all(void)
639 {
640         GtkCList *clist = GTK_CLIST(edit_account.clist);
641         gint row;
642         PrefsAccount *ac_prefs;
643
644         if (!clist->selection) return;
645
646         row = GPOINTER_TO_INT(clist->selection->data);
647         ac_prefs = gtk_clist_get_row_data(clist, row);
648         account_set_as_recv_at_get_all(ac_prefs);
649         account_clist_set();
650 }
651
652 static void account_edit_close(void)
653 {
654         account_list_set();
655         account_save_config_all();
656
657         if (!cur_account && account_list) {
658                 PrefsAccount *ac_prefs = (PrefsAccount *)account_list->data;
659                 account_set_as_default(ac_prefs);
660                 cur_account = ac_prefs;
661         }
662
663         account_set_menu();
664         main_window_reflect_prefs_all();
665
666         gtk_widget_hide(edit_account.window);
667
668         inc_autocheck_timer_set();
669 }
670
671 static gint account_delete_event(GtkWidget *widget, GdkEventAny *event,
672                                  gpointer data)
673 {
674         account_edit_close();
675         return TRUE;
676 }
677
678 static void account_selected(GtkCList *clist, gint row, gint column,
679                              GdkEvent *event, gpointer data)
680 {
681         if (event && event->type == GDK_2BUTTON_PRESS)
682                 account_edit_prefs();
683 }
684
685 static void account_key_pressed(GtkWidget *widget, GdkEventKey *event,
686                                 gpointer data)
687 {
688         if (event && event->keyval == GDK_Escape)
689                 account_edit_close();
690 }
691
692 /* set one CList row or add new row */
693 static gint account_clist_set_row(PrefsAccount *ac_prefs, gint row)
694 {
695         GtkCList *clist = GTK_CLIST(edit_account.clist);
696         gchar *text[N_EDIT_ACCOUNT_COLS];
697
698         text[COL_DEFAULT] = ac_prefs->is_default ? "*" : "";
699         text[COL_NAME] = ac_prefs->account_name;
700 #if !USE_SSL
701         text[COL_PROTOCOL] = ac_prefs->protocol == A_POP3  ? "POP3"  :
702                              ac_prefs->protocol == A_APOP  ? "APOP"  :
703                              ac_prefs->protocol == A_IMAP4 ? "IMAP4" :
704                              ac_prefs->protocol == A_LOCAL ? "Local" :
705                              ac_prefs->protocol == A_NNTP  ? "NNTP"  :  "";
706 #else
707         text[COL_PROTOCOL] = ac_prefs->protocol == A_POP3  ? (!ac_prefs->ssl_pop ? "POP3" : "POP3 (SSL)") :
708                              ac_prefs->protocol == A_APOP  ? (!ac_prefs->ssl_pop ? "APOP" : "APOP (SSL)") :
709                              ac_prefs->protocol == A_IMAP4 ? (!ac_prefs->ssl_imap ? "IMAP4" : "IMAP4 (SSL)") :
710                              ac_prefs->protocol == A_LOCAL ? "Local" :
711                              ac_prefs->protocol == A_NNTP  ? "NNTP"  :  "";
712 #endif
713         text[COL_SERVER] = ac_prefs->protocol == A_NNTP
714                 ? ac_prefs->nntp_server : ac_prefs->recv_server;
715
716         if (!ac_prefs->protocol == A_POP3) {
717         text[COL_GETALL] = ac_prefs->recv_at_getall == 1  ? _("No")  :
718                            ac_prefs->recv_at_getall == 0  ? _("No")   : "";
719         }
720         else {
721         text[COL_GETALL] = ac_prefs->recv_at_getall == 1  ? _("Yes")  :
722                            ac_prefs->recv_at_getall == 0  ? _("No")   : "";
723         }
724
725         if (row < 0)
726                 row = gtk_clist_append(clist, text);
727         else {
728                 gtk_clist_set_text(clist, row, COL_DEFAULT, text[COL_DEFAULT]);
729                 gtk_clist_set_text(clist, row, COL_NAME, text[COL_NAME]);
730                 gtk_clist_set_text(clist, row, COL_PROTOCOL, text[COL_PROTOCOL]);
731                 gtk_clist_set_text(clist, row, COL_SERVER, text[COL_SERVER]);
732                 gtk_clist_set_text(clist, row, COL_GETALL, text[COL_GETALL]);
733         }
734
735         gtk_clist_set_row_data(clist, row, ac_prefs);
736
737         return row;
738 }
739
740 /* set CList from account list */
741 static void account_clist_set(void)
742 {
743         GtkCList *clist = GTK_CLIST(edit_account.clist);
744         GList *cur;
745         gint prev_row;
746
747         if (clist->selection)
748                 prev_row = GPOINTER_TO_INT(clist->selection->data);
749         else
750                 prev_row = -1;
751
752         gtk_clist_freeze(clist);
753         gtk_clist_clear(clist);
754
755         for (cur = account_list; cur != NULL; cur = cur->next) {
756                 gint row;
757
758                 row = account_clist_set_row((PrefsAccount *)cur->data, -1);
759                 if ((PrefsAccount *)cur->data == cur_account) {
760                         gtk_clist_select_row(clist, row, -1);
761                         gtkut_clist_set_focus_row(clist, row);
762                 }
763         }
764
765         if (prev_row >= 0) {
766                 gtk_clist_select_row(clist, prev_row, -1);
767                 gtkut_clist_set_focus_row(clist, prev_row);
768         }
769
770         gtk_clist_thaw(clist);
771 }
772
773 /* set account list from CList */
774 static void account_list_set(void)
775 {
776         GtkCList *clist = GTK_CLIST(edit_account.clist);
777         gint row;
778         PrefsAccount *ac_prefs;
779
780         while (account_list)
781                 account_list = g_list_remove(account_list, account_list->data);
782
783         for (row = 0; (ac_prefs = gtk_clist_get_row_data(clist, row)) != NULL;
784              row++)
785                 account_list = g_list_append(account_list, ac_prefs);
786 }