* src/folder.[ch]
[claws.git] / src / account.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2002 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 "stock_pixmap.h"
44 #include "inc.h"
45 #include "gtkutils.h"
46 #include "utils.h"
47 #include "alertpanel.h"
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 static GdkPixmap *checkboxonxpm;
75 static GdkPixmap *checkboxonxpmmask;
76 static GdkPixmap *checkboxoffxpm;
77 static GdkPixmap *checkboxoffxpmmask;
78
79 static void account_edit_create         (void);
80
81 static void account_edit_prefs          (void);
82 static void account_delete              (void);
83
84 static void account_up                  (void);
85 static void account_down                (void);
86
87 static void account_set_default         (void);
88
89 static void account_edit_close          (void);
90 static gint account_delete_event        (GtkWidget      *widget,
91                                          GdkEventAny    *event,
92                                          gpointer        data);
93 static void account_selected            (GtkCList       *clist,
94                                          gint            row,
95                                          gint            column,
96                                          GdkEvent       *event,
97                                          gpointer        data);
98 static void account_row_moved           (GtkCList       *clist,
99                                          gint            source_row,
100                                          gint            dest_row);
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, "rb")) == 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 = prefs_account_new();
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 /*
173  * account_find_all_from_address:
174  * @ac_list: initial list of accounts. NULL to create a new one.
175  * Accounts found in the @address will be appended to this list.
176  * @address: Email address string.
177  *
178  * Find all the mail (not news) accounts within the specified address.
179  *
180  * Return value: the original accounts list with the found accounts appended.
181  */
182 GList *account_find_all_from_address(GList *ac_list, const gchar *address)
183 {
184         GList *cur;
185         PrefsAccount *ac;
186
187         if (address == NULL)
188                 return ac_list;
189
190         for (cur = account_list; cur != NULL; cur = cur->next) {
191                 ac = (PrefsAccount *)cur->data;
192                 if (ac->protocol != A_NNTP && ac->address &&
193                     strcasestr(address, ac->address) != NULL)
194                         ac_list = g_list_append(ac_list, ac);
195         }
196         return ac_list;
197 }
198         
199 PrefsAccount *account_find_from_smtp_server(const gchar *address,
200                                             const gchar *smtp_server)
201 {
202         GList *cur;
203         PrefsAccount *ac;
204
205         g_return_val_if_fail(address != NULL, NULL);
206         g_return_val_if_fail(smtp_server != NULL, NULL);
207
208         for (cur = account_list; cur != NULL; cur = cur->next) {
209                 ac = (PrefsAccount *)cur->data;
210                 if (!strcmp2(address, ac->address) &&
211                     !strcmp2(smtp_server, ac->smtp_server))
212                         return ac;
213         }
214
215         return NULL;
216 }
217
218 /*
219  * account_find_from_address:
220  * @address: Email address string.
221  *
222  * Find a mail (not news) account with the specified email address.
223  *
224  * Return value: The found account, or NULL if not found.
225  */
226 PrefsAccount *account_find_from_address(const gchar *address)
227 {
228         GList *cur;
229         PrefsAccount *ac;
230
231         g_return_val_if_fail(address != NULL, NULL);
232
233         for (cur = account_list; cur != NULL; cur = cur->next) {
234                 ac = (PrefsAccount *)cur->data;
235                 if (ac->protocol != A_NNTP && ac->address &&
236                     strcasestr(address, ac->address) != NULL)
237                         return ac;
238         }
239
240         return NULL;
241 }
242
243 PrefsAccount *account_find_from_id(gint id)
244 {
245         GList *cur;
246         PrefsAccount *ac;
247
248         for (cur = account_list; cur != NULL; cur = cur->next) {
249                 ac = (PrefsAccount *)cur->data;
250                 if (id == ac->account_id)
251                         return ac;
252         }
253
254         return NULL;
255 }
256
257 PrefsAccount *account_find_from_item(FolderItem *item)
258 {
259         PrefsAccount *ac;
260
261         g_return_val_if_fail(item != NULL, NULL);
262
263         ac = item->account;
264         if (!ac) {
265                 FolderItem *cur_item = item->parent;
266                 while (cur_item != NULL) {
267                         if (cur_item->account && cur_item->apply_sub) {
268                                 ac = cur_item->account;
269                                 break;
270                         }                               
271                         cur_item = cur_item->parent;
272                 }
273         }
274         if (!ac)
275                 ac = item->folder->account;
276
277         return ac;
278 }
279
280 void account_set_menu(void)
281 {
282         main_window_set_account_menu(account_list);
283 }
284
285 void account_foreach(AccountFunc func, gpointer user_data)
286 {
287         GList *cur;
288
289         for (cur = account_list; cur != NULL; cur = cur->next)
290                 if (func((PrefsAccount *)cur->data, user_data) != 0)
291                         return;
292 }
293
294 GList *account_get_list(void)
295 {
296         return account_list;
297 }
298
299 void account_edit_open(void)
300 {
301         inc_lock();
302
303         if (compose_get_compose_list()) {
304                 alertpanel_notice(_("Some composing windows are open.\n"
305                                     "Please close all the composing windows before editing the accounts."));
306                 inc_unlock();
307                 return;
308         }
309
310         debug_print("Opening account edit window...\n");
311
312         if (!edit_account.window)
313                 account_edit_create();
314
315         account_clist_set();
316
317         manage_window_set_transient(GTK_WINDOW(edit_account.window));
318         gtk_widget_grab_focus(edit_account.close_btn);
319         gtk_widget_show(edit_account.window);
320
321         manage_window_focus_in(edit_account.window, NULL, NULL);
322 }
323
324 void account_add(void)
325 {
326         PrefsAccount *ac_prefs;
327
328         ac_prefs = prefs_account_open(NULL);
329
330         if (!ac_prefs) return;
331
332         account_list = g_list_append(account_list, ac_prefs);
333
334         if (ac_prefs->is_default)
335                 account_set_as_default(ac_prefs);
336
337         account_clist_set();
338
339         if (ac_prefs->protocol == A_IMAP4 || ac_prefs->protocol == A_NNTP) {
340                 Folder *folder;
341
342                 if (ac_prefs->protocol == A_IMAP4) {
343                         folder = folder_new(F_IMAP, ac_prefs->account_name,
344                                             ac_prefs->recv_server);
345                 } else {
346                         folder = folder_new(F_NEWS, ac_prefs->account_name,
347                                             ac_prefs->nntp_server);
348                 }
349
350                 folder->account = ac_prefs;
351                 ac_prefs->folder = REMOTE_FOLDER(folder);
352                 folder_add(folder);
353                 if (ac_prefs->protocol == A_IMAP4)
354                         folder->create_tree(folder);
355                 folderview_set_all();
356         }
357 }
358
359 void account_open(PrefsAccount *ac_prefs)
360 {
361         gboolean prev_default;
362         gchar *ac_name;
363
364         g_return_if_fail(ac_prefs != NULL);
365
366         prev_default = ac_prefs->is_default;
367         Xstrdup_a(ac_name, ac_prefs->account_name ? ac_prefs->account_name : "",
368                   return);
369
370         prefs_account_open(ac_prefs);
371
372         if (!prev_default && ac_prefs->is_default)
373                 account_set_as_default(ac_prefs);
374
375         if (ac_prefs->folder && strcmp2(ac_name, ac_prefs->account_name) != 0) {
376                 folder_set_name(FOLDER(ac_prefs->folder),
377                                 ac_prefs->account_name);
378                 folderview_set_all();
379         }
380
381         account_save_config_all();
382         account_set_menu();
383         main_window_reflect_prefs_all();
384 }
385
386 void account_set_as_default(PrefsAccount *ac_prefs)
387 {
388         PrefsAccount *ap;
389         GList *cur;
390
391         for (cur = account_list; cur != NULL; cur = cur->next) {
392                 ap = (PrefsAccount *)cur->data;
393                 if (ap->is_default)
394                         ap->is_default = FALSE;
395         }
396
397         ac_prefs->is_default = TRUE;
398 }
399
400 PrefsAccount *account_get_default(void)
401 {
402         PrefsAccount *ap;
403         GList *cur;
404
405         for (cur = account_list; cur != NULL; cur = cur->next) {
406                 ap = (PrefsAccount *)cur->data;
407                 if (ap->is_default)
408                         return ap;
409         }
410
411         return NULL;
412 }
413
414 void account_set_missing_folder(void)
415 {
416         PrefsAccount *ap;
417         GList *cur;
418
419         for (cur = account_list; cur != NULL; cur = cur->next) {
420                 ap = (PrefsAccount *)cur->data;
421                 if ((ap->protocol == A_IMAP4 || ap->protocol == A_NNTP) &&
422                     !ap->folder) {
423                         Folder *folder;
424
425                         if (ap->protocol == A_IMAP4) {
426                                 folder = folder_new(F_IMAP, ap->account_name,
427                                                     ap->recv_server);
428                         } else {
429                                 folder = folder_new(F_NEWS, ap->account_name,
430                                                     ap->nntp_server);
431                         }
432
433                         folder->account = ap;
434                         ap->folder = REMOTE_FOLDER(folder);
435                         folder_add(folder);
436                         if (ap->protocol == A_IMAP4)
437                                 folder->create_tree(folder);
438                 }
439         }
440 }
441
442 FolderItem *account_get_special_folder(PrefsAccount *ac_prefs,
443                                        SpecialFolderItemType type)
444 {
445         FolderItem *item = NULL;
446
447         g_return_val_if_fail(ac_prefs != NULL, NULL);
448
449         if (type == F_OUTBOX) {
450                 if (ac_prefs->set_sent_folder && ac_prefs->sent_folder) {
451                         item = folder_find_item_from_identifier
452                                 (ac_prefs->sent_folder);
453                 }
454                 if (!item) {
455                         if (ac_prefs->folder)
456                                 item = FOLDER(ac_prefs->folder)->outbox;
457                         if (!item)
458                                 item = folder_get_default_outbox();
459                 }
460         } else if (type == F_DRAFT) {
461                 if (ac_prefs->set_draft_folder && ac_prefs->draft_folder) {
462                         item = folder_find_item_from_identifier
463                                 (ac_prefs->draft_folder);
464                 }
465                 if (!item) {
466                         if (ac_prefs->folder)
467                                 item = FOLDER(ac_prefs->folder)->draft;
468                         if (!item)
469                                 item = folder_get_default_draft();
470                 }
471         } else if (type == F_QUEUE) {
472                 if (ac_prefs->folder)
473                         item = FOLDER(ac_prefs->folder)->queue;
474                 if (!item)
475                         item = folder_get_default_queue();
476         } else if (type == F_TRASH) {
477                 if (ac_prefs->set_trash_folder && ac_prefs->trash_folder) {
478                         item = folder_find_item_from_identifier
479                                 (ac_prefs->trash_folder);
480                 }
481                 if (!item) {
482                         if (ac_prefs->folder)
483                                 item = FOLDER(ac_prefs->folder)->trash;
484                         if (!item)
485                                 item = folder_get_default_trash();
486                 }
487         }
488
489         return item;
490 }
491
492 void account_destroy(PrefsAccount *ac_prefs)
493 {
494         g_return_if_fail(ac_prefs != NULL);
495
496         folder_unref_account_all(ac_prefs);
497
498         prefs_account_free(ac_prefs);
499         account_list = g_list_remove(account_list, ac_prefs);
500
501         if (cur_account == ac_prefs) cur_account = NULL;
502         if (!cur_account && account_list) {
503                 cur_account = account_get_default();
504                 if (!cur_account) {
505                         ac_prefs = (PrefsAccount *)account_list->data;
506                         account_set_as_default(ac_prefs);
507                         cur_account = ac_prefs;
508                 }
509         }
510 }
511
512
513 static void account_edit_create(void)
514 {
515         GtkWidget *window;
516         GtkWidget *vbox;
517         GtkWidget *label;
518         GtkWidget *hbox;
519         GtkWidget *scrolledwin;
520         GtkWidget *clist;
521         gchar *titles[N_EDIT_ACCOUNT_COLS];
522         gint i;
523
524         GtkWidget *vbox2;
525         GtkWidget *add_btn;
526         GtkWidget *edit_btn;
527         GtkWidget *del_btn;
528         GtkWidget *up_btn;
529         GtkWidget *down_btn;
530
531         GtkWidget *default_btn;
532
533         GtkWidget *hbbox;
534         GtkWidget *close_btn;
535
536         debug_print("Creating account edit window...\n");
537
538         window = gtk_window_new (GTK_WINDOW_DIALOG);
539         gtk_widget_set_usize (window, 500, 320);
540         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
541         gtk_window_set_title (GTK_WINDOW (window), _("Edit accounts"));
542         gtk_window_set_modal (GTK_WINDOW (window), TRUE);
543         gtk_signal_connect (GTK_OBJECT (window), "delete_event",
544                             GTK_SIGNAL_FUNC (account_delete_event), NULL);
545         gtk_signal_connect (GTK_OBJECT (window), "key_press_event",
546                             GTK_SIGNAL_FUNC (account_key_pressed), NULL);
547         MANAGE_WINDOW_SIGNALS_CONNECT (window);
548         gtk_widget_realize(window);
549
550         vbox = gtk_vbox_new (FALSE, 10);
551         gtk_widget_show (vbox);
552         gtk_container_add (GTK_CONTAINER (window), vbox);
553
554         hbox = gtk_hbox_new (FALSE, 0);
555         gtk_widget_show (hbox);
556         gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
557
558         label = gtk_label_new
559                 (_("New messages will be checked in this order. Check the boxes\n"
560                    "on the `G' column to enable message retrieval by `Get all'."));
561         gtk_widget_show (label);
562         gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 4);
563         gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
564
565         hbox = gtk_hbox_new (FALSE, 8);
566         gtk_widget_show (hbox);
567         gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
568         gtk_container_set_border_width (GTK_CONTAINER (hbox), 2);
569
570         scrolledwin = gtk_scrolled_window_new (NULL, NULL);
571         gtk_widget_show (scrolledwin);
572         gtk_box_pack_start (GTK_BOX (hbox), scrolledwin, TRUE, TRUE, 0);
573         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwin),
574                                         GTK_POLICY_AUTOMATIC,
575                                         GTK_POLICY_AUTOMATIC);
576
577         titles[COL_DEFAULT]  = "D";
578         titles[COL_GETALL]   = "G";
579         titles[COL_NAME]     = _("Name");
580         titles[COL_PROTOCOL] = _("Protocol");
581         titles[COL_SERVER]   = _("Server");
582
583         clist = gtk_clist_new_with_titles (N_EDIT_ACCOUNT_COLS, titles);
584         gtk_widget_show (clist);
585         gtk_container_add (GTK_CONTAINER (scrolledwin), clist);
586         gtk_clist_set_column_width (GTK_CLIST(clist), COL_DEFAULT , 10);
587         gtk_clist_set_column_width (GTK_CLIST(clist), COL_GETALL  , 11);
588         gtk_clist_set_column_width (GTK_CLIST(clist), COL_NAME    , 100);
589         gtk_clist_set_column_width (GTK_CLIST(clist), COL_PROTOCOL, 100);
590         gtk_clist_set_column_width (GTK_CLIST(clist), COL_SERVER  , 100);
591         gtk_clist_set_column_justification (GTK_CLIST(clist), COL_DEFAULT,
592                                             GTK_JUSTIFY_CENTER);
593         gtk_clist_set_column_justification (GTK_CLIST(clist), COL_GETALL,
594                                             GTK_JUSTIFY_CENTER);
595         gtk_clist_set_selection_mode (GTK_CLIST(clist), GTK_SELECTION_BROWSE);
596
597         for (i = 0; i < N_EDIT_ACCOUNT_COLS; i++)
598                 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(clist)->column[i].button,
599                                        GTK_CAN_FOCUS);
600
601         gtk_signal_connect (GTK_OBJECT (clist), "select_row",
602                             GTK_SIGNAL_FUNC (account_selected), NULL);
603         gtk_signal_connect (GTK_OBJECT (clist), "row_move",
604                             GTK_SIGNAL_FUNC (account_row_moved), NULL);
605
606         vbox2 = gtk_vbox_new (FALSE, 0);
607         gtk_widget_show (vbox2);
608         gtk_box_pack_start (GTK_BOX (hbox), vbox2, FALSE, FALSE, 0);
609
610         add_btn = gtk_button_new_with_label (_("Add"));
611         gtk_widget_show (add_btn);
612         gtk_box_pack_start (GTK_BOX (vbox2), add_btn, FALSE, FALSE, 4);
613         gtk_signal_connect (GTK_OBJECT(add_btn), "clicked",
614                             GTK_SIGNAL_FUNC (account_add), NULL);
615
616         edit_btn = gtk_button_new_with_label (_("Edit"));
617         gtk_widget_show (edit_btn);
618         gtk_box_pack_start (GTK_BOX (vbox2), edit_btn, FALSE, FALSE, 4);
619         gtk_signal_connect (GTK_OBJECT(edit_btn), "clicked",
620                             GTK_SIGNAL_FUNC (account_edit_prefs), NULL);
621
622         del_btn = gtk_button_new_with_label (_(" Delete "));
623         gtk_widget_show (del_btn);
624         gtk_box_pack_start (GTK_BOX (vbox2), del_btn, FALSE, FALSE, 4);
625         gtk_signal_connect (GTK_OBJECT(del_btn), "clicked",
626                             GTK_SIGNAL_FUNC (account_delete), NULL);
627
628         down_btn = gtk_button_new_with_label (_("Down"));
629         gtk_widget_show (down_btn);
630         gtk_box_pack_end (GTK_BOX (vbox2), down_btn, FALSE, FALSE, 4);
631         gtk_signal_connect (GTK_OBJECT(down_btn), "clicked",
632                             GTK_SIGNAL_FUNC (account_down), NULL);
633
634         up_btn = gtk_button_new_with_label (_("Up"));
635         gtk_widget_show (up_btn);
636         gtk_box_pack_end (GTK_BOX (vbox2), up_btn, FALSE, FALSE, 4);
637         gtk_signal_connect (GTK_OBJECT(up_btn), "clicked",
638                             GTK_SIGNAL_FUNC (account_up), NULL);
639
640         hbox = gtk_hbox_new (FALSE, 8);
641         gtk_widget_show (hbox);
642         gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
643
644         vbox2 = gtk_vbox_new(FALSE, 0);
645         gtk_widget_show (vbox2);
646         gtk_box_pack_start (GTK_BOX (hbox), vbox2, FALSE, FALSE, 0);
647
648         default_btn = gtk_button_new_with_label (_(" Set as default account "));
649         gtk_widget_show (default_btn);
650         gtk_box_pack_start (GTK_BOX (vbox2), default_btn, TRUE, FALSE, 0);
651         gtk_signal_connect (GTK_OBJECT(default_btn), "clicked",
652                             GTK_SIGNAL_FUNC (account_set_default), NULL);
653
654         gtkut_button_set_create(&hbbox, &close_btn, _("Close"),
655                                 NULL, NULL, NULL, NULL);
656         gtk_widget_show(hbbox);
657         gtk_box_pack_end (GTK_BOX (hbox), hbbox, FALSE, FALSE, 0);
658         gtk_widget_grab_default (close_btn);
659
660         gtk_signal_connect (GTK_OBJECT (close_btn), "clicked",
661                             GTK_SIGNAL_FUNC (account_edit_close),
662                             NULL);
663
664         stock_pixmap_gdk(clist, STOCK_PIXMAP_MARK, &markxpm, &markxpmmask);
665         stock_pixmap_gdk(clist, STOCK_PIXMAP_CHECKBOX_ON,
666                          &checkboxonxpm, &checkboxonxpmmask);
667         stock_pixmap_gdk(clist, STOCK_PIXMAP_CHECKBOX_OFF,
668                          &checkboxoffxpm, &checkboxoffxpmmask);
669
670         edit_account.window    = window;
671         edit_account.clist     = clist;
672         edit_account.close_btn = close_btn;
673 }
674
675 static void account_edit_prefs(void)
676 {
677         GtkCList *clist = GTK_CLIST(edit_account.clist);
678         PrefsAccount *ac_prefs;
679         gint row;
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         account_open(ac_prefs);
686         
687         account_clist_set();
688 }
689
690 static gboolean account_delete_references_func(GNode *node, gpointer data)
691 {
692         FolderItem *item;
693         gint account;
694
695         g_return_val_if_fail(node->data != NULL, FALSE);
696
697         item = FOLDER_ITEM(node->data);
698         account = GPOINTER_TO_INT(data);
699
700         if(!item->prefs) /* && item->prefs->stype == F_NORMAL */
701                 return FALSE;
702         if(item->prefs->default_account != account)
703                 return FALSE;
704         
705         item->prefs->enable_default_account = FALSE;
706         item->prefs->default_account = 0;
707         prefs_folder_item_save_config(item);
708
709         return FALSE;
710 }
711
712 static void account_delete(void)
713 {
714         GtkCList *clist = GTK_CLIST(edit_account.clist);
715         PrefsAccount *ac_prefs;
716         gint row;
717         GList *list;
718         Folder *folder;
719         
720         if (!clist->selection) return;
721
722         if (alertpanel(_("Delete account"),
723                        _("Do you really want to delete this account?"),
724                        _("Yes"), _("+No"), NULL) != G_ALERTDEFAULT)
725                 return;
726
727         row = GPOINTER_TO_INT(clist->selection->data);
728         ac_prefs = gtk_clist_get_row_data(clist, row);
729         if (ac_prefs->folder) {
730                 folder_destroy(FOLDER(ac_prefs->folder));
731                 folderview_set_all();
732         }
733         account_destroy(ac_prefs);
734         account_clist_set();
735
736         debug_print("Removing deleted account references for all the folders...\n");
737         list = folder_get_list();
738         for (; list != NULL; list = list->next) {
739                 folder = FOLDER(list->data);
740                 if (folder->node)  /* && folder->type == F_? */
741                         g_node_traverse(folder->node, G_PRE_ORDER,
742                                 G_TRAVERSE_ALL, -1,
743                                 account_delete_references_func,
744                                 GINT_TO_POINTER(ac_prefs->account_id));
745         }
746 }
747
748 static void account_up(void)
749 {
750         GtkCList *clist = GTK_CLIST(edit_account.clist);
751         gint row;
752
753         if (!clist->selection) return;
754
755         row = GPOINTER_TO_INT(clist->selection->data);
756         if (row > 0)
757                 gtk_clist_row_move(clist, row, row - 1);
758 }
759
760 static void account_down(void)
761 {
762         GtkCList *clist = GTK_CLIST(edit_account.clist);
763         gint row;
764
765         if (!clist->selection) return;
766
767         row = GPOINTER_TO_INT(clist->selection->data);
768         if (row < clist->rows - 1)
769                 gtk_clist_row_move(clist, row, row + 1);
770 }
771
772 static void account_set_default(void)
773 {
774         GtkCList *clist = GTK_CLIST(edit_account.clist);
775         gint row;
776         PrefsAccount *ac_prefs;
777
778         if (!clist->selection) return;
779
780         row = GPOINTER_TO_INT(clist->selection->data);
781         ac_prefs = gtk_clist_get_row_data(clist, row);
782         account_set_as_default(ac_prefs);
783         account_clist_set();
784
785         cur_account = ac_prefs;
786         account_set_menu();
787         main_window_reflect_prefs_all();
788 }
789
790 static void account_edit_close(void)
791 {
792         account_list_set();
793         account_save_config_all();
794
795         if (!cur_account && account_list) {
796                 PrefsAccount *ac_prefs = (PrefsAccount *)account_list->data;
797                 account_set_as_default(ac_prefs);
798                 cur_account = ac_prefs;
799         }
800
801         account_set_menu();
802         main_window_reflect_prefs_all();
803
804         gtk_widget_hide(edit_account.window);
805
806         inc_unlock();
807 }
808
809 static gint account_delete_event(GtkWidget *widget, GdkEventAny *event,
810                                  gpointer data)
811 {
812         account_edit_close();
813         return TRUE;
814 }
815
816 static void account_selected(GtkCList *clist, gint row, gint column,
817                              GdkEvent *event, gpointer data)
818 {
819         if (event && event->type == GDK_2BUTTON_PRESS)
820                 account_edit_prefs();
821
822         if (column == COL_GETALL) {
823                 PrefsAccount *ac;
824
825                 ac = gtk_clist_get_row_data(clist, row);
826                 if (ac->protocol == A_POP3 || ac->protocol == A_APOP ||
827                     ac->protocol == A_IMAP4 || ac->protocol == A_NNTP ||
828                     ac->protocol == A_LOCAL) {
829                         ac->recv_at_getall ^= TRUE;
830                         account_clist_set_row(ac, row);
831                 }
832         }
833 }
834
835 static void account_row_moved(GtkCList *clist, gint source_row, gint dest_row)
836 {
837         account_list_set();
838         if (gtk_clist_row_is_visible(clist, dest_row) != GTK_VISIBILITY_FULL) {
839                 gtk_clist_moveto(clist, dest_row, -1,
840                                  source_row < dest_row ? 1.0 : 0.0, 0.0);
841         }
842 }
843
844 static void account_key_pressed(GtkWidget *widget, GdkEventKey *event,
845                                 gpointer data)
846 {
847         if (event && event->keyval == GDK_Escape)
848                 account_edit_close();
849 }
850
851 /* set one CList row or add new row */
852 static gint account_clist_set_row(PrefsAccount *ac_prefs, gint row)
853 {
854         GtkCList *clist = GTK_CLIST(edit_account.clist);
855         gchar *text[N_EDIT_ACCOUNT_COLS];
856         gboolean has_getallbox;
857         gboolean getall;
858
859         text[COL_DEFAULT] = "";
860         text[COL_GETALL]  = "";
861         text[COL_NAME]    = ac_prefs->account_name;
862 #if USE_SSL
863         text[COL_PROTOCOL] = ac_prefs->protocol == A_POP3 ?
864                              (ac_prefs->ssl_pop == SSL_TUNNEL ?
865                               "POP3 (SSL)" :
866                               ac_prefs->ssl_pop == SSL_STARTTLS ?
867                               "POP3 (TLS)" : "POP3") :
868                              ac_prefs->protocol == A_APOP ?
869                              (ac_prefs->ssl_pop == SSL_TUNNEL ?
870                               "POP3 (APOP, SSL)" :
871                               ac_prefs->ssl_pop == SSL_STARTTLS ?
872                               "POP3 (APOP, TLS)" : "POP3 (APOP)") :
873                              ac_prefs->protocol == A_IMAP4 ?
874                              (ac_prefs->ssl_imap == SSL_TUNNEL ?
875                               "IMAP4 (SSL)" :
876                               ac_prefs->ssl_imap == SSL_STARTTLS ?
877                               "IMAP4 (TLS)" : "IMAP4") :
878                              ac_prefs->protocol == A_NNTP ?
879                              (ac_prefs->ssl_nntp == SSL_TUNNEL ?
880                               "NNTP (SSL)" : "NNTP") :
881                              "";
882 #else
883         text[COL_PROTOCOL] = ac_prefs->protocol == A_POP3  ? "POP3" :
884                              ac_prefs->protocol == A_APOP  ? "POP3 (APOP)" :
885                              ac_prefs->protocol == A_IMAP4 ? "IMAP4" :
886                              ac_prefs->protocol == A_LOCAL ? "Local" :
887                              ac_prefs->protocol == A_NNTP  ? "NNTP" : "";
888 #endif
889         text[COL_SERVER] = ac_prefs->protocol == A_NNTP
890                 ? ac_prefs->nntp_server : ac_prefs->recv_server;
891
892         if (row < 0)
893                 row = gtk_clist_append(clist, text);
894         else {
895                 gtk_clist_set_text(clist, row, COL_DEFAULT, text[COL_DEFAULT]);
896                 gtk_clist_set_text(clist, row, COL_GETALL, text[COL_GETALL]);
897                 gtk_clist_set_text(clist, row, COL_NAME, text[COL_NAME]);
898                 gtk_clist_set_text(clist, row, COL_PROTOCOL, text[COL_PROTOCOL]);
899                 gtk_clist_set_text(clist, row, COL_SERVER, text[COL_SERVER]);
900         }
901
902         has_getallbox = (ac_prefs->protocol == A_POP3  ||
903                          ac_prefs->protocol == A_APOP  ||
904                          ac_prefs->protocol == A_IMAP4 ||
905                          ac_prefs->protocol == A_NNTP ||
906                          ac_prefs->protocol == A_LOCAL);
907         getall = has_getallbox && ac_prefs->recv_at_getall;
908
909         if (ac_prefs->is_default)
910                 gtk_clist_set_pixmap(clist, row, COL_DEFAULT,
911                                      markxpm, markxpmmask);
912         if (getall)
913                 gtk_clist_set_pixmap(clist, row, COL_GETALL,
914                                      checkboxonxpm, checkboxonxpmmask);
915         else if (has_getallbox)
916                 gtk_clist_set_pixmap(clist, row, COL_GETALL,
917                                      checkboxoffxpm, checkboxoffxpmmask);
918
919         gtk_clist_set_row_data(clist, row, ac_prefs);
920
921         return row;
922 }
923
924 /* set CList from account list */
925 static void account_clist_set(void)
926 {
927         GtkCList *clist = GTK_CLIST(edit_account.clist);
928         GList *cur;
929         gint row = -1, prev_row = -1;
930
931         if (clist->selection)
932                 prev_row = GPOINTER_TO_INT(clist->selection->data);
933
934         gtk_clist_freeze(clist);
935         gtk_clist_clear(clist);
936
937         for (cur = account_list; cur != NULL; cur = cur->next) {
938                 row = account_clist_set_row((PrefsAccount *)cur->data, -1);
939                 if ((PrefsAccount *)cur->data == cur_account) {
940                         gtk_clist_select_row(clist, row, -1);
941                         gtkut_clist_set_focus_row(clist, row);
942                 }
943         }
944
945         if (prev_row >= 0) {
946                 row = prev_row;
947                 gtk_clist_select_row(clist, row, -1);
948                 gtkut_clist_set_focus_row(clist, row);
949         }
950
951         if (row >= 0 &&
952             gtk_clist_row_is_visible(clist, row) != GTK_VISIBILITY_FULL)
953                 gtk_clist_moveto(clist, row, -1, 0.5, 0);
954
955         gtk_clist_thaw(clist);
956 }
957
958 /* set account list from CList */
959 static void account_list_set(void)
960 {
961         GtkCList *clist = GTK_CLIST(edit_account.clist);
962         gint row;
963         PrefsAccount *ac_prefs;
964
965         while (account_list)
966                 account_list = g_list_remove(account_list, account_list->data);
967
968         for (row = 0; (ac_prefs = gtk_clist_get_row_data(clist, row)) != NULL;
969              row++)
970                 account_list = g_list_append(account_list, ac_prefs);
971 }