kdeconnect-kde/plugins/sftp/sftpplugin.cpp

187 lines
5 KiB
C++
Raw Permalink Normal View History

2014-01-06 20:55:50 +00:00
/**
* SPDX-FileCopyrightText: 2014 Samoilenko Yuri <kinnalru@gmail.com>
2014-01-06 20:55:50 +00: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>
#include <QDebug>
#include <QDir>
#include <QStandardPaths>
2014-01-21 13:06:51 +00:00
#include <KIO/OpenUrlJob>
2014-01-06 20:55:50 +00:00
#include <KLocalizedString>
2014-01-14 21:03:20 +00:00
#include <KNotification>
#include <KNotificationJobUiDelegate>
#include <KPluginFactory>
2014-01-06 20:55:50 +00:00
#include "mounter.h"
#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
SftpPlugin::SftpPlugin(QObject *parent, const QVariantList &args)
: KdeConnectPlugin(parent, args)
, m_mounter(nullptr)
2024-03-05 20:50:56 +00:00
, deviceId(device()->id())
2019-06-12 21:17:16 +01:00
{
addToDolphin();
qCDebug(KDECONNECT_PLUGIN_SFTP) << "Created device:" << device()->name();
}
SftpPlugin::~SftpPlugin()
{
2019-06-12 21:17:16 +01:00
removeFromDolphin();
2014-02-05 19:26:54 +00:00
unmount();
}
void SftpPlugin::addToDolphin()
{
removeFromDolphin();
2019-06-12 21:17:16 +01:00
QUrl kioUrl(QStringLiteral("kdeconnect://") + deviceId + QStringLiteral("/"));
m_placesModel.addPlace(device()->name(), kioUrl, QStringLiteral("kdeconnect"));
qCDebug(KDECONNECT_PLUGIN_SFTP) << "add to dolphin";
}
void SftpPlugin::removeFromDolphin()
{
QUrl kioUrl(QStringLiteral("kdeconnect://") + deviceId + QStringLiteral("/"));
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-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
{
qCDebug(KDECONNECT_PLUGIN_SFTP) << "Mount device:" << device()->name();
if (m_mounter) {
2014-01-15 22:07:58 +00:00
return;
}
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
{
if (m_mounter) {
m_mounter->deleteLater();
m_mounter = nullptr;
}
2014-01-27 20:39:24 +00:00
}
2014-01-21 13:06:51 +00:00
bool SftpPlugin::mountAndWait()
{
mount();
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
{
return m_mounter && m_mounter->isMounted();
2014-01-15 22:07:58 +00:00
}
QString SftpPlugin::getMountError()
2019-06-12 21:17:16 +01:00
{
if (!mountError.isEmpty()) {
return mountError;
}
2019-03-09 14:03:01 +00:00
return QString();
}
2014-01-28 17:22:09 +00:00
bool SftpPlugin::startBrowsing()
2014-01-14 21:03:20 +00:00
{
if (mountAndWait()) {
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
void SftpPlugin::receivePacket(const NetworkPacket &np)
2014-01-06 20:55:50 +00: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"))) {
qCWarning(KDECONNECT_PLUGIN_SFTP) << "Invalid sftp packet received";
return;
2014-01-14 21:03:20 +00:00
}
2019-06-12 21:17:16 +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;
}
m_mounter->onPacketReceived(np);
2014-01-14 21:03:20 +00:00
remoteDirectories.clear();
if (np.has(QStringLiteral("multiPaths"))) {
QStringList paths = np.get<QStringList>(QStringLiteral("multiPaths"), QStringList());
QStringList names = np.get<QStringList>(QStringLiteral("pathNames"), QStringList());
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"));
remoteDirectories.insert(mountPoint() + QStringLiteral("/DCIM/Camera"), i18n("Camera pictures"));
}
2014-01-14 21:03:20 +00:00
}
QString SftpPlugin::mountPoint()
{
QString runtimePath = QStandardPaths::writableLocation(QStandardPaths::RuntimeLocation);
if (runtimePath.isEmpty()) {
runtimePath = QStandardPaths::writableLocation(QStandardPaths::TempLocation);
}
return QDir(runtimePath).absoluteFilePath(deviceId);
}
void SftpPlugin::onMounted()
2014-01-14 21:03:20 +00:00
{
qCDebug(KDECONNECT_PLUGIN_SFTP) << device()->name() << QStringLiteral("Remote filesystem mounted at %1").arg(mountPoint());
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
void SftpPlugin::onUnmounted()
2014-01-14 21:03:20 +00:00
{
qCDebug(KDECONNECT_PLUGIN_SFTP) << device()->name() << "Remote filesystem unmounted";
unmount();
2019-06-12 21:17:16 +01:00
2014-01-31 10:06:51 +00:00
Q_EMIT unmounted();
}
void SftpPlugin::onFailed(const QString &message)
{
mountError = message;
KNotification::event(KNotification::Error, device()->name(), message);
unmount();
2014-01-31 10:06:51 +00:00
Q_EMIT unmounted();
2014-01-15 22:07:58 +00:00
}
QVariantMap SftpPlugin::getDirectories()
{
return remoteDirectories;
}
#include "moc_sftpplugin.cpp"
#include "sftpplugin.moc"