1 // DBusProxy-gio.cc --- support for simple DBUS method execution
2 //
3 // Copyright (C) 2014 Mateusz Jończyk <mat.jonczyk@o2.pl>
4 // All rights reserved.
5 // It is possible that it uses some code and ideas from the KShutdown utility:
6 //              file src/actions/lock.cpp
7 //              Copyright (C) 2009  Konrad Twardowski
8 //
9 // This program is free software: you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation, either version 3 of the License, or
12 // (at your option) any later version.
13 //
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 //
22 
23 #include "DBusProxy-gio.hh"
24 #include <iostream>
25 
26 
27 
init_with_connection(GDBusConnection * connection,const char * name,const char * object_path,const char * interface_name,GDBusProxyFlags flags_in)28   bool DBusProxy::init_with_connection(GDBusConnection *connection, const char *name, const char *object_path,
29               const char *interface_name, GDBusProxyFlags flags_in)
30   {
31     TRACE_ENTER_MSG("DBus_proxy::init_with_connection", name);
32     this->flags = flags_in;
33     proxy = g_dbus_proxy_new_sync(connection,
34                                        flags,
35                                        NULL,
36                                        name,
37                                        object_path,
38                                        interface_name,
39                                        NULL,
40                                        &error);
41 
42     if (error != NULL)
43       {
44         TRACE_MSG("Error: " << error->message);
45         return false;
46       }
47     TRACE_EXIT();
48     return true;
49   }
50 
init(GBusType bus_type,const char * name,const char * object_path,const char * interface_name,GDBusProxyFlags flags_in)51   bool DBusProxy::init(GBusType bus_type, const char *name, const char *object_path,
52               const char *interface_name, GDBusProxyFlags flags_in)
53   {
54     TRACE_ENTER_MSG("DBus_proxy::init", name);
55     this->flags = flags_in;
56     error = NULL;
57     proxy = g_dbus_proxy_new_for_bus_sync(bus_type,
58                                                flags,
59                                                NULL,
60                                                name,
61                                                object_path,
62                                                interface_name,
63                                                NULL,
64                                                &error);
65 
66     if (error != NULL)
67       {
68         TRACE_MSG("Error: " << error->message);
69         return false;
70       }
71     TRACE_EXIT();
72     return true;
73   }
74 
75 
76   //Consumes (=deletes) method_parameters if it is floating
77   //method_result may be null, in this case the result of the method is ignored
call_method(const char * method_name,GVariant * method_parameters,GVariant ** method_result)78   bool DBusProxy::call_method(const char *method_name, GVariant *method_parameters, GVariant **method_result)
79   {
80     TRACE_ENTER_MSG("DBus_proxy::call_method", method_name);
81     if (proxy == NULL)
82       return false;
83 
84     if (error != NULL)
85       {
86         g_error_free(error);
87         error = NULL;
88       }
89 
90     GVariant *result = g_dbus_proxy_call_sync(proxy, method_name,
91                                               method_parameters,
92                                               G_DBUS_CALL_FLAGS_NONE,
93                                               -1,
94                                               NULL,
95                                               &error);
96 
97     if (method_result == NULL)
98       {
99         if (result != NULL)
100           {
101             g_variant_unref(result);
102             result = NULL;
103           }
104       }
105     else
106       {
107         if (error != NULL)
108           {
109             *method_result = NULL;
110             if (result != NULL)
111               {
112                 g_variant_unref(result);
113                 result = NULL;
114               }
115           }
116         else
117           {
118             *method_result = result;
119           }
120       }
121 
122     if (error != NULL)
123       {
124         TRACE_RETURN(error->message);
125         return false;
126       }
127 
128     TRACE_EXIT();
129     return true;
130   }
131 
132   //Consumes (=deletes) method_parameters if it is floating
call_method_asynch_no_result(const char * method_name,GVariant * method_parameters)133   bool DBusProxy::call_method_asynch_no_result(const char *method_name, GVariant *method_parameters)
134   {
135     TRACE_ENTER_MSG("DBus_proxy::call_method_asynch_no_result", method_name);
136     if (proxy == NULL)
137       return false;
138 
139     if (error != NULL)
140       {
141         g_error_free(error);
142         error = NULL;
143       }
144 
145     g_dbus_proxy_call(proxy, method_name,
146                               method_parameters,
147                               G_DBUS_CALL_FLAGS_NONE,
148                               -1,
149                               NULL,
150                               NULL,
151                               NULL);
152 
153     TRACE_EXIT();
154     return true;
155   }
156