gtk2/gtk/gtkappchooseronlinepk.c
Matthias Clasen a498d9a9ba Coding style fixups
This commit adjusts the app chooser code to GTK+ coding style:
- line up prototypes
- remove some excess {}
- remove tabs and trailing whitespace
- add docs
2010-11-24 18:32:05 -05:00

196 lines
6.3 KiB
C

/*
* 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);
G_DEFINE_TYPE_WITH_CODE (GtkAppChooserOnlinePk, gtk_app_chooser_online_pk,
G_TYPE_OBJECT,
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 *result;
GtkWindow *parent;
gchar *content_type;
};
static void
gtk_app_chooser_online_pk_finalize (GObject *obj)
{
GtkAppChooserOnlinePk *self = GTK_APP_CHOOSER_ONLINE_PK (obj);
g_free (self->priv->content_type);
g_clear_object (&self->priv->result);
G_OBJECT_CLASS (gtk_app_chooser_online_pk_parent_class)->finalize (obj);
}
static void
gtk_app_chooser_online_pk_class_init (GtkAppChooserOnlinePkClass *klass)
{
GObjectClass *oclass = G_OBJECT_CLASS (klass);
oclass->finalize = gtk_app_chooser_online_pk_finalize;
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);
}
static void
pk_proxy_appeared_cb (GObject *source,
GAsyncResult *res,
gpointer user_data)
{
GtkAppChooserOnlinePk *self = user_data;
GDBusProxy *proxy;
GError *error = NULL;
guint xid = 0;
GdkWindow *window;
const gchar *mime_types[2];
proxy = g_dbus_proxy_new_for_bus_finish (res, &error);
if (error != NULL)
{
g_simple_async_result_set_from_error (self->priv->result, error);
g_error_free (error);
g_simple_async_result_complete (self->priv->result);
return;
}
#ifdef GDK_WINDOWING_X11
window = gtk_widget_get_window (GTK_WIDGET (self->priv->parent));
xid = GDK_WINDOW_XID (window);
#else
xid = 0;
#endif
mime_types[0] = self->priv->content_type;
mime_types[1] = NULL;
g_dbus_proxy_call (proxy,
"InstallMimeTypes",
g_variant_new ("(u^ass)",
xid,
mime_types,
"hide-confirm-search"),
G_DBUS_CALL_FLAGS_NONE,
G_MAXINT, /* no timeout */
NULL,
install_mime_types_ready_cb,
self);
}
static void
pk_search_mime_async (GtkAppChooserOnline *obj,
const gchar *content_type,
GtkWindow *parent,
GAsyncReadyCallback callback,
gpointer user_data)
{
GtkAppChooserOnlinePk *self = GTK_APP_CHOOSER_ONLINE_PK (obj);
self->priv->result = g_simple_async_result_new (G_OBJECT (self),
callback, user_data,
gtk_app_chooser_online_search_for_mimetype_async);
self->priv->parent = parent;
self->priv->content_type = g_strdup (content_type);
g_dbus_proxy_new_for_bus (G_BUS_TYPE_SESSION,
G_DBUS_PROXY_FLAGS_NONE,
NULL,
"org.freedesktop.PackageKit",
"/org/freedesktop/PackageKit",
"org.freedesktop.PackageKit.Modify",
NULL,
pk_proxy_appeared_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;
}