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