4 * Copyright (C) 2012 Intel Corporation. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU Lesser General Public License,
8 * version 2.1, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
19 * Mark Ryan <mark.d.ryan@intel.com>
26 static msu_task_t *prv_upload_new_generic(msu_task_type_t type,
27 GDBusMethodInvocation *invocation,
29 GVariant *parameters);
31 msu_task_t *msu_task_get_version_new(GDBusMethodInvocation *invocation)
33 msu_task_t *task = g_new0(msu_task_t, 1);
35 task->type = MSU_TASK_GET_VERSION;
36 task->invocation = invocation;
37 task->result_format = "(@s)";
38 task->result = g_variant_ref_sink(g_variant_new_string(VERSION));
39 task->synchronous = TRUE;
44 msu_task_t *msu_task_get_servers_new(GDBusMethodInvocation *invocation)
46 msu_task_t *task = g_new0(msu_task_t, 1);
48 task->type = MSU_TASK_GET_SERVERS;
49 task->invocation = invocation;
50 task->result_format = "(@ao)";
51 task->synchronous = TRUE;
56 static msu_task_t *prv_m2spec_task_new(msu_task_type_t type,
57 GDBusMethodInvocation *invocation,
59 const gchar *result_format)
61 msu_task_t *task = g_new0(msu_task_t, 1);
64 task->invocation = invocation;
65 task->result_format = result_format;
67 task->path = g_strdup(path);
68 g_strstrip(task->path);
73 msu_task_t *msu_task_get_children_new(GDBusMethodInvocation *invocation,
74 const gchar *path, GVariant *parameters,
75 gboolean items, gboolean containers)
79 task = prv_m2spec_task_new(MSU_TASK_GET_CHILDREN, invocation, path,
82 task->ut.get_children.containers = containers;
83 task->ut.get_children.items = items;
85 g_variant_get(parameters, "(uu@as)", &task->ut.get_children.start,
86 &task->ut.get_children.count,
87 &task->ut.get_children.filter);
89 task->ut.get_children.sort_by = g_strdup("");
94 msu_task_t *msu_task_get_children_ex_new(GDBusMethodInvocation *invocation,
96 GVariant *parameters, gboolean items,
101 task = prv_m2spec_task_new(MSU_TASK_GET_CHILDREN, invocation, path,
104 task->ut.get_children.containers = containers;
105 task->ut.get_children.items = items;
107 g_variant_get(parameters, "(uu@ass)", &task->ut.get_children.start,
108 &task->ut.get_children.count,
109 &task->ut.get_children.filter,
110 &task->ut.get_children.sort_by);
115 msu_task_t *msu_task_get_prop_new(GDBusMethodInvocation *invocation,
116 const gchar *path, GVariant *parameters)
120 task = prv_m2spec_task_new(MSU_TASK_GET_PROP, invocation, path, "(v)");
122 g_variant_get(parameters, "(ss)", &task->ut.get_prop.interface_name,
123 &task->ut.get_prop.prop_name);
125 g_strstrip(task->ut.get_prop.interface_name);
126 g_strstrip(task->ut.get_prop.prop_name);
131 msu_task_t *msu_task_get_props_new(GDBusMethodInvocation *invocation,
132 const gchar *path, GVariant *parameters)
136 task = prv_m2spec_task_new(MSU_TASK_GET_ALL_PROPS, invocation, path,
139 g_variant_get(parameters, "(s)", &task->ut.get_props.interface_name);
140 g_strstrip(task->ut.get_props.interface_name);
145 msu_task_t *msu_task_search_new(GDBusMethodInvocation *invocation,
146 const gchar *path, GVariant *parameters)
150 task = prv_m2spec_task_new(MSU_TASK_SEARCH, invocation, path,
153 g_variant_get(parameters, "(suu@as)", &task->ut.search.query,
154 &task->ut.search.start, &task->ut.search.count,
155 &task->ut.search.filter);
157 task->ut.search.sort_by = g_strdup("");
162 msu_task_t *msu_task_search_ex_new(GDBusMethodInvocation *invocation,
163 const gchar *path, GVariant *parameters)
167 task = prv_m2spec_task_new(MSU_TASK_SEARCH, invocation, path,
170 g_variant_get(parameters, "(suu@ass)", &task->ut.search.query,
171 &task->ut.search.start, &task->ut.search.count,
172 &task->ut.search.filter, &task->ut.search.sort_by);
174 task->multiple_retvals = TRUE;
179 msu_task_t *msu_task_get_resource_new(GDBusMethodInvocation *invocation,
180 const gchar *path, GVariant *parameters)
184 task = prv_m2spec_task_new(MSU_TASK_GET_RESOURCE, invocation, path,
187 g_variant_get(parameters, "(s@as)", &task->ut.resource.protocol_info,
188 &task->ut.resource.filter);
193 msu_task_t *msu_task_set_protocol_info_new(GDBusMethodInvocation *invocation,
194 GVariant *parameters)
196 msu_task_t *task = g_new0(msu_task_t, 1);
198 task->type = MSU_TASK_SET_PROTOCOL_INFO;
199 task->invocation = invocation;
200 task->synchronous = TRUE;
201 g_variant_get(parameters, "(s)", &task->ut.protocol_info.protocol_info);
206 static msu_task_t *prv_upload_new_generic(msu_task_type_t type,
207 GDBusMethodInvocation *invocation,
209 GVariant *parameters)
213 task = prv_m2spec_task_new(type, invocation, path, "(uo)");
215 g_variant_get(parameters, "(ss)", &task->ut.upload.display_name,
216 &task->ut.upload.file_path);
217 g_strstrip(task->ut.upload.file_path);
218 task->multiple_retvals = TRUE;
223 msu_task_t *msu_task_prefer_local_addresses_new(
224 GDBusMethodInvocation *invocation,
225 GVariant *parameters)
227 msu_task_t *task = g_new0(msu_task_t, 1);
229 task->type = MSU_TASK_SET_PREFER_LOCAL_ADDRESSES;
230 task->invocation = invocation;
231 task->synchronous = TRUE;
232 g_variant_get(parameters, "(b)",
233 &task->ut.prefer_local_addresses.prefer);
238 msu_task_t *msu_task_upload_to_any_new(GDBusMethodInvocation *invocation,
239 const gchar *path, GVariant *parameters)
241 return prv_upload_new_generic(MSU_TASK_UPLOAD_TO_ANY, invocation,
245 msu_task_t *msu_task_upload_new(GDBusMethodInvocation *invocation,
246 const gchar *path, GVariant *parameters)
248 return prv_upload_new_generic(MSU_TASK_UPLOAD, invocation,
252 msu_task_t *msu_task_get_upload_status_new(GDBusMethodInvocation *invocation,
254 GVariant *parameters)
258 task = prv_m2spec_task_new(MSU_TASK_GET_UPLOAD_STATUS, invocation, path,
261 g_variant_get(parameters, "(u)",
262 &task->ut.upload_action.upload_id);
263 task->synchronous = TRUE;
264 task->multiple_retvals = TRUE;
269 msu_task_t *msu_task_get_upload_ids_new(GDBusMethodInvocation *invocation,
274 task = prv_m2spec_task_new(MSU_TASK_GET_UPLOAD_IDS, invocation, path,
277 task->synchronous = TRUE;
282 msu_task_t *msu_task_cancel_upload_new(GDBusMethodInvocation *invocation,
284 GVariant *parameters)
288 task = prv_m2spec_task_new(MSU_TASK_CANCEL_UPLOAD, invocation, path,
291 g_variant_get(parameters, "(u)",
292 &task->ut.upload_action.upload_id);
293 task->synchronous = TRUE;
298 msu_task_t *msu_task_delete_new(GDBusMethodInvocation *invocation,
303 task = prv_m2spec_task_new(MSU_TASK_DELETE_OBJECT, invocation,
308 msu_task_t *msu_task_create_container_new_generic(
309 GDBusMethodInvocation *invocation,
310 msu_task_type_t type,
312 GVariant *parameters)
316 task = prv_m2spec_task_new(type, invocation, path, "(@o)");
318 g_variant_get(parameters, "(ss@as)",
319 &task->ut.create_container.display_name,
320 &task->ut.create_container.type,
321 &task->ut.create_container.child_types);
326 msu_task_t *msu_task_update_new(GDBusMethodInvocation *invocation,
327 const gchar *path, GVariant *parameters)
331 task = prv_m2spec_task_new(MSU_TASK_UPDATE_OBJECT, invocation, path,
334 g_variant_get(parameters, "(@a{sv}@as)",
335 &task->ut.update.to_add_update,
336 &task->ut.update.to_delete);
341 static void prv_msu_task_delete(msu_task_t *task)
343 switch (task->type) {
344 case MSU_TASK_GET_CHILDREN:
345 if (task->ut.get_children.filter)
346 g_variant_unref(task->ut.get_children.filter);
347 g_free(task->ut.get_children.sort_by);
349 case MSU_TASK_GET_ALL_PROPS:
350 g_free(task->ut.get_props.interface_name);
352 case MSU_TASK_GET_PROP:
353 g_free(task->ut.get_prop.interface_name);
354 g_free(task->ut.get_prop.prop_name);
356 case MSU_TASK_SEARCH:
357 g_free(task->ut.search.query);
358 if (task->ut.search.filter)
359 g_variant_unref(task->ut.search.filter);
360 g_free(task->ut.search.sort_by);
362 case MSU_TASK_GET_RESOURCE:
363 if (task->ut.resource.filter)
364 g_variant_unref(task->ut.resource.filter);
365 g_free(task->ut.resource.protocol_info);
367 case MSU_TASK_SET_PROTOCOL_INFO:
368 if (task->ut.protocol_info.protocol_info)
369 g_free(task->ut.protocol_info.protocol_info);
371 case MSU_TASK_UPLOAD_TO_ANY:
372 case MSU_TASK_UPLOAD:
373 g_free(task->ut.upload.display_name);
374 g_free(task->ut.upload.file_path);
376 case MSU_TASK_CREATE_CONTAINER:
377 case MSU_TASK_CREATE_CONTAINER_IN_ANY:
378 g_free(task->ut.create_container.display_name);
379 g_free(task->ut.create_container.type);
380 g_variant_unref(task->ut.create_container.child_types);
382 case MSU_TASK_UPDATE_OBJECT:
383 if (task->ut.update.to_add_update)
384 g_variant_unref(task->ut.update.to_add_update);
385 if (task->ut.update.to_delete)
386 g_variant_unref(task->ut.update.to_delete);
394 g_variant_unref(task->result);
399 void msu_task_complete_and_delete(msu_task_t *task)
401 GVariant *variant = NULL;
406 if (task->invocation) {
407 if (task->result_format) {
408 if (task->multiple_retvals)
409 variant = task->result;
411 variant = g_variant_new(task->result_format,
414 g_dbus_method_invocation_return_value(task->invocation,
417 prv_msu_task_delete(task);
424 void msu_task_fail_and_delete(msu_task_t *task, GError *error)
429 if (task->invocation)
430 g_dbus_method_invocation_return_gerror(task->invocation, error);
432 prv_msu_task_delete(task);
439 void msu_task_cancel_and_delete(msu_task_t *task)
446 if (task->invocation) {
447 error = g_error_new(MSU_ERROR, MSU_ERROR_CANCELLED,
448 "Operation cancelled.");
449 g_dbus_method_invocation_return_gerror(task->invocation, error);
453 prv_msu_task_delete(task);
460 void msu_task_delete(msu_task_t *task)
467 if (task->invocation) {
468 error = g_error_new(MSU_ERROR, MSU_ERROR_DIED,
469 "Unable to complete command.");
470 g_dbus_method_invocation_return_gerror(task->invocation, error);
474 prv_msu_task_delete(task);