Tizen 2.1 base
[framework/multimedia/media-server.git] / lib / media-util-noti.c
1 /*
2  *  Media Utility
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 /**
23  * This file defines api utilities of contents manager engines.
24  *
25  * @file                media-util-noti.c
26  * @author      Yong Yeon Kim(yy9875.kim@samsung.com)
27  * @version     1.0
28  * @brief
29  */
30
31 #include <sys/errno.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <string.h>
35 #include <glib.h>
36 #include <dbus/dbus-glib.h>
37 #include <dbus/dbus.h>
38 #include <dbus/dbus-glib-lowlevel.h>
39
40 #include "media-util-internal.h"
41 #include "media-util-dbg.h"
42 #include "media-util.h"
43
44 static DBusHandlerResult
45 __message_filter (DBusConnection *connection, DBusMessage *message, void *user_data)
46 {
47         db_update_cb user_cb = user_data;
48
49         /* A Ping signal on the com.burtonini.dbus.Signal interface */
50         if (dbus_message_is_signal (message, MS_MEDIA_DBUS_INTERFACE, MS_MEDIA_DBUS_NAME)) {
51                 DBusError error;
52                 dbus_uint16_t  noti_type;
53
54                 dbus_error_init (&error);
55                 if (dbus_message_get_args (message, &error, DBUS_TYPE_UINT16, &noti_type, DBUS_TYPE_INVALID)) {
56                         MSAPI_DBG("noti type: %d\n", noti_type);
57                         user_cb();
58                 } else {
59                         MSAPI_DBG("messgae received, but error getting message: %s\n", error.message);
60                         dbus_error_free (&error);
61                 }
62                 return DBUS_HANDLER_RESULT_HANDLED;
63         }
64         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
65 }
66
67 int media_db_update_subscribe(db_update_cb user_cb)
68 {
69         DBusConnection *bus;
70         DBusError error;
71
72         dbus_g_thread_init();
73
74         dbus_error_init (&error);
75
76         bus = dbus_bus_get (DBUS_BUS_SESSION, &error);
77         if (!bus) {
78                 MSAPI_DBG ("Failed to connect to the D-BUS daemon: %s", error.message);
79                 dbus_error_free (&error);
80                 return MS_MEDIA_ERR_DBUS_GET;
81         }
82
83         dbus_connection_setup_with_g_main (bus, NULL);
84
85         /* listening to messages from all objects as no path is specified */
86         dbus_bus_add_match (bus, MS_MEDIA_DBUS_MATCH_RULE, &error);
87         if( !dbus_connection_add_filter (bus, __message_filter, user_cb, NULL))
88                 return MS_MEDIA_ERR_DBUS_ADD_FILTER;
89
90         return MS_MEDIA_ERR_NONE;
91 }
92