bool ConnectionManager::connected_ = false;
ConnectionManager::ConnectionManager(QObject* parent): QObject(parent),
-blocking_(true), stateReady_(false), connectionReady_(false), scanReady_(false),
+stateReady_(false), connectionReady_(false), scanReady_(false),
timeout_(false), numberOfConnections_(0),
-scannedConnections_(0), timer_(0), error_(NO_ERROR), connections_(0)
+scannedConnections_(0), timer_(0), searchType_(NO_TYPE), error_(NO_ERROR), connections_(0)
{
QDBusConnection systemBus = QDBusConnection::systemBus();
this, SLOT(scanResult(const QDBusMessage&)));
}
-void ConnectionManager::setBlocking(bool value)
-{
- blocking_ = value;
-}
-
bool ConnectionManager::connect()
{
connectionReady_ = false;
unsigned int flags = static_cast<unsigned int>(ICD_CONNECTION_FLAG_USER_EVENT);
icd2interface_->call(ICD_DBUS_API_CONNECT_REQ, QVariant(flags));
- if(blocking_)
- {
- waitSignal(&connectionReady_);
- return connected_;
- }
-
- return true;
+ waitSignal(&connectionReady_);
+ return connected_;
}
bool ConnectionManager::connect(ConnectionManager::Connection const& connection)
bool ConnectionManager::getBestConnection(Connection& connection, ConnectionType type)
{
- bool blockingValue = blocking_;
-
- blocking_ = true;
-
QList<Connection> connections;
if(!scanConnections(connections))
return false;
}
- blocking_ = blockingValue;
-
if(connections.size() == 0)
{
error_ = NO_AVAILABLE_CONNECTIONS;
for(int i = 0; i < connections.size(); i++)
{
- switch(connections.at(i).type)
- {
- case WLAN:
- if(type != GPRS && connections.at(i).strength > biggestWlan)
- {
- biggestWlan = connections.at(i).strength;
- bestWlan = i;
- }
- break;
-
- case GPRS:
- if(type != WLAN && connections.at(i).strength > biggestGprs)
- {
- biggestGprs = connections.at(i).strength;
- bestGprs = i;
- }
- break;
-
- default:
- qDebug() << "Unknown connection type";
- }
+ switch(connections.at(i).type)
+ {
+ case WLAN:
+ if(type != GPRS && connections.at(i).strength > biggestWlan)
+ {
+ biggestWlan = connections.at(i).strength;
+ bestWlan = i;
+ }
+ break;
+
+ case GPRS:
+ if(type != WLAN && connections.at(i).strength > biggestGprs)
+ {
+ biggestGprs = connections.at(i).strength;
+ bestGprs = i;
+ }
+ break;
+
+ default:
+ qDebug() << "Unknown connection type";
+ }
}
if(bestWlan >= 0)
if(numOfReplies == 0)
{
- emit isConnectedReply(false);
return false;
}
- if(blocking_)
- {
- waitSignal(&stateReady_);
- return connected_;
- }
-
- return true;
+ waitSignal(&stateReady_);
+ return connected_;
}
bool ConnectionManager::scanConnections(QList<ConnectionManager::Connection>& connections,
scanReady_ = false;
scannedConnections_ = 0;
connections_ = &connections;
-
- QStringList networks;
-
- switch(type)
- {
- case WLAN:
- networks << "WLAN_INFRA" << "WLAN_ADHOC";
- break;
- case GPRS:
- networks << "GPRS";
- break;
- default:
- break;
- }
+ searchType_ = type;
QDBusMessage rep = icd2interface_->call(ICD_DBUS_API_SCAN_REQ,
- QVariant(flags),
- QVariant(networks));
+ QVariant(flags));
numberOfConnections_ = rep.arguments().value(0).toList().size();
+ // For some reason, during call icd2 doesn't return any connections
+ // it is going to scan. However, it still scans them so use default value
+ // of 2.
if(numberOfConnections_ == 0)
{
- connections_ = 0;
- qDebug() << "No connections";
- return false;
- }
-
- if(blocking_)
- {
- bool ret = waitSignal(&scanReady_);
- connections_ = 0;
- return ret;
+ numberOfConnections_ = 2;
}
- return true;
+ bool ret = waitSignal(&scanReady_);
+ connections_ = 0;
+ return ret;
}
ConnectionManager::Error ConnectionManager::error() const
break;
}
- if(stateReady_)
- {
- emit isConnectedReply(connected_);
- }
-
}
void ConnectionManager::connectionChange(const QDBusMessage& rep)
break;
}
- if(connectionReady_)
- {
- emit connectReply(connected_);
- }
}
void ConnectionManager::scanResult(const QDBusMessage& rep)
{
scanReady_ = true;
connections_ = 0;
- emit scanReady();
return;
}
}
Connection connection;
- connection.id = QString(args.value(10).toByteArray());
- connection.name = args.value(8).toString();
- connection.strength = args.value(11).toInt();
QString type = args.value(7).toString();
if(type == "GPRS")
{
+ if(searchType_ == WLAN)
+ {
+ return;
+ }
+
connection.type = GPRS;
}
else if(type == "WLAN_INFRA" || type == "WLAN_ADHOC")
{
+ if(searchType_ == GPRS)
+ {
+ return;
+ }
+
connection.type = WLAN;
}
else
return;
}
- emit newConnection(connection);
+ connection.id = QString(args.value(10).toByteArray());
+ connection.name = args.value(8).toString();
+ connection.strength = args.value(11).toInt();
connections_->push_back(connection);
}
ConnectionManager(QObject* parent = 0);
~ConnectionManager();
- void setBlocking(bool value);
bool connect();
bool connect(Connection const& connection);
bool connect(QString const& id);
bool isConnected();
bool scanConnections(QList<Connection>& connections, ConnectionType type = NO_TYPE);
Error error() const;
- static unsigned int const TIMEOUT = 20000;
-
-signals:
- void connectReply(bool connected);
- void isConnectedReply(bool connected);
- void newConnection(ConnectionManager::Connection const& connection);
- void scanReady();
+ static unsigned int const TIMEOUT = 15000;
protected:
virtual void timerEvent(QTimerEvent* event);
private:
bool waitSignal(bool* ready);
- bool blocking_;
bool stateReady_;
bool connectionReady_;
bool scanReady_;
int numberOfConnections_;
int scannedConnections_;
int timer_;
+ ConnectionType searchType_;
Error error_;
QList<Connection>* connections_;
QDBusInterface* icd2interface_;
this,
SLOT(callTerminate()));
- findGprsId();
-
qDebug() << "Starting...";
return true;
}
searchClose();
-
- if(gprsId_.isEmpty())
- {
- findGprsId();
- }
}
void CallListener::showDelayedResult(QString const& text, int delay)
}
int cretries = 0;
+ int scans = 0;
+ bool found = false;
+ int maxScans = GPRS_SCANS;
- bool tryGprs = (!gprsId_.isEmpty() &&
- (configType == Settings::ANY || configType == Settings::GPRS));
+ if(lookupType == ConnectionManager::WLAN)
+ {
+ maxScans = WLAN_SCANS;
+ }
while(cretries < CONNECTION_LOOKUP_RETRIES)
{
return false;
}
- if(cm.getBestConnection(best, lookupType))
+ if(scans < maxScans)
{
- break;
+ if(cm.getBestConnection(best, lookupType))
+ {
+ found = true;
+ }
+
+ scans++;
}
- // ICD doesn't always find gprs connection during call, so
- // try to use gprs anyway.
- if(tryGprs && is3g())
+ // If there is only gprs connection available,
+ // make sure that we are on 3g network
+ if(found && (best.type != ConnectionManager::GPRS || is3g()))
{
- best.id = gprsId_;
- qDebug() << "Trying gprs";
break;
}
+ if(found)
+ {
+ sleep(WAIT_BETWEEN_RETRIES);
+ }
+
qDebug() << "No connections found, retrying...";
cretries++;
- sleep(WAIT_BETWEEN_RETRIES);
-
}
if(cretries >= CONNECTION_LOOKUP_RETRIES)
return false;
}
+ qDebug() << "Connecting to " << best.name;
+
if(cm.connect(best.id))
{
break;
void CallListener::showError(QString const& msg, int timeout)
{
qDebug() << "Error: " << msg;
+
+ if(!initialized_ || !box_)
+ {
+ return;
+ }
+
box_->setTimeout(ERROR_BANNER_TIMEOUT);
if(timeout)
}
}
-void CallListener::timerEvent(QTimerEvent* event)
-{
- Q_UNUSED(event);
- killTimer(timer_);
- timer_ = 0;
-}
-
-void CallListener::sleep(int ms)
-{
- if(timer_)
- {
- killTimer(timer_);
- }
-
- timer_ = startTimer(ms);
-
- while(timer_)
- {
- QCoreApplication::processEvents(QEventLoop::WaitForMoreEvents);
- }
-}
-
-void CallListener::findGprsId()
-{
- ConnectionManager cm;
-
- QList<ConnectionManager::Connection> connections;
-
- if(cm.scanConnections(connections, ConnectionManager::GPRS) &&
- connections.size() > 0)
- {
- gprsId_ = connections.at(0).id;
- }
-}
-
bool CallListener::is3g()
{
QDBusMessage msg = QDBusMessage::createMethodCall("com.nokia.phone.net",
// Something else
return false;
}
+
+void CallListener::timerEvent(QTimerEvent* event)
+{
+ Q_UNUSED(event);
+ killTimer(timer_);
+ timer_ = 0;
+}
+
+void CallListener::sleep(int ms)
+{
+ if(timer_)
+ {
+ killTimer(timer_);
+ }
+
+ timer_ = startTimer(ms);
+
+ while(timer_)
+ {
+ QCoreApplication::processEvents(QEventLoop::WaitForMoreEvents);
+ }
+}