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