No issues found
1 /*
2 * e-source-util.c
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
19 #include "e-source-util.h"
20
21 typedef struct _AsyncContext AsyncContext;
22
23 struct _AsyncContext {
24 EActivity *activity;
25 };
26
27 static void
28 async_context_free (AsyncContext *async_context)
29 {
30 if (async_context->activity != NULL)
31 g_object_unref (async_context->activity);
32
33 g_slice_free (AsyncContext, async_context);
34 }
35
36 static void
37 source_util_remove_cb (GObject *source_object,
38 GAsyncResult *result,
39 gpointer user_data)
40 {
41 ESource *source;
42 EActivity *activity;
43 EAlertSink *alert_sink;
44 AsyncContext *async_context;
45 const gchar *display_name;
46 GError *error = NULL;
47
48 source = E_SOURCE (source_object);
49 async_context = (AsyncContext *) user_data;
50
51 activity = async_context->activity;
52 alert_sink = e_activity_get_alert_sink (activity);
53 display_name = e_source_get_display_name (source);
54
55 e_source_remove_finish (source, result, &error);
56
57 if (e_activity_handle_cancellation (activity, error)) {
58 g_error_free (error);
59
60 } else if (error != NULL) {
61 e_alert_submit (
62 alert_sink,
63 "system:remove-source-fail",
64 display_name, error->message, NULL);
65 g_error_free (error);
66
67 } else {
68 e_activity_set_state (activity, E_ACTIVITY_COMPLETED);
69 }
70
71 async_context_free (async_context);
72 }
73
74 /**
75 * e_source_util_remove:
76 * @source: the #ESource to be removed
77 * @alert_sink: an #EAlertSink
78 *
79 * Requests the D-Bus service to delete the key files for @source and all of
80 * its descendants and broadcast their removal to all clients. If an error
81 * occurs, an #EAlert will be posted to @alert_sink.
82 *
83 * This function does not block. The returned #EActivity can either be
84 * ignored or passed to something that can display activity status to the
85 * user, such as e_shell_backend_add_activity().
86 *
87 * Returns: an #EActivity to track the operation
88 **/
89 EActivity *
90 e_source_util_remove (ESource *source,
91 EAlertSink *alert_sink)
92 {
93 AsyncContext *async_context;
94 GCancellable *cancellable;
95
96 g_return_val_if_fail (E_IS_SOURCE (source), NULL);
97 g_return_val_if_fail (E_IS_ALERT_SINK (alert_sink), NULL);
98
99 cancellable = g_cancellable_new ();
100
101 async_context = g_slice_new0 (AsyncContext);
102 async_context->activity = e_activity_new ();
103
104 e_activity_set_alert_sink (async_context->activity, alert_sink);
105 e_activity_set_cancellable (async_context->activity, cancellable);
106
107 e_source_remove (
108 source, cancellable,
109 source_util_remove_cb,
110 async_context);
111
112 g_object_unref (cancellable);
113
114 return async_context->activity;
115 }
116
117 static void
118 source_util_write_cb (GObject *source_object,
119 GAsyncResult *result,
120 gpointer user_data)
121 {
122 ESource *source;
123 EActivity *activity;
124 EAlertSink *alert_sink;
125 AsyncContext *async_context;
126 const gchar *display_name;
127 GError *error = NULL;
128
129 source = E_SOURCE (source_object);
130 async_context = (AsyncContext *) user_data;
131
132 activity = async_context->activity;
133 alert_sink = e_activity_get_alert_sink (activity);
134 display_name = e_source_get_display_name (source);
135
136 e_source_write_finish (source, result, &error);
137
138 if (e_activity_handle_cancellation (activity, error)) {
139 g_error_free (error);
140
141 } else if (error != NULL) {
142 e_alert_submit (
143 alert_sink,
144 "system:write-source-fail",
145 display_name, error->message, NULL);
146 g_error_free (error);
147
148 } else {
149 e_activity_set_state (activity, E_ACTIVITY_COMPLETED);
150 }
151
152 async_context_free (async_context);
153 }
154
155 /**
156 * e_source_util_write:
157 * @source: an #ESource
158 * @alert_sink: an #EAlertSink
159 *
160 * Submits the current contents of @source to the D-Bus service to be
161 * written to disk and broadcast to other clients. If an error occurs,
162 * an #EAlert will be posted to @alert_sink.
163 *
164 * This function does not block. The returned #EActivity can either be
165 * ignored or passed to something that can display activity status to the
166 * user, such as e_shell_backend_add_activity().
167 *
168 * Returns: an #EActivity to track the operation
169 **/
170 EActivity *
171 e_source_util_write (ESource *source,
172 EAlertSink *alert_sink)
173 {
174 AsyncContext *async_context;
175 GCancellable *cancellable;
176
177 g_return_val_if_fail (E_IS_SOURCE (source), NULL);
178 g_return_val_if_fail (E_IS_ALERT_SINK (alert_sink), NULL);
179
180 cancellable = g_cancellable_new ();
181
182 async_context = g_slice_new0 (AsyncContext);
183 async_context->activity = e_activity_new ();
184
185 e_activity_set_alert_sink (async_context->activity, alert_sink);
186 e_activity_set_cancellable (async_context->activity, cancellable);
187
188 e_source_write (
189 source, cancellable,
190 source_util_write_cb,
191 async_context);
192
193 g_object_unref (cancellable);
194
195 return async_context->activity;
196 }
197
198 static void
199 source_util_remote_delete_cb (GObject *source_object,
200 GAsyncResult *result,
201 gpointer user_data)
202 {
203 ESource *source;
204 EActivity *activity;
205 EAlertSink *alert_sink;
206 AsyncContext *async_context;
207 const gchar *display_name;
208 GError *error = NULL;
209
210 source = E_SOURCE (source_object);
211 async_context = (AsyncContext *) user_data;
212
213 activity = async_context->activity;
214 alert_sink = e_activity_get_alert_sink (activity);
215 display_name = e_source_get_display_name (source);
216
217 e_source_remote_delete_finish (source, result, &error);
218
219 if (e_activity_handle_cancellation (activity, error)) {
220 g_error_free (error);
221
222 } else if (error != NULL) {
223 e_alert_submit (
224 alert_sink,
225 "system:delete-resource-fail",
226 display_name, error->message, NULL);
227 g_error_free (error);
228
229 } else {
230 e_activity_set_state (activity, E_ACTIVITY_COMPLETED);
231 }
232
233 async_context_free (async_context);
234 }
235
236 /**
237 * e_source_util_remote_delete:
238 * @source: an #ESource
239 * @alert_sink: an #EAlertSink
240 *
241 * Deletes the resource represented by @source from a remote server.
242 * The @source must be #ESource:remote-deletable. This will also delete
243 * the key file for @source and broadcast its removal to all clients,
244 * similar to e_source_util_remove(). If an error occurs, an #EAlert
245 * will be posted to @alert_sink.
246 *
247 * This function does not block. The returned #EActivity can either be
248 * ignored or passed to something that can display activity status to the
249 * user, such as e_shell_backend_add_activity().
250 *
251 * Returns: an #EActivity to track the operation
252 **/
253 EActivity *
254 e_source_util_remote_delete (ESource *source,
255 EAlertSink *alert_sink)
256 {
257 AsyncContext *async_context;
258 GCancellable *cancellable;
259
260 g_return_val_if_fail (E_IS_SOURCE (source), NULL);
261 g_return_val_if_fail (E_IS_ALERT_SINK (alert_sink), NULL);
262
263 cancellable = g_cancellable_new ();
264
265 async_context = g_slice_new0 (AsyncContext);
266 async_context->activity = e_activity_new ();
267
268 e_activity_set_alert_sink (async_context->activity, alert_sink);
269 e_activity_set_cancellable (async_context->activity, cancellable);
270
271 e_source_remote_delete (
272 source, cancellable,
273 source_util_remote_delete_cb,
274 async_context);
275
276 g_object_unref (cancellable);
277
278 return async_context->activity;
279 }