No issues found
1 /*
2 * Evolution calendar - Copy source dialog
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) version 3.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with the program; if not, see <http://www.gnu.org/licenses/>
16 *
17 *
18 * Authors:
19 * Rodrigo Moya <rodrigo@ximian.com>
20 *
21 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
22 *
23 */
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <glib/gi18n.h>
30 #include <libedataserverui/libedataserverui.h>
31
32 #include "copy-source-dialog.h"
33 #include "select-source-dialog.h"
34
35 typedef struct {
36 GtkWindow *parent;
37 ESource *orig_source;
38 EClientSourceType obj_type;
39 ESource *selected_source;
40 ECalClient *source_client, *dest_client;
41 } CopySourceDialogData;
42
43 static void
44 show_error (CopySourceDialogData *csdd,
45 const gchar *msg,
46 const GError *error)
47 {
48 GtkWidget *dialog;
49
50 dialog = gtk_message_dialog_new (
51 csdd->parent, 0, GTK_MESSAGE_ERROR,
52 GTK_BUTTONS_CLOSE, error ? "%s\n%s" : "%s", msg, error ? error->message : "");
53 gtk_dialog_run (GTK_DIALOG (dialog));
54 gtk_widget_destroy (dialog);
55 }
56
57 struct ForeachTzidData
58 {
59 ECalClient *source_client;
60 ECalClient *dest_client;
61 };
62
63 static void
64 add_timezone_to_cal_cb (icalparameter *param,
65 gpointer data)
66 {
67 struct ForeachTzidData *ftd = data;
68 icaltimezone *tz = NULL;
69 const gchar *tzid;
70
71 g_return_if_fail (ftd != NULL);
72 g_return_if_fail (ftd->source_client != NULL);
73 g_return_if_fail (ftd->dest_client != NULL);
74
75 tzid = icalparameter_get_tzid (param);
76 if (!tzid || !*tzid)
77 return;
78
79 if (e_cal_client_get_timezone_sync (ftd->source_client, tzid, &tz, NULL, NULL) && tz)
80 e_cal_client_add_timezone_sync (ftd->dest_client, tz, NULL, NULL);
81 }
82
83 static void
84 free_copy_data (CopySourceDialogData *csdd)
85 {
86 if (!csdd)
87 return;
88
89 if (csdd->orig_source)
90 g_object_unref (csdd->orig_source);
91 if (csdd->selected_source)
92 g_object_unref (csdd->selected_source);
93 if (csdd->source_client)
94 g_object_unref (csdd->source_client);
95 if (csdd->dest_client)
96 g_object_unref (csdd->dest_client);
97 g_free (csdd);
98 }
99
100 static void
101 dest_source_opened_cb (GObject *source_object,
102 GAsyncResult *result,
103 gpointer user_data)
104 {
105 ESource *source = E_SOURCE (source_object);
106 CopySourceDialogData *csdd = user_data;
107 EClient *client = NULL;
108 GError *error = NULL;
109
110 e_client_utils_open_new_finish (source, result, &client, &error);
111
112 if (error != NULL) {
113 g_warn_if_fail (client == NULL);
114 show_error (csdd, _("Could not open destination"), error);
115 g_error_free (error);
116 free_copy_data (csdd);
117 return;
118 }
119
120 g_return_if_fail (E_IS_CLIENT (client));
121
122 csdd->dest_client = E_CAL_CLIENT (client);
123
124 /* check if the destination is read only */
125 if (e_client_is_readonly (E_CLIENT (csdd->dest_client))) {
126 show_error (csdd, _("Destination is read only"), NULL);
127 } else {
128 GSList *obj_list = NULL;
129 if (e_cal_client_get_object_list_sync (csdd->source_client, "#t", &obj_list, NULL, NULL)) {
130 GSList *l;
131 icalcomponent *icalcomp;
132 struct ForeachTzidData ftd;
133
134 ftd.source_client = csdd->source_client;
135 ftd.dest_client = csdd->dest_client;
136
137 for (l = obj_list; l != NULL; l = l->next) {
138 /* FIXME: process recurrences */
139 /* FIXME: process errors */
140 if (e_cal_client_get_object_sync (csdd->dest_client, icalcomponent_get_uid (l->data), NULL,
141 &icalcomp, NULL, NULL)) {
142 e_cal_client_modify_object_sync (csdd->dest_client, l->data, CALOBJ_MOD_ALL, NULL, NULL);
143 icalcomponent_free (icalcomp);
144 } else {
145 gchar *uid = NULL;
146 GError *error = NULL;
147
148 icalcomp = l->data;
149
150 /* Add timezone information from source
151 * ECal to the destination ECal. */
152 icalcomponent_foreach_tzid (
153 icalcomp,
154 add_timezone_to_cal_cb, &ftd);
155
156 if (e_cal_client_create_object_sync (csdd->dest_client, icalcomp, &uid, NULL, &error)) {
157 g_free (uid);
158 } else {
159 if (error) {
160 show_error (csdd, _("Cannot create object"), error);
161 g_error_free (error);
162 }
163 break;
164 }
165 }
166 }
167
168 e_cal_client_free_icalcomp_slist (obj_list);
169 }
170 }
171
172 free_copy_data (csdd);
173 }
174
175 static void
176 orig_source_opened_cb (GObject *source_object,
177 GAsyncResult *result,
178 gpointer user_data)
179 {
180 ESource *source = E_SOURCE (source_object);
181 CopySourceDialogData *csdd = user_data;
182 EClient *client = NULL;
183 GError *error = NULL;
184
185 e_client_utils_open_new_finish (source, result, &client, &error);
186
187 if (error != NULL) {
188 g_warn_if_fail (client == NULL);
189 show_error (csdd, _("Could not open source"), error);
190 g_error_free (error);
191 free_copy_data (csdd);
192 return;
193 }
194
195 g_return_if_fail (E_IS_CLIENT (client));
196
197 csdd->source_client = E_CAL_CLIENT (client);
198
199 e_client_utils_open_new (
200 csdd->selected_source, csdd->obj_type, FALSE, NULL,
201 dest_source_opened_cb, csdd);
202 }
203
204 static void
205 copy_source (const CopySourceDialogData *const_csdd)
206 {
207 CopySourceDialogData *csdd;
208
209 if (!const_csdd->selected_source)
210 return;
211
212 g_return_if_fail (const_csdd->obj_type != E_CLIENT_SOURCE_TYPE_LAST);
213
214 csdd = g_new0 (CopySourceDialogData, 1);
215 csdd->parent = const_csdd->parent;
216 csdd->orig_source = g_object_ref (const_csdd->orig_source);
217 csdd->obj_type = const_csdd->obj_type;
218 csdd->selected_source = g_object_ref (const_csdd->selected_source);
219
220 e_client_utils_open_new (
221 csdd->orig_source, csdd->obj_type, FALSE, NULL,
222 orig_source_opened_cb, csdd);
223 }
224
225 /**
226 * copy_source_dialog
227 *
228 * Implements the Copy command for sources, allowing the user to select a target
229 * source to copy to.
230 */
231 void
232 copy_source_dialog (GtkWindow *parent,
233 ESourceRegistry *registry,
234 ESource *source,
235 ECalClientSourceType obj_type)
236 {
237 CopySourceDialogData csdd;
238
239 g_return_if_fail (E_IS_SOURCE_REGISTRY (registry));
240 g_return_if_fail (E_IS_SOURCE (source));
241 g_return_if_fail (obj_type == E_CAL_CLIENT_SOURCE_TYPE_EVENTS ||
242 obj_type == E_CAL_CLIENT_SOURCE_TYPE_TASKS ||
243 obj_type == E_CAL_CLIENT_SOURCE_TYPE_MEMOS);
244
245 csdd.parent = parent;
246 csdd.orig_source = source;
247 csdd.selected_source = NULL;
248 csdd.obj_type = obj_type == E_CAL_CLIENT_SOURCE_TYPE_EVENTS ? E_CLIENT_SOURCE_TYPE_EVENTS :
249 obj_type == E_CAL_CLIENT_SOURCE_TYPE_TASKS ? E_CLIENT_SOURCE_TYPE_TASKS :
250 obj_type == E_CAL_CLIENT_SOURCE_TYPE_MEMOS ? E_CLIENT_SOURCE_TYPE_MEMOS :
251 E_CLIENT_SOURCE_TYPE_LAST;
252
253 csdd.selected_source = select_source_dialog (
254 parent, registry, obj_type, source);
255 if (csdd.selected_source) {
256 copy_source (&csdd);
257
258 /* free memory */
259 g_object_unref (csdd.selected_source);
260 }
261 }