2c6c673bc81c789937f18db7babd0aa00aff3736
[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 "prefs_folder_item.h"
41 #include "compose.h"
42 #include "manage_window.h"
43 #include "inc.h"
44 #include "gtkutils.h"
45 #include "utils.h"
46 #include "alertpanel.h"
47
48 #include "pixmaps/mark.xpm"
49
50 typedef enum
51 {
52         COL_DEFAULT     = 0,
53         COL_GETALL      = 1,
54         COL_NAME        = 2,
55         COL_PROTOCOL    = 3,
56         COL_SERVER      = 4
57 } EditAccountColumnPos;
58
59 # define N_EDIT_ACCOUNT_COLS    5
60
61 #define PREFSBUFSIZE            1024
62
63 PrefsAccount *cur_account;
64
65 static GList *account_list = NULL;
66
67 static struct EditAccount {
68         GtkWidget *window;
69         GtkWidget *clist;
70         GtkWidget *close_btn;
71 } edit_account;
72
73 static GdkPixmap *markxpm;
74 static GdkBitmap *markxpmmask;
75
76 static void account_edit_create         (void);
77
78 static void account_edit_prefs          (void);
79 static void account_delete              (void);
80
81 static void account_up                  (void);
82 static void account_down                (void);
83
84 static void account_set_default         (void);
85
86 static void account_set_recv_at_get_all (void);
87
88 static void account_edit_close          (void);
89 static gint account_delete_event        (GtkWidget      *widget,
90                                          GdkEventAny    *event,
91                                          gpointer        data);
92 static void account_selected            (GtkCList       *clist,
93                                          gint            row,
94                                          gint            column,
95                                          GdkEvent       *event,
96                                          gpointer        data);
97 static void account_key_pressed         (GtkWidget      *widget,
98                                          GdkEventKey    *event,
99                                          gpointer        data);
100
101 static gint account_clist_set_row       (PrefsAccount   *ac_prefs,
102                                          gint            row);
103 static void account_clist_set           (void);
104
105 static void account_list_set            (void);
106
107 void account_read_config_all(void)
108 {
109         GSList *ac_label_list = NULL, *cur;
110         gchar *rcpath;
111         FILE *fp;
112         gchar buf[PREFSBUFSIZE];
113         PrefsAccount *ac_prefs;
114
115         debug_print(_("Reading all config for each account...\n"));
116
117         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACCOUNT_RC, NULL);
118         if ((fp = fopen(rcpath, "r")) == NULL) {
119                 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
120                 g_free(rcpath);
121                 return;
122         }
123         g_free(rcpath);
124
125         while (fgets(buf, sizeof(buf), fp) != NULL) {
126                 if (!strncmp(buf, "[Account: ", 10)) {
127                         strretchomp(buf);
128                         memmove(buf, buf + 1, strlen(buf));
129                         buf[strlen(buf) - 1] = '\0';
130                         debug_print(_("Found label: %s\n"), buf);
131                         ac_label_list = g_slist_append(ac_label_list,
132                                                        g_strdup(buf));
133                 }
134         }
135         fclose(fp);
136
137         /* read config data from file */
138         cur_account = NULL;
139         for (cur = ac_label_list; cur != NULL; cur = cur->next) {
140                 ac_prefs = g_new0(PrefsAccount, 1);
141                 prefs_account_read_config(ac_prefs, (gchar *)cur->data);
142                 account_list = g_list_append(account_list, ac_prefs);
143                 if (ac_prefs->is_default)
144                         cur_account = ac_prefs;
145         }
146         /* if default is not set, assume first account as default */
147         if (!cur_account && account_list) {
148                 ac_prefs = (PrefsAccount *)account_list->data;
149                 account_set_as_default(ac_prefs);
150                 cur_account = ac_prefs;
151         }
152
153         account_set_menu();
154         main_window_reflect_prefs_all();
155
156         while (ac_label_list) {
157                 g_free(ac_label_list->data);
158                 ac_label_list = g_slist_remove(ac_label_list,
159                                                ac_label_list->data);
160         }
161 }
162
163 void account_save_config_all(void)
164 {
165         prefs_account_save_config_all(account_list);
166 }
167
168 PrefsAccount *account_find_from_smtp_server(const gchar *address,
169                                             const gchar *smtp_server)
170 {
171         GList *cur;
172         PrefsAccount *ac;
173
174         for (cur = account_list; cur != NULL; cur = cur->next) {
175                 ac = (PrefsAccount *)cur->data;
176                 if (!strcmp2(address, ac->address) &&
177                     !strcmp2(smtp_server, ac->smtp_server))
178                         return ac;
179         }
180
181         return NULL;
182 }
183
184 /*
185  * account_find_from_address:
186  * @address: Email address string.
187  *
188  * Find a mail (not news) account with the specified email address.
189  *
190  * Return value: The found account, or NULL if not found.
191  */
192 PrefsAccount *account_find_from_address(const gchar *address)
193 {
194         GList *cur;
195         PrefsAccount *ac;
196
197         for (cur = account_list; cur != NULL; cur = cur->next) {
198                 ac = (PrefsAccount *)cur->data;
199                 if (ac->protocol != A_NNTP && !strstr2(address, ac->address))
200                         return ac;
201         }
202
203         return NULL;
204 }
205
206 PrefsAccount *account_find_from_id(gint id)
207 {
208         GList *cur;
209         PrefsAccount *ac;
210
211         for (cur = account_list; cur != NULL; cur = cur->next) {
212                 ac = (PrefsAccount *)cur->data;
213                 if (id == ac->account_id)
214                         return ac;
215         }
216
217         return NULL;
218 }
219
220 void account_set_menu(void)
221 {
222         main_window_set_account_menu(account_list);
223 }
224
225 void account_foreach(AccountFunc func, gpointer user_data)
226 {
227         GList *cur;
228
229         for (cur = account_list; cur != NULL; cur = cur->next)
230                 if (func((PrefsAccount *)cur->data, user_data) != 0)
231                         return;
232 }
233
234 GList *account_get_list(void)
235 {
236         return account_list;
237 }
238
239 void account_edit_open(void)
240 {
241         inc_lock();
242
243         if (compose_get_compose_list()) {
244                 alertpanel_notice(_("Some composing windows are open.\n"
245                                     "Please close all the composing windows before editing the accounts."));
246                 inc_unlock();
247                 return;
248         }
249
250         debug_print(_("Opening account edit window...\n"));
251
252         if (!edit_account.window)
253                 account_edit_create();
254
255         account_clist_set();
256
257         manage_window_set_transient(GTK_WINDOW(edit_account.window));
258         gtk_widget_grab_focus(edit_account.close_btn);
259         gtk_widget_show(edit_account.window);
260
261         manage_window_focus_in(edit_account.window, NULL, NULL);
262 }
263
264 void account_add(void)
265 {
266         PrefsAccount *ac_prefs;
267
268         ac_prefs = prefs_account_open(NULL);
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, 100);
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
563         if (!prev_default && ac_prefs->is_default)
564                 account_set_as_default(ac_prefs);
565
566         if ((ac_prefs->protocol == A_IMAP4 || ac_prefs->protocol == A_NNTP) &&
567             ac_prefs->folder && strcmp(ac_name, ac_prefs->account_name) != 0) {
568                 folder_set_name(FOLDER(ac_prefs->folder),
569                                 ac_prefs->account_name);
570                 folderview_update_all();
571         }
572
573         account_clist_set();
574 }
575
576 static void account_delete_references_recursive(const GNode *node, const gint account)
577 {
578         /* the son */
579         if (node->data) {
580                 FolderItem *item = node->data;
581                 if (item->prefs) /* && item->prefs->stype == F_NORMAL */
582                         if (item->prefs->default_account == account) {
583                                 item->prefs->enable_default_account = FALSE;
584                                 item->prefs->default_account = 0;
585                                 prefs_folder_item_save_config(item);
586                         }
587         }
588         /* its children (vertical dive) */
589         if (node->children)
590                 account_delete_references_recursive(node->children, account);
591         /* its brothers (horizontal dive) */
592         if (node->next)
593                 account_delete_references_recursive(node->next, account);
594 }
595
596 static void account_delete(void)
597 {
598         GtkCList *clist = GTK_CLIST(edit_account.clist);
599         PrefsAccount *ac_prefs;
600         gint row;
601         GList *list;
602         Folder *folder;
603         
604         if (!clist->selection) return;
605
606         if (alertpanel(_("Delete account"),
607                        _("Do you really want to delete this account?"),
608                        _("Yes"), _("+No"), NULL) != G_ALERTDEFAULT)
609                 return;
610
611         row = GPOINTER_TO_INT(clist->selection->data);
612         ac_prefs = gtk_clist_get_row_data(clist, row);
613         if (ac_prefs->folder) {
614                 folder_destroy(FOLDER(ac_prefs->folder));
615                 folderview_update_all();
616         }
617         account_destroy(ac_prefs);
618         account_clist_set();
619
620         debug_print(_("Removing deleted account references for all the folders...\n"));
621         list = folder_get_list();
622         for (; list != NULL; list = list->next) {
623                 folder = FOLDER(list->data);
624                 if (folder->node)  /* && folder->type == F_? */
625                         account_delete_references_recursive(folder->node, ac_prefs->account_id);
626         }
627 }
628
629 static void account_up(void)
630 {
631         GtkCList *clist = GTK_CLIST(edit_account.clist);
632         gint row;
633
634         if (!clist->selection) return;
635
636         row = GPOINTER_TO_INT(clist->selection->data);
637         if (row > 0) {
638                 gtk_clist_row_move(clist, row, row - 1);
639                 account_list_set();
640         }
641 }
642
643 static void account_down(void)
644 {
645         GtkCList *clist = GTK_CLIST(edit_account.clist);
646         gint row;
647
648         if (!clist->selection) return;
649
650         row = GPOINTER_TO_INT(clist->selection->data);
651         if (row < clist->rows - 1) {
652                 gtk_clist_row_move(clist, row, row + 1);
653                 account_list_set();
654         }
655 }
656
657 static void account_set_default(void)
658 {
659         GtkCList *clist = GTK_CLIST(edit_account.clist);
660         gint row;
661         PrefsAccount *ac_prefs;
662
663         if (!clist->selection) return;
664
665         row = GPOINTER_TO_INT(clist->selection->data);
666         ac_prefs = gtk_clist_get_row_data(clist, row);
667         account_set_as_default(ac_prefs);
668         account_clist_set();
669
670         cur_account = ac_prefs;
671         account_set_menu();
672         main_window_reflect_prefs_all();
673 }
674
675 static void account_set_recv_at_get_all(void)
676 {
677         GtkCList *clist = GTK_CLIST(edit_account.clist);
678         gint row;
679         PrefsAccount *ac_prefs;
680
681         if (!clist->selection) return;
682
683         row = GPOINTER_TO_INT(clist->selection->data);
684         ac_prefs = gtk_clist_get_row_data(clist, row);
685
686         if ((ac_prefs->protocol != A_POP3) && (ac_prefs->protocol != A_APOP)) return;
687         
688         account_set_as_recv_at_get_all(ac_prefs);
689         account_clist_set();
690 }
691
692 static void account_edit_close(void)
693 {
694         account_list_set();
695         account_save_config_all();
696
697         if (!cur_account && account_list) {
698                 PrefsAccount *ac_prefs = (PrefsAccount *)account_list->data;
699                 account_set_as_default(ac_prefs);
700                 cur_account = ac_prefs;
701         }
702
703         account_set_menu();
704         main_window_reflect_prefs_all();
705
706         gtk_widget_hide(edit_account.window);
707
708         inc_unlock();
709 }
710
711 static gint account_delete_event(GtkWidget *widget, GdkEventAny *event,
712                                  gpointer data)
713 {
714         account_edit_close();
715         return TRUE;
716 }
717
718 static void account_selected(GtkCList *clist, gint row, gint column,
719                              GdkEvent *event, gpointer data)
720 {
721         if (event && event->type == GDK_2BUTTON_PRESS)
722                 account_edit_prefs();
723 }
724
725 static void account_key_pressed(GtkWidget *widget, GdkEventKey *event,
726                                 gpointer data)
727 {
728         if (event && event->keyval == GDK_Escape)
729                 account_edit_close();
730 }
731
732 /* set one CList row or add new row */
733 static gint account_clist_set_row(PrefsAccount *ac_prefs, gint row)
734 {
735         GtkCList *clist = GTK_CLIST(edit_account.clist);
736         gchar *text[N_EDIT_ACCOUNT_COLS];
737
738         text[COL_DEFAULT] = ac_prefs->is_default ? "*" : "";
739         text[COL_GETALL] = (ac_prefs->protocol == A_POP3 ||
740                             ac_prefs->protocol == A_APOP) &&
741                             ac_prefs->recv_at_getall ? "*" : "";
742         text[COL_NAME] = ac_prefs->account_name;
743 #if USE_SSL
744         text[COL_PROTOCOL] = ac_prefs->protocol == A_POP3 ?
745                              (ac_prefs->ssl_pop ? "POP3 (SSL)" : "POP3") :
746                              ac_prefs->protocol == A_APOP ?
747                              (ac_prefs->ssl_pop ?
748                               "POP3 (APOP, SSL)" : "POP3 (APOP)") :
749                              ac_prefs->protocol == A_IMAP4 ?
750                              (ac_prefs->ssl_imap ? "IMAP4 (SSL)" : "IMAP4") :
751                              ac_prefs->protocol == A_LOCAL ? "Local" :
752                              ac_prefs->protocol == A_NNTP ? "NNTP"  :  "";
753 #else
754         text[COL_PROTOCOL] = ac_prefs->protocol == A_POP3  ? "POP3" :
755                              ac_prefs->protocol == A_APOP  ? "POP3 (APOP)" :
756                              ac_prefs->protocol == A_IMAP4 ? "IMAP4" :
757                              ac_prefs->protocol == A_LOCAL ? "Local" :
758                              ac_prefs->protocol == A_NNTP  ? "NNTP" : "";
759 #endif
760         text[COL_SERVER] = ac_prefs->protocol == A_NNTP
761                 ? ac_prefs->nntp_server : ac_prefs->recv_server;
762
763         if (row < 0)
764                 row = gtk_clist_append(clist, text);
765         else {
766                 gtk_clist_set_text(clist, row, COL_DEFAULT, text[COL_DEFAULT]);
767                 gtk_clist_set_text(clist, row, COL_GETALL, text[COL_GETALL]);
768                 gtk_clist_set_text(clist, row, COL_NAME, text[COL_NAME]);
769                 gtk_clist_set_text(clist, row, COL_PROTOCOL, text[COL_PROTOCOL]);
770                 gtk_clist_set_text(clist, row, COL_SERVER, text[COL_SERVER]);
771         }
772
773         if (*text[COL_DEFAULT])
774                 gtk_clist_set_pixmap(clist, row, COL_DEFAULT,
775                                      markxpm, markxpmmask);
776         if (*text[COL_GETALL])
777                 gtk_clist_set_pixmap(clist, row, COL_GETALL,
778                                      markxpm, markxpmmask);
779
780         gtk_clist_set_row_data(clist, row, ac_prefs);
781
782         return row;
783 }
784
785 /* set CList from account list */
786 static void account_clist_set(void)
787 {
788         GtkCList *clist = GTK_CLIST(edit_account.clist);
789         GList *cur;
790         gint prev_row;
791
792         if (clist->selection)
793                 prev_row = GPOINTER_TO_INT(clist->selection->data);
794         else
795                 prev_row = -1;
796
797         gtk_clist_freeze(clist);
798         gtk_clist_clear(clist);
799
800         for (cur = account_list; cur != NULL; cur = cur->next) {
801                 gint row;
802
803                 row = account_clist_set_row((PrefsAccount *)cur->data, -1);
804                 if ((PrefsAccount *)cur->data == cur_account) {
805                         gtk_clist_select_row(clist, row, -1);
806                         gtkut_clist_set_focus_row(clist, row);
807                 }
808         }
809
810         if (prev_row >= 0) {
811                 gtk_clist_select_row(clist, prev_row, -1);
812                 gtkut_clist_set_focus_row(clist, prev_row);
813         }
814
815         gtk_clist_thaw(clist);
816 }
817
818 /* set account list from CList */
819 static void account_list_set(void)
820 {
821         GtkCList *clist = GTK_CLIST(edit_account.clist);
822         gint row;
823         PrefsAccount *ac_prefs;
824
825         while (account_list)
826                 account_list = g_list_remove(account_list, account_list->data);
827
828         for (row = 0; (ac_prefs = gtk_clist_get_row_data(clist, row)) != NULL;
829              row++)
830                 account_list = g_list_append(account_list, ac_prefs);
831 }