]> git.0d.be Git - empathy.git/blob - libempathy/empathy-pkg-kit.c
Updated Czech translation
[empathy.git] / libempathy / empathy-pkg-kit.c
1 /*
2  * Copyright (C) 2012 Collabora Ltd.
3  *
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.
8  *
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.
13  *
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
17  *
18  * Authors: Guillaume Desmottes <guillaume.desmottes@collabora.com>
19  */
20
21 #include "config.h"
22 #include "empathy-pkg-kit.h"
23
24 typedef struct
25 {
26   guint xid;
27   gchar **packages;
28   gchar *options;
29
30   GSimpleAsyncResult *result;
31   GCancellable *cancellable;
32 } InstallCtx;
33
34 static InstallCtx *
35 install_ctx_new (guint xid,
36     const gchar **packages,
37     const gchar *options,
38     GSimpleAsyncResult *result,
39     GCancellable *cancellable)
40 {
41   InstallCtx *ctx = g_slice_new (InstallCtx);
42
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;
49 }
50
51 static void
52 install_ctx_free (InstallCtx *ctx)
53 {
54   g_free (ctx->packages);
55   g_free (ctx->options);
56   g_object_unref (ctx->result);
57   g_slice_free (InstallCtx, ctx);
58 }
59
60 static void
61 install_ctx_complete (InstallCtx *ctx)
62 {
63   g_simple_async_result_complete (ctx->result);
64   install_ctx_free (ctx);
65 }
66
67 static void
68 install_ctx_failed (InstallCtx *ctx,
69     GError *error)
70 {
71   g_simple_async_result_take_error (ctx->result, error);
72   install_ctx_complete (ctx);
73 }
74
75 static void
76 install_package_names_cb (GObject *source,
77     GAsyncResult *result,
78     gpointer user_data)
79 {
80   InstallCtx *ctx = user_data;
81   GError *error = NULL;
82   GVariant *res;
83
84   res = g_dbus_proxy_call_finish (G_DBUS_PROXY (source), result, &error);
85   if (res == NULL)
86     {
87       install_ctx_failed (ctx, error);
88       return;
89     }
90
91   install_ctx_complete (ctx);
92
93   g_variant_unref (res);
94 }
95
96 static void
97 pkg_kit_proxy_new_cb (GObject *source,
98     GAsyncResult *result,
99     gpointer user_data)
100 {
101   InstallCtx *ctx = user_data;
102   GError *error = NULL;
103   GDBusProxy *proxy;
104
105   proxy = g_dbus_proxy_new_for_bus_finish (result, &error);
106   if (proxy == NULL)
107     {
108       install_ctx_failed (ctx, error);
109       return;
110     }
111
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);
115
116   g_object_unref (proxy);
117 }
118
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)
128 {
129   InstallCtx *ctx;
130   GSimpleAsyncResult *result;
131
132   result = g_simple_async_result_new (NULL, callback, user_data,
133       empathy_pkg_kit_install_packages_async);
134
135   ctx = install_ctx_new (xid, packages, options != NULL ? options : "",
136       result, cancellable);
137
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);
144
145   g_object_unref (result);
146 }
147
148 gboolean
149 empathy_pkg_kit_install_packages_finish (GAsyncResult *result,
150     GError **error)
151 {
152   g_return_val_if_fail (g_simple_async_result_is_valid (result, NULL,
153         empathy_pkg_kit_install_packages_async), FALSE);
154
155   if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
156         error))
157     return FALSE;
158
159   return TRUE;
160 }