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