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/>.
19 #include <QtCore/QDebug>
20 #include <QtCore/QTimerEvent>
21 #include <QtCore/QVariant>
22 #include <QtCore/QStringList>
23 #include <QtGui/QApplication>
24 #include <QtDBus/QDBusArgument>
25 #include <QtDBus/QDBusConnection>
26 #include <icd/dbus_api.h>
27 #include "connectionmanager.h"
29 bool ConnectionManager::connected_ = false;
31 ConnectionManager::ConnectionManager(QObject* parent): QObject(parent),
32 stateReady_(false), connectionReady_(false), scanReady_(false),
33 timeout_(false), numberOfConnections_(0),
34 scannedConnections_(0), timer_(0), searchType_(NO_TYPE), error_(NO_ERROR), connections_(0)
36 QDBusConnection systemBus = QDBusConnection::systemBus();
38 icd2interface_ = new QDBusInterface(ICD_DBUS_API_INTERFACE,
39 ICD_DBUS_API_PATH, ICD_DBUS_API_INTERFACE,
42 systemBus.connect(ICD_DBUS_API_INTERFACE, ICD_DBUS_API_PATH,
43 ICD_DBUS_API_INTERFACE, ICD_DBUS_API_STATE_SIG,
44 this, SLOT(stateChange(const QDBusMessage&)));
46 systemBus.connect(ICD_DBUS_API_INTERFACE, ICD_DBUS_API_PATH,
47 ICD_DBUS_API_INTERFACE, ICD_DBUS_API_CONNECT_SIG,
48 this, SLOT(connectionChange(const QDBusMessage&)));
50 systemBus.connect(ICD_DBUS_API_INTERFACE, ICD_DBUS_API_PATH,
51 ICD_DBUS_API_INTERFACE, ICD_DBUS_API_SCAN_SIG,
52 this, SLOT(scanResult(const QDBusMessage&)));
56 ConnectionManager::~ConnectionManager()
58 QDBusConnection systemBus = QDBusConnection::systemBus();
60 systemBus.disconnect(ICD_DBUS_API_INTERFACE, ICD_DBUS_API_PATH,
61 ICD_DBUS_API_INTERFACE, ICD_DBUS_API_STATE_SIG,
62 this, SLOT(stateChange(const QDBusMessage&)));
64 systemBus.disconnect(ICD_DBUS_API_INTERFACE, ICD_DBUS_API_PATH,
65 ICD_DBUS_API_INTERFACE, ICD_DBUS_API_CONNECT_SIG,
66 this, SLOT(connectionChange(const QDBusMessage&)));
68 systemBus.disconnect(ICD_DBUS_API_INTERFACE, ICD_DBUS_API_PATH,
69 ICD_DBUS_API_INTERFACE, ICD_DBUS_API_SCAN_SIG,
70 this, SLOT(scanResult(const QDBusMessage&)));
73 bool ConnectionManager::connect()
75 connectionReady_ = false;
76 unsigned int flags = static_cast<unsigned int>(ICD_CONNECTION_FLAG_USER_EVENT);
77 icd2interface_->call(ICD_DBUS_API_CONNECT_REQ, QVariant(flags));
79 waitSignal(&connectionReady_);
83 bool ConnectionManager::connect(ConnectionManager::Connection const& connection)
85 return connect(connection.id);
88 bool ConnectionManager::connect(QString const& id)
90 QDBusMessage msg = QDBusMessage::createMethodCall("com.nokia.icd",
94 QList<QVariant> arguments;
96 arguments.append(QVariant(id));
100 arguments.append(QVariant(val));
102 msg.setArguments(arguments);
104 QDBusMessage rep = QDBusConnection::systemBus().call(msg);
106 if(rep.type() == QDBusMessage::ErrorMessage)
108 if(rep.errorName() == "com.nokia.icd.error.invalid_iap")
110 error_ = INVALID_IAP;
114 error_ = UNKNOWN_ERROR;
127 bool ConnectionManager::getBestConnection(Connection& connection, ConnectionType type)
129 QList<Connection> connections;
131 if(!scanConnections(connections))
133 qDebug() << "Unable to scan connections";
137 if(connections.size() == 0)
139 error_ = NO_AVAILABLE_CONNECTIONS;
143 int biggestWlan = -1;
144 int biggestGprs = -1;
148 for(int i = 0; i < connections.size(); i++)
150 switch(connections.at(i).type)
153 if(type != GPRS && connections.at(i).strength > biggestWlan)
155 biggestWlan = connections.at(i).strength;
161 if(type != WLAN && connections.at(i).strength > biggestGprs)
163 biggestGprs = connections.at(i).strength;
169 qDebug() << "Unknown connection type";
175 connection = connections.at(bestWlan);
178 else if(bestGprs >= 0)
180 connection = connections.at(bestGprs);
185 error_ = NO_AVAILABLE_CONNECTIONS;
191 bool ConnectionManager::disconnect(bool force)
195 QDBusMessage msg = QDBusMessage::createSignal("/com/nokia/icd_ui",
199 QList<QVariant> arguments;
201 arguments.append(QVariant(val));
202 msg.setArguments(arguments);
204 bool ret = QDBusConnection::systemBus().send(msg);
214 connectionReady_ = false;
217 flags = static_cast<unsigned int>(ICD_CONNECTION_FLAG_USER_EVENT);
219 icd2interface_->call(ICD_DBUS_API_DISCONNECT_REQ, QVariant(flags));
224 bool ConnectionManager::isConnected()
227 QDBusMessage rep = icd2interface_->call(ICD_DBUS_API_STATE_REQ);
229 unsigned int numOfReplies = rep.arguments().value(0).value<unsigned int>();
231 if(numOfReplies == 0)
236 waitSignal(&stateReady_);
240 bool ConnectionManager::scanConnections(QList<ConnectionManager::Connection>& connections,
241 ConnectionManager::ConnectionType type)
243 unsigned int flags = static_cast<unsigned int>(ICD_SCAN_REQUEST_ACTIVE_SAVED);
245 scannedConnections_ = 0;
246 connections_ = &connections;
249 QDBusMessage rep = icd2interface_->call(ICD_DBUS_API_SCAN_REQ,
252 numberOfConnections_ = rep.arguments().value(0).toList().size();
254 // For some reason, during call icd2 doesn't return any connections
255 // it is going to scan. However, it still scans them so use default value
257 if(numberOfConnections_ == 0)
259 numberOfConnections_ = 2;
262 bool ret = waitSignal(&scanReady_);
267 ConnectionManager::Error ConnectionManager::error() const
272 void ConnectionManager::stateChange(const QDBusMessage& rep)
274 unsigned int status = rep.arguments().value(7).value<unsigned int>();
278 case ICD_STATE_CONNECTING:
280 case ICD_STATE_CONNECTED:
284 case ICD_STATE_DISCONNECTING:
286 case ICD_STATE_DISCONNECTED:
290 case ICD_STATE_LIMITED_CONN_ENABLED:
294 case ICD_STATE_LIMITED_CONN_DISABLED:
298 case ICD_STATE_SEARCH_START:
300 case ICD_STATE_SEARCH_STOP:
302 case ICD_STATE_INTERNAL_ADDRESS_ACQUIRED:
305 qDebug() << "Unknown connection status";
311 void ConnectionManager::connectionChange(const QDBusMessage& rep)
313 unsigned int status = rep.arguments().value(6).value<unsigned int>();
317 case ICD_CONNECTION_SUCCESSFUL:
319 connectionReady_ = true;
321 case ICD_CONNECTION_NOT_CONNECTED:
323 connectionReady_ = true;
325 case ICD_CONNECTION_DISCONNECTED:
327 connectionReady_ = true;
330 qDebug() << "Unknown connection status";
336 void ConnectionManager::scanResult(const QDBusMessage& rep)
343 QList<QVariant> args = rep.arguments();
345 unsigned int status = args.value(0).value<unsigned int>();
347 if(status == ICD_SCAN_COMPLETE)
349 scannedConnections_++;
352 if(scannedConnections_ >= numberOfConnections_)
359 if(status != ICD_SCAN_NEW && status != ICD_SCAN_NOTIFY)
364 Connection connection;
366 QString type = args.value(7).toString();
370 if(searchType_ == WLAN)
375 connection.type = GPRS;
377 else if(type == "WLAN_INFRA" || type == "WLAN_ADHOC")
379 if(searchType_ == GPRS)
384 connection.type = WLAN;
388 qDebug() << "Unknown connection type: " << type;
392 connection.id = QString(args.value(10).toByteArray());
393 connection.name = args.value(8).toString();
394 connection.strength = args.value(11).toInt();
396 for(int i = 0; i < connections_->size(); i++)
398 if(connections_->at(i).id == connection.id)
400 if(status == ICD_SCAN_NEW)
402 connections_->replace(i, connection);
409 connections_->push_back(connection);
412 bool ConnectionManager::waitSignal(bool* ready)
415 timer_ = startTimer(TIMEOUT);
417 while(!*ready && !timeout_)
419 QCoreApplication::processEvents(QEventLoop::WaitForMoreEvents);
424 return *ready || !timeout_;
427 void ConnectionManager::timerEvent(QTimerEvent* event)
434 qDebug() << "Connection request timed out";