diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt index 2dc466917..bc853e70a 100644 --- a/plugins/CMakeLists.txt +++ b/plugins/CMakeLists.txt @@ -1,9 +1,13 @@ add_subdirectory("datetime") #add_subdirectory("disk-mount") -add_subdirectory("network") +if (NOT (${CMAKE_BUILD_TYPE} MATCHES "Debug")) + add_subdirectory("network") +else() + add_subdirectory("new-network") +endif () add_subdirectory("shutdown") add_subdirectory("sound") add_subdirectory("system-tray") #add_subdirectory("trash") -add_subdirectory("keyboard-layout") \ No newline at end of file +add_subdirectory("keyboard-layout") diff --git a/plugins/new-network/CMakeLists.txt b/plugins/new-network/CMakeLists.txt new file mode 100644 index 000000000..4bd5ad874 --- /dev/null +++ b/plugins/new-network/CMakeLists.txt @@ -0,0 +1,35 @@ + +set(PLUGIN_NAME "new-network") + +project(${PLUGIN_NAME}) + +# Sources files +file(GLOB_RECURSE SRCS "*.h" "*.cpp") + +find_package(PkgConfig REQUIRED) +find_package(Qt5Widgets REQUIRED) +find_package(Qt5Svg REQUIRED) +find_package(Qt5DBus REQUIRED) +find_package(DtkWidget REQUIRED) + +pkg_check_modules(DDE-Network-Utils REQUIRED dde-network-utils) +pkg_check_modules(DFrameworkDBus REQUIRED dframeworkdbus) + +add_definitions("${QT_DEFINITIONS} -DQT_PLUGIN") +add_library(${PLUGIN_NAME} SHARED ${SRCS} resources.qrc) +set_target_properties(${PLUGIN_NAME} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ../) +target_include_directories(${PLUGIN_NAME} PUBLIC ${DtkWidget_INCLUDE_DIRS} + ${Qt5DBus_INCLUDE_DIRS} + ${DFrameworkDBus_INCLUDE_DIRS} + ${DDE-Network-Utils_INCLUDE_DIRS} + ../../interfaces) +target_link_libraries(${PLUGIN_NAME} PRIVATE + ${DtkWidget_LIBRARIES} + ${Qt5Widgets_LIBRARIES} + ${Qt5Svg_LIBRARIES} + ${Qt5DBus_LIBRARIES} + ${DDE-Network-Utils_LIBRARIES} + ${DFrameworkDBus_LIBRARIES} +) + +install(TARGETS ${PLUGIN_NAME} LIBRARY DESTINATION lib/dde-dock/plugins) diff --git a/plugins/new-network/item/deviceitem.cpp b/plugins/new-network/item/deviceitem.cpp new file mode 100644 index 000000000..7fae3cb5e --- /dev/null +++ b/plugins/new-network/item/deviceitem.cpp @@ -0,0 +1,99 @@ +/* + * Copyright (C) 2011 ~ 2018 Deepin Technology Co., Ltd. + * + * Author: sbw + * + * Maintainer: sbw + * + * 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 3 of the License, or + * any later version. + * + * 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 "deviceitem.h" + +#include +#include + +using namespace dde::network; + +DeviceItem::DeviceItem(dde::network::NetworkDevice *device) + : QWidget(nullptr), + + m_device(device), + m_path(device->path()) +{ +} + +QSize DeviceItem::sizeHint() const +{ + return QSize(26, 26); +} + +const QString DeviceItem::itemCommand() const +{ + return QString(); +} + +const QString DeviceItem::itemContextMenu() +{ + QList items; + items.reserve(2); + + QMap enable; + enable["itemId"] = "enable"; + if (!m_device->enabled()) + enable["itemText"] = tr("Enable network"); + else + enable["itemText"] = tr("Disable network"); + enable["isActive"] = true; + items.push_back(enable); + + QMap settings; + settings["itemId"] = "settings"; + settings["itemText"] = tr("Network settings"); + settings["isActive"] = true; + items.push_back(settings); + + QMap menu; + menu["items"] = items; + menu["checkableMenu"] = false; + menu["singleCheck"] = false; + + return QJsonDocument::fromVariant(menu).toJson(); +} + +QWidget *DeviceItem::itemTips() +{ + return nullptr; +} + +void DeviceItem::invokeMenuItem(const QString &menuId) +{ + if (menuId == "settings") + //QProcess::startDetached("dbus-send --print-reply --dest=com.deepin.dde.ControlCenter /com/deepin/dde/ControlCenter com.deepin.dde.ControlCenter.ShowModule \"string:network\""); + DDBusSender() + .service("com.deepin.dde.ControlCenter") + .interface("com.deepin.dde.ControlCenter") + .path("/com/deepin/dde/ControlCenter") + .method("ShowModule") + .arg(QString("network")) + .call(); + + else if (menuId == "enable") + Q_EMIT requestSetDeviceEnable(m_path, !m_device->enabled()); +} + +QWidget *DeviceItem::itemApplet() +{ + return nullptr; +} diff --git a/plugins/new-network/item/deviceitem.h b/plugins/new-network/item/deviceitem.h new file mode 100644 index 000000000..81ba295b2 --- /dev/null +++ b/plugins/new-network/item/deviceitem.h @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2011 ~ 2018 Deepin Technology Co., Ltd. + * + * Author: sbw + * + * Maintainer: sbw + * + * 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 3 of the License, or + * any later version. + * + * 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 DEVICEITEM_H +#define DEVICEITEM_H + +#include + +#include + +class DeviceItem : public QWidget +{ + Q_OBJECT + +public: + explicit DeviceItem(dde::network::NetworkDevice *device); + + const QString &path() const { return m_path; } + + const dde::network::NetworkDevice * device() { return m_device; }; + + virtual void refreshIcon() = 0; + virtual const QString itemCommand() const; + virtual const QString itemContextMenu(); + virtual QWidget *itemApplet(); + virtual QWidget *itemTips(); + virtual void invokeMenuItem(const QString &menuId); + +signals: + void requestContextMenu() const; + void requestSetDeviceEnable(const QString &path, const bool enable) const; + +protected: + QSize sizeHint() const; + +protected: + dde::network::NetworkDevice *m_device; + +private: + QString m_path; +}; + +#endif // DEVICEITEM_H diff --git a/plugins/new-network/item/wireditem.cpp b/plugins/new-network/item/wireditem.cpp new file mode 100644 index 000000000..0d9050702 --- /dev/null +++ b/plugins/new-network/item/wireditem.cpp @@ -0,0 +1,165 @@ +/* + * Copyright (C) 2011 ~ 2018 Deepin Technology Co., Ltd. + * + * Author: sbw + * + * Maintainer: sbw + * listenerri + * + * 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 3 of the License, or + * any later version. + * + * 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 "constants.h" +#include "wireditem.h" +#include "util/imageutil.h" + +#include +#include +#include +#include + +using namespace dde::network; + +WiredItem::WiredItem(WiredDevice *device) + : DeviceItem(device), + + m_itemTips(new QLabel(this)), + m_delayTimer(new QTimer(this)) +{ +// QIcon::setThemeName("deepin"); + + m_delayTimer->setSingleShot(false); + m_delayTimer->setInterval(200); + + m_itemTips->setObjectName("wired-" + m_device->path()); + m_itemTips->setVisible(false); + m_itemTips->setStyleSheet("color:white;" + "padding:0px 3px;"); + + connect(m_delayTimer, &QTimer::timeout, this, &WiredItem::reloadIcon); + connect(m_device, static_cast(&NetworkDevice::statusChanged), this, &WiredItem::deviceStateChanged); +} + +QWidget *WiredItem::itemTips() +{ + m_itemTips->setText(tr("Unknown")); + + do { + if (m_device->status() != NetworkDevice::Activated) + { + m_itemTips->setText(tr("No Network")); + break; + } + + const QJsonObject info = static_cast(m_device)->activeConnection(); + if (!info.contains("Ip4")) + break; + const QJsonObject ipv4 = info.value("Ip4").toObject(); + if (!ipv4.contains("Address")) + break; + m_itemTips->setText(tr("Wired connection: %1").arg(ipv4.value("Address").toString())); + } while (false); + + return m_itemTips; +} + +const QString WiredItem::itemCommand() const +{ + return "dbus-send --print-reply --dest=com.deepin.dde.ControlCenter /com/deepin/dde/ControlCenter com.deepin.dde.ControlCenter.ShowModule \"string:network\""; +} + +void WiredItem::paintEvent(QPaintEvent *e) +{ + QWidget::paintEvent(e); + + QPainter painter(this); + const auto ratio = qApp->devicePixelRatio(); + const int x = rect().center().x() - m_icon.rect().center().x() / ratio; + const int y = rect().center().y() - m_icon.rect().center().y() / ratio; + painter.drawPixmap(x, y, m_icon); +} + +void WiredItem::resizeEvent(QResizeEvent *e) +{ + DeviceItem::resizeEvent(e); + + m_delayTimer->start(); +} + +void WiredItem::mousePressEvent(QMouseEvent *e) +{ + if (e->button() != Qt::RightButton) + return QWidget::mousePressEvent(e); + + const QPoint p(e->pos() - rect().center()); + if (p.manhattanLength() < std::min(width(), height()) * 0.8 * 0.5) + { + emit requestContextMenu(); + return; + } + + return QWidget::mousePressEvent(e); +} + +void WiredItem::refreshIcon() +{ + m_delayTimer->start(); +} + +void WiredItem::reloadIcon() +{ + Q_ASSERT(sender() == m_delayTimer); + + const Dock::DisplayMode displayMode = qApp->property(PROP_DISPLAY_MODE).value(); + + QString iconName = "network-"; + NetworkDevice::DeviceStatus devState = m_device->status(); + if (devState != NetworkDevice::Activated) + { + if (devState < NetworkDevice::Disconnected) + iconName.append("error"); + else + iconName.append("offline"); + } else { + if (devState >= NetworkDevice::Prepare && devState <= NetworkDevice::Secondaries) { + m_delayTimer->start(); + const quint64 index = QDateTime::currentMSecsSinceEpoch() / 200; + const int num = (index % 5) + 1; + m_icon = QPixmap(QString(":/wired/resources/wired/network-wired-symbolic-connecting%1.svg").arg(num)); + update(); + return; + } + + if (devState == NetworkDevice::Activated) + iconName.append("online"); + else + iconName.append("idle"); + } + + m_delayTimer->stop(); + + if (displayMode == Dock::Efficient) + iconName.append("-symbolic"); + + const auto ratio = qApp->devicePixelRatio(); + const int size = displayMode == Dock::Efficient ? 16 : std::min(width(), height()) * 0.8; + m_icon = QIcon::fromTheme(iconName).pixmap(size * ratio, size * ratio); + m_icon.setDevicePixelRatio(ratio); + update(); +} + +void WiredItem::deviceStateChanged() +{ + m_delayTimer->start(); +} diff --git a/plugins/new-network/item/wireditem.h b/plugins/new-network/item/wireditem.h new file mode 100644 index 000000000..0999b03cc --- /dev/null +++ b/plugins/new-network/item/wireditem.h @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2011 ~ 2018 Deepin Technology Co., Ltd. + * + * Author: sbw + * + * Maintainer: sbw + * + * 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 3 of the License, or + * any later version. + * + * 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 WIREDITEM_H +#define WIREDITEM_H + +#include "deviceitem.h" + +#include +#include +#include + +#include + +class WiredItem : public DeviceItem +{ + Q_OBJECT + +public: + explicit WiredItem(dde::network::WiredDevice *device); + + QWidget *itemTips() override; + const QString itemCommand() const override; + +protected: + void paintEvent(QPaintEvent *e) override; + void resizeEvent(QResizeEvent *e) override; + void mousePressEvent(QMouseEvent *e) override; + +private slots: + void refreshIcon() override; + void reloadIcon(); + void deviceStateChanged(); + +private: + QPixmap m_icon; + + QLabel *m_itemTips; + QTimer *m_delayTimer; +}; + +#endif // WIREDITEM_H diff --git a/plugins/new-network/item/wirelessitem.cpp b/plugins/new-network/item/wirelessitem.cpp new file mode 100644 index 000000000..18a07f4c9 --- /dev/null +++ b/plugins/new-network/item/wirelessitem.cpp @@ -0,0 +1,225 @@ +/* + * Copyright (C) 2011 ~ 2018 Deepin Technology Co., Ltd. + * + * Author: sbw + * + * Maintainer: sbw + * + * 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 3 of the License, or + * any later version. + * + * 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 "wirelessitem.h" +#include "util/imageutil.h" + +#include +#include +#include +#include + +using namespace dde::network; + +WirelessItem::WirelessItem(WirelessDevice *device) + : DeviceItem(device), + + m_refershTimer(new QTimer(this)), + m_wirelessApplet(new QWidget), + m_wirelessPopup(new QLabel) + //m_APList(nullptr) +{ + m_refershTimer->setSingleShot(false); + m_refershTimer->setInterval(100); + + m_wirelessApplet->setVisible(false); + m_wirelessPopup->setObjectName("wireless-" + m_device->path()); + m_wirelessPopup->setVisible(false); + m_wirelessPopup->setStyleSheet("color:white;" + "padding: 0px 3px;"); + + connect(m_device, static_cast(&NetworkDevice::statusChanged), m_refershTimer, static_cast(&QTimer::start)); + connect(m_refershTimer, &QTimer::timeout, this, static_cast(&WirelessItem::update)); + QMetaObject::invokeMethod(this, "init", Qt::QueuedConnection); +} + +WirelessItem::~WirelessItem() +{ + //m_APList->deleteLater(); + //m_APList->controlPanel()->deleteLater(); +} + +QWidget *WirelessItem::itemApplet() +{ + return m_wirelessApplet; +} + +QWidget *WirelessItem::itemTips() +{ + const NetworkDevice::DeviceStatus stat = m_device->status(); + + m_wirelessPopup->setText(tr("No Network")); + + if (stat == NetworkDevice::Activated) + { + const QJsonObject obj = static_cast(m_device)->activeApInfo(); + if (obj.contains("Ip4")) + { + const QJsonObject ip4 = obj["Ip4"].toObject(); + if (ip4.contains("Address")) + { + m_wirelessPopup->setText(tr("Wireless Connection: %1").arg(ip4["Address"].toString())); + } + } + } + + return m_wirelessPopup; +} + +bool WirelessItem::eventFilter(QObject *o, QEvent *e) +{ + /* TODO: refactor */ + //if (o == m_APList && e->type() == QEvent::Resize) + //QMetaObject::invokeMethod(this, "adjustHeight", Qt::QueuedConnection); + + return false; +} + +void WirelessItem::paintEvent(QPaintEvent *e) +{ + DeviceItem::paintEvent(e); + + const Dock::DisplayMode displayMode = qApp->property(PROP_DISPLAY_MODE).value(); + + const auto ratio = qApp->devicePixelRatio(); + const int iconSize = displayMode == Dock::Fashion ? std::min(width(), height()) * 0.8 : 16; + QPixmap pixmap = iconPix(displayMode, iconSize * ratio); + pixmap.setDevicePixelRatio(ratio); + + QPainter painter(this); + if (displayMode == Dock::Fashion) + { + QPixmap pixmap = backgroundPix(iconSize * ratio); + pixmap.setDevicePixelRatio(ratio); + painter.drawPixmap(rect().center() - pixmap.rect().center() / ratio, pixmap); + } + painter.drawPixmap(rect().center() - pixmap.rect().center() / ratio, pixmap); +} + +void WirelessItem::resizeEvent(QResizeEvent *e) +{ + DeviceItem::resizeEvent(e); + + m_icons.clear(); +} + +void WirelessItem::mousePressEvent(QMouseEvent *e) +{ + if (e->button() != Qt::RightButton) + return e->ignore(); + + const QPoint p(e->pos() - rect().center()); + if (p.manhattanLength() < std::min(width(), height()) * 0.8 * 0.5) + { + emit requestContextMenu(); + return; + } + + return QWidget::mousePressEvent(e); +} + +const QPixmap WirelessItem::iconPix(const Dock::DisplayMode displayMode, const int size) +{ + QString type; + + /* TODO: refactor */ + //const auto state = m_APList->wirelessState(); + const auto state = m_device->status(); + if (state <= NetworkDevice::Disconnected) + { + type = "disconnect"; + m_refershTimer->stop(); + } + else + { + int strength = 0; + if (state == NetworkDevice::Activated) + { + strength = static_cast(m_device)->activeApStrgength(); + m_refershTimer->stop(); + } + else + { + strength = QTime::currentTime().msec() / 10 % 100; + if (!m_refershTimer->isActive()) + m_refershTimer->start(); + } + + if (strength == 100) + type = "80"; + else if (strength < 20) + type = "0"; + else + type = QString::number(strength / 10 & ~0x1) + "0"; + } + + const QString key = QString("wireless-%1%2") + .arg(type) + .arg(displayMode == Dock::Fashion ? "" : "-symbolic"); + + return cachedPix(key, size); +} + +const QPixmap WirelessItem::backgroundPix(const int size) +{ + return cachedPix("wireless-background", size); +} + +const QPixmap WirelessItem::cachedPix(const QString &key, const int size) +{ + if (!m_icons.contains(key)) + m_icons.insert(key, QIcon::fromTheme(key, + QIcon(":/wireless/resources/wireless/" + key + ".svg")).pixmap(size)); + + return m_icons.value(key); +} + +void WirelessItem::init() +{ + /* TODO: refactor */ + //const auto devInfo = m_networkManager->device(m_devicePath); + + //m_APList = new WirelessList(devInfo); + //m_APList->installEventFilter(this); + //m_APList->setObjectName("wireless-" + m_devicePath); + + //QVBoxLayout *vLayout = new QVBoxLayout; + //vLayout->addWidget(m_APList->controlPanel()); + //vLayout->addWidget(m_APList); + //vLayout->setMargin(0); + //vLayout->setSpacing(0); + //m_wirelessApplet->setLayout(vLayout); + + //connect(m_APList, &WirelessList::activeAPChanged, this, static_cast(&WirelessItem::update)); + //connect(m_APList, &WirelessList::wirelessStateChanged, this, static_cast(&WirelessItem::update)); +} + +void WirelessItem::adjustHeight() +{ + /* TODO: refactor */ + //m_wirelessApplet->setFixedHeight(m_APList->height() + m_APList->controlPanel()->height()); +} + +void WirelessItem::refreshIcon() +{ + update(); +} + diff --git a/plugins/new-network/item/wirelessitem.h b/plugins/new-network/item/wirelessitem.h new file mode 100644 index 000000000..09a1b345a --- /dev/null +++ b/plugins/new-network/item/wirelessitem.h @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2011 ~ 2018 Deepin Technology Co., Ltd. + * + * Author: sbw + * + * Maintainer: sbw + * + * 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 3 of the License, or + * any later version. + * + * 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 WIRELESSITEM_H +#define WIRELESSITEM_H + +#include "constants.h" + +#include "deviceitem.h" +//#include "applet/wirelessapplet.h" + +#include +#include + +#include + +class WirelessItem : public DeviceItem +{ + Q_OBJECT + +public: + explicit WirelessItem(dde::network::WirelessDevice *device); + ~WirelessItem(); + + QWidget *itemApplet(); + QWidget *itemTips(); + +protected: + bool eventFilter(QObject *o, QEvent *e); + void paintEvent(QPaintEvent *e); + void resizeEvent(QResizeEvent *e); + void mousePressEvent(QMouseEvent *e); + +private: + const QPixmap iconPix(const Dock::DisplayMode displayMode, const int size); + const QPixmap backgroundPix(const int size); + const QPixmap cachedPix(const QString &key, const int size); + +private slots: + void init(); + void adjustHeight(); + void refreshIcon(); + +private: + QHash m_icons; + + QTimer *m_refershTimer; + QWidget *m_wirelessApplet; + QLabel *m_wirelessPopup; + /* TODO: refactor */ + //WirelessList *m_APList; +}; + +#endif // WIRELESSITEM_H diff --git a/plugins/new-network/network.json b/plugins/new-network/network.json new file mode 100644 index 000000000..b9b78cb89 --- /dev/null +++ b/plugins/new-network/network.json @@ -0,0 +1,3 @@ +{ + "api": "1.0" +} diff --git a/plugins/new-network/networkplugin.cpp b/plugins/new-network/networkplugin.cpp new file mode 100644 index 000000000..96a2b276b --- /dev/null +++ b/plugins/new-network/networkplugin.cpp @@ -0,0 +1,209 @@ +/* + * Copyright (C) 2011 ~ 2018 Deepin Technology Co., Ltd. + * + * Author: sbw + * + * Maintainer: sbw + * + * 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 3 of the License, or + * any later version. + * + * 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 "networkplugin.h" +#include +#include + +using namespace dde::network; + +#define WIRED_ITEM "wired" +#define WIRELESS_ITEM "wireless" +#define STATE_KEY "enabled" + +NetworkPlugin::NetworkPlugin(QObject *parent) + : QObject(parent), + + m_networkModel(nullptr), + m_networkWorker(nullptr), + m_settings("deepin", "dde-dock-network") +{ +} + +const QString NetworkPlugin::pluginName() const +{ + return "new-network"; +} + +const QString NetworkPlugin::pluginDisplayName() const +{ + return tr("NewNetwork"); +} + +void NetworkPlugin::init(PluginProxyInterface *proxyInter) +{ + m_proxyInter = proxyInter; + + m_networkModel = new NetworkModel; + m_networkWorker = new NetworkWorker(m_networkModel); + + connect(m_networkModel, &NetworkModel::deviceListChanged, this, &NetworkPlugin::onDeviceListChanged); + + m_networkModel->moveToThread(qApp->thread()); + m_networkWorker->moveToThread(qApp->thread()); + + onDeviceListChanged(m_networkModel->devices()); +} + +void NetworkPlugin::invokedMenuItem(const QString &itemKey, const QString &menuId, const bool checked) +{ + Q_UNUSED(checked) + + DeviceItem *item = itemByPath(itemKey); + if (item) { + return item->invokeMenuItem(menuId); + } + + Q_UNREACHABLE(); +} + +void NetworkPlugin::refershIcon(const QString &itemKey) +{ + Q_UNUSED(itemKey); + +} + +void NetworkPlugin::pluginStateSwitched() +{ + m_settings.setValue(STATE_KEY, !m_settings.value(STATE_KEY, true).toBool()); +} + +bool NetworkPlugin::pluginIsDisable() +{ + return !m_settings.value(STATE_KEY, true).toBool(); +} + +const QString NetworkPlugin::itemCommand(const QString &itemKey) +{ + DeviceItem *item = itemByPath(itemKey); + if (item) { + return item->itemCommand(); + } + + Q_UNREACHABLE(); + return QString(); +} + +const QString NetworkPlugin::itemContextMenu(const QString &itemKey) +{ + DeviceItem *item = itemByPath(itemKey); + if (item) { + return item->itemContextMenu(); + } + + Q_UNREACHABLE(); + return QString(); +} + +QWidget *NetworkPlugin::itemWidget(const QString &itemKey) +{ + return itemByPath(itemKey); +} + +QWidget *NetworkPlugin::itemTipsWidget(const QString &itemKey) +{ + DeviceItem *item = itemByPath(itemKey); + if (item) { + return item->itemTips(); + } + + Q_UNREACHABLE(); + return nullptr; +} + +QWidget *NetworkPlugin::itemPopupApplet(const QString &itemKey) +{ + /* TODO: refactor */ + return nullptr; +} + +int NetworkPlugin::itemSortKey(const QString &itemKey) +{ + const QString key = QString("pos_%1_%2").arg(itemKey).arg(displayMode()); + return m_settings.value(key, 0).toInt(); +} + +void NetworkPlugin::setSortKey(const QString &itemKey, const int order) +{ + const QString key = QString("pos_%1_%2").arg(itemKey).arg(displayMode()); + m_settings.setValue(key, order); +} + +void NetworkPlugin::onDeviceListChanged(const QList devices) +{ + QList mPaths = m_itemsMap.keys(); + QList newPaths; + + for (auto device : devices) { + const QString &path = device->path(); + newPaths << path; + // new device + if (!mPaths.contains(path)) { + DeviceItem *item = nullptr; + switch (device->type()) { + case NetworkDevice::Wired: + item = new WiredItem(static_cast(device)); + break; + case NetworkDevice::Wireless: + item = new WirelessItem(static_cast(device)); + break; + default: + Q_UNREACHABLE(); + } + + mPaths << path; + m_itemsMap.insert(path, item); + m_proxyInter->itemAdded(this, path); + + connect(item, &DeviceItem::requestContextMenu, this, &NetworkPlugin::contextMenuRequested); + connect(item, &DeviceItem::requestSetDeviceEnable, m_networkWorker, &NetworkWorker::setDeviceEnable); + } + } + + for (auto mPath : mPaths) { + // removed device + if (!newPaths.contains(mPath)) { + m_proxyInter->itemRemoved(this, mPath); + m_itemsMap.take(mPath)->deleteLater(); + } + } +} + +DeviceItem *NetworkPlugin::itemByPath(const QString &path) +{ + for (auto item : m_itemsMap.values()) { + if (item->path() == path) { + return item; + } + } + + Q_UNREACHABLE(); + return nullptr; +} + + +void NetworkPlugin::contextMenuRequested() +{ + DeviceItem *item = qobject_cast(sender()); + Q_ASSERT(item); + + m_proxyInter->requestContextMenu(this, item->path()); +} diff --git a/plugins/new-network/networkplugin.h b/plugins/new-network/networkplugin.h new file mode 100644 index 000000000..7db65de33 --- /dev/null +++ b/plugins/new-network/networkplugin.h @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2011 ~ 2018 Deepin Technology Co., Ltd. + * + * Author: sbw + * + * Maintainer: sbw + * + * 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 3 of the License, or + * any later version. + * + * 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 NETWORKPLUGIN_H +#define NETWORKPLUGIN_H + +#include "pluginsiteminterface.h" +#include + +#include +#include +#include + +class NetworkPlugin : public QObject, PluginsItemInterface +{ + Q_OBJECT + Q_INTERFACES(PluginsItemInterface) + Q_PLUGIN_METADATA(IID "com.deepin.dock.PluginsItemInterface" FILE "network.json") + +public: + explicit NetworkPlugin(QObject *parent = 0); + + const QString pluginName() const; + const QString pluginDisplayName() const; + void init(PluginProxyInterface *proxyInter); + void invokedMenuItem(const QString &itemKey, const QString &menuId, const bool checked); + void refershIcon(const QString &itemKey); + void pluginStateSwitched(); + bool pluginIsAllowDisable() { return true; } + bool pluginIsDisable(); + const QString itemCommand(const QString &itemKey); + const QString itemContextMenu(const QString &itemKey); + QWidget *itemWidget(const QString &itemKey); + QWidget *itemTipsWidget(const QString &itemKey); + QWidget *itemPopupApplet(const QString &itemKey); + + int itemSortKey(const QString &itemKey); + void setSortKey(const QString &itemKey, const int order); + +private slots: + void onDeviceListChanged(const QList devices); + void contextMenuRequested(); + +private: + DeviceItem *itemByPath(const QString &path); + +private: + dde::network::NetworkModel *m_networkModel; + dde::network::NetworkWorker *m_networkWorker; + + QMap m_itemsMap; + QSettings m_settings; +}; + +#endif // NETWORKPLUGIN_H diff --git a/plugins/new-network/resources.qrc b/plugins/new-network/resources.qrc new file mode 100644 index 000000000..63321d657 --- /dev/null +++ b/plugins/new-network/resources.qrc @@ -0,0 +1,24 @@ + + + resources/wired/network-wired-symbolic-connecting1.svg + resources/wired/network-wired-symbolic-connecting2.svg + resources/wired/network-wired-symbolic-connecting3.svg + resources/wired/network-wired-symbolic-connecting4.svg + resources/wired/network-wired-symbolic-connecting5.svg + + + resources/wireless/wireless-0.svg + resources/wireless/wireless-20.svg + resources/wireless/wireless-40.svg + resources/wireless/wireless-60.svg + resources/wireless/wireless-80.svg + resources/wireless/wireless-background.svg + resources/wireless/wireless-disconnect.svg + resources/wireless/wireless-0-symbolic.svg + resources/wireless/wireless-20-symbolic.svg + resources/wireless/wireless-40-symbolic.svg + resources/wireless/wireless-60-symbolic.svg + resources/wireless/wireless-80-symbolic.svg + resources/wireless/wireless-disconnect-symbolic.svg + + diff --git a/plugins/new-network/resources/wired/network-wired-symbolic-connecting1.svg b/plugins/new-network/resources/wired/network-wired-symbolic-connecting1.svg new file mode 100644 index 000000000..4df47e0d9 --- /dev/null +++ b/plugins/new-network/resources/wired/network-wired-symbolic-connecting1.svg @@ -0,0 +1,13 @@ + + + + network-wired-symbolic-connecting1 + Created with Sketch. + + + + + + + + \ No newline at end of file diff --git a/plugins/new-network/resources/wired/network-wired-symbolic-connecting2.svg b/plugins/new-network/resources/wired/network-wired-symbolic-connecting2.svg new file mode 100644 index 000000000..0dc1af6f3 --- /dev/null +++ b/plugins/new-network/resources/wired/network-wired-symbolic-connecting2.svg @@ -0,0 +1,13 @@ + + + + network-wired-symbolic-connecting2 + Created with Sketch. + + + + + + + + \ No newline at end of file diff --git a/plugins/new-network/resources/wired/network-wired-symbolic-connecting3.svg b/plugins/new-network/resources/wired/network-wired-symbolic-connecting3.svg new file mode 100644 index 000000000..4c6ce3716 --- /dev/null +++ b/plugins/new-network/resources/wired/network-wired-symbolic-connecting3.svg @@ -0,0 +1,13 @@ + + + + network-wired-symbolic-connecting3 + Created with Sketch. + + + + + + + + \ No newline at end of file diff --git a/plugins/new-network/resources/wired/network-wired-symbolic-connecting4.svg b/plugins/new-network/resources/wired/network-wired-symbolic-connecting4.svg new file mode 100644 index 000000000..487a15eb7 --- /dev/null +++ b/plugins/new-network/resources/wired/network-wired-symbolic-connecting4.svg @@ -0,0 +1,13 @@ + + + + network-wired-symbolic-connecting4 + Created with Sketch. + + + + + + + + \ No newline at end of file diff --git a/plugins/new-network/resources/wired/network-wired-symbolic-connecting5.svg b/plugins/new-network/resources/wired/network-wired-symbolic-connecting5.svg new file mode 100644 index 000000000..1f76ba5ce --- /dev/null +++ b/plugins/new-network/resources/wired/network-wired-symbolic-connecting5.svg @@ -0,0 +1,13 @@ + + + + network-wired-symbolic-connecting5 + Created with Sketch. + + + + + + + + \ No newline at end of file diff --git a/plugins/new-network/resources/wireless/wireless-0-symbolic.svg b/plugins/new-network/resources/wireless/wireless-0-symbolic.svg new file mode 100644 index 000000000..5b6560d5c --- /dev/null +++ b/plugins/new-network/resources/wireless/wireless-0-symbolic.svg @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + diff --git a/plugins/new-network/resources/wireless/wireless-0.svg b/plugins/new-network/resources/wireless/wireless-0.svg new file mode 100644 index 000000000..b7d094690 --- /dev/null +++ b/plugins/new-network/resources/wireless/wireless-0.svg @@ -0,0 +1,15 @@ + + + + wireless-0-48px + Created with Sketch. + + + + + + + + + + \ No newline at end of file diff --git a/plugins/new-network/resources/wireless/wireless-20-symbolic.svg b/plugins/new-network/resources/wireless/wireless-20-symbolic.svg new file mode 100644 index 000000000..35cff25dd --- /dev/null +++ b/plugins/new-network/resources/wireless/wireless-20-symbolic.svg @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + diff --git a/plugins/new-network/resources/wireless/wireless-20.svg b/plugins/new-network/resources/wireless/wireless-20.svg new file mode 100644 index 000000000..2622c854f --- /dev/null +++ b/plugins/new-network/resources/wireless/wireless-20.svg @@ -0,0 +1,15 @@ + + + + wireless-20-48px + Created with Sketch. + + + + + + + + + + \ No newline at end of file diff --git a/plugins/new-network/resources/wireless/wireless-40-symbolic.svg b/plugins/new-network/resources/wireless/wireless-40-symbolic.svg new file mode 100644 index 000000000..d85c795cd --- /dev/null +++ b/plugins/new-network/resources/wireless/wireless-40-symbolic.svg @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + diff --git a/plugins/new-network/resources/wireless/wireless-40.svg b/plugins/new-network/resources/wireless/wireless-40.svg new file mode 100644 index 000000000..8dfb6b036 --- /dev/null +++ b/plugins/new-network/resources/wireless/wireless-40.svg @@ -0,0 +1,15 @@ + + + + wireless-40-48px + Created with Sketch. + + + + + + + + + + \ No newline at end of file diff --git a/plugins/new-network/resources/wireless/wireless-60-symbolic.svg b/plugins/new-network/resources/wireless/wireless-60-symbolic.svg new file mode 100644 index 000000000..26f32e3b9 --- /dev/null +++ b/plugins/new-network/resources/wireless/wireless-60-symbolic.svg @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + diff --git a/plugins/new-network/resources/wireless/wireless-60.svg b/plugins/new-network/resources/wireless/wireless-60.svg new file mode 100644 index 000000000..7c8cfa65a --- /dev/null +++ b/plugins/new-network/resources/wireless/wireless-60.svg @@ -0,0 +1,15 @@ + + + + wireless-60-48px + Created with Sketch. + + + + + + + + + + \ No newline at end of file diff --git a/plugins/new-network/resources/wireless/wireless-80-symbolic.svg b/plugins/new-network/resources/wireless/wireless-80-symbolic.svg new file mode 100644 index 000000000..30da30353 --- /dev/null +++ b/plugins/new-network/resources/wireless/wireless-80-symbolic.svg @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + + + + + diff --git a/plugins/new-network/resources/wireless/wireless-80.svg b/plugins/new-network/resources/wireless/wireless-80.svg new file mode 100644 index 000000000..701826cf1 --- /dev/null +++ b/plugins/new-network/resources/wireless/wireless-80.svg @@ -0,0 +1,15 @@ + + + + wireless-80-48px + Created with Sketch. + + + + + + + + + + \ No newline at end of file diff --git a/plugins/new-network/resources/wireless/wireless-background.svg b/plugins/new-network/resources/wireless/wireless-background.svg new file mode 100644 index 000000000..ba17ed434 --- /dev/null +++ b/plugins/new-network/resources/wireless/wireless-background.svg @@ -0,0 +1,18 @@ + + + + wireless-background-48px + Created with Sketch. + + + + + + + + + + + + + \ No newline at end of file diff --git a/plugins/new-network/resources/wireless/wireless-disconnect-symbolic.svg b/plugins/new-network/resources/wireless/wireless-disconnect-symbolic.svg new file mode 100644 index 000000000..5a5a0ff5f --- /dev/null +++ b/plugins/new-network/resources/wireless/wireless-disconnect-symbolic.svg @@ -0,0 +1,12 @@ + + + + + + + + diff --git a/plugins/new-network/resources/wireless/wireless-disconnect.svg b/plugins/new-network/resources/wireless/wireless-disconnect.svg new file mode 100644 index 000000000..724b3902a --- /dev/null +++ b/plugins/new-network/resources/wireless/wireless-disconnect.svg @@ -0,0 +1,14 @@ + + + + wireless-disconnect + Created with Sketch. + + + + + + + + + \ No newline at end of file diff --git a/plugins/new-network/util/imageutil.cpp b/plugins/new-network/util/imageutil.cpp new file mode 100644 index 000000000..2ff32f629 --- /dev/null +++ b/plugins/new-network/util/imageutil.cpp @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2011 ~ 2018 Deepin Technology Co., Ltd. + * + * Author: sbw + * + * Maintainer: sbw + * + * 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 3 of the License, or + * any later version. + * + * 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 "imageutil.h" + +#include + +const QPixmap ImageUtil::loadSvg(const QString &path, const int size) +{ + QPixmap pixmap(size, size); + QSvgRenderer renderer(path); + pixmap.fill(Qt::transparent); + + QPainter painter; + painter.begin(&pixmap); + renderer.render(&painter); + painter.end(); + + return pixmap; +} diff --git a/plugins/new-network/util/imageutil.h b/plugins/new-network/util/imageutil.h new file mode 100644 index 000000000..59c90c06d --- /dev/null +++ b/plugins/new-network/util/imageutil.h @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2011 ~ 2018 Deepin Technology Co., Ltd. + * + * Author: sbw + * + * Maintainer: sbw + * + * 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 3 of the License, or + * any later version. + * + * 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 IMAGEUTIL_H +#define IMAGEUTIL_H + +#include +#include + +class ImageUtil +{ +public: + static const QPixmap loadSvg(const QString &path, const int size); +}; + +#endif // IMAGEUTIL_H