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