Changeset View
Changeset View
Standalone View
Standalone View
libs/handler.cpp
Show All 14 Lines | 1 | /* | |||
---|---|---|---|---|---|
15 | Lesser General Public License for more details. | 15 | Lesser General Public License for more details. | ||
16 | 16 | | |||
17 | You should have received a copy of the GNU Lesser General Public | 17 | You should have received a copy of the GNU Lesser General Public | ||
18 | License along with this library. If not, see <http://www.gnu.org/licenses/>. | 18 | License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||
19 | */ | 19 | */ | ||
20 | 20 | | |||
21 | #include "handler.h" | 21 | #include "handler.h" | ||
22 | #include "connectioneditordialog.h" | 22 | #include "connectioneditordialog.h" | ||
23 | #include "configuration.h" | ||||
23 | #include "uiutils.h" | 24 | #include "uiutils.h" | ||
24 | #include "debug.h" | 25 | #include "debug.h" | ||
25 | 26 | | |||
26 | #include <NetworkManagerQt/Manager> | 27 | #include <NetworkManagerQt/Manager> | ||
27 | #include <NetworkManagerQt/AccessPoint> | 28 | #include <NetworkManagerQt/AccessPoint> | ||
28 | #include <NetworkManagerQt/WiredDevice> | 29 | #include <NetworkManagerQt/WiredDevice> | ||
29 | #include <NetworkManagerQt/WirelessDevice> | 30 | #include <NetworkManagerQt/WirelessDevice> | ||
30 | #include <NetworkManagerQt/Settings> | 31 | #include <NetworkManagerQt/Settings> | ||
▲ Show 20 Lines • Show All 53 Lines • ▼ Show 20 Line(s) | 82 | QDBusConnection::sessionBus().connect(QStringLiteral(AGENT_SERVICE), | |||
84 | QStringLiteral("secretsError"), | 85 | QStringLiteral("secretsError"), | ||
85 | this, SLOT(secretAgentError(QString, QString))); | 86 | this, SLOT(secretAgentError(QString, QString))); | ||
86 | } | 87 | } | ||
87 | 88 | | |||
88 | Handler::~Handler() | 89 | Handler::~Handler() | ||
89 | { | 90 | { | ||
90 | } | 91 | } | ||
91 | 92 | | |||
93 | bool Handler::hotspotSupported() const | ||||
94 | { | ||||
95 | if (NetworkManager::checkVersion(1, 16, 0)) { | ||||
96 | for (const NetworkManager::Device::Ptr &device : NetworkManager::networkInterfaces()) { | ||||
97 | if (device->type() == NetworkManager::Device::Wifi) { | ||||
98 | return true; | ||||
99 | } | ||||
100 | } | ||||
101 | } | ||||
102 | | ||||
103 | return false; | ||||
104 | } | ||||
105 | | ||||
92 | void Handler::activateConnection(const QString& connection, const QString& device, const QString& specificObject) | 106 | void Handler::activateConnection(const QString& connection, const QString& device, const QString& specificObject) | ||
93 | { | 107 | { | ||
94 | NetworkManager::Connection::Ptr con = NetworkManager::findConnection(connection); | 108 | NetworkManager::Connection::Ptr con = NetworkManager::findConnection(connection); | ||
95 | 109 | | |||
96 | if (!con) { | 110 | if (!con) { | ||
97 | qCWarning(PLASMA_NM) << "Not possible to activate this connection"; | 111 | qCWarning(PLASMA_NM) << "Not possible to activate this connection"; | ||
98 | return; | 112 | return; | ||
99 | } | 113 | } | ||
▲ Show 20 Lines • Show All 423 Lines • ▼ Show 20 Line(s) | 505 | if (wifiDevice && wifiDevice->state() != NetworkManager::WirelessDevice::Unavailable) { | |||
523 | watcher->setProperty("action", Handler::RequestScan); | 537 | watcher->setProperty("action", Handler::RequestScan); | ||
524 | watcher->setProperty("interface", wifiDevice->interfaceName()); | 538 | watcher->setProperty("interface", wifiDevice->interfaceName()); | ||
525 | connect(watcher, &QDBusPendingCallWatcher::finished, this, &Handler::replyFinished); | 539 | connect(watcher, &QDBusPendingCallWatcher::finished, this, &Handler::replyFinished); | ||
526 | } | 540 | } | ||
527 | } | 541 | } | ||
528 | } | 542 | } | ||
529 | } | 543 | } | ||
530 | 544 | | |||
545 | void Handler::createHotspot() | ||||
546 | { | ||||
547 | bool foundInactive = false; | ||||
548 | bool useApMode = false; | ||||
549 | NetworkManager::WirelessDevice::Ptr wifiDev; | ||||
550 | | ||||
551 | NetworkManager::ConnectionSettings::Ptr connectionSettings; | ||||
552 | connectionSettings = NetworkManager::ConnectionSettings::Ptr(new NetworkManager::ConnectionSettings(NetworkManager::ConnectionSettings::Wireless)); | ||||
553 | | ||||
554 | NetworkManager::WirelessSetting::Ptr wifiSetting = connectionSettings->setting(NetworkManager::Setting::Wireless).dynamicCast<NetworkManager::WirelessSetting>(); | ||||
555 | wifiSetting->setMode(NetworkManager::WirelessSetting::Adhoc); | ||||
556 | wifiSetting->setSsid(Configuration::hotspotName().toUtf8()); | ||||
557 | | ||||
558 | for (const NetworkManager::Device::Ptr &device : NetworkManager::networkInterfaces()) { | ||||
559 | if (device->type() == NetworkManager::Device::Wifi) { | ||||
560 | wifiDev = device.objectCast<NetworkManager::WirelessDevice>(); | ||||
561 | if (wifiDev) { | ||||
562 | if (!wifiDev->isActive()) { | ||||
563 | foundInactive = true; | ||||
564 | } else { | ||||
565 | // Prefer previous device if it was inactive | ||||
566 | if (foundInactive) { | ||||
567 | break; | ||||
568 | } | ||||
569 | } | ||||
570 | | ||||
571 | if (wifiDev->wirelessCapabilities().testFlag(NetworkManager::WirelessDevice::ApCap)) { | ||||
572 | useApMode = true; | ||||
573 | } | ||||
574 | | ||||
575 | // We prefer inactive wireless card with AP capabilities | ||||
576 | if (foundInactive && useApMode) { | ||||
577 | break; | ||||
578 | } | ||||
579 | } | ||||
580 | } | ||||
581 | } | ||||
582 | | ||||
583 | if (!wifiDev) { | ||||
584 | qCWarning(PLASMA_NM) << "Failed to create hotspot: missing wireless device"; | ||||
585 | return; | ||||
586 | } | ||||
587 | | ||||
588 | wifiSetting->setInitialized(true); | ||||
589 | wifiSetting->setMode(useApMode ? NetworkManager::WirelessSetting::Ap :NetworkManager::WirelessSetting::Adhoc); | ||||
590 | | ||||
591 | if (!Configuration::hotspotPassword().isEmpty()) { | ||||
592 | NetworkManager::WirelessSecuritySetting::Ptr wifiSecurity = connectionSettings->setting(NetworkManager::Setting::WirelessSecurity).dynamicCast<NetworkManager::WirelessSecuritySetting>(); | ||||
593 | wifiSecurity->setInitialized(true); | ||||
594 | | ||||
595 | if (useApMode) { | ||||
596 | // Use WPA2 | ||||
597 | wifiSecurity->setKeyMgmt(NetworkManager::WirelessSecuritySetting::WpaPsk); | ||||
598 | wifiSecurity->setPsk(Configuration::hotspotPassword()); | ||||
599 | wifiSecurity->setPskFlags(NetworkManager::Setting::AgentOwned); | ||||
600 | } else { | ||||
601 | // Use WEP | ||||
602 | wifiSecurity->setKeyMgmt(NetworkManager::WirelessSecuritySetting::Wep); | ||||
603 | wifiSecurity->setWepKeyType(NetworkManager::WirelessSecuritySetting::Passphrase); | ||||
604 | wifiSecurity->setWepTxKeyindex(0); | ||||
605 | wifiSecurity->setWepKey0(Configuration::hotspotPassword()); | ||||
606 | wifiSecurity->setWepKeyFlags(NetworkManager::Setting::AgentOwned); | ||||
607 | wifiSecurity->setAuthAlg(NetworkManager::WirelessSecuritySetting::Open); | ||||
608 | } | ||||
609 | } | ||||
610 | | ||||
611 | NetworkManager::Ipv4Setting::Ptr ipv4Setting = connectionSettings->setting(NetworkManager::Setting::Ipv4).dynamicCast<NetworkManager::Ipv4Setting>(); | ||||
612 | ipv4Setting->setMethod(NetworkManager::Ipv4Setting::Shared); | ||||
613 | ipv4Setting->setInitialized(true); | ||||
614 | | ||||
615 | connectionSettings->setId(Configuration::hotspotName()); | ||||
616 | connectionSettings->setAutoconnect(false); | ||||
617 | connectionSettings->setUuid(NetworkManager::ConnectionSettings::createNewUuid()); | ||||
618 | | ||||
619 | QVariantMap options; | ||||
apol: I'd prefer the initializer list syntax
`const QVariantMap options = { {QLatin1String("persist")… | |||||
620 | options.insert(QLatin1String("persist"), QLatin1String("volatile")); | ||||
621 | | ||||
622 | QDBusPendingReply<QDBusObjectPath, QDBusObjectPath, QVariantMap> reply = NetworkManager::addAndActivateConnection2(connectionSettings->toMap(), wifiDev->uni(), QString(), options); | ||||
623 | QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(reply, this); | ||||
624 | watcher->setProperty("action", Handler::CreateHotspot); | ||||
625 | watcher->setProperty("connection", Configuration::hotspotName()); | ||||
626 | connect(watcher, &QDBusPendingCallWatcher::finished, this, &Handler::replyFinished); | ||||
627 | connect(watcher, &QDBusPendingCallWatcher::finished, this, &Handler::hotspotCreated); | ||||
apol: Use QOverload instead of static_cast, for readability. | |||||
628 | } | ||||
629 | | ||||
630 | void Handler::stopHotspot() | ||||
631 | { | ||||
632 | const QString activeConnectionPath = Configuration::hotspotConnectionPath(); | ||||
633 | | ||||
634 | if (activeConnectionPath.isEmpty()) { | ||||
635 | return; | ||||
636 | } | ||||
637 | | ||||
638 | NetworkManager::ActiveConnection::Ptr hotspot = NetworkManager::findActiveConnection(activeConnectionPath); | ||||
639 | | ||||
640 | if (!hotspot) { | ||||
641 | return; | ||||
642 | } | ||||
643 | | ||||
644 | NetworkManager::deactivateConnection(activeConnectionPath); | ||||
645 | Configuration::setHotspotConnectionPath(QString()); | ||||
646 | } | ||||
647 | | ||||
531 | bool Handler::checkRequestScanRateLimit(const NetworkManager::WirelessDevice::Ptr &wifiDevice) | 648 | bool Handler::checkRequestScanRateLimit(const NetworkManager::WirelessDevice::Ptr &wifiDevice) | ||
532 | { | 649 | { | ||
533 | QDateTime now = QDateTime::currentDateTime(); | 650 | QDateTime now = QDateTime::currentDateTime(); | ||
534 | QDateTime lastScan = wifiDevice->lastScan(); | 651 | QDateTime lastScan = wifiDevice->lastScan(); | ||
535 | QDateTime lastRequestScan = wifiDevice->lastRequestScan(); | 652 | QDateTime lastRequestScan = wifiDevice->lastRequestScan(); | ||
536 | 653 | | |||
537 | // if the last scan finished within the last 10 seconds | 654 | // if the last scan finished within the last 10 seconds | ||
538 | bool ret = lastScan.isValid() && lastScan.msecsTo(now) < NM_REQUESTSCAN_LIMIT_RATE; | 655 | bool ret = lastScan.isValid() && lastScan.msecsTo(now) < NM_REQUESTSCAN_LIMIT_RATE; | ||
▲ Show 20 Lines • Show All 89 Lines • ▼ Show 20 Line(s) | 742 | case Handler::UpdateConnection: | |||
628 | break; | 745 | break; | ||
629 | case Handler::RequestScan: | 746 | case Handler::RequestScan: | ||
630 | { | 747 | { | ||
631 | const QString interface = watcher->property("interface").toString(); | 748 | const QString interface = watcher->property("interface").toString(); | ||
632 | qCWarning(PLASMA_NM) << "Wireless scan on" << interface << "failed:" << error; | 749 | qCWarning(PLASMA_NM) << "Wireless scan on" << interface << "failed:" << error; | ||
633 | scanRequestFailed(interface); | 750 | scanRequestFailed(interface); | ||
634 | break; | 751 | break; | ||
635 | } | 752 | } | ||
753 | case Handler::CreateHotspot: | ||||
754 | notification = new KNotification("FailedToCreateHotspot", KNotification::CloseOnTimeout, this); | ||||
755 | notification->setTitle(i18n("Failed to create hotspot %1", watcher->property("connection").toString())); | ||||
756 | break; | ||||
636 | default: | 757 | default: | ||
637 | break; | 758 | break; | ||
638 | } | 759 | } | ||
639 | 760 | | |||
640 | if (notification) { | 761 | if (notification) { | ||
641 | notification->setComponentName("networkmanagement"); | 762 | notification->setComponentName("networkmanagement"); | ||
642 | notification->setText(error); | 763 | notification->setText(error); | ||
643 | notification->setIconName(QStringLiteral("dialog-warning")); | 764 | notification->setIconName(QStringLiteral("dialog-warning")); | ||
Show All 29 Lines | 791 | if (notification) { | |||
673 | notification->setIconName(QStringLiteral("dialog-information")); | 794 | notification->setIconName(QStringLiteral("dialog-information")); | ||
674 | notification->sendEvent(); | 795 | notification->sendEvent(); | ||
675 | } | 796 | } | ||
676 | } | 797 | } | ||
677 | 798 | | |||
678 | watcher->deleteLater(); | 799 | watcher->deleteLater(); | ||
679 | } | 800 | } | ||
680 | 801 | | |||
802 | void Handler::hotspotCreated(QDBusPendingCallWatcher *watcher) | ||||
803 | { | ||||
804 | QDBusPendingReply<QDBusObjectPath, QDBusObjectPath, QVariantMap> reply = *watcher; | ||||
805 | | ||||
806 | if (!reply.isError() && reply.isValid()) { | ||||
807 | const QString activeConnectionPath = reply.argumentAt(1).value<QDBusObjectPath>().path(); | ||||
808 | | ||||
809 | if (activeConnectionPath.isEmpty()) { | ||||
810 | return; | ||||
811 | } | ||||
812 | | ||||
813 | Configuration::setHotspotConnectionPath(activeConnectionPath); | ||||
814 | | ||||
815 | NetworkManager::ActiveConnection::Ptr hotspot = NetworkManager::findActiveConnection(activeConnectionPath); | ||||
816 | | ||||
817 | if (!hotspot) { | ||||
818 | return; | ||||
819 | } | ||||
820 | | ||||
821 | connect(hotspot.data(), &NetworkManager::ActiveConnection::stateChanged, [=] (NetworkManager::ActiveConnection::State state) { | ||||
822 | if (state > NetworkManager::ActiveConnection::Activated) { | ||||
823 | Configuration::setHotspotConnectionPath(QString()); | ||||
824 | } | ||||
825 | }); | ||||
826 | } | ||||
827 | } | ||||
828 | | ||||
681 | #if WITH_MODEMMANAGER_SUPPORT | 829 | #if WITH_MODEMMANAGER_SUPPORT | ||
682 | void Handler::unlockRequiredChanged(MMModemLock modemLock) | 830 | void Handler::unlockRequiredChanged(MMModemLock modemLock) | ||
683 | { | 831 | { | ||
684 | if (modemLock == MM_MODEM_LOCK_NONE) { | 832 | if (modemLock == MM_MODEM_LOCK_NONE) { | ||
685 | activateConnection(m_tmpConnectionPath, m_tmpDevicePath, m_tmpSpecificPath); | 833 | activateConnection(m_tmpConnectionPath, m_tmpDevicePath, m_tmpSpecificPath); | ||
686 | } | 834 | } | ||
687 | } | 835 | } | ||
688 | #endif | 836 | #endif | ||
689 | 837 | |
I'd prefer the initializer list syntax
const QVariantMap options = { {QLatin1String("persist"), QLatin1String("volatile")} };