~ubuntu-branches/ubuntu/lucid/libopensync-plugin-evolution2/lucid

« back to all changes in this revision

Viewing changes to src/evolution2_etodo.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Banck
  • Date: 2006-04-13 20:42:22 UTC
  • Revision ID: james.westby@ubuntu.com-20060413204222-0ez2561jp27ez35q
Tags: upstream-0.18
ImportĀ upstreamĀ versionĀ 0.18

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * evolution2_sync - A plugin for the opensync framework
 
3
 * Copyright (C) 2004-2005  Armin Bauer <armin.bauer@opensync.org>
 
4
 * 
 
5
 * This library is free software; you can redistribute it and/or
 
6
 * modify it under the terms of the GNU Lesser General Public
 
7
 * License as published by the Free Software Foundation; either
 
8
 * version 2.1 of the License, or (at your option) any later version.
 
9
 * 
 
10
 * This library 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 GNU
 
13
 * Lesser General Public License for more details.
 
14
 * 
 
15
 * You should have received a copy of the GNU Lesser General Public
 
16
 * License along with this library; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 
18
 * 
 
19
 */
 
20
 
 
21
#include "evolution2_sync.h"
 
22
 
 
23
osync_bool evo2_todo_open(evo_environment *env, OSyncError **error)
 
24
{
 
25
        osync_trace(TRACE_ENTRY, "%s(%p)", __func__, env);
 
26
        ESourceList *sources = NULL;
 
27
        ESource *source = NULL;
 
28
        GError *gerror = NULL;
 
29
        
 
30
        if (strcmp(env->tasks_path, "default")) {
 
31
                if (!e_cal_get_sources(&sources, E_CAL_SOURCE_TYPE_TODO, &gerror)) {
 
32
                        osync_error_set(error, OSYNC_ERROR_GENERIC, "Unable to get sources for tasks: %s", gerror ? gerror->message : "None");
 
33
                        osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(error));
 
34
                        g_clear_error(&gerror);
 
35
                        return FALSE;
 
36
                }
 
37
                
 
38
                source = evo2_find_source(sources, env->tasks_path);
 
39
                if (!source) {
 
40
                        osync_error_set(error, OSYNC_ERROR_GENERIC, "Unable to find source for tasks");
 
41
                        osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(error));
 
42
                        return FALSE;
 
43
                }
 
44
                
 
45
                env->tasks = e_cal_new(source, E_CAL_SOURCE_TYPE_TODO);
 
46
                if(!env->tasks) {
 
47
                        osync_error_set(error, OSYNC_ERROR_GENERIC, "Failed to create new tasks");
 
48
                        osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(error));
 
49
                        return FALSE;
 
50
                }
 
51
                
 
52
                if(!e_cal_open(env->tasks, FALSE, &gerror)) {
 
53
                        osync_error_set(error, OSYNC_ERROR_GENERIC, "Failed to open tasks: %s", gerror ? gerror->message : "None");
 
54
                        g_object_unref(env->tasks);
 
55
                        env->tasks = NULL;
 
56
                        osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(error));
 
57
                        g_clear_error(&gerror);
 
58
                        return FALSE;
 
59
                }
 
60
        } else {
 
61
                if (!e_cal_open_default (&env->tasks, E_CAL_SOURCE_TYPE_TODO, NULL, NULL, &gerror)) {
 
62
                        osync_error_set(error, OSYNC_ERROR_GENERIC, "Failed to open default tasks: %s", gerror ? gerror->message : "None");
 
63
                        env->calendar = NULL;
 
64
                        osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(error));
 
65
                        g_clear_error(&gerror);
 
66
                        return FALSE;
 
67
                }
 
68
        }
 
69
        
 
70
        if (!osync_anchor_compare(env->member, "todo", env->tasks_path))
 
71
                osync_member_set_slow_sync(env->member, "todo", TRUE);
 
72
        
 
73
        osync_trace(TRACE_EXIT, "%s", __func__);
 
74
        return TRUE;
 
75
}
 
76
 
 
77
void evo2_todo_get_changes(OSyncContext *ctx)
 
