aboutsummaryrefslogtreecommitdiffstats
path: root/mail/e-mail-local.c
blob: 183635a30bb679a411b9db631dabc126cd404330 (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
/*
 * e-mail-local.c
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) version 3.
 *
 * 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>
 *
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

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

#include "e-mail-local.h"

#include <glib/gi18n.h>

#include "e-mail-folder-utils.h"

#define CHECK_LOCAL_FOLDER_TYPE(type) \
    ((type) < G_N_ELEMENTS (default_local_folders))

/* The array elements correspond to EMailLocalFolder. */
static struct {
    const gchar *display_name;
    CamelFolder *folder;
    gchar *folder_uri;
} default_local_folders[] = {
    { N_("Inbox") },
    { N_("Drafts") },
    { N_("Outbox") },
    { N_("Sent") },
    { N_("Templates") },
    { "Inbox" }  /* "always local" inbox */
};

static CamelStore *local_store;
static gboolean mail_local_initialized = FALSE;

void
e_mail_local_init (EMailSession *session,
                   const gchar *data_dir)
{
    CamelService *service;
    CamelURL *url;
    gchar *temp;
    gint ii;
    GError *error = NULL;

    if (mail_local_initialized)
        return;

    g_return_if_fail (E_IS_MAIL_SESSION (session));
    g_return_if_fail (data_dir != NULL);

    mail_local_initialized = TRUE;

    url = camel_url_new ("maildir:", NULL);
    temp = g_build_filename (data_dir, "local", NULL);
    camel_url_set_path (url, temp);
    g_free (temp);

    temp = camel_url_to_string (url, 0);
    service = camel_session_add_service (
        CAMEL_SESSION (session), "local", temp,
        CAMEL_PROVIDER_STORE, &error);
    g_free (temp);

    /* Shouldn't need to worry about other mail applications
     * altering files in our local mail store. */
    g_object_set (service, "need-summary-check", FALSE, NULL);

    if (error != NULL)
        goto fail;

    /* Populate the rest of the default_local_folders array. */
    for (ii = 0; ii < G_N_ELEMENTS (default_local_folders); ii++) {
        const gchar *display_name;

        display_name = default_local_folders[ii].display_name;

        default_local_folders[ii].folder_uri =
            e_mail_folder_uri_build (
            CAMEL_STORE (service), display_name);

        /* FIXME camel_store_get_folder() may block. */
        if (!strcmp (display_name, "Inbox"))
            default_local_folders[ii].folder =
                camel_store_get_inbox_folder_sync (
                CAMEL_STORE (service), NULL, &error);
        else
            default_local_folders[ii].folder =
                camel_store_get_folder_sync (
                CAMEL_STORE (service), display_name,
                CAMEL_STORE_FOLDER_CREATE, NULL, &error);

        if (error != NULL) {
            g_critical ("%s", error->message);
            g_clear_error (&error);
        }
    }

    camel_url_free (url);

    local_store = g_object_ref (service);

    return;

fail:
    g_critical (
        "Could not initialize local store/folder: %s",
        error->message);

    g_error_free (error);
    camel_url_free (url);
}

CamelFolder *
e_mail_local_get_folder (EMailLocalFolder type)
{
    g_return_val_if_fail (mail_local_initialized, NULL);
    g_return_val_if_fail (CHECK_LOCAL_FOLDER_TYPE (type), NULL);

    return default_local_folders[type].folder;
}

const gchar *
e_mail_local_get_folder_uri (EMailLocalFolder type)
{
    g_return_val_if_fail (mail_local_initialized, NULL);
    g_return_val_if_fail (CHECK_LOCAL_FOLDER_TYPE (type), NULL);

    return default_local_folders[type].folder_uri;
}

CamelStore *
e_mail_local_get_store (void)
{
    g_return_val_if_fail (mail_local_initialized, NULL);
    g_return_val_if_fail (CAMEL_IS_STORE (local_store), NULL);

    return local_store;
}