Refine error handling and fix some indentation.
[mmediamanager.git] / src / mm-dbus-manager.c
blob645b7fa74b7e8131267994a0eada24fd6d677e6d
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 <stdlib.h>
22 #include <glib.h>
23 #include <glib-object.h>
24 #include <dbus/dbus-glib.h>
25 #include <dbus/dbus-glib-bindings.h>
26 #include "mm-dbus-manager.h"
27 #include "mm-dbus-manager-type-builtins.h"
29 gboolean mm_dbus_manager_register_app (MMDBusManager *manager, char *path, GError **error);
30 gboolean mm_dbus_manager_get_registered_apps (MMDBusManager *manager, char ***app_paths,
31 GError **error);
33 #include "mm-dbus-manager-server-bindings.h"
35 #define MM_DBUS_MANAGER_GET_PRIVATE(o) \
36 (G_TYPE_INSTANCE_GET_PRIVATE ((o), MM_TYPE_DBUS_MANAGER, MMDBusManagerDetails))
38 struct _MMDBusManagerDetails {
39 GPtrArray *application_paths;
40 DBusGProxy *bus_proxy;
41 DBusGConnection *connection;
44 G_DEFINE_TYPE (MMDBusManager, mm_dbus_manager, G_TYPE_OBJECT);
46 static void
47 mm_dbus_manager_finalize (GObject *o)
49 MMDBusManager *m = MM_DBUS_MANAGER (o);
51 g_ptr_array_foreach (m->details->application_paths,
52 (GFunc) g_free,
53 NULL);
54 g_ptr_array_free (m->details->application_paths, TRUE);
56 if (m->details->bus_proxy) {
57 g_object_unref (m->details->bus_proxy);
60 G_OBJECT_CLASS (mm_dbus_manager_parent_class)->finalize (o);
63 static void
64 mm_dbus_manager_class_init (MMDBusManagerClass *klass)
66 GObjectClass *object_class = G_OBJECT_CLASS (klass);
68 object_class->finalize = mm_dbus_manager_finalize;
70 dbus_g_object_type_install_info (MM_TYPE_DBUS_MANAGER,
71 &dbus_glib_mm_dbus_manager_object_info);
72 dbus_g_error_domain_register (MM_DBUS_ERROR_QUARK, NULL, MM_TYPE_MD_BUS_ERROR_ENUM);
74 g_type_class_add_private (klass, sizeof (MMDBusManagerDetails));
77 static void
78 register_on_the_bus (MMDBusManager *manager)
80 GError *error = NULL;
81 MMDBusManagerDetails *details = manager->details;
83 details->connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
84 if (!details->connection) {
85 g_critical ("Error getting a connection to the session bus: %s",
86 error->message);
87 g_error_free (error);
88 exit (1);
91 details->bus_proxy = dbus_g_proxy_new_for_name (details->connection,
92 DBUS_SERVICE_DBUS,
93 DBUS_PATH_DBUS,
94 DBUS_INTERFACE_DBUS);
96 /* register the object */
97 dbus_g_connection_register_g_object (details->connection,
98 MM_DBUS_MANAGER_PATH,
99 G_OBJECT (manager));
102 static void
103 mm_dbus_manager_init (MMDBusManager *manager)
105 MMDBusManagerDetails *details = manager->details = MM_DBUS_MANAGER_GET_PRIVATE (manager);
107 details->application_paths = g_ptr_array_new ();
108 register_on_the_bus (manager);
111 /* implementation of dbus methods */
113 gboolean
114 mm_dbus_manager_register_app (MMDBusManager *manager, char *path, GError **error)
116 g_return_val_if_fail (MM_IS_DBUS_MANAGER (manager), FALSE);
118 if (path == NULL) {
119 /* set the error, the path can't be NULL */
120 g_set_error (error, MM_DBUS_ERROR_QUARK,
121 MM_DBUS_ERROR_NULL_ATTRIBUTE,
122 "Error: can't register an app with NULL path");
123 return FALSE;
126 g_ptr_array_add (manager->details->application_paths,
127 g_strdup (path));
128 return TRUE;
131 gboolean
132 mm_dbus_manager_get_registered_apps (MMDBusManager *manager,
133 char ***app_paths,
134 GError **error)
136 int index;
137 int size = manager->details->application_paths->len;
138 g_return_val_if_fail (MM_IS_DBUS_MANAGER (manager), FALSE);
140 if (app_paths == NULL) {
141 /* set the error, the client is doing something wrong */
142 g_set_error (error, MM_DBUS_ERROR_QUARK,
143 MM_DBUS_ERROR_NULL_ATTRIBUTE,
144 "Error: pointers passed to GetRegisteredApps can't be NULL");
145 return FALSE;
148 *app_paths = g_new (gchar *, size + 1);
149 (*app_paths) [size] = NULL;
151 for (index = 0; index < size; index++) {
152 (*app_paths) [index] =
153 g_strdup (g_ptr_array_index (manager->details->application_paths, index));
156 return TRUE;