Skip to content

Commit 685d695

Browse files
author
Alexandr Makarik
committed
[systemsettings] Ability to rename SD card and partitions. JB#54905
1 parent cdd4a33 commit 685d695

10 files changed

+118
-8
lines changed

src/partition.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ class SYSTEMSETTINGS_EXPORT Partition
7373
Unlocking,
7474
Unlocked,
7575
Locking,
76-
Locked
76+
Locked,
77+
Renamed
7778
};
7879

7980
Q_DECLARE_FLAGS(StorageTypes, StorageType)

src/partitionmanager.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ PartitionManagerPrivate::PartitionManagerPrivate()
6666
connect(m_udisksMonitor.data(), &UDisks2::Monitor::mountError, this, &PartitionManagerPrivate::mountError);
6767
connect(m_udisksMonitor.data(), &UDisks2::Monitor::unmountError, this, &PartitionManagerPrivate::unmountError);
6868
connect(m_udisksMonitor.data(), &UDisks2::Monitor::formatError, this, &PartitionManagerPrivate::formatError);
69+
connect(m_udisksMonitor.data(), &UDisks2::Monitor::setLabelError, this, &PartitionManagerPrivate::setLabelError);
6970
connect(UDisks2::BlockDevices::instance(), &UDisks2::BlockDevices::externalStoragesPopulated,
7071
this, &PartitionManagerPrivate::externalStoragesPopulatedChanged);
7172

@@ -344,6 +345,16 @@ void PartitionManagerPrivate::unmount(const Partition &partition)
344345
}
345346
}
346347

348+
void PartitionManagerPrivate::setLabel(const Partition &partition, const QString &label)
349+
{
350+
qCInfo(lcMemoryCardLog) << "Can setLabel:" << externalMedia.match(partition.deviceName()).hasMatch() << partition.devicePath();
351+
if (externalMedia.match(partition.deviceName()).hasMatch()) {
352+
m_udisksMonitor->setLabel(partition.devicePath(), label);
353+
} else {
354+
qCWarning(lcMemoryCardLog) << "setLabel allowed only for external memory cards," << partition.devicePath() << "is not allowed";
355+
}
356+
}
357+
347358
void PartitionManagerPrivate::format(const QString &devicePath, const QString &filesystemType, const QVariantMap &arguments)
348359
{
349360
QString deviceName = devicePath.section(QChar('/'), 2);

src/partitionmanager_p.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class PartitionManagerPrivate : public QObject, public QSharedData
7171
void mount(const Partition &partition);
7272
void unmount(const Partition &partition);
7373
void format(const QString &devicePath, const QString &filesystemType, const QVariantMap &arguments);
74+
void setLabel(const Partition &partition, const QString &label);
7475

7576
QString objectPath(const QString &devicePath) const;
7677

@@ -90,6 +91,7 @@ class PartitionManagerPrivate : public QObject, public QSharedData
9091
void mountError(Partition::Error error);
9192
void unmountError(Partition::Error error);
9293
void formatError(Partition::Error error);
94+
void setLabelError(Partition::Error error);
9395

9496
private:
9597
// TODO: This is leaking (Disks2::Monitor is never free'ed).

src/partitionmodel.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ PartitionModel::PartitionModel(QObject *parent)
6969
connect(m_manager.data(), &PartitionManagerPrivate::formatError, this, [this](Partition::Error error) {
7070
emit formatError(static_cast<PartitionModel::Error>(error));
7171
});
72+
connect(m_manager.data(), &PartitionManagerPrivate::setLabelError, this, [this](Partition::Error error) {
73+
emit setLabelError(static_cast<PartitionModel::Error>(error));
74+
});
7275
}
7376

7477
PartitionModel::~PartitionModel()
@@ -162,6 +165,16 @@ void PartitionModel::unmount(const QString &devicePath)
162165
}
163166
}
164167

