Commit c345fd2e authored by James John's avatar James John Committed by Harald Sitter
Browse files

Refresh MTP Storage list

MTP storage needs to be real-time. After storage permission is
allowed, storage count is still empty, causing you to relaunch
Dolphin to be able to access devices.

I have been having this issue for as long as I can remember.
This commit is refreshing device list before querying device list
in case new device information has arrived. This ensures we always
have the latest storage information.
parent f7031de0
Pipeline #118906 passed with stage
in 2 minutes and 14 seconds
......@@ -197,7 +197,7 @@ void MTPSlave::listDir(const QUrl &url)
}
// traverse into device
const KMTPDeviceInterface *mtpDevice = m_kmtpDaemon.deviceFromName(pathItems.first());
KMTPDeviceInterface *mtpDevice = m_kmtpDaemon.deviceFromName(pathItems.first());
if (mtpDevice) {
// list storage media
if (pathItems.size() == 1) {
......
......@@ -96,6 +96,12 @@ void KMTPd::checkDevice(const Solid::Device &solidDevice)
LIBMTP_mtpdevice_t *mtpDevice = LIBMTP_Open_Raw_Device_Uncached(rawDevice);
if (mtpDevice) {
MTPDevice *device = new MTPDevice(QStringLiteral("/modules/kmtpd/device%1").arg(m_devices.count()), mtpDevice, rawDevice, solidDevice.udi(), m_timeout);
// Always let MTPDevice know any changes to devices. It might be helpful Daemon interfaces
connect(this, &KMTPd::devicesChanged, device, [device] {
device->setDevicesUpdatedStatus(true);
});
m_devices.append(device);
Q_EMIT devicesChanged();
} else {
......
......@@ -34,7 +34,8 @@ MTPDevice::MTPDevice(const QString &dbusObjectPath, LIBMTP_mtpdevice_t *device,
m_timeout(timeout),
m_mtpdevice(device),
m_rawdevice(*rawdevice),
m_udi(udi)
m_udi(udi),
m_devicesUpdated(true)
{
const char *deviceName = LIBMTP_Get_Friendlyname(device);
const char *deviceModel = LIBMTP_Get_Modelname(device);
......@@ -63,6 +64,16 @@ MTPDevice::~MTPDevice()
LIBMTP_Release_Device(m_mtpdevice);
}
void MTPDevice::setDevicesUpdatedStatus(bool value)
{
m_devicesUpdated = value;
}
bool MTPDevice::devicesUpdated() const
{
return m_devicesUpdated;
}
LIBMTP_mtpdevice_t *MTPDevice::getDevice()
{
return m_mtpdevice;
......@@ -98,13 +109,17 @@ int MTPDevice::setFriendlyName(const QString &friendlyName)
return result;
}
QList<QDBusObjectPath> MTPDevice::listStorages() const
QList<QDBusObjectPath> MTPDevice::listStorages()
{
QList<QDBusObjectPath> list;
list.reserve(m_storages.count());
for (const MTPStorage *storage : m_storages) {
list.append(QDBusObjectPath(storage->dbusObjectPath()));
}
// New storages have been collected. No new changes anymore.
setDevicesUpdatedStatus(false);
return list;
}
......
......@@ -40,6 +40,9 @@ public:
QString udi() const;
QString friendlyName() const;
bool devicesUpdated() const;
void setDevicesUpdatedStatus(bool value);
private:
const QString m_dbusObjectName;
QList<MTPStorage *> m_storages;
......@@ -50,12 +53,13 @@ private:
QString m_udi;
QString m_friendlyName;
bool m_devicesUpdated;
public Q_SLOTS:
// D-Bus methods
int setFriendlyName(const QString &friendlyName);
QList<QDBusObjectPath> listStorages() const;
QList<QDBusObjectPath> listStorages();
Q_SIGNALS:
void friendlyNameChanged(const QString &friendlyName);
......
......@@ -16,6 +16,13 @@ KMTPDeviceInterface::KMTPDeviceInterface(const QString &dbusObjectPath, QObject
dbusObjectPath,
QDBusConnection::sessionBus(),
this);
updateStorages();
}
void KMTPDeviceInterface::updateStorages()
{
qDeleteAll(m_storages);
m_storages.clear();
const auto storageNames = m_dbusInterface->listStorages().value();
m_storages.reserve(storageNames.count());
......@@ -34,8 +41,13 @@ QString KMTPDeviceInterface::friendlyName() const
return m_dbusInterface->friendlyName();
}
QVector<KMTPStorageInterface *> KMTPDeviceInterface::storages() const
QVector<KMTPStorageInterface *> KMTPDeviceInterface::storages()
{
// Devices may have changed?
if (m_dbusInterface->devicesUpdated()) {
updateStorages();
}
return m_storages;
}
......
......@@ -32,8 +32,9 @@ public:
QString udi() const;
QString friendlyName() const;
QVector<KMTPStorageInterface *> storages() const;
QVector<KMTPStorageInterface *> storages();
KMTPStorageInterface *storageFromDescription(const QString &description) const;
void updateStorages();
private:
org::kde::kmtp::Device *m_dbusInterface;
......
......@@ -44,5 +44,12 @@
<arg direction="out" type="ao" name="storages" />
</method>
<!-- devicesUpdated: Get status if any device was changed.
@deviceUpdated: Status of any device change.
-->
<method name="devicesUpdated">
<arg direction="out" type="b" name="devicesUpdated" />
</method>
</interface>
</node>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment