Split errors in mm-error.h and GErrorize a bit the GetCategories
[mmediamanager.git] / src / mm-dbus-application.c
blobd22a4e4cabbb6b45179626977b9a6eb133a470fb
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 */
148 MMApplication *
149 mm_dbus_application_new (const char *desktop_id,
150 MMApplicationType supported_type,
151 const char *name,
152 const char *path)
154 MMApplication *app;
156 app = MM_APPLICATION (g_object_new (MM_TYPE_DBUS_APPLICATION, NULL));
157 mm_application_set_attributes (app, desktop_id, supported_type, name);
158 MM_DBUS_APPLICATION (app)->details->path = g_strdup (path);
160 return app;
163 const char *
164 mm_dbus_application_get_path (MMDBusApplication *app)
166 g_return_val_if_fail (MM_IS_DBUS_APPLICATION (app), NULL);
168 return app->details->path;