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 blocking_(true), stateReady_(false), connectionReady_(false), scanReady_(false),
33 timeout_(false), numberOfConnections_(0),
34 scannedConnections_(0), timer_(0), 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 void ConnectionManager::setBlocking(bool value)
78 bool ConnectionManager::connect()
80 connectionReady_ = false;
81 unsigned int flags = static_cast<unsigned int>(ICD_CONNECTION_FLAG_USER_EVENT);
82 icd2interface_->call(ICD_DBUS_API_CONNECT_REQ, QVariant(flags));
86 waitSignal(&connectionReady_);
93 bool ConnectionManager::connect(ConnectionManager::Connection const& connection)
95 return connect(connection.id);
98 bool ConnectionManager::connect(QString const& id)
100 QDBusMessage msg = QDBusMessage::createMethodCall("com.nokia.icd",
104 QList<QVariant> arguments;
106 arguments.append(QVariant(id));
108 unsigned int val = 0;
110 arguments.append(QVariant(val));
112 msg.setArguments(arguments);
114 QDBusMessage rep = QDBusConnection::systemBus().call(msg);
116 if(rep.type() == QDBusMessage::ErrorMessage)
118 if(rep.errorName() == "com.nokia.icd.error.invalid_iap")
120 error_ = INVALID_IAP;
124 error_ = UNKNOWN_ERROR;
137 bool ConnectionManager::getBestConnection(Connection& connection, ConnectionType type)
139 bool blockingValue = blocking_;
143 QList<Connection> connections;
145 if(!scanConnections(connections))
147 qDebug() << "Unable to scan connections";
151 blocking_ = blockingValue;
153 if(connections.size() == 0)
155 error_ = NO_AVAILABLE_CONNECTIONS;
159 int biggestWlan = -1;
160 int biggestGprs = -1;
164 for(int i = 0; i < connections.size(); i++)
166 switch(connections.at(i).type)
169 if(type != GPRS && connections.at(i).strength > biggestWlan)
171 biggestWlan = connections.at(i).strength;
177 if(type != WLAN && connections.at(i).strength > biggestGprs)
179 biggestGprs = connections.at(i).strength;
185 qDebug() << "Unknown connection type";
191 connection = connections.at(bestWlan);
194 else if(bestGprs >= 0)
196 connection = connections.at(bestGprs);
201 error_ = NO_AVAILABLE_CONNECTIONS;
207 bool ConnectionManager::disconnect(bool force)
211 // Do not allow forced disconnection if connection was not started
218 QDBusMessage msg = QDBusMessage::createSignal("/com/nokia/icd_ui",
222 QList<QVariant> arguments;
224 arguments.append(QVariant(val));
225 msg.setArguments(arguments);
227 bool ret = QDBusConnection::systemBus().send(msg);
237 connectionReady_ = false;
240 flags = static_cast<unsigned int>(ICD_CONNECTION_FLAG_USER_EVENT);
242 icd2interface_->call(ICD_DBUS_API_DISCONNECT_REQ, QVariant(flags));
247 bool ConnectionManager::isConnected()
250 QDBusMessage rep = icd2interface_->call(ICD_DBUS_API_STATE_REQ);
252 unsigned int numOfReplies = rep.arguments().value(0).value<unsigned int>();
254 if(numOfReplies == 0)
256 emit isConnectedReply(false);
262 waitSignal(&stateReady_);
269 bool ConnectionManager::scanConnections(QList<ConnectionManager::Connection>& connections,
270 ConnectionManager::ConnectionType type)
272 unsigned int flags = static_cast<unsigned int>(ICD_SCAN_REQUEST_ACTIVE_SAVED);
274 scannedConnections_ = 0;
275 connections_ = &connections;
277 QStringList networks;
282 networks << "WLAN_INFRA" << "WLAN_ADHOC";
291 QDBusMessage rep = icd2interface_->call(ICD_DBUS_API_SCAN_REQ,
295 numberOfConnections_ = rep.arguments().value(0).toList().size();
297 if(numberOfConnections_ == 0)
300 qDebug() << "No connections";
306 bool ret = waitSignal(&scanReady_);
314 ConnectionManager::Error ConnectionManager::error() const
319 void ConnectionManager::stateChange(const QDBusMessage& rep)
321 unsigned int status = rep.arguments().value(7).value<unsigned int>();
325 case ICD_STATE_CONNECTING:
327 case ICD_STATE_CONNECTED:
331 case ICD_STATE_DISCONNECTING:
333 case ICD_STATE_DISCONNECTED:
337 case ICD_STATE_LIMITED_CONN_ENABLED:
341 case ICD_STATE_LIMITED_CONN_DISABLED:
345 case ICD_STATE_SEARCH_START:
347 case ICD_STATE_SEARCH_STOP:
349 case ICD_STATE_INTERNAL_ADDRESS_ACQUIRED:
352 qDebug() << "Unknown connection status";
358 emit isConnectedReply(connected_);
363 void ConnectionManager::connectionChange(const QDBusMessage& rep)
365 unsigned int status = rep.arguments().value(6).value<unsigned int>();
369 case ICD_CONNECTION_SUCCESSFUL:
371 connectionReady_ = true;
373 case ICD_CONNECTION_NOT_CONNECTED:
375 connectionReady_ = true;
377 case ICD_CONNECTION_DISCONNECTED:
379 connectionReady_ = true;
382 qDebug() << "Unknown connection status";
388 emit connectReply(connected_);
392 void ConnectionManager::scanResult(const QDBusMessage& rep)
399 QList<QVariant> args = rep.arguments();
401 unsigned int status = args.value(0).value<unsigned int>();
403 if(status == ICD_SCAN_COMPLETE)
405 scannedConnections_++;
408 if(scannedConnections_ >= numberOfConnections_)
416 if(status != ICD_SCAN_NEW)
421 Connection connection;
422 connection.id = QString(args.value(10).toByteArray());
423 connection.name = args.value(8).toString();
424 connection.strength = args.value(11).toInt();
426 QString type = args.value(7).toString();
430 connection.type = GPRS;
432 else if(type == "WLAN_INFRA" || type == "WLAN_ADHOC")
434 connection.type = WLAN;
438 qDebug() << "Unknown connection type: " << type;
442 emit newConnection(connection);
444 connections_->push_back(connection);
447 bool ConnectionManager::waitSignal(bool* ready)
450 timer_ = startTimer(TIMEOUT);
452 while(!*ready && !timeout_)
454 QCoreApplication::processEvents(QEventLoop::WaitForMoreEvents);
459 return *ready || !timeout_;
462 void ConnectionManager::timerEvent(QTimerEvent* event)
469 qDebug() << "Connection request timed out";