aboutsummaryrefslogtreecommitdiffstats
path: root/mail/mail-accounts.c
blob: 39458858debef4d3b1638ab9dd1578932736c6ec (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 *  Authors: Jeffrey Stedfast <fejj@ximian.com>
 *
 *  Copyright 2002 Ximian, Inc. (www.ximian.com)
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
 *
 */


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "mail-accounts.h"

#include <libgnomeui/gnome-stock.h>
#include <libgnomeui/gnome-messagebox.h>
#include <gal/e-table/e-table-memory-store.h>
#include <gal/e-table/e-table-scrolled.h>
#include <gal/e-table/e-cell-toggle.h>
#include <gal/util/e-unicode-i18n.h>
#include <gal/widgets/e-unicode.h>
#include <camel/camel-url.h>

#include <bonobo/bonobo-generic-factory.h>

#include "mail.h"
#include "mail-config.h"
#include "mail-config-druid.h"
#include "mail-account-editor.h"
#include "mail-send-recv.h"

#include "art/mail-new.xpm"
#include "art/mail-read.xpm"

#include "art/mark.xpm"

#define USE_ETABLE 0

static void mail_accounts_tab_class_init (MailAccountsTabClass *class);
static void mail_accounts_tab_init       (MailAccountsTab *prefs);
static void mail_accounts_tab_finalise   (GtkObject *obj);

static void mail_accounts_load (MailAccountsTab *tab);

static GdkPixbuf *disabled_pixbuf = NULL;
static GdkPixbuf *enabled_pixbuf = NULL;

static GtkVBoxClass *parent_class = NULL;


GtkType
mail_accounts_tab_get_type (void)
{
    static GtkType type = 0;
    
    if (!type) {
        GtkTypeInfo type_info = {
            "MailAccountsTab",
            sizeof (MailAccountsTab),
            sizeof (MailAccountsTabClass),
            (GtkClassInitFunc) mail_accounts_tab_class_init,
            (GtkObjectInitFunc) mail_accounts_tab_init,
            (GtkArgSetFunc) NULL,
            (GtkArgGetFunc) NULL
        };
        
        type = gtk_type_unique (gtk_vbox_get_type (), &type_info);
    }
    
    return type;
}

static void
mail_accounts_tab_class_init (MailAccountsTabClass *klass)
{
    GtkObjectClass *object_class;
    
    object_class = (GtkObjectClass *) klass;
    parent_class = gtk_type_class (gtk_vbox_get_type ());
    
    object_class->finalize = mail_accounts_tab_finalise;
    /* override methods */
    
    
    /* setup static data */
    disabled_pixbuf = gdk_pixbuf_new_from_xpm_data ((const char **) mail_new_xpm);
    enabled_pixbuf = gdk_pixbuf_new_from_xpm_data ((const char **) mail_read_xpm);
}

static void
mail_accounts_tab_init (MailAccountsTab *prefs)
{
    prefs->druid = NULL;
    prefs->editor = NULL;
}

static void
mail_accounts_tab_finalise (GtkObject *obj)
{
    MailAccountsTab *prefs = (MailAccountsTab *) obj;
    
    gtk_object_unref (GTK_OBJECT (prefs->gui));
    
        ((GtkObjectClass *)(parent_class))->finalize (obj);
}

static void
account_add_finished (GtkWidget *widget, gpointer user_data)
{
    /* Either Cancel or Finished was clicked in the druid so reload the accounts */
    MailAccountsTab *prefs = user_data;
    
    prefs->druid = NULL;
    mail_accounts_load (prefs);
}

static void
account_add_clicked (GtkButton *button, gpointer user_data)
{
    MailAccountsTab *prefs = (MailAccountsTab *) user_data;
    
    if (prefs->druid == NULL) {
        prefs->druid = (GtkWidget *) mail_config_druid_new (prefs->shell);
        gtk_signal_connect (GTK_OBJECT (prefs->druid), "destroy",
                    GTK_SIGNAL_FUNC (account_add_finished), prefs);
        
        gtk_widget_show (prefs->druid);
    } else {
        gdk_window_raise (prefs->druid->window);
    }
}

static void
account_edit_finished (GtkWidget *widget, gpointer user_data)
{
    MailAccountsTab *prefs = user_data;
    
    prefs->editor = NULL;
    mail_accounts_load (prefs);
}

static void
account_edit_clicked (GtkButton *button, gpointer user_data)
{
    MailAccountsTab *prefs = (MailAccountsTab *) user_data;
    
    if (prefs->editor == NULL) {
        int row;
#if USE_ETABLE      
        row = e_table_get_cursor_row (prefs->table);
#else
        row = prefs->table->selection ? GPOINTER_TO_INT (prefs->table->selection->data) : -1;
#endif
        if (row >= 0) {
            MailConfigAccount *account;
            GtkWidget *window;
            
            window = gtk_widget_get_ancestor (GTK_WIDGET (prefs), GTK_TYPE_WINDOW);
            
#if USE_ETABLE
            account = e_table_memory_get_data (E_TABLE_MEMORY (prefs->model), row);
#else
            account = gtk_clist_get_row_data (prefs->table, row);
#endif
            prefs->editor = (GtkWidget *) mail_account_editor_new (account, GTK_WINDOW (window), prefs);
            gtk_signal_connect (GTK_OBJECT (prefs->editor), "destroy",
                        GTK_SIGNAL_FUNC (account_edit_finished),
                        prefs);
            gtk_widget_show (prefs->editor);
        }
    } else {
        gdk_window_raise (prefs->editor->window);
    }
}

static void
account_delete_clicked (GtkButton *button, gpointer user_data)
{
    MailAccountsTab *prefs = user_data;
    const MailConfigAccount *account;
    GnomeDialog *confirm;
    const GSList *list;
    int row, ans;
    
#if USE_ETABLE
    row = e_table_get_cursor_row (prefs->table);
#else
    row = prefs->table->selection ? GPOINTER_TO_INT (prefs->table->selection->data) : -1;
#endif
    
    /* make sure we have a valid account selected and that we aren't editing anything... */
    if (row < 0 || prefs->editor != NULL)
        return;
    
    confirm = GNOME_DIALOG (gnome_message_box_new (_("Are you sure you want to delete this account?"),
                               GNOME_MESSAGE_BOX_QUESTION,
                               NULL));
    gnome_dialog_append_button_with_pixmap (confirm, _("Delete"), GNOME_STOCK_BUTTON_YES);
    gnome_dialog_append_button_with_pixmap (confirm, _("Don't delete"), GNOME_STOCK_BUTTON_NO);
    gtk_window_set_policy (GTK_WINDOW (confirm), TRUE, TRUE, TRUE);
    gtk_window_set_modal (GTK_WINDOW (confirm), TRUE);
    gtk_window_set_title (GTK_WINDOW (confirm), _("Really delete account?"));
    gnome_dialog_set_parent (confirm, GTK_WINDOW (prefs));
    ans = gnome_dialog_run_and_close (confirm);
    
    if (ans == 0) {
        int select, len;
        
#if USE_ETABLE
        account = e_table_memory_get_data (E_TABLE_MEMORY (prefs->model), row);
#else
        account = gtk_clist_get_row_data (prefs->table, row);
#endif
        
        /* remove it from the folder-tree in the shell */
        if (account->source && account->source->url && account->source->enabled)
            mail_remove_storage_by_uri (account->source->url);
        
        /* remove it from the config file */
        list = mail_config_remove_account ((MailConfigAccount *) account);
        
        mail_config_write ();
        
        mail_autoreceive_setup ();
        
#if USE_ETABLE
        e_table_memory_store_remove (E_TABLE_MEMORY_STORE (prefs->model), row);
#else
        gtk_clist_remove (prefs->table, row);
#endif      
        
        len = list ? g_slist_length ((GSList *) list) : 0;
        if (len > 0) {
            select = row >= len ? len - 1 : row;
#if USE_ETABLE
            e_table_set_cursor_row (prefs->table, select);
#else
            gtk_clist_select_row (prefs->table, select, 0);
#endif
        } else {
            gtk_widget_set_sensitive (GTK_WIDGET (prefs->mail_edit), FALSE);
            gtk_widget_set_sensitive (GTK_WIDGET (prefs->mail_delete), FALSE);
            gtk_widget_set_sensitive (GTK_WIDGET (prefs->mail_default), FALSE);
            gtk_widget_set_sensitive (GTK_WIDGET (prefs->mail_able), FALSE);
        }
    }
}

static void
account_default_clicked (GtkButton *button, gpointer user_data)
{
    MailAccountsTab *prefs = user_data;
    const MailConfigAccount *account;
    int row;
    
#if USE_ETABLE
    row = e_table_get_cursor_row (prefs->table);
#else
    row = prefs->table->selection ? GPOINTER_TO_INT (prefs->table->selection->data) : -1;
#endif
    
    if (row >= 0) {
#if USE_ETABLE
        account = e_table_memory_get_data (E_TABLE_MEMORY (prefs->model), row);
#else
        account = gtk_clist_get_row_data (prefs->table, row);
#endif
        
        mail_config_set_default_account (account);
        
        mail_config_write ();
        
        mail_accounts_load (prefs);
    }
}

static void
account_able_clicked (GtkButton *button, gpointer user_data)
{
    MailAccountsTab *prefs = user_data;
    const MailConfigAccount *account;
    int row;
    
#if USE_ETABLE
    row = e_table_get_cursor_row (prefs->table);
#else
    row = prefs->table->selection ? GPOINTER_TO_INT (prefs->table->selection->data) : -1;
#endif
    
    if (row >= 0) {
#if USE_ETABLE
        account = e_table_memory_get_data (E_TABLE_MEMORY (prefs->model), row);
#else
        account = gtk_clist_get_row_data (prefs->table, row);
#endif
        
        account->source->enabled = !account->source->enabled;
        
        if (account->source && account->source->url) {
            if (account->source->enabled)
                mail_load_storage_by_uri (prefs->shell, account->source->url, account->name);
            else
                mail_remove_storage_by_uri (account->source->url);
        }
        
        mail_autoreceive_setup ();
        
        mail_config_write ();
        
        mail_accounts_load (prefs);
    }
}

#if USE_ETABLE
static void
account_cursor_change (ETable *table, int row, gpointer user_data)
{
    MailAccountsTab *prefs = user_data;
    
    if (row >= 0) {
        const MailConfigAccount *account;
        
        account = e_table_memory_get_data (E_TABLE_MEMORY (prefs->model), row);
        if (account->source && account->source->enabled)
            gtk_label_set_text (GTK_LABEL (GTK_BIN (prefs->mail_able)->child), _("Disable"));
        else
            gtk_label_set_text (GTK_LABEL (GTK_BIN (prefs->mail_able)->child), _("Enable"));
        
        gtk_widget_set_sensitive (GTK_WIDGET (prefs->mail_edit), TRUE);
        gtk_widget_set_sensitive (GTK_WIDGET (prefs->mail_delete), TRUE);
        gtk_widget_set_sensitive (GTK_WIDGET (prefs->mail_default), TRUE);
        gtk_widget_set_sensitive (GTK_WIDGET (prefs->mail_able), TRUE);
    } else {
        gtk_widget_set_sensitive (GTK_WIDGET (prefs->mail_edit), FALSE);
        gtk_widget_set_sensitive (GTK_WIDGET (prefs->mail_delete), FALSE);
        gtk_widget_set_sensitive (GTK_WIDGET (prefs->mail_default), FALSE);
        gtk_widget_set_sensitive (GTK_WIDGET (prefs->mail_able), FALSE);
        
        gtk_widget_grab_focus (GTK_WIDGET (prefs->mail_add));
    }
}

static void
account_double_click (ETable *table, int row, int col, GdkEvent *event, gpointer user_data)
{
    account_edit_clicked (NULL, user_data);
}
#else
static void
account_cursor_change (GtkCList *table, int row, int column, GdkEvent *event, gpointer user_data)
{
    MailAccountsTab *prefs = user_data;
    
    if (row >= 0) {
        const MailConfigAccount *account;
        
        account = gtk_clist_get_row_data (prefs->table, row);
        if (account->source && account->source->enabled)
            gtk_label_set_text (GTK_LABEL (GTK_BIN (prefs->mail_able)->child), _("Disable"));
        else
            gtk_label_set_text (GTK_LABEL (GTK_BIN (prefs->mail_able)->child), _("Enable"));
        
        gtk_widget_set_sensitive (GTK_WIDGET (prefs->mail_edit), TRUE);
        gtk_widget_set_sensitive (GTK_WIDGET (prefs->mail_delete), TRUE);
        gtk_widget_set_sensitive (GTK_WIDGET (prefs->mail_default), TRUE);
        gtk_widget_set_sensitive (GTK_WIDGET (prefs->mail_able), TRUE);
        
        if (event->type == GDK_2BUTTON_PRESS)
            account_edit_clicked (NULL, user_data);
    } else {
        gtk_widget_set_sensitive (GTK_WIDGET (prefs->mail_edit), FALSE);
        gtk_widget_set_sensitive (GTK_WIDGET (prefs->mail_delete), FALSE);
        gtk_widget_set_sensitive (GTK_WIDGET (prefs->mail_default), FALSE);
        gtk_widget_set_sensitive (GTK_WIDGET (prefs->mail_able), FALSE);
        
        gtk_widget_grab_focus (GTK_WIDGET (prefs->mail_add));
    }
}
#endif


static void
mail_accounts_load (MailAccountsTab *prefs)
{
    const GSList *node;
    int row = 0;
    
#if USE_ETABLE
    e_table_memory_freeze (E_TABLE_MEMORY (prefs->model));
    
    e_table_memory_store_clear (E_TABLE_MEMORY_STORE (prefs->model));
#else
    gtk_clist_freeze (prefs->table);
    
    gtk_clist_clear (prefs->table);
#endif
    
    node = mail_config_get_accounts ();
    while (node) {
        const MailConfigAccount *account;
        CamelURL *url;
        
        account = node->data;
        
        url = account->source && account->source->url ? camel_url_new (account->source->url, NULL) : NULL;
        
#if USE_ETABLE
        e_table_memory_store_insert_list (E_TABLE_MEMORY_STORE (prefs->model),
                          row, GINT_TO_POINTER (account->source->enabled),
                          account->name,
                          url && url->protocol ? url->protocol : U_("None"));
        
        e_table_memory_set_data (E_TABLE_MEMORY (prefs->model), row, (gpointer) account);
#else
        {
            char *text[3];
            
            text[0] = NULL;
            text[1] = e_utf8_to_gtk_string (GTK_WIDGET (prefs->table), account->name);
            text[2] = url && url->protocol ? url->protocol : (char *) U_("None");
            
            gtk_clist_insert (prefs->table, row, text);
            
            g_free (text[1]);
            
            gtk_clist_set_row_data (prefs->table, row, (gpointer) account);
        }
#endif
        
        if (url)
            camel_url_free (url);
        
        node = node->next;
        row++;
    }
    
#if USE_ETABLE
    e_table_memory_thaw (E_TABLE_MEMORY (prefs->model));
#else
    gtk_clist_thaw (prefs->table);
#endif
}


GtkWidget *mail_accounts_etable_new (char *widget_name, char *string1, char *string2,
                     int int1, int int2);

#if USE_ETABLE
GtkWidget *
mail_accounts_etable_new (char *widget_name, char *string1, char *string2, int int1, int int2)
{
    ETableModel *model;
    ETableExtras *extras;
    GdkPixbuf *images[2];
    ETableMemoryStoreColumnInfo columns[] = {
        E_TABLE_MEMORY_STORE_INTEGER,
        E_TABLE_MEMORY_STORE_STRING,
        E_TABLE_MEMORY_STORE_STRING,
        E_TABLE_MEMORY_STORE_TERMINATOR,
    };
    
    extras = e_table_extras_new ();
    
    images[0] = disabled_pixbuf;   /* disabled */
    images[1] = enabled_pixbuf;    /* enabled */
    e_table_extras_add_cell (extras, "render_able", e_cell_toggle_new (0, 2, images));
    
    model = e_table_memory_store_new (columns);
    
    return e_table_scrolled_new_from_spec_file (model, extras, EVOLUTION_ETSPECDIR "/mail-accounts.etspec", NULL);
}
#else
GtkWidget *
mail_accounts_etable_new (char *widget_name, char *string1, char *string2, int int1, int int2)
{
    GtkWidget *table, *scrolled;
    char *titles[3];
    
    scrolled = gtk_scrolled_window_new (NULL, NULL);
    gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled),
                    GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
    
    titles[0] = _("Enabled");
    titles[1] = _("Account name");
    titles[2] = _("Protocol");
    table = gtk_clist_new_with_titles (3, titles);
    gtk_clist_set_selection_mode (GTK_CLIST (table), GTK_SELECTION_SINGLE);
    gtk_clist_column_titles_show (GTK_CLIST (table));
    
    gtk_container_add (GTK_CONTAINER (scrolled), table);
    
    gtk_object_set_data (GTK_OBJECT (scrolled), "table", table);
    
    gtk_widget_show (scrolled);
    gtk_widget_show (table);
    
    return scrolled;
}
#endif

