plugins/sftp: Do not use pimpl for d-ptr

This doesn't make sense, because it is a plugin class and not exported
This commit is contained in:
Alexander Lohnau 2023-08-12 18:43:03 +02:00 committed by Albert Vaca Cintora
parent a1a7e57011
commit c3b4103d0c
2 changed files with 24 additions and 35 deletions

View file

@ -11,7 +11,6 @@
#include <QDir> #include <QDir>
#include <QStandardPaths> #include <QStandardPaths>
#include <KFilePlacesModel>
#include <KIO/OpenUrlJob> #include <KIO/OpenUrlJob>
#include <KLocalizedString> #include <KLocalizedString>
#include <KNotification> #include <KNotification>
@ -23,22 +22,8 @@
K_PLUGIN_CLASS_WITH_JSON(SftpPlugin, "kdeconnect_sftp.json") K_PLUGIN_CLASS_WITH_JSON(SftpPlugin, "kdeconnect_sftp.json")
static const QSet<QString> fields_c = QSet<QString>() << QStringLiteral("user") << QStringLiteral("port") << QStringLiteral("path");
struct SftpPlugin::Pimpl {
Pimpl()
: m_mounter(nullptr)
{
}
// Add KIO entry to Dolphin's Places
KFilePlacesModel m_placesModel;
Mounter *m_mounter;
};
SftpPlugin::SftpPlugin(QObject *parent, const QVariantList &args) SftpPlugin::SftpPlugin(QObject *parent, const QVariantList &args)
: KdeConnectPlugin(parent, args) : KdeConnectPlugin(parent, args)
, d(new Pimpl())
, deviceId(device()->id()) , deviceId(device()->id())
{ {
addToDolphin(); addToDolphin();
@ -56,50 +41,50 @@ void SftpPlugin::addToDolphin()
removeFromDolphin(); removeFromDolphin();
QUrl kioUrl(QStringLiteral("kdeconnect://") + deviceId + QStringLiteral("/")); QUrl kioUrl(QStringLiteral("kdeconnect://") + deviceId + QStringLiteral("/"));
d->m_placesModel.addPlace(device()->name(), kioUrl, QStringLiteral("kdeconnect")); m_placesModel.addPlace(device()->name(), kioUrl, QStringLiteral("kdeconnect"));
qCDebug(KDECONNECT_PLUGIN_SFTP) << "add to dolphin"; qCDebug(KDECONNECT_PLUGIN_SFTP) << "add to dolphin";
} }
void SftpPlugin::removeFromDolphin() void SftpPlugin::removeFromDolphin()
{ {
QUrl kioUrl(QStringLiteral("kdeconnect://") + deviceId + QStringLiteral("/")); QUrl kioUrl(QStringLiteral("kdeconnect://") + deviceId + QStringLiteral("/"));
QModelIndex index = d->m_placesModel.closestItem(kioUrl); QModelIndex index = m_placesModel.closestItem(kioUrl);
while (index.row() != -1) { while (index.row() != -1) {
d->m_placesModel.removePlace(index); m_placesModel.removePlace(index);
index = d->m_placesModel.closestItem(kioUrl); index = m_placesModel.closestItem(kioUrl);
} }
} }
void SftpPlugin::mount() void SftpPlugin::mount()
{ {
qCDebug(KDECONNECT_PLUGIN_SFTP) << "Mount device:" << device()->name(); qCDebug(KDECONNECT_PLUGIN_SFTP) << "Mount device:" << device()->name();
if (d->m_mounter) { if (m_mounter) {
return; return;
} }
d->m_mounter = new Mounter(this); m_mounter = new Mounter(this);
connect(d->m_mounter, &Mounter::mounted, this, &SftpPlugin::onMounted); connect(m_mounter, &Mounter::mounted, this, &SftpPlugin::onMounted);
connect(d->m_mounter, &Mounter::unmounted, this, &SftpPlugin::onUnmounted); connect(m_mounter, &Mounter::unmounted, this, &SftpPlugin::onUnmounted);
connect(d->m_mounter, &Mounter::failed, this, &SftpPlugin::onFailed); connect(m_mounter, &Mounter::failed, this, &SftpPlugin::onFailed);
} }
void SftpPlugin::unmount() void SftpPlugin::unmount()
{ {
if (d->m_mounter) { if (m_mounter) {
d->m_mounter->deleteLater(); m_mounter->deleteLater();
d->m_mounter = nullptr; m_mounter = nullptr;
} }
} }
bool SftpPlugin::mountAndWait() bool SftpPlugin::mountAndWait()
{ {
mount(); mount();
return d->m_mounter->wait(); return m_mounter->wait();
} }
bool SftpPlugin::isMounted() const bool SftpPlugin::isMounted() const
{ {
return d->m_mounter && d->m_mounter->isMounted(); return m_mounter && m_mounter->isMounted();
} }
QString SftpPlugin::getMountError() QString SftpPlugin::getMountError()
@ -123,6 +108,7 @@ bool SftpPlugin::startBrowsing()
void SftpPlugin::receivePacket(const NetworkPacket &np) void SftpPlugin::receivePacket(const NetworkPacket &np)
{ {
static const QSet<QString> fields_c{QStringLiteral("user"), QStringLiteral("port"), QStringLiteral("path")};
const QStringList keysList = np.body().keys(); const QStringList keysList = np.body().keys();
const auto keys = QSet(keysList.begin(), keysList.end()); const auto keys = QSet(keysList.begin(), keysList.end());
if (!(fields_c - keys).isEmpty() && !np.has(QStringLiteral("errorMessage"))) { if (!(fields_c - keys).isEmpty() && !np.has(QStringLiteral("errorMessage"))) {
@ -130,7 +116,7 @@ void SftpPlugin::receivePacket(const NetworkPacket &np)
return; return;
} }
d->m_mounter->onPacketReceived(np); m_mounter->onPacketReceived(np);
remoteDirectories.clear(); remoteDirectories.clear();
if (np.has(QStringLiteral("multiPaths"))) { if (np.has(QStringLiteral("multiPaths"))) {

View file

@ -6,10 +6,12 @@
#pragma once #pragma once
#include <KFilePlacesModel>
#include <core/device.h> #include <core/device.h>
#include <core/kdeconnectplugin.h> #include <core/kdeconnectplugin.h>
#define PACKET_TYPE_SFTP_REQUEST QStringLiteral("kdeconnect.sftp.request") #define PACKET_TYPE_SFTP_REQUEST QStringLiteral("kdeconnect.sftp.request")
class Mounter;
class SftpPlugin : public KdeConnectPlugin class SftpPlugin : public KdeConnectPlugin
{ {
@ -45,13 +47,14 @@ private Q_SLOTS:
void onFailed(const QString &message); void onFailed(const QString &message);
private: private:
struct Pimpl; void knotify(int type, const QString &text, const QPixmap &icon) const;
QScopedPointer<Pimpl> d; void addToDolphin();
void removeFromDolphin();
// Add KIO entry to Dolphin's Places
KFilePlacesModel m_placesModel;
Mounter *m_mounter;
QString deviceId; // Storing it to avoid accessing device() from the destructor which could cause a crash QString deviceId; // Storing it to avoid accessing device() from the destructor which could cause a crash
QVariantMap remoteDirectories; // Actually a QMap<String, String>, but QDBus prefers this QVariantMap remoteDirectories; // Actually a QMap<String, String>, but QDBus prefers this
QString mountError; QString mountError;
void addToDolphin();
void removeFromDolphin();
}; };