fixed local account
[claws.git] / src / account.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2002 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 <gtk/gtk.h>
28 #include <gdk/gdkkeysyms.h>
29 #include <stdio.h>
30 #include <errno.h>
31
32 #include "intl.h"
33 #include "main.h"
34 #include "mainwindow.h"
35 #include "folderview.h"
36 #include "folder.h"
37 #include "account.h"
38 #include "prefs.h"
39 #include "prefs_account.h"
40 #include "prefs_folder_item.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
49 typedef enum
50 {
51         COL_DEFAULT     = 0,
52         COL_GETALL      = 1,
53         COL_NAME        = 2,
54         COL_PROTOCOL    = 3,
55         COL_SERVER      = 4
56 } EditAccountColumnPos;
57
58 # define N_EDIT_ACCOUNT_COLS    5
59
60 #define PREFSBUFSIZE            1024
61
62 PrefsAccount *cur_account;
63
64 static GList *account_list = NULL;
65
66 static struct EditAccount {
67         GtkWidget *window;
68         GtkWidget *clist;
69         GtkWidget *close_btn;
70 } edit_account;
71
72 static GdkPixmap *markxpm;
73 static GdkBitmap *markxpmmask;
74 static GdkPixmap *checkboxonxpm;
75 static GdkPixmap *checkboxonxpmmask;
76 static GdkPixmap *checkboxoffxpm;
77 static GdkPixmap *checkboxoffxpmmask;
78
79 static void account_edit_create         (void);
80
81 static void account_edit_prefs          (void);
82 static void account_delete              (void);
83
84 static void account_up                  (void);
85 static void account_down                (void);
86
87 static void account_set_default         (void);
88
89 static void account_edit_close          (void);
90 static gint account_delete_event        (GtkWidget      *widget,
91                                          GdkEventAny    *event,
92                                          gpointer        data);
93 static void account_selected            (GtkCList       *clist,
94                                          gint            row,
95                                          gint            column,
96                                          GdkEvent       *event,
97                                          gpointer        data);
98 static void account_row_moved           (GtkCList       *clist,
99                                          gint            source_row,
100                                          gint            dest_row);
101 static void account_key_pressed         (GtkWidget      *widget,
102                                          GdkEventKey    *event,
103                                          gpointer        data);
104
105 static gint account_clist_set_row       (PrefsAccount   *ac_prefs,
106                                          gint            row);
107 static void account_clist_set           (void);
108
109 static void account_list_set            (void);
110
111 void account_read_config_all(void)
112 {
113         GSList *ac_label_list = NULL, *cur;
114         gchar *rcpath;
115         FILE *fp;
116         gchar buf[PREFSBUFSIZE];
117         PrefsAccount *ac_prefs;
118
119         debug_print(_("Reading all config for each account...\n"));
120
121         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACCOUNT_RC, NULL);
122         if ((fp = fopen(rcpath, "r")) == NULL) {
123                 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
124                 g_free(rcpath);
125                 return;
126         }
127         g_free(rcpath);
128
129         while (fgets(buf, sizeof(buf), fp) != NULL) {
130                 if (!strncmp(buf, "[Account: ", 10)) {
131                         strretchomp(buf);
132                         memmove(buf, buf + 1, strlen(buf));
133                         buf[strlen(buf) - 1] = '\0';
134                         debug_print(_("Found label: %s\n"), buf);
135                         ac_label_list = g_slist_append(ac_label_list,
136                                                        g_strdup(buf));
137                 }
138         }
139         fclose(fp);
140
141         /* read config data from file */
142         cur_account = NULL;
143         for (cur = ac_label_list; cur != NULL; cur = cur->next) {
144                 ac_prefs = g_new0(PrefsAccount, 1);
145                 prefs_account_read_config(ac_prefs, (gchar *)cur->data);
146                 account_list = g_list_append(account_list, ac_prefs);
147                 if (ac_prefs->is_default)
148                         cur_account = ac_prefs;
149         }
150         /* if default is not set, assume first account as default */
151         if (!cur_account && account_list) {
152                 ac_prefs = (PrefsAccount *)account_list->data;
153                 account_set_as_default(ac_prefs);
154                 cur_account = ac_prefs;
155         }
156
157         account_set_menu();
158         main_window_reflect_prefs_all();
159
160         while (ac_label_list) {
161                 g_free(ac_label_list->data);
162                 ac_label_list = g_slist_remove(ac_label_list,
163                                                ac_label_list->data);
164         }
165 }
166
167 void account_save_config_all(void)
168 {
169         prefs_account_save_config_all(account_list);
170 }
171
172 PrefsAccount *account_find_from_smtp_server(const gchar *address,
173                                             const gchar *smtp_server)
174 {
175         GList *cur;
176         PrefsAccount *ac;
177
178         g_return_val_if_fail(address != NULL, NULL);
179         g_return_val_if_fail(smtp_server != NULL, NULL);
180
181         for (cur = account_list; cur != NULL; cur = cur->next) {
182                 ac = (PrefsAccount *)cur->data;
183                 if (!strcmp2(address, ac->address) &&
184                     !strcmp2(smtp_server, ac->smtp_server))
185                         return ac;
186         }
187
188         return NULL;
189 }
190
191 /*
192  * account_find_from_address:
193  * @address: Email address string.
194  *
195  * Find a mail (not news) account with the specified email address.
196  *
197  * Return value: The found account, or NULL if not found.
198  */
199 PrefsAccount *account_find_from_address(const gchar *address)
200 {
201         GList *cur;
202         PrefsAccount *ac;
203
204         g_return_val_if_fail(address != NULL, NULL);
205
206         for (cur = account_list; cur != NULL; cur = cur->next) {
207                 ac = (PrefsAccount *)cur->data;
208                 if (ac->protocol != A_NNTP && ac->address &&
209                     strcasestr(address, ac->address))
210                         return ac;
211         }
212
213         return NULL;
214 }
215
216 PrefsAccount *account_find_from_id(gint id)
217 {
218         GList *cur;
219         PrefsAccount *ac;
220
221         for (cur = account_list; cur != NULL; cur = cur->next) {
222                 ac = (PrefsAccount *)cur->data;
223                 if (id == ac->account_id)
224                         return ac;
225         }
226
227         return NULL;
228 }
229
230 void account_set_menu(void)
231 {
232         main_window_set_account_menu(account_list);
233 }
234
235 void account_foreach(AccountFunc func, gpointer user_data)
236 {
237         GList *cur;
238
239         for (cur = account_list; cur != NULL; cur = cur->next)
240                 if (func((PrefsAccount *)cur->data, user_data) != 0)
241                         return;
242 }
243
244 GList *account_get_list(void)
245 {
246         return account_list;
247 }
248
249 void account_edit_open(void)
250 {
251         inc_lock();
252
253         if (compose_get_compose_list()) {
254                 alertpanel_notice(_("Some composing windows are open.\n"
255                                     "Please close all the composing windows before editing the accounts."));
256                 inc_unlock();
257                 return;
258         }
259
260         debug_print(_("Opening account edit window...\n"));
261
262         if (!edit_account.window)
263                 account_edit_create();
264
265         account_clist_set();
266
267         manage_window_set_transient(GTK_WINDOW(edit_account.window));
268         gtk_widget_grab_focus(edit_account.close_btn);
269         gtk_widget_show(edit_account.window);
270
271         manage_window_focus_in(edit_account.window, NULL, NULL);
272 }
273
274 void account_add(void)
275 {
276         PrefsAccount *ac_prefs;
277
278         ac_prefs = prefs_account_open(NULL);
279
280         if (!ac_prefs) return;
281
282         account_list = g_list_append(account_list, ac_prefs);
283
284         if (ac_prefs->is_default)
285                 account_set_as_default(ac_prefs);
286
287         account_clist_set();
288
289         if (ac_prefs->protocol == A_IMAP4 || ac_prefs->protocol == A_NNTP) {
290                 Folder *folder;
291
292                 if (ac_prefs->protocol == A_IMAP4) {
293                         folder = folder_new(F_IMAP, ac_prefs->account_name,
294                                             ac_prefs->recv_server);
295                 } else {
296                         folder = folder_new(F_NEWS, ac_prefs->account_name,
297                                             ac_prefs->nntp_server);
298                 }
299
300                 folder->account = ac_prefs;
301                 ac_prefs->folder = REMOTE_FOLDER(folder);
302                 folder_add(folder);
303                 if (ac_prefs->protocol == A_IMAP4)
304                         folder->create_tree(folder);
305                 folderview_set_all();
306         }
307 }
308
309 void account_set_as_default(PrefsAccount *ac_prefs)
310 {
311         PrefsAccount *ap;
312         GList *cur;
313
314         for (cur = account_list; cur != NULL; cur = cur->next) {
315                 ap = (PrefsAccount *)cur->data;
316                 if (ap->is_default)
317                         ap->is_default = FALSE;
318         }
319
320         ac_prefs->is_default = TRUE;
321 }
322
323 PrefsAccount *account_get_default(void)
324 {
325         PrefsAccount *ap;
326         GList *cur;
327
328         for (cur = account_list; cur != NULL; cur = cur->next) {
329                 ap = (PrefsAccount *)cur->data;
330                 if (ap->is_default)
331                         return ap;
332         }
333
334         return NULL;
335 }
336
337 void account_set_missing_folder(void)
338 {
339         PrefsAccount *ap;
340         GList *cur;
341
342         for (cur = account_list; cur != NULL; cur = cur->next) {
343                 ap = (PrefsAccount *)cur->data;
344                 if ((ap->protocol == A_IMAP4 || ap->protocol == A_NNTP) &&
345                     !ap->folder) {
346                         Folder *folder;
347
348                         if (ap->protocol == A_IMAP4) {
349                                 folder = folder_new(F_IMAP, ap->account_name,
350                                                     ap->recv_server);
351                         } else {
352                                 folder = folder_new(F_NEWS, ap->account_name,
353                                                     ap->nntp_server);
354                         }
355
356                         folder->account = ap;
357                         ap->folder = REMOTE_FOLDER(folder);
358                         folder_add(folder);
359                         if (ap->protocol == A_IMAP4)
360                                 folder->create_tree(folder);
361                 }
362         }
363 }
364
365 void account_destroy(PrefsAccount *ac_prefs)
366 {
367         g_return_if_fail(ac_prefs != NULL);
368
369         prefs_account_free(ac_prefs);
370         account_list = g_list_remove(account_list, ac_prefs);
371
372         if (cur_account == ac_prefs) cur_account = NULL;
373         if (!cur_account && account_list) {
374                 cur_account = account_get_default();
375                 if (!cur_account) {
376                         ac_prefs = (PrefsAccount *)account_list->data;
377                         account_set_as_default(ac_prefs);
378                         cur_account = ac_prefs;
379                 }
380         }
381 }
382
383
384 static void account_edit_create(void)
385 {
386         GtkWidget *window;
387         GtkWidget *vbox;
388         GtkWidget *label;
389         GtkWidget *hbox;
390         GtkWidget *scrolledwin;
391         GtkWidget *clist;
392         gchar *titles[N_EDIT_ACCOUNT_COLS];
393         gint i;
394
395         GtkWidget *vbox2;
396         GtkWidget *add_btn;
397         GtkWidget *edit_btn;
398         GtkWidget *del_btn;
399         GtkWidget *up_btn;
400         GtkWidget *down_btn;
401
402         GtkWidget *default_btn;
403
404         GtkWidget *hbbox;
405         GtkWidget *close_btn;
406
407         debug_print(_("Creating account edit window...\n"));
408
409         window = gtk_window_new (GTK_WINDOW_DIALOG);
410         gtk_widget_set_usize (window, 500, 320);
411         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
412         gtk_window_set_title (GTK_WINDOW (window), _("Edit accounts"));
413         gtk_window_set_modal (GTK_WINDOW (window), TRUE);
414         gtk_signal_connect (GTK_OBJECT (window), "delete_event",
415                             GTK_SIGNAL_FUNC (account_delete_event), NULL);
416         gtk_signal_connect (GTK_OBJECT (window), "key_press_event",
417                             GTK_SIGNAL_FUNC (account_key_pressed), NULL);
418         MANAGE_WINDOW_SIGNALS_CONNECT (window);
419         gtk_widget_realize(window);
420
421         vbox = gtk_vbox_new (FALSE, 10);
422         gtk_widget_show (vbox);
423         gtk_container_add (GTK_CONTAINER (window), vbox);
424
425         hbox = gtk_hbox_new (FALSE, 0);
426         gtk_widget_show (hbox);
427         gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
428
429         label = gtk_label_new
430                 (_("New messages will be checked in this order. Check the boxes\n"
431                    "on the `G' column to enable message retrieval by `Get all'."));
432         gtk_widget_show (label);
433         gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 4);
434         gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
435
436         hbox = gtk_hbox_new (FALSE, 8);
437         gtk_widget_show (hbox);
438         gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
439         gtk_container_set_border_width (GTK_CONTAINER (hbox), 2);
440
441         scrolledwin = gtk_scrolled_window_new (NULL, NULL);
442         gtk_widget_show (scrolledwin);
443         gtk_box_pack_start (GTK_BOX (hbox), scrolledwin, TRUE, TRUE, 0);
444         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwin),
445                                         GTK_POLICY_AUTOMATIC,
446                                         GTK_POLICY_AUTOMATIC);
447
448         titles[COL_DEFAULT]  = "D";
449         titles[COL_GETALL]   = "G";
450         titles[COL_NAME]     = _("Name");
451         titles[COL_PROTOCOL] = _("Protocol");
452         titles[COL_SERVER]   = _("Server");
453
454         clist = gtk_clist_new_with_titles (N_EDIT_ACCOUNT_COLS, titles);
455         gtk_widget_show (clist);
456         gtk_container_add (GTK_CONTAINER (scrolledwin), clist);
457         gtk_clist_set_column_width (GTK_CLIST(clist), COL_DEFAULT , 10);
458         gtk_clist_set_column_width (GTK_CLIST(clist), COL_GETALL  , 11);
459         gtk_clist_set_column_width (GTK_CLIST(clist), COL_NAME    , 100);
460         gtk_clist_set_column_width (GTK_CLIST(clist), COL_PROTOCOL, 100);
461         gtk_clist_set_column_width (GTK_CLIST(clist), COL_SERVER  , 100);
462         gtk_clist_set_column_justification (GTK_CLIST(clist), COL_DEFAULT,
463                                             GTK_JUSTIFY_CENTER);
464         gtk_clist_set_column_justification (GTK_CLIST(clist), COL_GETALL,
465                                             GTK_JUSTIFY_CENTER);
466         gtk_clist_set_selection_mode (GTK_CLIST(clist), GTK_SELECTION_BROWSE);
467
468         for (i = 0; i < N_EDIT_ACCOUNT_COLS; i++)
469                 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(clist)->column[i].button,
470                                        GTK_CAN_FOCUS);
471
472         gtk_signal_connect (GTK_OBJECT (clist), "select_row",
473                             GTK_SIGNAL_FUNC (account_selected), NULL);
474         gtk_signal_connect (GTK_OBJECT (clist), "row_move",
475                             GTK_SIGNAL_FUNC (account_row_moved), NULL);
476
477         vbox2 = gtk_vbox_new (FALSE, 0);
478         gtk_widget_show (vbox2);
479         gtk_box_pack_start (GTK_BOX (hbox), vbox2, FALSE, FALSE, 0);
480
481         add_btn = gtk_button_new_with_label (_("Add"));
482         gtk_widget_show (add_btn);
483         gtk_box_pack_start (GTK_BOX (vbox2), add_btn, FALSE, FALSE, 4);
484         gtk_signal_connect (GTK_OBJECT(add_btn), "clicked",
485                             GTK_SIGNAL_FUNC (account_add), NULL);
486
487         edit_btn = gtk_button_new_with_label (_("Edit"));
488         gtk_widget_show (edit_btn);
489         gtk_box_pack_start (GTK_BOX (vbox2), edit_btn, FALSE, FALSE, 4);
490         gtk_signal_connect (GTK_OBJECT(edit_btn), "clicked",
491                             GTK_SIGNAL_FUNC (account_edit_prefs), NULL);
492
493         del_btn = gtk_button_new_with_label (_(" Delete "));
494         gtk_widget_show (del_btn);
495         gtk_box_pack_start (GTK_BOX (vbox2), del_btn, FALSE, FALSE, 4);
496         gtk_signal_connect (GTK_OBJECT(del_btn), "clicked",
497                             GTK_SIGNAL_FUNC (account_delete), NULL);
498
499         down_btn = gtk_button_new_with_label (_("Down"));
500         gtk_widget_show (down_btn);
501         gtk_box_pack_end (GTK_BOX (vbox2), down_btn, FALSE, FALSE, 4);
502         gtk_signal_connect (GTK_OBJECT(down_btn), "clicked",
503                             GTK_SIGNAL_FUNC (account_down), NULL);
504
505         up_btn = gtk_button_new_with_label (_("Up"));
506         gtk_widget_show (up_btn);
507         gtk_box_pack_end (GTK_BOX (vbox2), up_btn, FALSE, FALSE, 4);
508         gtk_signal_connect (GTK_OBJECT(up_btn), "clicked",
509                             GTK_SIGNAL_FUNC (account_up), NULL);
510
511         hbox = gtk_hbox_new (FALSE, 8);
512         gtk_widget_show (hbox);
513         gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
514
515         vbox2 = gtk_vbox_new(FALSE, 0);
516         gtk_widget_show (vbox2);
517         gtk_box_pack_start (GTK_BOX (hbox), vbox2, FALSE, FALSE, 0);
518
519         default_btn = gtk_button_new_with_label (_(" Set as default account "));
520         gtk_widget_show (default_btn);
521         gtk_box_pack_start (GTK_BOX (vbox2), default_btn, TRUE, FALSE, 0);
522         gtk_signal_connect (GTK_OBJECT(default_btn), "clicked",
523                             GTK_SIGNAL_FUNC (account_set_default), NULL);
524
525         gtkut_button_set_create(&hbbox, &close_btn, _("Close"),
526                                 NULL, NULL, NULL, NULL);
527         gtk_widget_show(hbbox);
528         gtk_box_pack_end (GTK_BOX (hbox), hbbox, FALSE, FALSE, 0);
529         gtk_widget_grab_default (close_btn);
530
531         gtk_signal_connect (GTK_OBJECT (close_btn), "clicked",
532                             GTK_SIGNAL_FUNC (account_edit_close),
533                             NULL);
534
535         stock_pixmap_gdk(clist, STOCK_PIXMAP_MARK, &markxpm, &markxpmmask);
536         stock_pixmap_gdk(clist, STOCK_PIXMAP_CHECKBOX_ON,
537                          &checkboxonxpm, &checkboxonxpmmask);
538         stock_pixmap_gdk(clist, STOCK_PIXMAP_CHECKBOX_OFF,
539                          &checkboxoffxpm, &checkboxoffxpmmask);
540
541         edit_account.window    = window;
542         edit_account.clist     = clist;
543         edit_account.close_btn = close_btn;
544 }
545
546 static void account_edit_prefs(void)
547 {
548         GtkCList *clist = GTK_CLIST(edit_account.clist);
549         PrefsAccount *ac_prefs;
550         gint row;
551         gboolean prev_default;
552         gchar *ac_name;
553
554         if (!clist->selection) return;
555
556         row = GPOINTER_TO_INT(clist->selection->data);
557         ac_prefs = gtk_clist_get_row_data(clist, row);
558         prev_default = ac_prefs->is_default;
559         Xstrdup_a(ac_name, ac_prefs->account_name, return);
560
561         prefs_account_open(ac_prefs);
562
563         if (!prev_default && ac_prefs->is_default)
564                 account_set_as_default(ac_prefs);
565
566         if ((ac_prefs->protocol == A_IMAP4 || ac_prefs->protocol == A_NNTP) &&
567             ac_prefs->folder && strcmp(ac_name, ac_prefs->account_name) != 0) {
568                 folder_set_name(FOLDER(ac_prefs->folder),
569                                 ac_prefs->account_name);
570                 folderview_rescan_all();
571         }
572
573         account_clist_set();
574 }
575
576 static gboolean account_delete_references_func(GNode *node, gpointer data)
577 {
578         FolderItem *item;
579         gint account;
580
581         g_return_val_if_fail(node->data != NULL, FALSE);
582
583         item = FOLDER_ITEM(node->data);
584         account = GPOINTER_TO_INT(data);
585
586         if(!item->prefs) /* && item->prefs->stype == F_NORMAL */
587                 return FALSE;
588         if(item->prefs->default_account != account)
589                 return FALSE;
590         
591         item->prefs->enable_default_account = FALSE;
592         item->prefs->default_account = 0;
593         prefs_folder_item_save_config(item);
594 }
595
596 static void account_delete(void)
597 {
598         GtkCList *clist = GTK_CLIST(edit_account.clist);
599         PrefsAccount *ac_prefs;
600         gint row;
601         GList *list;
602         Folder *folder;
603         
604         if (!clist->selection) return;
605
606         if (alertpanel(_("Delete account"),
607                        _("Do you really want to delete this account?"),
608                        _("Yes"), _("+No"), NULL) != G_ALERTDEFAULT)
609                 return;
610
611         row = GPOINTER_TO_INT(clist->selection->data);
612         ac_prefs = gtk_clist_get_row_data(clist, row);
613         if (ac_prefs->folder) {
614                 folder_destroy(FOLDER(ac_prefs->folder));
615                 folderview_rescan_all();
616         }
617         account_destroy(ac_prefs);
618         account_clist_set();
619
620         debug_print(_("Removing deleted account references for all the folders...\n"));
621         list = folder_get_list();
622         for (; list != NULL; list = list->next) {
623                 folder = FOLDER(list->data);
624                 if (folder->node)  /* && folder->type == F_? */
625                         g_node_traverse(folder->node, G_PRE_ORDER,
626                                 G_TRAVERSE_ALL, -1,
627                                 account_delete_references_func,
628                                 GINT_TO_POINTER(ac_prefs->account_id));
629         }
630 }
631
632 static void account_up(void)
633 {
634         GtkCList *clist = GTK_CLIST(edit_account.clist);
635         gint row;
636
637         if (!clist->selection) return;
638
639         row = GPOINTER_TO_INT(clist->selection->data);
640         if (row > 0)
641                 gtk_clist_row_move(clist, row, row - 1);
642 }
643
644 static void account_down(void)
645 {
646         GtkCList *clist = GTK_CLIST(edit_account.clist);
647         gint row;
648
649         if (!clist->selection) return;
650
651         row = GPOINTER_TO_INT(clist->selection->data);
652         if (row < clist->rows - 1)
653                 gtk_clist_row_move(clist, row, row + 1);
654 }
655
656 static void account_set_default(void)
657 {
658         GtkCList *clist = GTK_CLIST(edit_account.clist);
659         gint row;
660         PrefsAccount *ac_prefs;
661
662         if (!clist->selection) return;
663
664         row = GPOINTER_TO_INT(clist->selection->data);
665         ac_prefs = gtk_clist_get_row_data(clist, row);
666         account_set_as_default(ac_prefs);
667         account_clist_set();
668
669         cur_account = ac_prefs;
670         account_set_menu();
671         main_window_reflect_prefs_all();
672 }
673
674 static void account_edit_close(void)
675 {
676         account_list_set();
677         account_save_config_all();
678
679         if (!cur_account && account_list) {
680                 PrefsAccount *ac_prefs = (PrefsAccount *)account_list->data;
681                 account_set_as_default(ac_prefs);
682                 cur_account = ac_prefs;
683         }
684
685         account_set_menu();
686         main_window_reflect_prefs_all();
687
688         gtk_widget_hide(edit_account.window);
689
690         inc_unlock();
691 }
692
693 static gint account_delete_event(GtkWidget *widget, GdkEventAny *event,
694                                  gpointer data)
695 {
696         account_edit_close();
697         return TRUE;
698 }
699
700 static void account_selected(GtkCList *clist, gint row, gint column,
701                              GdkEvent *event, gpointer data)
702 {
703         if (event && event->type == GDK_2BUTTON_PRESS)
704                 account_edit_prefs();
705
706         if (column == COL_GETALL) {
707                 PrefsAccount *ac;
708
709                 ac = gtk_clist_get_row_data(clist, row);
710                 if (ac->protocol == A_POP3 || ac->protocol == A_APOP ||
711                     ac->protocol == A_IMAP4 || ac->protocol == A_NNTP ||
712                     ac->protocol == A_LOCAL) {
713                         ac->recv_at_getall ^= TRUE;
714                         account_clist_set_row(ac, row);
715                 }
716         }
717 }
718
719 static void account_row_moved(GtkCList *clist, gint source_row, gint dest_row)
720 {
721         account_list_set();
722         if (gtk_clist_row_is_visible(clist, dest_row) != GTK_VISIBILITY_FULL) {
723                 gtk_clist_moveto(clist, dest_row, -1,
724                                  source_row < dest_row ? 1.0 : 0.0, 0.0);
725         }
726 }
727
728 static void account_key_pressed(GtkWidget *widget, GdkEventKey *event,
729                                 gpointer data)
730 {
731         if (event && event->keyval == GDK_Escape)
732                 account_edit_close();
733 }
734
735 /* set one CList row or add new row */
736 static gint account_clist_set_row(PrefsAccount *ac_prefs, gint row)
737 {
738         GtkCList *clist = GTK_CLIST(edit_account.clist);
739         gchar *text[N_EDIT_ACCOUNT_COLS];
740         gboolean has_getallbox;
741         gboolean getall;
742
743         text[COL_DEFAULT] = "";
744         text[COL_GETALL]  = "";
745         text[COL_NAME]    = ac_prefs->account_name;
746 #if USE_SSL
747         text[COL_PROTOCOL] = ac_prefs->protocol == A_POP3 ?
748                              (ac_prefs->ssl_pop ? "POP3 (SSL)" : "POP3") :
749                              ac_prefs->protocol == A_APOP ?
750                              (ac_prefs->ssl_pop ?
751                               "POP3 (APOP, SSL)" : "POP3 (APOP)") :
752                              ac_prefs->protocol == A_IMAP4 ?
753                              (ac_prefs->ssl_imap ? "IMAP4 (SSL)" : "IMAP4") :
754                              ac_prefs->protocol == A_LOCAL ? "Local" :
755                              ac_prefs->protocol == A_NNTP ? "NNTP"  :  "";
756 #else
757         text[COL_PROTOCOL] = ac_prefs->protocol == A_POP3  ? "POP3" :
758                              ac_prefs->protocol == A_APOP  ? "POP3 (APOP)" :
759                              ac_prefs->protocol == A_IMAP4 ? "IMAP4" :
760                              ac_prefs->protocol == A_LOCAL ? "Local" :
761                              ac_prefs->protocol == A_NNTP  ? "NNTP" : "";
762 #endif
763         text[COL_SERVER] = ac_prefs->protocol == A_NNTP
764                 ? ac_prefs->nntp_server : ac_prefs->recv_server;
765
766         if (row < 0)
767                 row = gtk_clist_append(clist, text);
768         else {
769                 gtk_clist_set_text(clist, row, COL_DEFAULT, text[COL_DEFAULT]);
770                 gtk_clist_set_text(clist, row, COL_GETALL, text[COL_GETALL]);
771                 gtk_clist_set_text(clist, row, COL_NAME, text[COL_NAME]);
772                 gtk_clist_set_text(clist, row, COL_PROTOCOL, text[COL_PROTOCOL]);
773                 gtk_clist_set_text(clist, row, COL_SERVER, text[COL_SERVER]);
774         }
775
776         has_getallbox = (ac_prefs->protocol == A_POP3  ||
777                          ac_prefs->protocol == A_APOP  ||
778                          ac_prefs->protocol == A_IMAP4 ||
779                          ac_prefs->protocol == A_NNTP ||
780                          ac_prefs->protocol == A_LOCAL);
781         getall = has_getallbox && ac_prefs->recv_at_getall;
782
783         if (ac_prefs->is_default)
784                 gtk_clist_set_pixmap(clist, row, COL_DEFAULT,
785                                      markxpm, markxpmmask);
786         if (getall)
787                 gtk_clist_set_pixmap(clist, row, COL_GETALL,
788                                      checkboxonxpm, checkboxonxpmmask);
789         else if (has_getallbox)
790                 gtk_clist_set_pixmap(clist, row, COL_GETALL,
791                                      checkboxoffxpm, checkboxoffxpmmask);
792
793         gtk_clist_set_row_data(clist, row, ac_prefs);
794
795         return row;
796 }
797
798 /* set CList from account list */
799 static void account_clist_set(void)
800 {
801         GtkCList *clist = GTK_CLIST(edit_account.clist);
802         GList *cur;
803         gint row = -1, prev_row = -1;
804
805         if (clist->selection)
806                 prev_row = GPOINTER_TO_INT(clist->selection->data);
807
808         gtk_clist_freeze(clist);
809         gtk_clist_clear(clist);
810
811         for (cur = account_list; cur != NULL; cur = cur->next) {
812                 row = account_clist_set_row((PrefsAccount *)cur->data, -1);
813                 if ((PrefsAccount *)cur->data == cur_account) {
814                         gtk_clist_select_row(clist, row, -1);
815                         gtkut_clist_set_focus_row(clist, row);
816                 }
817         }
818
819         if (prev_row >= 0) {
820                 row = prev_row;
821                 gtk_clist_select_row(clist, row, -1);
822                 gtkut_clist_set_focus_row(clist, row);
823         }
824
825         if (row >= 0 &&
826             gtk_clist_row_is_visible(clist, row) != GTK_VISIBILITY_FULL)
827                 gtk_clist_moveto(clist, row, -1, 0.5, 0);
828
829         gtk_clist_thaw(clist);
830 }
831
832 /* set account list from CList */
833 static void account_list_set(void)
834 {
835         GtkCList *clist = GTK_CLIST(edit_account.clist);
836         gint row;
837         PrefsAccount *ac_prefs;
838
839         while (account_list)
840                 account_list = g_list_remove(account_list, account_list->data);
841
842         for (row = 0; (ac_prefs = gtk_clist_get_row_data(clist, row)) != NULL;
843              row++)
844                 account_list = g_list_append(account_list, ac_prefs);
845 }