From 1c6a463c15d09c4af3138d3fb49d12c5ca5aaa4b Mon Sep 17 00:00:00 2001 From: fpc_diesel Date: Thu, 14 May 2020 13:09:26 +0800 Subject: [PATCH] feat(bluetooth):add bluetooth plugin MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加蓝牙插件 bug:9128 (cherry picked from commit 7412d26cda31a617318842cb919242558e135a69) (cherry picked from commit 1f62819e0fc3a97723c386b6cdf2fc68d1318827) --- plugins/CMakeLists.txt | 1 + plugins/bluetooth/CMakeLists.txt | 35 +++ plugins/bluetooth/bluetooth.json | 4 + plugins/bluetooth/bluetoothapplet.cpp | 144 +++++++++ plugins/bluetooth/bluetoothapplet.h | 66 ++++ plugins/bluetooth/bluetoothitem.cpp | 171 +++++++++++ plugins/bluetooth/bluetoothitem.h | 67 +++++ plugins/bluetooth/bluetoothplugin.cpp | 147 +++++++++ plugins/bluetooth/bluetoothplugin.h | 62 ++++ plugins/bluetooth/componments/adapter.cpp | 175 +++++++++++ plugins/bluetooth/componments/adapter.h | 76 +++++ plugins/bluetooth/componments/adapteritem.cpp | 187 ++++++++++++ plugins/bluetooth/componments/adapteritem.h | 56 ++++ .../bluetooth/componments/adaptersmanager.cpp | 283 ++++++++++++++++++ .../bluetooth/componments/adaptersmanager.h | 71 +++++ plugins/bluetooth/componments/device.cpp | 89 ++++++ plugins/bluetooth/componments/device.h | 84 ++++++ plugins/bluetooth/componments/deviceitem.cpp | 140 +++++++++ plugins/bluetooth/componments/deviceitem.h | 84 ++++++ plugins/bluetooth/componments/switchitem.cpp | 59 ++++ plugins/bluetooth/componments/switchitem.h | 56 ++++ .../bluetooth-active-symbolic-dark.svg | 3 + .../resources/bluetooth-active-symbolic.svg | 3 + .../bluetooth-disable-symbolic-dark.svg | 3 + .../resources/bluetooth-disable-symbolic.svg | 3 + .../bluetooth-waiting-symbolic-dark.svg | 31 ++ .../resources/bluetooth-waiting-symbolic.svg | 31 ++ plugins/bluetooth/resources/bluetooth.qrc | 14 + plugins/bluetooth/resources/list_select.png | Bin 0 -> 508 bytes .../bluetooth/resources/list_select@2x.png | Bin 0 -> 1176 bytes .../resources/notify_close_press.png | Bin 0 -> 398 bytes .../resources/notify_close_press@2x.png | Bin 0 -> 749 bytes plugins/network/item/applet/wirelist.cpp | 90 ++++++ plugins/network/item/applet/wirelist.h | 43 +++ plugins/network/item/plugincase.cpp | 80 +++++ plugins/network/item/plugincase.h | 250 ++++++++++++++++ translations/dde-dock.ts | 42 ++- 37 files changed, 2648 insertions(+), 2 deletions(-) create mode 100644 plugins/bluetooth/CMakeLists.txt create mode 100644 plugins/bluetooth/bluetooth.json create mode 100644 plugins/bluetooth/bluetoothapplet.cpp create mode 100644 plugins/bluetooth/bluetoothapplet.h create mode 100644 plugins/bluetooth/bluetoothitem.cpp create mode 100644 plugins/bluetooth/bluetoothitem.h create mode 100644 plugins/bluetooth/bluetoothplugin.cpp create mode 100644 plugins/bluetooth/bluetoothplugin.h create mode 100644 plugins/bluetooth/componments/adapter.cpp create mode 100644 plugins/bluetooth/componments/adapter.h create mode 100644 plugins/bluetooth/componments/adapteritem.cpp create mode 100644 plugins/bluetooth/componments/adapteritem.h create mode 100644 plugins/bluetooth/componments/adaptersmanager.cpp create mode 100644 plugins/bluetooth/componments/adaptersmanager.h create mode 100644 plugins/bluetooth/componments/device.cpp create mode 100644 plugins/bluetooth/componments/device.h create mode 100644 plugins/bluetooth/componments/deviceitem.cpp create mode 100644 plugins/bluetooth/componments/deviceitem.h create mode 100644 plugins/bluetooth/componments/switchitem.cpp create mode 100644 plugins/bluetooth/componments/switchitem.h create mode 100644 plugins/bluetooth/resources/bluetooth-active-symbolic-dark.svg create mode 100644 plugins/bluetooth/resources/bluetooth-active-symbolic.svg create mode 100644 plugins/bluetooth/resources/bluetooth-disable-symbolic-dark.svg create mode 100644 plugins/bluetooth/resources/bluetooth-disable-symbolic.svg create mode 100644 plugins/bluetooth/resources/bluetooth-waiting-symbolic-dark.svg create mode 100644 plugins/bluetooth/resources/bluetooth-waiting-symbolic.svg create mode 100644 plugins/bluetooth/resources/bluetooth.qrc create mode 100644 plugins/bluetooth/resources/list_select.png create mode 100644 plugins/bluetooth/resources/list_select@2x.png create mode 100644 plugins/bluetooth/resources/notify_close_press.png create mode 100644 plugins/bluetooth/resources/notify_close_press@2x.png create mode 100644 plugins/network/item/applet/wirelist.cpp create mode 100644 plugins/network/item/applet/wirelist.h create mode 100644 plugins/network/item/plugincase.cpp create mode 100644 plugins/network/item/plugincase.h diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt index b98574c6c..0bfd332f2 100644 --- a/plugins/CMakeLists.txt +++ b/plugins/CMakeLists.txt @@ -12,3 +12,4 @@ add_subdirectory("onboard") add_subdirectory("overlay-warning") add_subdirectory("show-desktop") add_subdirectory("multitasking") +add_subdirectory("bluetooth") diff --git a/plugins/bluetooth/CMakeLists.txt b/plugins/bluetooth/CMakeLists.txt new file mode 100644 index 000000000..59f7cb91f --- /dev/null +++ b/plugins/bluetooth/CMakeLists.txt @@ -0,0 +1,35 @@ + +set(PLUGIN_NAME "bluetooth") + +project(${PLUGIN_NAME}) + +# Sources files +file(GLOB_RECURSE SRCS "*.h" "*.cpp" "../../widgets/*.h" "../../widgets/*.cpp" "../../frame/util/imageutil.h" "../../frame/util/imageutil.cpp") + +find_package(PkgConfig REQUIRED) +find_package(Qt5Widgets REQUIRED) +find_package(Qt5Svg REQUIRED) +find_package(Qt5DBus REQUIRED) +find_package(DtkWidget REQUIRED) + +pkg_check_modules(DFrameworkDBus REQUIRED dframeworkdbus) +pkg_check_modules(QGSettings REQUIRED gsettings-qt) + +add_definitions("${QT_DEFINITIONS} -DQT_PLUGIN") +add_library(${PLUGIN_NAME} SHARED ${SRCS} resources/bluetooth.qrc) +set_target_properties(${PLUGIN_NAME} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ../system-trays) +target_include_directories(${PLUGIN_NAME} PUBLIC ${DtkWidget_INCLUDE_DIRS} + ${DFrameworkDBus_INCLUDE_DIRS} + ${QGSettings_INCLUDE_DIRS} + ../../interfaces + ../../frame) +target_link_libraries(${PLUGIN_NAME} PRIVATE + ${DtkWidget_LIBRARIES} + ${DFrameworkDBus_LIBRARIES} + ${QGSettings_LIBRARIES} + ${Qt5DBus_LIBRARIES} + ${Qt5Widgets_LIBRARIES} + ${Qt5Svg_LIBRARIES} +) + +install(TARGETS ${PLUGIN_NAME} LIBRARY DESTINATION lib/dde-dock/plugins/system-trays) diff --git a/plugins/bluetooth/bluetooth.json b/plugins/bluetooth/bluetooth.json new file mode 100644 index 000000000..5b1e078b2 --- /dev/null +++ b/plugins/bluetooth/bluetooth.json @@ -0,0 +1,4 @@ +{ + "api": "1.1.1", + "depends-daemon-dbus-service": "com.deepin.daemon.Bluetooth" +} diff --git a/plugins/bluetooth/bluetoothapplet.cpp b/plugins/bluetooth/bluetoothapplet.cpp new file mode 100644 index 000000000..2ac4412f6 --- /dev/null +++ b/plugins/bluetooth/bluetoothapplet.cpp @@ -0,0 +1,144 @@ +/* + * Copyright (C) 2016 ~ 2018 Deepin Technology Co., Ltd. + * + * Author: zhaolong + * + * Maintainer: zhaolong + * + * + * 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 "bluetoothapplet.h" + +#include "componments/switchitem.h" +#include "componments/deviceitem.h" +#include "componments/adapter.h" +#include "componments/switchitem.h" + +const int Width = 200; + +BluetoothApplet::BluetoothApplet(QWidget *parent) + : QScrollArea(parent) + , m_line(new HorizontalSeparator(this)) + , m_appletName(new QLabel(this)) + , m_centralWidget(new QWidget(this)) + , m_centrealLayout(new QVBoxLayout) + , m_adaptersManager(new AdaptersManager(this)) +{ + m_line->setVisible(false); + + m_appletName->setText(tr("Bluetooth")); + m_appletName->setVisible(false); + + m_centrealLayout->setMargin(0); + m_centrealLayout->setSpacing(0); + m_centrealLayout->addWidget(m_appletName); + m_centrealLayout->addWidget(m_line); + m_centralWidget->setLayout(m_centrealLayout); + m_centralWidget->setFixedWidth(Width); + m_centralWidget->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred); + + setFixedWidth(Width); + setWidget(m_centralWidget); + setFrameShape(QFrame::NoFrame); + setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + m_centralWidget->setAutoFillBackground(false); + viewport()->setAutoFillBackground(false); + + connect(m_adaptersManager, &AdaptersManager::adapterIncreased, this, &BluetoothApplet::addAdapter); + connect(m_adaptersManager, &AdaptersManager::adapterDecreased, this, &BluetoothApplet::removeAdapter); +} + +void BluetoothApplet::setAdapterPowered(bool powered) +{ + for (auto adapterItem : m_adapterItems) { + if (adapterItem) + adapterItem->setPowered(powered); + } +} + +bool BluetoothApplet::poweredInitState() +{ + return m_adaptersManager->defaultAdapterInitPowerState(); +} + +bool BluetoothApplet::hasAadapter() +{ + return m_adaptersManager->adaptersCount(); +} + +void BluetoothApplet::addAdapter(Adapter *adapter) +{ + if (!adapter) + return; + + auto adapterId = adapter->id(); + auto adatpterItem = new AdapterItem(m_adaptersManager, adapter, this); + m_adapterItems[adapterId] = adatpterItem; + m_centrealLayout->addWidget(adatpterItem); + + connect(adatpterItem, &AdapterItem::deviceStateChanged, this, &BluetoothApplet::deviceStateChanged); + connect(adatpterItem, &AdapterItem::powerChanged, this, &BluetoothApplet::powerChanged); + connect(adatpterItem, &AdapterItem::sizeChange, this, &BluetoothApplet::updateView); + + updateView(); +} + +void BluetoothApplet::removeAdapter(Adapter *adapter) +{ + if (adapter) { + auto adapterId = adapter->id(); + auto adapterItem = m_adapterItems.value(adapterId); + if (adapterItem) { + delete adapterItem; + m_adapterItems.remove(adapterId); + updateView(); + if (!m_adapterItems.size()) + emit noAdapter(); + } + } +} + +void BluetoothApplet::updateView() +{ + int itemCount = 0; + for (auto adapterItem : m_adapterItems) { + if (adapterItem) + itemCount += adapterItem->pairedDeviceCount(); + } + + if (m_adapterItems.size() > 1) { + m_line->setVisible(true); + m_appletName->setVisible(true); + } else { + m_line->setVisible(false); + m_appletName->setVisible(false); + } + + int contentHeight = 0; + if (itemCount <= 16) { + contentHeight = m_centralWidget->sizeHint().height(); + m_centralWidget->setFixedHeight(contentHeight); + setFixedHeight(contentHeight); + if (16 == itemCount) + sixteenDeviceHeight = contentHeight; + } else { + contentHeight = m_centralWidget->sizeHint().height(); + m_centralWidget->setFixedHeight(contentHeight); + setFixedHeight(sixteenDeviceHeight); + setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn); + } +} diff --git a/plugins/bluetooth/bluetoothapplet.h b/plugins/bluetooth/bluetoothapplet.h new file mode 100644 index 000000000..58f9bce67 --- /dev/null +++ b/plugins/bluetooth/bluetoothapplet.h @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2016 ~ 2018 Deepin Technology Co., Ltd. + * + * Author: zhaolong + * + * Maintainer: zhaolong + * + * + * 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 BLUETOOTHAPPLET_H +#define BLUETOOTHAPPLET_H + +#include "componments/adaptersmanager.h" +#include "componments/adapteritem.h" + +#include +#include + +class AdapterItem; +class BluetoothApplet : public QScrollArea +{ + Q_OBJECT +public: + explicit BluetoothApplet(QWidget *parent = nullptr); + void setAdapterPowered(bool powered); + bool poweredInitState(); + bool hasAadapter(); + +public slots : + void addAdapter(Adapter *constadapter); + void removeAdapter(Adapter *adapter); + +signals: + void powerChanged(bool state); + void deviceStateChanged(int state); + void noAdapter(); + +private: + void updateView(); + +private: + HorizontalSeparator *m_line; + QLabel *m_appletName; + QWidget *m_centralWidget; + QVBoxLayout *m_centrealLayout; + int sixteenDeviceHeight = 0; + + AdaptersManager *m_adaptersManager; + + QMap m_adapterItems; +}; + +#endif // BLUETOOTHAPPLET_H diff --git a/plugins/bluetooth/bluetoothitem.cpp b/plugins/bluetooth/bluetoothitem.cpp new file mode 100644 index 000000000..094c15fff --- /dev/null +++ b/plugins/bluetooth/bluetoothitem.cpp @@ -0,0 +1,171 @@ +/* + * Copyright (C) 2016 ~ 2018 Deepin Technology Co., Ltd. + * + * Author: zhaolong + * + * Maintainer: zhaolong + * + * + * 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 "bluetoothitem.h" +#include "constants.h" +#include "bluetoothapplet.h" + +#include +#include +#include +#include +#include +#include +#include "../widgets/tipswidget.h" +#include "../frame/util/imageutil.h" +#include + +// menu actions +#define SHIFT "shift" +#define SETTINGS "settings" + +DWIDGET_USE_NAMESPACE +DGUI_USE_NAMESPACE + + +BluetoothItem::BluetoothItem(QWidget *parent) + : QWidget(parent) + , m_applet(new BluetoothApplet(this)) +{ + m_applet->setVisible(false); + m_adapterPowered = m_applet->poweredInitState(); + + connect(m_applet, &BluetoothApplet::powerChanged, [&](bool powered) { + m_adapterPowered = powered; + refreshIcon(); + }); + connect(m_applet, &BluetoothApplet::deviceStateChanged, [&](int state) { + m_devState = state; + refreshIcon(); + }); + connect(m_applet, SIGNAL(noAdapter()), this, SIGNAL(noAdapter())); +} + +//QWidget *BluetoothItem::tipsWidget() +//{ +//} + +QWidget *BluetoothItem::popupApplet() +{ + return m_applet->hasAadapter() ? m_applet : nullptr; +} + +const QString BluetoothItem::contextMenu() const +{ + QList items; + if (m_applet->hasAadapter()) { + items.reserve(2); + + QMap shift; + shift["itemId"] = SHIFT; + if (m_adapterPowered) + shift["itemText"] = tr("Turn off"); + else + shift["itemText"] = tr("Turn on"); + shift["isActive"] = true; + items.push_back(shift); + + QMap settings; + settings["itemId"] = SETTINGS; + settings["itemText"] = tr("Bluetooth settings"); + settings["isActive"] = true; + items.push_back(settings); + + QMap menu; + menu["items"] = items; + menu["checkableMenu"] = false; + menu["singleCheck"] = false; + return QJsonDocument::fromVariant(menu).toJson(); + } + return QByteArray(); +} + +void BluetoothItem::invokeMenuItem(const QString menuId, const bool checked) +{ + Q_UNUSED(checked); + + if (menuId == SHIFT) { + m_applet->setAdapterPowered(!m_adapterPowered); + } + else if (menuId == SETTINGS) + DDBusSender() + .service("com.deepin.dde.ControlCenter") + .interface("com.deepin.dde.ControlCenter") + .path("/com/deepin/dde/ControlCenter") + .method(QString("ShowModule")) + .arg(QString("bluetooth")) + .call(); +} + +void BluetoothItem::refreshIcon() +{ + if (!m_applet) + return; + + QString stateString; + + m_adapterPowered ? (m_devState == 2 ? stateString = "waiting" : stateString = "active") : stateString = "disable"; + + QString iconString = QString("bluetooth-%1-symbolic").arg(stateString); + + const auto ratio = devicePixelRatioF(); + int iconSize = PLUGIN_ICON_MAX_SIZE; + if (height() <= PLUGIN_BACKGROUND_MIN_SIZE && DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::LightType) + iconString.append(PLUGIN_MIN_ICON_NAME); + + m_iconPixmap = ImageUtil::loadSvg(iconString, ":/", iconSize, ratio); + + update(); +} + +bool BluetoothItem::hasAdapter() +{ + return m_applet->hasAadapter(); +} + +void BluetoothItem::resizeEvent(QResizeEvent *e) +{ + QWidget::resizeEvent(e); + + const Dock::Position position = qApp->property(PROP_POSITION).value(); + // 保持横纵比 + if (position == Dock::Bottom || position == Dock::Top) { + setMaximumWidth(height()); + setMaximumHeight(QWIDGETSIZE_MAX); + } else { + setMaximumHeight(width()); + setMaximumWidth(QWIDGETSIZE_MAX); + } + + refreshIcon(); +} + +void BluetoothItem::paintEvent(QPaintEvent *e) +{ + QWidget::paintEvent(e); + + QPainter painter(this); + const QRectF &rf = QRectF(rect()); + const QRectF &rfp = QRectF(m_iconPixmap.rect()); + painter.drawPixmap(rf.center() - rfp.center() / m_iconPixmap.devicePixelRatioF(), m_iconPixmap); +} + diff --git a/plugins/bluetooth/bluetoothitem.h b/plugins/bluetooth/bluetoothitem.h new file mode 100644 index 000000000..bac321a61 --- /dev/null +++ b/plugins/bluetooth/bluetoothitem.h @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2016 ~ 2018 Deepin Technology Co., Ltd. + * + * Author: zhaolong + * + * Maintainer: zhaolong + * + * + * 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 BLUETOOTHITEM_H +#define BLUETOOTHITEM_H + +#include +#include + +#define BLUETOOTH_KEY "bluetooth-item-key" + +class BluetoothApplet; +class TipsWidget; +class BluetoothItem : public QWidget +{ + Q_OBJECT + +public: + explicit BluetoothItem(QWidget *parent = nullptr); + +// QWidget *tipsWidget(); + QWidget *popupApplet(); + + const QString contextMenu() const; + void invokeMenuItem(const QString menuId, const bool checked); + + void refreshIcon(); + + bool hasAdapter(); + +protected: + void resizeEvent(QResizeEvent *e); + void paintEvent(QPaintEvent *e); + +signals: + void requestContextMenu() const; + void noAdapter(); + +private: +// TipsWidget *m_tipsLabel; + BluetoothApplet *m_applet; + QPixmap m_iconPixmap; + + int m_devState; + bool m_adapterPowered; +}; + +#endif // BLUETOOTHITEM_H diff --git a/plugins/bluetooth/bluetoothplugin.cpp b/plugins/bluetooth/bluetoothplugin.cpp new file mode 100644 index 000000000..aabc46931 --- /dev/null +++ b/plugins/bluetooth/bluetoothplugin.cpp @@ -0,0 +1,147 @@ +/* + * Copyright (C) 2016 ~ 2018 Deepin Technology Co., Ltd. + * + * Author: zhaolong + * + * Maintainer: zhaolong + * + * + * 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 "bluetoothplugin.h" + +#define STATE_KEY "enable" + +BluetoothPlugin::BluetoothPlugin(QObject *parent) + : QObject(parent), + m_bluetoothItem(nullptr) +{ +} + +const QString BluetoothPlugin::pluginName() const +{ + return "bluetooth"; +} + +const QString BluetoothPlugin::pluginDisplayName() const +{ + return tr("Bluetooth"); +} + +void BluetoothPlugin::init(PluginProxyInterface *proxyInter) +{ + m_proxyInter = proxyInter; + + m_bluetoothItem = new BluetoothItem; + + connect(m_bluetoothItem, &BluetoothItem::noAdapter, [&]{ + m_enableState = false; + pluginIsDisable(); + }); + + m_enableState = m_bluetoothItem->hasAdapter(); + + if (!pluginIsDisable()) + m_proxyInter->itemAdded(this, BLUETOOTH_KEY); +} + +void BluetoothPlugin::pluginStateSwitched() +{ + m_proxyInter->saveValue(this, STATE_KEY, pluginIsDisable()); + + refreshPluginItemsVisible(); +} + +bool BluetoothPlugin::pluginIsDisable() +{ + return !m_proxyInter->getValue(this, STATE_KEY, m_enableState).toBool(); +} + +QWidget *BluetoothPlugin::itemWidget(const QString &itemKey) +{ + if (itemKey == BLUETOOTH_KEY) { + return m_bluetoothItem; + } + + return nullptr; +} + +//QWidget *BluetoothPlugin::itemTipsWidget(const QString &itemKey) +//{ +// if (itemKey == BLUETOOTH_KEY) { +// return m_bluetoothItem->tipsWidget(); +// } + +// return nullptr; +//} + +QWidget *BluetoothPlugin::itemPopupApplet(const QString &itemKey) +{ + if (itemKey == BLUETOOTH_KEY) { + return m_bluetoothItem->popupApplet(); + } + + return nullptr; +} + +const QString BluetoothPlugin::itemContextMenu(const QString &itemKey) +{ + if (itemKey == BLUETOOTH_KEY) { + return m_bluetoothItem->contextMenu(); + } + + return QString(); +} + +void BluetoothPlugin::invokedMenuItem(const QString &itemKey, const QString &menuId, const bool checked) +{ + if (itemKey == BLUETOOTH_KEY) { + m_bluetoothItem->invokeMenuItem(menuId, checked); + } +} + +int BluetoothPlugin::itemSortKey(const QString &itemKey) +{ + const QString key = QString("pos_%1_%2").arg(itemKey).arg(Dock::Efficient); + + return m_proxyInter->getValue(this, key, 5).toInt(); +} + +void BluetoothPlugin::setSortKey(const QString &itemKey, const int order) +{ + const QString key = QString("pos_%1_%2").arg(itemKey).arg(Dock::Efficient); + + m_proxyInter->saveValue(this, key, order); +} + +void BluetoothPlugin::refreshIcon(const QString &itemKey) +{ + if (itemKey == BLUETOOTH_KEY) { + m_bluetoothItem->refreshIcon(); + } +} + +void BluetoothPlugin::pluginSettingsChanged() +{ + refreshPluginItemsVisible(); +} + +void BluetoothPlugin::refreshPluginItemsVisible() +{ + if (pluginIsDisable()) + m_proxyInter->itemRemoved(this, BLUETOOTH_KEY); + else + m_proxyInter->itemAdded(this, BLUETOOTH_KEY); +} diff --git a/plugins/bluetooth/bluetoothplugin.h b/plugins/bluetooth/bluetoothplugin.h new file mode 100644 index 000000000..7f0109dc8 --- /dev/null +++ b/plugins/bluetooth/bluetoothplugin.h @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2016 ~ 2018 Deepin Technology Co., Ltd. + * + * Author: zhaolong + * + * Maintainer: zhaolong + * + * + * 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 BLUETOOTHPLUGIN_H +#define BLUETOOTHPLUGIN_H + +#include "pluginsiteminterface.h" +#include "bluetoothitem.h" + +class BluetoothPlugin : public QObject, PluginsItemInterface +{ + Q_OBJECT + Q_INTERFACES(PluginsItemInterface) + Q_PLUGIN_METADATA(IID "com.deepin.dock.PluginsItemInterface" FILE "bluetooth.json") + +public: + explicit BluetoothPlugin(QObject *parent = nullptr); + + const QString pluginName() const Q_DECL_OVERRIDE; + const QString pluginDisplayName() const Q_DECL_OVERRIDE; + void init(PluginProxyInterface *proxyInter) Q_DECL_OVERRIDE; + void pluginStateSwitched() Q_DECL_OVERRIDE; + bool pluginIsAllowDisable() Q_DECL_OVERRIDE { return true; } + bool pluginIsDisable() Q_DECL_OVERRIDE; + QWidget *itemWidget(const QString &itemKey) Q_DECL_OVERRIDE; +// QWidget *itemTipsWidget(const QString &itemKey) Q_DECL_OVERRIDE; + QWidget *itemPopupApplet(const QString &itemKey) Q_DECL_OVERRIDE; + const QString itemContextMenu(const QString &itemKey) Q_DECL_OVERRIDE; + void invokedMenuItem(const QString &itemKey, const QString &menuId, const bool checked) Q_DECL_OVERRIDE; + int itemSortKey(const QString &itemKey) Q_DECL_OVERRIDE; + void setSortKey(const QString &itemKey, const int order) Q_DECL_OVERRIDE; + void refreshIcon(const QString &itemKey) Q_DECL_OVERRIDE; + void pluginSettingsChanged() Q_DECL_OVERRIDE; + +private: + void refreshPluginItemsVisible(); + +private: + BluetoothItem *m_bluetoothItem; + bool m_enableState = true; +}; + +#endif // BLUETOOTHPLUGIN_H diff --git a/plugins/bluetooth/componments/adapter.cpp b/plugins/bluetooth/componments/adapter.cpp new file mode 100644 index 000000000..35e0405b0 --- /dev/null +++ b/plugins/bluetooth/componments/adapter.cpp @@ -0,0 +1,175 @@ +/* + * Copyright (C) 2016 ~ 2018 Deepin Technology Co., Ltd. + * + * Author: zhaolong + * + * Maintainer: zhaolong + * + * + * 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 "adapter.h" + +#include +#include +#include +#include + +Adapter::Adapter(QObject *parent) + : QObject(parent) + , m_id("") + , m_name("") + , m_powered(false) + , m_current(false) +{ +} + +void Adapter::setName(const QString &name) +{ + if (name != m_name) { + m_name = name; + Q_EMIT nameChanged(name); + } +} + +void Adapter::addDevice(const QJsonObject &deviceObj) +{ + const QString id = deviceObj["Path"].toString(); + const QString name = deviceObj["Alias"].toString(); + const bool paired = deviceObj["Paired"].toBool(); + const Device::State state = Device::State(deviceObj["State"].toInt()); + + removeDevice(id); + + auto device = new Device(this); + + device->setId(id); + device->setName(name); + device->setPaired(paired); + device->setState(state); + device->setAdapterId(m_id); + + m_devices[id] = device; + divideDevice(device); + + emit deviceAdded(device); +} + +void Adapter::removeDevice(const QString &deviceId) +{ + auto constDevice = m_devices.value(deviceId); + auto device = const_cast(constDevice); + if (device) { + m_devices.remove(deviceId); + m_paredDev.remove(deviceId); + emit deviceRemoved(device); + delete device; + } +} + +void Adapter::updateDevice(const QJsonObject &json) +{ + const QString id = json["Path"].toString(); + const QString name = json["Alias"].toString(); + const bool paired = json["Paired"].toBool(); + const Device::State state = Device::State(json["State"].toInt()); + + auto constdevice = m_devices.value(id); + auto device = const_cast(constdevice); + + if (device) { + device->setId(id); + device->setName(name); + device->setPaired(paired); + device->setState(state); + } +} + +void Adapter::removeAllDevices() +{ + QMapIterator iterator(m_devices); + while (iterator.hasNext()) { + iterator.next(); + auto device = const_cast(iterator.value()); + if (device) { + m_devices.remove(device->id()); + m_paredDev.remove(device->id()); + delete device; + } + } +} + +const QMap &Adapter::paredDevices() const +{ + return m_paredDev; +} + +int Adapter::paredDevicesCount() const +{ + return m_paredDev.size(); +} + +void Adapter::divideDevice(const Device *device) +{ + if (device->paired()) { + m_paredDev[device->id()] = device; + } +} + +void Adapter::setPowered(bool powered) +{ + if (powered != m_powered) { + m_powered = powered; + Q_EMIT poweredChanged(powered); + } +} + +void Adapter::initDevicesList(const QJsonDocument &doc) +{ + auto arr = doc.array(); + for (QJsonValue val : arr) { + auto deviceObj = val.toObject(); + const QString adapterId = deviceObj["AdapterPath"].toString(); + const QString id = deviceObj["Path"].toString(); + const QString name = deviceObj["Alias"].toString(); + const bool paired = deviceObj["Paired"].toBool(); + const Device::State state = Device::State(deviceObj["State"].toInt()); + + auto device = new Device(this); + device->setId(id); + device->setName(name); + device->setPaired(paired); + device->setState(state); + device->setAdapterId(adapterId); + + m_devices[id] = device; + divideDevice(device); + } +} + +QMap Adapter::devices() const +{ + return m_devices; +} + +const Device *Adapter::deviceById(const QString &id) const +{ + return m_devices.keys().contains(id) ? m_devices[id] : nullptr; +} + +void Adapter::setId(const QString &id) +{ + m_id = id; +} diff --git a/plugins/bluetooth/componments/adapter.h b/plugins/bluetooth/componments/adapter.h new file mode 100644 index 000000000..0b9a265bd --- /dev/null +++ b/plugins/bluetooth/componments/adapter.h @@ -0,0 +1,76 @@ +/* + * Copyright (C) 2016 ~ 2018 Deepin Technology Co., Ltd. + * + * Author: zhaolong + * + * Maintainer: zhaolong + * + * + * 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 ADAPTER_H +#define ADAPTER_H + +#include "device.h" + +class Adapter : public QObject +{ + Q_OBJECT +public: + explicit Adapter(QObject *parent = nullptr); + + inline QString name() const { return m_name; } + void setName(const QString &name); + + inline QString id() const { return m_id; } + void setId(const QString &id); + + QMap devices() const; + const Device *deviceById(const QString &id) const; + + inline bool powered() const { return m_powered; } + void setPowered(bool powered); + + inline bool isCurrent() { return m_current; } + inline void setCurrent(bool c) { m_current = c; } + + void initDevicesList(const QJsonDocument &doc); + void addDevice(const QJsonObject &deviceObj); + void removeDevice(const QString &deviceId); + void updateDevice(const QJsonObject &json); + void removeAllDevices(); + const QMap &paredDevices() const; + int paredDevicesCount() const; + +Q_SIGNALS: + void nameChanged(const QString &name) const; + void deviceAdded(const Device *device) const; + void deviceRemoved(const Device *device) const; + void poweredChanged(const bool powered) const; + +private: + void divideDevice(const Device *device); + +private: + QString m_id; + QString m_name; + bool m_powered; + bool m_current; + + QMap m_devices; + QMap m_paredDev; +}; + +#endif // ADAPTER_H diff --git a/plugins/bluetooth/componments/adapteritem.cpp b/plugins/bluetooth/componments/adapteritem.cpp new file mode 100644 index 000000000..f5c2c22d4 --- /dev/null +++ b/plugins/bluetooth/componments/adapteritem.cpp @@ -0,0 +1,187 @@ +#include "adapteritem.h" +#include "switchitem.h" +#include "deviceitem.h" +#include "adapter.h" +#include "adaptersmanager.h" + +#include + +const int Width = 200; + +AdapterItem::AdapterItem(AdaptersManager *adapterManager, Adapter *adapter, QWidget *parent) + : QScrollArea(parent) + , m_centralWidget(new QWidget(this)) + , m_line(new HorizontalSeparator(this)) + , m_devGoupName(new QLabel(this)) + , m_deviceLayout(new QVBoxLayout) + , m_openControlCenter(new MenueItem(this)) + , m_adaptersManager(adapterManager) + , m_adapter(adapter) + , m_switchItem(new SwitchItem(this)) +{ + m_centralWidget->setFixedWidth(Width); + m_line->setVisible(true); + m_devGoupName->setText(tr("My Device")); + m_devGoupName->setVisible(false); + m_deviceLayout->setMargin(0); + m_deviceLayout->setSpacing(0); + m_openControlCenter->setText("Bluetooth settings"); + m_openControlCenter->setVisible(false); + m_switchItem->setTitle(adapter->name()); + m_switchItem->setChecked(adapter->powered()); + +// m_deviceLayout->setMargin(5); + m_deviceLayout->addWidget(m_switchItem); + m_deviceLayout->addWidget(m_line); + m_deviceLayout->addWidget(m_devGoupName); + m_deviceLayout->addWidget(m_openControlCenter); + m_centralWidget->setFixedWidth(Width); + m_centralWidget->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred); + m_centralWidget->setLayout(m_deviceLayout); + + setFixedWidth(Width); + setWidget(m_centralWidget); + setFrameShape(QFrame::NoFrame); + setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + m_centralWidget->setAutoFillBackground(false); + viewport()->setAutoFillBackground(false); + + auto myDevices = adapter->devices(); + for (auto constDevice : myDevices) { + auto device = const_cast(constDevice); + if (device) { + createDeviceItem(device); + } + } + + connect(m_switchItem, &SwitchItem::checkedChanged, this, &AdapterItem::showAndConnect); + connect(adapter, &Adapter::nameChanged, m_switchItem, &SwitchItem::setTitle); + connect(adapter, &Adapter::deviceAdded, this, &AdapterItem::addDeviceItem); + connect(adapter, &Adapter::deviceRemoved, this, &AdapterItem::removeDeviceItem); + connect(adapter, &Adapter::poweredChanged, m_switchItem, &SwitchItem::setChecked); + connect(m_openControlCenter, &MenueItem::clicked, []{ + DDBusSender() + .service("com.deepin.dde.ControlCenter") + .interface("com.deepin.dde.ControlCenter") + .path("/com/deepin/dde/ControlCenter") + .method(QString("ShowModule")) + .arg(QString("bluetooth")) + .call(); + }); + + showDevices(adapter->powered()); +} + +int AdapterItem::pairedDeviceCount() +{ + return m_pairedDeviceItems.size(); +} + +int AdapterItem::deviceCount() +{ + return m_deviceItems.size(); +} + +void AdapterItem::setPowered(bool powered) +{ + m_switchItem->setChecked(powered); +} + +void AdapterItem::deviceItemPaired(const bool paired) +{ + auto device = qobject_cast(sender()); + if (device) { + auto deviceId = device->id(); + auto deviceItem = m_deviceItems.value(deviceId); + if (deviceItem) { + if (paired) + m_pairedDeviceItems[deviceId] = deviceItem; + else + m_pairedDeviceItems.remove(deviceId); + } + showDevices(m_adapter->powered()); + } +} + +void AdapterItem::removeDeviceItem(const Device *device) +{ + if (!device) + return; + + auto deviceItem = m_deviceItems.value(device->id()); + if (deviceItem) { + m_deviceItems.remove(device->id()); + if (device->paired()) { + m_pairedDeviceItems.remove(device->id()); + m_deviceLayout->removeWidget(deviceItem); + } + delete deviceItem; + showDevices(m_adapter->powered()); + } +} + +void AdapterItem::showAndConnect(bool change) +{ + showDevices(change); + + m_adaptersManager->setAdapterPowered(m_adapter, change); + if (change) { + m_adaptersManager->connectAllPairedDevice(m_adapter); + } + emit powerChanged(change); +} + +void AdapterItem::addDeviceItem(const Device *constDevice) +{ + auto device = const_cast(constDevice); + if (!device) + return; + + createDeviceItem(device); + showDevices(m_adapter->powered()); +} + +void AdapterItem::createDeviceItem(Device *device) +{ + if (!device) + return; + auto paired = device->paired(); + auto deviceId = device->id(); + auto deviceItem = new DeviceItem(device->name(), this); + deviceItem->setDevice(device); + m_deviceItems[deviceId] = deviceItem; + if (paired) + m_pairedDeviceItems[deviceId] = deviceItem; + connect(device, &Device::pairedChanged, this, &AdapterItem::deviceItemPaired); + connect(device, &Device::nameChanged, deviceItem, &DeviceItem::setTitle); + connect(device, &Device::stateChanged, deviceItem, &DeviceItem::chaneState); + connect(device, &Device::stateChanged, this, &AdapterItem::deviceStateChanged); + connect(deviceItem, &DeviceItem::clicked, m_adaptersManager, &AdaptersManager::connectDevice); + deviceItem->setVisible(paired); +} + +void AdapterItem::updateView() +{ + auto contentHeight = m_centralWidget->sizeHint().height(); + m_centralWidget->setFixedHeight(contentHeight); + setFixedHeight(contentHeight); + emit sizeChange(); +} + +void AdapterItem::showDevices(bool change) +{ + for (auto deviceItem : m_pairedDeviceItems) { + if (change) + m_deviceLayout->addWidget(deviceItem); + else { + m_deviceLayout->removeWidget(deviceItem); + } + deviceItem->setVisible(change); + } + auto itemCount = m_pairedDeviceItems.size(); + m_line->setVisible(change); + m_devGoupName->setVisible(itemCount && change); + m_openControlCenter->setVisible(!itemCount); + updateView(); +} diff --git a/plugins/bluetooth/componments/adapteritem.h b/plugins/bluetooth/componments/adapteritem.h new file mode 100644 index 000000000..88141ac1c --- /dev/null +++ b/plugins/bluetooth/componments/adapteritem.h @@ -0,0 +1,56 @@ +#ifndef ADAPTERITEM_H +#define ADAPTERITEM_H + +#include +#include +#include +#include + +class HorizontalSeparator; +class Adapter; +class SwitchItem; +class DeviceItem; +class Device; +class AdaptersManager; +class MenueItem; +class AdapterItem : public QScrollArea +{ + Q_OBJECT +public: + explicit AdapterItem(AdaptersManager *a, Adapter *adapter, QWidget *parent = nullptr); + int pairedDeviceCount(); + int deviceCount(); + void setPowered(bool powered); + +signals: + void deviceStateChanged(int state); + void powerChanged(bool powered); + void sizeChange(); + +private slots: + void deviceItemPaired(const bool paired); + void removeDeviceItem(const Device *device); + void showAndConnect(bool change); + void addDeviceItem(const Device *constDevice); + +private: + void createDeviceItem(Device *device); + void updateView(); + void showDevices(bool change); + +private: + QWidget *m_centralWidget; + HorizontalSeparator *m_line; + QLabel *m_devGoupName; + QVBoxLayout *m_deviceLayout; + MenueItem *m_openControlCenter; + + AdaptersManager *m_adaptersManager; + + Adapter *m_adapter; + SwitchItem *m_switchItem; + QMap m_deviceItems; + QMap m_pairedDeviceItems; +}; + +#endif // ADAPTERITEM_H diff --git a/plugins/bluetooth/componments/adaptersmanager.cpp b/plugins/bluetooth/componments/adaptersmanager.cpp new file mode 100644 index 000000000..fcb43b723 --- /dev/null +++ b/plugins/bluetooth/componments/adaptersmanager.cpp @@ -0,0 +1,283 @@ +/* + * Copyright (C) 2016 ~ 2018 Deepin Technology Co., Ltd. + * + * Author: zhaolong + * + * Maintainer: zhaolong + * + * + * 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 "adaptersmanager.h" + +#include +#include +#include +#include + +AdaptersManager::AdaptersManager(QObject *parent) + : QObject(parent) + , m_bluetoothInter(new DBusBluetooth("com.deepin.daemon.Bluetooth", + "/com/deepin/daemon/Bluetooth", + QDBusConnection::sessionBus(), + this)) +{ + connect(m_bluetoothInter, &DBusBluetooth::AdapterAdded, this, &AdaptersManager::addAdapter); + connect(m_bluetoothInter, &DBusBluetooth::AdapterRemoved, this, &AdaptersManager::removeAdapter); + connect(m_bluetoothInter, &DBusBluetooth::AdapterPropertiesChanged, this, &AdaptersManager::onAdapterPropertiesChanged); + connect(m_bluetoothInter, &DBusBluetooth::DeviceAdded, this, &AdaptersManager::addDevice); + connect(m_bluetoothInter, &DBusBluetooth::DeviceRemoved, this, &AdaptersManager::removeDevice); + connect(m_bluetoothInter, &DBusBluetooth::DevicePropertiesChanged, this, &AdaptersManager::onDevicePropertiesChanged); + + connect(m_bluetoothInter, &DBusBluetooth::RequestAuthorization, this, [](const QDBusObjectPath & in0) { + qDebug() << "request authorization: " << in0.path(); + }); + + connect(m_bluetoothInter, &DBusBluetooth::RequestPasskey, this, [](const QDBusObjectPath & in0) { + qDebug() << "request passkey: " << in0.path(); + }); + + connect(m_bluetoothInter, &DBusBluetooth::RequestPinCode, this, [](const QDBusObjectPath & in0) { + qDebug() << "request pincode: " << in0.path(); + }); + + connect(m_bluetoothInter, &DBusBluetooth::DisplayPasskey, this, [](const QDBusObjectPath & in0, uint in1, uint in2) { + qDebug() << "request display passkey: " << in0.path() << in1 << in2; + }); + + connect(m_bluetoothInter, &DBusBluetooth::DisplayPinCode, this, [](const QDBusObjectPath & in0, const QString & in1) { + qDebug() << "request display pincode: " << in0.path() << in1; + }); + + QDBusInterface *inter = new QDBusInterface("com.deepin.daemon.Bluetooth", + "/com/deepin/daemon/Bluetooth", + "com.deepin.daemon.Bluetooth", + QDBusConnection::sessionBus()); + QDBusReply reply = inter->call(QDBus::Block, "GetAdapters"); + const QString replyStr = reply.value(); + QJsonDocument doc = QJsonDocument::fromJson(replyStr.toUtf8()); + QJsonArray arr = doc.array(); + + for (int index = 0; index < arr.size(); index++) { + auto *adapter = new Adapter(this); + adapterAdd(adapter, arr[index].toObject()); + if (!index) { + adapter->setCurrent(true); + m_defaultAdapterState = adapter->powered(); + } + } +} + +QMap AdaptersManager::adapters() const +{ + return m_adapters; +} + +const Adapter *AdaptersManager::adapterById(const QString &id) +{ + return m_adapters.keys().contains(id) ? m_adapters[id] : nullptr; +} + +void AdaptersManager::setAdapterPowered(const Adapter *adapter, const bool &powered) +{ + if (adapter) { + QDBusObjectPath path(adapter->id()); + QDBusPendingCall call = m_bluetoothInter->SetAdapterPowered(path, powered); + + if (powered) { + QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(call, this); + connect(watcher, &QDBusPendingCallWatcher::finished, [this, call, adapter] { + if (!call.isError()) + { + QDBusObjectPath dPath(adapter->id()); + m_bluetoothInter->SetAdapterDiscoverableTimeout(dPath, 60 * 5); + m_bluetoothInter->SetAdapterDiscoverable(dPath, true); + + m_bluetoothInter->RequestDiscovery(dPath); + } else + { + qWarning() << call.error().message(); + } + }); + } + } +} + +void AdaptersManager::connectAllPairedDevice(const Adapter *adapter) +{ + for (const Device *d : adapter->devices()) { + Device *vd = const_cast(d); + if (vd) { + QDBusObjectPath path(vd->id()); + m_bluetoothInter->ConnectDevice(path); + qDebug() << "connect to device: " << vd->name(); + } + } +} + +void AdaptersManager::connectDevice(Device *device) +{ + if (device) { + QDBusObjectPath path(device->id()); + switch (device->state()) { + case 0: + { + m_bluetoothInter->ConnectDevice(path); + qDebug() << "connect to device: " << device->name(); + } + break; + case 1: + break; + case 2: + { + m_bluetoothInter->DisconnectDevice(path); + qDebug() << "disconnect device: " << device->name(); + } + break; + } + } +} + +bool AdaptersManager::defaultAdapterInitPowerState() +{ + return m_defaultAdapterState; +} + +int AdaptersManager::adaptersCount() +{ + return m_adapters.size(); +} + +void AdaptersManager::onAdapterPropertiesChanged(const QString &json) +{ + const QJsonDocument doc = QJsonDocument::fromJson(json.toUtf8()); + const QJsonObject obj = doc.object(); + const QString id = obj["Path"].toString(); + + Adapter *adapter = const_cast(m_adapters[id]); + if (adapter) { + inflateAdapter(adapter, obj); + } +} + +void AdaptersManager::onDevicePropertiesChanged(const QString &json) +{ + const QJsonDocument doc = QJsonDocument::fromJson(json.toUtf8()); + const QJsonObject obj = doc.object(); + for (auto constAdapter : m_adapters) { + auto adapter = const_cast(constAdapter); + if (adapter) + adapter->updateDevice(obj); + } +} + +void AdaptersManager::addAdapter(const QString &json) +{ + const QJsonDocument doc = QJsonDocument::fromJson(json.toUtf8()); + auto adapter = new Adapter(this); + adapterAdd(adapter, doc.object()); +} + +void AdaptersManager::removeAdapter(const QString &json) +{ + QJsonDocument doc = QJsonDocument::fromJson(json.toUtf8()); + QJsonObject obj = doc.object(); + const QString id = obj["Path"].toString(); + + const Adapter *result = m_adapters[id]; + Adapter *adapter = const_cast(result); + if (adapter) { + m_adapters.remove(id); + emit adapterDecreased(adapter); + adapter->deleteLater(); + } +} + +void AdaptersManager::addDevice(const QString &json) +{ + const QJsonDocument doc = QJsonDocument::fromJson(json.toUtf8()); + const QJsonObject obj = doc.object(); + const QString adapterId = obj["AdapterPath"].toString(); + const QString deviceId = obj["Path"].toString(); + + const Adapter *result = m_adapters[adapterId]; + Adapter *adapter = const_cast(result); + if (adapter) { + const Device *result1 = adapter->deviceById(deviceId); + Device *device = const_cast(result1); + if (!device) { + adapter->addDevice(obj); + } + } +} + +void AdaptersManager::removeDevice(const QString &json) +{ + QJsonDocument doc = QJsonDocument::fromJson(json.toUtf8()); + QJsonObject obj = doc.object(); + const QString adapterId = obj["AdapterPath"].toString(); + const QString deviceId = obj["Path"].toString(); + + const Adapter *result = m_adapters[adapterId]; + Adapter *adapter = const_cast(result); + if (adapter) { + adapter->removeDevice(deviceId); + } +} + +void AdaptersManager::adapterAdd(Adapter *adapter, const QJsonObject &adpterObj) +{ + if (!adapter) + return; + + inflateAdapter(adapter, adpterObj); + QDBusObjectPath dPath(adpterObj["Path"].toString()); + QDBusPendingCall call = m_bluetoothInter->GetDevices(dPath); + QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(call, this); + connect(watcher, &QDBusPendingCallWatcher::finished, [this, adapter, call, watcher] { + if (adapter) + { + if (!call.isError()) { + QDBusReply reply = call.reply(); + const QString replyStr = reply.value(); + auto doc = QJsonDocument::fromJson(replyStr.toUtf8()); + adapter->initDevicesList(doc); + emit this->adapterIncreased(adapter); + } else { + qWarning() << call.error().message(); + } + } + delete watcher; + }); + + auto id = adapter->id(); + if (!id.isEmpty()) { + m_adapters[id] = adapter; + } +} + +void AdaptersManager::inflateAdapter(Adapter *adapter, const QJsonObject &adapterObj) +{ + if (!adapter) + return; + + const QString path = adapterObj["Path"].toString(); + const QString alias = adapterObj["Alias"].toString(); + const bool powered = adapterObj["Powered"].toBool(); + + adapter->setId(path); + adapter->setName(alias); + adapter->setPowered(powered); +} diff --git a/plugins/bluetooth/componments/adaptersmanager.h b/plugins/bluetooth/componments/adaptersmanager.h new file mode 100644 index 000000000..39c073dc8 --- /dev/null +++ b/plugins/bluetooth/componments/adaptersmanager.h @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2016 ~ 2018 Deepin Technology Co., Ltd. + * + * Author: zhaolong + * + * Maintainer: zhaolong + * + * + * 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 ADAPTERSMANAGER_H +#define ADAPTERSMANAGER_H + +#include "adapter.h" + +#include +using DBusBluetooth = com::deepin::daemon::Bluetooth; + +class AdaptersManager : public QObject +{ + Q_OBJECT +public: + explicit AdaptersManager(QObject *parent = nullptr); + + QMap adapters() const; + const Adapter *adapterById(const QString &id); + + void setAdapterPowered(const Adapter *adapter, const bool &powered); + void connectAllPairedDevice(const Adapter *adapter); + void connectDevice(Device *deviceId); + bool defaultAdapterInitPowerState(); + int adaptersCount(); + +signals: + void adapterIncreased(Adapter *adapter); + void adapterDecreased(Adapter *adapter); + +private slots: + void onAdapterPropertiesChanged(const QString &json); + void onDevicePropertiesChanged(const QString &json); + + void addAdapter(const QString &json); + void removeAdapter(const QString &json); + + void addDevice(const QString &json); + void removeDevice(const QString &json); + +private: + void adapterAdd(Adapter *adapter, const QJsonObject &adpterObj); + void inflateAdapter(Adapter *adapter, const QJsonObject &adapterObj); + +private: + DBusBluetooth *m_bluetoothInter; + QMap m_adapters; + + bool m_defaultAdapterState; +}; + +#endif // ADAPTERSMANAGER_H diff --git a/plugins/bluetooth/componments/device.cpp b/plugins/bluetooth/componments/device.cpp new file mode 100644 index 000000000..2f1538d14 --- /dev/null +++ b/plugins/bluetooth/componments/device.cpp @@ -0,0 +1,89 @@ +/* + * Copyright (C) 2016 ~ 2018 Deepin Technology Co., Ltd. + * + * Author: zhaolong + * + * Maintainer: zhaolong + * + * + * 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 "device.h" + +Device::Device(QObject *parent) : + QObject(parent), + m_id(""), + m_name(""), + m_paired(false), + m_trusted(false), + m_connecting(false), + m_state(StateUnavailable), + m_adapterId(QString()) +{ +} + +void Device::setId(const QString &id) +{ + m_id = id; +} + +void Device::setName(const QString &name) +{ + if (name != m_name) { + m_name = name; + Q_EMIT nameChanged(name); + } +} + +void Device::setPaired(bool paired) +{ + if (paired != m_paired) { + m_paired = paired; + Q_EMIT pairedChanged(paired); + } +} + +void Device::setState(const State &state) +{ + if (state != m_state) { + m_state = state; + Q_EMIT stateChanged(state); + } +} + +void Device::setTrusted(bool trusted) +{ + if (trusted != m_trusted) { + m_trusted = trusted; + Q_EMIT trustedChanged(trusted); + } +} + +void Device::setConnecting(bool connecting) +{ + if (connecting != m_connecting) { + m_connecting = connecting; + Q_EMIT connectingChanged(connecting); + } +} + +QDebug &operator<<(QDebug &stream, const Device *device) +{ + stream << "Device name:" << device->name() + << " paired:" << device->paired() + << " state:" << device->state(); + + return stream; +} diff --git a/plugins/bluetooth/componments/device.h b/plugins/bluetooth/componments/device.h new file mode 100644 index 000000000..103a39864 --- /dev/null +++ b/plugins/bluetooth/componments/device.h @@ -0,0 +1,84 @@ +/* + * Copyright (C) 2016 ~ 2018 Deepin Technology Co., Ltd. + * + * Author: zhaolong + * + * Maintainer: zhaolong + * + * + * 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 DEVICE_H +#define DEVICE_H + +#include +#include + +class Device : public QObject +{ + Q_OBJECT + +public: + enum State { + StateUnavailable = 0, + StateAvailable = 1, + StateConnected = 2 + }; + Q_ENUM(State) + +public: + explicit Device(QObject *parent = nullptr); + + inline QString id() const { return m_id; } + void setId(const QString &id); + + inline QString name() const { return m_name; } + void setName(const QString &name); + + inline bool paired() const { return m_paired; } + void setPaired(bool paired); + + inline State state() const { return m_state; } + void setState(const State &state); + + inline bool trusted() const { return m_trusted; } + void setTrusted(bool trusted); + + inline bool connecting() const { return m_connecting; } + void setConnecting(bool connecting); + + inline void setAdapterId(const QString &id) { m_adapterId = id; } + inline const QString &getAdapterId() const { return m_adapterId; } + +Q_SIGNALS: + void nameChanged(const QString &name) const; + void pairedChanged(const bool paired) const; + void stateChanged(int state) const; + void trustedChanged(const bool trusted) const; + void connectingChanged(const bool &connecting) const; + +private: + QString m_id; + QString m_name; + bool m_paired; + bool m_trusted; + bool m_connecting; + State m_state; + QString m_adapterId; +}; + +QDebug &operator<<(QDebug &stream, const Device *device); + +#endif // DEVICE_H diff --git a/plugins/bluetooth/componments/deviceitem.cpp b/plugins/bluetooth/componments/deviceitem.cpp new file mode 100644 index 000000000..d7865ffe7 --- /dev/null +++ b/plugins/bluetooth/componments/deviceitem.cpp @@ -0,0 +1,140 @@ +/* + * Copyright (C) 2016 ~ 2018 Deepin Technology Co., Ltd. + * + * Author: zhaolong + * + * Maintainer: zhaolong + * + * + * 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 "device.h" + +#include +#include +#include + + +DeviceItem::DeviceItem(const QString &title, QWidget *parent) + : QWidget(parent) + , m_title(new QLabel(title, this)) + , m_state(new QLabel(this)) + , m_loadingStat(new DSpinner) + , m_line(new HorizontalSeparator(this)) +{ + m_state->setPixmap(QPixmap(":/list_select@2x.png")); + + m_line->setVisible(true); + m_state->setVisible(false); + m_loadingStat->setFixedSize(20, 20); + m_loadingStat->setVisible(false); + + auto deviceLayout = new QVBoxLayout(this); + deviceLayout->setMargin(0); + deviceLayout->setSpacing(0); + deviceLayout->addWidget(m_line); + auto itemLayout = new QHBoxLayout(this); + itemLayout->setMargin(0); + itemLayout->setSpacing(0); + itemLayout->addSpacing(5); + itemLayout->addWidget(m_title); + itemLayout->addStretch(); + itemLayout->addWidget(m_state); + itemLayout->addWidget(m_loadingStat); + itemLayout->addSpacing(5); + deviceLayout->addLayout(itemLayout); + setLayout(deviceLayout); +} + +void DeviceItem::mousePressEvent(QMouseEvent *event) +{ + emit clicked(m_device); + QWidget::mousePressEvent(event); +} + +void DeviceItem::enterEvent(QEvent *event) +{ + QWidget::enterEvent(event); + if (m_device) { + if (2 == m_device->state()) { + m_state->setPixmap(QPixmap(":/notify_close_press@2x.png")); + } + } +} + +void DeviceItem::leaveEvent(QEvent *event) +{ + QWidget::enterEvent(event); + if (m_device) { + if (2 == m_device->state()) { + m_state->setPixmap(QPixmap(":/list_select@2x.png")); + } + } +} + +void DeviceItem::chaneState(int state) +{ + switch (state) { + case 0: { + m_state->setVisible(false); + m_loadingStat->stop(); + m_loadingStat->hide(); + m_loadingStat->setVisible(false); + } + break; + case 1: { + m_state->setVisible(false); + m_loadingStat->start(); + m_loadingStat->show(); + m_loadingStat->setVisible(true); + } + break; + case 2: { + m_loadingStat->stop(); + m_loadingStat->hide(); + m_loadingStat->setVisible(false); + m_state->setVisible(true); + } + break; + } +} + +HorizontalSeparator::HorizontalSeparator(QWidget *parent) + : QWidget(parent) +{ + setFixedHeight(1); + setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); +} + +void HorizontalSeparator::paintEvent(QPaintEvent *e) +{ + QWidget::paintEvent(e); + + QPainter painter(this); + painter.fillRect(rect(), QColor(0, 0, 0, 125)); +} + +MenueItem::MenueItem(QWidget *parent) + : QLabel(parent) +{ +} + +void MenueItem::mousePressEvent(QMouseEvent *event) +{ + QLabel::mousePressEvent(event); + emit clicked(); +} diff --git a/plugins/bluetooth/componments/deviceitem.h b/plugins/bluetooth/componments/deviceitem.h new file mode 100644 index 000000000..592cbf697 --- /dev/null +++ b/plugins/bluetooth/componments/deviceitem.h @@ -0,0 +1,84 @@ +/* + * Copyright (C) 2016 ~ 2018 Deepin Technology Co., Ltd. + * + * Author: zhaolong + * + * Maintainer: zhaolong + * + * + * 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 + +DWIDGET_USE_NAMESPACE + +class Device; +class HorizontalSeparator; +class DeviceItem : public QWidget +{ + Q_OBJECT +public: + explicit DeviceItem(const QString &title, QWidget *parent = nullptr); + + inline void setTitle(const QString &name) { m_title->setText(name); } + + inline void setDevice(Device *d) { m_device = d; } + inline Device *device() { return m_device; } + +protected: + void mousePressEvent(QMouseEvent *event) override; + void enterEvent(QEvent *event) override; + void leaveEvent(QEvent *event) override; + +signals: + void clicked(Device *); + +public slots: + void chaneState(int state); + +private: + QLabel *m_title; + QLabel *m_state; + DSpinner *m_loadingStat; + Device *m_device = nullptr; + HorizontalSeparator *m_line; +}; + +class HorizontalSeparator : public QWidget +{ + Q_OBJECT +public: + explicit HorizontalSeparator(QWidget *parent = nullptr); + +protected: + void paintEvent(QPaintEvent *e); +}; + +class MenueItem : public QLabel +{ + Q_OBJECT +public: + explicit MenueItem(QWidget *parent = nullptr); +signals: + void clicked(); +protected: + void mousePressEvent(QMouseEvent *event) override; +}; + +#endif // DEVICEITEM_H diff --git a/plugins/bluetooth/componments/switchitem.cpp b/plugins/bluetooth/componments/switchitem.cpp new file mode 100644 index 000000000..7919bffe8 --- /dev/null +++ b/plugins/bluetooth/componments/switchitem.cpp @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2016 ~ 2018 Deepin Technology Co., Ltd. + * + * Author: zhaolong + * + * Maintainer: zhaolong + * + * + * 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 "switchitem.h" + +#include "QHBoxLayout" + +SwitchItem::SwitchItem(QWidget *parent) + : QWidget(parent) + , m_title(new QLabel(this)) + , m_switchBtn(new DSwitchButton(this)) + , m_default(false) +{ + auto switchLayout = new QHBoxLayout(this); + switchLayout->setMargin(2); + switchLayout->addWidget(m_title); + switchLayout->addStretch(); + switchLayout->addWidget(m_switchBtn); + setLayout(switchLayout); + + connect(m_switchBtn, &DSwitchButton::toggled, [&](bool change) { + emit checkedChanged(change); + }); +} + +void SwitchItem::setChecked(const bool checked) +{ + m_switchBtn->setChecked(checked); +} + +void SwitchItem::setTitle(const QString &title) +{ + m_title->setText(title); +} + +//void SwitchItem::mousePressEvent(QMouseEvent *event) +//{ +// emit clicked(m_adapterId); +// QWidget::mousePressEvent(event); +//} diff --git a/plugins/bluetooth/componments/switchitem.h b/plugins/bluetooth/componments/switchitem.h new file mode 100644 index 000000000..2da856a39 --- /dev/null +++ b/plugins/bluetooth/componments/switchitem.h @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2016 ~ 2018 Deepin Technology Co., Ltd. + * + * Author: zhaolong + * + * Maintainer: zhaolong + * + * + * 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 SWITCHITEM_H +#define SWITCHITEM_H + +#include + +#include + +DWIDGET_USE_NAMESPACE + +class SwitchItem : public QWidget +{ + Q_OBJECT +public: + explicit SwitchItem(QWidget *parent = nullptr); + void setChecked(const bool checked = true); + void setTitle(const QString &title); + + inline bool isdefault() { return m_default; } + inline void setDefault(bool def) { m_default = def; } + +//protected: +// void mousePressEvent(QMouseEvent *event) override; + +signals: + void checkedChanged(bool checked); +// void clicked(const QString &adapterId); + +private: + QLabel *m_title; + DSwitchButton *m_switchBtn; + bool m_default; +}; + +#endif // SWITCHITEM_H diff --git a/plugins/bluetooth/resources/bluetooth-active-symbolic-dark.svg b/plugins/bluetooth/resources/bluetooth-active-symbolic-dark.svg new file mode 100644 index 000000000..01b0d2de6 --- /dev/null +++ b/plugins/bluetooth/resources/bluetooth-active-symbolic-dark.svg @@ -0,0 +1,3 @@ + + + diff --git a/plugins/bluetooth/resources/bluetooth-active-symbolic.svg b/plugins/bluetooth/resources/bluetooth-active-symbolic.svg new file mode 100644 index 000000000..95451d854 --- /dev/null +++ b/plugins/bluetooth/resources/bluetooth-active-symbolic.svg @@ -0,0 +1,3 @@ + + + diff --git a/plugins/bluetooth/resources/bluetooth-disable-symbolic-dark.svg b/plugins/bluetooth/resources/bluetooth-disable-symbolic-dark.svg new file mode 100644 index 000000000..ca52701dc --- /dev/null +++ b/plugins/bluetooth/resources/bluetooth-disable-symbolic-dark.svg @@ -0,0 +1,3 @@ + + + diff --git a/plugins/bluetooth/resources/bluetooth-disable-symbolic.svg b/plugins/bluetooth/resources/bluetooth-disable-symbolic.svg new file mode 100644 index 000000000..d490dae3d --- /dev/null +++ b/plugins/bluetooth/resources/bluetooth-disable-symbolic.svg @@ -0,0 +1,3 @@ + + + diff --git a/plugins/bluetooth/resources/bluetooth-waiting-symbolic-dark.svg b/plugins/bluetooth/resources/bluetooth-waiting-symbolic-dark.svg new file mode 100644 index 000000000..6a708a3cd --- /dev/null +++ b/plugins/bluetooth/resources/bluetooth-waiting-symbolic-dark.svg @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/plugins/bluetooth/resources/bluetooth-waiting-symbolic.svg b/plugins/bluetooth/resources/bluetooth-waiting-symbolic.svg new file mode 100644 index 000000000..c4683870f --- /dev/null +++ b/plugins/bluetooth/resources/bluetooth-waiting-symbolic.svg @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/plugins/bluetooth/resources/bluetooth.qrc b/plugins/bluetooth/resources/bluetooth.qrc new file mode 100644 index 000000000..b99132e0c --- /dev/null +++ b/plugins/bluetooth/resources/bluetooth.qrc @@ -0,0 +1,14 @@ + + + bluetooth-active-symbolic-dark.svg + bluetooth-active-symbolic.svg + bluetooth-disable-symbolic-dark.svg + bluetooth-disable-symbolic.svg + bluetooth-waiting-symbolic-dark.svg + bluetooth-waiting-symbolic.svg + list_select.png + list_select@2x.png + notify_close_press.png + notify_close_press@2x.png + + diff --git a/plugins/bluetooth/resources/list_select.png b/plugins/bluetooth/resources/list_select.png new file mode 100644 index 0000000000000000000000000000000000000000..886736105df8591d6a50568e75e8642c7d408e74 GIT binary patch literal 508 zcmVPx$wn;=mR5%fhQ#(ilK@fejcb7mAu`(8lAOu7qApSsNnKbDIJBxsY5G`!%?G({~ zAecU2txYTyL{S706;r9GXeEM0O!n5D)gv28qTB3z-kY7@5`LYbo$^3cQU*{dMd;OT zMGE%-66nfzIH+(=^=Yq!O1J6D-d_N zVG4f3ngb%=5f@lbxF|mOIDXMN08Ukkj4nZ?So`ZNkGm*KVEL?SK~+lpjLI?sU6DE! zGyM+6q5=y#bq*37=9(r32EO|{2)pSJ#-ra89_=J#;|`XNs(Adckw#WRFUg&4zIjvF zx|GOGdPsH)tW0>=zLGe2_HCGvdBJpRsHpoeXnXV55^Wx^mhf=!BuzluUz4fWa*VTs z4hpvt?`G#}G?sWPd5osec;R!yho7y7wAKVf?qWJLZ<@%v&Fk5kQu8vFIczZjc&B-{ yIVz`CgVsyzV@&W5*lS9#VGX`Bz9-fAJAVOTu&U0u{Yv=&0000Px(Qb|NXR9FeMmtANaRTRh1nNOo_LK(~q=Z+aN_P4F*#b3VuBK z;6p0}>WdG}L&UbA2o@2k4?d+D35XE1QfjKyB(<6>shUMk*fBY z>$TIvk6|`WXr5cVHT2S5l*zmSi2I4Cs<^i|iikIX=wmd^!#}m-AI0WG3h3w|ai&1Gro8P#@L7tTkS8wMVG!-lD;t zwJMBWci`o(voM?UsW<}_#(kr0IO%Dd<#S%@QHy{-zfp1FGCY2R@^CQ-OPJhH_G8Sp zuCcXJ4CQEzH3%nKbxu9P*t7%tzs!Q=6h_8d3Du}oexcmXa>7@c0LQj@@S7IF!GSQi zh@nbhD>m6}h1yE(^Mc`Miw&WwWG!WHrLp)qzVh5PaQA zgs#mV{OXLC`hY`3N7#}tenz*ZT7(aG>F~~W9Ui@Nv4{%hzP(+8J9LDM?G@dvSYHAz zlca9a(RhG=<}z8KA-wmv4hhc2!q&A(gY{M3zIP4f;3A*-xJ}r+1yoc(>N>%zXR_|< zR`4p{-=V|0N>9WK_p7kGp6^?3J>xd?{kCM$1+)oUQAimHSVKBP*9cx4$U)X2s5TKk zU=e%QDsZ6DgO5x((0$&&XF~!+n1qcDJ=tB3ZJ&zp37B`+EAZA<4HV38!*qtAqu+wb zzXPtZI*O{cDywR>GkhGTtzIaYuU0%`o0_3Z?!Z0>`-;Pu6 z`H>%d8+7{)ah|2Z-Pp`>ncVRXF#o7oZ5-p;`YI0mzxW74SbmIrIkV;A XJw9fJ6 zxY2hg?-PH;mB!|gJ^7-a&wzkrz(nIo-lLz(3+6KCm=b#-To?fXViP#(N3}>>Qh+~~?+AS4WVYERYU97*N&ezm!yjI*AOcKt qbx-jB^puL&ccDWcPx$NJ&INR45glQn60MKn%4n3Ka;bgam5_U@J_WC}Kd&?ZyZ210Bj|(1C%K8L`nB zu|S!afQ>Ghk&sFeC`8HO=k&-a>Qqb7#m|2B^PQPIYu(tD&!68@OVk#4YZ@9c(O`9P z;cVF9iAxu(u-)tQFwe3}@gj#+&e#OO$@6h|2V99t%JW;Y_PFHf1mMP3%rt~U*P+Q6O zGe8{qfXWl8=+u2EW&1hc7%4n(AZ`!yVNS@y*Ly6;h&cmi?&r`l^DKZ~4mJTBAyQ%| sXT}-3twF35E{?b4c-u`Rh|OH`Kh4#Xcx*b4Z~y=R07*qoM6N<$f?boY0RR91 literal 0 HcmV?d00001 diff --git a/plugins/bluetooth/resources/notify_close_press@2x.png b/plugins/bluetooth/resources/notify_close_press@2x.png new file mode 100644 index 0000000000000000000000000000000000000000..7d67765b4382dcd75440c6994dd941f5716b1337 GIT binary patch literal 749 zcmVPx%r%6OXR7efImQ9NjK@f(kdh&7cAS`;&lP3k;T@?gHMO^Tt2f+{*e~U+di>x8S z-UUTgQ4rM4D(K16uAl@Iy(E*Fvfc?j+v)8YCvk8fq`Uguf6sTJkWlWRtn}3hYe?1+`bRhR9^#VUKo6WWpFpTfKc0o4%XWr5Gj6TOC9n97N6M|{UVV= zA%SmmqrzCxzWVd>KJ)MV9OlNdhOSB3aD_3^C8fOzLdv3R@|s=7qC_h5rL3<=n%y!H z##H#t-?KE9r}IL=LegxN0sYAAS=6;XT6SbYKEX2j=9{5G*xV!uC5W57p>yi{1uOen zEN_fHg}&;z8*yEQV6HRw#m+v8FHh3sg*%Qms)3#L_NgpOQ5N_D^e3Dgh6B5dAq{Lc z(hR0^LORRHB7{Jre6?sv5dye2lpGq}CD`(Fh#Eu#AG*SN+FEL4zNg-DAil_`K{S)% zdO$c%9K)ym+QV-SL?^2~v<9gsQe7>to;d)tP}Cq;ck~x|Fc&`D;>r5Slg1j{|NmA9 fyHK?2EcO2YZ*iaQG~OMb00000NkvXXu0mjfZ?#?x literal 0 HcmV?d00001 diff --git a/plugins/network/item/applet/wirelist.cpp b/plugins/network/item/applet/wirelist.cpp new file mode 100644 index 000000000..5a29d375d --- /dev/null +++ b/plugins/network/item/applet/wirelist.cpp @@ -0,0 +1,90 @@ +#include "wirelist.h" + +#include +#include + +using namespace dde::network; + +const int Width = 300; +const int ItemHeight = 30; + +WireList::WireList(WiredDevice *device, QWidget *parent) + : QScrollArea(parent) + , m_device(device) + , m_updateAPTimer(new QTimer(this)) + , m_deviceName(new QLabel(this)) + , m_switchBtn(new DSwitchButton(this)) +{ + + m_updateAPTimer->setSingleShot(true); + m_updateAPTimer->setInterval(100); + + auto centralWidget = new QWidget(this); + centralWidget->setFixedWidth(Width); + m_centralLayout = new QVBoxLayout; + m_centralLayout->setMargin(0); + m_centralLayout->setSpacing(0); + centralWidget->setLayout(m_centralLayout); + + auto controlPanel = new QWidget(this); + controlPanel->setFixedWidth(Width); + controlPanel->setFixedHeight(ItemHeight); + auto controlPanelLayout = new QHBoxLayout; + controlPanelLayout->setMargin(0); + controlPanelLayout->setSpacing(0); + controlPanelLayout->addSpacing(5); + controlPanelLayout->addWidget(m_deviceName); + controlPanelLayout->addStretch(); + controlPanelLayout->addWidget(m_switchBtn); + controlPanelLayout->addSpacing(5); + controlPanel->setLayout(controlPanelLayout); + + m_centralLayout->addWidget(controlPanel); + + setWidget(centralWidget); + setFixedWidth(Width); + setFrameShape(QFrame::NoFrame); + setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + centralWidget->setAutoFillBackground(false); + viewport()->setAutoFillBackground(false); + + connect(m_device, &WiredDevice::connectionsChanged, this, &WireList::changeConnections); + connect(m_device, &WiredDevice::activeWiredConnectionInfoChanged, this, &WireList::changeActiveWiredConnectionInfo); + connect(m_device, &WiredDevice::activeConnectionsChanged, this, &WireList::changeActiveConnections); + connect(m_device, &WiredDevice::activeConnectionsInfoChanged, this, &WireList::changeActiveConnectionsInfo); + + connect(m_switchBtn, &DSwitchButton::checkedChanged, this, &WireList::deviceEnabled); + + connect(m_updateAPTimer, &QTimer::timeout, this, &WireList::updateConnectionList); + + QMetaObject::invokeMethod(this, "loadConnectionList", Qt::QueuedConnection); +} + +void WireList::changeConnections(const QList &connections) +{ + for (auto object : connections) { + qDebug() << object; + } +} + +void WireList::changeActiveWiredConnectionInfo(const QJsonObject &connInfo) +{ + for (auto object : connInfo) { + qDebug() << object; + } +} + +void WireList::changeActiveConnections(const QList &activeConns) +{ + for (auto object : activeConns) { + qDebug() << object; + } +} + +void WireList::changeActiveConnectionsInfo(const QList &activeConnInfoList) +{ + for (auto object : activeConnInfoList) { + qDebug() << object; + } +} diff --git a/plugins/network/item/applet/wirelist.h b/plugins/network/item/applet/wirelist.h new file mode 100644 index 000000000..d6d65b2e6 --- /dev/null +++ b/plugins/network/item/applet/wirelist.h @@ -0,0 +1,43 @@ +#ifndef WIRELIST_H +#define WIRELIST_H + +#include +#include +#include +#include + +#include +#include +#include + +DWIDGET_USE_NAMESPACE + +class WireList : public QScrollArea +{ + Q_OBJECT +public: + WireList(dde::network::WiredDevice *device, QWidget *parent = nullptr); + +public slots: + void changeConnections(const QList &connections); + void changeActiveWiredConnectionInfo(const QJsonObject &connInfo); + void changeActiveConnections(const QList &activeConns); + void changeActiveConnectionsInfo(const QList &activeConnInfoList); + void deviceEnabled(bool enabled); + void updateConnectionList(); + +private slots: + void loadConnectionList(); + +private: + QPointer m_device; + + QTimer *m_updateAPTimer; + + QLabel *m_deviceName; + DSwitchButton *m_switchBtn; + + QVBoxLayout *m_centralLayout; +}; + +#endif // WIRELIST_H diff --git a/plugins/network/item/plugincase.cpp b/plugins/network/item/plugincase.cpp new file mode 100644 index 000000000..66952710c --- /dev/null +++ b/plugins/network/item/plugincase.cpp @@ -0,0 +1,80 @@ +#include "plugincase.h" + +PluginCase::PluginCase(QObject *parent) + : QObject(parent) +{ + wireOpenState = new WireOpenState(this); + wireCloseState = new WireCloseState(this); + wireConnectingState = new WireConnetingState(this); + wireNoCableState = new WireNoCableState(this); + wireNoIpState = new WireNoIpState(this); + wireNoInternetState = new WireNoInternetState(this); + wirelessOpenState = new WirelessOpenState(this); + wirelessCloseState = new WirelessCloseState(this); + wirelessNoIp = new WirelessNoIp(this); + wirelessNoInternet = new WirelessNoInternet(this); +} + +void PluginCase::setState(PluginState *state) +{ + m_state = state; + state->setPluginCase(this); +} + +PluginState *PluginCase::getState() +{ + return m_state; +} + +void PluginCase::openWire() +{ + m_state->openWire(); +} + +void PluginCase::closeWire() +{ + m_state->closeWire(); +} + +void PluginCase::wireConnect() +{ + m_state->wireConnect(); +} + +void PluginCase::openWireless() +{ + m_state->openWireless(); +} + +void PluginCase::closeWireless() +{ + m_state->closeWireless(); +} + +void PluginCase::wirelessConnect() +{ + m_state->wirelessConnect(); +} + +void WireOpenState::closeWire() +{ + m_pluginCase->setState(m_pluginCase->wireCloseState); + m_pluginCase->closeWire(); +} + +void WireOpenState::wireConnect() +{ + m_pluginCase->setState(m_pluginCase->wireConnectingState); + m_pluginCase->wireLinkCheck(); +} + +void WireConnetingState::wireLinkCheck() +{ + if (m_pluginCase->cablePluged) { + m_pluginCase->setState(m_pluginCase->wireConnectingState); + m_pluginCase->wireIpCheck(); + } + else { + m_pluginCase->setState(m_pluginCase->wireNoCableState); + } +} diff --git a/plugins/network/item/plugincase.h b/plugins/network/item/plugincase.h new file mode 100644 index 000000000..a45c2e420 --- /dev/null +++ b/plugins/network/item/plugincase.h @@ -0,0 +1,250 @@ +#ifndef PLUGINCASE_H +#define PLUGINCASE_H + +#include + +class PluginCase; +class PluginState : public QObject +{ + Q_OBJECT +public: + void setPluginCase(PluginCase *pcase) {m_pluginCase = pcase;} + virtual void openWire() {} + virtual void closeWire() {} + virtual void wireConnect() {} + virtual void wireLinkCheck() {} + virtual void wireIpCheck() {} + virtual void wireInternetCheck() {} + virtual void openWireless() {} + virtual void closeWireless() {} + virtual void wirelessConnect() {} +protected: + explicit PluginState(QObject *parent = nullptr) : QObject(parent) {} + +protected: + PluginCase *m_pluginCase; +}; + +class WireOpenState : public PluginState +{ + Q_OBJECT +public: + explicit WireOpenState(QObject *parent = nullptr); + void closeWire() override; + void wireConnect() override; + void openWireless() override; + void closeWireless() override; + void wirelessConnect() override; +}; + +class WireCloseState : public PluginState +{ + Q_OBJECT +public: + explicit WireCloseState(QObject *parent = nullptr); + void openWire() override; + void closeWire() override; + void wireConnect() override; + void openWireless() override; + void closeWireless() override; + void wirelessConnect() override; +}; + +class WireOpenWirelessCloseState : public PluginState +{ + Q_OBJECT +public: + explicit WireOpenWirelessCloseState(QObject *parent = nullptr); + void closeWire() override; + void wireConnect() override; + void openWireless() override; + void closeWireless() override; + void wirelessConnect() override; +}; + +class WireCloseWirelessOpenState : public PluginState +{ + Q_OBJECT +public: + explicit WireCloseWirelessOpenState(QObject *parent = nullptr); + void closeWire() override; + void wireConnect() override; + void openWireless() override; + void closeWireless() override; + void wirelessConnect() override; +}; + +class WireConnetingState : public PluginState +{ + Q_OBJECT +public: + explicit WireConnetingState(QObject *parent = nullptr); + void openWire() override; + void closeWire() override; + void wireLinkCheck() override; + void wireIpCheck() override; + void wireInternetCheck() override; + void openWireless() override; + void closeWireless() override; + void wirelessConnect() override; +}; + +class WireNoCableState : public PluginState +{ + Q_OBJECT +public: + explicit WireNoCableState(QObject *parent = nullptr); + void openWire() override; + void closeWire() override; + void wireConnect() override; + void openWireless() override; + void closeWireless() override; + void wirelessConnect() override; +}; + +class WireNoIpState : public PluginState +{ + Q_OBJECT +public: + explicit WireNoIpState(QObject *parent = nullptr); + void openWire() override; + void closeWire() override; + void wireConnect() override; + void openWireless() override; + void closeWireless() override; + void wirelessConnect() override; +}; + +class WireNoInternetState : public PluginState +{ + Q_OBJECT +public: + explicit WireNoInternetState(QObject *parent = nullptr); + void openWire() override; + void closeWire() override; + void wireConnect() override; + void openWireless() override; + void closeWireless() override; + void wirelessConnect() override; +}; + +class WirelessOpenState : public PluginState +{ + Q_OBJECT +public: + explicit WirelessOpenState(QObject *parent = nullptr); + void openWire() override; + void closeWire() override; + void wireConnect() override; + void openWireless() override; + void closeWireless() override; + void wirelessConnect() override; +}; + +class WirelessCloseState : public PluginState +{ + Q_OBJECT +public: + explicit WirelessCloseState(QObject *parent = nullptr); + void openWire() override; + void closeWire() override; + void wireConnect() override; + void openWireless() override; + void closeWireless() override; + void wirelessConnect() override; +}; + +class WirelessNoIp : public PluginState +{ + Q_OBJECT +public: + explicit WirelessNoIp(QObject *parent = nullptr); + void openWire() override; + void closeWire() override; + void wireConnect() override; + void openWireless() override; + void closeWireless() override; + void wirelessConnect() override; +}; + +class WirelessNoInternet : public PluginState +{ + Q_OBJECT +public: + explicit WirelessNoInternet(QObject *parent = nullptr); + void openWire() override; + void closeWire() override; + void wireConnect() override; + void openWireless() override; + void closeWireless() override; + void wirelessConnect() override; +}; + +class AllOpenState : public PluginState +{ + Q_OBJECT +public: + explicit AllOpenState(QObject *parent = nullptr); + void openWire() override; + void closeWire() override; + void wireConnect() override; + void openWireless() override; + void closeWireless() override; + void wirelessConnect() override; +}; + +class AllCloseState : public PluginState +{ + Q_OBJECT +public: + explicit AllCloseState(QObject *parent = nullptr); + void openWire() override; + void closeWire() override; + void wireConnect() override; + void openWireless() override; + void closeWireless() override; + void wirelessConnect() override; +}; + +class PluginCase : public QObject +{ + Q_OBJECT +public: + explicit PluginCase(QObject *parent = nullptr); + + void setState(PluginState *state); + PluginState *getState(); + + void openWire(); + void closeWire(); + void wireConnect(); + void wireConnecting(); + void wireLinkCheck(); + void wireIpCheck(); + void wireInternetCheck(); + void openWireless(); + void closeWireless(); + void wirelessConnect(); + +signals: + +public slots: + +public: + WireOpenState *wireOpenState; + WireCloseState *wireCloseState; + WireConnetingState *wireConnectingState; + WireNoCableState *wireNoCableState; + WireNoIpState *wireNoIpState; + WireNoInternetState *wireNoInternetState; + WirelessOpenState *wirelessOpenState; + WirelessCloseState *wirelessCloseState; + WirelessNoIp *wirelessNoIp; + WirelessNoInternet *wirelessNoInternet; + bool cablePluged; + +private: + PluginState *m_state = nullptr; +}; + +#endif // PLUGINCASE_H diff --git a/translations/dde-dock.ts b/translations/dde-dock.ts index 465a2153b..ef173dab2 100644 --- a/translations/dde-dock.ts +++ b/translations/dde-dock.ts @@ -1,4 +1,6 @@ - + + + AbstractPluginsController @@ -6,6 +8,42 @@ The plugin %1 is not compatible with the system. + + AdapterItem + + My Device + + + + + BluetoothApplet + + Bluetooth + + + + + BluetoothItem + + Turn off + + + + Turn on + + + + Bluetooth settings + + + + + BluetoothPlugin + + Bluetooth + + + DBusAdaptors @@ -383,4 +421,4 @@ Wireless Network %1 - \ No newline at end of file +