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