527b16d9f19d099a3ec470a2522576edebb57b97
[claws.git] / src / account.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2015 Hiroyuki Yamamoto and the Claws Mail team
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 3 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, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #include "claws-features.h"
23 #endif
24
25 #include "defs.h"
26
27 #include <glib.h>
28 #include <glib/gi18n.h>
29 #include <gtk/gtk.h>
30 #include <gdk/gdkkeysyms.h>
31 #include <stdio.h>
32 #include <errno.h>
33
34 #include "main.h"
35 #include "mainwindow.h"
36 #include "folderview.h"
37 #include "folder.h"
38 #include "account.h"
39 #include "prefs_gtk.h"
40 #include "prefs_account.h"
41 #include "prefs_common.h"
42 #include "folder_item_prefs.h"
43 #include "compose.h"
44 #include "manage_window.h"
45 #include "stock_pixmap.h"
46 #include "inc.h"
47 #include "gtkutils.h"
48 #include "utils.h"
49 #include "alertpanel.h"
50 #include "procheader.h"
51 #include "customheader.h"
52 #include "remotefolder.h"
53 #include "manual.h"
54 #include "filtering.h"
55 #include "prefs_actions.h"
56 #include "hooks.h"
57 #include "passwordstore.h"
58
59 enum {
60         ACCOUNT_IS_DEFAULT,
61         ACCOUNT_ENABLE_GET_ALL, 
62         ACCOUNT_NAME,
63         ACCOUNT_PROTOCOL,
64         ACCOUNT_SERVER,
65         ACCOUNT_DATA,
66         N_ACCOUNT_COLUMNS
67 };
68
69
70 typedef enum
71 {
72         COL_DEFAULT     = 0,
73         COL_GETALL      = 1,
74         COL_NAME        = 2,
75         COL_PROTOCOL    = 3,
76         COL_SERVER      = 4
77 } EditAccountColumnPos;
78
79 # define N_EDIT_ACCOUNT_COLS    5
80
81 PrefsAccount *cur_account;
82
83 static GList *account_list = NULL;
84 static gboolean account_list_dirty = FALSE;
85
86 static struct EditAccount {
87         GtkWidget *window;
88         GtkWidget *list_view;
89         GtkWidget *close_btn;
90 } edit_account;
91
92 static void account_edit_create         (void);
93 static void           account_destroy           (PrefsAccount   *ac_prefs);
94 static void           account_set_as_default    (PrefsAccount   *ac_prefs);
95 static void           account_set_menu          (void);
96
97 static void account_edit_prefs          (GtkWidget *widget, gpointer data);
98 static void account_delete              (GtkWidget *widget, gpointer data);
99 static void account_clone               (GtkWidget *widget, gpointer data);
100
101 static void account_up                  (GtkWidget *widget, gpointer data);
102 static void account_down                (GtkWidget *widget, gpointer data);
103
104 static void account_set_default         (GtkWidget *widget, gpointer data);
105
106 static void account_edit_close          (GtkWidget *widget, gpointer data);
107
108 static gint account_delete_event        (GtkWidget      *widget,
109                                          GdkEventAny    *event,
110                                          gpointer        data);
111 static void account_size_allocate_cb(GtkWidget *widget,
112                                          GtkAllocation *allocation);
113 static gboolean account_key_pressed     (GtkWidget      *widget,
114                                          GdkEventKey    *event,
115                                          gpointer        data);
116 static gboolean account_search_func_cb (GtkTreeModel *model, gint column, 
117                                                 const gchar *key, GtkTreeIter *iter, 
118                                                 gpointer search_data);
119 static void account_list_view_add       (PrefsAccount   *ac_prefs);
120 static void account_list_view_set       (void);
121
122 static void account_list_set            (void);
123
124 typedef struct FindAccountInStore {
125         gint             account_id;
126         GtkTreePath     *path;
127         GtkTreeIter      iter;
128 } FindAccountInStore;
129
130 static GtkListStore* account_create_data_store  (void);
131
132 static void account_list_view_insert_account_item (GtkListStore *list_store, 
133                                                    const gchar  *account_name,
134                                                    const gchar  *protocol, 
135                                                    const gchar  *server_name,
136                                                    gboolean      is_default, 
137                                                    gboolean      is_get_all,
138                                                    PrefsAccount *account_data);
139
140 static GtkWidget *account_list_view_create      (void);
141 static void account_create_list_view_columns    (GtkWidget *list_view);
142
143 static gint account_list_view_get_selected_account_id           (GtkWidget *list_view);
144 static GtkTreePath *account_list_view_get_selected_account_path (GtkWidget *list_view);
145 static PrefsAccount *account_list_view_get_selected_account             (GtkWidget *list_view);
146 static gboolean account_list_view_select_account                        (GtkWidget *list_view, 
147                                                                  gint       account_id);
148
149 static void account_list_view_set_default_by_id(GtkWidget *list_view,
150                                                 gint account_id);
151
152 static gboolean set_new_default_account         (GtkTreeModel *model,
153                                                  GtkTreePath  *path,
154                                                  GtkTreeIter  *iter,
155                                                  gint         *account_id);
156
157 static gboolean find_account_in_store           (GtkTreeModel *model,
158                                                  GtkTreePath  *path,
159                                                  GtkTreeIter  *iter,
160                                                  FindAccountInStore *data);
161
162 static void account_get_all_toggled             (GtkCellRendererToggle  *widget, 
163                                                  gchar                  *path, 
164                                                  GtkWidget              *list_view);
165                                                  
166 static void account_double_clicked              (GtkTreeView            *list_view,
167                                                  GtkTreePath            *path,
168                                                  GtkTreeViewColumn      *column,
169                                                  gpointer                data);
170                                                  
171 static void drag_begin                          (GtkTreeView *list_view,
172                                                  GdkDragContext *context,
173                                                  gpointer data);
174
175 static void drag_end                            (GtkTreeView *list_view,
176                                                  GdkDragContext *context,
177                                                  gpointer data);
178                       
179 static void account_row_changed_while_drag_drop (GtkTreeModel *model, 
180                                                  GtkTreePath  *path,
181                                                  GtkTreeIter  *iter,
182                                                  gpointer      arg3,
183                                                  GtkTreeView  *list_view);
184
185 static void account_flush_state(void)
186 {
187         account_set_menu();
188         main_window_reflect_prefs_all();
189
190         account_list_dirty = FALSE;
191 }
192
193 void account_read_config_all(void)
194 {
195         GSList *ac_label_list = NULL, *cur;
196         gchar *rcpath;
197         FILE *fp;
198         gchar buf[PREFSBUFSIZE];
199         PrefsAccount *ac_prefs;
200
201         debug_print("Reading all config for each account...\n");
202
203         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACCOUNT_RC, NULL);
204         if ((fp = g_fopen(rcpath, "rb")) == NULL) {
205                 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
206                 g_free(rcpath);
207                 return;
208         }
209         g_free(rcpath);
210
211         while (fgets(buf, sizeof(buf), fp) != NULL) {
212                 if (!strncmp(buf, "[Account: ", 10)) {
213                         strretchomp(buf);
214                         memmove(buf, buf + 1, sizeof(buf) - 1);
215                         buf[strlen(buf) - 1] = '\0';
216                         debug_print("Found label: %s\n", buf);
217                         ac_label_list = g_slist_append(ac_label_list,
218                                                        g_strdup(buf));
219                 }
220         }
221         fclose(fp);
222
223         /* read config data from file */
224         cur_account = NULL;
225         for (cur = ac_label_list; cur != NULL; cur = cur->next) {
226                 ac_prefs = prefs_account_new_from_config((gchar *)cur->data);
227                 account_list = g_list_append(account_list, ac_prefs);
228                 if (ac_prefs->is_default)
229                         cur_account = ac_prefs;
230         }
231         /* if default is not set, assume first account as default */
232         if (!cur_account && account_list) {
233                 ac_prefs = (PrefsAccount *)account_list->data;
234                 account_set_as_default(ac_prefs);
235                 cur_account = ac_prefs;
236         }
237
238         account_set_menu();
239         main_window_reflect_prefs_all_now();
240
241         while (ac_label_list) {
242                 g_free(ac_label_list->data);
243                 ac_label_list = g_slist_remove(ac_label_list,
244                                                ac_label_list->data);
245         }
246 }
247
248 void account_write_config_all(void)
249 {
250         prefs_account_write_config_all(account_list);
251 }
252
253 /*
254  * account_find_all_from_address:
255  * @ac_list: initial list of accounts. NULL to create a new one.
256  * Accounts found in the @address will be appended to this list.
257  * @address: Email address string.
258  *
259  * Find all the mail (not news) accounts within the specified address.
260  *
261  * Return value: the original accounts list with the found accounts appended.
262  */
263 GList *account_find_all_from_address(GList *ac_list, const gchar *address)
264 {
265         GList *cur;
266         PrefsAccount *ac;
267
268         if (address == NULL)
269                 return ac_list;
270
271         for (cur = account_list; cur != NULL; cur = cur->next) {
272                 ac = (PrefsAccount *)cur->data;
273                 if (ac->protocol != A_NNTP && ac->address &&
274                     strcasestr(address, ac->address) != NULL)
275                         ac_list = g_list_append(ac_list, ac);
276         }
277         return ac_list;
278 }
279         
280 PrefsAccount *account_find_from_smtp_server(const gchar *address,
281                                             const gchar *smtp_server)
282 {
283         GList *cur;
284         PrefsAccount *ac;
285
286         cm_return_val_if_fail(address != NULL, NULL);
287         cm_return_val_if_fail(smtp_server != NULL, NULL);
288
289         for (cur = account_list; cur != NULL; cur = cur->next) {
290                 ac = (PrefsAccount *)cur->data;
291                 if (!strcmp2(address, ac->address) &&
292                     !strcmp2(smtp_server, ac->smtp_server))
293                         return ac;
294         }
295
296         return NULL;
297 }
298
299 /*
300  * account_find_from_address:
301  * @address: Email address string.
302  *
303  * Find a mail (not news if newsgroups_ok is FALSE) account with the specified email address.
304  *
305  * Return value: The found account, or NULL if not found.
306  */
307 PrefsAccount *account_find_from_address(const gchar *address, gboolean newsgroups_ok)
308 {
309         GList *cur;
310         PrefsAccount *ac;
311
312         cm_return_val_if_fail(address != NULL, NULL);
313
314         for (cur = account_list; cur != NULL; cur = cur->next) {
315                 ac = (PrefsAccount *)cur->data;
316                 if ((ac->protocol != A_NNTP || newsgroups_ok) && ac->address &&
317                     g_ascii_strcasecmp(address, ac->address) == 0)
318                         return ac;
319         }
320
321         return NULL;
322 }
323
324 PrefsAccount *account_find_from_id(gint id)
325 {
326         GList *cur;
327         PrefsAccount *ac;
328
329         for (cur = account_list; cur != NULL; cur = cur->next) {
330                 ac = (PrefsAccount *)cur->data;
331                 if (id == ac->account_id)
332                         return ac;
333         }
334
335         return NULL;
336 }
337
338 PrefsAccount *account_find_from_item(FolderItem *item)
339 {
340         PrefsAccount *ac;
341
342         cm_return_val_if_fail(item != NULL, NULL);
343
344         ac = item->account;
345         if (!ac) {
346                 FolderItem *cur_item = folder_item_parent(item);
347                 while (cur_item != NULL) {
348                         if (cur_item->account && cur_item->apply_sub) {
349                                 ac = cur_item->account;
350                                 break;
351                         }                               
352                         cur_item = folder_item_parent(cur_item);
353                 }
354         }
355         if (!ac)
356                 ac = item->folder->account;
357
358         return ac;
359 }
360
361 static void account_set_menu(void)
362 {
363         main_window_set_account_menu(account_list);
364 }
365
366 void account_set_menu_only_toolbar(void)
367 {
368         main_window_set_account_menu_only_toolbar(account_list);
369 }
370
371 GList *account_get_list(void)
372 {
373         return account_list;
374 }
375
376 void account_edit_focus(void)
377 {
378         if (edit_account.window == NULL) {
379                 return;
380         }
381         manage_window_set_transient(GTK_WINDOW(edit_account.window));
382         gtk_widget_grab_focus(edit_account.close_btn);
383         gtk_widget_show(edit_account.window);
384         gtk_window_set_modal(GTK_WINDOW(edit_account.window), TRUE);
385         manage_window_focus_in(edit_account.window, NULL, NULL);
386 }
387
388 void account_edit_open(gpointer a, gpointer b)
389 {
390         inc_lock();
391
392         account_list_dirty = FALSE;
393
394         if (compose_get_compose_list()) {
395                 alertpanel_error(_("Some composing windows are open.\n"
396                                    "Please close all the composing "
397                                    "windows before editing accounts."));
398                 inc_unlock();
399                 return;
400         }
401
402         debug_print("Opening account edit window...\n");
403
404         if (!edit_account.window)
405                 account_edit_create();
406
407         account_list_view_set();
408
409         account_edit_focus();
410 }
411
412 void account_add(void)
413 {
414         PrefsAccount *ac_prefs;
415
416         ac_prefs = prefs_account_open(NULL, &account_list_dirty);
417
418         if (!ac_prefs) return;
419
420         account_edit_focus();
421
422         account_list = g_list_append(account_list, ac_prefs);
423
424         if (ac_prefs->is_default)
425                 account_set_as_default(ac_prefs);
426
427         account_list_view_set();
428
429         if (ac_prefs->protocol == A_IMAP4 || ac_prefs->protocol == A_NNTP) {
430                 Folder *folder;
431
432                 if (ac_prefs->protocol == A_IMAP4) {
433                         folder = folder_new(folder_get_class_from_string("imap"), ac_prefs->account_name,
434                                             ac_prefs->recv_server);
435                 } else {
436                         folder = folder_new(folder_get_class_from_string("news"), ac_prefs->account_name,
437                                             ac_prefs->nntp_server);
438                 }
439                 if (folder == NULL) {
440                         alertpanel_error(_("Can't create folder."));
441                         return;
442                 }
443                 folder->account = ac_prefs;
444                 ac_prefs->folder = folder;
445                 folder_add(folder);
446                 if (ac_prefs->protocol == A_IMAP4)
447                         folder->klass->create_tree(folder);
448                 folderview_set_all();
449                 folder_write_list();
450         }
451 }
452
453 void account_open(PrefsAccount *ac_prefs, gboolean called_from_acc_list)
454 {
455         gboolean prev_default;
456         gchar *ac_name, *old_prefix, *new_prefix;
457         gboolean account_dirty = FALSE;
458
459         cm_return_if_fail(ac_prefs != NULL);
460
461         if (compose_get_compose_list()) {
462                 alertpanel_error(_("Some composing windows are open.\n"
463                                    "Please close all the composing "
464                                    "windows before editing accounts."));
465                 return;
466         }
467
468         prev_default = ac_prefs->is_default;
469         Xstrdup_a(ac_name, ac_prefs->account_name ? ac_prefs->account_name : "",
470                   return);
471
472         prefs_account_open(ac_prefs, &account_dirty);
473
474         if (called_from_acc_list)
475                 account_edit_focus();
476
477         if (account_dirty) {
478                 if (!prev_default && ac_prefs->is_default)
479                         account_set_as_default(ac_prefs);
480
481                 if (ac_prefs->folder && strcmp2(ac_name, ac_prefs->account_name) != 0) {
482                         old_prefix = folder_get_identifier(FOLDER(ac_prefs->folder));
483                         folder_set_name(FOLDER(ac_prefs->folder),
484                                         ac_prefs->account_name);
485                         folderview_set_all();
486                         folder_prefs_save_config_recursive(FOLDER(ac_prefs->folder));
487                         new_prefix = folder_get_identifier(FOLDER(ac_prefs->folder));
488
489                         account_rename_path(old_prefix, new_prefix);
490                         prefs_filtering_rename_path(old_prefix, new_prefix);
491                         prefs_actions_rename_path(old_prefix, new_prefix);
492                         
493                         g_free(old_prefix);
494                         g_free(new_prefix);
495                 }
496
497                 account_write_config_all();
498
499                 account_flush_state();
500         }
501 }
502
503 static void account_set_as_default(PrefsAccount *ac_prefs)
504 {
505         PrefsAccount *ap;
506         GList *cur;
507
508         for (cur = account_list; cur != NULL; cur = cur->next) {
509                 ap = (PrefsAccount *)cur->data;
510                 if (ap->is_default)
511                         ap->is_default = FALSE;
512         }
513
514         ac_prefs->is_default = TRUE;
515 }
516
517 PrefsAccount *account_get_default(void)
518 {
519         PrefsAccount *ap;
520         GList *cur;
521
522         for (cur = account_list; cur != NULL; cur = cur->next) {
523                 ap = (PrefsAccount *)cur->data;
524                 if (ap->is_default)
525                         return ap;
526         }
527
528         return NULL;
529 }
530
531 void account_set_missing_folder(void)
532 {
533         PrefsAccount *ap;
534         GList *cur;
535
536         for (cur = account_list; cur != NULL; cur = cur->next) {
537                 ap = (PrefsAccount *)cur->data;
538                 if ((ap->protocol == A_IMAP4 || ap->protocol == A_NNTP) &&
539                     !ap->folder) {
540                         Folder *folder;
541
542                         if (ap->protocol == A_IMAP4) {
543                                 folder = folder_new(folder_get_class_from_string("imap"), ap->account_name,
544                                                     ap->recv_server);
545                         } else {
546                                 folder = folder_new(folder_get_class_from_string("news"), ap->account_name,
547                                                     ap->nntp_server);
548                         }
549                         if (folder == NULL)
550                                 return;
551                         folder->account = ap;
552                         ap->folder = folder;
553                         folder_add(folder);
554                         if (ap->protocol == A_IMAP4)
555                                 folder->klass->create_tree(folder);
556                         folder_write_list();
557
558                 }
559         }
560 }
561
562 #define CHECK_CHANGE_FOLDER(folder) {                                           \
563         if (folder && !strncmp(folder, old_id, strlen(old_id))) {               \
564                 if (strlen(folder) == strlen(old_id)) {                         \
565                         g_free(folder);                                         \
566                         folder = g_strdup(new_id);                              \
567                 } else if (strlen(folder) > strlen(old_id)                      \
568                   && folder[strlen(old_id)] == G_DIR_SEPARATOR) {               \
569                         gchar *new_path = g_strdup_printf("%s%s",               \
570                                         new_id, (folder + strlen(old_id)));     \
571                         g_free(folder);                                         \
572                         folder = new_path;                                      \
573                 }                                                               \
574         }                                                                       \
575 }
576
577 void account_rename_path(const gchar *old_id, const gchar *new_id)
578 {
579         GList *cur = account_list;
580         for (; cur != NULL; cur = g_list_next(cur)) {
581                 PrefsAccount *ap = (PrefsAccount *)cur->data;
582                 CHECK_CHANGE_FOLDER(ap->inbox);
583                 CHECK_CHANGE_FOLDER(ap->local_inbox);
584                 CHECK_CHANGE_FOLDER(ap->queue_folder);
585                 CHECK_CHANGE_FOLDER(ap->sent_folder);
586                 CHECK_CHANGE_FOLDER(ap->draft_folder);
587                 CHECK_CHANGE_FOLDER(ap->trash_folder);
588         }
589 }
590
591 FolderItem *account_get_special_folder(PrefsAccount *ac_prefs,
592                                        SpecialFolderItemType type)
593 {
594         FolderItem *item = NULL;
595
596         cm_return_val_if_fail(ac_prefs != NULL, NULL);
597
598         switch (type) {
599         case F_INBOX:
600                 if (ac_prefs->folder)
601                         item = FOLDER(ac_prefs->folder)->inbox;
602                 if (!item)
603                         item = folder_get_default_inbox();
604                 break;
605         case F_OUTBOX:
606                 if (ac_prefs->set_sent_folder && ac_prefs->sent_folder) {
607                         item = folder_find_item_from_identifier
608                                 (ac_prefs->sent_folder);
609                 }
610                 if (!item) {
611                         if (ac_prefs->folder)
612                                 item = FOLDER(ac_prefs->folder)->outbox;
613                         if (!item)
614                                 item = folder_get_default_outbox_for_class(F_MH);
615                         if (!item)
616                                 item = folder_get_default_outbox();
617                 }
618                 break;
619         case F_DRAFT:
620                 if (ac_prefs->set_draft_folder && ac_prefs->draft_folder) {
621                         item = folder_find_item_from_identifier
622                                 (ac_prefs->draft_folder);
623                 }
624                 if (!item) {
625                         if (ac_prefs->folder)
626                                 item = FOLDER(ac_prefs->folder)->draft;
627                         if (!item)
628                                 item = folder_get_default_draft_for_class(F_MH);
629                         if (!item)
630                                 item = folder_get_default_draft();
631                 }
632                 break;
633         case F_QUEUE:
634                 if (ac_prefs->set_queue_folder && ac_prefs->queue_folder) {
635                         item = folder_find_item_from_identifier
636                                 (ac_prefs->queue_folder);
637                 }
638                 if (!item) {
639                         if (ac_prefs->folder)
640                                 item = FOLDER(ac_prefs->folder)->queue;
641                         if (!item)
642                                 item = folder_get_default_queue_for_class(F_MH);
643                         if (!item)
644                                 item = folder_get_default_queue();
645                 }
646                 break;
647         case F_TRASH:
648                 if (ac_prefs->set_trash_folder && ac_prefs->trash_folder) {
649                         item = folder_find_item_from_identifier
650                                 (ac_prefs->trash_folder);
651                 }
652                 if (!item) {
653                         if (ac_prefs->folder)
654                                 item = FOLDER(ac_prefs->folder)->trash;
655                         if (!item)
656                                 item = folder_get_default_trash_for_class(F_MH);
657                         if (!item)
658                                 item = folder_get_default_trash();
659                 }
660                 break;
661         default:
662                 break;
663         }
664
665         return item;
666 }
667
668 void account_destroy(PrefsAccount *ac_prefs)
669 {
670         cm_return_if_fail(ac_prefs != NULL);
671
672         folder_unref_account_all(ac_prefs);
673
674         account_list = g_list_remove(account_list, ac_prefs);
675
676         if (cur_account == ac_prefs) cur_account = NULL;
677         if (!cur_account && account_list) {
678                 cur_account = account_get_default();
679                 if (!cur_account) {
680                         ac_prefs = (PrefsAccount *)account_list->data;
681                         account_set_as_default(ac_prefs);
682                         cur_account = ac_prefs;
683                 }
684         }
685 }
686
687 /*!
688  *\brief        Save Gtk object size to prefs dataset
689  */
690 static void account_size_allocate_cb(GtkWidget *widget,
691                                          GtkAllocation *allocation)
692 {
693         cm_return_if_fail(allocation != NULL);
694
695         prefs_common.accountswin_width = allocation->width;
696         prefs_common.accountswin_height = allocation->height;
697 }
698
699 static void account_edit_create(void)
700 {
701         GtkWidget *window;
702         GtkWidget *vbox;
703         GtkWidget *label;
704         GtkWidget *hbox;
705         GtkWidget *scrolledwin;
706         GtkWidget *list_view;
707
708         GtkWidget *vbox2;
709         GtkWidget *add_btn;
710         GtkWidget *edit_btn;
711         GtkWidget *del_btn;
712         GtkWidget *clone_btn;
713         GtkWidget *up_btn;
714         GtkWidget *down_btn;
715
716         GtkWidget *default_btn;
717
718         GtkWidget *confirm_area;
719         GtkWidget *help_btn;
720         GtkWidget *close_btn;
721
722         static GdkGeometry geometry;
723
724         debug_print("Creating account edit window...\n");
725
726         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "account");
727         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
728         gtk_window_set_title (GTK_WINDOW (window), _("Edit accounts"));
729         g_signal_connect (G_OBJECT (window), "delete_event",
730                           G_CALLBACK (account_delete_event), NULL);
731         g_signal_connect (G_OBJECT (window), "key_press_event",
732                           G_CALLBACK (account_key_pressed), NULL);
733         MANAGE_WINDOW_SIGNALS_CONNECT (window);
734         gtk_widget_realize(window);
735
736         vbox = gtk_vbox_new (FALSE, 10);
737         gtk_widget_show (vbox);
738         gtk_container_add (GTK_CONTAINER (window), vbox);
739
740         hbox = gtk_hbox_new (FALSE, 0);
741         gtk_widget_show (hbox);
742         gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
743
744         label = gtk_label_new
745                 (_("Using 'Get Mail' will retrieve messages from your Accounts "
746                    "in the order given, the checkbox indicates which accounts "
747                    "will be included. Bold text indicates the default account."));
748         gtk_widget_show (label);
749         gtk_widget_set_size_request(GTK_WIDGET(label), 
750                                     prefs_common.accountswin_width-8, -1);
751         gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 4);
752         gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
753         gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
754
755         hbox = gtk_hbox_new (FALSE, 8);
756         gtk_widget_show (hbox);
757         gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
758         gtk_container_set_border_width (GTK_CONTAINER (hbox), 2);
759
760         scrolledwin = gtk_scrolled_window_new (NULL, NULL);
761         gtk_widget_show (scrolledwin);
762         gtk_box_pack_start (GTK_BOX (hbox), scrolledwin, TRUE, TRUE, 0);
763         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwin),
764                                         GTK_POLICY_AUTOMATIC,
765                                         GTK_POLICY_AUTOMATIC);
766
767         list_view = account_list_view_create();
768         gtk_widget_show(list_view);
769         gtk_container_add(GTK_CONTAINER(scrolledwin), list_view);
770
771         vbox2 = gtk_vbox_new (FALSE, 0);
772         gtk_widget_show (vbox2);
773         gtk_box_pack_start (GTK_BOX (hbox), vbox2, FALSE, FALSE, 0);
774
775         add_btn = gtk_button_new_from_stock(GTK_STOCK_NEW);
776         gtk_widget_show (add_btn);
777         gtk_box_pack_start (GTK_BOX (vbox2), add_btn, FALSE, FALSE, 4);
778         g_signal_connect (G_OBJECT(add_btn), "clicked",
779                           G_CALLBACK (account_add), NULL);
780
781         edit_btn = gtk_button_new_from_stock (GTK_STOCK_EDIT);
782         gtk_widget_show (edit_btn);
783         gtk_box_pack_start (GTK_BOX (vbox2), edit_btn, FALSE, FALSE, 4);
784         g_signal_connect (G_OBJECT(edit_btn), "clicked",
785                           G_CALLBACK (account_edit_prefs), NULL);
786
787         del_btn = gtk_button_new_from_stock(GTK_STOCK_DELETE);
788         gtk_widget_show (del_btn);
789         gtk_box_pack_start (GTK_BOX (vbox2), del_btn, FALSE, FALSE, 4);
790         g_signal_connect (G_OBJECT(del_btn), "clicked",
791                           G_CALLBACK (account_delete), NULL);
792
793         clone_btn = gtk_button_new_from_stock(GTK_STOCK_COPY);
794         gtk_widget_show (clone_btn);
795         gtk_box_pack_start (GTK_BOX (vbox2), clone_btn, FALSE, FALSE, 4);
796         g_signal_connect(G_OBJECT(clone_btn), "clicked",
797                          G_CALLBACK(account_clone), NULL);
798         
799         down_btn = gtk_button_new_from_stock(GTK_STOCK_GO_DOWN);
800         gtk_widget_show (down_btn);
801         gtk_box_pack_end (GTK_BOX (vbox2), down_btn, FALSE, FALSE, 4);
802         g_signal_connect (G_OBJECT(down_btn), "clicked",
803                           G_CALLBACK (account_down), NULL);
804
805         up_btn = gtk_button_new_from_stock(GTK_STOCK_GO_UP);
806         gtk_widget_show (up_btn);
807         gtk_box_pack_end (GTK_BOX (vbox2), up_btn, FALSE, FALSE, 4);
808         g_signal_connect (G_OBJECT(up_btn), "clicked",
809                           G_CALLBACK (account_up), NULL);
810
811         hbox = gtk_hbox_new (FALSE, 8);
812         gtk_widget_show (hbox);
813         gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
814
815         default_btn = gtk_button_new_with_mnemonic
816                 (_(" _Set as default account "));
817         gtk_widget_show (default_btn);
818         gtk_box_pack_start (GTK_BOX (hbox), default_btn, FALSE, FALSE, 0);
819         g_signal_connect (G_OBJECT(default_btn), "clicked",
820                           G_CALLBACK (account_set_default), NULL);
821
822         gtkut_stock_button_set_create_with_help(&confirm_area, &help_btn,
823                         &close_btn, GTK_STOCK_CLOSE,
824                         NULL, NULL, NULL, NULL);
825         gtk_widget_show(confirm_area);
826
827         gtk_box_pack_end (GTK_BOX (hbox), confirm_area, FALSE, FALSE, 0);
828         gtk_widget_grab_default (close_btn);
829
830         g_signal_connect (G_OBJECT (close_btn), "clicked",
831                           G_CALLBACK (account_edit_close),
832                           NULL);
833         g_signal_connect(G_OBJECT(help_btn), "clicked",
834                         G_CALLBACK(manual_open_with_anchor_cb),
835                         MANUAL_ANCHOR_ACCOUNTPREFS);
836
837
838         g_signal_connect(G_OBJECT(window), "size_allocate",
839                          G_CALLBACK(account_size_allocate_cb), NULL);
840
841         if (!geometry.min_height) {
842                 geometry.min_width = 500;
843                 geometry.min_height = 380;
844         }
845
846         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
847                                       GDK_HINT_MIN_SIZE);
848         gtk_widget_set_size_request(window, prefs_common.accountswin_width,
849                                     prefs_common.accountswin_height);
850 #ifdef G_OS_WIN32
851         gtk_window_move(GTK_WINDOW(window), 48, 48);
852 #endif
853
854         edit_account.window    = window;
855         edit_account.list_view = list_view;
856         edit_account.close_btn = close_btn;
857 }
858
859 static void account_edit_prefs(GtkWidget *widget, gpointer data)
860 {
861         PrefsAccount *ac_prefs;
862
863         ac_prefs = account_list_view_get_selected_account(edit_account.list_view);
864         
865         if (ac_prefs) {
866                 account_open(ac_prefs, TRUE);
867                 account_list_view_set();
868         }               
869 }
870
871 static gboolean account_delete_references_func(GNode *node, gpointer data)
872 {
873         FolderItem *item;
874         gint account;
875
876         cm_return_val_if_fail(node->data != NULL, FALSE);
877
878         item = FOLDER_ITEM(node->data);
879         account = GPOINTER_TO_INT(data);
880
881         if(!item->prefs) /* && item->prefs->stype == F_NORMAL */
882                 return FALSE;
883         if(item->prefs->default_account != account)
884                 return FALSE;
885         
886         item->prefs->enable_default_account = FALSE;
887         item->prefs->default_account = 0;
888         folder_item_prefs_save_config(item);
889
890         return FALSE;
891 }
892
893         
894 #define ACP_FDUP(fld) ac_clon->fld = ((ac_prefs->fld) != NULL)?\
895                                      g_strdup(ac_prefs->fld): NULL
896 #define ACP_FASSIGN(fld) ac_clon->fld = ac_prefs->fld
897 static void account_clone(GtkWidget *widget, gpointer data)
898 {
899         PrefsAccount *ac_prefs, *ac_clon;
900         GSList *hdrs = NULL;
901         CustomHeader *cch = NULL, *ch = NULL;
902         
903         ac_prefs = account_list_view_get_selected_account(edit_account.list_view);
904         if (ac_prefs == NULL)
905                 return;
906
907         if (ac_prefs->protocol == A_IMAP4 || ac_prefs->protocol == A_NNTP) {
908                 alertpanel_error(_("Accounts with remote folders cannot be copied."));
909                 return;
910         }
911         account_list_dirty = TRUE;
912         
913         ac_clon = prefs_account_new();
914         /* copy fields */
915         ac_clon->account_name = g_strdup_printf(_("Copy of %s"),
916                                                 ac_prefs->account_name);
917         /* personal */
918         ACP_FDUP(name);
919         ACP_FDUP(address);
920         ACP_FDUP(organization);
921
922         /* server */
923         ACP_FASSIGN(protocol);
924         ACP_FDUP(recv_server);
925         ACP_FDUP(smtp_server);
926         ACP_FDUP(nntp_server);
927         ACP_FASSIGN(use_nntp_auth);
928         ACP_FASSIGN(use_nntp_auth_onconnect);
929         ACP_FDUP(userid);
930         ACP_FDUP(passwd);
931
932         ACP_FDUP(local_mbox);
933         ACP_FASSIGN(use_mail_command);
934         ACP_FDUP(mail_command);
935         
936         ACP_FASSIGN(ssl_pop);
937         ACP_FASSIGN(ssl_imap);
938         ACP_FASSIGN(ssl_nntp);
939         ACP_FASSIGN(ssl_smtp);
940         ACP_FASSIGN(use_nonblocking_ssl);
941         
942         /* receive */
943         ACP_FASSIGN(use_apop_auth);
944         ACP_FASSIGN(rmmail);
945         ACP_FASSIGN(msg_leave_time);
946         ACP_FASSIGN(msg_leave_hour);
947         ACP_FASSIGN(recv_at_getall);
948         ACP_FASSIGN(sd_rmmail_on_download);
949         ACP_FASSIGN(enable_size_limit);
950         ACP_FASSIGN(size_limit);
951         ACP_FASSIGN(filter_on_recv);
952         ACP_FASSIGN(filterhook_on_recv);
953         ACP_FDUP(inbox);
954         ACP_FDUP(local_inbox);
955         ACP_FASSIGN(max_articles);
956
957         ACP_FASSIGN(imap_auth_type);
958
959         /* send */
960         ACP_FASSIGN(gen_msgid);
961         ACP_FASSIGN(gen_xmailer);
962         ACP_FASSIGN(add_customhdr);
963         ACP_FASSIGN(use_smtp_auth);
964         ACP_FASSIGN(smtp_auth_type);
965         ACP_FDUP(smtp_userid);
966         ACP_FDUP(smtp_passwd);
967
968         ACP_FASSIGN(pop_before_smtp);
969         ACP_FASSIGN(pop_before_smtp_timeout);
970         ACP_FASSIGN(last_pop_login_time);
971
972         ac_clon->customhdr_list = NULL;
973         hdrs = ac_prefs->customhdr_list;
974         while (hdrs != NULL) {
975                 ch = (CustomHeader *)hdrs->data;
976
977                 cch = g_new0(CustomHeader, 1);
978                 cch->account_id = ac_clon->account_id;  
979                 cch->name = (ch->name != NULL) ? g_strdup(ch->name) : NULL;
980                 cch->value = (ch->value != NULL) ? g_strdup(ch->value) : NULL;
981                 
982                 ac_clon->customhdr_list = g_slist_append(ac_clon->customhdr_list, cch);
983                 
984                 hdrs = g_slist_next(hdrs);
985         }
986
987         /* compose */
988         ACP_FASSIGN(sig_type);
989         ACP_FDUP(sig_path);
990         ACP_FASSIGN(auto_sig);
991         ACP_FDUP(sig_sep);
992         ACP_FASSIGN(set_autocc);
993         ACP_FDUP(auto_cc);
994         ACP_FASSIGN(set_autobcc);
995         ACP_FDUP(auto_bcc);
996         ACP_FASSIGN(set_autoreplyto);
997         ACP_FDUP(auto_replyto);
998         ACP_FASSIGN(enable_default_dictionary);
999         ACP_FDUP(default_dictionary);
1000         ACP_FASSIGN(enable_default_alt_dictionary);
1001         ACP_FDUP(default_alt_dictionary);
1002         ACP_FASSIGN(compose_with_format);
1003         ACP_FDUP(compose_subject_format);
1004         ACP_FDUP(compose_body_format);
1005         ACP_FASSIGN(reply_with_format);
1006         ACP_FDUP(reply_quotemark);
1007         ACP_FDUP(reply_body_format);
1008         ACP_FASSIGN(forward_with_format);
1009         ACP_FDUP(forward_quotemark);
1010         ACP_FDUP(forward_body_format);
1011
1012         /* privacy */
1013         ACP_FDUP(default_privacy_system);
1014         ACP_FASSIGN(default_encrypt);
1015         ACP_FASSIGN(default_encrypt_reply);
1016         ACP_FASSIGN(default_sign);
1017         ACP_FASSIGN(default_sign_reply);
1018         ACP_FASSIGN(save_encrypted_as_clear_text);
1019         ACP_FASSIGN(encrypt_to_self);
1020         
1021         /* advanced */
1022         ACP_FASSIGN(set_smtpport);
1023         ACP_FASSIGN(smtpport);
1024         ACP_FASSIGN(set_popport);
1025         ACP_FASSIGN(popport);
1026         ACP_FASSIGN(set_imapport);
1027         ACP_FASSIGN(imapport);
1028         ACP_FASSIGN(set_nntpport);
1029         ACP_FASSIGN(nntpport);
1030         ACP_FASSIGN(set_domain);
1031         ACP_FDUP(domain);
1032         ACP_FASSIGN(mark_crosspost_read);
1033         ACP_FASSIGN(crosspost_col);
1034
1035 #ifndef G_OS_WIN32
1036         ACP_FASSIGN(set_tunnelcmd);
1037         ACP_FDUP(tunnelcmd);
1038 #endif
1039
1040         ACP_FDUP(imap_dir);
1041         ACP_FASSIGN(imap_subsonly);
1042         ACP_FASSIGN(low_bandwidth);
1043
1044         ACP_FASSIGN(set_sent_folder);
1045         ACP_FDUP(sent_folder);
1046         ACP_FASSIGN(set_queue_folder);
1047         ACP_FDUP(queue_folder);
1048         ACP_FASSIGN(set_draft_folder);
1049         ACP_FDUP(draft_folder);
1050         ACP_FASSIGN(set_trash_folder);
1051         ACP_FDUP(trash_folder);
1052         /* don't want two default accounts */
1053         ac_clon->is_default = FALSE;
1054         ACP_FASSIGN(folder);
1055
1056         ACP_FASSIGN(config_version);
1057
1058         account_list = g_list_append(account_list, ac_clon);
1059         account_list_view_set();
1060 }
1061 #undef ACP_FDUP
1062 #undef ACP_FASSIGN
1063
1064 static void account_empty_cache(PrefsAccount *ac_prefs)
1065 {
1066         gchar *cache_dir;
1067
1068         cache_dir = prefs_account_cache_dir(ac_prefs, FALSE);
1069         if (cache_dir == NULL)
1070                 return; /* no cache dir, nothing to do */
1071
1072         if (is_dir_exist(cache_dir) && remove_dir_recursive(cache_dir) < 0) {
1073                 g_warning("can't remove directory '%s'", cache_dir);
1074         } else {
1075                 gchar *server_dir =  prefs_account_cache_dir(ac_prefs, TRUE);
1076                 if (g_rmdir(server_dir) == 0)
1077                         debug_print("Removed empty cache server directory\n");
1078                 else
1079                         debug_print("Cache server directory not empty: not removed\n");
1080                 g_free(server_dir);
1081         }
1082         g_free(cache_dir);
1083 }
1084
1085 static void account_delete(GtkWidget *widget, gpointer data)
1086 {
1087         PrefsAccount *ac_prefs;
1088         gchar buf[BUFFSIZE];
1089         GList *list;
1090         Folder *folder;
1091         GSList *cur;
1092
1093         ac_prefs = account_list_view_get_selected_account(edit_account.list_view);
1094         if (ac_prefs == NULL)
1095                 return;
1096
1097         g_snprintf(buf, sizeof(buf),
1098                    _("Do you really want to delete the account '%s'?"),
1099                    ac_prefs->account_name ? ac_prefs->account_name :
1100                    _("(Untitled)"));
1101         if (alertpanel_full(_("Delete account"), buf,
1102                             GTK_STOCK_CANCEL, GTK_STOCK_DELETE, NULL, ALERTFOCUS_FIRST, FALSE,
1103                             NULL, ALERT_WARNING) != G_ALERTALTERNATE)
1104                 return;
1105         account_list_dirty = TRUE;
1106
1107         if (ac_prefs->folder) {
1108                 FolderItem *item;
1109
1110                 item = mainwindow_get_mainwindow()->summaryview->folder_item;
1111                 if (item && item->folder == FOLDER(ac_prefs->folder))
1112                         summary_clear_all(mainwindow_get_mainwindow()->summaryview);
1113                 folder_destroy(FOLDER(ac_prefs->folder));
1114                 folderview_set_all();
1115         }
1116         account_destroy(ac_prefs);
1117         account_list_view_set();
1118
1119         debug_print("Removing deleted account references for all the folders...\n");
1120         list = folder_get_list();
1121         for (; list != NULL; list = list->next) {
1122                 folder = FOLDER(list->data);
1123                 if (folder->node)  /* && folder->type == F_? */
1124                         g_node_traverse(folder->node, G_PRE_ORDER,
1125                                 G_TRAVERSE_ALL, -1,
1126                                 account_delete_references_func,
1127                                 GINT_TO_POINTER(ac_prefs->account_id));
1128         }
1129
1130         gchar *uid = g_strdup_printf("%d", ac_prefs->account_id);
1131         passwd_store_delete_block(PWS_ACCOUNT, uid);
1132         g_free(uid);
1133
1134         debug_print("Removing filter rules relative to this account...\n");
1135         for(cur = filtering_rules ; cur != NULL ;) {
1136                 FilteringProp * prop = (FilteringProp *) cur->data;
1137
1138                 if (prop && (prop->account_id == ac_prefs->account_id)) {
1139                         /* get next item before we kill the current one */
1140                         cur = g_slist_next(cur);
1141
1142                         /* unallocate filteringprop and unchain it from the list */
1143                         filteringprop_free(prop);
1144                         filtering_rules = g_slist_remove(filtering_rules, prop);
1145                 } else {
1146                         cur = g_slist_next(cur);
1147                 }
1148         }
1149
1150         debug_print("Removing cache directory of this account...\n");
1151         account_empty_cache(ac_prefs);
1152
1153         folder_write_list();
1154 }
1155
1156 static void account_up(GtkWidget *widget, gpointer data)
1157 {
1158         GtkTreePath *sel = account_list_view_get_selected_account_path
1159                                 (edit_account.list_view),
1160                     *up;
1161         GtkTreeIter isel, iup;
1162         GtkTreeModel *model = gtk_tree_view_get_model
1163                                 (GTK_TREE_VIEW(edit_account.list_view));
1164         
1165         if (!sel) 
1166                 return;
1167         account_list_dirty = TRUE;
1168
1169         up = gtk_tree_path_copy(sel);
1170         if (!up) {
1171                 gtk_tree_path_free(sel);
1172                 return;
1173         }
1174
1175         if (!gtk_tree_path_prev(up)) {
1176                 gtk_tree_path_free(up);
1177                 gtk_tree_path_free(sel);
1178                 return;
1179         }
1180
1181         if (!gtk_tree_model_get_iter(model, &isel, sel)
1182         ||  !gtk_tree_model_get_iter(model, &iup,  up)) {
1183                 gtk_tree_path_free(up);
1184                 gtk_tree_path_free(sel);
1185                 return;
1186         }
1187
1188         gtk_list_store_swap(GTK_LIST_STORE(model), &isel, &iup);
1189
1190         account_list_set();
1191         
1192         gtk_tree_path_free(up);
1193         gtk_tree_path_free(sel);
1194 }
1195
1196 static void account_down(GtkWidget *widget, gpointer data)
1197 {
1198         GtkTreePath *sel = account_list_view_get_selected_account_path
1199                                 (edit_account.list_view),
1200                     *dn;
1201         GtkTreeIter isel, idn;
1202         GtkTreeModel *model = gtk_tree_view_get_model
1203                                 (GTK_TREE_VIEW(edit_account.list_view));
1204         
1205         if (!sel) 
1206                 return;
1207         account_list_dirty = TRUE;
1208
1209         dn = gtk_tree_path_copy(sel);
1210         if (!dn) {
1211                 gtk_tree_path_free(sel);
1212                 return;
1213         }
1214
1215         /* XXX no check possible??? however, if down but at bottom, then 
1216          * nothing seems to happen much anyway, so the following seems to 
1217          * be okay */
1218         gtk_tree_path_next(dn);
1219
1220         if (!gtk_tree_model_get_iter(model, &isel, sel)
1221         ||  !gtk_tree_model_get_iter(model, &idn,  dn)) {
1222                 gtk_tree_path_free(dn);
1223                 gtk_tree_path_free(sel);
1224                 return;
1225         }
1226
1227         gtk_list_store_swap(GTK_LIST_STORE(model), &isel, &idn);
1228
1229         account_list_set();
1230         
1231         gtk_tree_path_free(dn);
1232         gtk_tree_path_free(sel);
1233 }
1234
1235 static void account_set_default(GtkWidget *widget, gpointer data)
1236 {
1237         PrefsAccount *ac_prefs;
1238
1239         if (NULL == (ac_prefs = account_list_view_get_selected_account
1240                                         (edit_account.list_view))) 
1241                 return; 
1242
1243         /* we need to change the store variables by resetting everything
1244          * and setting the new default one */
1245         account_list_view_set_default_by_id(edit_account.list_view,
1246                                             ac_prefs->account_id);               
1247         
1248         account_set_as_default(ac_prefs);
1249         account_list_view_set();
1250         
1251         cur_account = ac_prefs;
1252         account_flush_state();
1253 }
1254
1255 static void account_edit_close(GtkWidget *widget, gpointer data)
1256 {
1257         account_list_set();
1258         account_write_config_all();
1259
1260         if (!cur_account && account_list) {
1261                 PrefsAccount *ac_prefs = (PrefsAccount *)account_list->data;
1262                 account_set_as_default(ac_prefs);
1263                 cur_account = ac_prefs;
1264         }
1265
1266         if (account_list_dirty)
1267                 account_flush_state();
1268
1269         gtk_widget_hide(edit_account.window);
1270         gtk_window_set_modal(GTK_WINDOW(edit_account.window), FALSE);
1271         inc_unlock();
1272 }
1273
1274 static gint account_delete_event(GtkWidget *widget, GdkEventAny *event,
1275                                  gpointer data)
1276 {
1277         account_edit_close(NULL, NULL);
1278         return TRUE;
1279 }
1280
1281 static gboolean account_key_pressed(GtkWidget *widget, GdkEventKey *event,
1282                                     gpointer data)
1283 {
1284         if (event && event->keyval == GDK_KEY_Escape)
1285                 account_edit_close(NULL, NULL);
1286         return FALSE;
1287 }
1288
1289 static gboolean account_search_func_cb (GtkTreeModel *model, gint column, const gchar *key, 
1290                                                 GtkTreeIter *iter, gpointer search_data) 
1291 {
1292         gboolean retval;
1293         PrefsAccount *ac;
1294
1295         gtk_tree_model_get (model, iter, ACCOUNT_DATA, &ac, -1);
1296
1297         if (!ac->name || !key) return FALSE;
1298
1299         retval = (strncmp (key, ac->account_name, strlen(key)) != 0);
1300
1301         debug_print("selecting row\n");
1302         account_list_view_select_account(edit_account.list_view, ac->account_id);
1303
1304         return retval;
1305 }
1306 static void account_list_view_add(PrefsAccount *ac_prefs)
1307 {
1308         GtkTreeView *list_view = GTK_TREE_VIEW(edit_account.list_view);
1309         GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model(list_view));
1310         gchar *name, *protocol, *server;
1311         gboolean has_getallbox;
1312         gboolean getall;
1313
1314         name = ac_prefs->account_name;
1315 #ifdef USE_GNUTLS
1316         protocol = ac_prefs->protocol == A_POP3 ?
1317                   (ac_prefs->ssl_pop == SSL_TUNNEL ?
1318                    "POP (SSL/TLS)" :
1319                    ac_prefs->ssl_pop == SSL_STARTTLS ?
1320                    "POP (STARTTLS)" : "POP") :
1321                    ac_prefs->protocol == A_IMAP4 ?
1322                   (ac_prefs->ssl_imap == SSL_TUNNEL ?
1323                    "IMAP (SSL/TLS)" :
1324                    ac_prefs->ssl_imap == SSL_STARTTLS ?
1325                    "IMAP (STARTTLS)" : "IMAP") :
1326                    ac_prefs->protocol == A_NNTP ?
1327                   (ac_prefs->ssl_nntp == SSL_TUNNEL ?
1328                    "NNTP (SSL/TLS)" : "NNTP") :
1329                    ac_prefs->protocol == A_LOCAL ? "Local" :
1330                    ac_prefs->protocol == A_NONE ?  "SMTP" : "-";
1331 #else
1332         protocol = ac_prefs->protocol == A_POP3  ? "POP" :
1333                    ac_prefs->protocol == A_IMAP4 ? "IMAP" :
1334                    ac_prefs->protocol == A_LOCAL ? "Local" :
1335                    ac_prefs->protocol == A_NNTP  ? "NNTP" :
1336                    ac_prefs->protocol == A_NONE ?  "SMTP" : "-";
1337 #endif
1338         server= ac_prefs->protocol == A_NNTP ? ac_prefs->nntp_server :
1339                    ac_prefs->protocol == A_LOCAL ?  "-" :
1340                    ac_prefs->protocol == A_NONE ? ac_prefs->smtp_server :
1341                    ac_prefs->recv_server;
1342
1343         has_getallbox = (ac_prefs->protocol == A_POP3  ||
1344                          ac_prefs->protocol == A_IMAP4 ||
1345                          ac_prefs->protocol == A_NNTP  ||
1346                          ac_prefs->protocol == A_LOCAL);
1347         getall = has_getallbox && ac_prefs->recv_at_getall;
1348
1349         account_list_view_insert_account_item(list_store,
1350                                              name, protocol, server,
1351                                              ac_prefs->is_default,
1352                                              getall, ac_prefs);
1353         return;
1354 }
1355
1356 static void account_list_view_set(void)
1357 {
1358         GList *cur;
1359         gint prev_sel_account;
1360         GtkListStore *store;
1361         
1362         store = GTK_LIST_STORE(gtk_tree_view_get_model
1363                 (GTK_TREE_VIEW(edit_account.list_view)));
1364
1365         prev_sel_account = account_list_view_get_selected_account_id
1366                 (edit_account.list_view); 
1367
1368         gtk_list_store_clear(store);
1369         
1370         for (cur = account_list; cur != NULL; cur = cur->next) {
1371                 account_list_view_add((PrefsAccount *)cur->data);
1372                 if ((PrefsAccount *)cur->data == cur_account)
1373                         account_list_view_select_account
1374                                 (edit_account.list_view, 
1375                                  cur_account->account_id);
1376         }
1377         
1378         if (prev_sel_account >= 0)
1379                 account_list_view_select_account(edit_account.list_view, 
1380                                                  prev_sel_account); 
1381 }
1382
1383 /* set account list from CList */
1384 static void account_list_set(void)
1385 {
1386         /* want to make sure we iterate *IN ORDER*, so therefore using
1387          * gtk_tree_model_XXXX_nth_child() */
1388         gint row, n_rows;
1389         PrefsAccount *ac_prefs;
1390         GtkTreeModel *model = gtk_tree_view_get_model
1391                                 (GTK_TREE_VIEW(edit_account.list_view));
1392         
1393         while (account_list)
1394                 account_list = g_list_remove(account_list, account_list->data);
1395
1396         n_rows = gtk_tree_model_iter_n_children(model, NULL);
1397
1398         for (row = 0; row < n_rows; row++) {
1399                 GtkTreeIter iter;
1400
1401                 if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row)) {
1402                         g_warning("%s(%d) - no iter found???", __FILE__, __LINE__);
1403                         continue;
1404                 }
1405         
1406                 ac_prefs = NULL;
1407                 gtk_tree_model_get(model, &iter,
1408                                    ACCOUNT_DATA, &ac_prefs,
1409                                    -1);
1410                 if (ac_prefs)
1411                         account_list = g_list_append(account_list, ac_prefs);
1412         }
1413 }
1414
1415 /*!
1416  *\brief        finds the PrefsAccounts which should be used to answer a mail
1417  *
1418  *\param        msginfo The message to be answered
1419  *\param        reply_autosel Indicates whether reply account autoselection is on
1420  *
1421  *\return       PrefsAccount * the correct account, NULL if not found
1422  */
1423 PrefsAccount *account_get_reply_account(MsgInfo *msginfo, gboolean reply_autosel)
1424 {
1425         PrefsAccount *account = NULL;
1426         /* select the account set in folderitem's property (if enabled) */
1427         if (msginfo->folder->prefs && msginfo->folder->prefs->enable_default_account)
1428                 account = account_find_from_id(msginfo->folder->prefs->default_account);
1429         else if (folder_has_parent_of_type(msginfo->folder, F_QUEUE) ||
1430                  folder_has_parent_of_type(msginfo->folder, F_OUTBOX) ||
1431                  folder_has_parent_of_type(msginfo->folder, F_DRAFT)) {
1432                         gchar *from = NULL;
1433                         if (!procheader_get_header_from_msginfo
1434                                 (msginfo, &from, "From:")) {
1435                                 gchar *buf = from + strlen("From:");
1436                                 extract_address(buf);
1437                                 account = account_find_from_address(buf, FALSE);
1438                         g_free(from);
1439                         }
1440         }
1441         /* select account by to: and cc: header if enabled */
1442         if (reply_autosel) {
1443                 gchar * field = NULL;
1444                 int fieldno = 0;
1445                 for (field = msginfo->to; fieldno++ < 2; field = msginfo->cc) {
1446                         if (!account && field) {
1447                                 gchar *f = g_strdup(field);
1448                                 if (f) {
1449                                         gchar *cur = f;
1450                                         gchar *next = NULL;
1451                                         gchar *to = NULL;
1452                                         do {
1453                                                 next = strchr_with_skip_quote(cur, '"', ',');
1454                                                 if (next)
1455                                                         (*next) = 0;
1456                                                 Xstrdup_a(to, cur, return NULL);
1457                                                 extract_address(to);
1458                                                 account = account_find_from_address(to, FALSE);
1459                                                 if (next)
1460                                                         cur = next + 1;
1461                                                 else
1462                                                         break;
1463                                         } while (!account);
1464                                         g_free(f);
1465                                 }
1466                         }
1467                 }
1468                 if (!account) {
1469                         gchar *deliveredto = NULL;
1470                         if (!procheader_get_header_from_msginfo
1471                                 (msginfo, &deliveredto, "Delivered-To:")) {
1472                                 gchar *buf = deliveredto + strlen("Delivered-To:");
1473                                 extract_address(buf);
1474                                 account = account_find_from_address(buf, FALSE);
1475                         g_free(deliveredto);
1476                         }
1477                 }
1478         }
1479
1480         /* select the account for the whole folder (IMAP / NNTP) */
1481         if (!account) 
1482                 /* FIXME: this is not right, because folder may be nested. we should
1483                  * ascend the tree until we find a parent with proper account 
1484                  * information */
1485                 account = msginfo->folder->folder->account;
1486
1487         /* select current account */
1488         if (!account) account = cur_account;
1489         
1490         return account;
1491 }
1492
1493 /*!
1494  *\brief        Create data store
1495  */
1496 static GtkListStore* account_create_data_store(void)
1497 {
1498         return gtk_list_store_new(N_ACCOUNT_COLUMNS,
1499                                  G_TYPE_INT,            /* ACCOUNT_IS_DEFAULT */
1500                                  G_TYPE_BOOLEAN,        /* ACCOUNT_ENABLE_GET_ALL */
1501                                  G_TYPE_STRING,         /* ACCOUNT_NAME */
1502                                  G_TYPE_STRING,         /* ACCOUNT_PROTOCOL */
1503                                  G_TYPE_STRING,         /* ACCOUNT_SERVER */
1504                                  G_TYPE_POINTER,        /* ACCOUNT_DATA */
1505                                  -1);
1506 }
1507
1508 /*!
1509  *\brief        Insert an account item in the list. 
1510  *
1511  *\return       GtkTreeRowReference * A tree row reference, which is guaranteed to 
1512  *              stable whatever operations are performed on the list.
1513  */
1514 static void account_list_view_insert_account_item(GtkListStore *list_store, 
1515                                                   const gchar *account_name,
1516                                                   const gchar *protocol, 
1517                                                   const gchar *server_name,
1518                                                   gboolean is_default, 
1519                                                   gboolean is_get_all,
1520                                                   PrefsAccount *account_data)
1521 {
1522         GtkTreeIter iter;
1523         
1524         gtk_list_store_append(list_store, &iter);
1525         gtk_list_store_set(list_store, &iter, 
1526                            ACCOUNT_IS_DEFAULT,     is_default ? PANGO_WEIGHT_BOLD : PANGO_WEIGHT_NORMAL,
1527                            ACCOUNT_ENABLE_GET_ALL, is_get_all,
1528                            ACCOUNT_NAME,           account_name,
1529                            ACCOUNT_PROTOCOL,       protocol,
1530                            ACCOUNT_SERVER,         server_name,
1531                            ACCOUNT_DATA,           account_data,
1532                            -1);
1533 }
1534
1535 /*!
1536  *\brief        Create and set up account list view, including tasks like
1537  *              creating the data store (\ref account_create_data_store()),
1538  *              and setting up the account list's individual columns (\ref 
1539  *              account_create_list_view_columns()).
1540  *
1541  *\return       GtkWidget * The created list view widget.
1542  */
1543 static GtkWidget *account_list_view_create(void)
1544 {
1545         GtkTreeView *list_view;
1546         GtkTreeSelection *selector;
1547         GtkListStore *store = account_create_data_store();
1548
1549         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(GTK_TREE_MODEL(store)));
1550         g_object_unref(G_OBJECT(store));
1551 #ifdef GENERIC_UMPC
1552         g_object_set(list_view, "allow-checkbox-mode", FALSE, NULL);
1553 #endif
1554         gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
1555         gtk_tree_view_set_reorderable(list_view, TRUE);
1556         
1557         selector = gtk_tree_view_get_selection(list_view);
1558         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
1559
1560         /* create the columns */
1561         account_create_list_view_columns(GTK_WIDGET(list_view));
1562
1563         /* set a double click listener */
1564         g_signal_connect(G_OBJECT(list_view), "row_activated",
1565                          G_CALLBACK(account_double_clicked),
1566                          list_view);
1567
1568         g_signal_connect(G_OBJECT(list_view), "drag_begin",                      
1569                          G_CALLBACK(drag_begin),
1570                          list_view);
1571                          
1572         g_signal_connect(G_OBJECT(list_view), "drag_end",                        
1573                          G_CALLBACK(drag_end),
1574                          list_view);
1575                          
1576         gtk_tree_view_set_reorderable(list_view, TRUE);
1577         return GTK_WIDGET(list_view);
1578 }
1579
1580 static void account_create_list_view_columns(GtkWidget *list_view)
1581 {
1582         GtkTreeViewColumn *column;
1583         GtkCellRenderer *renderer;
1584
1585         renderer = gtk_cell_renderer_toggle_new();
1586         g_object_set(renderer, 
1587                      "radio", FALSE, 
1588                      "activatable", TRUE,
1589                       NULL);
1590         column = gtk_tree_view_column_new_with_attributes
1591                 (C_("Accounts List Get Column Name", "G"), renderer,
1592                  "active", ACCOUNT_ENABLE_GET_ALL,
1593                  NULL);
1594         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
1595         gtk_tree_view_column_set_alignment (column, 0.5);
1596         CLAWS_SET_TIP(gtk_tree_view_column_get_widget(column),
1597                         _("'Get Mail' retrieves mail from the checked accounts"));
1598         g_signal_connect(G_OBJECT(renderer), "toggled",                      
1599                          G_CALLBACK(account_get_all_toggled),
1600                          list_view);
1601
1602         renderer = gtk_cell_renderer_text_new();
1603         column = gtk_tree_view_column_new_with_attributes
1604                 (_("Name"), renderer,
1605                  "text", ACCOUNT_NAME,
1606                  "weight", ACCOUNT_IS_DEFAULT,
1607                  NULL);
1608         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
1609         
1610         renderer = gtk_cell_renderer_text_new();
1611         column = gtk_tree_view_column_new_with_attributes
1612                 (_("Protocol"), renderer,
1613                  "text", ACCOUNT_PROTOCOL,
1614                  "weight", ACCOUNT_IS_DEFAULT,
1615                  NULL);
1616         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);           
1617         
1618         renderer = gtk_cell_renderer_text_new();
1619         column = gtk_tree_view_column_new_with_attributes
1620                 (_("Server"), renderer,
1621                  "text", ACCOUNT_SERVER,
1622                  "weight", ACCOUNT_IS_DEFAULT,
1623                  NULL);
1624         gtk_tree_view_set_search_column(GTK_TREE_VIEW(list_view), ACCOUNT_NAME);
1625         gtk_tree_view_set_search_equal_func(GTK_TREE_VIEW(list_view), account_search_func_cb , NULL, NULL);
1626         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);           
1627 }
1628
1629 /*!
1630  *\brief        Get currently selected account (by its unique ID)
1631  */
1632 static gint account_list_view_get_selected_account_id(GtkWidget *list_view)
1633 {
1634         PrefsAccount *res = NULL;
1635
1636         res = (PrefsAccount *)gtkut_tree_view_get_selected_pointer(
1637                         GTK_TREE_VIEW(list_view), ACCOUNT_DATA,
1638                         NULL, NULL, NULL);
1639
1640         return (res != NULL ? res->account_id : -1);
1641 }
1642
1643 /*!
1644  *\brief        Get the tree path of the currently selected account
1645  */
1646 static GtkTreePath *account_list_view_get_selected_account_path(GtkWidget *list_view)
1647 {
1648         GtkTreeSelection *selector;
1649         GtkTreeModel *model;
1650         GtkTreeIter iter;
1651
1652         selector = gtk_tree_view_get_selection(GTK_TREE_VIEW(list_view));
1653         
1654         if (!gtk_tree_selection_get_selected(selector, &model, &iter))
1655                 return NULL;
1656
1657         return gtk_tree_model_get_path(gtk_tree_view_get_model
1658                 (GTK_TREE_VIEW(list_view)), &iter);
1659 }
1660
1661 /*!
1662  *\brief        Get the account data of the currently selected account
1663  */
1664 static PrefsAccount *account_list_view_get_selected_account(GtkWidget *list_view)
1665 {
1666         PrefsAccount *res =
1667                 (PrefsAccount *)gtkut_tree_view_get_selected_pointer(
1668                         GTK_TREE_VIEW(list_view), ACCOUNT_DATA,
1669                         NULL, NULL, NULL);
1670
1671         return res;
1672 }
1673
1674 /*!
1675  *\brief        Select a row by the account it represents
1676  *
1677  *\return       gboolean TRUE if found and selected, FALSE if not.
1678  */
1679 static gboolean account_list_view_select_account(GtkWidget *list_view, gint account_id)
1680 {
1681         FindAccountInStore fis;
1682         GtkTreeModel *model;
1683         
1684         fis.account_id = account_id;
1685         fis.path = NULL;
1686
1687         model = gtk_tree_view_get_model(GTK_TREE_VIEW(list_view));
1688
1689         gtk_tree_model_foreach(model, (GtkTreeModelForeachFunc) find_account_in_store,
1690                                &fis);
1691                                
1692         if (fis.path) {
1693                 GtkTreeSelection *selection;
1694                 GtkTreePath* path;
1695
1696                 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(list_view));
1697                 gtk_tree_selection_select_iter(selection, &fis.iter);
1698                 path = gtk_tree_model_get_path(model, &fis.iter);
1699                 /* XXX returned path may not be valid??? create new one to be sure */ 
1700                 gtk_tree_view_set_cursor(GTK_TREE_VIEW(list_view), path, NULL, FALSE);
1701                 gtk_tree_path_free(path);
1702         }
1703
1704         return fis.path != NULL;
1705 }
1706
1707 /*!
1708  *\brief        Set a new default account by its ID. (There is only one
1709  *              default account.)
1710  */
1711 static void account_list_view_set_default_by_id(GtkWidget *list_view,
1712                                                 gint account_id)
1713 {
1714         GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(list_view));
1715         
1716         gtk_tree_model_foreach
1717                 (model, (GtkTreeModelForeachFunc) set_new_default_account,
1718                  &account_id);
1719 }
1720
1721 static gboolean set_new_default_account(GtkTreeModel *model,
1722                                         GtkTreePath  *path,
1723                                         GtkTreeIter  *iter,
1724                                         gint         *account_id)
1725 {
1726         PrefsAccount *account = NULL;
1727         PangoWeight weight;
1728         
1729         gtk_tree_model_get(model, iter, 
1730                            ACCOUNT_DATA, &account, 
1731                            -1);
1732
1733         if (*account_id == account->account_id)
1734                 weight = PANGO_WEIGHT_NORMAL;
1735         else
1736                 weight = PANGO_WEIGHT_BOLD;
1737
1738         gtk_list_store_set(GTK_LIST_STORE(model), iter, 
1739                            ACCOUNT_IS_DEFAULT, weight, -1);
1740
1741         return FALSE;
1742 }
1743                                         
1744 static gboolean find_account_in_store(GtkTreeModel *model,
1745                                       GtkTreePath  *path,
1746                                       GtkTreeIter  *iter,
1747                                       FindAccountInStore *data)
1748 {
1749         PrefsAccount *account = NULL;
1750         gtk_tree_model_get(model, iter, ACCOUNT_DATA, &account, -1);
1751
1752         if (data->account_id == account->account_id) {
1753                 data->path = path; /* signal we found it */
1754                 data->iter = *iter;
1755                 return TRUE;
1756         }
1757
1758         return FALSE; 
1759 }
1760
1761 /*!
1762  *\brief        Triggered when "get all" column is activated or de-activated
1763  */
1764 static void account_get_all_toggled(GtkCellRendererToggle *widget, 
1765                                     gchar *path, 
1766                                     GtkWidget *list_view)
1767 {
1768         GtkTreeIter iter;
1769         GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(list_view));
1770         PrefsAccount *ac = NULL;
1771         gboolean get_all;
1772         
1773         if (!gtk_tree_model_get_iter_from_string(model, &iter, path))
1774                 return;
1775
1776         gtk_tree_model_get(model, &iter, 
1777                            ACCOUNT_DATA, &ac,
1778                            ACCOUNT_ENABLE_GET_ALL, &get_all,
1779                            -1);
1780
1781         /* check if the account has a selectable get all checkbox anyway... */
1782         if (!(ac->protocol == A_POP3  || 
1783               ac->protocol == A_IMAP4 ||
1784               ac->protocol == A_NNTP  ||
1785               ac->protocol == A_LOCAL))
1786                 return;       
1787
1788         /* set value in store */
1789         gtk_list_store_set(GTK_LIST_STORE(model), &iter,
1790                            ACCOUNT_ENABLE_GET_ALL, !get_all,
1791                            -1);
1792
1793         /* set value in account */
1794         ac->recv_at_getall ^= TRUE;
1795 }
1796
1797 static void account_double_clicked(GtkTreeView          *list_view,
1798                                    GtkTreePath          *path,
1799                                    GtkTreeViewColumn    *column,
1800                                    gpointer              data)
1801 {
1802         account_edit_prefs(NULL, NULL); 
1803 }
1804
1805 static void drag_begin(GtkTreeView *list_view,
1806                       GdkDragContext *context,
1807                       gpointer data)
1808 {
1809         /* XXX unfortunately a completed drag & drop does not emit 
1810          * a "rows_reordered" signal, but a "row_changed" signal.
1811          * So during drag and drop, listen to "row_changed", and
1812          * update the account list accordingly */
1813
1814         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1815         g_signal_connect(G_OBJECT(model), "row_changed",
1816                          G_CALLBACK(account_row_changed_while_drag_drop),
1817                          list_view);
1818 }
1819
1820 static void drag_end(GtkTreeView *list_view,
1821                     GdkDragContext *context,
1822                     gpointer data)
1823 {
1824         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1825         g_signal_handlers_disconnect_by_func(G_OBJECT(model),
1826                                              G_CALLBACK(account_row_changed_while_drag_drop),
1827                                              list_view);
1828 }
1829
1830 static void account_row_changed_while_drag_drop(GtkTreeModel *model, 
1831                                    GtkTreePath  *path,
1832                                    GtkTreeIter  *iter,
1833                                    gpointer      arg3,
1834                                    GtkTreeView  *list_view)
1835 {       
1836         account_list_set();     
1837 }
1838
1839 gchar *account_get_signature_str(PrefsAccount *account)
1840 {
1841         gchar *sig_body = NULL;
1842         gchar *sig_str = NULL;
1843         gchar *utf8_sig_str = NULL;
1844
1845         cm_return_val_if_fail(account != NULL, NULL);
1846
1847         if (!account->sig_path)
1848                 return NULL;
1849
1850         if (account->sig_type == SIG_FILE) {
1851                 gchar *sig_full_path;
1852                 if (!g_path_is_absolute(account->sig_path)) {
1853                         sig_full_path = g_build_filename(get_home_dir(), account->sig_path, NULL);
1854                 } else {
1855                         sig_full_path = g_strdup(account->sig_path);
1856                 }
1857
1858                 if (!is_file_or_fifo_exist(sig_full_path)) {
1859                         g_warning("can't open signature file: '%s'", sig_full_path);
1860                         g_free(sig_full_path);
1861                         return NULL;
1862                 }
1863
1864                 debug_print("Reading signature from file '%s'\n", sig_full_path);
1865                 gchar *tmp = file_read_to_str(sig_full_path);
1866                 g_free(sig_full_path);
1867
1868                 if (!tmp)
1869                         return NULL;
1870
1871                 sig_body = normalize_newlines(tmp);
1872                 g_free(tmp);
1873         } else {
1874                 sig_body = get_command_output(account->sig_path);
1875         }
1876
1877         if (account->sig_sep) {
1878                 sig_str = g_strconcat("\n", account->sig_sep, "\n", sig_body,
1879                                       NULL);
1880                 g_free(sig_body);
1881         } else
1882                 sig_str = g_strconcat("\n", sig_body, NULL);
1883
1884         if (sig_str) {
1885                 if (g_utf8_validate(sig_str, -1, NULL) == TRUE)
1886                         utf8_sig_str = sig_str;
1887                 else {
1888                         utf8_sig_str = conv_codeset_strdup
1889                                 (sig_str, conv_get_locale_charset_str_no_utf8(),
1890                                  CS_INTERNAL);
1891                         g_free(sig_str);
1892                 }
1893         }
1894
1895         return utf8_sig_str;
1896 }
1897
1898 PrefsAccount *account_get_cur_account (void)
1899 {
1900         return cur_account;
1901 }
1902
1903 gboolean password_get(const gchar *user,
1904                     const gchar *server,
1905                     const gchar *protocol,
1906                     guint16 port,
1907                     gchar **password)
1908 {
1909         PasswordRequest req;
1910
1911         /* all have to be set */
1912         cm_return_val_if_fail(user != NULL, FALSE);
1913         cm_return_val_if_fail(server != NULL, FALSE);
1914         cm_return_val_if_fail(protocol != NULL, FALSE);
1915         cm_return_val_if_fail(port != 0, FALSE);
1916
1917         req.user = user;
1918         req.server = server;
1919         req.protocol = protocol;
1920         req.port = port;
1921
1922         if (hooks_invoke(PASSWORD_GET_HOOKLIST, &req)) {
1923                 *password = req.password;
1924                 return TRUE;
1925         }
1926         return FALSE;
1927 }