2014-01-06 20:55:50 +00:00
|
|
|
/**
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-FileCopyrightText: 2014 Samoilenko Yuri <kinnalru@gmail.com>
|
2014-01-06 20:55:50 +00:00
|
|
|
*
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
2014-01-06 20:55:50 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "sftpplugin.h"
|
|
|
|
|
2014-01-21 13:06:51 +00:00
|
|
|
#include <QDBusConnection>
|
2014-09-21 23:45:59 +01:00
|
|
|
#include <QDebug>
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <QDir>
|
2017-02-18 15:14:51 +00:00
|
|
|
#include <QStandardPaths>
|
2014-01-21 13:06:51 +00:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <KFilePlacesModel>
|
2020-10-09 20:30:09 +01:00
|
|
|
#include <KIO/OpenUrlJob>
|
2014-01-06 20:55:50 +00:00
|
|
|
#include <KLocalizedString>
|
2014-01-14 21:03:20 +00:00
|
|
|
#include <KNotification>
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <KNotificationJobUiDelegate>
|
2014-09-22 01:37:10 +01:00
|
|
|
#include <KPluginFactory>
|
2014-01-06 20:55:50 +00:00
|
|
|
|
2014-01-22 21:58:53 +00:00
|
|
|
#include "mounter.h"
|
2020-05-26 17:55:47 +01:00
|
|
|
#include "plugin_sftp_debug.h"
|
2014-01-06 20:55:50 +00:00
|
|
|
|
2019-06-12 21:16:54 +01:00
|
|
|
K_PLUGIN_CLASS_WITH_JSON(SftpPlugin, "kdeconnect_sftp.json")
|
2014-01-06 20:55:50 +00:00
|
|
|
|
2023-06-22 10:22:01 +01:00
|
|
|
static const QSet<QString> fields_c = QSet<QString>() << QStringLiteral("user") << QStringLiteral("port") << QStringLiteral("path");
|
2014-01-14 21:03:20 +00:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
struct SftpPlugin::Pimpl {
|
|
|
|
Pimpl()
|
|
|
|
: m_mounter(nullptr)
|
|
|
|
{
|
|
|
|
}
|
2019-06-12 21:17:16 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
// Add KIO entry to Dolphin's Places
|
|
|
|
KFilePlacesModel m_placesModel;
|
|
|
|
Mounter *m_mounter;
|
2014-01-15 16:36:01 +00:00
|
|
|
};
|
2014-01-06 20:55:50 +00:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
SftpPlugin::SftpPlugin(QObject *parent, const QVariantList &args)
|
2014-01-15 16:36:01 +00:00
|
|
|
: KdeConnectPlugin(parent, args)
|
2017-09-03 20:39:44 +01:00
|
|
|
, d(new Pimpl())
|
2022-12-22 16:24:31 +00:00
|
|
|
, deviceId(device()->id())
|
2019-06-12 21:17:16 +01:00
|
|
|
{
|
2014-01-16 14:32:35 +00:00
|
|
|
addToDolphin();
|
2014-09-21 23:45:59 +01:00
|
|
|
qCDebug(KDECONNECT_PLUGIN_SFTP) << "Created device:" << device()->name();
|
2014-01-17 08:57:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SftpPlugin::~SftpPlugin()
|
|
|
|
{
|
2019-06-12 21:17:16 +01:00
|
|
|
removeFromDolphin();
|
2014-02-05 19:26:54 +00:00
|
|
|
unmount();
|
2014-01-16 14:32:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SftpPlugin::addToDolphin()
|
|
|
|
{
|
|
|
|
removeFromDolphin();
|
2019-06-12 21:17:16 +01:00
|
|
|
|
2019-06-10 15:40:28 +01:00
|
|
|
QUrl kioUrl(QStringLiteral("kdeconnect://") + deviceId + QStringLiteral("/"));
|
2017-09-03 20:39:44 +01:00
|
|
|
d->m_placesModel.addPlace(device()->name(), kioUrl, QStringLiteral("kdeconnect"));
|
2014-09-21 23:45:59 +01:00
|
|
|
qCDebug(KDECONNECT_PLUGIN_SFTP) << "add to dolphin";
|
2014-01-16 14:32:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SftpPlugin::removeFromDolphin()
|
|
|
|
{
|
2019-06-10 15:40:28 +01:00
|
|
|
QUrl kioUrl(QStringLiteral("kdeconnect://") + deviceId + QStringLiteral("/"));
|
2017-09-03 20:39:44 +01:00
|
|
|
QModelIndex index = d->m_placesModel.closestItem(kioUrl);
|
2014-01-16 14:32:35 +00:00
|
|
|
while (index.row() != -1) {
|
2017-09-03 20:39:44 +01:00
|
|
|
d->m_placesModel.removePlace(index);
|
|
|
|
index = d->m_placesModel.closestItem(kioUrl);
|
2014-01-16 14:32:35 +00:00
|
|
|
}
|
2014-01-06 20:55:50 +00:00
|
|
|
}
|
|
|
|
|
2014-01-15 22:07:58 +00:00
|
|
|
void SftpPlugin::mount()
|
2014-01-06 20:55:50 +00:00
|
|
|
{
|
2014-09-21 23:45:59 +01:00
|
|
|
qCDebug(KDECONNECT_PLUGIN_SFTP) << "Mount device:" << device()->name();
|
2017-09-03 20:39:44 +01:00
|
|
|
if (d->m_mounter) {
|
2014-01-15 22:07:58 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-03 20:39:44 +01:00
|
|
|
d->m_mounter = new Mounter(this);
|
|
|
|
connect(d->m_mounter, &Mounter::mounted, this, &SftpPlugin::onMounted);
|
|
|
|
connect(d->m_mounter, &Mounter::unmounted, this, &SftpPlugin::onUnmounted);
|
|
|
|
connect(d->m_mounter, &Mounter::failed, this, &SftpPlugin::onFailed);
|
2014-01-06 20:55:50 +00:00
|
|
|
}
|
|
|
|
|
2014-02-05 19:26:54 +00:00
|
|
|
void SftpPlugin::unmount()
|
2014-01-27 20:39:24 +00:00
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
if (d->m_mounter) {
|
2017-09-03 20:39:44 +01:00
|
|
|
d->m_mounter->deleteLater();
|
|
|
|
d->m_mounter = nullptr;
|
2014-06-07 17:27:08 +01:00
|
|
|
}
|
2014-01-27 20:39:24 +00:00
|
|
|
}
|
|
|
|
|
2014-01-21 13:06:51 +00:00
|
|
|
bool SftpPlugin::mountAndWait()
|
|
|
|
{
|
|
|
|
mount();
|
2017-09-03 20:39:44 +01:00
|
|
|
return d->m_mounter->wait();
|
2014-01-21 13:06:51 +00:00
|
|
|
}
|
|
|
|
|
2014-04-05 19:04:59 +01:00
|
|
|
bool SftpPlugin::isMounted() const
|
2014-01-15 22:07:58 +00:00
|
|
|
{
|
2017-09-03 20:39:44 +01:00
|
|
|
return d->m_mounter && d->m_mounter->isMounted();
|
2014-01-15 22:07:58 +00:00
|
|
|
}
|
|
|
|
|
2019-01-22 17:42:00 +00:00
|
|
|
QString SftpPlugin::getMountError()
|
2019-06-12 21:17:16 +01:00
|
|
|
{
|
2019-01-22 17:42:00 +00:00
|
|
|
if (!mountError.isEmpty()) {
|
|
|
|
return mountError;
|
|
|
|
}
|
2019-03-09 14:03:01 +00:00
|
|
|
return QString();
|
2019-01-22 17:42:00 +00:00
|
|
|
}
|
|
|
|
|
2014-01-28 17:22:09 +00:00
|
|
|
bool SftpPlugin::startBrowsing()
|
2014-01-14 21:03:20 +00:00
|
|
|
{
|
2014-06-07 17:27:08 +01:00
|
|
|
if (mountAndWait()) {
|
2020-10-09 20:30:09 +01:00
|
|
|
auto *job = new KIO::OpenUrlJob(QUrl(QStringLiteral("kdeconnect://") + deviceId));
|
|
|
|
job->setUiDelegate(new KNotificationJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled));
|
|
|
|
job->start();
|
|
|
|
return true;
|
2014-01-15 22:07:58 +00:00
|
|
|
}
|
2014-01-28 17:22:09 +00:00
|
|
|
return false;
|
2014-01-14 21:03:20 +00:00
|
|
|
}
|
2014-01-06 20:55:50 +00:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
bool SftpPlugin::receivePacket(const NetworkPacket &np)
|
2014-01-06 20:55:50 +00:00
|
|
|
{
|
2023-04-17 18:10:08 +01:00
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
|
|
|
|
const auto keys = np.body().keys().toSet();
|
|
|
|
#else
|
|
|
|
const QStringList keysList = np.body().keys();
|
|
|
|
const auto keys = QSet(keysList.begin(), keysList.end());
|
|
|
|
#endif
|
|
|
|
if (!(fields_c - keys).isEmpty() && !np.has(QStringLiteral("errorMessage"))) {
|
2018-03-04 19:48:51 +00:00
|
|
|
// packet is invalid
|
2014-01-14 21:03:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-06-12 21:17:16 +01:00
|
|
|
|
2018-03-04 19:48:51 +00:00
|
|
|
Q_EMIT packetReceived(np);
|
2014-01-14 21:03:20 +00:00
|
|
|
|
2014-10-10 23:01:21 +01:00
|
|
|
remoteDirectories.clear();
|
2016-11-26 14:38:08 +00:00
|
|
|
if (np.has(QStringLiteral("multiPaths"))) {
|
2022-09-10 22:23:52 +01:00
|
|
|
QStringList paths = np.get<QStringList>(QStringLiteral("multiPaths"), QStringList());
|
|
|
|
QStringList names = np.get<QStringList>(QStringLiteral("pathNames"), QStringList());
|
2014-10-10 23:01:21 +01:00
|
|
|
int size = qMin<int>(names.size(), paths.size());
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
remoteDirectories.insert(mountPoint() + paths.at(i), names.at(i));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
remoteDirectories.insert(mountPoint(), i18n("All files"));
|
2019-06-10 15:40:28 +01:00
|
|
|
remoteDirectories.insert(mountPoint() + QStringLiteral("/DCIM/Camera"), i18n("Camera pictures"));
|
2014-10-10 23:01:21 +01:00
|
|
|
}
|
2014-01-14 21:03:20 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-01-16 14:32:35 +00:00
|
|
|
QString SftpPlugin::mountPoint()
|
|
|
|
{
|
2017-02-18 15:14:51 +00:00
|
|
|
QString runtimePath = QStandardPaths::writableLocation(QStandardPaths::RuntimeLocation);
|
|
|
|
if (runtimePath.isEmpty()) {
|
|
|
|
runtimePath = QStandardPaths::writableLocation(QStandardPaths::TempLocation);
|
|
|
|
}
|
|
|
|
return QDir(runtimePath).absoluteFilePath(deviceId);
|
2014-01-16 14:32:35 +00:00
|
|
|
}
|
|
|
|
|
2014-01-22 21:58:53 +00:00
|
|
|
void SftpPlugin::onMounted()
|
2014-01-14 21:03:20 +00:00
|
|
|
{
|
2016-11-26 14:38:08 +00:00
|
|
|
qCDebug(KDECONNECT_PLUGIN_SFTP) << device()->name() << QStringLiteral("Remote filesystem mounted at %1").arg(mountPoint());
|
2014-02-16 10:33:18 +00:00
|
|
|
|
2014-01-31 10:06:51 +00:00
|
|
|
Q_EMIT mounted();
|
2014-01-14 21:03:20 +00:00
|
|
|
}
|
2014-01-06 20:55:50 +00:00
|
|
|
|
2015-03-09 05:12:55 +00:00
|
|
|
void SftpPlugin::onUnmounted()
|
2014-01-14 21:03:20 +00:00
|
|
|
{
|
2015-03-09 05:12:55 +00:00
|
|
|
qCDebug(KDECONNECT_PLUGIN_SFTP) << device()->name() << "Remote filesystem unmounted";
|
2014-02-16 10:33:18 +00:00
|
|
|
|
2014-06-07 17:27:08 +01:00
|
|
|
unmount();
|
2019-06-12 21:17:16 +01:00
|
|
|
|
2014-01-31 10:06:51 +00:00
|
|
|
Q_EMIT unmounted();
|
2014-01-15 16:36:01 +00:00
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void SftpPlugin::onFailed(const QString &message)
|
2014-01-15 16:36:01 +00:00
|
|
|
{
|
2019-03-09 14:24:29 +00:00
|
|
|
mountError = message;
|
2015-03-09 05:12:55 +00:00
|
|
|
KNotification::event(KNotification::Error, device()->name(), message);
|
2014-06-07 17:27:08 +01:00
|
|
|
|
|
|
|
unmount();
|
|
|
|
|
2014-01-31 10:06:51 +00:00
|
|
|
Q_EMIT unmounted();
|
2014-01-15 22:07:58 +00:00
|
|
|
}
|
|
|
|
|
2014-10-10 23:01:21 +01:00
|
|
|
QVariantMap SftpPlugin::getDirectories()
|
|
|
|
{
|
|
|
|
return remoteDirectories;
|
|
|
|
}
|
|
|
|
|
2014-06-16 19:02:07 +01:00
|
|
|
#include "sftpplugin.moc"
|