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