DBus-1-TQt  1.0
tqdbusproxy.cpp
Go to the documentation of this file.
1 /* qdbusproxy.cpp DBUS Object proxy
2  *
3  * Copyright (C) 2005 Kevin Krammer <kevin.krammer@gmx.at>
4  *
5  * Licensed under the Academic Free License version 2.1
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20  * USA.
21  *
22  */
23 
24 #include "tqdbuserror.h"
25 #include "tqdbusconnection.h"
26 #include "tqdbusmessage.h"
27 #include "tqdbusproxy.h"
28 
30 {
31 public:
32  Private() : canSend(false) {}
33  ~Private() {}
34 
35  void checkCanSend()
36  {
37  canSend = !path.isEmpty() && !service.isEmpty() && !interface.isEmpty();
38  }
39 
40 public:
42 
43  TQString service;
44  TQString path;
45  TQString interface;
46  bool canSend;
47 
49 };
50 
51 TQT_DBusProxy::TQT_DBusProxy(TQObject* parent, const char* name)
52  : TQObject(parent, (name ? name : "TQT_DBusProxy")),
53  d(new Private())
54 {
55 }
56 
58  TQObject* parent, const char* name)
59  : TQObject(parent, (name ? name : "TQT_DBusProxy")),
60  d(new Private())
61 {
63 }
64 
65 TQT_DBusProxy::TQT_DBusProxy(const TQString& service, const TQString& path,
66  const TQString& interface, const TQT_DBusConnection& connection,
67  TQObject* parent, const char* name)
68  : TQObject(parent, (name ? name : "TQT_DBusProxy")),
69  d(new Private())
70 {
72 
73  d->service = service;
74  d->path = path;
76  d->checkCanSend();
77 }
78 
80 {
81  delete d;
82 }
83 
85 {
86  d->connection.disconnect(this, TQ_SLOT(handleDBusSignal(const TQT_DBusMessage&)));
87 
89 
90  return d->connection.connect(this, TQ_SLOT(handleDBusSignal(const TQT_DBusMessage&)));
91 }
92 
94 {
95  return d->connection;
96 }
97 
98 void TQT_DBusProxy::setService(const TQString& service)
99 {
100  d->service = service;
101  d->checkCanSend();
102 }
103 
104 TQString TQT_DBusProxy::service() const
105 {
106  return d->service;
107 }
108 
109 void TQT_DBusProxy::setPath(const TQString& path)
110 {
111  d->path = path;
112  d->checkCanSend();
113 }
114 
115 TQString TQT_DBusProxy::path() const
116 {
117  return d->path;
118 }
119 
120 void TQT_DBusProxy::setInterface(const TQString& interface)
121 {
122  d->interface = interface;
123  d->checkCanSend();
124 }
125 
126 TQString TQT_DBusProxy::interface() const
127 {
128  return d->interface;
129 }
130 
132 {
133  return d->canSend && d->connection.isConnected();
134 }
135 
136 bool TQT_DBusProxy::send(const TQString& method, const TQValueList<TQT_DBusData>& params) const
137 {
138  if (!d->canSend || method.isEmpty() || !d->connection.isConnected())
139  return false;
140 
142  d->interface, method);
143  message += params;
144 
145  return d->connection.send(message);
146 }
147 
149  const TQValueList<TQT_DBusData>& params,
150  TQT_DBusError* error) const
151 {
152  if (!d->canSend || method.isEmpty() || !d->connection.isConnected())
153  return TQT_DBusMessage();
154 
156  d->interface, method);
157  message += params;
158 
159  TQT_DBusMessage reply = d->connection.sendWithReply(message, &d->error);
160 
161  if (error)
162  *error = d->error;
163 
164  return reply;
165 }
166 
167 int TQT_DBusProxy::sendWithAsyncReply(const TQString& method, const TQValueList<TQT_DBusData>& params)
168 {
169  if (!d->canSend || method.isEmpty() || !d->connection.isConnected())
170  return 0;
171 
173  d->interface, method);
174  message += params;
175 
176  return d->connection.sendWithAsyncReply(message, this,
177  TQ_SLOT(handleAsyncReply(const TQT_DBusMessage&)));
178 }
179 
181 {
182  return d->error;
183 }
184 
186 {
187  if (!d->path.isEmpty() && d->path != message.path())
188  return;
189 
190  // only filter by service name if the name is a unique name
191  // because signals are always coming from a connection's unique name
192  // and filtering by a generic name would reject all signals
193  if (d->service.startsWith(":") && d->service != message.sender())
194  return;
195 
196  if (!d->interface.isEmpty() && d->interface != message.interface())
197  return;
198 
199  emit dbusSignal(message);
200 }
201 
203 {
204  d->error = message.error();
205 
206  emit asyncReply(message.replySerialNumber(), message);
207 }
208 
209 #include "tqdbusproxy.moc"
Provides access to a specific D-Bus bus.
bool connect(TQObject *object, const char *slot)
Connects an object to receive D-Bus signals.
TQT_DBusMessage sendWithReply(const TQT_DBusMessage &message, TQT_DBusError *error=0) const
Sends a message over the bus and waits for the reply.
int sendWithAsyncReply(const TQT_DBusMessage &message, TQObject *receiver, const char *slot) const
Sends a message over the bus, specifying a receiver object for replies.
bool disconnect(TQObject *object, const char *slot)
Disconnects a given receiver from the D-Bus signal handling.
bool send(const TQT_DBusMessage &message) const
Sends a message over the bus.
bool isConnected() const
Returns whether the connection is connected to a bus.
Class for transporting D-Bus errors.
Definition: tqdbuserror.h:41
A message converts and transports data over D-Bus.
static TQT_DBusMessage methodCall(const TQString &service, const TQString &path, const TQString &interface, const TQString &method)
Creates a message for sending a D-Bus method call.
TQT_DBusError error() const
Returns the error of an error message.
TQString interface() const
Returns the message's interface name.
TQString sender() const
Returns the name of the message sender.
int replySerialNumber() const
Returns the message's reply serial number.
TQString path() const
Returns the message's object path.
TQT_DBusConnection connection
Definition: tqdbusproxy.cpp:41
virtual ~TQT_DBusProxy()
Destroys the proxy instance.
Definition: tqdbusproxy.cpp:79
void setService(const TQString &service)
Sets the peer's service name.
Definition: tqdbusproxy.cpp:98
virtual void handleDBusSignal(const TQT_DBusMessage &message)
Handles D-Bus signals received on the proxy's connection.
void dbusSignal(const TQT_DBusMessage &message)
Signal emitted for D-Bus signals from the peer.
Private * d
Definition: tqdbusproxy.h:576
bool canSend() const
Returns whether the proxy can be used to send method calls.
TQT_DBusMessage sendWithReply(const TQString &method, const TQValueList< TQT_DBusData > &params, TQT_DBusError *error=0) const
Sends a method call to the peer object and waits for the reply.
TQString interface() const
Returns the name of the peer interface.
void setInterface(const TQString &interface)
Sets the name of the peer interface.
void setPath(const TQString &path)
Sets the peer's object path.
TQString service() const
Returns the peer's service name.
bool setConnection(const TQT_DBusConnection &connection)
Sets the D-Bus connection to work on.
Definition: tqdbusproxy.cpp:84
int sendWithAsyncReply(const TQString &method, const TQValueList< TQT_DBusData > &params)
Sends a method call to the peer object but does not wait for an answer.
bool send(const TQString &method, const TQValueList< TQT_DBusData > &params) const
Sends a method call to the peer object.
virtual void handleAsyncReply(const TQT_DBusMessage &message)
Handles replies to asynchronous method calls.
TQString path() const
Returns the peer's object path.
TQT_DBusProxy(TQObject *parent=0, const char *name=0)
Creates a proxy without binding it to a service or connection.
Definition: tqdbusproxy.cpp:51
const TQT_DBusConnection & connection() const
Returns the currently used D-Bus connection.
Definition: tqdbusproxy.cpp:93
void asyncReply(int callID, const TQT_DBusMessage &message)
Signal emitted for received replies to asynchronous method calls.
TQT_DBusError lastError() const
Returns the last error seen by the proxy.