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