168+
void PartitionModel::setLabel(const QString &devicePath, const QString &label)
169+
{
170+
qCInfo(lcMemoryCardLog) << Q_FUNC_INFO << devicePath << m_partitions.count();
171+
if (const Partition *partition = getPartition(devicePath)) {
172+
m_manager->setLabel(*partition, label);
173+
} else {
174+
qCWarning(lcMemoryCardLog) << "Unable to rename unknown device:" << devicePath;
175+
}
176+
}
177+
165178
void PartitionModel::format(const QString &devicePath, const QVariantMap &arguments)
166179
{
167180
QString filesystemType = arguments.value(QLatin1String("filesystemType"), QString()).toString();

src/partitionmodel.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ class SYSTEMSETTINGS_EXPORT PartitionModel : public QAbstractListModel
143143
Q_INVOKABLE void mount(const QString &devicePath);
144144
Q_INVOKABLE void unmount(const QString &devicePath);
145145
Q_INVOKABLE void format(const QString &devicePath, const QVariantMap &arguments);
146+
Q_INVOKABLE void setLabel(const QString &devicePath, const QString &label);
146147

147148
Q_INVOKABLE QString objectPath(const QString &devicePath) const;
148149

@@ -162,6 +163,7 @@ class SYSTEMSETTINGS_EXPORT PartitionModel : public QAbstractListModel
162163
void mountError(Error error);
163164
void unmountError(Error error);
164165
void formatError(Error error);
166+
void setLabelError(Error error);
165167

166168
private:
167169
void update();

src/udisks2defines.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,25 +64,27 @@ Q_DECLARE_METATYPE(UDisks2::InterfacePropertyMap)
6464
#define UDISKS2_JOB_INTERFACE QLatin1String("org.freedesktop.UDisks2.Job")
6565

6666
// Jobs
67-
#define UDISKS2_JOB_OP_ENC_LOCK QLatin1String("encrypted-lock")
68-
#define UDISKS2_JOB_OP_ENC_UNLOCK QLatin1String("encrypted-unlock")
69-
#define UDISKS2_JOB_OP_FS_UNMOUNT QLatin1String("filesystem-unmount")
70-
#define UDISKS2_JOB_OP_FS_MOUNT QLatin1String("filesystem-mount")
71-
#define UDISKS2_JOB_OP_CLEANUP QLatin1String("cleanup")
72-
#define UDISKS2_JOB_OF_FS_FORMAT QLatin1String("format-mkfs")
67+
#define UDISKS2_JOB_OP_ENC_LOCK QLatin1String("encrypted-lock")
68+
#define UDISKS2_JOB_OP_ENC_UNLOCK QLatin1String("encrypted-unlock")
69+
#define UDISKS2_JOB_OP_FS_UNMOUNT QLatin1String("filesystem-unmount")
70+
#define UDISKS2_JOB_OP_FS_MOUNT QLatin1String("filesystem-mount")
71+
#define UDISKS2_JOB_OP_FS_SETLABEL QLatin1String("filesystem-setlabel")
72+
#define UDISKS2_JOB_OP_CLEANUP QLatin1String("cleanup")
73+
#define UDISKS2_JOB_OF_FS_FORMAT QLatin1String("format-mkfs")
7374

7475
// Job keys
7576
#define UDISKS2_JOB_KEY_OPERATION QLatin1String("Operation")
7677
#define UDISKS2_JOB_KEY_OBJECTS QLatin1String("Objects")
7778

78-
// Lock, Unlock, Mount, Unmount, Format
79+
// Lock, Unlock, Mount, Unmount, Format, SetLabel
7980
#define UDISKS2_BLOCK_DEVICE_PATH QString(QLatin1String("/org/freedesktop/UDisks2/block_devices/%1"))
8081
#define UDISKS2_BLOCK_FORMAT QLatin1String("Format")
8182
#define UDISKS2_ENCRYPTED_LOCK QLatin1String("Lock")
8283
#define UDISKS2_ENCRYPTED_UNLOCK QLatin1String("Unlock")
8384
#define UDISKS2_FILESYSTEM_MOUNT QLatin1String("Mount")
8485
#define UDISKS2_FILESYSTEM_UNMOUNT QLatin1String("Unmount")
8586
#define UDISKS2_BLOCK_RESCAN QLatin1String("Rescan")
87+
#define UDISKS2_BLOCK_SETLABEL QLatin1String("SetLabel")
8688

8789
// Errors
8890
#define UDISKS2_ERROR_DEVICE_BUSY QLatin1String("org.freedesktop.UDisks2.Error.DeviceBusy")

src/udisks2job.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ UDisks2::Job::Operation UDisks2::Job::operation() const
132132
return Lock;
133133
} else if (operation == UDISKS2_JOB_OP_ENC_UNLOCK) {
134134
return Unlock;
135+
} else if (operation == UDISKS2_JOB_OP_FS_SETLABEL) {
136+
return SetLabel;
135137
} else {
136138
return Unknown;
137139
}

src/udisks2job_p.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class Job : public QObject
5858
Mount,
5959
Unmount,
6060
Format,
61+
SetLabel,
6162
Unknown
6263
};
6364
Q_ENUM(Operation)

src/udisks2monitor.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,41 @@ void UDisks2::Monitor::unmount(const QString &devicePath)
203203
startMountOperation(devicePath, UDISKS2_FILESYSTEM_UNMOUNT, m_blockDevices->objectPath(devicePath), arguments);
204204
}
205205

