Tizen 2.1 base
[framework/multimedia/media-server.git] / common / media-server-dbus.c
1 /*
2  *  Media Server
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Yong Yeon Kim <yy9875.kim@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <glib.h>
23 #include <dbus/dbus-glib.h>
24 #include <dbus/dbus.h>
25 #include <dbus/dbus-glib-lowlevel.h>
26
27 #include "media-server-dbg.h"
28 #include "media-server-types.h"
29 #include "media-server-dbus.h"
30
31 void ms_dbus_init(void)
32 {
33         DBusConnection *bus;
34         DBusError error;
35
36         /* Get a connection to the session bus */
37         dbus_error_init (&error);
38         bus = dbus_bus_get (DBUS_BUS_SESSION, &error);
39         if (!bus) {
40                 MS_DBG ("Failed to connect to the D-BUS daemon: %s", error.message);
41                 dbus_error_free (&error);
42                 return;
43         }
44
45         /* Set up this connection to work in a GLib event loop */
46         dbus_connection_setup_with_g_main (bus, NULL);
47 }
48
49 gboolean ms_dbus_send_noti(ms_dbus_noti_type_t data)
50 {
51         MS_DBG("");
52         DBusMessage *message;
53         DBusConnection *bus;
54         DBusError error;
55         dbus_uint16_t noti_type = data;
56
57         /* Get a connection to the session bus */
58         dbus_error_init (&error);
59         bus = dbus_bus_get (DBUS_BUS_SESSION, &error);
60         if (!bus) {
61                 MS_DBG ("Failed to connect to the D-BUS daemon: %s", error.message);
62                 dbus_error_free (&error);
63                 return false;
64         }
65
66         /* Create a new signal on the "MS_DBUS_INTERFACE" interface,
67         * from the object "MS_DBUS_PATH". */
68         message = dbus_message_new_signal (MS_DBUS_PATH, MS_DBUS_INTERFACE, MS_DBUS_NAME);
69
70         /* Append the notification type to the signal */
71         dbus_message_append_args (message, DBUS_TYPE_UINT16, &noti_type, DBUS_TYPE_INVALID);
72
73         /* Send the signal */
74         dbus_connection_send (bus, message, NULL);
75
76         /* Free the signal now we have finished with it */
77         dbus_message_unref (message);
78
79         /* Return TRUE to tell the event loop we want to be called again */
80         return true;
81 }