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