2004-12-30 [paul] 0.9.13cvs22
[claws.git] / src / account.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2004 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_gtk.h"
39 #include "prefs_account.h"
40 #include "folder_item_prefs.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 #include "procheader.h"
49 #include "customheader.h"
50 #include "remotefolder.h"
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 static void account_clone               (void);
87
88 static void account_up                  (void);
89 static void account_down                (void);
90
91 static void account_set_default         (void);
92
93 static void account_edit_close          (void);
94
95 static gint account_delete_event        (GtkWidget      *widget,
96                                          GdkEventAny    *event,
97                                          gpointer        data);
98 static void account_selected            (GtkCList       *clist,
99                                          gint            row,
100                                          gint            column,
101                                          GdkEvent       *event,
102                                          gpointer        data);
103 static void account_row_moved           (GtkCList       *clist,
104                                          gint            source_row,
105                                          gint            dest_row);
106 static void account_key_pressed         (GtkWidget      *widget,
107                                          GdkEventKey    *event,
108                                          gpointer        data);
109
110 static gint account_clist_set_row       (PrefsAccount   *ac_prefs,
111                                          gint            row);
112 static void account_clist_set           (void);
113
114 static void account_list_set            (void);
115
116 void account_read_config_all(void)
117 {
118         GSList *ac_label_list = NULL, *cur;
119         gchar *rcpath;
120         FILE *fp;
121         gchar buf[PREFSBUFSIZE];
122         PrefsAccount *ac_prefs;
123
124         debug_print("Reading all config for each account...\n");
125
126         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACCOUNT_RC, NULL);
127         if ((fp = fopen(rcpath, "rb")) == NULL) {
128                 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
129                 g_free(rcpath);
130                 return;
131         }
132         g_free(rcpath);
133
134         while (fgets(buf, sizeof(buf), fp) != NULL) {
135                 if (!strncmp(buf, "[Account: ", 10)) {
136                         strretchomp(buf);
137                         memmove(buf, buf + 1, strlen(buf));
138                         buf[strlen(buf) - 1] = '\0';
139                         debug_print("Found label: %s\n", buf);
140                         ac_label_list = g_slist_append(ac_label_list,
141                                                        g_strdup(buf));
142                 }
143         }
144         fclose(fp);
145
146         /* read config data from file */
147         cur_account = NULL;
148         for (cur = ac_label_list; cur != NULL; cur = cur->next) {
149                 ac_prefs = prefs_account_new();
150                 prefs_account_read_config(ac_prefs, (gchar *)cur->data);
151                 account_list = g_list_append(account_list, ac_prefs);
152                 if (ac_prefs->is_default)
153                         cur_account = ac_prefs;
154         }
155         /* if default is not set, assume first account as default */
156         if (!cur_account && account_list) {
157                 ac_prefs = (PrefsAccount *)account_list->data;
158                 account_set_as_default(ac_prefs);
159                 cur_account = ac_prefs;
160         }
161
162         account_set_menu();
163         main_window_reflect_prefs_all();
164
165         while (ac_label_list) {
166                 g_free(ac_label_list->data);
167                 ac_label_list = g_slist_remove(ac_label_list,
168                                                ac_label_list->data);
169         }
170 }
171
172 void account_write_config_all(void)
173 {
174         prefs_account_write_config_all(account_list);
175 }
176
177 /*
178  * account_find_all_from_address:
179  * @ac_list: initial list of accounts. NULL to create a new one.
180  * Accounts found in the @address will be appended to this list.
181  * @address: Email address string.
182  *
183  * Find all the mail (not news) accounts within the specified address.
184  *
185  * Return value: the original accounts list with the found accounts appended.
186  */
187 GList *account_find_all_from_address(GList *ac_list, const gchar *address)
188 {
189         GList *cur;
190         PrefsAccount *ac;
191
192         if (address == NULL)
193                 return ac_list;
194
195         for (cur = account_list; cur != NULL; cur = cur->next) {
196                 ac = (PrefsAccount *)cur->data;
197                 if (ac->protocol != A_NNTP && ac->address &&
198                     strcasestr(address, ac->address) != NULL)
199                         ac_list = g_list_append(ac_list, ac);
200         }
201         return ac_list;
202 }
203         
204 PrefsAccount *account_find_from_smtp_server(const gchar *address,
205                                             const gchar *smtp_server)
206 {
207         GList *cur;
208         PrefsAccount *ac;
209
210         g_return_val_if_fail(address != NULL, NULL);
211         g_return_val_if_fail(smtp_server != NULL, NULL);
212
213         for (cur = account_list; cur != NULL; cur = cur->next) {
214                 ac = (PrefsAccount *)cur->data;
215                 if (!strcmp2(address, ac->address) &&
216                     !strcmp2(smtp_server, ac->smtp_server))
217                         return ac;
218         }
219
220         return NULL;
221 }
222
223 /*
224  * account_find_from_address:
225  * @address: Email address string.
226  *
227  * Find a mail (not news) account with the specified email address.
228  *
229  * Return value: The found account, or NULL if not found.
230  */
231 PrefsAccount *account_find_from_address(const gchar *address)
232 {
233         GList *cur;
234         PrefsAccount *ac;
235
236         g_return_val_if_fail(address != NULL, NULL);
237
238         for (cur = account_list; cur != NULL; cur = cur->next) {
239                 ac = (PrefsAccount *)cur->data;
240                 if (ac->protocol != A_NNTP && ac->address &&
241                     g_strcasecmp(address, ac->address) == 0)
242                         return ac;
243         }
244
245         return NULL;
246 }
247
248 PrefsAccount *account_find_from_id(gint id)
249 {
250         GList *cur;
251         PrefsAccount *ac;
252
253         for (cur = account_list; cur != NULL; cur = cur->next) {
254                 ac = (PrefsAccount *)cur->data;
255                 if (id == ac->account_id)
256                         return ac;
257         }
258
259         return NULL;
260 }
261
262 PrefsAccount *account_find_from_item(FolderItem *item)
263 {
264         PrefsAccount *ac;
265
266         g_return_val_if_fail(item != NULL, NULL);
267
268         ac = item->account;
269         if (!ac) {
270                 FolderItem *cur_item = folder_item_parent(item);
271                 while (cur_item != NULL) {
272                         if (cur_item->account && cur_item->apply_sub) {
273                                 ac = cur_item->account;
274                                 break;
275                         }                               
276                         cur_item = folder_item_parent(cur_item);
277                 }
278         }
279         if (!ac)
280                 ac = item->folder->account;
281
282         return ac;
283 }
284
285 void account_set_menu(void)
286 {
287         main_window_set_account_menu(account_list);
288 }
289
290 void account_foreach(AccountFunc func, gpointer user_data)
291 {
292         GList *cur;
293
294         for (cur = account_list; cur != NULL; cur = cur->next)
295                 if (func((PrefsAccount *)cur->data, user_data) != 0)
296                         return;
297 }
298
299 GList *account_get_list(void)
300 {
301         return account_list;
302 }
303
304 void account_edit_open(void)
305 {
306         inc_lock();
307
308         if (compose_get_compose_list()) {
309                 alertpanel_error(_("Some composing windows are open.\n"
310                                    "Please close all the composing "
311                                    "windows before editing the accounts."));
312                 inc_unlock();
313                 return;
314         }
315
316         debug_print("Opening account edit window...\n");
317
318         if (!edit_account.window)
319                 account_edit_create();
320
321         account_clist_set();
322
323         manage_window_set_transient(GTK_WINDOW(edit_account.window));
324         gtk_widget_grab_focus(edit_account.close_btn);
325         gtk_widget_show(edit_account.window);
326
327         manage_window_focus_in(edit_account.window, NULL, NULL);
328 }
329
330 void account_add(void)
331 {
332         PrefsAccount *ac_prefs;
333
334         ac_prefs = prefs_account_open(NULL);
335
336         if (!ac_prefs) return;
337
338         account_list = g_list_append(account_list, ac_prefs);
339
340         if (ac_prefs->is_default)
341                 account_set_as_default(ac_prefs);
342
343         account_clist_set();
344
345         if (ac_prefs->protocol == A_IMAP4 || ac_prefs->protocol == A_NNTP) {
346                 Folder *folder;
347
348                 if (ac_prefs->protocol == A_IMAP4) {
349                         folder = folder_new(folder_get_class_from_string("imap"), ac_prefs->account_name,
350                                             ac_prefs->recv_server);
351                 } else {
352                         folder = folder_new(folder_get_class_from_string("news"), ac_prefs->account_name,
353                                             ac_prefs->nntp_server);
354                 }
355
356                 folder->account = ac_prefs;
357                 ac_prefs->folder = folder;
358                 folder_add(folder);
359                 if (ac_prefs->protocol == A_IMAP4)
360                         folder->klass->create_tree(folder);
361                 folderview_set_all();
362         }
363 }
364
365 void account_open(PrefsAccount *ac_prefs)
366 {
367         gboolean prev_default;
368         gchar *ac_name;
369
370         g_return_if_fail(ac_prefs != NULL);
371
372         prev_default = ac_prefs->is_default;
373         Xstrdup_a(ac_name, ac_prefs->account_name ? ac_prefs->account_name : "",
374                   return);
375
376         prefs_account_open(ac_prefs);
377
378         if (!prev_default && ac_prefs->is_default)
379                 account_set_as_default(ac_prefs);
380
381         if (ac_prefs->folder && strcmp2(ac_name, ac_prefs->account_name) != 0) {
382                 folder_set_name(FOLDER(ac_prefs->folder),
383                                 ac_prefs->account_name);
384                 folderview_set_all();
385         }
386
387         account_write_config_all();
388         account_set_menu();
389         main_window_reflect_prefs_all();
390 }
391
392 void account_set_as_default(PrefsAccount *ac_prefs)
393 {
394         PrefsAccount *ap;
395         GList *cur;
396
397         for (cur = account_list; cur != NULL; cur = cur->next) {
398                 ap = (PrefsAccount *)cur->data;
399                 if (ap->is_default)
400                         ap->is_default = FALSE;
401         }
402
403         ac_prefs->is_default = TRUE;
404 }
405
406 PrefsAccount *account_get_default(void)
407 {
408         PrefsAccount *ap;
409         GList *cur;
410
411         for (cur = account_list; cur != NULL; cur = cur->next) {
412                 ap = (PrefsAccount *)cur->data;
413                 if (ap->is_default)
414                         return ap;
415         }
416
417         return NULL;
418 }
419
420 void account_set_missing_folder(void)
421 {
422         PrefsAccount *ap;
423         GList *cur;
424
425         for (cur = account_list; cur != NULL; cur = cur->next) {
426                 ap = (PrefsAccount *)cur->data;
427                 if ((ap->protocol == A_IMAP4 || ap->protocol == A_NNTP) &&
428                     !ap->folder) {
429                         Folder *folder;
430
431                         if (ap->protocol == A_IMAP4) {
432                                 folder = folder_new(folder_get_class_from_string("imap"), ap->account_name,
433                                                     ap->recv_server);
434                         } else {
435                                 folder = folder_new(folder_get_class_from_string("news"), ap->account_name,
436                                                     ap->nntp_server);
437                         }
438
439                         folder->account = ap;
440                         ap->folder = folder;
441                         folder_add(folder);
442                         if (ap->protocol == A_IMAP4)
443                                 folder->klass->create_tree(folder);
444                 }
445         }
446 }
447
448 FolderItem *account_get_special_folder(PrefsAccount *ac_prefs,
449                                        SpecialFolderItemType type)
450 {
451         FolderItem *item = NULL;
452
453         g_return_val_if_fail(ac_prefs != NULL, NULL);
454
455         switch (type) {
456         case F_INBOX:
457                 if (ac_prefs->folder)
458                         item = FOLDER(ac_prefs->folder)->inbox;
459                 if (!item)
460                         item = folder_get_default_inbox();
461                 break;
462         case F_OUTBOX:
463                 if (ac_prefs->set_sent_folder && ac_prefs->sent_folder) {
464                         item = folder_find_item_from_identifier
465                                 (ac_prefs->sent_folder);
466                 }
467                 if (!item) {
468                         if (ac_prefs->folder)
469                                 item = FOLDER(ac_prefs->folder)->outbox;
470                         if (!item)
471                                 item = folder_get_default_outbox();
472                 }
473                 break;
474         case F_DRAFT:
475                 if (ac_prefs->set_draft_folder && ac_prefs->draft_folder) {
476                         item = folder_find_item_from_identifier
477                                 (ac_prefs->draft_folder);
478                 }
479                 if (!item) {
480                         if (ac_prefs->folder)
481                                 item = FOLDER(ac_prefs->folder)->draft;
482                         if (!item)
483                                 item = folder_get_default_draft();
484                 }
485                 break;
486         case F_QUEUE:
487                 if (ac_prefs->folder)
488                         item = FOLDER(ac_prefs->folder)->queue;
489                 if (!item)
490                         item = folder_get_default_queue();
491                 break;
492         case F_TRASH:
493                 if (ac_prefs->set_trash_folder && ac_prefs->trash_folder) {
494                         item = folder_find_item_from_identifier
495                                 (ac_prefs->trash_folder);
496                 }
497                 if (!item) {
498                         if (ac_prefs->folder)
499                                 item = FOLDER(ac_prefs->folder)->trash;
500                         if (!item)
501                                 item = folder_get_default_trash();
502                 }
503                 break;
504         default:
505                 break;
506         }
507
508         return item;
509 }
510
511 void account_destroy(PrefsAccount *ac_prefs)
512 {
513         g_return_if_fail(ac_prefs != NULL);
514
515         folder_unref_account_all(ac_prefs);
516
517         prefs_account_free(ac_prefs);
518         account_list = g_list_remove(account_list, ac_prefs);
519
520         if (cur_account == ac_prefs) cur_account = NULL;
521         if (!cur_account && account_list) {
522                 cur_account = account_get_default();
523                 if (!cur_account) {
524                         ac_prefs = (PrefsAccount *)account_list->data;
525                         account_set_as_default(ac_prefs);
526                         cur_account = ac_prefs;
527                 }
528         }
529 }
530
531
532 static void account_edit_create(void)
533 {
534         GtkWidget *window;
535         GtkWidget *vbox;
536         GtkWidget *label;
537         GtkWidget *hbox;
538         GtkWidget *scrolledwin;
539         GtkWidget *clist;
540         gchar *titles[N_EDIT_ACCOUNT_COLS];
541         gint i;
542
543         GtkWidget *vbox2;
544         GtkWidget *add_btn;
545         GtkWidget *edit_btn;
546         GtkWidget *del_btn;
547         GtkWidget *clone_btn;
548         GtkWidget *up_btn;
549         GtkWidget *down_btn;
550
551         GtkWidget *default_btn;
552
553         GtkWidget *hbbox;
554         GtkWidget *close_btn;
555
556         debug_print("Creating account edit window...\n");
557
558         window = gtk_window_new (GTK_WINDOW_DIALOG);
559         gtk_widget_set_usize (window, 500, 350);
560         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
561         gtk_window_set_title (GTK_WINDOW (window), _("Edit accounts"));
562         gtk_window_set_modal (GTK_WINDOW (window), TRUE);
563         gtk_signal_connect (GTK_OBJECT (window), "delete_event",
564                             GTK_SIGNAL_FUNC (account_delete_event), NULL);
565         gtk_signal_connect (GTK_OBJECT (window), "key_press_event",
566                             GTK_SIGNAL_FUNC (account_key_pressed), NULL);
567         MANAGE_WINDOW_SIGNALS_CONNECT (window);
568         gtk_widget_realize(window);
569
570         vbox = gtk_vbox_new (FALSE, 10);
571         gtk_widget_show (vbox);
572         gtk_container_add (GTK_CONTAINER (window), vbox);
573
574         hbox = gtk_hbox_new (FALSE, 0);
575         gtk_widget_show (hbox);
576         gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
577
578         label = gtk_label_new
579                 (_("New messages will be checked in this order. Check the boxes\n"
580                    "on the `G' column to enable message retrieval by `Get all'."));
581         gtk_widget_show (label);
582         gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 4);
583         gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
584
585         hbox = gtk_hbox_new (FALSE, 8);
586         gtk_widget_show (hbox);
587         gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
588         gtk_container_set_border_width (GTK_CONTAINER (hbox), 2);
589
590         scrolledwin = gtk_scrolled_window_new (NULL, NULL);
591         gtk_widget_show (scrolledwin);
592         gtk_box_pack_start (GTK_BOX (hbox), scrolledwin, TRUE, TRUE, 0);
593         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwin),
594                                         GTK_POLICY_AUTOMATIC,
595                                         GTK_POLICY_AUTOMATIC);
596
597         titles[COL_DEFAULT]  = "D";
598         titles[COL_GETALL]   = "G";
599         titles[COL_NAME]     = _("Name");
600         titles[COL_PROTOCOL] = _("Protocol");
601         titles[COL_SERVER]   = _("Server");
602
603         clist = gtk_clist_new_with_titles (N_EDIT_ACCOUNT_COLS, titles);
604         gtk_widget_show (clist);
605         gtk_container_add (GTK_CONTAINER (scrolledwin), clist);
606         gtk_clist_set_column_width (GTK_CLIST(clist), COL_DEFAULT , 10);
607         gtk_clist_set_column_width (GTK_CLIST(clist), COL_GETALL  , 11);
608         gtk_clist_set_column_width (GTK_CLIST(clist), COL_NAME    , 100);
609         gtk_clist_set_column_width (GTK_CLIST(clist), COL_PROTOCOL, 100);
610         gtk_clist_set_column_width (GTK_CLIST(clist), COL_SERVER  , 100);
611         gtk_clist_set_column_justification (GTK_CLIST(clist), COL_DEFAULT,
612                                             GTK_JUSTIFY_CENTER);
613         gtk_clist_set_column_justification (GTK_CLIST(clist), COL_GETALL,
614                                             GTK_JUSTIFY_CENTER);
615         gtk_clist_set_selection_mode (GTK_CLIST(clist), GTK_SELECTION_BROWSE);
616
617         for (i = 0; i < N_EDIT_ACCOUNT_COLS; i++)
618                 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(clist)->column[i].button,
619                                        GTK_CAN_FOCUS);
620
621         gtk_signal_connect (GTK_OBJECT (clist), "select_row",
622                             GTK_SIGNAL_FUNC (account_selected), NULL);
623         gtk_signal_connect_after (GTK_OBJECT (clist), "row_move",
624                                   GTK_SIGNAL_FUNC (account_row_moved), NULL);
625
626         vbox2 = gtk_vbox_new (FALSE, 0);
627         gtk_widget_show (vbox2);
628         gtk_box_pack_start (GTK_BOX (hbox), vbox2, FALSE, FALSE, 0);
629
630         add_btn = gtk_button_new_with_label (_("Add"));
631         gtk_widget_show (add_btn);
632         gtk_box_pack_start (GTK_BOX (vbox2), add_btn, FALSE, FALSE, 4);
633         gtk_signal_connect (GTK_OBJECT(add_btn), "clicked",
634                             GTK_SIGNAL_FUNC (account_add), NULL);
635
636         edit_btn = gtk_button_new_with_label (_("Edit"));
637         gtk_widget_show (edit_btn);
638         gtk_box_pack_start (GTK_BOX (vbox2), edit_btn, FALSE, FALSE, 4);
639         gtk_signal_connect (GTK_OBJECT(edit_btn), "clicked",
640                             GTK_SIGNAL_FUNC (account_edit_prefs), NULL);
641
642         del_btn = gtk_button_new_with_label (_(" Delete "));
643         gtk_widget_show (del_btn);
644         gtk_box_pack_start (GTK_BOX (vbox2), del_btn, FALSE, FALSE, 4);
645         gtk_signal_connect (GTK_OBJECT(del_btn), "clicked",
646                             GTK_SIGNAL_FUNC (account_delete), NULL);
647
648         clone_btn = gtk_button_new_with_label (_(" Clone "));
649         gtk_widget_show (clone_btn);
650         gtk_box_pack_start (GTK_BOX (vbox2), clone_btn, FALSE, FALSE, 4);
651         gtk_signal_connect (GTK_OBJECT(clone_btn), "clicked",
652                             GTK_SIGNAL_FUNC (account_clone), NULL);
653         
654         down_btn = gtk_button_new_with_label (_("Down"));
655         gtk_widget_show (down_btn);
656         gtk_box_pack_end (GTK_BOX (vbox2), down_btn, FALSE, FALSE, 4);
657         gtk_signal_connect (GTK_OBJECT(down_btn), "clicked",
658                             GTK_SIGNAL_FUNC (account_down), NULL);
659
660         up_btn = gtk_button_new_with_label (_("Up"));
661         gtk_widget_show (up_btn);
662         gtk_box_pack_end (GTK_BOX (vbox2), up_btn, FALSE, FALSE, 4);
663         gtk_signal_connect (GTK_OBJECT(up_btn), "clicked",
664                             GTK_SIGNAL_FUNC (account_up), NULL);
665
666         hbox = gtk_hbox_new (FALSE, 8);
667         gtk_widget_show (hbox);
668         gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
669
670         vbox2 = gtk_vbox_new(FALSE, 0);
671         gtk_widget_show (vbox2);
672         gtk_box_pack_start (GTK_BOX (hbox), vbox2, FALSE, FALSE, 0);
673
674         default_btn = gtk_button_new_with_label (_(" Set as default account "));
675         gtk_widget_show (default_btn);
676         gtk_box_pack_start (GTK_BOX (vbox2), default_btn, TRUE, FALSE, 0);
677         gtk_signal_connect (GTK_OBJECT(default_btn), "clicked",
678                             GTK_SIGNAL_FUNC (account_set_default), NULL);
679
680         gtkut_button_set_create(&hbbox, &close_btn, _("Close"),
681                                 NULL, NULL, NULL, NULL);
682         gtk_widget_show(hbbox);
683         gtk_box_pack_end (GTK_BOX (hbox), hbbox, FALSE, FALSE, 0);
684         gtk_widget_grab_default (close_btn);
685
686         gtk_signal_connect (GTK_OBJECT (close_btn), "clicked",
687                             GTK_SIGNAL_FUNC (account_edit_close),
688                             NULL);
689
690         stock_pixmap_gdk(clist, STOCK_PIXMAP_MARK, &markxpm, &markxpmmask);
691         stock_pixmap_gdk(clist, STOCK_PIXMAP_CHECKBOX_ON,
692                          &checkboxonxpm, &checkboxonxpmmask);
693         stock_pixmap_gdk(clist, STOCK_PIXMAP_CHECKBOX_OFF,
694                          &checkboxoffxpm, &checkboxoffxpmmask);
695
696         edit_account.window    = window;
697         edit_account.clist     = clist;
698         edit_account.close_btn = close_btn;
699 }
700
701 static void account_edit_prefs(void)
702 {
703         GtkCList *clist = GTK_CLIST(edit_account.clist);
704         PrefsAccount *ac_prefs;
705         gint row;
706
707         if (!clist->selection) return;
708
709         row = GPOINTER_TO_INT(clist->selection->data);
710         ac_prefs = gtk_clist_get_row_data(clist, row);
711         account_open(ac_prefs);
712         
713         account_clist_set();
714 }
715
716 static gboolean account_delete_references_func(GNode *node, gpointer data)
717 {
718         FolderItem *item;
719         gint account;
720
721         g_return_val_if_fail(node->data != NULL, FALSE);
722
723         item = FOLDER_ITEM(node->data);
724         account = GPOINTER_TO_INT(data);
725
726         if(!item->prefs) /* && item->prefs->stype == F_NORMAL */
727                 return FALSE;
728         if(item->prefs->default_account != account)
729                 return FALSE;
730         
731         item->prefs->enable_default_account = FALSE;
732         item->prefs->default_account = 0;
733         folder_item_prefs_save_config(item);
734
735         return FALSE;
736 }
737
738         
739 #define ACP_FDUP(fld) ac_clon->fld = ((ac_prefs->fld) != NULL)?\
740                                      g_strdup(ac_prefs->fld): NULL
741 #define ACP_FASSIGN(fld) ac_clon->fld = ac_prefs->fld
742 static void account_clone(void)
743 {
744         GtkCList *clist = GTK_CLIST(edit_account.clist);
745         gint row;
746         PrefsAccount *ac_prefs, *ac_clon;
747         GSList *hdrs = NULL;
748         CustomHeader *cch = NULL, *ch = NULL;
749
750         if (!clist->selection) return;
751
752         row = GPOINTER_TO_INT(clist->selection->data);
753         ac_prefs = gtk_clist_get_row_data(clist, row);
754
755         if (ac_prefs->protocol == A_IMAP4 || ac_prefs->protocol == A_NNTP) {
756                 alertpanel_error(_("Accounts with remote folders cannot be cloned"));
757                 return;
758         }
759         
760         ac_clon = prefs_account_new();
761         /* copy fields */
762         ac_clon->account_name = g_strdup_printf(_("Cloned %s"), 
763                                                 ac_prefs->account_name);
764         /* personal */
765         ACP_FDUP(name);
766         ACP_FDUP(address);
767         ACP_FDUP(organization);
768
769         /* server */
770         ACP_FASSIGN(protocol);
771         ACP_FDUP(recv_server);
772         ACP_FDUP(smtp_server);
773         ACP_FDUP(nntp_server);
774         ACP_FASSIGN(use_nntp_auth);
775         ACP_FASSIGN(use_nntp_auth_onconnect);
776         ACP_FDUP(userid);
777         ACP_FDUP(passwd);
778
779         ACP_FDUP(local_mbox);
780         ACP_FASSIGN(use_mail_command);
781         ACP_FDUP(mail_command);
782         
783 #if USE_OPENSSL
784         ACP_FASSIGN(ssl_pop);
785         ACP_FASSIGN(ssl_imap);
786         ACP_FASSIGN(ssl_nntp);
787         ACP_FASSIGN(ssl_smtp);
788         ACP_FASSIGN(use_nonblocking_ssl);
789 #endif /* USE_OPENSSL */
790         
791         ac_clon->tmp_pass = NULL;
792
793         /* receive */
794         ACP_FASSIGN(rmmail);
795         ACP_FASSIGN(msg_leave_time);
796         ACP_FASSIGN(getall);
797         ACP_FASSIGN(recv_at_getall);
798         ACP_FASSIGN(sd_rmmail_on_download);
799         ACP_FASSIGN(sd_filter_on_recv);
800         ACP_FASSIGN(enable_size_limit);
801         ACP_FASSIGN(size_limit);
802         ACP_FASSIGN(filter_on_recv);
803         ACP_FDUP(inbox);
804         ACP_FASSIGN(max_articles);
805
806         ACP_FASSIGN(imap_auth_type);
807
808         /* send */
809         ACP_FASSIGN(add_date);
810         ACP_FASSIGN(gen_msgid);
811         ACP_FASSIGN(add_customhdr);
812         ACP_FASSIGN(use_smtp_auth);
813         ACP_FASSIGN(smtp_auth_type);
814         ACP_FDUP(smtp_userid);
815         ACP_FDUP(smtp_passwd);
816
817         ac_clon->tmp_smtp_pass = NULL;
818
819         ACP_FASSIGN(pop_before_smtp);
820         ACP_FASSIGN(pop_before_smtp_timeout);
821         ACP_FASSIGN(last_pop_login_time);
822
823         ac_clon->customhdr_list = NULL;
824         hdrs = ac_prefs->customhdr_list;
825         while (hdrs != NULL) {
826                 ch = (CustomHeader *)hdrs->data;
827
828                 cch = g_new0(CustomHeader, 1);
829                 cch->account_id = ac_clon->account_id;  
830                 cch->name = (ch->name != NULL) ? g_strdup(ch->name) : NULL;
831                 cch->value = (ch->value != NULL) ? g_strdup(ch->value) : NULL;
832                 
833                 ac_clon->customhdr_list = g_slist_append(ac_clon->customhdr_list, cch);
834                 
835                 hdrs = g_slist_next(hdrs);
836         }
837
838         /* compose */
839         ACP_FASSIGN(sig_type);
840         ACP_FDUP(sig_path);
841         ACP_FASSIGN(auto_sig);
842         ACP_FDUP(sig_sep);
843         ACP_FASSIGN(set_autocc);
844         ACP_FDUP(auto_cc);
845         ACP_FASSIGN(set_autobcc);
846         ACP_FDUP(auto_bcc);
847         ACP_FASSIGN(set_autoreplyto);
848         ACP_FDUP(auto_replyto);
849
850         /* privacy */
851         ACP_FASSIGN(default_encrypt);
852         ACP_FASSIGN(default_sign);
853         
854         /* advanced */
855         ACP_FASSIGN(set_smtpport);
856         ACP_FASSIGN(smtpport);
857         ACP_FASSIGN(set_popport);
858         ACP_FASSIGN(popport);
859         ACP_FASSIGN(set_imapport);
860         ACP_FASSIGN(imapport);
861         ACP_FASSIGN(set_nntpport);
862         ACP_FASSIGN(nntpport);
863         ACP_FASSIGN(set_domain);
864         ACP_FDUP(domain);
865         ACP_FASSIGN(mark_crosspost_read);
866         ACP_FASSIGN(crosspost_col);
867
868         ACP_FASSIGN(set_tunnelcmd);
869         ACP_FDUP(tunnelcmd);
870
871         ACP_FDUP(imap_dir);
872
873         ACP_FASSIGN(set_sent_folder);
874         ACP_FDUP(sent_folder);
875         ACP_FASSIGN(set_draft_folder);
876         ACP_FDUP(draft_folder);
877         ACP_FASSIGN(set_trash_folder);
878         ACP_FDUP(trash_folder);
879         /* don't want two default accounts */
880         ac_clon->is_default = FALSE;
881         ACP_FASSIGN(folder);
882
883         account_list = g_list_append(account_list, ac_clon);
884         account_clist_set();
885 }
886 #undef ACP_FDUP
887 #undef ACP_FASSIGN
888
889 static void account_delete(void)
890 {
891         GtkCList *clist = GTK_CLIST(edit_account.clist);
892         PrefsAccount *ac_prefs;
893         gint row;
894         GList *list;
895         Folder *folder;
896         
897         if (!clist->selection) return;
898
899         if (alertpanel(_("Delete account"),
900                        _("Do you really want to delete this account?"),
901                        _("Yes"), _("+No"), NULL) != G_ALERTDEFAULT)
902                 return;
903
904         row = GPOINTER_TO_INT(clist->selection->data);
905         ac_prefs = gtk_clist_get_row_data(clist, row);
906         if (ac_prefs->folder) {
907                 FolderItem *item;
908
909                 item = mainwindow_get_mainwindow()->summaryview->folder_item;
910                 if (item && item->folder == FOLDER(ac_prefs->folder))
911                         summary_clear_all(mainwindow_get_mainwindow()->summaryview);
912                 folder_destroy(FOLDER(ac_prefs->folder));
913                 folderview_set_all();
914         }
915         account_destroy(ac_prefs);
916         account_clist_set();
917
918         debug_print("Removing deleted account references for all the folders...\n");
919         list = folder_get_list();
920         for (; list != NULL; list = list->next) {
921                 folder = FOLDER(list->data);
922                 if (folder->node)  /* && folder->type == F_? */
923                         g_node_traverse(folder->node, G_PRE_ORDER,
924                                 G_TRAVERSE_ALL, -1,
925                                 account_delete_references_func,
926                                 GINT_TO_POINTER(ac_prefs->account_id));
927         }
928 }
929
930 static void account_up(void)
931 {
932         GtkCList *clist = GTK_CLIST(edit_account.clist);
933         gint row;
934
935         if (!clist->selection) return;
936
937         row = GPOINTER_TO_INT(clist->selection->data);
938         if (row > 0)
939                 gtk_clist_row_move(clist, row, row - 1);
940 }
941
942 static void account_down(void)
943 {
944         GtkCList *clist = GTK_CLIST(edit_account.clist);
945         gint row;
946
947         if (!clist->selection) return;
948
949         row = GPOINTER_TO_INT(clist->selection->data);
950         if (row < clist->rows - 1)
951                 gtk_clist_row_move(clist, row, row + 1);
952 }
953
954 static void account_set_default(void)
955 {
956         GtkCList *clist = GTK_CLIST(edit_account.clist);
957         gint row;
958         PrefsAccount *ac_prefs;
959
960         if (!clist->selection) return;
961
962         row = GPOINTER_TO_INT(clist->selection->data);
963         ac_prefs = gtk_clist_get_row_data(clist, row);
964         account_set_as_default(ac_prefs);
965         account_clist_set();
966
967         cur_account = ac_prefs;
968         account_set_menu();
969         main_window_reflect_prefs_all();
970 }
971
972 static void account_edit_close(void)
973 {
974         account_list_set();
975         account_write_config_all();
976
977         if (!cur_account && account_list) {
978                 PrefsAccount *ac_prefs = (PrefsAccount *)account_list->data;
979                 account_set_as_default(ac_prefs);
980                 cur_account = ac_prefs;
981         }
982
983         account_set_menu();
984         main_window_reflect_prefs_all();
985
986         gtk_widget_hide(edit_account.window);
987
988         inc_unlock();
989 }
990
991 static gint account_delete_event(GtkWidget *widget, GdkEventAny *event,
992                                  gpointer data)
993 {
994         account_edit_close();
995         return TRUE;
996 }
997
998 static void account_selected(GtkCList *clist, gint row, gint column,
999                              GdkEvent *event, gpointer data)
1000 {
1001         if (event && event->type == GDK_2BUTTON_PRESS) {
1002                 account_edit_prefs();
1003                 return;
1004         }
1005
1006         if (column == COL_GETALL) {
1007                 PrefsAccount *ac;
1008
1009                 ac = gtk_clist_get_row_data(clist, row);
1010                 if (ac->protocol == A_POP3 || ac->protocol == A_IMAP4 ||
1011                     ac->protocol == A_NNTP || ac->protocol == A_LOCAL) {
1012                         ac->recv_at_getall ^= TRUE;
1013                         account_clist_set_row(ac, row);
1014                 }
1015         }
1016 }
1017
1018 static void account_row_moved(GtkCList *clist, gint source_row, gint dest_row)
1019 {
1020         account_list_set();
1021         if (gtk_clist_row_is_visible(clist, dest_row) != GTK_VISIBILITY_FULL)
1022                 gtk_clist_moveto(clist, dest_row, -1, 0.5, 0.0);
1023 }
1024
1025 static void account_key_pressed(GtkWidget *widget, GdkEventKey *event,
1026                                 gpointer data)
1027 {
1028         if (event && event->keyval == GDK_Escape)
1029                 account_edit_close();
1030 }
1031
1032 /* set one CList row or add new row */
1033 static gint account_clist_set_row(PrefsAccount *ac_prefs, gint row)
1034 {
1035         GtkCList *clist = GTK_CLIST(edit_account.clist);
1036         gchar *text[N_EDIT_ACCOUNT_COLS];
1037         gboolean has_getallbox;
1038         gboolean getall;
1039
1040         text[COL_DEFAULT] = "";
1041         text[COL_GETALL]  = "";
1042         text[COL_NAME]    = ac_prefs->account_name;
1043 #if USE_OPENSSL
1044         text[COL_PROTOCOL] = ac_prefs->protocol == A_POP3 ?
1045                              (ac_prefs->ssl_pop == SSL_TUNNEL ?
1046                               "POP3 (SSL)" :
1047                               ac_prefs->ssl_pop == SSL_STARTTLS ?
1048                               "POP3 (TLS)" : "POP3") :
1049                              ac_prefs->protocol == A_IMAP4 ?
1050                              (ac_prefs->ssl_imap == SSL_TUNNEL ?
1051                               "IMAP4 (SSL)" :
1052                               ac_prefs->ssl_imap == SSL_STARTTLS ?
1053                               "IMAP4 (TLS)" : "IMAP4") :
1054                              ac_prefs->protocol == A_NNTP ?
1055                              (ac_prefs->ssl_nntp == SSL_TUNNEL ?
1056                               "NNTP (SSL)" : "NNTP") :
1057                              "";
1058 #else
1059         text[COL_PROTOCOL] = ac_prefs->protocol == A_POP3  ? "POP3" :
1060                              ac_prefs->protocol == A_IMAP4 ? "IMAP4" :
1061                              ac_prefs->protocol == A_LOCAL ? "Local" :
1062                              ac_prefs->protocol == A_NNTP  ? "NNTP" : "";
1063 #endif
1064         text[COL_SERVER] = ac_prefs->protocol == A_NNTP
1065                 ? ac_prefs->nntp_server : ac_prefs->recv_server;
1066
1067         if (row < 0)
1068                 row = gtk_clist_append(clist, text);
1069         else {
1070                 gtk_clist_set_text(clist, row, COL_DEFAULT, text[COL_DEFAULT]);
1071                 gtk_clist_set_text(clist, row, COL_GETALL, text[COL_GETALL]);
1072                 gtk_clist_set_text(clist, row, COL_NAME, text[COL_NAME]);
1073                 gtk_clist_set_text(clist, row, COL_PROTOCOL, text[COL_PROTOCOL]);
1074                 gtk_clist_set_text(clist, row, COL_SERVER, text[COL_SERVER]);
1075         }
1076
1077         has_getallbox = (ac_prefs->protocol == A_POP3  ||
1078                          ac_prefs->protocol == A_IMAP4 ||
1079                          ac_prefs->protocol == A_NNTP ||
1080                          ac_prefs->protocol == A_LOCAL);
1081         getall = has_getallbox && ac_prefs->recv_at_getall;
1082
1083         if (ac_prefs->is_default)
1084                 gtk_clist_set_pixmap(clist, row, COL_DEFAULT,
1085                                      markxpm, markxpmmask);
1086         if (getall)
1087                 gtk_clist_set_pixmap(clist, row, COL_GETALL,
1088                                      checkboxonxpm, checkboxonxpmmask);
1089         else if (has_getallbox)
1090                 gtk_clist_set_pixmap(clist, row, COL_GETALL,
1091                                      checkboxoffxpm, checkboxoffxpmmask);
1092
1093         gtk_clist_set_row_data(clist, row, ac_prefs);
1094
1095         return row;
1096 }
1097
1098 /* set CList from account list */
1099 static void account_clist_set(void)
1100 {
1101         GtkCList *clist = GTK_CLIST(edit_account.clist);
1102         GList *cur;
1103         gint row = -1, prev_row = -1;
1104
1105         if (clist->selection)
1106                 prev_row = GPOINTER_TO_INT(clist->selection->data);
1107
1108         gtk_clist_freeze(clist);
1109         gtk_clist_clear(clist);
1110
1111         for (cur = account_list; cur != NULL; cur = cur->next) {
1112                 row = account_clist_set_row((PrefsAccount *)cur->data, -1);
1113                 if ((PrefsAccount *)cur->data == cur_account) {
1114                         gtk_clist_select_row(clist, row, -1);
1115                         gtkut_clist_set_focus_row(clist, row);
1116                 }
1117         }
1118
1119         if (prev_row >= 0) {
1120                 row = prev_row;
1121                 gtk_clist_select_row(clist, row, -1);
1122                 gtkut_clist_set_focus_row(clist, row);
1123         }
1124
1125         if (row >= 0 &&
1126             gtk_clist_row_is_visible(clist, row) != GTK_VISIBILITY_FULL)
1127                 gtk_clist_moveto(clist, row, -1, 0.5, 0);
1128
1129         gtk_clist_thaw(clist);
1130 }
1131
1132 /* set account list from CList */
1133 static void account_list_set(void)
1134 {
1135         GtkCList *clist = GTK_CLIST(edit_account.clist);
1136         gint row;
1137         PrefsAccount *ac_prefs;
1138
1139         while (account_list)
1140                 account_list = g_list_remove(account_list, account_list->data);
1141
1142         for (row = 0; (ac_prefs = gtk_clist_get_row_data(clist, row)) != NULL;
1143              row++)
1144                 account_list = g_list_append(account_list, ac_prefs);
1145 }
1146
1147 /*!
1148  *\brief        finds the PrefsAccounts which should be used to answer a mail
1149  *
1150  *\param        msginfo The message to be answered
1151  *\param        reply_autosel Indicates whether reply account autoselection is on
1152  *
1153  *\return       PrefsAccount * the correct account, NULL if not found
1154  */
1155 PrefsAccount *account_get_reply_account(MsgInfo *msginfo, gboolean reply_autosel)
1156 {
1157         PrefsAccount *account = NULL;
1158         /* select the account set in folderitem's property (if enabled) */
1159         if (msginfo->folder->prefs && msginfo->folder->prefs->enable_default_account)
1160                 account = account_find_from_id(msginfo->folder->prefs->default_account);
1161         
1162         /* select account by to: and cc: header if enabled */
1163         if (reply_autosel) {
1164                 gchar * field = NULL;
1165                 int fieldno = 0;
1166                 for (field = msginfo->to; fieldno++ < 2; field = msginfo->cc) {
1167                         if (!account && field) {
1168                                 gchar *to = NULL;
1169                                 if (!strchr(field, ',')) {
1170                                         Xstrdup_a(to, field, return NULL);
1171                                         extract_address(to);
1172                                         account = account_find_from_address(to);
1173                                 } else {
1174                                         gchar **split = g_strsplit(field, ",", -1);
1175                                         int i = -1;
1176                                         do {
1177                                                 i++;
1178                                                 if (!split[i])
1179                                                         break;
1180                                                 Xstrdup_a(to, split[i], return NULL);
1181                                                 extract_address(to);
1182                                                 account = account_find_from_address(to);
1183                                         } while (!account);
1184                                         g_strfreev(split);
1185                                 }
1186                         }
1187                 }
1188         }
1189
1190         /* select the account for the whole folder (IMAP / NNTP) */
1191         if (!account) 
1192                 /* FIXME: this is not right, because folder may be nested. we should
1193                  * ascend the tree until we find a parent with proper account 
1194                  * information */
1195                 account = msginfo->folder->folder->account;
1196
1197         /* select current account */
1198         if (!account) account = cur_account;
1199         
1200         return account;
1201 }