summaryrefslogtreecommitdiff
path: root/gtk/gtkappchooseronlinepk.c
blob: 0f62bb2594364e704d1066a266f0ddcd4a2e317c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
 * gtkappchooseronlinepk.c: packagekit module for app-chooser
 *
 * Copyright (C) 2010 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with the Gnome Library; see the file COPYING.LIB.  If not,
 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Authors: Cosimo Cecchi <ccecchi@redhat.com>
 */

#include "config.h"

#include "gtkappchooseronlinepk.h"

#include "gtkappchooseronline.h"
#ifdef GDK_WINDOWING_X11
#include "x11/gdkx.h"
#endif

#include <gio/gio.h>

#define gtk_app_chooser_online_pk_get_type _gtk_app_chooser_online_pk_get_type
static void app_chooser_online_iface_init (GtkAppChooserOnlineInterface *iface);
static void app_chooser_online_pk_async_initable_init (GAsyncInitableIface *iface);

G_DEFINE_TYPE_WITH_CODE (GtkAppChooserOnlinePk, gtk_app_chooser_online_pk,
                         G_TYPE_OBJECT,
                         G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE,
                                                app_chooser_online_pk_async_initable_init)
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_APP_CHOOSER_ONLINE,
                                                app_chooser_online_iface_init)
                         g_io_extension_point_implement ("gtkappchooser-online",
                                                         g_define_type_id,
                                                         "packagekit", 10));

struct _GtkAppChooserOnlinePkPrivate {
  GSimpleAsyncResult *init_result;
  guint watch_id;

  GDBusProxy *proxy;
  GSimpleAsyncResult *result;
  GtkWindow *parent;
};

static void
gtk_app_chooser_online_pk_dispose (GObject *obj)
{
  GtkAppChooserOnlinePk *self = GTK_APP_CHOOSER_ONLINE_PK (obj);

  g_clear_object (&self->priv->result);
  g_clear_object (&self->priv->proxy);

  G_OBJECT_CLASS (gtk_app_chooser_online_pk_parent_class)->dispose (obj);
}

static void
gtk_app_chooser_online_pk_class_init (GtkAppChooserOnlinePkClass *klass)
{
  GObjectClass *oclass = G_OBJECT_CLASS (klass);

  oclass->dispose = gtk_app_chooser_online_pk_dispose;

  g_type_class_add_private (klass, sizeof (GtkAppChooserOnlinePkPrivate));
}

static void
gtk_app_chooser_online_pk_init (GtkAppChooserOnlinePk *self)
{
  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, GTK_TYPE_APP_CHOOSER_ONLINE_PK,
                                            GtkAppChooserOnlinePkPrivate);
}

static gboolean
pk_search_mime_finish (GtkAppChooserOnline *obj,
                       GAsyncResult *res,
                       GError **error)
{
  GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);

  return !g_simple_async_result_propagate_error (simple, error);
}

static void
install_mime_types_ready_cb (GObject      *source,
                             GAsyncResult *res,
                             gpointer      user_data)
{
  GtkAppChooserOnlinePk *self = user_data;
  GDBusProxy *proxy = G_DBUS_PROXY (source);
  GError *error = NULL;
  GVariant *variant;

  variant = g_dbus_proxy_call_finish (proxy, res, &error);

  if (variant == NULL)
    {
      /* don't show errors if the user cancelled the installation explicitely
       * or if PK wasn't able to find any apps
       */
      if (g_strcmp0 (g_dbus_error_get_remote_error (error), "org.freedesktop.PackageKit.Modify.Cancelled") != 0 &&
          g_strcmp0 (g_dbus_error_get_remote_error (error), "org.freedesktop.PackageKit.Modify.NoPackagesFound") != 0)
        g_simple_async_result_set_from_error (self->priv->result, error);

      g_error_free (error);
    }

  g_simple_async_result_complete (self->priv->result);
  g_clear_object (&self->priv->result);
}

