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