Update Indonesian translation
[empathy-mirror.git] / libempathy / empathy-pkg-kit.c
blob21f703b68166f59ac75b7193f1ef00d9941de3c5
1 /*
2 * Copyright (C) 2012 Collabora Ltd.
4 * This library 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.1 of the License, or (at your option) any later version.
9 * This library 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.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 * Authors: Guillaume Desmottes <guillaume.desmottes@collabora.com>
21 #include "config.h"
22 #include "empathy-pkg-kit.h"
24 typedef struct
26 guint xid;
27 gchar **packages;
28 gchar *options;
30 GSimpleAsyncResult *result;
31 GCancellable *cancellable;
32 } InstallCtx;
34 static InstallCtx *
35 install_ctx_new (guint xid,
36 const gchar **packages,
37 const gchar *options,
38 GSimpleAsyncResult *result,
39 GCancellable *cancellable)
41 InstallCtx *ctx = g_slice_new (InstallCtx);
43 ctx->xid = xid;
44 ctx->packages = g_strdupv ((gchar **) packages);
45 ctx->options = g_strdup (options);
46 ctx->result = g_object_ref (result);
47 ctx->cancellable = cancellable != NULL ? g_object_ref (cancellable) : NULL;
48 return ctx;
51 static void
52 install_ctx_free (InstallCtx *ctx)
54 g_free (ctx->packages);
55 g_free (ctx->options);
56 g_object_unref (ctx->result);
57 g_slice_free (InstallCtx, ctx);
60 static void
61 install_ctx_complete (InstallCtx *ctx)
63 g_simple_async_result_complete (ctx->result);
64 install_ctx_free (ctx);
67 static void
68 install_ctx_failed (InstallCtx *ctx,
69 GError *error)
71 g_simple_async_result_take_error (ctx->result, error);
72 install_ctx_complete (ctx);
75 static void
76 install_package_names_cb (GObject *source,
77 GAsyncResult *result,
78 gpointer user_data)
80 InstallCtx *ctx = user_data;
81 GError *error = NULL;
82 GVariant *res;
84 res = g_dbus_proxy_call_finish (G_DBUS_PROXY (source), result, &error);
85 if (res == NULL)
87 install_ctx_failed (ctx, error);
88 return;
91 install_ctx_complete (ctx);
93 g_variant_unref (res);
96 static void
97 pkg_kit_proxy_new_cb (GObject *source,
98 GAsyncResult *result,
99 gpointer user_data)
101 InstallCtx *ctx = user_data;
102 GError *error = NULL;
103 GDBusProxy *proxy;
105 proxy = g_dbus_proxy_new_for_bus_finish (result, &error);
106 if (proxy == NULL)
108 install_ctx_failed (ctx, error);
109 return;
112 g_dbus_proxy_call (proxy, "InstallPackageNames",
113 g_variant_new ("(u^a&ss)", ctx->xid, ctx->packages, ctx->options),
114 G_DBUS_CALL_FLAGS_NONE, G_MAXINT, NULL, install_package_names_cb, ctx);
116 g_object_unref (proxy);
119 /* Ideally this should live in PackageKit (fdo #45741) */
120 void
121 empathy_pkg_kit_install_packages_async (
122 guint xid,
123 const gchar **packages,
124 const gchar *options,
125 GCancellable *cancellable,
126 GAsyncReadyCallback callback,
127 gpointer user_data)
129 InstallCtx *ctx;
130 GSimpleAsyncResult *result;
132 result = g_simple_async_result_new (NULL, callback, user_data,
133 empathy_pkg_kit_install_packages_async);
135 ctx = install_ctx_new (xid, packages, options != NULL ? options : "",
136 result, cancellable);
138 g_dbus_proxy_new_for_bus (G_BUS_TYPE_SESSION,
139 G_DBUS_PROXY_FLAGS_NONE, NULL,
140 "org.freedesktop.PackageKit",
141 "/org/freedesktop/PackageKit",
142 "org.freedesktop.PackageKit.Modify",
143 NULL, pkg_kit_proxy_new_cb, ctx);
145 g_object_unref (result);
148 gboolean
149 empathy_pkg_kit_install_packages_finish (GAsyncResult *result,
150 GError **error)
152 g_return_val_if_fail (g_simple_async_result_is_valid (result, NULL,
153 empathy_pkg_kit_install_packages_async), FALSE);
155 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
156 error))
157 return FALSE;
159 return TRUE;