2014-01-06 20:55:50 +00:00
|
|
|
/**
|
2014-01-22 21:58:53 +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
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "sftpplugin.h"
|
|
|
|
|
2014-01-21 13:06:51 +00:00
|
|
|
#include <QDBusConnection>
|
2014-02-14 16:12:15 +00:00
|
|
|
#include <QDir>
|
2014-09-21 23:45:59 +01:00
|
|
|
#include <QDebug>
|
2017-02-18 15:14:51 +00:00
|
|
|
#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>
|
2014-02-14 16:12:15 +00:00
|
|
|
#include <KFilePlacesModel>
|
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"
|
2015-06-21 20:32:18 +01:00
|
|
|
#include "sftp_debug.h"
|
2014-01-06 20:55:50 +00:00
|
|
|
|
2015-03-19 15:36:53 +00:00
|
|
|
K_PLUGIN_FACTORY_WITH_JSON( KdeConnectPluginFactory, "kdeconnect_sftp.json", registerPlugin< SftpPlugin >(); )
|
2014-01-06 20:55:50 +00:00
|
|
|
|
2014-09-21 23:45:59 +01:00
|
|
|
Q_LOGGING_CATEGORY(KDECONNECT_PLUGIN_SFTP, "kdeconnect.plugin.sftp")
|
|
|
|
|
2016-11-26 14:38:08 +00:00
|
|
|
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
|
|
|
|
2014-01-15 16:36:01 +00:00
|
|
|
struct SftpPlugin::Pimpl
|
2014-01-06 20:55:50 +00:00
|
|
|
{
|
2017-09-03 20:39:44 +01: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
|
2017-09-03 20:39:44 +01:00
|
|
|
KFilePlacesModel m_placesModel;
|
|
|
|
Mounter* m_mounter;
|
2014-01-15 16:36:01 +00:00
|
|
|
};
|
2014-01-06 20:55:50 +00:00
|
|
|
|
2017-09-03 20:39:44 +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())
|
2014-01-15 16:36:01 +00:00
|
|
|
{
|
2015-01-20 06:49:47 +00:00
|
|
|
deviceId = device()->id();
|
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()
|
|
|
|
{
|
|
|
|
QDBusConnection::sessionBus().unregisterObject(dbusPath(), QDBusConnection::UnregisterTree);
|
2014-01-22 21:58:53 +00:00
|
|
|
removeFromDolphin();
|
2014-02-05 19:26:54 +00:00
|
|
|
unmount();
|
2014-01-16 14:32:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SftpPlugin::addToDolphin()
|
|
|
|
{
|
|
|
|
removeFromDolphin();
|
2019-01-16 13:55:12 +00:00
|
|
|
|
2015-01-21 06:44:31 +00:00
|
|
|
QUrl kioUrl("kdeconnect://"+deviceId+"/");
|
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()
|
|
|
|
{
|
2015-01-21 06:44:31 +00:00
|
|
|
QUrl kioUrl("kdeconnect://"+deviceId+"/");
|
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
|
|
|
{
|
2017-09-03 20:39:44 +01:00
|
|
|
if (d->m_mounter)
|
2014-06-07 17:27:08 +01:00
|
|
|
{
|
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()
|
|
|
|
{
|
|
|
|
if (!mountError.isEmpty()) {
|
|
|
|
return mountError;
|
|
|
|
} else {
|
|
|
|
return i18n("Could not mount device filesystem");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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()) {
|
2014-09-21 21:23:31 +01:00
|
|
|
//return new KRun(QUrl::fromLocalFile(mountPoint()), 0);
|
2015-09-08 09:46:59 +01:00
|
|
|
return new KRun(QUrl("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
|
|
|
|
2018-03-04 19:48:51 +00:00
|
|
|
bool SftpPlugin::receivePacket(const NetworkPacket& np)
|
2014-01-06 20:55:50 +00:00
|
|
|
{
|
2019-01-22 17:42:00 +00:00
|
|
|
if (!(fields_c - np.body().keys().toSet()).isEmpty() && !np.has("errorMessage")) {
|
2018-03-04 19:48:51 +00:00
|
|
|
// packet is invalid
|
2014-01-14 21:03:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
2014-01-06 20:55:50 +00: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"))) {
|
|
|
|
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"));
|
|
|
|
remoteDirectories.insert(mountPoint() + "/DCIM/Camera", i18n("Camera pictures"));
|
|
|
|
}
|
2019-01-22 17:42:00 +00:00
|
|
|
|
|
|
|
if (np.has("errorMessage")) {
|
|
|
|
mountError = np.get<QString>("errorMessage");
|
|
|
|
}
|
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();
|
2014-01-31 10:06:51 +00:00
|
|
|
|
|
|
|
Q_EMIT unmounted();
|
2014-01-15 16:36:01 +00:00
|
|
|
}
|
|
|
|
|
2014-01-22 21:58:53 +00:00
|
|
|
void SftpPlugin::onFailed(const QString& message)
|
2014-01-15 16:36:01 +00:00
|
|
|
{
|
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"
|