Merge pull request #5039 from CEikermann/patch-1
[vuplus_xbmc] / xbmc / linux / DBusMessage.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20 #include "DBusMessage.h"
21 #ifdef HAS_DBUS
22 #include "utils/log.h"
23 #include "settings/AdvancedSettings.h"
24
25 CDBusMessage::CDBusMessage(const char *destination, const char *object, const char *interface, const char *method)
26 {
27   m_reply = NULL;
28   m_message = dbus_message_new_method_call (destination, object, interface, method);
29   m_haveArgs = false;
30
31   CLog::Log(LOGDEBUG|LOGDBUS, "DBus: Creating message to %s on %s with interface %s and method %s\n", destination, object, interface, method);
32 }
33
34 CDBusMessage::~CDBusMessage()
35 {
36   Close();
37 }
38
39 bool CDBusMessage::AppendObjectPath(const char *object)
40 {
41   PrepareArgument();
42   return dbus_message_iter_append_basic(&m_args, DBUS_TYPE_OBJECT_PATH, &object);
43 }
44
45 bool CDBusMessage::AppendArgument(const char *string)
46 {
47   PrepareArgument();
48   return dbus_message_iter_append_basic(&m_args, DBUS_TYPE_STRING, &string);
49 }
50
51 bool CDBusMessage::AppendArgument(const bool b)
52 {
53   dbus_bool_t arg = (b == true);
54   PrepareArgument();
55   return dbus_message_iter_append_basic(&m_args, DBUS_TYPE_BOOLEAN, &arg);
56 }
57
58 bool CDBusMessage::AppendArgument(const char **arrayString, unsigned int length)
59 {
60   PrepareArgument();
61   DBusMessageIter sub;
62   bool success = dbus_message_iter_open_container(&m_args, DBUS_TYPE_ARRAY, DBUS_TYPE_STRING_AS_STRING, &sub);
63
64   for (unsigned int i = 0; i < length && success; i++)
65     success &= dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &arrayString[i]);
66
67   success &= dbus_message_iter_close_container(&m_args, &sub);
68
69   return success;
70 }
71
72 DBusMessage *CDBusMessage::SendSystem()
73 {
74   return Send(DBUS_BUS_SYSTEM);
75 }
76
77 DBusMessage *CDBusMessage::SendSession()
78 {
79   return Send(DBUS_BUS_SESSION);
80 }
81
82 bool CDBusMessage::SendAsyncSystem()
83 {
84   return SendAsync(DBUS_BUS_SYSTEM);
85 }
86
87 bool CDBusMessage::SendAsyncSession()
88 {
89   return SendAsync(DBUS_BUS_SESSION);
90 }
91
92 DBusMessage *CDBusMessage::Send(DBusBusType type)
93 {
94   DBusError error;
95   dbus_error_init (&error);
96   DBusConnection *con = dbus_bus_get(type, &error);
97
98   DBusMessage *returnMessage = Send(con, &error);
99
100   if (dbus_error_is_set(&error))
101     CLog::Log(LOGERROR, "DBus: Error %s - %s", error.name, error.message);
102
103   dbus_error_free (&error);
104   dbus_connection_unref(con);
105
106   return returnMessage;
107 }
108
109 bool CDBusMessage::SendAsync(DBusBusType type)
110 {
111   DBusError error;
112   dbus_error_init (&error);
113   DBusConnection *con = dbus_bus_get(type, &error);
114
115   bool result;
116   if (con && m_message)
117     result = dbus_connection_send(con, m_message, NULL);
118   else
119     result = false;
120
121   dbus_error_free (&error);
122   dbus_connection_unref(con);
123   return result;
124 }
125
126 DBusMessage *CDBusMessage::Send(DBusConnection *con, DBusError *error)
127 {
128   if (con && m_message)
129   {
130     if (m_reply)
131       dbus_message_unref(m_reply);
132
133     m_reply = dbus_connection_send_with_reply_and_block(con, m_message, -1, error);
134   }
135
136   return m_reply;
137 }
138
139 void CDBusMessage::Close()
140 {
141   if (m_message)
142     dbus_message_unref(m_message);
143
144   if (m_reply)
145     dbus_message_unref(m_reply);
146 }
147
148 void CDBusMessage::PrepareArgument()
149 {
150   if (!m_haveArgs)
151     dbus_message_iter_init_append(m_message, &m_args);
152
153   m_haveArgs = true;
154 }
155 #endif