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