2023-02-16 13:51:55 +08:00
|
|
|
|
// Copyright (C) 2011 ~ 2018 Deepin Technology Co., Ltd.
|
|
|
|
|
// SPDX-FileCopyrightText: 2018 - 2023 UnionTech Software Technology Co., Ltd.
|
2022-09-06 11:36:55 +08:00
|
|
|
|
//
|
|
|
|
|
// SPDX-License-Identifier: LGPL-3.0-or-later
|
2016-08-08 16:28:37 +08:00
|
|
|
|
|
|
|
|
|
#include "constants.h"
|
2016-08-01 10:58:10 +08:00
|
|
|
|
#include "trashwidget.h"
|
2023-03-08 11:56:28 +08:00
|
|
|
|
#include "imageutil.h"
|
2016-08-01 10:58:10 +08:00
|
|
|
|
|
2022-11-04 06:29:03 +00:00
|
|
|
|
#include <DDBusSender>
|
|
|
|
|
|
2016-08-01 10:58:10 +08:00
|
|
|
|
#include <QPainter>
|
|
|
|
|
#include <QIcon>
|
2016-08-08 16:28:37 +08:00
|
|
|
|
#include <QApplication>
|
2016-08-08 16:53:18 +08:00
|
|
|
|
#include <QDragEnterEvent>
|
2016-09-19 15:06:25 +08:00
|
|
|
|
#include <QJsonDocument>
|
2018-09-19 10:56:17 +08:00
|
|
|
|
#include <QApplication>
|
2021-08-20 10:19:10 +08:00
|
|
|
|
#include <QDBusConnection>
|
2016-08-01 10:58:10 +08:00
|
|
|
|
|
|
|
|
|
TrashWidget::TrashWidget(QWidget *parent)
|
2019-09-06 15:28:22 +08:00
|
|
|
|
: QWidget(parent)
|
|
|
|
|
, m_popupApplet(new PopupControlWidget(this))
|
2021-08-20 10:19:10 +08:00
|
|
|
|
, m_fileManagerInter(new DBusFileManager1("org.freedesktop.FileManager1",
|
|
|
|
|
"/org/freedesktop/FileManager1",
|
|
|
|
|
QDBusConnection::sessionBus(),
|
|
|
|
|
this))
|
2021-09-03 13:31:32 +08:00
|
|
|
|
, m_dragging(false)
|
2016-08-01 10:58:10 +08:00
|
|
|
|
{
|
2016-08-01 15:20:44 +08:00
|
|
|
|
m_popupApplet->setVisible(false);
|
2016-08-08 16:28:37 +08:00
|
|
|
|
|
2019-09-06 15:28:22 +08:00
|
|
|
|
connect(m_popupApplet, &PopupControlWidget::emptyChanged, this, &TrashWidget::updateIconAndRefresh);
|
2016-08-08 16:28:37 +08:00
|
|
|
|
|
2016-08-08 16:53:18 +08:00
|
|
|
|
setAcceptDrops(true);
|
2019-09-06 14:44:07 +08:00
|
|
|
|
|
2019-10-22 14:53:30 +08:00
|
|
|
|
m_defaulticon = QIcon::fromTheme(":/icons/user-trash.svg");
|
2019-10-31 15:38:56 +08:00
|
|
|
|
|
|
|
|
|
setMinimumSize(PLUGIN_ICON_MIN_SIZE, PLUGIN_ICON_MIN_SIZE);
|
2016-08-01 15:20:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QWidget *TrashWidget::popupApplet()
|
|
|
|
|
{
|
|
|
|
|
return m_popupApplet;
|
2016-08-01 10:58:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-19 15:06:25 +08:00
|
|
|
|
const QString TrashWidget::contextMenu() const
|
|
|
|
|
{
|
|
|
|
|
QList<QVariant> items;
|
|
|
|
|
items.reserve(2);
|
|
|
|
|
|
|
|
|
|
QMap<QString, QVariant> open;
|
|
|
|
|
open["itemId"] = "open";
|
|
|
|
|
open["itemText"] = tr("Open");
|
|
|
|
|
open["isActive"] = true;
|
|
|
|
|
items.push_back(open);
|
|
|
|
|
|
2019-08-22 10:44:04 +08:00
|
|
|
|
if (!m_popupApplet->empty()) {
|
2016-09-19 15:06:25 +08:00
|
|
|
|
QMap<QString, QVariant> empty;
|
|
|
|
|
empty["itemId"] = "empty";
|
|
|
|
|
empty["itemText"] = tr("Empty");
|
|
|
|
|
empty["isActive"] = true;
|
|
|
|
|
items.push_back(empty);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QMap<QString, QVariant> menu;
|
|
|
|
|
menu["items"] = items;
|
|
|
|
|
menu["checkableMenu"] = false;
|
|
|
|
|
menu["singleCheck"] = false;
|
|
|
|
|
|
|
|
|
|
return QJsonDocument::fromVariant(menu).toJson();
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-20 14:42:22 +08:00
|
|
|
|
int TrashWidget::trashItemCount() const
|
|
|
|
|
{
|
|
|
|
|
return m_popupApplet->trashItems();
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-19 15:06:25 +08:00
|
|
|
|
void TrashWidget::invokeMenuItem(const QString &menuId, const bool checked)
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(checked);
|
|
|
|
|
|
|
|
|
|
if (menuId == "open")
|
|
|
|
|
m_popupApplet->openTrashFloder();
|
|
|
|
|
else if (menuId == "empty")
|
|
|
|
|
m_popupApplet->clearTrashFloder();
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-08 16:53:18 +08:00
|
|
|
|
void TrashWidget::dragEnterEvent(QDragEnterEvent *e)
|
|
|
|
|
{
|
2018-09-19 10:56:17 +08:00
|
|
|
|
if (e->mimeData()->hasFormat("RequestDock")) {
|
2023-02-21 15:49:36 +02:00
|
|
|
|
// accept prevent the event from being propagated to the dock main panel
|
2018-09-19 10:56:17 +08:00
|
|
|
|
// which also takes drag event;
|
|
|
|
|
|
|
|
|
|
if (!e->mimeData()->hasFormat("Removable")) {
|
2023-02-21 15:49:36 +02:00
|
|
|
|
// show the forbid dropping cursor.
|
2018-09-19 10:56:17 +08:00
|
|
|
|
e->setDropAction(Qt::IgnoreAction);
|
2020-03-07 16:36:03 +08:00
|
|
|
|
} else {
|
|
|
|
|
e->setDropAction(Qt::MoveAction);
|
|
|
|
|
e->accept();
|
2018-09-19 10:56:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-07 16:36:03 +08:00
|
|
|
|
if (!e->mimeData()->hasUrls())
|
|
|
|
|
return e->ignore();
|
|
|
|
|
|
2018-09-19 10:56:17 +08:00
|
|
|
|
e->setDropAction(Qt::MoveAction);
|
|
|
|
|
|
|
|
|
|
if (e->dropAction() != Qt::MoveAction) {
|
|
|
|
|
e->ignore();
|
|
|
|
|
} else {
|
2021-09-03 13:31:32 +08:00
|
|
|
|
// 设置item是否拖入回收站的状态,给DockItem发送鼠标进入事件
|
|
|
|
|
setDragging(true);
|
|
|
|
|
qApp->postEvent(this->parent(), new QEnterEvent(e->pos(), mapToParent(e->pos()), mapToGlobal(e->pos())));
|
2018-09-19 10:56:17 +08:00
|
|
|
|
e->accept();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TrashWidget::dragMoveEvent(QDragMoveEvent *e)
|
|
|
|
|
{
|
|
|
|
|
if (!e->mimeData()->hasUrls())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
e->setDropAction(Qt::MoveAction);
|
2016-08-11 16:03:45 +08:00
|
|
|
|
|
2018-09-19 10:56:17 +08:00
|
|
|
|
if (e->dropAction() != Qt::MoveAction) {
|
|
|
|
|
e->ignore();
|
|
|
|
|
} else {
|
|
|
|
|
e->accept();
|
|
|
|
|
}
|
2016-08-08 16:53:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-03 13:31:32 +08:00
|
|
|
|
void TrashWidget::dragLeaveEvent(QDragLeaveEvent *e)
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(e);
|
|
|
|
|
|
|
|
|
|
// 设置item是否拖入回收站的状态,给DockItem发送鼠标离开事件
|
|
|
|
|
setDragging(false);
|
|
|
|
|
qApp->postEvent(this->parent(), new QEvent(QEvent::Leave));
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-08 16:53:18 +08:00
|
|
|
|
void TrashWidget::dropEvent(QDropEvent *e)
|
|
|
|
|
{
|
2016-08-11 16:03:45 +08:00
|
|
|
|
if (e->mimeData()->hasFormat("RequestDock"))
|
2022-11-17 14:03:17 +08:00
|
|
|
|
return removeApp(e->mimeData()->data("DesktopPath"));
|
2016-08-08 16:53:18 +08:00
|
|
|
|
|
2018-09-19 10:56:17 +08:00
|
|
|
|
if (!e->mimeData()->hasUrls()) {
|
|
|
|
|
return e->ignore();
|
2016-08-11 16:03:45 +08:00
|
|
|
|
}
|
2018-09-19 10:56:17 +08:00
|
|
|
|
|
|
|
|
|
e->setDropAction(Qt::MoveAction);
|
|
|
|
|
|
|
|
|
|
if (e->dropAction() != Qt::MoveAction) {
|
|
|
|
|
return e->ignore();
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-03 13:31:32 +08:00
|
|
|
|
// 设置item是否拖入回收站的状态,给DockItem发送鼠标离开事件
|
|
|
|
|
setDragging(false);
|
|
|
|
|
qApp->postEvent(this->parent(), new QEvent(QEvent::Leave));
|
|
|
|
|
|
2018-09-19 10:56:17 +08:00
|
|
|
|
const QMimeData *mime = e->mimeData();
|
|
|
|
|
for (auto url : mime->urls())
|
|
|
|
|
moveToTrash(url);
|
2016-08-08 16:53:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-01 10:58:10 +08:00
|
|
|
|
void TrashWidget::paintEvent(QPaintEvent *e)
|
|
|
|
|
{
|
|
|
|
|
QWidget::paintEvent(e);
|
|
|
|
|
|
2019-09-06 15:28:22 +08:00
|
|
|
|
updateIcon();
|
|
|
|
|
|
2019-10-22 14:53:30 +08:00
|
|
|
|
QPainter painter(this);
|
2019-09-28 17:23:10 +08:00
|
|
|
|
const QRectF &rf = QRectF(rect());
|
|
|
|
|
const QRectF &rfp = QRectF(m_icon.rect());
|
|
|
|
|
painter.drawPixmap(rf.center() - rfp.center() / devicePixelRatioF(), m_icon);
|
2016-08-08 16:28:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TrashWidget::updateIcon()
|
|
|
|
|
{
|
2023-03-08 11:56:28 +08:00
|
|
|
|
Dock::DisplayMode displayMode = qApp->property(PROP_DISPLAY_MODE).value<Dock::DisplayMode>();
|
2016-08-09 14:51:03 +08:00
|
|
|
|
|
2016-08-08 16:28:37 +08:00
|
|
|
|
QString iconString = "user-trash";
|
|
|
|
|
if (!m_popupApplet->empty())
|
|
|
|
|
iconString.append("-full");
|
2019-10-31 15:38:56 +08:00
|
|
|
|
|
2023-03-08 11:56:28 +08:00
|
|
|
|
int size = qMin(width(), height());
|
|
|
|
|
size = qMax(PLUGIN_ICON_MIN_SIZE, static_cast<int>(size * ((Dock::Efficient == displayMode) ? 0.7 : 0.8)));
|
2019-09-06 15:28:22 +08:00
|
|
|
|
|
2023-03-08 11:56:28 +08:00
|
|
|
|
m_icon = ImageUtil::loadSvg(iconString, QSize(size, size), devicePixelRatioF());
|
2019-09-06 15:28:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TrashWidget::updateIconAndRefresh()
|
|
|
|
|
{
|
|
|
|
|
updateIcon();
|
2016-08-08 16:28:37 +08:00
|
|
|
|
update();
|
2016-08-01 10:58:10 +08:00
|
|
|
|
}
|
2016-08-08 16:53:18 +08:00
|
|
|
|
|
2021-09-03 13:31:32 +08:00
|
|
|
|
bool TrashWidget::getDragging() const
|
|
|
|
|
{
|
|
|
|
|
return m_dragging;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TrashWidget::setDragging(bool state)
|
|
|
|
|
{
|
|
|
|
|
m_dragging = state;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-11 16:03:45 +08:00
|
|
|
|
void TrashWidget::removeApp(const QString &appKey)
|
|
|
|
|
{
|
2022-11-04 06:29:03 +00:00
|
|
|
|
DDBusSender().service("org.deepin.dde.Launcher1")
|
|
|
|
|
.path("/org/deepin/dde/Launcher1")
|
|
|
|
|
.interface("org.deepin.dde.Launcher1")
|
|
|
|
|
.method("UninstallApp")
|
|
|
|
|
.arg(appKey)
|
|
|
|
|
.call();
|
2016-08-11 16:03:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-08 16:53:18 +08:00
|
|
|
|
void TrashWidget::moveToTrash(const QUrl &url)
|
|
|
|
|
{
|
|
|
|
|
const QFileInfo info = url.toLocalFile();
|
|
|
|
|
|
2021-08-20 10:19:10 +08:00
|
|
|
|
QStringList argumentList;
|
|
|
|
|
argumentList << info.absoluteFilePath();
|
|
|
|
|
|
|
|
|
|
m_fileManagerInter->Trash(argumentList);
|
2016-08-08 16:53:18 +08:00
|
|
|
|
}
|