diff --git a/connd/qconnectionagent.cpp b/connd/qconnectionagent.cpp index b364a04..376df72 100644 --- a/connd/qconnectionagent.cpp +++ b/connd/qconnectionagent.cpp @@ -57,14 +57,16 @@ QConnectionAgent::QConnectionAgent(QObject *parent) : valid = false; } - connect(this,SIGNAL(configurationNeeded(QString)),this,SLOT(openConnectionDialog(QString))); + connect(this, &QConnectionAgent::configurationNeeded, this, &QConnectionAgent::openConnectionDialog); - connect(netman,SIGNAL(availabilityChanged(bool)),this,SLOT(connmanAvailabilityChanged(bool))); - 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())); + connect(netman, &NetworkManager::availabilityChanged, this, &QConnectionAgent::connmanAvailabilityChanged); + connect(netman, &NetworkManager::servicesListChanged, this, &QConnectionAgent::servicesListChanged); + connect(netman, &NetworkManager::stateChanged, this, &QConnectionAgent::networkStateChanged); + connect(netman, &NetworkManager::offlineModeChanged, this, &QConnectionAgent::offlineModeChanged); + connect(netman, &NetworkManager::servicesChanged, this, [=]() { + updateServices(); + }); + connect(netman, &NetworkManager::technologiesChanged, this, &QConnectionAgent::techChanged); QFile connmanConf("/etc/connman/main.conf"); if (connmanConf.open(QIODevice::ReadOnly | QIODevice::Text)) { @@ -85,7 +87,7 @@ QConnectionAgent::QConnectionAgent(QObject *parent) : connmanAvailable = QDBusConnection::systemBus().interface()->isServiceRegistered("net.connman"); scanTimer = new QTimer(this); - connect(scanTimer,SIGNAL(timeout()),this,SLOT(scanTimeout())); + connect(scanTimer, &QTimer::timeout, this, &QConnectionAgent::scanTimeout); scanTimer->setSingleShot(true); if (connmanAvailable && valid) setup(); @@ -100,26 +102,10 @@ bool QConnectionAgent::isValid() const return valid; } -// from useragent -void QConnectionAgent::onUserInputRequested(const QString &servicePath, const QVariantMap &fields) -{ - qDebug() << servicePath; - // gets called when a connman service gets called to connect and needs more configurations. - Q_EMIT userInputRequested(servicePath, fields); -} - -// from useragent -void QConnectionAgent::onUserInputCanceled() -{ - qDebug() ; - Q_EMIT userInputCanceled(); -} - // from useragent void QConnectionAgent::onErrorReported(const QString &servicePath, const QString &error) { - // Suppress errors when switching to offline mode - if (error == "connect-failed" && servicePath.contains("cellular") && netman->offlineMode()) + if (shouldSuppressError(error, servicePath.contains("cellular"))) return; if (!tetheringWifiTech) return; @@ -189,19 +175,11 @@ void QConnectionAgent::servicesListChanged(const QStringList &list) void QConnectionAgent::serviceErrorChanged(const QString &error) { - qDebug() << error; - if (error == "Operation aborted") - return; NetworkService *service = static_cast(sender()); - - if (error == "connect-failed" - && (service->type() == "cellular") && netman->offlineMode()) { - return; - } - if (error == "In progress" || error.contains("Method")) // catch dbus errors and discard + if (shouldSuppressError(error, service->type() == QLatin1String("cellular"))) return; - Q_EMIT errorReported(service->path(),error); + Q_EMIT errorReported(service->path(), error); } void QConnectionAgent::serviceStateChanged(const QString &state) @@ -230,8 +208,6 @@ void QConnectionAgent::serviceStateChanged(const QString &state) if (delayedTethering && service->type() == "cellular" && tetheringWifiTech->tethering()) { Q_EMIT tetheringFinished(false); } -// serviceInProgress.clear(); -// // service->requestDisconnect(); } if (delayedTethering && service->type() == "wifi" && state == "association") { @@ -314,11 +290,6 @@ void QConnectionAgent::connectToType(const QString &type) Q_EMIT configurationNeeded(convType); } -void QConnectionAgent::onScanFinished() -{ - qDebug() << "<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"; -} - void QConnectionAgent::updateServices() { qDebug() << Q_FUNC_INFO; @@ -342,16 +313,14 @@ void QConnectionAgent::updateServices() //new! qDebug() <<"new service"<< servicePath; - QObject::connect(serv, SIGNAL(stateChanged(QString)), - this,SLOT(serviceStateChanged(QString)), Qt::UniqueConnection); - QObject::connect(serv, SIGNAL(connectRequestFailed(QString)), - this,SLOT(serviceErrorChanged(QString)), Qt::UniqueConnection); - - QObject::connect(serv, SIGNAL(errorChanged(QString)), - this,SLOT(servicesError(QString)), Qt::UniqueConnection); - - QObject::connect(serv, SIGNAL(autoConnectChanged(bool)), - this,SLOT(serviceAutoconnectChanged(bool)), Qt::UniqueConnection); + QObject::connect(serv, &NetworkService::stateChanged, + this, &QConnectionAgent::serviceStateChanged, Qt::UniqueConnection); + QObject::connect(serv, &NetworkService::connectRequestFailed, + this, &QConnectionAgent::serviceErrorChanged, Qt::UniqueConnection); + QObject::connect(serv, &NetworkService::errorChanged, + this, &QConnectionAgent::servicesError, Qt::UniqueConnection); + QObject::connect(serv, &NetworkService::autoConnectChanged, + this, &QConnectionAgent::serviceAutoconnectChanged, Qt::UniqueConnection); } } } @@ -363,7 +332,7 @@ void QConnectionAgent::servicesError(const QString &errorMessage) return; NetworkService *serv = static_cast(sender()); qDebug() << serv->name() << errorMessage; - Q_EMIT onErrorReported(serv->path(), errorMessage); + onErrorReported(serv->path(), errorMessage); } void QConnectionAgent::networkStateChanged(const QString &state) @@ -424,16 +393,15 @@ void QConnectionAgent::setup() delete ua; ua = new UserAgent(this); - connect(ua,SIGNAL(userInputRequested(QString,QVariantMap)), - this,SLOT(onUserInputRequested(QString,QVariantMap))); - - connect(ua,SIGNAL(connectionRequest()),this,SLOT(onConnectionRequest())); - connect(ua,SIGNAL(errorReported(QString, QString)),this,SLOT(onErrorReported(QString, QString))); - connect(ua,SIGNAL(userInputCanceled()),this,SLOT(onUserInputCanceled())); - connect(ua,SIGNAL(userInputRequested(QString,QVariantMap)), - this,SLOT(onUserInputRequested(QString,QVariantMap)), Qt::UniqueConnection); - connect(ua,SIGNAL(browserRequested(QString,QString)), - this,SLOT(browserRequest(QString,QString)), Qt::UniqueConnection); + connect(ua, &UserAgent::connectionRequest, this, &QConnectionAgent::onConnectionRequest); + connect(ua, &UserAgent::errorReported, this, &QConnectionAgent::onErrorReported); + connect(ua, &UserAgent::userInputCanceled, this, &QConnectionAgent::userInputCanceled); + connect(ua, &UserAgent::userInputRequested, this, &QConnectionAgent::userInputRequested); + connect(ua, &UserAgent::browserRequested, + this, [=](const QString &servicePath, const QString &url) { + Q_UNUSED(servicePath); + Q_EMIT requestBrowser(url); + }); updateServices(); offlineModeChanged(netman->offlineMode()); @@ -466,7 +434,7 @@ void QConnectionAgent::technologyPowerChanged(bool powered) if (netman && powered && delayedTethering) { // wifi tech might not be ready, so delay this - QTimer::singleShot(1000,this,SLOT(setWifiTetheringEnabled())); + QTimer::singleShot(1000, this, &QConnectionAgent::setWifiTetheringEnabled); } } @@ -491,10 +459,9 @@ void QConnectionAgent::techChanged() knownTechnologies << technology->path(); if (technology->type() == "wifi") { tetheringWifiTech = technology; - connect(tetheringWifiTech,SIGNAL(poweredChanged(bool)),this,SLOT(technologyPowerChanged(bool))); - connect(tetheringWifiTech,SIGNAL(scanFinished()),this,SLOT(onScanFinished())); - connect(tetheringWifiTech, SIGNAL(tetheringChanged(bool)), - this,SLOT(techTetheringChanged(bool)), Qt::UniqueConnection); + connect(tetheringWifiTech, &NetworkTechnology::poweredChanged, this, &QConnectionAgent::technologyPowerChanged); + connect(tetheringWifiTech, &NetworkTechnology::tetheringChanged, + this, &QConnectionAgent::techTetheringChanged, Qt::UniqueConnection); } } else { knownTechnologies.removeOne(technology->path()); @@ -502,15 +469,6 @@ void QConnectionAgent::techChanged() } } -void QConnectionAgent::browserRequest(const QString &servicePath, const QString &url) -{ - Q_UNUSED(servicePath) - qDebug() << servicePath; - qDebug() << url; - - Q_EMIT requestBrowser(url); -} - bool QConnectionAgent::isStateOnline(const QString &state) { if (state == "online" || state == "ready") @@ -547,7 +505,7 @@ void QConnectionAgent::offlineModeChanged(bool b) { flightModeSuppression = b; if (b) { - QTimer::singleShot(5 * 1000 * 60,this,SLOT(flightModeDialogSuppressionTimeout())); //5 minutes + QTimer::singleShot(5 * 1000 * 60, this, &QConnectionAgent::flightModeDialogSuppressionTimeout); //5 minutes } } @@ -569,22 +527,6 @@ void QConnectionAgent::serviceAutoconnectChanged(bool on) } } -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->tethering()) @@ -599,57 +541,6 @@ void QConnectionAgent::scanTimeout() } } -void QConnectionAgent::servicesChanged() -{ - qDebug(); - disconnect(netman,SIGNAL(servicesChanged()),this,SLOT(servicesChanged())); - - updateServices(); -} - -QString QConnectionAgent::findBestConnectableService() -{ - for (int i = 0; i < orderedServicesList.count(); i++) { - - QString path = orderedServicesList.at(i).path; - - NetworkService *service = orderedServicesList.at(i).service; - 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() <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 - - if (isBestService(service) - && service->favorite()) { - qDebug() << path; - return path; - } - } - return QString(); -} - void QConnectionAgent::removeAllTypes(const QString &type) { Q_FOREACH (Service elem, orderedServicesList) { @@ -658,6 +549,24 @@ void QConnectionAgent::removeAllTypes(const QString &type) } } +bool QConnectionAgent::shouldSuppressError(const QString &error, bool cellular) const +{ + if (error.isEmpty()) + return true; + if (error == QLatin1String("Operation aborted")) + return true; + if (error == QLatin1String("Already connected")) + return true; + if (error == QLatin1String("No carrier") && cellular) // Don't report cellular carrier lost. + return true; + if (error == QLatin1String("connect-failed") && cellular && netman->offlineMode()) // Suppress errors when switching to offline mode + return true; + if (error == QLatin1String("In progress") || error.contains(QLatin1String("Method"))) // Catch dbus errors and discard + return true; + + return false; +} + void QConnectionAgent::openConnectionDialog(const QString &type) { // open Connection Selector diff --git a/connd/qconnectionagent.h b/connd/qconnectionagent.h index f10abba..2b8ee02 100644 --- a/connd/qconnectionagent.h +++ b/connd/qconnectionagent.h @@ -38,7 +38,6 @@ class QConnectionAgent : public QObject bool isValid() const; Q_SIGNALS: - void userInputRequested(const QString &servicePath, const QVariantMap &fields); void userInputCanceled(); void errorReported(const QString &servicePath, const QString &error); @@ -51,9 +50,6 @@ class QConnectionAgent : public QObject void tetheringFinished(bool); public Q_SLOTS: - - void onUserInputRequested(const QString &servicePath, const QVariantMap &fields); - void onUserInputCanceled(); void onErrorReported(const QString &servicePath, const QString &error); void onConnectionRequest(); @@ -106,10 +102,10 @@ public Q_SLOTS: void setup(); void updateServices(); bool isStateOnline(const QString &state); - bool isBestService(NetworkService *service); - QString findBestConnectableService(); void removeAllTypes(const QString &type); + bool shouldSuppressError(const QString &error, bool cellular) const; + UserAgent *ua; NetworkManager *netman; QString currentNetworkState; @@ -129,8 +125,6 @@ public Q_SLOTS: bool valid; private slots: - void onScanFinished(); - void serviceErrorChanged(const QString &error); void serviceStateChanged(const QString &state); void networkStateChanged(const QString &state); @@ -138,7 +132,6 @@ private slots: void connmanAvailabilityChanged(bool b); void servicesError(const QString &); void technologyPowerChanged(bool); - void browserRequest(const QString &servicePath, const QString &url); void techChanged(); void servicesListChanged(const QStringList &); @@ -148,7 +141,6 @@ private slots: void serviceAutoconnectChanged(bool); void scanTimeout(); void techTetheringChanged(bool b); - void servicesChanged(); void openConnectionDialog(const QString &type); void setWifiTetheringEnabled(); diff --git a/connectionagentplugin/declarativeconnectionagent.cpp b/connectionagentplugin/declarativeconnectionagent.cpp index be0a18c..0b3d05a 100644 --- a/connectionagentplugin/declarativeconnectionagent.cpp +++ b/connectionagentplugin/declarativeconnectionagent.cpp @@ -28,16 +28,16 @@ DeclarativeConnectionAgent::DeclarativeConnectionAgent(QObject *parent): QObject(parent), - connManagerInterface(0) + connManagerInterface(nullptr) { connectiondWatcher = new QDBusServiceWatcher(CONND_SERVICE,QDBusConnection::sessionBus(), QDBusServiceWatcher::WatchForRegistration | QDBusServiceWatcher::WatchForUnregistration, this); - connect(connectiondWatcher, SIGNAL(serviceRegistered(QString)), - this, SLOT(connectToConnectiond(QString))); - connect(connectiondWatcher, SIGNAL(serviceUnregistered(QString)), - this, SLOT(connectiondUnregistered(QString))); + connect(connectiondWatcher, &QDBusServiceWatcher::serviceRegistered, + this, &DeclarativeConnectionAgent::connectToConnectiond); + connect(connectiondWatcher, &QDBusServiceWatcher::serviceUnregistered, + this, &DeclarativeConnectionAgent::connectiondUnregistered); connectToConnectiond(); } @@ -46,12 +46,10 @@ DeclarativeConnectionAgent::~DeclarativeConnectionAgent() { } -void DeclarativeConnectionAgent::connectToConnectiond(QString) +void DeclarativeConnectionAgent::connectToConnectiond() { - if (connManagerInterface) { - delete connManagerInterface; - connManagerInterface = 0; - } + delete connManagerInterface; + connManagerInterface = nullptr; if (!QDBusConnection::sessionBus().interface()->isServiceRegistered(CONND_SERVICE)) { qDebug() << Q_FUNC_INFO << QString("connection service not available").arg(CONND_SERVICE); @@ -68,28 +66,22 @@ void DeclarativeConnectionAgent::connectToConnectiond(QString) if (!connManagerInterface->isValid()) { qDebug() << Q_FUNC_INFO << "is not valid interface"; } - connect(connManagerInterface,SIGNAL(connectionRequest()), - this,SLOT(onConnectionRequested())); - connect(connManagerInterface,SIGNAL(configurationNeeded(QString)), - this,SIGNAL(configurationNeeded(QString))); - - connect(connManagerInterface,SIGNAL(userInputCanceled()), - this,SIGNAL(userInputCanceled())); - - connect(connManagerInterface, SIGNAL(errorReported(QString, QString)), - this, SLOT(onErrorReported(QString, QString))); - - connect(connManagerInterface,SIGNAL(connectionState(QString, QString)), - this,SLOT(onConnectionState(QString, QString))); - - connect(connManagerInterface,SIGNAL(requestBrowser(QString)), - this,SLOT(onRequestBrowser(QString))); - - connect(connManagerInterface,SIGNAL(userInputRequested(QString,QVariantMap)), - this,SLOT(onUserInputRequested(QString,QVariantMap)), Qt::UniqueConnection); - - connect(connManagerInterface,SIGNAL(tetheringFinished(bool)), - this,SLOT(onTetheringFinished(bool))); + connect(connManagerInterface, &com::jolla::Connectiond::connectionRequest, + this, &DeclarativeConnectionAgent::connectionRequest); + connect(connManagerInterface, &com::jolla::Connectiond::configurationNeeded, + this, &DeclarativeConnectionAgent::configurationNeeded); + connect(connManagerInterface, &com::jolla::Connectiond::userInputCanceled, + this, &DeclarativeConnectionAgent::userInputCanceled); + connect(connManagerInterface, &com::jolla::Connectiond::errorReported, + this, &DeclarativeConnectionAgent::errorReported); + connect(connManagerInterface, &com::jolla::Connectiond::connectionState, + this, &DeclarativeConnectionAgent::connectionState); + connect(connManagerInterface, &com::jolla::Connectiond::requestBrowser, + this, &DeclarativeConnectionAgent::browserRequested); + connect(connManagerInterface, &com::jolla::Connectiond::userInputRequested, + this, &DeclarativeConnectionAgent::onUserInputRequested, Qt::UniqueConnection); + connect(connManagerInterface, &com::jolla::Connectiond::tetheringFinished, + this, &DeclarativeConnectionAgent::tetheringFinished); } void DeclarativeConnectionAgent::sendUserReply(const QVariantMap &input) @@ -125,17 +117,6 @@ void DeclarativeConnectionAgent::connectToType(const QString &type) connManagerInterface->connectToType(type); } -void DeclarativeConnectionAgent::onErrorReported(const QString &servicePath, const QString &error) -{ - Q_EMIT errorReported(servicePath, error); -} - -void DeclarativeConnectionAgent::onRequestBrowser(const QString &url) -{ - qDebug() << Q_FUNC_INFO <startTethering(type); } -void DeclarativeConnectionAgent::onTetheringFinished(bool success) -{ - Q_EMIT tetheringFinished(success); -} - void DeclarativeConnectionAgent::stopTethering(bool keepPowered) { connManagerInterface->stopTethering(keepPowered); diff --git a/connectionagentplugin/declarativeconnectionagent.h b/connectionagentplugin/declarativeconnectionagent.h index befc985..1300c01 100644 --- a/connectionagentplugin/declarativeconnectionagent.h +++ b/connectionagentplugin/declarativeconnectionagent.h @@ -87,15 +87,10 @@ public slots: QDBusServiceWatcher *connectiondWatcher; private slots: - void onErrorReported(const QString &servicePath, const QString &error); - void onRequestBrowser(const QString &url); void onUserInputRequested(const QString &service, const QVariantMap &fields); - void onConnectionRequested(); - void onConnectionState(const QString &state, const QString &type); - void onTetheringFinished(bool); - void connectToConnectiond(const QString = QString()); - void connectiondUnregistered(const QString = QString()); + void connectToConnectiond(); + void connectiondUnregistered(); }; #endif diff --git a/test/auto/tst_connectionagent/tst_connectionagent.cpp b/test/auto/tst_connectionagent/tst_connectionagent.cpp index 96b9092..bd68d12 100644 --- a/test/auto/tst_connectionagent/tst_connectionagent.cpp +++ b/test/auto/tst_connectionagent/tst_connectionagent.cpp @@ -34,8 +34,6 @@ class Tst_connectionagent : public QObject Q_OBJECT private Q_SLOTS: - void tst_onUserInputRequested(); - void tst_onUserInputCanceled(); void tst_onErrorReported(); void tst_onConnectionRequest(); @@ -43,29 +41,6 @@ private Q_SLOTS: QConnectionAgent agent; }; -void Tst_connectionagent::tst_onUserInputRequested() -{ - QSignalSpy spy(&agent, SIGNAL(userInputRequested(QString,QVariantMap))); - QVariantMap map; - map.insert("test",true); - - agent.onUserInputRequested(QLatin1String("test_path"), map); - QCOMPARE(spy.count(),1); - QList arguments; - arguments = spy.takeFirst(); - QCOMPARE(arguments.at(0).toString(), QString("test_path")); - QVariantMap map2 = arguments.at(1).toMap(); - QCOMPARE(map2.keys().at(0), QString("test")); - -} - -void Tst_connectionagent::tst_onUserInputCanceled() -{ - QSignalSpy spy(&agent, SIGNAL(userInputCanceled())); - agent.onUserInputCanceled(); - QCOMPARE(spy.count(),1); -} - void Tst_connectionagent::tst_onErrorReported() { QSignalSpy spy(&agent, SIGNAL(errorReported(QString,QString)));