static void
pk_search_mime_async (GtkAppChooserOnline *obj,
                      const gchar         *content_type,
                      GtkWindow           *parent,
                      GCancellable        *cancellable,
                      GAsyncReadyCallback  callback,
                      gpointer             user_data)
{
  GtkAppChooserOnlinePk *self = GTK_APP_CHOOSER_ONLINE_PK (obj);
  guint xid = 0;
  GdkWindow *window;
  const gchar *mime_types[2];

  self->priv->result = g_simple_async_result_new (G_OBJECT (self),
                                                  callback, user_data,
                                                  _gtk_app_chooser_online_search_for_mimetype_async);

#ifdef GDK_WINDOWING_X11
  window = gtk_widget_get_window (GTK_WIDGET (parent));
  xid = GDK_WINDOW_XID (window);
#endif

  mime_types[0] = content_type;
  mime_types[1] = NULL;

  g_dbus_proxy_call (self->priv->proxy,
                     "InstallMimeTypes",
                     g_variant_new ("(u^ass)",
                                    xid,
                                    mime_types,
                                    "hide-confirm-search"),
                     G_DBUS_CALL_FLAGS_NONE,
                     G_MAXINT, /* no timeout */
                     cancellable,
                     install_mime_types_ready_cb,
                     self);
}

static void
app_chooser_online_iface_init (GtkAppChooserOnlineInterface *iface)
{
  iface->search_for_mimetype_async = pk_search_mime_async;
  iface->search_for_mimetype_finish = pk_search_mime_finish;
}

static void
pk_proxy_created_cb (GObject *source,
                     GAsyncResult *result,
                     gpointer user_data)
{
  GtkAppChooserOnlinePk *self = user_data;
  GDBusProxy *proxy;

  proxy = g_dbus_proxy_new_finish (result, NULL);

  if (proxy == NULL)
    {
      g_simple_async_result_set_op_res_gboolean (self->priv->init_result, FALSE);
    }
  else
    {
      g_simple_async_result_set_op_res_gboolean (self->priv->init_result, TRUE);
      self->priv->proxy = proxy;
    }

  g_simple_async_result_complete (self->priv->init_result);
  g_clear_object (&self->priv->init_result);
}

static void
pk_appeared_cb (GDBusConnection *conn,
                const gchar *name,
                const gchar *owner,
                gpointer user_data)
{
  GtkAppChooserOnlinePk *self = user_data;

  /* create the proxy */
  g_dbus_proxy_new (conn, 0, NULL,
                    "org.freedesktop.PackageKit",
                    "/org/freedesktop/PackageKit",
                    "org.freedesktop.PackageKit.Modify",
                    NULL,
                    pk_proxy_created_cb,
                    self);

  g_bus_unwatch_name (self->priv->watch_id);
}

static void
pk_vanished_cb (GDBusConnection *conn,
                const gchar *name,
                gpointer user_data)
{
  GtkAppChooserOnlinePk *self = user_data;

  /* just return */
  g_simple_async_result_set_op_res_gboolean (self->priv->init_result, FALSE);
  g_simple_async_result_complete (self->priv->init_result);

  g_bus_unwatch_name (self->priv->watch_id);

  g_clear_object (&self->priv->init_result);
}

static gboolean
app_chooser_online_pk_init_finish (GAsyncInitable *init,
                                   GAsyncResult *res,
                                   GError **error)
{
  return g_simple_async_result_get_op_res_gboolean (G_SIMPLE_ASYNC_RESULT (res));
}

static void
app_chooser_online_pk_init_async (GAsyncInitable *init,
                                  int io_priority,
                                  GCancellable *cancellable,
                                  GAsyncReadyCallback callback,
                                  gpointer user_data)
{
  GtkAppChooserOnlinePk *self = GTK_APP_CHOOSER_ONLINE_PK (init);

  self->priv->init_result = g_simple_async_result_new (G_OBJECT (self),
                                                       callback, user_data,
                                                       _gtk_app_chooser_online_get_default_async);

  self->priv->watch_id =
    g_bus_watch_name (G_BUS_TYPE_SESSION,
                      "org.freedesktop.PackageKit",
                      G_BUS_NAME_WATCHER_FLAGS_AUTO_START,
                      pk_appeared_cb,
                      pk_vanished_cb,
                      self,
                      NULL);
}

static void
app_chooser_online_pk_async_initable_init (GAsyncInitableIface *iface)
{
  iface->init_async = app_chooser_online_pk_init_async;
  iface->init_finish = app_chooser_online_pk_init_finish;
}