From 33e0ac383b8487a70a86a46f5d14967f26bbb7fa Mon Sep 17 00:00:00 2001 From: Gary Wang Date: Wed, 19 Sep 2018 10:56:17 +0800 Subject: [PATCH] chore(trash): now trash plugin is a part of dde-dock Change-Id: I27d74962726a3b4dc5fe85a56e72a36bd5edbcd8 --- .gitignore | 1 + debian/control | 9 ++- plugins/CMakeLists.txt | 2 +- plugins/trash/CMakeLists.txt | 7 -- plugins/trash/popupcontrolwidget.cpp | 104 +++++++++++++++------------ plugins/trash/popupcontrolwidget.h | 7 +- plugins/trash/trashplugin.cpp | 34 +++++++-- plugins/trash/trashplugin.h | 12 +++- plugins/trash/trashwidget.cpp | 79 ++++++++++++++------ plugins/trash/trashwidget.h | 12 +++- 10 files changed, 175 insertions(+), 92 deletions(-) diff --git a/.gitignore b/.gitignore index ecd276d66..75e419f14 100644 --- a/.gitignore +++ b/.gitignore @@ -17,6 +17,7 @@ build*/ CMakeLists.txt.user* *.DS_Store *.qm +.directory # binary file dde-dock diff --git a/debian/control b/debian/control index 8fca036f0..624425088 100644 --- a/debian/control +++ b/debian/control @@ -21,16 +21,15 @@ Homepage: http://www.deepin.org/ Package: dde-dock Architecture: any Depends: ${shlibs:Depends}, ${misc:Depends}, deepin-menu -Recommends: dde-disk-mount-plugin, - dde-trash-plugin +Recommends: dde-disk-mount-plugin Conflicts: dde-workspace (<< 2.90.5), - dde-dock-applets + dde-dock-applets, dde-trash-plugin Replaces: dde-dock-applets Description: deepin desktop-environment - dock module - Deepin desktop environment 2015 - dock module. + Deepin desktop environment - dock module. Package: dde-dock-dev Architecture: all Depends: ${shlibs:Depends}, ${misc:Depends} Description: deepin desktop-environment - dock module development files - Deepin desktop environment 2015 - dock module development files. + Deepin desktop environment - dock module development files. diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt index 890a77ecb..453c5e027 100644 --- a/plugins/CMakeLists.txt +++ b/plugins/CMakeLists.txt @@ -5,5 +5,5 @@ add_subdirectory("network") add_subdirectory("shutdown") add_subdirectory("sound") add_subdirectory("system-tray") -#add_subdirectory("trash") +add_subdirectory("trash") add_subdirectory("keyboard-layout") diff --git a/plugins/trash/CMakeLists.txt b/plugins/trash/CMakeLists.txt index 85700c345..18a48b802 100644 --- a/plugins/trash/CMakeLists.txt +++ b/plugins/trash/CMakeLists.txt @@ -9,23 +9,16 @@ file(GLOB 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(DFrameworkDBus REQUIRED dframeworkdbus) - add_definitions("${QT_DEFINITIONS} -DQT_PLUGIN") add_library(${PLUGIN_NAME} SHARED ${SRCS}) set_target_properties(${PLUGIN_NAME} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ../) target_include_directories(${PLUGIN_NAME} PUBLIC ${DtkWidget_INCLUDE_DIRS} - ${DFrameworkDBus_INCLUDE_DIRS} - ${Qt5DBus_INCLUDE_DIRS} ../../interfaces) target_link_libraries(${PLUGIN_NAME} PRIVATE ${DtkWidget_LIBRARIES} - ${DFrameworkDBus_LIBRARIES} ${Qt5Widgets_LIBRARIES} - ${Qt5DBus_LIBRARIES} ${Qt5Svg_LIBRARIES} ) diff --git a/plugins/trash/popupcontrolwidget.cpp b/plugins/trash/popupcontrolwidget.cpp index 8f6b1dabb..d4086c4d8 100644 --- a/plugins/trash/popupcontrolwidget.cpp +++ b/plugins/trash/popupcontrolwidget.cpp @@ -1,9 +1,14 @@ /* * Copyright (C) 2011 ~ 2018 Deepin Technology Co., Ltd. + * 2016 ~ 2018 dragondjf * * Author: sbw + * dragondjf + * zccrs + * Tangtong * - * Maintainer: sbw + * Maintainer: dragondjf + * zccrs * * 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 @@ -27,14 +32,16 @@ #include #include +#include +#include -#include +#include DWIDGET_USE_NAMESPACE const QString TrashDir = QDir::homePath() + "/.local/share/Trash"; +const QDir::Filters ItemsShouldCount = QDir::AllEntries | QDir::Hidden | QDir::System | QDir::NoDotAndDotDot; -using SoundEffectInter = com::deepin::daemon::SoundEffect; PopupControlWidget::PopupControlWidget(QWidget *parent) : QWidget(parent), @@ -73,54 +80,56 @@ const QString PopupControlWidget::trashDir() void PopupControlWidget::openTrashFloder() { - QProcess *proc = new QProcess; - - connect(proc, static_cast(&QProcess::finished), proc, &QProcess::deleteLater); - - proc->startDetached("gvfs-open trash:///"); + QProcess::startDetached("gio", QStringList() << "open" << "trash:///"); } void PopupControlWidget::clearTrashFloder() { + QString ClearTrashMutliple = qApp->translate("DialogManager", "Are you sure to empty %1 items?"); + // show confrim dialog - bool accept = false; - const int itemCount = trashItemCount(); - const QStringList btns = {tr("Cancel"), tr("Empty")}; + DDialog d; + QStringList buttonTexts; + buttonTexts << qApp->translate("DialogManager", "Cancel") << qApp->translate("DialogManager", "Delete"); - DDialog *dialog = new DDialog(nullptr); - dialog->addButtons(btns); - dialog->setIconPixmap(QIcon::fromTheme("user-trash-full").pixmap(48, 48)); - dialog->setMessage(tr("This action cannot be restored")); - if (itemCount == 1) - dialog->setTitle(tr("Are you sure to empty 1 item ?")); - else - dialog->setTitle(tr("Are you sure to empty %1 items ?").arg(itemCount)); + if (!d.parentWidget()) { + d.setWindowFlags(d.windowFlags() | Qt::WindowStaysOnTopHint); + } - connect(dialog, &DDialog::buttonClicked, [&] (const int index) { - accept = index; - }); - dialog->exec(); - dialog->deleteLater(); + QDir dir(TrashDir + "/files");//QDir::homePath() + "/.local/share/Trash/files"); + uint count = dir.entryList(ItemsShouldCount).count(); + int execCode = -1; - if (!accept) + if (count > 0) { + // blumia: Workaround. There is a bug with DDialog which will let DDialog always use the smallest + // avaliable size of the given icon. So we create a m_dialogTrashFullIcon and leave a minimum + // 64*64 pixmap size icon here. + QIcon m_dialogTrashFullIcon; + QIcon trash_full_icon = QIcon::fromTheme("user-trash-full-opened"); + m_dialogTrashFullIcon.addPixmap(trash_full_icon.pixmap(64)); + m_dialogTrashFullIcon.addPixmap(trash_full_icon.pixmap(128)); + + d.setTitle(ClearTrashMutliple.arg(count)); + d.setMessage(qApp->translate("DialogManager", "This action cannot be restored")); + d.setIcon(m_dialogTrashFullIcon); + d.addButton(buttonTexts[0], true, DDialog::ButtonNormal); + d.addButton(buttonTexts[1], false, DDialog::ButtonWarning); + d.setDefaultButton(1); + d.getButton(1)->setFocus(); + d.moveToCenter(); + execCode = d.exec(); + } + + if (execCode != QDialog::Accepted) { return; + } - QProcess::startDetached("gvfs-trash", QStringList() << "-f" << "--empty"); - - // play sound effects - SoundEffectInter sei("com.deepin.daemon.SoundEffect", "/com/deepin/daemon/SoundEffect", QDBusConnection::sessionBus()); - sei.PlaySystemSound("trash-empty"); - -// for (auto item : QDir(TrashDir).entryInfoList()) -// { -// if (item.fileName() == "." || item.fileName() == "..") -// continue; - -// if (item.isFile()) -// QFile(item.fileName()).remove(); -// else if (item.isDir()) -// QDir(item.absoluteFilePath()).removeRecursively(); -// } + if (DTrashManager::instance()->cleanTrash()) { + DDesktopServices::playSystemSoundEffect(DDesktopServices::SSE_EmptyTrash); + } else { + qWarning() << "Clear trash failed"; + } +// DFMGlobal::instance()->clearTrash(); } int PopupControlWidget::trashItemCount() const @@ -135,20 +144,23 @@ void PopupControlWidget::trashStatusChanged() // add monitor paths m_fsWatcher->addPath(TrashDir); - if (files) + if (files) { m_fsWatcher->addPath(TrashDir + "/files"); + } // if (info) // m_fsWatcher->addPath(TrashDir + "/info"); // check empty - if (!files) + if (!files) { m_trashItemsCount = 0; - else - m_trashItemsCount = QDir(TrashDir + "/files").entryList().count() - 2; + } else { + m_trashItemsCount = QDir(TrashDir + "/files").entryList(ItemsShouldCount).count(); + } const bool empty = m_trashItemsCount == 0; - if (m_empty == empty) + if (m_empty == empty) { return; + } // m_clearBtn->setVisible(!empty); m_empty = empty; diff --git a/plugins/trash/popupcontrolwidget.h b/plugins/trash/popupcontrolwidget.h index 165243c59..a391697b8 100644 --- a/plugins/trash/popupcontrolwidget.h +++ b/plugins/trash/popupcontrolwidget.h @@ -1,9 +1,14 @@ /* * Copyright (C) 2011 ~ 2018 Deepin Technology Co., Ltd. + * 2016 ~ 2018 dragondjf * * Author: sbw + * dragondjf + * zccrs + * Tangtong * - * Maintainer: sbw + * Maintainer: dragondjf + * zccrs * * 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 diff --git a/plugins/trash/trashplugin.cpp b/plugins/trash/trashplugin.cpp index 0a2c08772..4efde69da 100644 --- a/plugins/trash/trashplugin.cpp +++ b/plugins/trash/trashplugin.cpp @@ -1,9 +1,14 @@ /* * Copyright (C) 2011 ~ 2018 Deepin Technology Co., Ltd. + * 2016 ~ 2018 dragondjf * * Author: sbw + * dragondjf + * zccrs + * Tangtong * - * Maintainer: sbw + * Maintainer: dragondjf + * zccrs * * 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 @@ -21,10 +26,15 @@ #include "trashplugin.h" +#include + +DWIDGET_USE_NAMESPACE + TrashPlugin::TrashPlugin(QObject *parent) : QObject(parent), m_trashWidget(new TrashWidget), - m_tipsLabel(new QLabel) + m_tipsLabel(new QLabel), + m_settings("deepin", "dde-dock-trash") { m_tipsLabel->setObjectName("trash"); m_tipsLabel->setStyleSheet("color:white;" @@ -40,8 +50,14 @@ const QString TrashPlugin::pluginName() const void TrashPlugin::init(PluginProxyInterface *proxyInter) { - m_proxyInter = proxyInter; + // blumia: we are using i10n translation from DFM so... + QString applicationName = qApp->applicationName(); + qApp->setApplicationName("dde-file-manager"); + qDebug() << qApp->loadTranslator(); + qApp->setApplicationName(applicationName); + m_proxyInter = proxyInter; +// DFMGlobal::instance()->installTranslator(); displayModeChanged(displayMode()); } @@ -78,7 +94,7 @@ const QString TrashPlugin::itemCommand(const QString &itemKey) Q_UNUSED(itemKey); // return QString(); - return "gvfs-open trash:///"; + return "gio open trash:///"; } const QString TrashPlugin::itemContextMenu(const QString &itemKey) @@ -104,10 +120,14 @@ void TrashPlugin::invokedMenuItem(const QString &itemKey, const QString &menuId, int TrashPlugin::itemSortKey(const QString &itemKey) { - Q_UNUSED(itemKey); + const QString &key = QString("pos_%1_%2").arg(itemKey).arg(displayMode()); + return m_settings.value(key, -1).toInt(); +} - // always place at last - return -1; +void TrashPlugin::setSortKey(const QString &itemKey, const int order) +{ + const QString &key = QString("pos_%1_%2").arg(itemKey).arg(displayMode()); + m_settings.setValue(key, order); } void TrashPlugin::displayModeChanged(const Dock::DisplayMode displayMode) diff --git a/plugins/trash/trashplugin.h b/plugins/trash/trashplugin.h index a5ae5cdd7..d1c28fdad 100644 --- a/plugins/trash/trashplugin.h +++ b/plugins/trash/trashplugin.h @@ -1,9 +1,14 @@ /* * Copyright (C) 2011 ~ 2018 Deepin Technology Co., Ltd. + * 2016 ~ 2018 dragondjf * * Author: sbw + * dragondjf + * zccrs + * Tangtong * - * Maintainer: sbw + * Maintainer: dragondjf + * zccrs * * 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 @@ -26,6 +31,7 @@ #include "trashwidget.h" #include +#include class TrashPlugin : public QObject, PluginsItemInterface { @@ -47,7 +53,8 @@ public: void refershIcon(const QString &itemKey); void invokedMenuItem(const QString &itemKey, const QString &menuId, const bool checked); - int itemSortKey(const QString &itemKey); + int itemSortKey(const QString &itemKey) override; + void setSortKey(const QString &itemKey, const int order) override; void displayModeChanged(const Dock::DisplayMode displayMode); private: @@ -56,6 +63,7 @@ private: private: TrashWidget *m_trashWidget; QLabel *m_tipsLabel; + QSettings m_settings; }; #endif // TRASHPLUGIN_H diff --git a/plugins/trash/trashwidget.cpp b/plugins/trash/trashwidget.cpp index 43371db15..53d56fe78 100644 --- a/plugins/trash/trashwidget.cpp +++ b/plugins/trash/trashwidget.cpp @@ -1,9 +1,14 @@ /* * Copyright (C) 2011 ~ 2018 Deepin Technology Co., Ltd. + * 2016 ~ 2018 dragondjf * * Author: sbw + * dragondjf + * zccrs + * Tangtong * - * Maintainer: sbw + * Maintainer: dragondjf + * zccrs * * 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 @@ -27,6 +32,7 @@ #include #include #include +#include DWIDGET_USE_NAMESPACE @@ -100,11 +106,43 @@ void TrashWidget::invokeMenuItem(const QString &menuId, const bool checked) void TrashWidget::dragEnterEvent(QDragEnterEvent *e) { - if (e->mimeData()->hasFormat("RequestDock")) - return e->accept(); + if (e->mimeData()->hasFormat("RequestDock")) { + // accept prevent the event from being propgated to the dock main panel + // which also takes drag event; + e->accept(); - if (e->mimeData()->hasFormat("text/uri-list")) - return e->accept(); + if (!e->mimeData()->hasFormat("Removable")) { + // show the forbit dropping cursor. + e->setDropAction(Qt::IgnoreAction); + } + + return; + } + + if (!e->mimeData()->hasUrls()) + e->ignore(); + + e->setDropAction(Qt::MoveAction); + + if (e->dropAction() != Qt::MoveAction) { + e->ignore(); + } else { + e->accept(); + } +} + +void TrashWidget::dragMoveEvent(QDragMoveEvent *e) +{ + if (!e->mimeData()->hasUrls()) + return; + + e->setDropAction(Qt::MoveAction); + + if (e->dropAction() != Qt::MoveAction) { + e->ignore(); + } else { + e->accept(); + } } void TrashWidget::dropEvent(QDropEvent *e) @@ -112,12 +150,19 @@ void TrashWidget::dropEvent(QDropEvent *e) if (e->mimeData()->hasFormat("RequestDock")) return removeApp(e->mimeData()->data("AppKey")); - if (e->mimeData()->hasFormat("text/uri-list")) - { - const QMimeData *mime = e->mimeData(); - for (auto url : mime->urls()) - moveToTrash(url); + if (!e->mimeData()->hasUrls()) { + return e->ignore(); } + + e->setDropAction(Qt::MoveAction); + + if (e->dropAction() != Qt::MoveAction) { + return e->ignore(); + } + + const QMimeData *mime = e->mimeData(); + for (auto url : mime->urls()) + moveToTrash(url); } void TrashWidget::paintEvent(QPaintEvent *e) @@ -125,7 +170,7 @@ void TrashWidget::paintEvent(QPaintEvent *e) QWidget::paintEvent(e); QPainter painter(this); - painter.drawPixmap(rect().center() - m_icon.rect().center(), m_icon); + painter.drawPixmap(rect().center() - m_icon.rect().center() / qApp->devicePixelRatio(), m_icon); } void TrashWidget::resizeEvent(QResizeEvent *e) @@ -156,8 +201,8 @@ void TrashWidget::updateIcon() const int size = displayMode == Dock::Fashion ? std::min(width(), height()) * 0.8 : 16; QIcon icon = QIcon::fromTheme(iconString); - m_icon = icon.pixmap(size, size); - + m_icon = icon.pixmap(size * qApp->devicePixelRatio(), size * qApp->devicePixelRatio()); + m_icon.setDevicePixelRatio(qApp->devicePixelRatio()); update(); } @@ -176,11 +221,5 @@ void TrashWidget::moveToTrash(const QUrl &url) { const QFileInfo info = url.toLocalFile(); - QProcess::startDetached("gvfs-trash", QStringList() << "-f" << info.absoluteFilePath()); - -// QDir trashDir(m_popupApplet->trashDir() + "/files"); -// if (!trashDir.exists()) -// trashDir.mkpath("."); - -// QDir().rename(info.absoluteFilePath(), trashDir.absoluteFilePath(info.fileName())); + QProcess::startDetached("gio", QStringList() << "trash" << "-f" << info.absoluteFilePath()); } diff --git a/plugins/trash/trashwidget.h b/plugins/trash/trashwidget.h index 4fbe1975c..eee0b18ac 100644 --- a/plugins/trash/trashwidget.h +++ b/plugins/trash/trashwidget.h @@ -1,9 +1,14 @@ /* * Copyright (C) 2011 ~ 2018 Deepin Technology Co., Ltd. + * 2016 ~ 2018 dragondjf * * Author: sbw + * dragondjf + * zccrs + * Tangtong * - * Maintainer: sbw + * Maintainer: dragondjf + * zccrs * * 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 @@ -48,8 +53,9 @@ signals: void requestContextMenu() const; protected: - void dragEnterEvent(QDragEnterEvent *e); - void dropEvent(QDropEvent *e); + void dragEnterEvent(QDragEnterEvent *e) override; + void dragMoveEvent(QDragMoveEvent *e) override; + void dropEvent(QDropEvent *e) override; void paintEvent(QPaintEvent *e); void resizeEvent(QResizeEvent *e); void mousePressEvent(QMouseEvent *e);