2014-01-06 20:55:50 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2013 Albert Vaca <albertvaka@gmail.com>
|
|
|
|
*
|
|
|
|
* 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-15 16:36:01 +00:00
|
|
|
#include <QDBusConnection>
|
2014-01-15 22:07:58 +00:00
|
|
|
#include <QDir>
|
|
|
|
#include <QTimerEvent>
|
2014-01-15 16:36:01 +00:00
|
|
|
|
2014-01-15 22:07:58 +00:00
|
|
|
#include <KConfig>
|
|
|
|
#include <KConfigGroup>
|
2014-01-14 21:03:20 +00:00
|
|
|
#include <KIconLoader>
|
2014-01-06 20:55:50 +00:00
|
|
|
#include <KLocalizedString>
|
2014-01-14 21:03:20 +00:00
|
|
|
#include <KNotification>
|
|
|
|
#include <KProcess>
|
2014-01-06 20:55:50 +00:00
|
|
|
#include <KRun>
|
2014-01-14 21:03:20 +00:00
|
|
|
#include <KStandardDirs>
|
2014-01-15 22:07:58 +00:00
|
|
|
#include <kde_file.h>
|
2014-01-16 14:32:35 +00:00
|
|
|
#include <kfileplacesmodel.h>
|
2014-01-06 20:55:50 +00:00
|
|
|
|
2014-01-15 22:07:58 +00:00
|
|
|
#include "sftp_config.h"
|
2014-01-06 20:55:50 +00:00
|
|
|
#include "../../kdebugnamespace.h"
|
|
|
|
|
|
|
|
K_PLUGIN_FACTORY( KdeConnectPluginFactory, registerPlugin< SftpPlugin >(); )
|
|
|
|
K_EXPORT_PLUGIN( KdeConnectPluginFactory("kdeconnect_sftp", "kdeconnect_sftp") )
|
|
|
|
|
2014-01-14 21:03:20 +00:00
|
|
|
static const char* passwd_c = "sftppassword";
|
2014-01-15 22:07:58 +00:00
|
|
|
static const char* mountpoint_c = "sftpmountpoint";
|
|
|
|
static const char* timestamp_c = "timestamp";
|
2014-01-16 14:32:35 +00:00
|
|
|
static const QSet<QString> fields_c = QSet<QString>() << "ip" << "port" << "user" << "port" << "password" << "path";
|
2014-01-14 21:03:20 +00:00
|
|
|
|
2014-01-15 22:07:58 +00:00
|
|
|
inline bool isTimeout(QObject* o, const KConfigGroup& cfg)
|
|
|
|
{
|
|
|
|
int duration = o->property(timestamp_c).toDateTime().secsTo(QDateTime::currentDateTime());
|
|
|
|
return cfg.readEntry("idle", true) && duration > (cfg.readEntry("idletimeout", 60) * 60);
|
|
|
|
}
|
|
|
|
|
2014-01-15 16:36:01 +00:00
|
|
|
struct SftpPlugin::Pimpl
|
2014-01-06 20:55:50 +00:00
|
|
|
{
|
2014-01-15 22:07:58 +00:00
|
|
|
Pimpl() : waitForMount(false)
|
|
|
|
{
|
|
|
|
mountTimer.setSingleShot(true);
|
|
|
|
};
|
2014-01-15 16:36:01 +00:00
|
|
|
|
|
|
|
QPointer<KProcess> mountProc;
|
2014-01-16 14:32:35 +00:00
|
|
|
KFilePlacesModel* m_placesModel;
|
2014-01-15 22:07:58 +00:00
|
|
|
QTimer mountTimer;
|
|
|
|
int idleTimer;
|
|
|
|
bool waitForMount;
|
2014-01-15 16:36:01 +00:00
|
|
|
};
|
2014-01-06 20:55:50 +00:00
|
|
|
|
2014-01-15 16:36:01 +00:00
|
|
|
SftpPlugin::SftpPlugin(QObject *parent, const QVariantList &args)
|
|
|
|
: KdeConnectPlugin(parent, args)
|
|
|
|
, m_d(new Pimpl)
|
|
|
|
{
|
|
|
|
QDBusConnection::sessionBus().registerObject(dbusPath(), this, QDBusConnection::ExportScriptableContents);
|
2014-01-15 22:07:58 +00:00
|
|
|
|
|
|
|
m_d->idleTimer = startTimer(20 * 1000);
|
|
|
|
|
|
|
|
connect(&m_d->mountTimer, SIGNAL(timeout()), this, SLOT(mountTimeout()));
|
2014-01-16 14:32:35 +00:00
|
|
|
|
|
|
|
//Add KIO entry to Dolphin's Places
|
|
|
|
m_d->m_placesModel = new KFilePlacesModel();
|
|
|
|
addToDolphin();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void SftpPlugin::addToDolphin()
|
|
|
|
{
|
|
|
|
removeFromDolphin();
|
|
|
|
KUrl kioUrl("kdeconnect://"+device()->id()+"/");
|
|
|
|
m_d->m_placesModel->addPlace(device()->name(), kioUrl, "smartphone");
|
|
|
|
kDebug(kdeconnect_kded()) << "add to dolphin";
|
|
|
|
}
|
|
|
|
|
|
|
|
void SftpPlugin::removeFromDolphin()
|
|
|
|
{
|
|
|
|
KUrl kioUrl("kdeconnect://"+device()->id()+"/");
|
|
|
|
QModelIndex index = m_d->m_placesModel->closestItem(kioUrl);
|
|
|
|
while (index.row() != -1) {
|
|
|
|
m_d->m_placesModel->removePlace(index);
|
|
|
|
index = m_d->m_placesModel->closestItem(kioUrl);
|
|
|
|
}
|
2014-01-06 20:55:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SftpPlugin::connected()
|
|
|
|
{
|
2014-01-16 14:32:35 +00:00
|
|
|
|
2014-01-06 20:55:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SftpPlugin::~SftpPlugin()
|
|
|
|
{
|
2014-01-15 16:36:01 +00:00
|
|
|
QDBusConnection::sessionBus().unregisterObject(dbusPath(), QDBusConnection::UnregisterTree);
|
2014-01-15 22:07:58 +00:00
|
|
|
umount();
|
2014-01-16 14:32:35 +00:00
|
|
|
removeFromDolphin();
|
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-01-15 22:07:58 +00:00
|
|
|
if (m_d->mountTimer.isActive() || m_d->mountProc)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_d->mountTimer.start(10000);
|
|
|
|
}
|
|
|
|
|
2014-01-06 20:55:50 +00:00
|
|
|
NetworkPackage np(PACKAGE_TYPE_SFTP);
|
|
|
|
np.set("startBrowsing", true);
|
|
|
|
device()->sendPackage(np);
|
|
|
|
}
|
|
|
|
|
2014-01-15 22:07:58 +00:00
|
|
|
void SftpPlugin::umount()
|
|
|
|
{
|
|
|
|
if (m_d->mountProc)
|
|
|
|
{
|
2014-01-16 14:32:35 +00:00
|
|
|
cleanMountPoint();
|
2014-01-15 22:07:58 +00:00
|
|
|
if (m_d->mountProc)
|
|
|
|
{
|
|
|
|
m_d->mountProc->terminate();
|
|
|
|
QTimer::singleShot(5000, m_d->mountProc, SLOT(kill()));
|
|
|
|
m_d->mountProc->waitForFinished();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SftpPlugin::startBrowsing()
|
2014-01-14 21:03:20 +00:00
|
|
|
{
|
2014-01-15 16:36:01 +00:00
|
|
|
if (m_d->mountProc)
|
2014-01-14 21:03:20 +00:00
|
|
|
{
|
2014-01-16 14:32:35 +00:00
|
|
|
new KRun(KUrl::fromLocalFile(mountPoint()), 0);
|
2014-01-15 22:07:58 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_d->waitForMount = true;
|
|
|
|
mount();
|
2014-01-14 21:03:20 +00:00
|
|
|
}
|
|
|
|
}
|
2014-01-06 20:55:50 +00:00
|
|
|
|
|
|
|
bool SftpPlugin::receivePackage(const NetworkPackage& np)
|
|
|
|
{
|
2014-01-15 16:36:01 +00:00
|
|
|
if (!(fields_c - np.body().keys().toSet()).isEmpty())
|
|
|
|
{
|
|
|
|
// package is invalid
|
2014-01-14 21:03:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
2014-01-06 20:55:50 +00:00
|
|
|
|
2014-01-15 16:36:01 +00:00
|
|
|
if (!m_d->mountProc.isNull())
|
|
|
|
{
|
2014-01-15 22:07:58 +00:00
|
|
|
return true;
|
2014-01-06 20:55:50 +00:00
|
|
|
}
|
2014-01-14 21:03:20 +00:00
|
|
|
|
2014-01-15 22:07:58 +00:00
|
|
|
m_d->mountTimer.stop();
|
|
|
|
|
2014-01-15 16:36:01 +00:00
|
|
|
m_d->mountProc = new KProcess(this);
|
|
|
|
m_d->mountProc->setOutputChannelMode(KProcess::SeparateChannels);
|
2014-01-14 21:03:20 +00:00
|
|
|
|
2014-01-15 16:36:01 +00:00
|
|
|
connect(m_d->mountProc, SIGNAL(started()), SLOT(onStarted()));
|
|
|
|
connect(m_d->mountProc, SIGNAL(error(QProcess::ProcessError)), SLOT(onError(QProcess::ProcessError)));
|
|
|
|
connect(m_d->mountProc, SIGNAL(finished(int,QProcess::ExitStatus)), SLOT(onFinished(int,QProcess::ExitStatus)));
|
|
|
|
connect(m_d->mountProc, SIGNAL(finished(int,QProcess::ExitStatus)), m_d->mountProc, SLOT(deleteLater()));
|
2014-01-14 21:03:20 +00:00
|
|
|
|
2014-01-16 14:32:35 +00:00
|
|
|
const QString mpoint = mountPoint();
|
2014-01-15 22:07:58 +00:00
|
|
|
QDir().mkpath(mpoint);
|
|
|
|
|
2014-01-14 21:03:20 +00:00
|
|
|
const QString program = "sshfs";
|
|
|
|
const QStringList arguments = QStringList()
|
2014-01-15 16:36:01 +00:00
|
|
|
<< QString("%1@%2:%3")
|
|
|
|
.arg(np.get<QString>("user"))
|
|
|
|
.arg(np.get<QString>("ip"))
|
|
|
|
.arg(np.get<QString>("path"))
|
|
|
|
<< "-p" << np.get<QString>("port")
|
|
|
|
<< "-d"
|
|
|
|
<< "-f"
|
|
|
|
<< "-o" << "password_stdin"
|
2014-01-15 22:07:58 +00:00
|
|
|
<< mpoint;
|
2014-01-14 21:03:20 +00:00
|
|
|
|
2014-01-15 16:36:01 +00:00
|
|
|
m_d->mountProc->setProgram(program, arguments);
|
|
|
|
m_d->mountProc->setProperty(passwd_c, np.get<QString>("password"));
|
2014-01-14 21:03:20 +00:00
|
|
|
|
2014-01-16 14:32:35 +00:00
|
|
|
cleanMountPoint();
|
2014-01-15 16:36:01 +00:00
|
|
|
m_d->mountProc->start();
|
2014-01-14 21:03:20 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-01-16 14:32:35 +00:00
|
|
|
QString SftpPlugin::mountPoint()
|
|
|
|
{
|
|
|
|
const QString defaultMountDir = KStandardDirs::locateLocal("appdata", "", true, componentData());
|
|
|
|
const QString mountDir = KConfig("kdeconnect/plugins/sftp").group("main").readEntry("mountpoint", defaultMountDir);
|
|
|
|
return mountDir + "/" + device()->id() + "/";
|
|
|
|
}
|
|
|
|
|
2014-01-15 22:07:58 +00:00
|
|
|
void SftpPlugin::timerEvent(QTimerEvent* event)
|
|
|
|
{
|
|
|
|
if (event->timerId() == m_d->idleTimer)
|
|
|
|
{
|
|
|
|
if (isTimeout(m_d->mountProc, SftpConfig::config()->group("main")))
|
|
|
|
{
|
|
|
|
umount();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QObject::timerEvent(event);
|
|
|
|
}
|
|
|
|
|
2014-01-14 21:03:20 +00:00
|
|
|
void SftpPlugin::onStarted()
|
|
|
|
{
|
2014-01-15 22:07:58 +00:00
|
|
|
m_d->mountProc->setProperty(timestamp_c, QDateTime::currentDateTime());
|
|
|
|
|
2014-01-15 16:36:01 +00:00
|
|
|
m_d->mountProc->write(m_d->mountProc->property(passwd_c).toString().toLocal8Bit() + "\n");
|
|
|
|
m_d->mountProc->closeWriteChannel();
|
2014-01-14 21:03:20 +00:00
|
|
|
|
2014-01-15 16:36:01 +00:00
|
|
|
knotify(KNotification::Notification
|
2014-01-16 14:32:35 +00:00
|
|
|
, i18n("Filesystem mounted at %1").arg(mountPoint())
|
2014-01-15 16:36:01 +00:00
|
|
|
, KIconLoader::global()->loadIcon("drive-removable-media", KIconLoader::Desktop)
|
|
|
|
);
|
2014-01-14 21:03:20 +00:00
|
|
|
|
2014-01-15 22:07:58 +00:00
|
|
|
if (m_d->waitForMount)
|
|
|
|
{
|
|
|
|
m_d->waitForMount = false;
|
2014-01-16 14:32:35 +00:00
|
|
|
new KRun(KUrl::fromLocalFile(mountPoint()), 0);
|
2014-01-15 22:07:58 +00:00
|
|
|
}
|
|
|
|
|
2014-01-14 21:03:20 +00:00
|
|
|
}
|
2014-01-06 20:55:50 +00:00
|
|
|
|
2014-01-14 21:03:20 +00:00
|
|
|
void SftpPlugin::onError(QProcess::ProcessError error)
|
|
|
|
{
|
|
|
|
if (error == QProcess::FailedToStart)
|
|
|
|
{
|
2014-01-15 16:36:01 +00:00
|
|
|
knotify(KNotification::Error
|
|
|
|
, i18n("Failed to start sshfs")
|
|
|
|
, KIconLoader::global()->loadIcon("dialog-error", KIconLoader::Desktop)
|
|
|
|
);
|
2014-01-16 14:32:35 +00:00
|
|
|
cleanMountPoint();
|
2014-01-14 21:03:20 +00:00
|
|
|
}
|
2014-01-06 20:55:50 +00:00
|
|
|
}
|
|
|
|
|
2014-01-14 21:03:20 +00:00
|
|
|
void SftpPlugin::onFinished(int exitCode, QProcess::ExitStatus exitStatus)
|
|
|
|
{
|
|
|
|
Q_UNUSED(exitCode);
|
|
|
|
|
|
|
|
if (exitStatus == QProcess::NormalExit)
|
|
|
|
{
|
2014-01-15 22:07:58 +00:00
|
|
|
if (isTimeout(sender(), SftpConfig::config()->group("main")))
|
|
|
|
{
|
|
|
|
knotify(KNotification::Notification
|
|
|
|
, i18n("Filesystem unmounted by idle timeout")
|
|
|
|
, KIconLoader::global()->loadIcon("clock", KIconLoader::Desktop)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
knotify(KNotification::Notification
|
|
|
|
, i18n("Filesystem unmounted")
|
|
|
|
, KIconLoader::global()->loadIcon("dialog-ok", KIconLoader::Desktop)
|
|
|
|
);
|
|
|
|
}
|
2014-01-14 21:03:20 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-01-15 16:36:01 +00:00
|
|
|
knotify(KNotification::Error
|
|
|
|
, i18n("Error when accessing to filesystem")
|
|
|
|
, KIconLoader::global()->loadIcon("dialog-error", KIconLoader::Desktop)
|
|
|
|
);
|
2014-01-14 21:03:20 +00:00
|
|
|
}
|
2014-01-15 16:36:01 +00:00
|
|
|
|
2014-01-16 14:32:35 +00:00
|
|
|
cleanMountPoint();
|
2014-01-15 16:36:01 +00:00
|
|
|
m_d->mountProc = 0;
|
|
|
|
}
|
|
|
|
|
2014-01-15 22:07:58 +00:00
|
|
|
void SftpPlugin::knotify(int type, const QString& text, const QPixmap& icon) const
|
2014-01-15 16:36:01 +00:00
|
|
|
{
|
2014-01-15 22:07:58 +00:00
|
|
|
KNotification::event(KNotification::StandardEvent(type)
|
|
|
|
, i18n("Device %1").arg(device()->name()), text, icon, 0
|
2014-01-15 16:36:01 +00:00
|
|
|
, KNotification::CloseOnTimeout);
|
|
|
|
}
|
|
|
|
|
2014-01-16 14:32:35 +00:00
|
|
|
void SftpPlugin::cleanMountPoint()
|
2014-01-15 16:36:01 +00:00
|
|
|
{
|
2014-01-16 14:32:35 +00:00
|
|
|
KProcess::execute(QStringList() << "fusermount" << "-u" << mountPoint(), 10000);
|
2014-01-14 21:03:20 +00:00
|
|
|
}
|
2014-01-15 22:07:58 +00:00
|
|
|
|
|
|
|
void SftpPlugin::mountTimeout()
|
|
|
|
{
|
|
|
|
knotify(KNotification::Error
|
|
|
|
, i18n("Failed to mount filesystem: device not responding")
|
|
|
|
, KIconLoader::global()->loadIcon("dialog-error", KIconLoader::Desktop)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|