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