aboutsummaryrefslogtreecommitdiffstats
path: root/mail/mail-config.c
blob: 47a2392767db265fbd97b0207305403de092e2bd (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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 *  Authors: Jeffrey Stedfast <fejj@helixcode.com>
 *
 *  Copyright 2001 Helix Code, Inc. (www.helixcode.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.
 *
 */

#include <config.h>
#include <pwd.h>
#include <ctype.h>

#include <gnome.h>
#include <gtkhtml/gtkhtml.h>
#include <glade/glade.h>

#include <gal/util/e-util.h>
#include "e-util/e-html-utils.h"
#include "mail.h"
#include "mail-config.h"
#include "mail-ops.h"

typedef struct {
    gboolean thread_list;
    gboolean view_source;
    gint paned_size;
    gboolean send_html;
    gint seen_timeout;
    
    GSList *accounts;
    GSList *news;
} MailConfig;

static const char GCONFPATH[] = "/apps/Evolution/Mail";
static MailConfig *config = NULL;

/* Prototypes */
static void config_read (void);

/* Identity */
MailConfigIdentity *
identity_copy (const MailConfigIdentity *id)
{
    MailConfigIdentity *new;
    
    g_return_val_if_fail (id != NULL, NULL);
    
    new = g_new0 (MailConfigIdentity, 1);
    new->name = g_strdup (id->name);
    new->address = g_strdup (id->address);
    new->reply_to = g_strdup (id->reply_to);
    new->organization = g_strdup (id->organization);
    new->signature = g_strdup (id->signature);
    
    return new;
}

void
identity_destroy (MailConfigIdentity *id)
{
    if (!id)
        return;
    
    g_free (id->name);
    g_free (id->address);
    g_free (id->reply_to);
    g_free (id->organization);
    g_free (id->signature);
    
    g_free (id);
}

/* Service */
MailConfigService *
service_copy (const MailConfigService *source) 
{
    MailConfigService *new;
    
    g_return_val_if_fail (source != NULL, NULL);
    
    new = g_new0 (MailConfigService, 1);
    new->url = g_strdup (source->url);
    new->keep_on_server = source->keep_on_server;
    new->save_passwd = source->save_passwd;
    new->use_ssl = source->use_ssl;
    
    return new;
}

void
service_destroy (MailConfigService *source)
{
    if (!source)
        return;
    
    g_free (source->url);
    
    g_free (source);
}

void
service_destroy_each (gpointer item, gpointer data)
{
    service_destroy ((MailConfigService *)item);
}

/* Account */
MailConfigAccount *
account_copy (const MailConfigAccount *account) 
{
    MailConfigAccount *new;
    
    g_return_val_if_fail (account != NULL, NULL);
    
    new = g_new0 (MailConfigAccount, 1);
    new->name = g_strdup (account->name);
    new->default_account = account->default_account;
    
    new->id = identity_copy (account->id);
    new->source = service_copy (account->source);
    new->transport = service_copy (account->transport);
    
    return new;
}

void
account_destroy (MailConfigAccount *account)
{
    if (!account)
        return;
    
    g_free (account->name);
    
    identity_destroy (account->id);
    service_destroy (account->source);
    service_destroy (account->transport);
    
    g_free (account);
}

void
account_destroy_each (gpointer item, gpointer data)
{
    account_destroy ((MailConfigAccount *)item);
}

/* Config struct routines */
void
mail_config_init (void)
{
    if (config)
        return;
    
    config = g_new0 (MailConfig, 1);
    config_read ();
}

void
mail_config_clear (void)
{
    if (!config)
        return;
    
    if (config->accounts) {
        g_slist_foreach (config->accounts, account_destroy_each, NULL);
        g_slist_free (config->accounts);
        config->accounts = NULL;
    }
    
    if (config->news) {
        g_slist_foreach (config->news, service_destroy_each, NULL);
        g_slist_free (config->news);
        config->news = NULL;
    }
    
    /* overkill? */
    memset (config, 0, sizeof (MailConfig));
}

static void
config_read (void)
{
    gchar *str;
    gint len, i;
    gboolean have_default = FALSE;
    
    mail_config_clear ();
    
    /* Accounts */
    str = g_strdup_printf ("=%s/config/Mail=/Accounts/", evolution_dir);
    gnome_config_push_prefix (str);
    g_free (str);
    
    len = gnome_config_get_int ("num");
    for (i = 0; i < len; i++) {
        MailConfigAccount *account;
        MailConfigIdentity *id;
        MailConfigService *source;
        MailConfigService *transport;
        gchar *path;
        
        account = g_new0 (MailConfigAccount, 1);
        path = g_strdup_printf ("account_name_%d", i);
        account->name = gnome_config_get_string (path);
        g_free (path);
        path = g_strdup_printf ("account_is_default_%d", i);
        account->default_account = gnome_config_get_bool (path) && !have_default;
        if (account->default_account)
            have_default = TRUE;
        g_free (path);
        
        /* get the identity info */
        id = g_new0 (MailConfigIdentity, 1);        
        path = g_strdup_printf ("identity_name_%d", i);
        id->name = gnome_config_get_string (path);
        g_free (path);
        path = g_strdup_printf ("identity_replyto_%d", i);
        id->reply_to = gnome_config_get_string (path);
        g_free (path);
        path = g_strdup_printf ("identity_address_%d", i);
        id->address = gnome_config_get_string (path);
        g_free (path);
        path = g_strdup_printf ("identity_organization_%d", i);
        id->organization = gnome_config_get_string (path);
        g_free (path);
        path = g_strdup_printf ("identity_signature_%d", i);
        id->signature = gnome_config_get_string (path);
        g_free (path);
        
        /* get the source */
        source = g_new0 (MailConfigService, 1);
        path = g_strdup_printf ("source_url_%d", i);
        source->url = gnome_config_get_string (path);
        g_free (path);
        
        if (!*source->url) {
            /* no source associated with this account */
            g_free (source->url);
            source->url = NULL;
        }
        
        path = g_strdup_printf ("source_keep_on_server_%d", i);
        source->keep_on_server = gnome_config_get_bool (path);
        g_free (path);
        path = g_strdup_printf ("source_save_passwd_%d", i);
        source->save_passwd = gnome_config_get_bool (path);
        g_free (path);
        path = g_strdup_printf ("source_use_ssl_%d", i);
        source->use_ssl = gnome_config_get_bool (path);
        g_free (path);
        
        /* get the transport */
        transport = g_new0 (MailConfigService, 1);
        path = g_strdup_printf ("transport_url_%d", i);
        transport->url = gnome_config_get_string (path);
        g_free (path);
        
        if (!*transport->url) {
            /* no transport associated with this account */
            g_free (transport->url);
            transport->url = NULL;
        }
        
        path = g_strdup_printf ("transport_use_ssl_%d", i);
        transport->use_ssl = gnome_config_get_bool (path);
        g_free (path);
        
        account->id = id;
        account->source = source;
        account->transport = transport;
        
        config->accounts = g_slist_append (config->accounts, account);
    }
    gnome_config_pop_prefix ();
    
    /* News */
    str = g_strdup_printf ("=%s/config/News=/Sources/", evolution_dir);
    gnome_config_push_prefix (str);
    g_free (str);
    
    len = gnome_config_get_int ("num");
    for (i = 0; i < len; i++) {
        MailConfigService *n;
        gchar *path;
        
        n = g_new0 (MailConfigService, 1);
        
        path = g_strdup_printf ("url_%d", i);
        n->url = gnome_config_get_string (path);
        g_free (path);
        
        config->news = g_slist_append (config->news, n);
    }
    gnome_config_pop_prefix ();
    
    /* Format */
    str = g_strdup_printf ("=%s/config/Mail=/Format/send_html", 
                   evolution_dir);
    config->send_html = gnome_config_get_bool (str);
    g_free (str);
    
    /* Mark as seen timeout */
    str = g_strdup_printf ("=%s/config/Mail=/Display/seen_timeout=1500", 
                   evolution_dir);
    config->seen_timeout = gnome_config_get_int (str);
    g_free (str);
    
    /* Show Messages Threaded */
    str = g_strdup_printf ("=%s/config/Mail=/Display/thread_list", 
                   evolution_dir);
    config->thread_list = gnome_config_get_bool (str);
    g_free (str);
    
    /* Size of vpaned in mail view */
    str = g_strdup_printf ("=%s/config/Mail=/Display/paned_size=200", 
                   evolution_dir);
    config->paned_size = gnome_config_get_int (str);
    g_free (str);
    
    gnome_config_sync ();
}

void
mail_config_write (void)
{
    gchar *str;
    gint len, i;
    
    /* Accounts */
    str = g_strdup_printf ("=%s/config/Mail=/Accounts/", evolution_dir);
    gnome_config_push_prefix (str);
    g_free (str);
    
    len = g_slist_length (config->accounts);
    gnome_config_set_int ("num", len);
    for (i = 0; i < len; i++) {
        MailConfigAccount *account;
        gchar *path;
        
        account = g_slist_nth_data (config->accounts, i);
        
        /* account info */
        path = g_strdup_printf ("account_name_%d", i);
        gnome_config_set_string (path, account->name);
        g_free (path);
        path = g_strdup_printf ("account_is_default_%d", i);
        gnome_config_set_bool (path, account->default_account);
        g_free (path);
        
        /* identity info */
        path = g_strdup_printf ("identity_name_%d", i);
        gnome_config_set_string (path, account->id->name);
        g_free (path);
        path = g_strdup_printf ("identity_address_%d", i);
        gnome_config_set_string (path, account->id->address);
        g_free (path);
        path = g_strdup_printf ("identity_organization_%d", i);
        gnome_config_set_string (path, account->id->organization);
        g_free (path);
        path = g_strdup_printf ("identity_signature_%d", i);
        gnome_config_set_string (path, account->id->signature);
        g_free (path);
        
        /* source info */
        path = g_strdup_printf ("source_url_%d", i);
        gnome_config_set_string (path, account->source->url ? account->source->url : "");
        g_free (path);
        path = g_strdup_printf ("source_keep_on_server_%d", i);
        gnome_config_set_bool (path, account->source->keep_on_server);
        g_free (path);
        path = g_strdup_printf ("source_save_passwd_%d", i);
        gnome_config_set_bool (path, account->source->save_passwd);
        g_free (path);
        
        /* transport info */
        path = g_strdup_printf ("transport_url_%d", i);
        gnome_config_set_string (path, account->transport->url ? account->transport->url : "");
        g_free (path);
    }
    gnome_config_pop_prefix ();
    
    /* News */
    str = g_strdup_printf ("=%s/config/News=/Sources/", evolution_dir);
    gnome_config_push_prefix (str);
    g_free (str);
    
    len = g_slist_length (config->news);
    gnome_config_set_int ("num", len);
    for (i = 0; i < len; i++) {
        MailConfigService *n;
        gchar *path;
        
        n = g_slist_nth_data (config->news, i);
        
        path = g_strdup_printf ("url_%d", i);
        gnome_config_set_string (path, n->url);
        g_free (path);
    }
    gnome_config_pop_prefix ();
    
    /* Mark as seen timeout */
    str = g_strdup_printf ("=%s/config/Mail=/Display/seen_timeout", 
                   evolution_dir);
    gnome_config_set_int (str, config->seen_timeout);
    g_free (str);
    
    /* Format */
    str = g_strdup_printf ("=%s/config/Mail=/Format/send_html", 
                   evolution_dir);
    gnome_config_set_bool (str, config->send_html);
    g_free (str);
    
    gnome_config_sync ();
}

void
mail_config_write_on_exit (void)
{
    gchar *str;
    GSList *sources;
    MailConfigService *s;
    
    /* Show Messages Threaded */
    str = g_strdup_printf ("=%s/config/Mail=/Display/thread_list", 
                   evolution_dir);
    gnome_config_set_bool (str, config->thread_list);
    g_free (str);
    
    /* Size of vpaned in mail view */
    str = g_strdup_printf ("=%s/config/Mail=/Display/paned_size", 
                   evolution_dir);
    gnome_config_set_int (str, config->paned_size);
    g_free (str);
    
    /* Passwords */
    gnome_config_private_clean_section ("/Evolution/Passwords");
    sources = mail_config_get_sources ();
    for ( ; sources; sources = sources->next) {
        s = sources->data;
        if (s->save_passwd)
            mail_session_remember_password (s->url);
    }
    g_slist_free (sources);
    
    gnome_config_sync ();
}

/* Accessor functions */
gboolean
mail_config_is_configured (void)
{
    return config->accounts != NULL;
}

gboolean
mail_config_get_thread_list (void)
{
    return config->thread_list;
}

void
mail_config_set_thread_list (gboolean value)
{
    config->thread_list = value;
}

gboolean
mail_config_get_view_source (void)
{
    return config->view_source;
}

void
mail_config_set_view_source (gboolean value)
{
    config->view_source = value;
}

gint
mail_config_get_paned_size (void)
{
    return config->paned_size;
}

void
mail_config_set_paned_size (gint value)
{
    config->paned_size = value;
}

gboolean
mail_config_get_send_html (void)
{
    return config->send_html;
}

void
mail_config_set_send_html (gboolean send_html)
{
    config->send_html = send_html;
}

gint
mail_config_get_mark_as_seen_timeout (void)
{
    return config->seen_timeout;
}

void
mail_config_set_mark_as_seen_timeout (gint timeout)
{
    config->seen_timeout = timeout;
}

const MailConfigAccount *
mail_config_get_default_account (void)
{
    const MailConfigAccount *account;
    GSList *l;
    
    if (!config->accounts)
        return NULL;
    
    /* find the default account */
    l = config->accounts;
    while (l) {
        account = l->data;
        if (account->default_account)
            return account;
        
        l = l->next;
    }
    
    /* none are marked as default so mark the first one as the default */
    account = config->accounts->data;
    mail_config_set_default_account (account);
    
    return account;
}

const MailConfigAccount *
mail_config_get_account_by_name (const char *account_name)
{
    /* FIXME: this should really use a hash */
    const MailConfigAccount *account;
    GSList *l;
    
    l = config->accounts;
    while (l) {
        account = l->data;
        if (account && !strcmp (account->name, account_name))
            return account;
        
        l = l->next;
    }
    
    return NULL;
}

const GSList *
mail_config_get_accounts (void)
{
    return config->accounts;
}

void
mail_config_add_account (MailConfigAccount *account)
{
    if (account->default_account) {
        /* Un-defaultify other accounts */
        GSList *node = config->accounts;
        
        while (node) {
            MailConfigAccount *acnt = node->data;
            
            acnt->default_account = FALSE;
            
            node = node->next;
        }
    }
    
    config->accounts = g_slist_append (config->accounts, account);
}

const GSList *
mail_config_remove_account (MailConfigAccount *account)
{
    config->accounts = g_slist_remove (config->accounts, account);
    account_destroy (account);
    
    return config->accounts;
}

void
mail_config_set_default_account (const MailConfigAccount *account)
{
    GSList *node = config->accounts;
    
    while (node) {
        MailConfigAccount *acnt = node->data;
        
        acnt->default_account = FALSE;
        
        node = node->next;
    }
    
    ((MailConfigAccount *) account)->default_account = TRUE;
}

const MailConfigIdentity *
mail_config_get_default_identity (void)
{
    const MailConfigAccount *account;
    
    account = mail_config_get_default_account ();
    if (account)
        return account->id;
    else
        return NULL;
}

const MailConfigService *
mail_config_get_default_transport (void)
{
    const MailConfigAccount *account;
    
    account = mail_config_get_default_account ();
    if (account)
        return account->transport;
    else
        return NULL;
}

const MailConfigService *
mail_config_get_default_news (void)
{
    if (!config->news)
        return NULL;
    
    return (MailConfigService *)config->news->data;
}

const GSList *
mail_config_get_news (void)
{
    return config->news;
}

void
mail_config_add_news (MailConfigService *news) 
{
    config->news = g_slist_append (config->news, news);
}

const GSList *
mail_config_remove_news (MailConfigService *news)
{
    config->news = g_slist_remove (config->news, news);
    service_destroy (news);
    
    return config->news;
}

GSList *
mail_config_get_sources (void)
{
    const GSList *accounts;
    GSList *sources = NULL;
    
    accounts = mail_config_get_accounts ();
    while (accounts) {
        const MailConfigAccount *account = accounts->data;
        
        if (account->source)
            sources = g_slist_append (sources, account->source);
        
        accounts = accounts->next;
    }
    
    return sources;
}

char *
mail_config_folder_to_cachename (CamelFolder *folder, const char *prefix)
{
    char *url, *filename;
    
    url = camel_url_to_string (CAMEL_SERVICE (folder->parent_store)->url, FALSE);
    e_filename_make_safe (url);
    
    filename = g_strdup_printf ("%s/config/%s%s", evolution_dir, prefix, url);
    g_free (url);
    
    return filename;
}


/* Async service-checking/authtype-lookup code. */

typedef struct {
    char *url;
    CamelProviderType type;
    GList **authtypes;
    gboolean success;
} check_service_input_t;

static char *
describe_check_service (gpointer in_data, gboolean gerund)
{
    if (gerund)
        return g_strdup (_("Connecting to server"));
    else
        return g_strdup (_("Connect to server"));
}

static void
do_check_service (gpointer in_data, gpointer op_data, CamelException *ex)
{
    check_service_input_t *input = in_data;
    CamelService *service;
    
    if (input->authtypes) {
        service = camel_session_get_service (
            session, input->url, input->type, ex);
        if (!service)
            return;
        *input->authtypes = camel_service_query_auth_types (service, ex);
    } else {
        service = camel_session_get_service_connected (
            session, input->url, input->type, ex);
    }
    if (service)
        camel_object_unref (CAMEL_OBJECT (service));
    if (!camel_exception_is_set (ex))
        input->success = TRUE;
}

static const mail_operation_spec op_check_service = {
    describe_check_service,
    0,
    NULL,
    do_check_service,
    NULL
};

gboolean
mail_config_check_service (CamelURL *url, CamelProviderType type, GList **authtypes)
{
    check_service_input_t input;
    
    input.url = camel_url_to_string (url, TRUE);
    input.type = type;
    input.authtypes = authtypes;
    input.success = FALSE;
    
    mail_operation_queue (&op_check_service, &input, FALSE);
    mail_operation_wait_for_finish ();
    g_free (input.url);
    
    return input.success;
}