Skip to content

Commit

Permalink
Merge pull request #59 from lpotter/master
Browse files Browse the repository at this point in the history
dont connect when autoconnect is toggled if network is already connected
  • Loading branch information
lpotter committed Apr 15, 2014
2 parents e3d48c2 + f42a928 commit 75ef1a5
Show file tree
Hide file tree
Showing 3 changed files with 189 additions and 19 deletions.
201 changes: 182 additions & 19 deletions connd/qconnectionagent.cpp
Expand Up @@ -82,11 +82,14 @@ QConnectionAgent::QConnectionAgent(QObject *parent) :
qDebug() << "XXXXXXXXXXX could not register object XXXXXXXXXXXXXXXXXX";
}

connect(this,SIGNAL(configurationNeeded(QString)),this,SLOT(openConnectionDialog(QString)));
askForRoaming = askRoaming();

connect(netman,SIGNAL(servicesListChanged(QStringList)),this,SLOT(servicesListChanged(QStringList)));
connect(netman,SIGNAL(stateChanged(QString)),this,SLOT(networkStateChanged(QString)));
connect(netman,SIGNAL(offlineModeChanged(bool)),this,SLOT(offlineModeChanged(bool)));
connect(netman,SIGNAL(servicesChanged()),this,SLOT(servicesChanged()));
connect(netman,SIGNAL(technologiesChanged()),this,SLOT(techChanged()));

QFile connmanConf("/etc/connman/main.conf");
if (connmanConf.open(QIODevice::ReadOnly | QIODevice::Text)) {
Expand Down Expand Up @@ -274,6 +277,7 @@ void QConnectionAgent::connectToType(const QString &type)
if (service) {
if (service->favorite() && service->autoConnect()) {
needConfig = false;
qDebug() << "<<<<<<<<<<< requestConnect() >>>>>>>>>>>>";
service->requestConnect();
break;
}
Expand Down Expand Up @@ -309,8 +313,7 @@ void QConnectionAgent::updateServicesMap()

if (!oldServices.contains(servicePath)) {
//new!
qDebug() <<"new service"
<< servicePath;
qDebug() <<"new service"<< servicePath;

QObject::connect(serv, SIGNAL(stateChanged(QString)),
this,SLOT(serviceStateChanged(QString)), Qt::UniqueConnection);
Expand All @@ -325,8 +328,6 @@ void QConnectionAgent::updateServicesMap()
}
}
}

qDebug() << orderedServicesList;
}

void QConnectionAgent::servicesError(const QString &errorMessage)
Expand Down Expand Up @@ -444,12 +445,6 @@ void QConnectionAgent::setup()
updateServicesMap();
offlineModeChanged(netman->offlineMode());

tetheringWifiTech = netman->getTechnology("wifi");
QObject::connect(tetheringWifiTech,SIGNAL(scanFinished()),this,SLOT(onScanFinished()));

techChanged();
connect(netman,SIGNAL(technologiesChanged()),this,SLOT(techChanged()));

QSettings confFile;
confFile.beginGroup("Connectionagent");
scanTimeoutInterval = confFile.value("scanTimerInterval", "1").toUInt(); //in minutes
Expand All @@ -466,18 +461,27 @@ void QConnectionAgent::setup()
}
}

