b294644a07d7b856b849ddb3a278c5cfe4403807
[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   *tmp;
32     gchar       *filter_string;
33     DBusError   error;
34
35 //    if(!priv->dbus_is_initialize){
36         dbus_error_init (&error);
37         /* Add D-BUS signal handler for 'status_changed' */
38 #if !defined APPLICATION
39         priv->dbus_conn = (DBusConnection *) osso_get_sys_dbus_connection(priv->osso);
40         priv->dbus_conn_session = (DBusConnection *) osso_get_dbus_connection(priv->osso);
41 #else
42         priv->dbus_conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
43         priv->dbus_conn_session = dbus_bus_get(DBUS_BUS_SESSION, NULL);
44 #endif
45
46         if (priv->dbus_conn_session){
47             filter_string =
48                 g_strdup_printf("type='signal', interface='%s'", LIVEWP_SIGNAL_INTERFACE);
49             dbus_bus_add_match(priv->dbus_conn_session, filter_string, &error);
50             if (dbus_error_is_set(&error)){
51                  fprintf(stderr,"dbus_bus_add_match failed: %s", error.message);
52                  dbus_error_free(&error);
53             }
54             g_free(filter_string);
55             /* add the callback */
56             dbus_connection_add_filter(priv->dbus_conn_session,
57                                        get_livewp_signal_cb,
58                                        NULL, NULL);
59         }
60
61   // }
62
63 }
64 /*******************************************************************************/
65 void
66 livewp_deinitialize_dbus(Animation_WallpaperPrivate *priv){
67 #ifdef DEBUGFUNCTIONCALL
68     START_FUNCTION;
69 #endif
70     gchar       *filter_string;
71     DBusError   error;
72
73     if (priv->dbus_conn){
74 #if defined APPLICATION
75          dbus_connection_close(priv->dbus_conn);
76          dbus_connection_unref(priv->dbus_conn);
77 #endif
78     }
79     if (priv->dbus_conn_session){
80         filter_string =
81                 g_strdup_printf("type='signal', interface='%s'", LIVEWP_SIGNAL_INTERFACE);
82
83         dbus_error_init (&error);
84         dbus_bus_remove_match(priv->dbus_conn_session, filter_string, &error);
85         if (!dbus_error_is_set(&error)){
86             dbus_connection_remove_filter(priv->dbus_conn_session,
87                                           (DBusHandleMessageFunction)get_livewp_signal_cb, 
88                                           NULL);
89         }else{
90       
91             fprintf(stderr,"dbus_bus_add_match failed: %s", error.message);
92             dbus_error_free(&error);
93         }
94
95          g_free(filter_string);
96     }
97
98 }
99 /*******************************************************************************/
100 void
101 send_dbus_signal (Animation_WallpaperPrivate *priv,
102                   const gchar *interface,
103                   const gchar *path,
104                   const gchar *member)
105 {
106   gboolean       success;
107   
108   DBusMessage *message = dbus_message_new (DBUS_MESSAGE_TYPE_SIGNAL);
109   dbus_message_set_interface (message, interface);
110   dbus_message_set_path (message, path);
111   dbus_message_set_member (message, member);
112   success = dbus_connection_send (priv->dbus_conn_session, message, NULL);
113   dbus_message_unref (message);
114   
115   fprintf (stderr, "%s '%s' message.\n",
116                                  success ? "Sent" : "Failed to send",
117                                  member);
118
119 }
120
121 /*******************************************************************************/
122 DBusHandlerResult
123 get_livewp_signal_cb(DBusConnection *conn, DBusMessage *msg, Animation_WallpaperPrivate *priv){
124
125 #ifdef DEBUGFUNCTIONCALL
126     START_FUNCTION;
127 #endif
128
129 #if defined APPLICATION
130     fprintf(stderr,"Application\n");
131 #else 
132     fprintf(stderr,"Plugin\n");
133 #endif
134
135     if (dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_RELOAD_CONFIG)){
136         if(read_config(priv)){
137                 fprintf(stderr, "\nCan not read config file.\n");
138         }else{
139             read_config(priv);
140 #ifndef APPLICATION
141             reload_scene(priv->desktop_plugin);
142 #endif
143         }
144     }
145 #ifndef APPLICATION
146 //    if (dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_RELOAD_PLUGIN))
147 //        reload_livewp_plugin();
148 #endif
149     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
150 }
151 /*******************************************************************************/
152