Start documenting objects.
[mmediamanager.git] / libmmanager / mm-dbus-application.c
blob4b31e1271b9caa0546da80dde0aef37f2c46c255
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-dbus-category.h"
24 #include "mm-error.h"
25 #include <glib.h>
26 #include <glib-object.h>
27 #include <dbus/dbus-glib.h>
29 #define MM_DBUS_APPLICATION_GET_PRIVATE(o) \
30 (G_TYPE_INSTANCE_GET_PRIVATE ((o), MM_TYPE_DBUS_APPLICATION, MMDBusApplicationDetails))
32 struct _MMDBusApplicationDetails {
33 char *path;
36 G_DEFINE_TYPE (MMDBusApplication, mm_dbus_application, MM_TYPE_APPLICATION);
38 static void
39 create_and_append_category (MMApplication *app,
40 GList *categories,
41 const char *name,
42 const char *icon_name)
44 MMCategory *cat;
46 cat = mm_dbus_category_new (MM_DBUS_APPLICATION (app), name, g_themed_icon_new (icon_name));
47 categories = g_list_prepend (categories, cat);
50 static GList *
51 mm_dbus_application_get_categories (MMApplication *app, GError **ret_error)
53 DBusGProxy *app_proxy;
54 DBusGConnection *connection;
55 char **cat_names, **cat_icon_names;
56 gboolean ret;
57 int idx;
58 GList *categories = NULL;
59 GError *error = NULL;
60 const char *remote_name = mm_application_get_id (app);
62 connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
63 if (!connection) {
64 g_set_error (ret_error,
65 MM_DBUS_ERROR_QUARK,
66 MM_DBUS_ERROR_BUS_UNAVAILABLE,
67 "Unable to get a connection to the session bus: %s", error->message);
68 g_error_free (error);
69 return NULL;
72 app_proxy = dbus_g_proxy_new_for_name (connection,
73 remote_name,
74 MM_DBUS_APPLICATION (app)->details->path,
75 "org.gnome.MediaManager.Application");
76 ret = dbus_g_proxy_call (app_proxy,
77 "GetCategories",
78 &error,
79 G_TYPE_INVALID,
80 G_TYPE_STRV,
81 &cat_names,
82 G_TYPE_STRV,
83 &cat_icon_names,
84 G_TYPE_INVALID);
85 if (!ret) {
86 g_set_error (ret_error,
87 MM_DBUS_ERROR_QUARK,
88 MM_DBUS_ERROR_REMOTE_METHOD_FAILED,
89 "Error while calling GetCategories on %s: %s", remote_name,
90 error->message);
91 g_error_free (error);
92 goto out;
95 if (G_N_ELEMENTS (cat_names) != G_N_ELEMENTS (cat_icon_names)) {
96 /* something must be broken, return */
97 g_set_error (ret_error,
98 MM_DBUS_ERROR_QUARK,
99 MM_DBUS_ERROR_REMOTE_METHOD_FAILED,
100 "Error in GetCategories on %s: the length of the returned "
101 "arrays do not match", remote_name);
102 goto free;
105 /* create the category objects */
106 for (idx = 0; idx < G_N_ELEMENTS (cat_names); idx++) {
107 create_and_append_category (app, categories, cat_names[idx],
108 cat_icon_names[idx]);
111 free:
112 g_strfreev (cat_names);
113 g_strfreev (cat_icon_names);
115 out:
116 g_object_unref (app_proxy);
117 return categories;
120 static void
121 mm_dbus_application_finalize (GObject *o)
123 g_free (MM_DBUS_APPLICATION (o)->details->path);
125 G_OBJECT_CLASS (mm_dbus_application_parent_class)->finalize (o);
128 static void
129 mm_dbus_application_class_init (MMDBusApplicationClass *klass)
131 MMApplicationClass *app_class = MM_APPLICATION_CLASS (klass);
133 app_class->get_categories = mm_dbus_application_get_categories;
134 G_OBJECT_CLASS (klass)->finalize = mm_dbus_application_finalize;
136 g_type_class_add_private (klass, sizeof (MMDBusApplicationDetails));
139 static void
140 mm_dbus_application_init (MMDBusApplication *app)
142 MMDBusApplicationDetails *details = app->details = MM_DBUS_APPLICATION_GET_PRIVATE (app);
143 details->path = NULL;
146 /* public methods */
149 * mm_dbus_application_new:
150 * @desktop_id: the desktop file id for the application.
151 * @supported_type: the media type supported by the application.
152 * @name: the name of the application. This will be used as unique id.
153 * @path: the path on the session bus of the requesting application.
155 * Builds a new #MMApplication object with the specified properties.
157 * Return value: a #MMApplication object.
160 MMApplication *
161 mm_dbus_application_new (const char *desktop_id,
162 MMApplicationType supported_type,
163 const char *name,
164 const char *path)
166 MMApplication *app;
168 app = MM_APPLICATION (g_object_new (MM_TYPE_DBUS_APPLICATION, NULL));
169 mm_application_set_attributes (app, desktop_id, supported_type, name);
170 MM_DBUS_APPLICATION (app)->details->path = g_strdup (path);
172 return app;
176 * mm_dbus_application_get_path:
177 * @app: a #MMDBusApplication.
179 * Gets the path of the application on the session bus.
181 * Return value: a string.
184 const char *
185 mm_dbus_application_get_path (MMDBusApplication *app)
187 g_return_val_if_fail (MM_IS_DBUS_APPLICATION (app), NULL);
189 return app->details->path;