Add a first implementation of the DBus application.
[mmediamanager.git] / src / mm-dbus-application.c
blobae13df4223186164ae6393fabd7f68aa4e3663cf
1 /* MManager - a Desktop wide manager for multimedia applications.
3 * Copyright (C) 2008 Cosimo Cecchi <cosimoc@gnome.org>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
21 #include "mm-application.h"
22 #include "mm-dbus-application.h"
23 #include "mm-category.h"
24 #include <glib.h>
25 #include <glib-object.h>
26 #include <dbus/dbus-glib.h>
28 #define MM_DBUS_APPLICATION_GET_PRIVATE(o) \
29 (G_TYPE_INSTANCE_GET_PRIVATE ((o), MM_TYPE_DBUS_APPLICATION, MMDBusApplicationDetails))
31 struct _MMDBusApplicationDetails {
32 char *path;
35 G_DEFINE_TYPE (MMDBusApplication, mm_dbus_application, MM_TYPE_APPLICATION);
37 static void
38 create_and_append_category (MMApplication *app,
39 GList *categories,
40 const char *name,
41 const char *icon_name)
43 MMCategory *cat;
45 cat = mm_category_new (app, name, g_themed_icon_new (icon_name));
46 categories = g_list_prepend (categories, cat);
49 static GList *
50 mm_dbus_application_get_categories (MMApplication *app)
52 DBusGProxy *app_proxy;
53 DBusGConnection *connection;
54 char **cat_names, **cat_icon_names;
55 gboolean ret;
56 int idx;
57 GList *categories = NULL;
58 GError *error = NULL;
59 const char *remote_name = mm_application_get_id (app);
61 connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
62 if (!connection) {
63 g_warning ("Unable to get a connection to the session bus: %s", error->message);
64 g_error_free (error);
65 return NULL;
68 app_proxy = dbus_g_proxy_new_for_name (connection,
69 remote_name,
70 MM_DBUS_APPLICATION (app)->details->path,
71 "org.gnome.MediaManager.Application");
72 ret = dbus_g_proxy_call (app_proxy,
73 "GetCategories",
74 &error,
75 G_TYPE_INVALID,
76 G_TYPE_STRV,
77 &cat_names,
78 G_TYPE_STRV,
79 &cat_icon_names,
80 G_TYPE_INVALID);
81 if (!ret) {
82 g_warning ("Error while calling GetCategories on %s: %s", remote_name,
83 error->message);
84 g_error_free (error);
85 goto out;
88 if (G_N_ELEMENTS (cat_names) != G_N_ELEMENTS (cat_icon_names)) {
89 /* something must be broken, return */
90 g_warning ("Error in GetCategories on %s: the length of the returned "
91 "arrays do not match", remote_name);
92 goto free;
95 /* create the category objects */
96 for (idx = 0; idx < G_N_ELEMENTS (cat_names); idx++) {
97 create_and_append_category (app, categories, cat_names[idx],
98 cat_icon_names[idx]);
101 free:
102 g_strfreev (cat_names);
103 g_strfreev (cat_icon_names);
105 out:
106 g_object_unref (app_proxy);
107 return categories;
110 static void
111 mm_dbus_application_finalize (GObject *o)
113 g_free (MM_DBUS_APPLICATION (o)->details->path);
115 G_OBJECT_CLASS (mm_dbus_application_parent_class)->finalize (o);
118 static void
119 mm_dbus_application_class_init (MMDBusApplicationClass *klass)
121 MMApplicationClass *app_class = MM_APPLICATION_CLASS (klass);
123 app_class->get_categories = mm_dbus_application_get_categories;
124 G_OBJECT_CLASS (klass)->finalize = mm_dbus_application_finalize;
126 g_type_class_add_private (klass, sizeof (MMDBusApplicationDetails));
129 static void
130 mm_dbus_application_init (MMDBusApplication *app)
132 MMDBusApplicationDetails *details = app->details = MM_DBUS_APPLICATION_GET_PRIVATE (app);
133 details->path = NULL;
136 /* public methods */
138 MMApplication *
139 mm_dbus_application_new (const char *desktop_id,
140 MMApplicationType supported_type,
141 const char *name,
142 const char *path)
144 MMApplication *app;
146 app = MM_APPLICATION (g_object_new (MM_TYPE_DBUS_APPLICATION, NULL));
147 mm_application_set_attributes (app, desktop_id, supported_type, name);
148 MM_DBUS_APPLICATION (app)->details->path = g_strdup (path);
150 return app;