fixed notification in dbus
[livewp] / applet / src / livewp-dbus.c
1 /* vim: set sw=4 ts=4 et: */
2 /*
3  * This file is part of Live Wallpaper (livewp)
4  * 
5  * Copyright (C) 2010 Vlad Vasiliev
6  * Copyright (C) 2010 Tanya Makova
7  *       for the code
8  * 
9  * This software is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1 of
12  * the License, or (at your option) any later version.
13  * 
14  * This software is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  * 
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this software; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22  * 02110-1301 USA
23 */
24
25 /*******************************************************************************/
26 #include "livewp-common.h"
27 #include "livewp-dbus.h"
28 /*******************************************************************************/
29 void
30 livewp_initialize_dbus(Animation_WallpaperPrivate *priv){
31     gchar       *filter_string;
32     DBusError   error;
33
34     dbus_error_init (&error);
35     /* Add D-BUS signal handler for 'status_changed' */
36     priv->dbus_conn = (DBusConnection *) osso_get_sys_dbus_connection(priv->osso);
37     priv->dbus_conn_session = (DBusConnection *) osso_get_dbus_connection(priv->osso);
38
39     if (priv->dbus_conn_session){
40         filter_string =
41             g_strdup_printf("type='signal', interface='%s'", LIVEWP_SIGNAL_INTERFACE);
42         dbus_bus_add_match(priv->dbus_conn_session, filter_string, &error);
43         if (dbus_error_is_set(&error)){
44              fprintf(stderr,"dbus_bus_add_match failed: %s", error.message);
45              dbus_error_free(&error);
46         }
47         g_free(filter_string);
48         /* add the callback */
49         dbus_connection_add_filter(priv->dbus_conn_session,
50                                    (DBusHandleMessageFunction)get_livewp_signal_cb,
51                                    priv, NULL);
52     }
53 }
54 /*******************************************************************************/
55 void
56 livewp_deinitialize_dbus(Animation_WallpaperPrivate *priv){
57
58     gchar       *filter_string;
59     DBusError   error;
60
61     if (priv->dbus_conn_session){
62         filter_string =
63                 g_strdup_printf("type='signal', interface='%s'", LIVEWP_SIGNAL_INTERFACE);
64
65         dbus_error_init (&error);
66         dbus_bus_remove_match(priv->dbus_conn_session, filter_string, &error);
67         if (!dbus_error_is_set(&error)){
68             dbus_connection_remove_filter(priv->dbus_conn_session,
69                                           (DBusHandleMessageFunction)get_livewp_signal_cb, 
70                                           NULL);
71         }else{
72       
73             fprintf(stderr,"dbus_bus_add_match failed: %s", error.message);
74             dbus_error_free(&error);
75         }
76
77          g_free(filter_string);
78     }
79
80 }
81 /*******************************************************************************/
82 void
83 send_dbus_signal (Animation_WallpaperPrivate *priv,
84                   const gchar *interface,
85                   const gchar *path,
86                   const gchar *member)
87 {
88   gboolean       success;
89   
90   DBusMessage *message = dbus_message_new (DBUS_MESSAGE_TYPE_SIGNAL);
91   dbus_message_set_interface (message, interface);
92   dbus_message_set_path (message, path);
93   dbus_message_set_member (message, member);
94   success = dbus_connection_send (priv->dbus_conn_session, message, NULL);
95   dbus_message_unref (message);
96   
97   fprintf (stderr, "%s '%s' message.\n",
98                                  success ? "Sent" : "Failed to send",
99                                  member);
100
101 }
102
103 /*******************************************************************************/
104 DBusHandlerResult
105 get_livewp_signal_cb(DBusConnection *conn, DBusMessage *msg, Animation_WallpaperPrivate *priv){
106
107 #ifdef DEBUGFUNCTIONCALL
108     START_FUNCTION;
109 #endif
110
111     //fprintf (stderr, "PATH11111111111111111111 %s %s %s\n",   dbus_message_get_path(msg),   dbus_message_get_interface (msg), dbus_message_get_member (msg));
112     if (dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_RELOAD_CONFIG)){
113         if(read_config(priv)){
114                 fprintf(stderr, "\nCan not read config file.\n");
115         }else{
116             read_config(priv);
117 #if  !(defined APPLICATION || defined CONTROLPANEL)
118             reload_scene(priv->desktop_plugin);
119 #endif
120         }
121     }
122     if (dbus_message_is_signal(msg, NOTIFY_SIGNAL_INTERFACE, NOTIFY_MEMBER) 
123         || dbus_message_is_signal(msg, CLOSENOTIFY_SIGNAL_INTERFACE, CLOSENOTIFY_MEMBER) ){
124         //fprintf(stderr,"read notifications.db\n");    
125         read_notification(priv->desktop_plugin);
126     }
127     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
128 }
129