2 * This file is part of Jenirok.
4 * Jenirok is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Jenirok is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Jenirok. If not, see <http://www.gnu.org/licenses/>.
20 #include <QApplication>
21 #include <QtCore/QTimerEvent>
22 #include <QtCore/QVariant>
23 #include <icd/dbus_api.h>
24 #include "connectionmanager.h"
27 ConnectionManager::ConnectionManager(QObject* parent): QObject(parent),
28 ready_(false), connected_(false), timeout_(false), ignoreStateChanges_(false),
31 QDBusConnection systemBus = QDBusConnection::systemBus();
33 icd2interface_ = new QDBusInterface(ICD_DBUS_API_INTERFACE,
34 ICD_DBUS_API_PATH, ICD_DBUS_API_INTERFACE,
37 systemBus.connect(ICD_DBUS_API_INTERFACE, ICD_DBUS_API_PATH,
38 ICD_DBUS_API_INTERFACE, ICD_DBUS_API_STATE_SIG,
39 this, SLOT(stateChange(const QDBusMessage&)));
41 systemBus.connect(ICD_DBUS_API_INTERFACE, ICD_DBUS_API_PATH,
42 ICD_DBUS_API_INTERFACE, ICD_DBUS_API_CONNECT_SIG,
43 this, SLOT(connectionChange(const QDBusMessage&)));
48 bool ConnectionManager::connect()
51 ignoreStateChanges_ = true;
52 unsigned int flags = static_cast<unsigned int>(ICD_CONNECTION_FLAG_USER_EVENT);
53 icd2interface_->call(ICD_DBUS_API_CONNECT_REQ, QVariant(flags));
58 bool ConnectionManager::disconnect()
66 ignoreStateChanges_ = false;
67 unsigned int flags = static_cast<unsigned int>(ICD_CONNECTION_FLAG_USER_EVENT);
68 icd2interface_->call(ICD_DBUS_API_DISCONNECT_REQ, QVariant(flags));
73 bool ConnectionManager::isConnected()
76 ignoreStateChanges_ = false;
77 QDBusMessage rep = icd2interface_->call(ICD_DBUS_API_STATE_REQ);
79 unsigned int numOfReplies = rep.arguments().value(0).value<unsigned int>();
90 void ConnectionManager::stateChange(const QDBusMessage& rep)
92 if(ignoreStateChanges_)
97 unsigned int status = rep.arguments().value(7).value<unsigned int>();
101 case ICD_STATE_CONNECTING:
102 qDebug() << "Connecting";
104 case ICD_STATE_CONNECTED:
107 qDebug() << "Connected";
109 case ICD_STATE_DISCONNECTING:
110 qDebug() << "Disconnecting";
112 case ICD_STATE_DISCONNECTED:
115 qDebug() << "Disconnected";
117 case ICD_STATE_LIMITED_CONN_ENABLED:
120 qDebug() << "Limited connection enabled";
122 case ICD_STATE_LIMITED_CONN_DISABLED:
125 qDebug() << "Limited connection disabled";
127 case ICD_STATE_SEARCH_START:
128 qDebug() << "Search start";
130 case ICD_STATE_SEARCH_STOP:
131 qDebug() << "Search stop";
133 case ICD_STATE_INTERNAL_ADDRESS_ACQUIRED:
134 qDebug() << "Internal address acquired";
137 qDebug() << "Unknown connection status";
143 void ConnectionManager::connectionChange(const QDBusMessage& rep)
145 unsigned int status = rep.arguments().value(6).value<unsigned int>();
149 case ICD_CONNECTION_SUCCESSFUL:
152 qDebug() << "Connection successful";
154 case ICD_CONNECTION_NOT_CONNECTED:
157 qDebug() << "Connection not connected";
159 case ICD_CONNECTION_DISCONNECTED:
162 qDebug() << "Connection disconnected";
165 qDebug() << "Unknown connection status";
170 bool ConnectionManager::waitSignal()
173 timer_ = startTimer(TIMEOUT);
175 while(!ready_ && !timeout_)
177 QCoreApplication::processEvents(QEventLoop::WaitForMoreEvents);
182 return ready_ || !timeout_;
185 void ConnectionManager::timerEvent(QTimerEvent* event)
192 qDebug() << "Connection request timed out";