diff --git a/libs/declarative/CMakeLists.txt b/libs/declarative/CMakeLists.txt index fb3c997d..1bebf127 100644 --- a/libs/declarative/CMakeLists.txt +++ b/libs/declarative/CMakeLists.txt @@ -1,38 +1,39 @@ include_directories(models) set(plasmanm_qml_plugins_SRCS # FIXME ../debug.cpp availabledevices.cpp connectionicon.cpp enabledconnections.cpp enums.cpp networkstatus.cpp utils.cpp qmlplugins.cpp models/appletproxymodel.cpp models/creatableconnectionsmodel.cpp models/editorproxymodel.cpp models/kcmidentitymodel.cpp + models/mobileproxymodel.cpp models/networkitemslist.cpp models/networkmodel.cpp models/networkmodelitem.cpp ) add_library(plasmanm_qmlplugins SHARED ${plasmanm_qml_plugins_SRCS}) target_link_libraries(plasmanm_qmlplugins plasmanm_internal Qt5::Quick KF5::NetworkManagerQt KF5::I18n ) if (WITH_MODEMMANAGER_SUPPORT) target_link_libraries(plasmanm_qmlplugins KF5::ModemManagerQt) endif() install(TARGETS plasmanm_qmlplugins DESTINATION ${KDE_INSTALL_QMLDIR}/org/kde/plasma/networkmanagement) install(FILES qmldir DESTINATION ${KDE_INSTALL_QMLDIR}/org/kde/plasma/networkmanagement) diff --git a/libs/declarative/models/mobileproxymodel.cpp b/libs/declarative/models/mobileproxymodel.cpp new file mode 100644 index 00000000..e3fc7e04 --- /dev/null +++ b/libs/declarative/models/mobileproxymodel.cpp @@ -0,0 +1,133 @@ +/* + * Mobile proxy model - model for displaying netwoks in mobile kcm + * Copyright 2017 Martin Kacej + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License or (at your option) version 3 or any later version + * accepted by the membership of KDE e.V. (or its successor approved + * by the membership of KDE e.V.), which shall act as a proxy + * defined in Section 14 of version 3 of the license. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * +*/ + +#include "mobileproxymodel.h" +#include "networkmodel.h" +#include "networkmodelitem.h" +#include "uiutils.h" + +MobileProxyModel::MobileProxyModel(QObject* parent) + : QSortFilterProxyModel(parent) +{ + setDynamicSortFilter(true); + sort(0, Qt::DescendingOrder); +} + +MobileProxyModel::~MobileProxyModel() +{ +} + +void MobileProxyModel::setShowSavedMode(bool mode){ + m_showSavedMode = mode; + emit showSavedModeChanged(mode); + invalidate(); +} + +bool MobileProxyModel::showSavedMode() const{ + return m_showSavedMode; +} + +bool MobileProxyModel::filterAcceptsRow(int source_row, const QModelIndex& source_parent) const +{ + const QModelIndex index = sourceModel()->index(source_row, 0, source_parent); + + // slaves are always filtered-out + const bool isSlave = sourceModel()->data(index, NetworkModel::SlaveRole).toBool(); + + if (isSlave) { + return false; + } + + const NetworkManager::ConnectionSettings::ConnectionType type = (NetworkManager::ConnectionSettings::ConnectionType) sourceModel()->data(index, NetworkModel::TypeRole).toUInt(); + if (type == NetworkManager::ConnectionSettings::Wireless) { + NetworkModelItem::ItemType itemType = (NetworkModelItem::ItemType)sourceModel()->data(index, NetworkModel::ItemTypeRole).toUInt(); + if (showSavedMode()) { + return itemType == NetworkModelItem::UnavailableConnection; + } else { + if (sourceModel()->data(index,NetworkModel::SignalRole).toUInt() == 0) + return false; // if signal is 0 yet we still have connection, it means local access point -> we dont want to show that + return itemType >= NetworkModelItem::AvailableConnection; + } + } + return false; +} + +bool MobileProxyModel::lessThan(const QModelIndex& left, const QModelIndex& right) const +{ + const bool leftAvailable = (NetworkModelItem::ItemType)sourceModel()->data(left, NetworkModel::ItemTypeRole).toUInt() != NetworkModelItem::UnavailableConnection; + const bool leftConnected = sourceModel()->data(left, NetworkModel::ConnectionStateRole).toUInt() == NetworkManager::ActiveConnection::Activated; + const int leftConnectionState = sourceModel()->data(left, NetworkModel::ConnectionStateRole).toUInt(); + const QString leftName = sourceModel()->data(left, NetworkModel::NameRole).toString(); + const QString leftUuid = sourceModel()->data(left, NetworkModel::UuidRole).toString(); + const int leftSignal = sourceModel()->data(left, NetworkModel::SignalRole).toInt(); + const QDateTime leftDate = sourceModel()->data(left, NetworkModel::TimeStampRole).toDateTime(); + + const bool rightAvailable = (NetworkModelItem::ItemType)sourceModel()->data(right, NetworkModel::ItemTypeRole).toUInt() != NetworkModelItem::UnavailableConnection; + const bool rightConnected = sourceModel()->data(right, NetworkModel::ConnectionStateRole).toUInt() == NetworkManager::ActiveConnection::Activated; + const int rightConnectionState = sourceModel()->data(right, NetworkModel::ConnectionStateRole).toUInt(); + const QString rightName = sourceModel()->data(right, NetworkModel::NameRole).toString(); + const QString rightUuid = sourceModel()->data(right, NetworkModel::UuidRole).toString(); + const int rightSignal = sourceModel()->data(right, NetworkModel::SignalRole).toInt(); + const QDateTime rightDate = sourceModel()->data(right, NetworkModel::TimeStampRole).toDateTime(); + + if (leftAvailable < rightAvailable) { + return true; + } else if (leftAvailable > rightAvailable) { + return false; + } + + if (leftConnected < rightConnected) { + return true; + } else if (leftConnected > rightConnected) { + return false; + } + + if (leftConnectionState > rightConnectionState) { + return true; + } else if (leftConnectionState < rightConnectionState) { + return false; + } + + if (leftUuid.isEmpty() && !rightUuid.isEmpty()) { + return true; + } else if (!leftUuid.isEmpty() && rightUuid.isEmpty()) { + return false; + } + + if (leftDate > rightDate) { + return false; + } else if (leftDate < rightDate) { + return true; + } + + if (leftSignal < rightSignal) { + return true; + } else if (leftSignal > rightSignal) { + return false; + } + + if (QString::localeAwareCompare(leftName, rightName) > 0) { + return true; + } else { + return false; + } +} diff --git a/libs/declarative/models/mobileproxymodel.h b/libs/declarative/models/mobileproxymodel.h new file mode 100644 index 00000000..0fdd1731 --- /dev/null +++ b/libs/declarative/models/mobileproxymodel.h @@ -0,0 +1,48 @@ +/* + * Mobile proxy model - model for displaying netwoks in mobile kcm + * Copyright 2017 Martin Kacej + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License or (at your option) version 3 or any later version + * accepted by the membership of KDE e.V. (or its successor approved + * by the membership of KDE e.V.), which shall act as a proxy + * defined in Section 14 of version 3 of the license. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * +*/ + +#ifndef PLASMA_NM_MOBILE_PROXY_MODEL_H +#define PLASMA_NM_MOBILE_PROXY_MODEL_H + +#include + +class Q_DECL_EXPORT MobileProxyModel : public QSortFilterProxyModel +{ +Q_OBJECT + Q_PROPERTY(QAbstractItemModel * sourceModel READ sourceModel WRITE setSourceModel) + Q_PROPERTY(bool showSavedMode READ showSavedMode WRITE setShowSavedMode NOTIFY showSavedModeChanged) +public: + explicit MobileProxyModel(QObject* parent = 0); + virtual ~MobileProxyModel(); + void setShowSavedMode(bool mode); + bool showSavedMode() const; +signals: + void showSavedModeChanged(bool mode); + +protected: + bool filterAcceptsRow(int source_row, const QModelIndex& source_parent) const Q_DECL_OVERRIDE; + bool lessThan(const QModelIndex& left, const QModelIndex& right) const Q_DECL_OVERRIDE; +private: + bool m_showSavedMode; +}; + +#endif // PLASMA_NM_MOBILE_PROXY_MODEL_H