78
{
 
79
        osync_trace(TRACE_ENTRY, "%s(%p)", __func__, ctx);
 
80
        evo_environment *env = (evo_environment *)osync_context_get_plugin_data(ctx);
 
81
        
 
82
        GList *changes = NULL;
 
83
        GList *l = NULL;
 
84
        char *data = NULL;
 
85
        const char *uid = NULL;
 
86
        int datasize = 0;
 
87
        GError *gerror = NULL;
 
88
        
 
89
        if (osync_member_get_slow_sync(env->member, "todo") == FALSE) {
 
90
                osync_debug("EVO2-SYNC", 4, "No slow_sync for todo");
 
91
                if (!e_cal_get_changes(env->tasks, env->change_id, &changes, &gerror)) {
 
92
                        osync_context_send_log(ctx, "Unable to open changed tasks entries");
 
93
                        osync_trace(TRACE_EXIT_ERROR, "%s: Unable to open changed tasks entries: %s", __func__, gerror ? gerror->message : "None");
 
94
                        g_clear_error(&gerror);
 
95
                        return;
 
96
                }
 
97
                
 
98
                for (l = changes; l; l = l->next) {
 
99
                        ECalChange *ecc = (ECalChange *)l->data;
 
100
                        e_cal_component_get_uid(ecc->comp, &uid);
 
101
                        e_cal_component_commit_sequence (ecc->comp);
 
102
                        e_cal_component_strip_errors(ecc->comp);
 
103
                        switch (ecc->type) {
 
104
                                case E_CAL_CHANGE_ADDED:
 
105
                                        data = e_cal_get_component_as_string(env->calendar, e_cal_component_get_icalcomponent(ecc->comp));
 
106
                                        datasize = strlen(data) + 1;
 
107
                                        evo2_report_change(ctx, "todo", "vtodo20", data, datasize, uid, CHANGE_ADDED);
 
108
                                        break;
 
109
                                case E_CAL_CHANGE_MODIFIED:
 
110
                                        data = e_cal_get_component_as_string(env->calendar, e_cal_component_get_icalcomponent(ecc->comp));
 
111
                                        datasize = strlen(data) + 1;
 
112
                                        evo2_report_change(ctx, "todo", "vtodo20", data, datasize, uid, CHANGE_MODIFIED);
 
113
                                        break;
 
114
                                case E_CAL_CHANGE_DELETED:
 
115
                                        evo2_report_change(ctx, "todo", "vtodo20", NULL, 0, uid, CHANGE_DELETED);
 
116
                                        break;
 
117
                        }
 
118
                }
 
119
        } else {
 
120
                osync_debug("EVO2-SYNC", 4, "slow_sync for todo");
 
121
        if (!e_cal_get_object_list_as_comp (env->tasks, "(contains? \"any\" \"\")", &changes, &gerror)) {
 
122
                        osync_context_send_log(ctx, "Unable to get all todos");
 
123
                        osync_trace(TRACE_EXIT_ERROR, "%s: Unable to get all todos: %s", __func__, gerror ? gerror->message : "None");
 
124
                        g_clear_error(&gerror);
 
125
                        return;
 
126
        }
 
127
                for (l = changes; l; l = l->next) {
 
128
                        ECalComponent *comp = E_CAL_COMPONENT (l->data);
 
129
                        char *data = e_cal_get_component_as_string(env->tasks, e_cal_component_get_icalcomponent(comp));
 
130
                        const char *uid = NULL;
 
131
                        e_cal_component_get_uid(comp, &uid);
 
132
                        int datasize = strlen(data) + 1;
 
133
                        evo2_report_change(ctx, "todo", "vtodo20", data, datasize, uid, CHANGE_ADDED);
 
134
                        g_object_unref (comp);
 
135
                }
 
136
        }
 
137
        osync_trace(TRACE_EXIT, "%s", __func__);
 
138
}
 
139
 
 
140
 
 
141
static osync_bool evo2_todo_modify(OSyncContext *ctx, OSyncChange *change)
 
