QtBase  v6.3.1
qmyserver.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Copyright (C) 2016 Intel Corporation.
5 ** Contact: https://www.qt.io/licensing/
6 **
7 ** This file is part of the test suite of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
10 ** Commercial License Usage
11 ** Licensees holding valid commercial Qt licenses may use this file in
12 ** accordance with the commercial license agreement provided with the
13 ** Software or, alternatively, in accordance with the terms contained in
14 ** a written agreement between you and The Qt Company. For licensing terms
15 ** and conditions see https://www.qt.io/terms-conditions. For further
16 ** information use the contact form at https://www.qt.io/contact-us.
17 **
18 ** GNU General Public License Usage
19 ** Alternatively, this file may be used under the terms of the GNU
20 ** General Public License version 3 as published by the Free Software
21 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
22 ** included in the packaging of this file. Please review the following
23 ** information to ensure the GNU General Public License requirements will
24 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
25 **
26 ** $QT_END_LICENSE$
27 **
28 ****************************************************************************/
29 
30 #include <QCoreApplication>
31 #include <QDBusServer>
32 
33 #include "../myobject.h"
34 
35 static const char serviceName[] = "org.qtproject.autotests.qmyserver";
36 static const char objectPath[] = "/org/qtproject/qmyserver";
37 //static const char *interfaceName = serviceName;
38 
39 int MyObject::callCount = 0;
40 QVariantList MyObject::callArgs;
41 
42 class MyServer : public QDBusServer, protected QDBusContext
43 {
44  Q_OBJECT
45  Q_CLASSINFO("D-Bus Interface", "org.qtproject.autotests.qmyserver")
46 
47 public:
48  MyServer(QObject *parent = nullptr)
50  m_conn("none")
51  {
52  connect(this, SIGNAL(newConnection(QDBusConnection)), SLOT(handleConnection(QDBusConnection)));
53  }
54 
55 public slots:
56  QString address() const
57  {
60  return QDBusServer::address();
61  }
62 
64  {
65  if (callPendingReply.type() != QDBusMessage::InvalidMessage) {
66  sendErrorReply(QDBusError::NotSupported, "One call already pending!");
67  return;
68  }
69  if (m_conn.isConnected())
70  return;
71  // not connected, we'll reply later
72  setDelayedReply(true);
73  callPendingReply = message();
74  }
75 
76  void emitSignal(const QString &interface, const QString &name, const QString &arg)
77  {
79  msg << arg;
80  m_conn.send(msg);
81  }
82 
83  void reset()
84  {
86  obj.m_complexProp.clear();
87  }
88 
89  int callCount()
90  {
91  return MyObject::callCount;
92  }
93 
95  {
96  return MyObject::callArgs;
97  }
98 
99  void setProp1(int val)
100  {
101  obj.m_prop1 = val;
102  }
103 
104  int prop1()
105  {
106  return obj.m_prop1;
107  }
108 
110  {
111  obj.m_complexProp = val;
112  }
113 
115  {
116  return obj.m_complexProp;
117  }
118 
120  {
121  if (message().isInteractiveAuthorizationAllowed())
122  return true;
123 
124  sendErrorReply(QStringLiteral("org.freedesktop.DBus.Error.InteractiveAuthorizationRequired"),
125  QStringLiteral("Interactive authentication required."));
126  return false;
127  }
128 
129  void quit()
130  {
131  qApp->quit();
132  }
133 
134 private slots:
135  void handleConnection(const QDBusConnection& con)
136  {
137  m_conn = con;
141  if (callPendingReply.type() != QDBusMessage::InvalidMessage) {
142  QDBusConnection::sessionBus().send(callPendingReply.createReply());
143  callPendingReply = QDBusMessage();
144  }
145  }
146 
147 private:
148  QDBusConnection m_conn;
149  QDBusMessage callPendingReply;
150  MyObject obj;
151 };
152 
153 int main(int argc, char *argv[])
154 {
155  QCoreApplication app(argc, argv);
156 
158  if (!con.isConnected())
159  exit(1);
160 
161  if (!con.registerService(serviceName))
162  exit(2);
163 
165  con.registerObject(objectPath, &server, QDBusConnection::ExportAllSlots);
166 
167  printf("ready.\n");
168  fflush(stdout);
169 
170  return app.exec();
171 }
172 
173 #include "qmyserver.moc"
const char msg[]
Definition: arch.cpp:46
[0]
Definition: myobject.h:58
void waitForConnected()
Definition: qmyserver.cpp:63
void setComplexProp(QList< int > val)
Definition: qmyserver.cpp:109
QList< int > complexProp()
Definition: qmyserver.cpp:114
QVariantList callArgs()
Definition: qmyserver.cpp:94
int callCount()
Definition: qmyserver.cpp:89
bool interactiveAuthorization()
Definition: qmyserver.cpp:119
void setProp1(int val)
Definition: qmyserver.cpp:99
void reset()
Definition: qmyserver.cpp:83
QString address() const
Definition: qmyserver.cpp:56
void emitSignal(const QString &interface, const QString &name, const QString &arg)
Definition: qmyserver.cpp:76
void quit()
Definition: qmyserver.cpp:129
int prop1()
Definition: qmyserver.cpp:104
static int exec()
The QCoreApplication class provides an event loop for Qt applications without UI.
The QDBusConnection class represents a connection to the D-Bus bus daemon.
bool send(const QDBusMessage &message) const
bool isConnected() const
static QDBusConnection sessionBus()
bool registerObject(const QString &path, QObject *object, RegisterOptions options=ExportAdaptors)
The QDBusContext class allows slots to determine the D-Bus context of the calls.
Definition: qdbuscontext.h:57
void setDelayedReply(bool enable) const
void sendErrorReply(const QString &name, const QString &msg=QString()) const
const QDBusMessage & message() const
@ NotSupported
Definition: qdbuserror.h:68
The QDBusMessage class represents one message sent or received over the D-Bus bus.
Definition: qdbusmessage.h:58
static QDBusMessage createSignal(const QString &path, const QString &interface, const QString &name)
QDBusMessage createReply(const QList< QVariant > &arguments=QList< QVariant >()) const
MessageType type() const
The QDBusServer class provides peer-to-peer communication between processes on the same computer.
Definition: qdbusserver.h:57
void newConnection(const QDBusConnection &connection)
QDBusError lastError() const
bool isConnected() const
QString address() const
The QObject class is the base class of all Qt objects.
Definition: qobject.h:125
QObject * parent() const
Definition: qobject.h:409
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Definition: qobject.cpp:2772
The QString class provides a Unicode character string.
Definition: qstring.h:388
#define qApp
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter int const void return DBusMessageIter DBusMessageIter return DBusMessageIter void DBusMessageIter void int return DBusMessage DBusMessageIter return DBusMessageIter return DBusMessageIter DBusMessageIter const char const char const char const char return DBusMessage return DBusMessage const char return DBusMessage dbus_bool_t return DBusMessage dbus_uint32_t return DBusMessage return DBusPendingCall DBusPendingCall return DBusPendingCall return dbus_int32_t return DBusServer * server
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter int const void return DBusMessageIter DBusMessageIter return DBusMessageIter void DBusMessageIter void int return DBusMessage DBusMessageIter return DBusMessageIter return DBusMessageIter DBusMessageIter const char const char const char * interface
int main(int argc, char *argv[])
[1]
Definition: qmyserver.cpp:171
#define SLOT(a)
Definition: qobjectdefs.h:87
#define SIGNAL(a)
Definition: qobjectdefs.h:88
GLuint name
GLhandleARB obj
[2]
Definition: qopenglext.h:4164
GLuint GLfloat * val
Definition: qopenglext.h:1513
SSL_CTX int(*) void arg)
#define QStringLiteral(str)
#define Q_OBJECT
Definition: qtmetamacros.h:158
#define Q_CLASSINFO(name, value)
Definition: qtmetamacros.h:88
#define slots
Definition: qtmetamacros.h:76
QApplication app(argc, argv)
[0]