Skip to content

Commit bfd0866

Browse files
Alexandr Makarikamakarik
Alexandr Makarik
authored andcommitted
[systemsettings] Ability to rename SD card and partitions. JB#54905
1 parent cdd4a33 commit bfd0866

10 files changed

+104
-8
lines changed

src/partition.h

+2-1
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+
Renaming
7778
};
7879

7980
Q_DECLARE_FLAGS(StorageTypes, StorageType)

src/partitionmanager.cpp

+11
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

+2
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

+13
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

+2
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

+9-7
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

+2
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

+1
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

+59
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,26 @@ 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 (devicePath.isEmpty()) {
209+
qCCritical(lcMemoryCardLog) << "Cannot set label without device name";
210+
return;
211+
}
212+
213+
if (m_blockDevices->find(devicePath)) {
214+
QVariantList arguments;
215+
arguments << label;
216+
QVariantMap options;
217+
arguments << options;
218+
219+
doSetLabel(devicePath, m_blockDevices->objectPath(devicePath), arguments);
220+
}
221+
else {
222+
qCWarning(lcMemoryCardLog) << "Block device" << devicePath << "not found";
223+
}
224+
}
225+
206226
void UDisks2::Monitor::format(const QString &devicePath, const QString &filesystemType, const QVariantMap &arguments)
207227
{
208228
if (devicePath.isEmpty()) {
@@ -651,6 +671,45 @@ void UDisks2::Monitor::doFormat(const QString &devicePath, const QString &dbusOb
651671
});
652672
}
653673

674+
void UDisks2::Monitor::doSetLabel(const QString &devicePath, const QString &dbusObjectPath, const QVariantList &arguments)
675+
{
676+
const QString dbusMethod = UDISKS2_BLOCK_SETLABEL;
677+
if (devicePath.isEmpty()) {
678+
qCCritical(lcMemoryCardLog) << "Cannot" << dbusMethod.toLower() << "without device name";
679+
return;
680+
}
681+
682+
QDBusMessage method = QDBusMessage::createMethodCall(
683+
UDISKS2_SERVICE,
684+
dbusObjectPath,
685+
UDISKS2_FILESYSTEM_INTERFACE,
686+
dbusMethod);
687+
method.setArguments(arguments);
688+
QDBusPendingCall pendingCall = QDBusConnection::systemBus().asyncCall(method);
689+
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pendingCall, this);
690+
connect(watcher, &QDBusPendingCallWatcher::finished,
691+
this, [this, devicePath, dbusMethod](QDBusPendingCallWatcher *watcher) {
692+
if (watcher->isError()) {
693+
QDBusError error = watcher->error();
694+
QByteArray errorData = error.name().toLocal8Bit();
695+
const char *errorCStr = errorData.constData();
696+
qCWarning(lcMemoryCardLog) << dbusMethod << "error:" << errorCStr;
697+
698+
for (uint i = 0; i < sizeof(dbus_error_entries) / sizeof(ErrorEntry); i++) {
699+
if (strcmp(dbus_error_entries[i].dbusErrorName, errorCStr) == 0) {
700+
emit setLabelError(dbus_error_entries[i].errorCode);
701+
break;
702+
}
703+
}
704+
}
705+
706+
emit status(devicePath, Partition::Unmounted);
707+
watcher->deleteLater();
708+
});
709+
710+
emit status(devicePath, Partition::Renaming);
711+
}
712+
654713
void UDisks2::Monitor::getBlockDevices()
655714
{
656715
QDBusInterface managerInterface(UDISKS2_SERVICE,

src/udisks2monitor_p.h

+3
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)