void QConnectionAgent::technologyPowerChanged(bool b)
void QConnectionAgent::technologyPowerChanged(bool powered)
{
NetworkTechnology *tech = static_cast<NetworkTechnology *>(sender());
qDebug() << tech->name() << b;

if (b && tech->type() == "wifi") {
tetheringWifiTech->scan();
qDebug() << tech->name() << powered;
if (tech->type() == "wifi") {
if (powered) {
tetheringWifiTech->scan();
} else {
removeAllTypes("wifi"); //dont wait for connman, he's too slow at this
QString bestService = findBestConnectableService();
if (!bestService.isEmpty()) {
qDebug() << "<<<<<<<<<<< requestConnect() >>>>>>>>>>>>";
servicesMap.value(bestService)->requestConnect();
}
}
}
}

void QConnectionAgent::techChanged()
{
qDebug() << netman->getTechnology("wifi");
if (netman->getTechnologies().isEmpty()) {
knownTechnologies.clear();
}
Expand All @@ -498,6 +502,7 @@ void QConnectionAgent::techChanged()
if (technology->type() == "wifi") {
tetheringWifiTech = technology;
connect(tetheringWifiTech,SIGNAL(poweredChanged(bool)),this,SLOT(technologyPowerChanged(bool)));
connect(tetheringWifiTech,SIGNAL(scanFinished()),this,SLOT(onScanFinished()));
}
} else {
knownTechnologies.removeOne(technology->path());
Expand All @@ -521,6 +526,12 @@ bool QConnectionAgent::isStateOnline(const QString &state)
return false;
}

void QConnectionAgent::techTetheringChanged(bool b)
{
qDebug() << b;
tetheringEnabled = b;
}

void QConnectionAgent::offlineModeChanged(bool b)
{
flightModeSuppression = b;
Expand Down Expand Up @@ -548,15 +559,56 @@ void QConnectionAgent::displayStateChanged(const QString &state)
void QConnectionAgent::serviceAutoconnectChanged(bool on)
{
NetworkService *service = qobject_cast<NetworkService *>(sender());

if (!service)
return;
qDebug() << service->path() << "AutoConnect is" << on;
bool mobileConnected = false;

if (!on) {
servicesMap.value(service->path())->requestDisconnect();
} else if (!servicesMap.value(service->path())->connected()){
servicesMap.value(service->path())->requestConnect();
if (service->state() != "idle")
service->requestDisconnect();

qDebug() << "find best service here";
QString selectedServicePath = findBestConnectableService();
if (!selectedServicePath.isEmpty()) {
qDebug() << "<<<<<<<<<<< requestConnect() >>>>>>>>>>>>";

servicesMap.value(selectedServicePath)->requestConnect();
}
} else {
if (service->type() == "cellular") {
QVector<NetworkService*> cellServices = netman->getServices("cellular");
Q_FOREACH (NetworkService *cService, cellServices) {
if (isStateOnline(cService->state())) {
mobileConnected = true;
}
}
}

if ((service->type() == "wifi" && mobileConnected)
|| isBestService(service)) {
qDebug() << "<<<<<<<<<<< requestConnect() >>>>>>>>>>>>";
service->requestConnect();
}
}
}

bool QConnectionAgent::isBestService(NetworkService *service)
{

qDebug() << Q_FUNC_INFO
<< service->path()
<< tetheringEnabled
<< netman->defaultRoute()->path().contains(service->path())
<< (netman->defaultRoute()->state() != "online");

if (tetheringEnabled) return false;
if (netman->offlineMode() && service->type() == "cellular") return false;
if (netman->defaultRoute()->type() == "wifi" && service->type() == "cellular") return false;
if (netman->defaultRoute()->path().contains(service->path())) return false;
return true;
}

void QConnectionAgent::scanTimeout()
{
if (tetheringWifiTech && tetheringWifiTech->powered() && !tetheringWifiTech->connected() && netman->defaultRoute()->type() != "wifi" ) {
Expand All @@ -567,3 +619,114 @@ void QConnectionAgent::scanTimeout()
}
}
}

void QConnectionAgent::servicesChanged()
{
qDebug();
disconnect(netman,SIGNAL(servicesChanged()),this,SLOT(servicesChanged()));

updateServicesMap();
}

QString QConnectionAgent::findBestConnectableService()
{
for (int i = 0; i < orderedServicesList.count(); i++) {

QString path = orderedServicesList.at(i);

NetworkService *service = servicesMap.value(path);
if (!service)
continue;

qDebug() << "looking at"
<< service->name()
<< service->autoConnect();

bool online = isStateOnline(netman->defaultRoute()->state());
qDebug() << "state is"<< online << netman->defaultRoute()->path();

if (!service->autoConnect()) {
continue;
}

qDebug() <<Q_FUNC_INFO<< "continued"
<< online
<< (netman->defaultRoute()->path() == service->path())
<< netman->defaultRoute()->strength()
<< service->strength();

if ((netman->defaultRoute()->type() == "wifi" && service->type() == "wifi")
&& netman->defaultRoute()->strength() > service->strength())
return QString(); //better quality already connected

if (netman->defaultRoute()->type() == "wifi" && service->type() != "wifi")
return QString(); // prefer connected wifi

bool isCellRoaming = false;
if (service->type() == "cellular" && service->roaming() && !service->connected()) {

isCellRoaming = askForRoaming;

QOfonoManager oManager;
if (!oManager.available()) {
qDebug() << "ofono not available.";
}
if (oManager.modems().count() < 1)
return QString();

QOfonoConnectionManager oConnManager;
oConnManager.setModemPath(oManager.modems().at(0));

if (oConnManager.roamingAllowed()) {
if (askRoaming()) {
// ask user
if (!flightModeSuppression) {
Q_EMIT connectionRequest();
}
return QString();
}
}
//roaming and user doesnt want connection while roaming
qDebug() << "roaming not allowed";
return QString();
}

if (isBestService(service)
&& service->favorite()
&& !isCellRoaming) {
qDebug() << path;
return path;
}
}
return QString();
}

void QConnectionAgent::removeAllTypes(const QString &type)
{
Q_FOREACH (const QString &path, orderedServicesList) {
if (path.contains(type))
orderedServicesList.removeOne(path);
}
}

void QConnectionAgent::openConnectionDialog(const QString &type)
{
// open Connection Selector
QDBusInterface *connSelectorInterface = new QDBusInterface(QStringLiteral("com.jolla.lipstick.ConnectionSelector"),
QStringLiteral("/"),
QStringLiteral("com.jolla.lipstick.ConnectionSelectorIf"),
QDBusConnection::sessionBus(),
this);

connSelectorInterface->connection().connect(QStringLiteral("com.jolla.lipstick.ConnectionSelector"),
QStringLiteral("/"),
QStringLiteral("com.jolla.lipstick.ConnectionSelectorIf"),
QStringLiteral("connectionSelectorClosed"),
this,
SLOT(connectionSelectorClosed(bool)));

QList<QVariant> args;
args.append(type);
QDBusMessage reply = connSelectorInterface->callWithArgumentList(QDBus::NoBlock,
QStringLiteral("openConnection"), args);
}
6 changes: 6 additions & 0 deletions connd/qconnectionagent.h
Expand Up @@ -104,6 +104,9 @@ public Q_SLOTS:

QTimer *scanTimer;
QStringList knownTechnologies;
bool isBestService(NetworkService *service);
QString findBestConnectableService();
void removeAllTypes(const QString &type);

private slots:
void onScanFinished();
Expand Down Expand Up @@ -132,7 +135,10 @@ private slots:

void serviceAutoconnectChanged(bool);
void scanTimeout();
void techTetheringChanged(bool b);
void servicesChanged();

void openConnectionDialog(const QString &type);
};

#endif // QCONNECTIONAGENT_H
1 change: 1 addition & 0 deletions rpm/connectionagent-qt5.spec
Expand Up @@ -14,6 +14,7 @@ Source1: connectionagent.tracing
Requires: connman-qt5-declarative
Requires: systemd
Requires: systemd-user-session-targets
Requires: connman >= 1.21
BuildRequires: pkgconfig(Qt5Core)
BuildRequires: pkgconfig(Qt5DBus)
BuildRequires: pkgconfig(connman-qt5)
Expand Down

0 comments on commit 75ef1a5

Please sign in to comment.