static void
mail_accounts_tab_construct (MailAccountsTab *prefs)
{
    GtkWidget *toplevel, *widget;
    GladeXML *gui;
    
    gui = glade_xml_new (EVOLUTION_GLADEDIR "/mail-config.glade", "accounts_tab");
    prefs->gui = gui;
    
    /* get our toplevel widget */
    toplevel = glade_xml_get_widget (gui, "toplevel");
    
    /* reparent */
    gtk_widget_ref (toplevel);
    gtk_container_remove (GTK_CONTAINER (toplevel->parent), toplevel);
    gtk_container_add (GTK_CONTAINER (prefs), toplevel);
    gtk_widget_unref (toplevel);
    
    widget = glade_xml_get_widget (gui, "etableMailAccounts");
    
#if USE_ETABLE
    prefs->table = e_table_scrolled_get_table (E_TABLE_SCROLLED (widget));
    prefs->model = prefs->table->model;
    
    gtk_signal_connect (GTK_OBJECT (prefs->table), "cursor_change",
                account_cursor_change, prefs);
    
    gtk_signal_connect (GTK_OBJECT (prefs->table), "double_click",
                account_double_click, prefs);
    
    mail_accounts_load (prefs);
#else
    prefs->table = GTK_CLIST (gtk_object_get_data (GTK_OBJECT (widget), "table"));
    
    gtk_signal_connect (GTK_OBJECT (prefs->table), "select-row",
                account_cursor_change, prefs);
    
    mail_accounts_load (prefs);
    
    {
        int col;
        
        for (col = 0;  col < 3; col++) {
            gtk_clist_set_column_auto_resize (prefs->table, col, TRUE);
        }
    }
#endif
    
    prefs->mail_add = GTK_BUTTON (glade_xml_get_widget (gui, "cmdAccountAdd"));
    gtk_signal_connect (GTK_OBJECT (prefs->mail_add), "clicked",
                account_add_clicked, prefs);
    
    prefs->mail_edit = GTK_BUTTON (glade_xml_get_widget (gui, "cmdAccountEdit"));
    gtk_signal_connect (GTK_OBJECT (prefs->mail_edit), "clicked",
                account_edit_clicked, prefs);
    
    prefs->mail_delete = GTK_BUTTON (glade_xml_get_widget (gui, "cmdAccountDelete"));
    gtk_signal_connect (GTK_OBJECT (prefs->mail_delete), "clicked",
                account_delete_clicked, prefs);
    
    prefs->mail_default = GTK_BUTTON (glade_xml_get_widget (gui, "cmdAccountDefault"));
    gtk_signal_connect (GTK_OBJECT (prefs->mail_default), "clicked",
                account_default_clicked, prefs);
    
    prefs->mail_able = GTK_BUTTON (glade_xml_get_widget (gui, "cmdAccountAble"));
    gtk_signal_connect (GTK_OBJECT (prefs->mail_able), "clicked",
                account_able_clicked, prefs);
}


GtkWidget *
mail_accounts_tab_new (GNOME_Evolution_Shell shell)
{
    MailAccountsTab *new;
    
    new = (MailAccountsTab *) gtk_type_new (mail_accounts_tab_get_type ());
    mail_accounts_tab_construct (new);
    new->shell = shell;
    
    return (GtkWidget *) new;
}


void
mail_accounts_tab_apply (MailAccountsTab *prefs)
{
    /* nothing to do here... */
}