2 * Copyright (C) 2005-2013 Team XBMC
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)
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.
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/>.
20 #include "DBusMessage.h"
22 #include "utils/log.h"
23 #include "settings/AdvancedSettings.h"
25 CDBusMessage::CDBusMessage(const char *destination, const char *object, const char *interface, const char *method)
28 m_message = dbus_message_new_method_call (destination, object, interface, method);
31 CLog::Log(LOGDEBUG|LOGDBUS, "DBus: Creating message to %s on %s with interface %s and method %s\n", destination, object, interface, method);
34 CDBusMessage::~CDBusMessage()
39 bool CDBusMessage::AppendObjectPath(const char *object)
42 return dbus_message_iter_append_basic(&m_args, DBUS_TYPE_OBJECT_PATH, &object);
45 bool CDBusMessage::AppendArgument(const char *string)
48 return dbus_message_iter_append_basic(&m_args, DBUS_TYPE_STRING, &string);
51 bool CDBusMessage::AppendArgument(const bool b)
53 dbus_bool_t arg = (b == true);
55 return dbus_message_iter_append_basic(&m_args, DBUS_TYPE_BOOLEAN, &arg);
58 bool CDBusMessage::AppendArgument(const char **arrayString, unsigned int length)
62 bool success = dbus_message_iter_open_container(&m_args, DBUS_TYPE_ARRAY, DBUS_TYPE_STRING_AS_STRING, &sub);
64 for (unsigned int i = 0; i < length && success; i++)
65 success &= dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &arrayString[i]);
67 success &= dbus_message_iter_close_container(&m_args, &sub);
72 DBusMessage *CDBusMessage::SendSystem()
74 return Send(DBUS_BUS_SYSTEM);
77 DBusMessage *CDBusMessage::SendSession()
79 return Send(DBUS_BUS_SESSION);
82 bool CDBusMessage::SendAsyncSystem()
84 return SendAsync(DBUS_BUS_SYSTEM);
87 bool CDBusMessage::SendAsyncSession()
89 return SendAsync(DBUS_BUS_SESSION);
92 DBusMessage *CDBusMessage::Send(DBusBusType type)
95 dbus_error_init (&error);
96 DBusConnection *con = dbus_bus_get(type, &error);
98 DBusMessage *returnMessage = Send(con, &error);
100 if (dbus_error_is_set(&error))
101 CLog::Log(LOGERROR, "DBus: Error %s - %s", error.name, error.message);
103 dbus_error_free (&error);
104 dbus_connection_unref(con);
106 return returnMessage;
109 bool CDBusMessage::SendAsync(DBusBusType type)
112 dbus_error_init (&error);
113 DBusConnection *con = dbus_bus_get(type, &error);
116 if (con && m_message)
117 result = dbus_connection_send(con, m_message, NULL);
121 dbus_error_free (&error);
122 dbus_connection_unref(con);
126 DBusMessage *CDBusMessage::Send(DBusConnection *con, DBusError *error)
128 if (con && m_message)
131 dbus_message_unref(m_reply);
133 m_reply = dbus_connection_send_with_reply_and_block(con, m_message, -1, error);
139 void CDBusMessage::Close()
142 dbus_message_unref(m_message);
145 dbus_message_unref(m_reply);
148 void CDBusMessage::PrepareArgument()
151 dbus_message_iter_init_append(m_message, &m_args);