Commit a41d3438 authored by Harald Sitter's avatar Harald Sitter
Browse files

mtp: kdirnotify on device changes

this causes the kdirmodel in the UIs to reload when devices change and
thus pick up changes without having to hit f5

removal is on a 5s delay because removing causes the models to switch
out of the directory, so we must only emit removal after we are sure the
device disappeared rather than temporarily getting reconnected with
storages
parent 4726f891
Pipeline #118927 passed with stage
in 1 minute and 51 seconds
......@@ -15,6 +15,7 @@ target_link_libraries(kmtpd
KF5::CoreAddons
KF5::DBusAddons
KF5::Solid
KF5::KIOCore
Qt5::Core
Libmtp::Libmtp
)
......
......@@ -2,15 +2,19 @@
This file is part of the MTP KIOD module, part of the KDE project.
SPDX-FileCopyrightText: 2018 Andreas Krutzler <andreas.krutzler@gmx.net>
SPDX-FileCopyrightText: 2022 Harald Sitter <sitter@kde.org>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "kmtpd.h"
#include <chrono>
#include <QDBusConnection>
#include <QDebug>
#include <KDirNotify>
#include <KPluginFactory>
#include <Solid/GenericInterface>
#include <Solid/DeviceNotifier>
......@@ -19,6 +23,8 @@
#include "kiod_kmtpd_debug.h"
#include "mtpdevice.h"
using namespace std::chrono_literals;
K_PLUGIN_CLASS_WITH_JSON(KMTPd, "kmtpd.json")
KMTPd::KMTPd(QObject *parent, const QList<QVariant> &parameters)
......@@ -100,7 +106,9 @@ void KMTPd::checkDevice(const Solid::Device &solidDevice)
// Always let MTPDevice know any changes to devices. It might be helpful Daemon interfaces
connect(this, &KMTPd::devicesChanged, device, [device] {
device->setDevicesUpdatedStatus(true);
org::kde::KDirNotify::emitFilesAdded(device->url());
});
org::kde::KDirNotify::emitFilesAdded(device->url()); // notify for the current change as well
m_devices.append(device);
Q_EMIT devicesChanged();
......@@ -147,6 +155,7 @@ void KMTPd::deviceAdded(const QString &udi)
if (device.isDeviceInterface(Solid::DeviceInterface::PortableMediaPlayer)) {
qCDebug(LOG_KIOD_KMTPD) << "SOLID: New Device with udi=" << udi;
org::kde::KDirNotify::emitFilesAdded(QUrl(QStringLiteral("mtp:/")));
checkDevice(device);
}
}
......@@ -157,6 +166,19 @@ void KMTPd::deviceRemoved(const QString &udi)
if (device) {
qCDebug(LOG_KIOD_KMTPD) << "SOLID: Device with udi=" << udi << " removed.";
const QUrl url = device->url();
// When allowing access on the Device side the device is briefly removed and then added again.
// If we were to signal removal right away that would lead the KDirModel to leave the directory as it thinks
// the dir no longer exists. To mitigate we delay the removal and if it was a real removal, rather than a
// temporary remove-add dance, we'll emit it.
QTimer::singleShot(5s, this, [this, udi, url] {
if (!deviceFromUdi(udi)) {
qCDebug(LOG_KIOD_KMTPD) << "executing scheduled removal of " << udi;
org::kde::KDirNotify::emitFilesRemoved({url});
}
});
m_devices.removeOne(device);
delete device;
Q_EMIT devicesChanged();
......
......@@ -2,6 +2,7 @@
This file is part of the MTP KIOD module, part of the KDE project.
SPDX-FileCopyrightText: 2018 Andreas Krutzler <andreas.krutzler@gmx.net>
SPDX-FileCopyrightText: 2022 Harald Sitter <sitter@kde.org>
SPDX-License-Identifier: GPL-2.0-or-later
*/
......@@ -123,4 +124,12 @@ QList<QDBusObjectPath> MTPDevice::listStorages()
return list;
}
QUrl MTPDevice::url() const
{
QUrl friendlyUrl;
friendlyUrl.setScheme(QStringLiteral("mtp"));
friendlyUrl.setPath(QLatin1Char('/') + friendlyName());
return friendlyUrl;
}
#include "moc_mtpdevice.cpp"
......@@ -2,6 +2,7 @@
This file is part of the MTP KIOD module, part of the KDE project.
SPDX-FileCopyrightText: 2018 Andreas Krutzler <andreas.krutzler@gmx.net>
SPDX-FileCopyrightText: 2022 Harald Sitter <sitter@kde.org>
SPDX-License-Identifier: GPL-2.0-or-later
*/
......@@ -43,6 +44,9 @@ public:
bool devicesUpdated() const;
void setDevicesUpdatedStatus(bool value);
/// The friendly name url of this device.
QUrl url() const;
private:
const QString m_dbusObjectName;
QList<MTPStorage *> m_storages;
......
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