next step in external themes
[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 #if 0
53        filter_string =
54             g_strdup_printf("type='signal', interface='%s'", NOTIFY_SIGNAL_INTERFACE);
55         dbus_bus_add_match(priv->dbus_conn_session, filter_string, &error);
56         if (dbus_error_is_set(&error)){
57              fprintf(stderr,"dbus_bus_add_match failed: %s", error.message);
58              dbus_error_free(&error);
59         }
60         g_free(filter_string);
61         /* add the callback */
62         dbus_connection_add_filter(priv->dbus_conn_session,
63                                    (DBusHandleMessageFunction)get_livewp_signal_cb,
64                                    priv, NULL);
65 #endif
66
67     }
68 }
69 /*******************************************************************************/
70 void
71 livewp_deinitialize_dbus(Animation_WallpaperPrivate *priv){
72
73     gchar       *filter_string;
74     DBusError   error;
75
76 fprintf(stderr,"dddddddddddddddddddd\n");
77     if (priv->dbus_conn_session){
78         filter_string =
79                 g_strdup_printf("type='signal', interface='%s'", LIVEWP_SIGNAL_INTERFACE);
80
81         dbus_error_init (&error);
82         dbus_bus_remove_match(priv->dbus_conn_session, filter_string, &error);
83         if (!dbus_error_is_set(&error)){
84             dbus_connection_remove_filter(priv->dbus_conn_session,
85                                           (DBusHandleMessageFunction)get_livewp_signal_cb, 
86                                           NULL);
87         }else{
88       
89             fprintf(stderr,"dbus_bus_add_match failed: %s", error.message);
90             dbus_error_free(&error);
91         }
92
93          g_free(filter_string);
94          /*
95          filter_string =
96                 g_strdup_printf("type='signal', interface='%s'", NOTIFY_SIGNAL_INTERFACE);
97
98         dbus_error_init (&error);
99         dbus_bus_remove_match(priv->dbus_conn_session, filter_string, &error);
100         if (!dbus_error_is_set(&error)){
101             dbus_connection_remove_filter(priv->dbus_conn_session,
102                                           (DBusHandleMessageFunction)get_livewp_signal_cb, 
103                                           NULL);
104         }else{
105       
106             fprintf(stderr,"dbus_bus_add_match failed: %s", error.message);
107             dbus_error_free(&error);
108         }
109
110          g_free(filter_string);
111 */
112     }
113
114 }
115 /*******************************************************************************/
116 void
117 send_dbus_signal (Animation_WallpaperPrivate *priv,
118                   const gchar *interface,
119                   const gchar *path,
120                   const gchar *member)
121 {
122   gboolean       success;
123   
124   DBusMessage *message = dbus_message_new (DBUS_MESSAGE_TYPE_SIGNAL);
125   dbus_message_set_interface (message, interface);
126   dbus_message_set_path (message, path);
127   dbus_message_set_member (message, member);
128   success = dbus_connection_send (priv->dbus_conn_session, message, NULL);
129   dbus_message_unref (message);
130   
131 #if 0
132   fprintf (stderr, "%s '%s' message.\n",
133                                  success ? "Sent" : "Failed to send",
134                                  member);
135 #endif
136
137 }
138
139 /*******************************************************************************/
140 DBusHandlerResult
141 get_livewp_signal_cb(DBusConnection *conn, DBusMessage *msg, Animation_WallpaperPrivate *priv)
142 {
143
144 #ifdef DEBUGFUNCTIONCALL
145     START_FUNCTION;
146 #endif
147
148 #if 0
149 #if  defined APPLICATION 
150     fprintf (stderr, "APPLICATION PATH11111111111111111111 %s %s %s\n",   dbus_message_get_path(msg),   dbus_message_get_interface (msg), dbus_message_get_member (msg)); 
151 #endif
152
153 #if  defined CONTROLPANEL
154     fprintf (stderr, "APPLICATION PATH11111111111111111111 %s %s %s\n",   dbus_message_get_path(msg),   dbus_message_get_interface (msg), dbus_message_get_member (msg)); 
155 #endif
156
157 #if !(defined CONTROLPANEL || defined APPLICATION) 
158     fprintf (stderr, "APPLET PATH11111111111111111111 %s %s %s\n",   dbus_message_get_path(msg),   dbus_message_get_interface (msg), dbus_message_get_member (msg)); 
159 #endif
160 #endif
161     if (!priv)
162         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
163
164     if (dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_RELOAD_CONFIG)){
165         if(read_config(priv)){
166                 fprintf(stderr, "\nCan not read config file.\n");
167         }else{
168             read_config(priv);
169 #if  defined APPLICATION 
170             reload_scene(priv->desktop_plugin);
171 #endif
172         }
173     }
174 #if  defined APPLICATION
175     /* QUIT FROM APPLICATION */
176     if ((priv->view == 1 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_STOP_LIVEBG_ON_VIEW1))||
177         (priv->view == 2 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_STOP_LIVEBG_ON_VIEW2))||   
178         (priv->view == 3 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_STOP_LIVEBG_ON_VIEW3))||   
179         (priv->view == 4 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_STOP_LIVEBG_ON_VIEW4))){
180
181         quit_from_program(priv);
182     }
183     /* PLAY SCENE */
184     if ((priv->view == 1 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_PLAY_LIVEBG_ON_VIEW1))||
185         (priv->view == 2 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_PLAY_LIVEBG_ON_VIEW2))||   
186         (priv->view == 3 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_PLAY_LIVEBG_ON_VIEW3))||   
187         (priv->view == 4 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_PLAY_LIVEBG_ON_VIEW4))){
188         /* fprintf(stderr, "Play scene visible %i\n", priv->visible); */
189         if (priv->visible != TRUE){
190             priv->visible = TRUE;
191             view_state_changed(priv);
192         }
193     }
194     /* PAUSE SCENE */
195     if ((priv->view == 1 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_PAUSE_LIVEBG_ON_VIEW1))||
196         (priv->view == 2 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_PAUSE_LIVEBG_ON_VIEW2))||   
197         (priv->view == 3 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_PAUSE_LIVEBG_ON_VIEW3))||   
198         (priv->view == 4 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_PAUSE_LIVEBG_ON_VIEW4))){
199          /* fprintf(stderr, "Pause scene visible %i\n", priv->visible); */
200          if (priv->visible != FALSE){
201             priv->visible = FALSE;
202             view_state_changed(priv);
203          }
204     }
205     /* ALIVE APPLICATION */
206     if ((priv->view == 1 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_ALIVE_LIVEBG_ON_VIEW1))||
207         (priv->view == 2 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_ALIVE_LIVEBG_ON_VIEW2))||   
208         (priv->view == 3 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_ALIVE_LIVEBG_ON_VIEW3))||   
209         (priv->view == 4 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_ALIVE_LIVEBG_ON_VIEW4))){
210
211         priv->last_alive_event = time(NULL);
212     }
213
214     /* Check notification of mail, sms, call */
215   //fprintf(stderr, "Type %i\n", dbus_message_get_type (msg));
216     if (dbus_message_is_method_call(msg, NOTIFY_SIGNAL_INTERFACE, NOTIFY_MEMBER)){
217         //fprintf(stderr,"read notifications.db notify\n");    
218         //read_notification(priv->desktop_plugin);
219         priv->scene->notification = time(NULL) + 5;
220     }
221     if (dbus_message_is_signal(msg, NOTIFY_SIGNAL_INTERFACE, CLOSENOTIFY_MEMBER)){
222         //fprintf(stderr,"read notifications.db close notify\n");    
223         //read_notification(priv->desktop_plugin);
224         priv->scene->notification = time(NULL) + 5;
225     }
226 #endif
227
228     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
229 }
230