142
{
 
143
        osync_trace(TRACE_ENTRY, "%s(%p, %p)", __func__, ctx, change);
 
144
        evo_environment *env = (evo_environment *)osync_context_get_plugin_data(ctx);
 
145
        
 
146
        const char *uid = osync_change_get_uid(change);
 
147
        char *data = osync_change_get_data(change);
 
148
        icalcomponent *icomp = NULL;
 
149
        char *returnuid = NULL;
 
150
        GError *gerror = NULL;
 
151
        
 
152
        switch (osync_change_get_changetype(change)) {
 
153
                case CHANGE_DELETED:
 
154
                        if (!e_cal_remove_object(env->tasks, uid, &gerror)) {
 
155
                                osync_context_report_error(ctx, OSYNC_ERROR_GENERIC, "Unable to delete todo: %s", gerror ? gerror->message : "None");
 
156
                                osync_trace(TRACE_EXIT_ERROR, "%s: Unable to delete todo: %s", __func__, gerror ? gerror->message : "None");
 
157
                                g_clear_error(&gerror);
 
158
                                return FALSE;
 
159
                        }
 
160
                        break;
 
161
                case CHANGE_ADDED:
 
162
                        icomp = icalcomponent_new_from_string(data);
 
163
                        if (!icomp) {
 
164
                                osync_context_report_error(ctx, OSYNC_ERROR_GENERIC, "Unable to convert todo");
 
165
                                osync_trace(TRACE_EXIT_ERROR, "%s: Unable to convert todo", __func__);
 
166
                                return FALSE;
 
167
                        }
 
168
                        
 
169
                        icomp = icalcomponent_get_first_component (icomp, ICAL_VTODO_COMPONENT);
 
170
                        if (!icomp) {
 
171
                                osync_context_report_error(ctx, OSYNC_ERROR_GENERIC, "Unable to get todo");
 
172
                                osync_trace(TRACE_EXIT_ERROR, "%s: Unable to get todo", __func__);
 
173
                                return FALSE;
 
174
                        }
 
175
                        
 
176
                        if (!e_cal_create_object(env->tasks, icomp, &returnuid, &gerror)) {
 
177
                                osync_context_report_error(ctx, OSYNC_ERROR_GENERIC, "Unable to create todo: %s", gerror ? gerror->message : "None");
 
178
                                osync_trace(TRACE_EXIT_ERROR, "%s: Unable to create todo: %s", __func__, gerror ? gerror->message : "None");
 
179
                                g_clear_error(&gerror);
 
180
                                return FALSE;
 
181
                        }
 
182
                        osync_change_set_uid(change, returnuid);
 
183
                        break;
 
184
                case CHANGE_MODIFIED:
 
185
                        icomp = icalcomponent_new_from_string(data);
 
186
                        if (!icomp) {
 
187
                                osync_context_report_error(ctx, OSYNC_ERROR_GENERIC, "Unable to convert todo2");
 
188
                                osync_trace(TRACE_EXIT_ERROR, "%s: Unable to convert todo2", __func__);
 
189
                                return FALSE;
 
190
                        }
 
191
                        
 
192
                        icomp = icalcomponent_get_first_component (icomp, ICAL_VTODO_COMPONENT);
 
193
                        if (!icomp) {
 
194
                                osync_context_report_error(ctx, OSYNC_ERROR_GENERIC, "Unable to get todo2");
 
195
                                osync_trace(TRACE_EXIT_ERROR, "%s: Unable to get todo2", __func__);
 
196
                                return FALSE;
 
197
                        }
 
198
                        
 
199
                        icalcomponent_set_uid (icomp, uid);
 
200
                        if (!e_cal_modify_object(env->tasks, icomp, CALOBJ_MOD_ALL, &gerror)) {
 
201
                                osync_context_report_error(ctx, OSYNC_ERROR_GENERIC, "Unable to modify todo: %s", gerror ? gerror->message : "None");
 
202
                                osync_trace(TRACE_EXIT_ERROR, "%s: Unable to modify todo: %s", __func__, gerror ? gerror->message : "None");
 
203
                                g_clear_error(&gerror);
 
204
                                return FALSE;
 
205
                        }
 
206
                        break;
 
207
                default:
 
208
                        printf("Error\n");
 
209
        }
 
210
        
 
211
        osync_context_report_success(ctx);
 
212
        osync_trace(TRACE_EXIT, "%s", __func__);
 
213
        return TRUE;
 
214
}
 
215
 
 
216
void evo2_tasks_setup(OSyncPluginInfo *info)
 
217
{
 
218
        osync_plugin_accept_objtype(info, "todo");
 
219
        osync_plugin_accept_objformat(info, "todo", "vtodo20", NULL);
 
220
        osync_plugin_set_commit_objformat(info, "todo", "vtodo20", evo2_todo_modify);
 
221
        osync_plugin_set_access_objformat(info, "todo", "vtodo20", evo2_todo_modify);
 
222
}