DBus-1-TQt  1.0
tqdbusproxy.cpp
Go to the documentation of this file.
00001 /* qdbusproxy.cpp DBUS Object proxy
00002  *
00003  * Copyright (C) 2005 Kevin Krammer <kevin.krammer@gmx.at>
00004  *
00005  * Licensed under the Academic Free License version 2.1
00006  *
00007  * This program is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 2 of the License, or
00010  * (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
00020  * USA.
00021  *
00022  */
00023 
00024 #include "tqdbuserror.h"
00025 #include "tqdbusconnection.h"
00026 #include "tqdbusmessage.h"
00027 #include "tqdbusproxy.h"
00028 
00029 class TQT_DBusProxy::Private
00030 {
00031 public:
00032     Private() : canSend(false) {}
00033     ~Private() {}
00034 
00035     void checkCanSend()
00036     {
00037         canSend = !path.isEmpty() && !service.isEmpty() && !interface.isEmpty();
00038     }
00039 
00040 public:
00041     TQT_DBusConnection connection;
00042 
00043     TQString service;
00044     TQString path;
00045     TQString interface;
00046     bool canSend;
00047 
00048     TQT_DBusError error;
00049 };
00050 
00051 TQT_DBusProxy::TQT_DBusProxy(TQObject* parent, const char* name)
00052     : TQObject(parent, (name ? name : "TQT_DBusProxy")),
00053       d(new Private())
00054 {
00055 }
00056 
00057 TQT_DBusProxy::TQT_DBusProxy(const TQT_DBusConnection& connection,
00058                        TQObject* parent, const char* name)
00059     : TQObject(parent, (name ? name : "TQT_DBusProxy")),
00060       d(new Private())
00061 {
00062     setConnection(connection);
00063 }
00064 
00065 TQT_DBusProxy::TQT_DBusProxy(const TQString& service, const TQString& path,
00066                        const TQString& interface, const TQT_DBusConnection& connection,
00067                        TQObject* parent, const char* name)
00068     : TQObject(parent, (name ? name : "TQT_DBusProxy")),
00069       d(new Private())
00070 {
00071     setConnection(connection);
00072 
00073     d->service = service;
00074     d->path = path;
00075     d->interface = interface;
00076     d->checkCanSend();
00077 }
00078 
00079 TQT_DBusProxy::~TQT_DBusProxy()
00080 {
00081     delete d;
00082 }
00083 
00084 bool TQT_DBusProxy::setConnection(const TQT_DBusConnection& connection)
00085 {
00086     d->connection.disconnect(this, TQT_SLOT(handleDBusSignal(const TQT_DBusMessage&)));
00087 
00088     d->connection = connection;
00089 
00090     return d->connection.connect(this, TQT_SLOT(handleDBusSignal(const TQT_DBusMessage&)));
00091 }
00092 
00093 const TQT_DBusConnection& TQT_DBusProxy::connection() const
00094 {
00095     return d->connection;
00096 }
00097 
00098 void TQT_DBusProxy::setService(const TQString& service)
00099 {
00100     d->service = service;
00101     d->checkCanSend();
00102 }
00103 
00104 TQString TQT_DBusProxy::service() const
00105 {
00106     return d->service;
00107 }
00108 
00109 void TQT_DBusProxy::setPath(const TQString& path)
00110 {
00111     d->path = path;
00112     d->checkCanSend();
00113 }
00114 
00115 TQString TQT_DBusProxy::path() const
00116 {
00117     return d->path;
00118 }
00119 
00120 void TQT_DBusProxy::setInterface(const TQString& interface)
00121 {
00122     d->interface = interface;
00123     d->checkCanSend();
00124 }
00125 
00126 TQString TQT_DBusProxy::interface() const
00127 {
00128     return d->interface;
00129 }
00130 
00131 bool TQT_DBusProxy::canSend() const
00132 {
00133     return d->canSend && d->connection.isConnected();
00134 }
00135 
00136 bool TQT_DBusProxy::send(const TQString& method, const TQValueList<TQT_DBusData>& params) const
00137 {
00138     if (!d->canSend || method.isEmpty() || !d->connection.isConnected())
00139         return false;
00140 
00141     TQT_DBusMessage message = TQT_DBusMessage::methodCall(d->service, d->path,
00142                                                     d->interface, method);
00143     message += params;
00144 
00145     return d->connection.send(message);
00146 }
00147 
00148 TQT_DBusMessage TQT_DBusProxy::sendWithReply(const TQString& method,
00149                                        const TQValueList<TQT_DBusData>& params,
00150                                        TQT_DBusError* error) const
00151 {
00152     if (!d->canSend || method.isEmpty() || !d->connection.isConnected())
00153         return TQT_DBusMessage();
00154 
00155     TQT_DBusMessage message = TQT_DBusMessage::methodCall(d->service, d->path,
00156                                                     d->interface, method);
00157     message += params;
00158 
00159     TQT_DBusMessage reply = d->connection.sendWithReply(message, &d->error);
00160 
00161     if (error)
00162         *error = d->error;
00163 
00164     return reply;
00165 }
00166 
00167 int TQT_DBusProxy::sendWithAsyncReply(const TQString& method, const TQValueList<TQT_DBusData>& params)
00168 {
00169     if (!d->canSend || method.isEmpty() || !d->connection.isConnected())
00170         return 0;
00171 
00172     TQT_DBusMessage message = TQT_DBusMessage::methodCall(d->service, d->path,
00173                                                     d->interface, method);
00174     message += params;
00175 
00176     return d->connection.sendWithAsyncReply(message, this,
00177                    TQT_SLOT(handleAsyncReply(const TQT_DBusMessage&)));
00178 }
00179 
00180 TQT_DBusError TQT_DBusProxy::lastError() const
00181 {
00182     return d->error;
00183 }
00184 
00185 void TQT_DBusProxy::handleDBusSignal(const TQT_DBusMessage& message)
00186 {
00187     if (!d->path.isEmpty() && d->path != message.path())
00188         return;
00189 
00190     // only filter by service name if the name is a unique name
00191     // because signals are always coming from a connection's unique name
00192     // and filtering by a generic name would reject all signals
00193     if (d->service.startsWith(":") && d->service != message.sender())
00194         return;
00195 
00196     if (!d->interface.isEmpty() && d->interface != message.interface())
00197         return;
00198 
00199     emit dbusSignal(message);
00200 }
00201 
00202 void TQT_DBusProxy::handleAsyncReply(const TQT_DBusMessage& message)
00203 {
00204     d->error = message.error();
00205 
00206     emit asyncReply(message.replySerialNumber(), message);
00207 }
00208 
00209 #include "tqdbusproxy.moc"
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines