aboutsummaryrefslogtreecommitdiffstats
path: root/modules/calendar/e-task-shell-migrate.c
blob: 91da8f45ef953663dc1b286f4df2e639813cd530 (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
/*
 * e-task-shell-backend-migrate.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)
 *
 */

#include "e-task-shell-migrate.h"

#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>

#include <glib/gi18n.h>
#include <glib/gstdio.h>
#include <libebackend/e-dbhash.h>
#include <libedataserver/e-source.h>
#include <libedataserver/e-source-group.h>
#include <libedataserver/e-source-list.h>
#include <libedataserver/e-xml-hash-utils.h>
#include <libedataserver/e-xml-utils.h>

#include "e-util/e-util-private.h"
#include "calendar/gui/calendar-config.h"
#include "calendar/gui/calendar-config-keys.h"
#include "shell/e-shell.h"

#define WEBCAL_BASE_URI "webcal://"
#define PERSONAL_RELATIVE_URI "system"

static void
create_task_sources (EShellBackend *shell_backend,
             ESourceList *source_list,
             ESourceGroup **on_this_computer,
             ESourceGroup **on_the_web,
             ESource **personal_source)
{
    EShell *shell;
    EShellSettings *shell_settings;
    GSList *groups;
    ESourceGroup *group;
    gchar *base_uri, *base_uri_proto;
    const gchar *base_dir;

    *on_this_computer = NULL;
    *on_the_web = NULL;
    *personal_source = NULL;

    shell = e_shell_backend_get_shell (shell_backend);
    shell_settings = e_shell_get_shell_settings (shell);

    base_dir = e_shell_backend_get_data_dir (shell_backend);
    base_uri = g_build_filename (base_dir, "local", NULL);

    base_uri_proto = g_filename_to_uri (base_uri, NULL, NULL);

    groups = e_source_list_peek_groups (source_list);
    if (groups) {
        /* groups are already there, we need to search for things... */
        GSList *g;

        for (g = groups; g; g = g->next) {

            group = E_SOURCE_GROUP (g->data);

            if (!*on_this_computer && !strcmp (base_uri_proto,
                e_source_group_peek_base_uri (group)))
                *on_this_computer = g_object_ref (group);
            else if (!*on_the_web && !strcmp (WEBCAL_BASE_URI,
                e_source_group_peek_base_uri (group)))
                *on_the_web = g_object_ref (group);
        }
    }

    if (*on_this_computer) {
        /* make sure "Personal" shows up as a source under
           this group */
        GSList *sources = e_source_group_peek_sources (*on_this_computer);
        GSList *s;
        for (s = sources; s; s = s->next) {
            ESource *source = E_SOURCE (s->data);
            const gchar *relative_uri;

            relative_uri = e_source_peek_relative_uri (source);
            if (relative_uri == NULL)
                continue;
            if (!strcmp (PERSONAL_RELATIVE_URI, relative_uri)) {
                *personal_source = g_object_ref (source);
                break;
            }
        }
    } else {
        /* create the local source group */
        group = e_source_group_new (_("On This Computer"), base_uri_proto);
        e_source_list_add_group (source_list, group, -1);

        *on_this_computer = group;
    }

    if (!*personal_source) {
        gchar *primary_task_list;

        /* Create the default Person task list */
        ESource *source = e_source_new (_("Personal"), PERSONAL_RELATIVE_URI);
        e_source_group_add_source (*on_this_computer, source, -1);

        primary_task_list = e_shell_settings_get_string (
            shell_settings, "cal-primary-task-list");

        if (!primary_task_list && !calendar_config_get_tasks_selected ()) {
            GSList selected;

            e_shell_settings_set_string (
                shell_settings, "cal-primary-task-list",
                e_source_peek_uid (source));

            selected.data = (gpointer)e_source_peek_uid (source);
            selected.next = NULL;
            calendar_config_set_tasks_selected (&selected);
        }

        e_source_set_color_spec (source, "#BECEDD");
        *personal_source = source;
    }

    if (!*on_the_web) {
        /* Create the Webcal source group */
        group = e_source_group_new (_("On The Web"), WEBCAL_BASE_URI);
        e_source_list_add_group (source_list, group, -1);

        *on_the_web = group;
    }

    g_free (base_uri_proto);
    g_free (base_uri);
}

gboolean
e_task_shell_backend_migrate (EShellBackend *shell_backend,
                             gint major,
                             gint minor,
                             gint micro,
                             GError **error)
{
    ESourceGroup *on_this_computer = NULL;
    ESourceGroup *on_the_web = NULL;
    ESource *personal_source = NULL;
    ESourceList *source_list;

    g_object_get (shell_backend, "source-list", &source_list, NULL);

    /* we call this unconditionally now - create_groups either
       creates the groups/sources or it finds the necessary
       groups/sources. */
    create_task_sources (
        shell_backend, source_list, &on_this_computer,
        &on_the_web, &personal_source);

    e_source_list_sync (source_list, NULL);

    if (on_this_computer)
        g_object_unref (on_this_computer);
    if (on_the_web)
        g_object_unref (on_the_web);
    if (personal_source)
        g_object_unref (personal_source);

    return TRUE;
}