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