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
|
|
|
|
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
|
|
|
|
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)
|
2023-09-16 00:10:24 +01:00
|
|
|
, m_mounter(nullptr)
|
2024-03-05 20:50:56 +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("/"));
|
2023-08-12 17:43:03 +01:00
|
|
|
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("/"));
|
2023-09-22 20:51:25 +01:00
|
|
|
for (int i = 0; i < m_placesModel.rowCount(); ++i) {
|
|
|
|
QModelIndex index = m_placesModel.index(i, 0);
|
|
|
|
QUrl url = m_placesModel.url(index);
|
|
|
|
if (url == kioUrl) {
|
|
|
|
m_placesModel.removePlace(index);
|
|
|
|
--i;
|
|
|
|
}
|
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();
|
2023-08-12 17:43:03 +01:00
|
|
|
if (m_mounter) {
|
2014-01-15 22:07:58 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-12 17:43:03 +01:00
|
|
|
m_mounter = new Mounter(this);
|
|
|
|
connect(m_mounter, &Mounter::mounted, this, &SftpPlugin::onMounted);
|
|
|
|
connect(m_mounter, &Mounter::unmounted, this, &SftpPlugin::onUnmounted);
|
|
|
|
connect(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
|
|
|
{
|
2023-08-12 17:43:03 +01:00
|
|
|
if (m_mounter) {
|
|
|
|
m_mounter->deleteLater();
|
|
|
|
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();
|
2023-08-12 17:43:03 +01:00
|
|
|
return 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
|
|
|
{
|
2023-08-12 17:43:03 +01:00
|
|
|
return m_mounter && 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
|
|
|
|
2023-07-31 08:25:45 +01:00
|
|
|
void SftpPlugin::receivePacket(const NetworkPacket &np)
|
2014-01-06 20:55:50 +00:00
|
|
|
{
|
2023-08-12 17:43:03 +01:00
|
|
|
static const QSet<QString> fields_c{QStringLiteral("user"), QStringLiteral("port"), QStringLiteral("path")};
|
2023-04-17 18:10:08 +01:00
|
|
|
const QStringList keysList = np.body().keys();
|
|
|
|
const auto keys = QSet(keysList.begin(), keysList.end());
|
|
|
|
if (!(fields_c - keys).isEmpty() && !np.has(QStringLiteral("errorMessage"))) {
|
2023-08-01 17:31:45 +01:00
|
|
|
qCWarning(KDECONNECT_PLUGIN_SFTP) << "Invalid sftp packet received";
|
2023-07-31 08:25:45 +01:00
|
|
|
return;
|
2014-01-14 21:03:20 +00:00
|
|
|
}
|
2019-06-12 21:17:16 +01:00
|
|
|
|
2024-10-20 00:01:52 +01:00
|
|
|
// if a packet arrives before mounting or after the mount timed out, ignore it
|
|
|
|
if (!m_mounter) {
|
|
|
|
qCDebug(KDECONNECT_PLUGIN_SFTP) << "Received network packet but no mount is active, ignoring";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-12 17:43:03 +01:00
|
|
|
m_mounter->onPacketReceived(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
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-07-26 09:15:11 +01:00
|
|
|
#include "moc_sftpplugin.cpp"
|
2014-06-16 19:02:07 +01:00
|
|
|
#include "sftpplugin.moc"
|