1/****************************************************************************
2**
3** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4** Contact: http://www.qt-project.org/legal
5**
6** This file is part of the QtDBus module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Digia. For licensing terms and
14** conditions see http://qt.digia.com/licensing. For further information
15** use the contact form at http://qt.digia.com/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 2.1 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 2.1 requirements
23** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24**
25** In addition, as a special exception, Digia gives you certain additional
26** rights. These rights are described in the Digia Qt LGPL Exception
27** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28**
29** GNU General Public License Usage
30** Alternatively, this file may be used under the terms of the GNU
31** General Public License version 3.0 as published by the Free Software
32** Foundation and appearing in the file LICENSE.GPL included in the
33** packaging of this file. Please review the following information to
34** ensure the GNU General Public License version 3.0 requirements will be
35** met: http://www.gnu.org/copyleft/gpl.html.
36**
37**
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qdbusserver.h"
43#include "qdbusconnection_p.h"
44#include "qdbusconnectionmanager_p.h"
45
46#ifndef QT_NO_DBUS
47
48QT_BEGIN_NAMESPACE
49
50/*!
51 \class QDBusServer
52 \inmodule QtDBus
53 \internal
54
55 \brief The QDBusServer class provides peer-to-peer communication
56 between processes on the same computer.
57*/
58
59/*!
60 Constructs a QDBusServer with the given \a address, and the given
61 \a parent.
62*/
63QDBusServer::QDBusServer(const QString &address, QObject *parent)
64 : QObject(parent)
65{
66 if (address.isEmpty())
67 return;
68
69 if (!qdbus_loadLibDBus()) {
70 d = 0;
71 return;
72 }
73 d = new QDBusConnectionPrivate(this);
74
75 QObject::connect(d, SIGNAL(newServerConnection(QDBusConnection)),
76 this, SIGNAL(newConnection(QDBusConnection)));
77
78 QDBusErrorInternal error;
79 d->setServer(q_dbus_server_listen(address.toUtf8().constData(), error), error);
80}
81
82/*!
83 Destructs a QDBusServer
84*/
85QDBusServer::~QDBusServer()
86{
87 if (QDBusConnectionManager::instance()) {
88 QMutexLocker locker(&QDBusConnectionManager::instance()->mutex);
89 Q_FOREACH (const QString &name, d->serverConnectionNames) {
90 QDBusConnectionManager::instance()->removeConnection(name);
91 }
92 d->serverConnectionNames.clear();
93 }
94}
95
96/*!
97 Returns true if this QDBusServer object is connected.
98
99 If it isn't connected, you need to call the constructor again.
100*/
101bool QDBusServer::isConnected() const
102{
103 return d && d->server && q_dbus_server_get_is_connected(d->server);
104}
105
106/*!
107 Returns the last error that happened in this server.
108
109 This function is provided for low-level code.
110*/
111QDBusError QDBusServer::lastError() const
112{
113 return d->lastError;
114}
115
116/*!
117 Returns the address this server is associated with.
118*/
119QString QDBusServer::address() const
120{
121 QString addr;
122 if (d && d->server) {
123 char *c = q_dbus_server_get_address(d->server);
124 addr = QString::fromUtf8(c);
125 q_dbus_free(c);
126 }
127
128 return addr;
129}
130
131/*!
132 \fn void QDBusServer::newConnection(const QDBusConnection &connection)
133
134 This signal is emitted when a new client connection \a connection is
135 established to the server.
136 */
137
138QT_END_NAMESPACE
139
140#endif // QT_NO_DBUS
141