Paste P332

[Dolphin/PlacesPanel] Add menu action to change partition label
ActivePublic

Authored by chinmoyr on Feb 24 2019, 1:49 PM.
diff --git a/src/panels/places/placesitemmodel.cpp b/src/panels/places/placesitemmodel.cpp
index 7513a25d5..91cafea52 100644
--- a/src/panels/places/placesitemmodel.cpp
+++ b/src/panels/places/placesitemmodel.cpp
@@ -36,12 +36,18 @@
#include <KUrlMimeData>
#include <Solid/DeviceNotifier>
#include <Solid/OpticalDrive>
+#include <Solid/Block>
#include <QAction>
#include <QIcon>
#include <QMimeData>
#include <QTimer>
+#include <QInputDialog>
+#include <QDBusMessage>
+#include <QDBusConnection>
+#include <QDBusPendingCallWatcher>
+
namespace {
static QList<QUrl> balooURLs = {
QUrl(QStringLiteral("timeline:/today")),
@@ -249,6 +255,16 @@ QAction* PlacesItemModel::teardownAction(int index) const
return new QAction(QIcon::fromTheme(iconName), text, nullptr);
}
+QAction* PlacesItemModel::changeLabelAction(int index) const
+{
+ const PlacesItem* item = placesItem(index);
+ if (item && item->device().is<Solid::StorageVolume>() && item->device().as<Solid::StorageVolume>()->usage() == Solid::StorageVolume::FileSystem) {
+ return new QAction(QIcon::fromTheme(QStringLiteral("edit-entry")), i18nc("@item", "Change Label"), nullptr);
+ }
+
+ return nullptr;
+}
+
void PlacesItemModel::requestEject(int index)
{
const PlacesItem* item = placesItem(index);
@@ -283,6 +299,42 @@ void PlacesItemModel::requestTearDown(int index)
}
}
+void PlacesItemModel::requestChangeLabel(int index)
+{
+ PlacesItem* item = placesItem(index);
+ if (item) {
+ Solid::Block *blk = item->device().as<Solid::Block>();
+ if (blk) {
+ bool ok;
+ const QString oldLabel = item->device().as<Solid::StorageVolume>()->label();
+ const QString newLabel = QInputDialog::getText(nullptr, i18n("Enter filesystem label"),
+ i18n("Label:"), QLineEdit::Normal,
+ oldLabel, &ok);
+
+ if (newLabel == oldLabel || !ok || newLabel.isEmpty()) {
+ return;
+ }
+
+ const QString devFile = blk->device().split('/').last();
+ QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.UDisks2"),
+ QStringLiteral("/org/freedesktop/UDisks2/block_devices/%1").arg(devFile),
+ QStringLiteral("org.freedesktop.UDisks2.Filesystem"),
+ QStringLiteral("SetLabel"));
+ QVariantList argList;
+ argList << newLabel << QVariantMap();
+ msg.setArguments(argList);
+ QDBusPendingCall async = QDBusConnection::systemBus().asyncCall(msg);
+ QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(async, this);
+ connect(watcher, &QDBusPendingCallWatcher::finished,
+ this, [this, item, newLabel](QDBusPendingCallWatcher* watcher) {
+ item->setText(newLabel);
+ refresh();
+ watcher->deleteLater();
+ });
+ }
+ }
+}
+
bool PlacesItemModel::storageSetupNeeded(int index) const
{
const PlacesItem* item = placesItem(index);
diff --git a/src/panels/places/placesitemmodel.h b/src/panels/places/placesitemmodel.h
index dde3f3d7b..841e54cf1 100644
--- a/src/panels/places/placesitemmodel.h
+++ b/src/panels/places/placesitemmodel.h
@@ -91,9 +91,11 @@ public:
QAction* ejectAction(int index) const;
QAction* teardownAction(int index) const;
+ QAction* changeLabelAction(int index) const;
void requestEject(int index);
void requestTearDown(int index);
+ void requestChangeLabel(int index);
bool storageSetupNeeded(int index) const;
void requestStorageSetup(int index);
diff --git a/src/panels/places/placespanel.cpp b/src/panels/places/placespanel.cpp
index 93fba7168..f9c5f8c98 100644
--- a/src/panels/places/placespanel.cpp
+++ b/src/panels/places/placespanel.cpp
@@ -172,6 +172,7 @@ void PlacesPanel::slotItemContextMenuRequested(int index, const QPointF& pos)
QAction* teardownAction = nullptr;
QAction* ejectAction = nullptr;
QAction* mountAction = nullptr;
+ QAction* changeLabelAction = nullptr;
const bool isDevice = !item->udi().isEmpty();
const bool isTrash = (item->url().scheme() == QLatin1String("trash"));
@@ -198,11 +199,17 @@ void PlacesPanel::slotItemContextMenuRequested(int index, const QPointF& pos)
menu.addAction(teardownAction);
}
+ changeLabelAction = m_model->changeLabelAction(index);
+ if (changeLabelAction) {
+ changeLabelAction->setParent(&menu);
+ menu.addAction(changeLabelAction);
+ }
+
if (item->storageSetupNeeded()) {
mountAction = menu.addAction(QIcon::fromTheme(QStringLiteral("media-mount")), i18nc("@action:inmenu", "Mount"));
}
- if (teardownAction || ejectAction || mountAction) {
+ if (teardownAction || ejectAction || mountAction || changeLabelAction) {
menu.addSeparator();
}
} else {
@@ -273,6 +280,8 @@ void PlacesPanel::slotItemContextMenuRequested(int index, const QPointF& pos)
m_model->requestTearDown(index);
} else if (action == ejectAction) {
m_model->requestEject(index);
+ } else if (action == changeLabelAction) {
+ m_model->requestChangeLabel(index);
} else if (action == propertiesAction) {
KPropertiesDialog* dialog = new KPropertiesDialog(item->url(), this);
dialog->setAttribute(Qt::WA_DeleteOnClose);
chinmoyr created this paste.Feb 24 2019, 1:49 PM
chinmoyr created this object in space S1 KDE Community.
chinmoyr edited the content of this paste. (Show Details)