206+
void UDisks2::Monitor::setLabel(const QString &devicePath, const QString &label)
207+
{
208+
if (m_blockDevices->find(devicePath)) {
209+
QVariantList args;
210+
QVariantMap opts;
211+
args << opts;
212+
213+
const QString dbusMethod = UDISKS2_FILESYSTEM_UNMOUNT;
214+
215+
QDBusInterface udisks2Interface(UDISKS2_SERVICE,
216+
m_blockDevices->objectPath(devicePath),
217+
UDISKS2_FILESYSTEM_INTERFACE,
218+
QDBusConnection::systemBus());
219+
220+
QDBusPendingCall pendingCall = udisks2Interface.asyncCallWithArgumentList(dbusMethod, args);
221+
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pendingCall, this);
222+
connect(watcher, &QDBusPendingCallWatcher::finished,
223+
this, [this, devicePath, label](QDBusPendingCallWatcher *watcher) {
224+
if (watcher->isValid() && watcher->isFinished()) {
225+
QVariantList arguments;
226+
arguments << label;
227+
QVariantMap options;
228+
arguments << options;
229+
230+
doSetLabel(devicePath, m_blockDevices->objectPath(devicePath), arguments);
231+
}
232+
watcher->deleteLater();
233+
});
234+
235+
}
236+
else {
237+
qCWarning(lcMemoryCardLog) << "Block device" << devicePath << "not found";
238+
}
239+
}
240+
206241
void UDisks2::Monitor::format(const QString &devicePath, const QString &filesystemType, const QVariantMap &arguments)
207242
{
208243
if (devicePath.isEmpty()) {
@@ -651,6 +686,44 @@ void UDisks2::Monitor::doFormat(const QString &devicePath, const QString &dbusOb
651686
});
652687
}
653688

689+
void UDisks2::Monitor::doSetLabel(const QString &devicePath, const QString &dbusObjectPath, const QVariantList &arguments)
690+
{
691+
const QString dbusMethod = UDISKS2_BLOCK_SETLABEL;
692+
if (devicePath.isEmpty()) {
693+
qCCritical(lcMemoryCardLog) << "Cannot" << dbusMethod.toLower() << "without device name";
694+
return;
695+
}
696+
697+
QDBusInterface udisks2Interface(UDISKS2_SERVICE,
698+
dbusObjectPath,
699+
UDISKS2_FILESYSTEM_INTERFACE,
700+
QDBusConnection::systemBus());
701+
702+
QDBusPendingCall pendingCall = udisks2Interface.asyncCallWithArgumentList(dbusMethod, arguments);
703+
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pendingCall, this);
704+
connect(watcher, &QDBusPendingCallWatcher::finished,
705+
this, [this, devicePath, dbusMethod](QDBusPendingCallWatcher *watcher) {
706+
if (watcher->isValid() && watcher->isFinished()) {
707+
emit status(devicePath, Partition::Renamed);
708+
mount(devicePath);
709+
} else if (watcher->isError()) {
710+
QDBusError error = watcher->error();
711+
QByteArray errorData = error.name().toLocal8Bit();
712+
const char *errorCStr = errorData.constData();
713+
qCWarning(lcMemoryCardLog) << dbusMethod << "error:" << errorCStr;
714+
715+
for (uint i = 0; i < sizeof(dbus_error_entries) / sizeof(ErrorEntry); i++) {
716+
if (strcmp(dbus_error_entries[i].dbusErrorName, errorCStr) == 0) {
717+
emit setLabelError(dbus_error_entries[i].errorCode);
718+
break;
719+
}
720+
}
721+
}
722+
723+
watcher->deleteLater();
724+
});
725+
}
726+
654727
void UDisks2::Monitor::getBlockDevices()
655728
{
656729
QDBusInterface managerInterface(UDISKS2_SERVICE,

src/udisks2monitor_p.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ class Monitor : public QObject
8686
void unmount(const QString &devicePath);
8787

8888
void format(const QString &devicePath, const QString &filesystemType, const QVariantMap &arguments);
89+
void setLabel(const QString &devicePath, const QString &label);
8990

9091
signals:
9192
void status(const QString &devicePath, Partition::Status);
@@ -95,12 +96,14 @@ class Monitor : public QObject
9596
void mountError(Partition::Error error);
9697
void unmountError(Partition::Error error);
9798
void formatError(Partition::Error error);
99+
void setLabelError(Partition::Error error);
98100

99101
private slots:
100102
void interfacesAdded(const QDBusObjectPath &objectPath, const UDisks2::InterfacePropertyMap &interfaces);
101103
void interfacesRemoved(const QDBusObjectPath &objectPath, const QStringList &interfaces);
102104
void doFormat(const QString &devicePath, const QString &dbusObjectPath, const QString &filesystemType, const QVariantMap &arguments);
103105
void handleNewBlock(UDisks2::Block *block);
106+
void doSetLabel(const QString &devicePath, const QString &dbusObjectPath, const QVariantList &arguments);
104107

105108
private:
106109
void setPartitionProperties(QExplicitlySharedDataPointer<PartitionPrivate> &partition, const Block *blockDevice);

0 commit comments

Comments
 (0)