Commit d9018cb9 authored by Samoilenko Yuri's avatar Samoilenko Yuri
Browse files

Merge branch 'filebrowsing' into daemonsplit

parents 75155b1b 71283bbe
......@@ -12,8 +12,9 @@ include_directories(
)
set(kdeconnect_sftp_SRCS
sftpplugin.cpp
mounter.cpp
mountloop.cpp
sftpplugin.cpp
../kdeconnectplugin.cpp
../pluginloader.cpp
../../networkpackage.cpp
......@@ -42,7 +43,6 @@ include(../../../macros.cmake)
generate_and_install_dbus_interface(
kdeconnect_sftp
sftpplugin.h
mountloop.h
org.kde.kdeconnect.device.sftp.xml
OPTIONS -a
)
......
/**
* Copyright 2014 Samoilenko Yuri<kinnalru@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 <QDir>
#include <QTimerEvent>
#include <KLocalizedString>
#include <KStandardDirs>
#include "../../kdebugnamespace.h"
#include "mounter.h"
static const char* idleTimeout_c = "idleTimeout";
Mounter::Mounter(SftpPlugin* sftp, int idleTimeout)
: QObject(sftp)
, m_sftp(sftp)
, m_proc(0)
, m_id(generateId())
, m_mpoint(m_sftp->mountPoint())
, m_started(false)
{
Q_ASSERT(sftp);
connect(m_sftp, SIGNAL(packageReceived(NetworkPackage)), this, SLOT(onPakcageReceived(NetworkPackage)));
connect(&m_connectTimer, SIGNAL(timeout()), this, SLOT(onMountTimeout()));
connect(this, SIGNAL(mounted()), &m_connectTimer, SLOT(stop()));
connect(this, SIGNAL(failed(QString)), &m_connectTimer, SLOT(stop()));
if (idleTimeout)
{
connect(&m_idleTimer, SIGNAL(timeout()), this, SLOT(onIdleTimeout()));
}
m_connectTimer.setInterval(10000);
m_connectTimer.setSingleShot(true);
m_idleTimer.setInterval(idleTimeout);
m_idleTimer.setSingleShot(false);
QTimer::singleShot(0, this, SLOT(start()));
kDebug(kdeconnect_kded()) << "Created";
}
Mounter::~Mounter()
{
unmount();
kDebug(kdeconnect_kded()) << "Destroyed";
}
bool Mounter::wait()
{
if (m_started)
{
return true;
}
kDebug(kdeconnect_kded()) << "Starting loop to wait for mount";
MountLoop loop;
connect(this, SIGNAL(mounted()), &loop, SLOT(successed()));
connect(this, SIGNAL(failed(QString)), &loop, SLOT(failed()));
return loop.exec();
}
void Mounter::onPakcageReceived(const NetworkPackage& np)
{
if (np.get<bool>("stop", false))
{
kDebug(kdeconnect_kded()) << "SFTP server stopped";
unmount();
return;
}
//TODO implement on amdroid side
//if (np.get<int>("id") != m_id) return;
m_proc.reset(new KProcess(this));
m_proc->setOutputChannelMode(KProcess::MergedChannels);
connect(m_proc.data(), SIGNAL(started()), SLOT(onStarted()));
connect(m_proc.data(), SIGNAL(error(QProcess::ProcessError)), SLOT(onError(QProcess::ProcessError)));
connect(m_proc.data(), SIGNAL(finished(int,QProcess::ExitStatus)), SLOT(onFinished(int,QProcess::ExitStatus)));
const QString mpoint = m_sftp->mountPoint();
QDir().mkpath(mpoint);
const QString program = "sshfs";
const QStringList arguments = QStringList()
<< QString("%1@%2:%3")
.arg(np.get<QString>("user"))
.arg(np.get<QString>("ip"))
.arg(np.get<QString>("path"))
<< mpoint
<< "-p" << np.get<QString>("port")
<< "-d"
<< "-f"
<< "-o" << "IdentityFile=" + m_sftp->device()->privateKey();
m_proc->setProgram(program, arguments);
cleanMountPoint();
kDebug(kdeconnect_kded()) << "Staring process: " << m_proc->program().join(" ");
m_proc->start();
}
void Mounter::onStarted()
{
kDebug(kdeconnect_kded()) << "Porcess started";
m_started = true;
Q_EMIT mounted();
connect(m_proc.data(), SIGNAL(readyReadStandardError()), this, SLOT(readProcessOut()));
connect(m_proc.data(), SIGNAL(readyReadStandardOutput()), this, SLOT(readProcessOut()));
m_lastActivity = QDateTime::currentDateTime();
if (m_idleTimer.interval())
{
m_idleTimer.start();
}
}
void Mounter::onError(QProcess::ProcessError error)
{
if (error == QProcess::FailedToStart)
{
kDebug(kdeconnect_kded()) << "Porcess failed to start";
m_started = false;
Q_EMIT failed(i18n("Failed to start sshfs"));
}
}
void Mounter::onFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
Q_UNUSED(exitCode);
if (exitStatus == QProcess::NormalExit)
{
kDebug(kdeconnect_kded()) << "Porcess finished";
if (m_proc->property(idleTimeout_c).toBool())
{
Q_EMIT unmounted(true);
}
else
{
Q_EMIT unmounted(false);
}
}
else
{
kDebug(kdeconnect_kded()) << "Porcess failed";
Q_EMIT failed(i18n("Error when accessing to filesystem"));
}
cleanMountPoint();
m_proc.reset();
m_started = false;
}
void Mounter::onMountTimeout()
{
kDebug(kdeconnect_kded()) << "Timeout: device not responding";
Q_EMIT failed(i18n("Failed to mount filesystem: device not responding"));
}
void Mounter::onIdleTimeout()
{
Q_ASSERT(m_proc.data());
if (m_lastActivity.secsTo(QDateTime::currentDateTime()) >= m_idleTimer.interval() / 1000)
{
kDebug(kdeconnect_kded()) << "Timeout: there is no activity on moutned filesystem";
m_proc->setProperty(idleTimeout_c, true);
unmount();
}
}
void Mounter::readProcessOut()
{
Q_ASSERT(m_proc.data());
m_lastActivity = QDateTime::currentDateTime();
m_proc->readAll();
}
void Mounter::start()
{
NetworkPackage np(PACKAGE_TYPE_SFTP);
np.set("startBrowsing", true);
np.set("start", true);
np.set("id", m_id);
np.set("idleTimeout", m_idleTimer.interval());
m_sftp->device()->sendPackage(np);
m_connectTimer.start();
}
int Mounter::generateId()
{
static int id = 0;
return id++;
}
void Mounter::cleanMountPoint()
{
KProcess::execute(QStringList() << "fusermount" << "-u" << m_mpoint, 10000);
}
void Mounter::unmount()
{
m_idleTimer.stop();
if (m_proc)
{
cleanMountPoint();
if (m_proc)
{
m_proc->terminate();
QTimer::singleShot(5000, m_proc.data(), SLOT(kill()));
m_proc->waitForFinished();
}
}
m_started = false;
}
/**
* Copyright 2014 Samoilenko Yuri<kinnalru@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/>.
*/
#ifndef SFTPPLUGIN_MOUNTJOB_H
#define SFTPPLUGIN_MOUNTJOB_H
#include <KJob>
#include <KProcess>
#include "sftpplugin.h"
#include "mountloop.h"
class Mounter
: public QObject
{
Q_OBJECT
public:
explicit Mounter(SftpPlugin *sftp, int idleTimeout);
virtual ~Mounter();
bool wait();
Q_SIGNALS:
void mounted();
void unmounted(bool idleTimeout);
void failed(const QString& message);
private Q_SLOTS:
void onPakcageReceived(const NetworkPackage& np);
void onStarted();
void onError(QProcess::ProcessError error);
void onFinished(int exitCode, QProcess::ExitStatus exitStatus);
void onMountTimeout();
void onIdleTimeout();
void readProcessOut();
void start();
private:
int generateId();
void cleanMountPoint();
void unmount();
private:
SftpPlugin* m_sftp;
QScopedPointer<KProcess> m_proc;
int m_id;
const QString m_mpoint;
QTimer m_connectTimer;
QTimer m_idleTimer;
QDateTime m_lastActivity;
MountLoop m_loop;
bool m_started;
};
#endif
/**
* Copyright 2013 Albert Vaca <albertvaka@gmail.com>
* Copyright 2014 Samoilenko Yuri<kinnalru@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
......
/**
* Copyright 2013 Albert Vaca <albertvaka@gmail.com>
* Copyright 2014 Samoilenko Yuri<kinnalru@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
......
/**
* Copyright 2013 Albert Vaca <albertvaka@gmail.com>
* Copyright 2014 Samoilenko Yuri<kinnalru@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
......
/**
* Copyright 2013 Albert Vaca <albertvaka@gmail.com>
* Copyright 2014 Samoilenko Yuri<kinnalru@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
......@@ -20,9 +20,6 @@
#include "sftpplugin.h"
#include <QDir>
#include <QTimerEvent>
#include <QDBusConnection>
#include <KConfig>
......@@ -30,79 +27,46 @@
#include <KIconLoader>
#include <KLocalizedString>
#include <KNotification>
#include <KProcess>
#include <KRun>
#include <KStandardDirs>
#include <kde_file.h>
#include <kfileplacesmodel.h>
#include "sftp_config.h"
#include "mountloop.h"
#include "mounter.h"
#include "../../kdebugnamespace.h"
K_PLUGIN_FACTORY( KdeConnectPluginFactory, registerPlugin< SftpPlugin >(); )
K_EXPORT_PLUGIN( KdeConnectPluginFactory("kdeconnect_sftp", "kdeconnect_sftp") )
static const char* lastaccess_c = "lastaccess";
static const QSet<QString> fields_c = QSet<QString>() << "ip" << "port" << "user" << "port" << "path";
inline bool isTimeout(QObject* o, const KConfigGroup& cfg)
{
if (!o) return false;
int duration = o->property(lastaccess_c).toDateTime().secsTo(QDateTime::currentDateTime());
return cfg.readEntry("idle", true) && duration > (cfg.readEntry("idletimeout", 60) * 60);
}
struct SftpPlugin::Pimpl
{
Pimpl()
{
connectTimer.setInterval(10 * 1000);
connectTimer.setSingleShot(true);
//Add KIO entry to Dolphin's Places
placesModel = new KFilePlacesModel();
}
QPointer<KProcess> mountProc;
KFilePlacesModel* placesModel;
QTimer connectTimer;
int idleTimerId;
MountLoop loop;
QPointer<Mounter> mounter;
};
SftpPlugin::SftpPlugin(QObject *parent, const QVariantList &args)
: KdeConnectPlugin(parent, args)
, m_d(new Pimpl)
{
kDebug(kdeconnect_kded()) << "creating [" << device()->name() << "]...";
m_d->idleTimerId = startTimer(20 * 1000);
connect(&m_d->connectTimer, SIGNAL(timeout()), this, SLOT(mountTimeout()));
connect(&m_d->connectTimer, SIGNAL(timeout()), &m_d->loop, SLOT(failed()));
connect(this, SIGNAL(mount_succesed()), &m_d->loop, SLOT(successed()));
connect(this, SIGNAL(mount_failed()), &m_d->loop, SLOT(failed()));
addToDolphin();
kDebug(kdeconnect_kded()) << "created [" << device()->name() << "]";
kDebug(kdeconnect_kded()) << "Created device:" << device()->name();
}
SftpPlugin::~SftpPlugin()
{
kDebug(kdeconnect_kded()) << "destroying [" << device()->name() << "]...";
QDBusConnection::sessionBus().unregisterObject(dbusPath(), QDBusConnection::UnregisterTree);
removeFromDolphin();
umount();
removeFromDolphin();
kDebug(kdeconnect_kded()) << "destroyed [" << device()->name() << "]";
kDebug(kdeconnect_kded()) << "Destroyed device:" << device()->name();
}
void SftpPlugin::addToDolphin()
......@@ -125,64 +89,40 @@ void SftpPlugin::removeFromDolphin()
void SftpPlugin::connected()
{
kDebug(kdeconnect_kded()) << "exposing DBUS interface: "
kDebug(kdeconnect_kded()) << "Exposing DBUS interface: "
<< QDBusConnection::sessionBus().registerObject(dbusPath(), this, QDBusConnection::ExportScriptableContents);
}
void SftpPlugin::mount()
{
kDebug(kdeconnect_kded()) << "start mounting device:" << device()->name();
if (m_d->loop.isRunning() || m_d->mountProc)
kDebug(kdeconnect_kded()) << "Device:" << device()->name();
if (m_d->mounter)
{
return;
}
m_d->connectTimer.start();
KConfigGroup cfg = SftpConfig::config()->group("main");
const int idleTimeout = cfg.readEntry("idle", true)
? cfg.readEntry("idletimeout", 60) * 60 * 1000
: 0;
NetworkPackage np(PACKAGE_TYPE_SFTP);
np.set("startBrowsing", true);
device()->sendPackage(np);
m_d->mounter = new Mounter(this, idleTimeout);
connect(m_d->mounter, SIGNAL(mounted()), this, SLOT(onMounted()));
connect(m_d->mounter, SIGNAL(unmounted(bool)), this, SLOT(onUnmounted(bool)));
connect(m_d->mounter, SIGNAL(failed(QString)), this, SLOT(onFailed(QString)));
}
bool SftpPlugin::mountAndWait()
{
kDebug(kdeconnect_kded()) << "start mounting device and block:" << device()->name();
if (m_d->mountProc && !m_d->loop.isRunning())
{
return true;
}
if (m_d->loop.isRunning())
{
kDebug(kdeconnect_kded()) << "start secondary loop";
MountLoop loop;
connect(&m_d->loop, SIGNAL(result(bool)), &loop, SLOT(exitWith(bool)));
return loop.exec();
}
mount();
QTimer mt;
connect(&mt, SIGNAL(timeout()), &m_d->loop, SLOT(failed()));
mt.start(15000);
kDebug(kdeconnect_kded()) << "start primary loop";
return m_d->loop.exec();
return m_d->mounter->wait();
}
void SftpPlugin::umount()
{
if (m_d->mountProc)
{
cleanMountPoint();
if (m_d->mountProc)
{
m_d->mountProc->terminate();
QTimer::singleShot(5000, m_d->mountProc, SLOT(kill()));
m_d->mountProc->waitForFinished();
}
}
kDebug(kdeconnect_kded()) << "Device:" << device()->name();
delete m_d->mounter.data();
}
void SftpPlugin::startBrowsing()
......@@ -201,41 +141,8 @@ bool SftpPlugin::receivePackage(const NetworkPackage& np)
return false;
}
if (!m_d->mountProc.isNull())
{
return true;
}
m_d->connectTimer.stop();
m_d->mountProc = new KProcess(this);
m_d->mountProc->setOutputChannelMode(KProcess::MergedChannels);
Q_EMIT packageReceived(np);
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()));
const QString mpoint = mountPoint();
QDir().mkpath(mpoint);
const QString program = "sshfs";
const QStringList arguments = QStringList()
<< QString("%1@%2:%3")
.arg(np.get<QString>("user"))
.arg(np.get<QString>("ip"))
.arg(np.get<QString>("path"))
<< mpoint
<< "-p" << np.get<QString>("port")
<< "-d"
<< "-f"
<< "-o" << "IdentityFile=" + device()->privateKey();
m_d->mountProc->setProgram(program, arguments);
cleanMountPoint();
m_d->mountProc->start();
return true;
}
......@@ -246,168 +153,47 @@ QString SftpPlugin::mountPoint()
return mountDir + "/" + device()->id() + "/";
}
void SftpPlugin::timerEvent(QTimerEvent* event)
{
if (event->timerId() == m_d->idleTimerId)
{
if (isTimeout(m_d->mountProc, SftpConfig::config()->group("main")))
{
umount();
}
}
QObject::timerEvent(event);
}
void SftpPlugin::onStarted()
void SftpPlugin::onMounted()
{
kDebug(kdeconnect_kded()) << qobject_cast<KProcess*>(sender())->program().join(" ");
m_d->mountProc->setProperty(lastaccess_c, QDateTime::currentDateTime());
knotify(KNotification::Notification
, i18n("Filesystem mounted at %1").arg(mountPoint())
, KIconLoader::global()->loadIcon("drive-removable-media", KIconLoader::Desktop)