]> git.0d.be Git - empathy.git/blob - libempathy/empathy-pkg-kit.c
UOA: Do not segfault when "Done" or "Cancel" button clicked but widget is not ready yet
[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
23 #include <libempathy/empathy-pkg-kit.h>
24
25 typedef struct
26 {
27   guint xid;
28   gchar **packages;
29   gchar *options;
30
31   GSimpleAsyncResult *result;
32   GCancellable *cancellable;
33 } InstallCtx;
34
35 static InstallCtx *
36 install_ctx_new (guint xid,
37     const gchar **packages,
38     const gchar *options,
39     GSimpleAsyncResult *result,
40     GCancellable *cancellable)
41 {
42   InstallCtx *ctx = g_slice_new (InstallCtx);
43
44   ctx->xid = xid;
45   ctx->packages = g_strdupv ((gchar **) packages);
46   ctx->options = g_strdup (options);
47   ctx->result = g_object_ref (result);
48   ctx->cancellable = cancellable != NULL ? g_object_ref (cancellable) : NULL;
49   return ctx;
50 }
51
52 static void
53 install_ctx_free (InstallCtx *ctx)
54 {
55   g_free (ctx->packages);
56   g_free (ctx->options);
57   g_object_unref (ctx->result);
58   g_slice_free (InstallCtx, ctx);
59 }
60
61 static void
62 install_ctx_complete (InstallCtx *ctx)
63 {
64   g_simple_async_result_complete (ctx->result);
65   install_ctx_free (ctx);
66 }
67
68 static void
69 install_ctx_failed (InstallCtx *ctx,
70     GError *error)
71 {
72   g_simple_async_result_take_error (ctx->result, error);
73   install_ctx_complete (ctx);
74 }
75
76 static void
77 install_package_names_cb (GObject *source,
78     GAsyncResult *result,
79     gpointer user_data)
80 {
81   InstallCtx *ctx = user_data;
82   GError *error = NULL;
83   GVariant *res;
84
85   res = g_dbus_proxy_call_finish (G_DBUS_PROXY (source), result, &error);
86   if (res == NULL)
87     {
88       install_ctx_failed (ctx, error);
89       return;
90     }
91
92   install_ctx_complete (ctx);
93
94   g_variant_unref (res);
95 }
96
97 static void
98 pkg_kit_proxy_new_cb (GObject *source,
99     GAsyncResult *result,
100     gpointer user_data)
101 {
102   InstallCtx *ctx = user_data;
103   GError *error = NULL;
104   GDBusProxy *proxy;
105
106   proxy = g_dbus_proxy_new_for_bus_finish (result, &error);
107   if (proxy == NULL)
108     {
109       install_ctx_failed (ctx, error);
110       return;
111     }
112
113   g_dbus_proxy_call (proxy, "InstallPackageNames",
114       g_variant_new ("(u^a&ss)", ctx->xid, ctx->packages, ctx->options),
115       G_DBUS_CALL_FLAGS_NONE, G_MAXINT, NULL, install_package_names_cb, ctx);
116
117   g_object_unref (proxy);
118 }
119
120 /* Ideally this should live in PackageKit (fdo #45741) */
121 void
122 empathy_pkg_kit_install_packages_async (
123     guint xid,
124     const gchar **packages,
125     const gchar *options,
126     GCancellable *cancellable,
127     GAsyncReadyCallback callback,
128     gpointer user_data)
129 {
130   InstallCtx *ctx;
131   GSimpleAsyncResult *result;
132
133   result = g_simple_async_result_new (NULL, callback, user_data,
134       empathy_pkg_kit_install_packages_async);
135
136   ctx = install_ctx_new (xid, packages, options != NULL ? options : "",
137       result, cancellable);
138
139   g_dbus_proxy_new_for_bus (G_BUS_TYPE_SESSION,
140       G_DBUS_PROXY_FLAGS_NONE, NULL,
141       "org.freedesktop.PackageKit",
142       "/org/freedesktop/PackageKit",
143       "org.freedesktop.PackageKit.Modify",
144       NULL, pkg_kit_proxy_new_cb, ctx);
145
146   g_object_unref (result);
147 }
148
149 gboolean
150 empathy_pkg_kit_install_packages_finish (GAsyncResult *result,
151     GError **error)
152 {
153   g_return_val_if_fail (g_simple_async_result_is_valid (result, NULL,
154         empathy_pkg_kit_install_packages_async), FALSE);
155
156   if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
157         error))
158     return FALSE;
159
160   return TRUE;
161 }