Keep on documenting objects.
[mmediamanager.git] / src / mm-dbus-manager-main.c
blob702479091255959a74a7bb7e4f8928de94dd9b96
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-lowlevel.h>
25 #include "mm-dbus-manager.h"
27 static DBusGConnection *
28 get_session_bus (void)
30 GError *error;
31 DBusGConnection *bus;
32 DBusConnection *connection;
34 error = NULL;
35 bus = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
36 if (bus == NULL) {
37 g_warning ("Couldn't connect to session bus: %s",
38 error->message);
39 g_error_free (error);
40 goto out;
43 connection = dbus_g_connection_get_connection (bus);
44 dbus_connection_set_exit_on_disconnect (connection, FALSE);
46 out:
47 return bus;
50 static DBusGProxy *
51 get_bus_proxy (DBusGConnection *connection)
53 DBusGProxy *bus_proxy;
55 bus_proxy = dbus_g_proxy_new_for_name (connection,
56 DBUS_SERVICE_DBUS,
57 DBUS_PATH_DBUS,
58 DBUS_INTERFACE_DBUS);
59 return bus_proxy;
62 static gboolean
63 get_name_on_bus (DBusGProxy *proxy)
65 gboolean ret = FALSE;
66 GError *error = NULL;
67 guint result;
68 gboolean res;
70 g_return_val_if_fail (proxy != NULL, FALSE);
72 res = dbus_g_proxy_call (proxy,
73 "RequestName",
74 &error,
75 G_TYPE_STRING, "org.gnome.MediaManager",
76 G_TYPE_UINT, 0,
77 G_TYPE_INVALID,
78 G_TYPE_UINT, &result,
79 G_TYPE_INVALID);
80 if (!res) {
81 g_warning ("Failed to acquire the name on the bus: %s", error->message);
82 g_error_free (error);
84 goto out;
87 if (result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
88 g_warning ("Failed to acquire the name on the bus: %s", error->message);
89 g_error_free (error);
91 goto out;
94 ret = TRUE;
96 out:
97 return ret;
100 static void
101 bus_proxy_destroyed_cb (DBusGProxy *bus_proxy,
102 GMainLoop *mainloop)
104 g_main_loop_quit (mainloop);
107 int main (int argc, char **argv)
109 MMDBusManager *manager;
110 GMainLoop *mainloop;
111 DBusGConnection *connection;
112 DBusGProxy *bus_proxy;
113 int ret = 1;
115 if (!g_thread_supported ()) {
116 g_thread_init (NULL);
118 dbus_g_thread_init ();
119 g_type_init ();
121 connection = get_session_bus ();
122 if (!connection) {
123 goto out;
126 bus_proxy = get_bus_proxy (connection);
127 if (!bus_proxy) {
128 g_warning ("Error creating the bus proxy, exiting");
129 goto out;
132 if (!get_name_on_bus (bus_proxy)) {
133 g_warning ("Error getting the name on the bus, exiting");
134 goto out;
137 /* this creates the manager object and registers it on the session bus */
138 manager = g_object_new (MM_TYPE_DBUS_MANAGER, NULL);
140 if (!manager) {
141 goto out;
144 mainloop = g_main_loop_new (NULL, FALSE);
146 g_signal_connect (bus_proxy, "destroy",
147 G_CALLBACK (bus_proxy_destroyed_cb),
148 mainloop);
150 g_main_loop_run (mainloop);
152 if (manager != NULL) {
153 g_object_unref (manager);
156 g_main_loop_unref (mainloop);
158 ret = 0;
160 out:
161 return ret;