kdeconnect-kde/plugins/sftp/sftpplugin.cpp

202 lines
5.5 KiB
C++
Raw Normal View History

2014-01-06 20:55:50 +00:00
/**
* Copyright 2014 Samoilenko Yuri<kinnalru@gmail.com>
2014-01-06 20:55:50 +00:00
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2019-03-23 16:29:26 +00:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2014-01-06 20:55:50 +00:00
*/
#include "sftpplugin.h"
2014-01-21 13:06:51 +00:00
#include <QDBusConnection>
#include <QDir>
#include <QDebug>
#include <QStandardPaths>
2014-01-21 13:06:51 +00:00
2014-01-06 20:55:50 +00:00
#include <KLocalizedString>
2014-01-14 21:03:20 +00:00
#include <KNotification>
2014-01-06 20:55:50 +00:00
#include <KRun>
#include <KFilePlacesModel>
#include <KPluginFactory>
2014-01-06 20:55:50 +00:00
#include "mounter.h"
2015-06-21 20:32:18 +01:00
#include "sftp_debug.h"
2014-01-06 20:55:50 +00:00
K_PLUGIN_FACTORY_WITH_JSON( KdeConnectPluginFactory, "kdeconnect_sftp.json", registerPlugin< SftpPlugin >(); )
2014-01-06 20:55:50 +00:00
Q_LOGGING_CATEGORY(KDECONNECT_PLUGIN_SFTP, "kdeconnect.plugin.sftp")
static const QSet<QString> fields_c = QSet<QString>() << QStringLiteral("ip") << QStringLiteral("port") << QStringLiteral("user") << QStringLiteral("port") << QStringLiteral("path");
2014-01-14 21:03:20 +00:00
struct SftpPlugin::Pimpl
2014-01-06 20:55:50 +00:00
{
Pimpl() : m_mounter(nullptr) {}
2014-01-21 19:49:50 +00:00
2014-02-27 19:29:34 +00:00
//Add KIO entry to Dolphin's Places
KFilePlacesModel m_placesModel;
Mounter* m_mounter;
};
2014-01-06 20:55:50 +00:00
SftpPlugin::SftpPlugin(QObject* parent, const QVariantList& args)
: KdeConnectPlugin(parent, args)
, d(new Pimpl())
{
deviceId = device()->id();
addToDolphin();
qCDebug(KDECONNECT_PLUGIN_SFTP) << "Created device:" << device()->name();
}
SftpPlugin::~SftpPlugin()
{
removeFromDolphin();
2014-02-05 19:26:54 +00:00
unmount();
}
void SftpPlugin::addToDolphin()
{
removeFromDolphin();
QUrl kioUrl(QStringLiteral("kdeconnect://") + deviceId + QStringLiteral("/"));
d->m_placesModel.addPlace(device()->name(), kioUrl, QStringLiteral("kdeconnect"));
qCDebug(KDECONNECT_PLUGIN_SFTP) << "add to dolphin";
}
void SftpPlugin::removeFromDolphin()
{
QUrl kioUrl(QStringLiteral("kdeconnect://") + deviceId + QStringLiteral("/"));
QModelIndex index = d->m_placesModel.closestItem(kioUrl);
while (index.row() != -1) {
d->m_placesModel.removePlace(index);
index = d->m_placesModel.closestItem(kioUrl);
}
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 (d->m_mounter) {
2014-01-15 22:07:58 +00:00
return;
}
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
{
if (d->m_mounter)
{
d->m_mounter->deleteLater();
d->m_mounter = nullptr;
}
2014-01-27 20:39:24 +00:00
}
2014-01-21 13:06:51 +00:00
bool SftpPlugin::mountAndWait()
{
mount();
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
{
return d->m_mounter && d->m_mounter->isMounted();
2014-01-15 22:07:58 +00:00
}
QString SftpPlugin::getMountError()
{
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()) {
//return new KRun(QUrl::fromLocalFile(mountPoint()), 0);
return new KRun(QUrl(QStringLiteral("kdeconnect://") + deviceId), nullptr);
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
bool SftpPlugin::receivePacket(const NetworkPacket& np)
2014-01-06 20:55:50 +00:00
{
if (!(fields_c - np.body().keys().toSet()).isEmpty() && !np.has(QStringLiteral("errorMessage"))) {
// packet is invalid
2014-01-14 21:03:20 +00:00
return false;
}
2014-01-06 20:55:50 +00:00
Q_EMIT packetReceived(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
return true;
}
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();
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 "sftpplugin.moc"