Added a first implementation of the DBus manager.
[mmediamanager.git] / src / mm-dbus-manager.c
blob99841490eb562a85e4ab642b09ba3266f19aeee2
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 <glib.h>
22 #include <glib-object.h>
23 #include <dbus/dbus-glib.h>
24 #include <dbus/dbus-glib-bindings.h>
25 #include "mm-dbus-manager.h"
27 gboolean server_register_app (MMDBusManager *manager, char *path, GError **error);
29 #include "mm-dbus-manager-server-bindings.h"
31 #define MM_DBUS_MANAGER_GET_PRIVATE(o) \
32 (G_TYPE_INSTANCE_GET_PRIVATE ((o), MM_TYPE_DBUS_MANAGER, MMDBusManagerDetails))
34 /* FIXME: move this to a separate helper program, so that the manager can
35 * be a real session singleton.
38 struct _MMDBusManagerDetails {
39 GPtrArray *application_paths;
42 static MMDBusManager *dbus_manager = NULL;
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_OBJECT_CLASS (mm_dbus_manager_parent_class)->finalize (o);
54 static void
55 mm_dbus_manager_class_init (MMDBusManagerClass *klass)
57 GObjectClass *object_class = G_OBJECT_CLASS (klass);
58 GError *error = NULL;
60 object_class->finalize = mm_dbus_manager_finalize;
62 /* get a connection to the session bus */
63 klass->connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
64 if (!klass->connection) {
65 g_warning ("Unable to connect to the session bus: %s", error->message);
66 g_error_free (error);
67 } else {
68 dbus_g_object_type_install_info (MM_TYPE_DBUS_MANAGER,
69 &dbus_glib_mm_dbus_manager_object_info);
72 g_type_class_add_private (klass, sizeof (MMDBusManagerDetails));
75 static void
76 register_on_the_bus (MMDBusManager *manager)
78 GError *error = NULL;
79 DBusGProxy *driver_proxy;
80 guint request_ret;
81 MMDBusManagerClass *klass = MM_DBUS_MANAGER_GET_CLASS (manager);
83 /* register the object */
84 dbus_g_connection_register_g_object (klass->connection,
85 "/org/gnome/MediaManager",
86 G_OBJECT (manager));
87 /* register the service name */
88 driver_proxy = dbus_g_proxy_new_for_name (klass->connection,
89 DBUS_SERVICE_DBUS,
90 DBUS_PATH_DBUS,
91 DBUS_INTERFACE_DBUS);
92 if (!org_freedesktop_DBus_request_name (driver_proxy,
93 "org.gnome.MediaManager",
94 DBUS_NAME_FLAG_DO_NOT_QUEUE,
95 &request_ret,
96 &error)) {
97 g_warning ("RequestName failed: %s", error->message);
98 g_error_free (error);
101 g_object_unref (driver_proxy);
104 static void
105 mm_dbus_manager_init (MMDBusManager *manager)
107 MMDBusManagerDetails *details = manager->details = MM_DBUS_MANAGER_GET_PRIVATE (manager);
109 details->application_paths = g_ptr_array_new ();
110 register_on_the_bus (manager);
113 /* implementation of dbus methods */
115 gboolean
116 server_register_app (MMDBusManager *manager, char *path, GError **error)
118 if (!path) {
119 /* set the error, the path can't be NULL */
120 g_set_error (error, MM_DBUS_ERROR_QUARK,
121 MM_DBUS_ERROR_NULL_PATH,
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 /* public methods */
133 MMDBusManager *
134 mm_dbus_manager_get (void)
136 if (!dbus_manager) {
137 dbus_manager = g_object_new (MM_TYPE_DBUS_MANAGER, NULL);
139 return dbus_manager;