Merge branch 'filebrowsing' into daemonsplit
This commit is contained in:
commit
d9018cb9f8
8 changed files with 374 additions and 280 deletions
|
@ -12,8 +12,9 @@ include_directories(
|
||||||
)
|
)
|
||||||
|
|
||||||
set(kdeconnect_sftp_SRCS
|
set(kdeconnect_sftp_SRCS
|
||||||
sftpplugin.cpp
|
mounter.cpp
|
||||||
mountloop.cpp
|
mountloop.cpp
|
||||||
|
sftpplugin.cpp
|
||||||
../kdeconnectplugin.cpp
|
../kdeconnectplugin.cpp
|
||||||
../pluginloader.cpp
|
../pluginloader.cpp
|
||||||
../../networkpackage.cpp
|
../../networkpackage.cpp
|
||||||
|
@ -42,7 +43,6 @@ include(../../../macros.cmake)
|
||||||
generate_and_install_dbus_interface(
|
generate_and_install_dbus_interface(
|
||||||
kdeconnect_sftp
|
kdeconnect_sftp
|
||||||
sftpplugin.h
|
sftpplugin.h
|
||||||
mountloop.h
|
|
||||||
org.kde.kdeconnect.device.sftp.xml
|
org.kde.kdeconnect.device.sftp.xml
|
||||||
OPTIONS -a
|
OPTIONS -a
|
||||||
)
|
)
|
||||||
|
|
249
kded/plugins/sftp/mounter.cpp
Normal file
249
kded/plugins/sftp/mounter.cpp
Normal file
|
@ -0,0 +1,249 @@
|
||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
|
|
74
kded/plugins/sftp/mounter.h
Normal file
74
kded/plugins/sftp/mounter.h
Normal file
|
@ -0,0 +1,74 @@
|
||||||
|
/**
|
||||||
|
* 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
|
|
@ -1,5 +1,5 @@
|
||||||
/**
|
/**
|
||||||
* 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
|
* This program is free software; you can redistribute it and/or
|
||||||
* modify it under the terms of the GNU General Public License as
|
* modify it under the terms of the GNU General Public License as
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/**
|
/**
|
||||||
* 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
|
* This program is free software; you can redistribute it and/or
|
||||||
* modify it under the terms of the GNU General Public License as
|
* modify it under the terms of the GNU General Public License as
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/**
|
/**
|
||||||
* 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
|
* This program is free software; you can redistribute it and/or
|
||||||
* modify it under the terms of the GNU General Public License as
|
* modify it under the terms of the GNU General Public License as
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/**
|
/**
|
||||||
* 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
|
* This program is free software; you can redistribute it and/or
|
||||||
* modify it under the terms of the GNU General Public License as
|
* modify it under the terms of the GNU General Public License as
|
||||||
|
@ -20,9 +20,6 @@
|
||||||
|
|
||||||
#include "sftpplugin.h"
|
#include "sftpplugin.h"
|
||||||
|
|
||||||
#include <QDir>
|
|
||||||
#include <QTimerEvent>
|
|
||||||
|
|
||||||
#include <QDBusConnection>
|
#include <QDBusConnection>
|
||||||
|
|
||||||
#include <KConfig>
|
#include <KConfig>
|
||||||
|
@ -30,79 +27,46 @@
|
||||||
#include <KIconLoader>
|
#include <KIconLoader>
|
||||||
#include <KLocalizedString>
|
#include <KLocalizedString>
|
||||||
#include <KNotification>
|
#include <KNotification>
|
||||||
#include <KProcess>
|
|
||||||
#include <KRun>
|
#include <KRun>
|
||||||
#include <KStandardDirs>
|
#include <KStandardDirs>
|
||||||
#include <kde_file.h>
|
#include <kde_file.h>
|
||||||
#include <kfileplacesmodel.h>
|
#include <kfileplacesmodel.h>
|
||||||
|
|
||||||
#include "sftp_config.h"
|
#include "sftp_config.h"
|
||||||
#include "mountloop.h"
|
#include "mounter.h"
|
||||||
#include "../../kdebugnamespace.h"
|
#include "../../kdebugnamespace.h"
|
||||||
|
|
||||||
K_PLUGIN_FACTORY( KdeConnectPluginFactory, registerPlugin< SftpPlugin >(); )
|
K_PLUGIN_FACTORY( KdeConnectPluginFactory, registerPlugin< SftpPlugin >(); )
|
||||||
K_EXPORT_PLUGIN( KdeConnectPluginFactory("kdeconnect_sftp", "kdeconnect_sftp") )
|
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";
|
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
|
struct SftpPlugin::Pimpl
|
||||||
{
|
{
|
||||||
Pimpl()
|
Pimpl()
|
||||||
{
|
{
|
||||||
connectTimer.setInterval(10 * 1000);
|
|
||||||
connectTimer.setSingleShot(true);
|
|
||||||
|
|
||||||
//Add KIO entry to Dolphin's Places
|
//Add KIO entry to Dolphin's Places
|
||||||
placesModel = new KFilePlacesModel();
|
placesModel = new KFilePlacesModel();
|
||||||
}
|
}
|
||||||
|
|
||||||
QPointer<KProcess> mountProc;
|
|
||||||
KFilePlacesModel* placesModel;
|
KFilePlacesModel* placesModel;
|
||||||
|
QPointer<Mounter> mounter;
|
||||||
QTimer connectTimer;
|
|
||||||
int idleTimerId;
|
|
||||||
|
|
||||||
MountLoop loop;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
SftpPlugin::SftpPlugin(QObject *parent, const QVariantList &args)
|
SftpPlugin::SftpPlugin(QObject *parent, const QVariantList &args)
|
||||||
: KdeConnectPlugin(parent, args)
|
: KdeConnectPlugin(parent, args)
|
||||||
, m_d(new Pimpl)
|
, 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();
|
addToDolphin();
|
||||||
kDebug(kdeconnect_kded()) << "created [" << device()->name() << "]";
|
kDebug(kdeconnect_kded()) << "Created device:" << device()->name();
|
||||||
}
|
}
|
||||||
|
|
||||||
SftpPlugin::~SftpPlugin()
|
SftpPlugin::~SftpPlugin()
|
||||||
{
|
{
|
||||||
kDebug(kdeconnect_kded()) << "destroying [" << device()->name() << "]...";
|
|
||||||
|
|
||||||
QDBusConnection::sessionBus().unregisterObject(dbusPath(), QDBusConnection::UnregisterTree);
|
QDBusConnection::sessionBus().unregisterObject(dbusPath(), QDBusConnection::UnregisterTree);
|
||||||
umount();
|
|
||||||
removeFromDolphin();
|
removeFromDolphin();
|
||||||
kDebug(kdeconnect_kded()) << "destroyed [" << device()->name() << "]";
|
umount();
|
||||||
|
kDebug(kdeconnect_kded()) << "Destroyed device:" << device()->name();
|
||||||
}
|
}
|
||||||
|
|
||||||
void SftpPlugin::addToDolphin()
|
void SftpPlugin::addToDolphin()
|
||||||
|
@ -125,64 +89,40 @@ void SftpPlugin::removeFromDolphin()
|
||||||
|
|
||||||
void SftpPlugin::connected()
|
void SftpPlugin::connected()
|
||||||
{
|
{
|
||||||
kDebug(kdeconnect_kded()) << "exposing DBUS interface: "
|
kDebug(kdeconnect_kded()) << "Exposing DBUS interface: "
|
||||||
<< QDBusConnection::sessionBus().registerObject(dbusPath(), this, QDBusConnection::ExportScriptableContents);
|
<< QDBusConnection::sessionBus().registerObject(dbusPath(), this, QDBusConnection::ExportScriptableContents);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SftpPlugin::mount()
|
void SftpPlugin::mount()
|
||||||
{
|
{
|
||||||
kDebug(kdeconnect_kded()) << "start mounting device:" << device()->name();
|
kDebug(kdeconnect_kded()) << "Device:" << device()->name();
|
||||||
if (m_d->loop.isRunning() || m_d->mountProc)
|
if (m_d->mounter)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_d->connectTimer.start();
|
KConfigGroup cfg = SftpConfig::config()->group("main");
|
||||||
|
|
||||||
NetworkPackage np(PACKAGE_TYPE_SFTP);
|
const int idleTimeout = cfg.readEntry("idle", true)
|
||||||
np.set("startBrowsing", true);
|
? cfg.readEntry("idletimeout", 60) * 60 * 1000
|
||||||
device()->sendPackage(np);
|
: 0;
|
||||||
|
|
||||||
|
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()
|
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();
|
mount();
|
||||||
|
return m_d->mounter->wait();
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void SftpPlugin::umount()
|
void SftpPlugin::umount()
|
||||||
{
|
{
|
||||||
if (m_d->mountProc)
|
kDebug(kdeconnect_kded()) << "Device:" << device()->name();
|
||||||
{
|
delete m_d->mounter.data();
|
||||||
cleanMountPoint();
|
|
||||||
if (m_d->mountProc)
|
|
||||||
{
|
|
||||||
m_d->mountProc->terminate();
|
|
||||||
QTimer::singleShot(5000, m_d->mountProc, SLOT(kill()));
|
|
||||||
m_d->mountProc->waitForFinished();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SftpPlugin::startBrowsing()
|
void SftpPlugin::startBrowsing()
|
||||||
|
@ -201,40 +141,7 @@ bool SftpPlugin::receivePackage(const NetworkPackage& np)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!m_d->mountProc.isNull())
|
Q_EMIT packageReceived(np);
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
m_d->connectTimer.stop();
|
|
||||||
|
|
||||||
m_d->mountProc = new KProcess(this);
|
|
||||||
m_d->mountProc->setOutputChannelMode(KProcess::MergedChannels);
|
|
||||||
|
|
||||||
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -246,88 +153,41 @@ QString SftpPlugin::mountPoint()
|
||||||
return mountDir + "/" + device()->id() + "/";
|
return mountDir + "/" + device()->id() + "/";
|
||||||
}
|
}
|
||||||
|
|
||||||
void SftpPlugin::timerEvent(QTimerEvent* event)
|
void SftpPlugin::onMounted()
|
||||||
{
|
{
|
||||||
if (event->timerId() == m_d->idleTimerId)
|
|
||||||
{
|
|
||||||
if (isTimeout(m_d->mountProc, SftpConfig::config()->group("main")))
|
|
||||||
{
|
|
||||||
umount();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
QObject::timerEvent(event);
|
|
||||||
}
|
|
||||||
|
|
||||||
void SftpPlugin::onStarted()
|
|
||||||
{
|
|
||||||
kDebug(kdeconnect_kded()) << qobject_cast<KProcess*>(sender())->program().join(" ");
|
|
||||||
|
|
||||||
m_d->mountProc->setProperty(lastaccess_c, QDateTime::currentDateTime());
|
|
||||||
|
|
||||||
knotify(KNotification::Notification
|
knotify(KNotification::Notification
|
||||||
, i18n("Filesystem mounted at %1").arg(mountPoint())
|
, i18n("Filesystem mounted at %1").arg(mountPoint())
|
||||||
, KIconLoader::global()->loadIcon("drive-removable-media", KIconLoader::Desktop)
|
, KIconLoader::global()->loadIcon("drive-removable-media", KIconLoader::Desktop)
|
||||||
);
|
);
|
||||||
|
|
||||||
//Used to notify MountLoop about success.
|
|
||||||
Q_EMIT mount_succesed();
|
|
||||||
|
|
||||||
connect(m_d->mountProc, SIGNAL(readyReadStandardError()), this, SLOT(readProcessOut()));
|
|
||||||
connect(m_d->mountProc, SIGNAL(readyReadStandardOutput()), this, SLOT(readProcessOut()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SftpPlugin::onError(QProcess::ProcessError error)
|
void SftpPlugin::onUnmounted(bool idleTimeout)
|
||||||
{
|
{
|
||||||
kDebug(kdeconnect_kded()) << qobject_cast<KProcess*>(sender())->program();
|
if (idleTimeout)
|
||||||
kDebug(kdeconnect_kded()) << "ARGS: error=" << error;
|
|
||||||
|
|
||||||
if (error == QProcess::FailedToStart)
|
|
||||||
{
|
{
|
||||||
knotify(KNotification::Error
|
knotify(KNotification::Notification
|
||||||
, i18n("Failed to start sshfs")
|
, i18n("Filesystem unmounted by idle timeout")
|
||||||
, KIconLoader::global()->loadIcon("dialog-error", KIconLoader::Desktop)
|
, KIconLoader::global()->loadIcon("clock", KIconLoader::Desktop)
|
||||||
);
|
);
|
||||||
cleanMountPoint();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void SftpPlugin::onFinished(int exitCode, QProcess::ExitStatus exitStatus)
|
|
||||||
{
|
|
||||||
kDebug(kdeconnect_kded()) << qobject_cast<KProcess*>(sender())->program();
|
|
||||||
kDebug(kdeconnect_kded()) << "ARGS: exitCode=" << exitCode << " exitStatus=" << exitStatus;
|
|
||||||
|
|
||||||
if (exitStatus == QProcess::NormalExit)
|
|
||||||
{
|
|
||||||
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)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
knotify(KNotification::Error
|
knotify(KNotification::Notification
|
||||||
, i18n("Error when accessing to filesystem")
|
, i18n("Filesystem unmounted")
|
||||||
, KIconLoader::global()->loadIcon("dialog-error", KIconLoader::Desktop)
|
, KIconLoader::global()->loadIcon("dialog-ok", KIconLoader::Desktop)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
cleanMountPoint();
|
m_d->mounter->deleteLater();
|
||||||
m_d->mountProc = 0;
|
m_d->mounter = 0;
|
||||||
|
}
|
||||||
|
|
||||||
//Used to notify MountLoop about error.
|
void SftpPlugin::onFailed(const QString& message)
|
||||||
//There is no loop running if mount was succesful!
|
{
|
||||||
Q_EMIT mount_failed();
|
knotify(KNotification::Error
|
||||||
|
, message
|
||||||
|
, KIconLoader::global()->loadIcon("dialog-error", KIconLoader::Desktop)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SftpPlugin::knotify(int type, const QString& text, const QPixmap& icon) const
|
void SftpPlugin::knotify(int type, const QString& text, const QPixmap& icon) const
|
||||||
|
@ -337,77 +197,3 @@ void SftpPlugin::knotify(int type, const QString& text, const QPixmap& icon) con
|
||||||
, KNotification::CloseOnTimeout);
|
, KNotification::CloseOnTimeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SftpPlugin::cleanMountPoint()
|
|
||||||
{
|
|
||||||
KProcess::execute(QStringList() << "fusermount" << "-u" << mountPoint(), 10000);
|
|
||||||
}
|
|
||||||
|
|
||||||
void SftpPlugin::mountTimeout()
|
|
||||||
{
|
|
||||||
kDebug(kdeconnect_kded()) << "loop.... TIMEOUT";
|
|
||||||
knotify(KNotification::Error
|
|
||||||
, i18n("Failed to mount filesystem: device not responding")
|
|
||||||
, KIconLoader::global()->loadIcon("dialog-error", KIconLoader::Desktop)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
void SftpPlugin::readProcessOut()
|
|
||||||
{
|
|
||||||
m_d->mountProc->setProperty(lastaccess_c, QDateTime::currentDateTime());
|
|
||||||
m_d->mountProc->readAll();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool SftpPlugin::waitForMount()
|
|
||||||
{
|
|
||||||
if (m_d->loop.isRunning())
|
|
||||||
{
|
|
||||||
MountLoop loop;
|
|
||||||
connect(&m_d->loop, SIGNAL(result(bool)), &loop, SLOT(exitWith(bool)));
|
|
||||||
|
|
||||||
kDebug(kdeconnect_kded()) << "start secondary loop...";
|
|
||||||
bool ret = loop.exec();
|
|
||||||
kDebug(kdeconnect_kded()) << "finish secondary loop...:" << ret;
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
return m_d->mountProc;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// void SftpPlugin::startAgent()
|
|
||||||
// {
|
|
||||||
// m_d->agentProc = new KProcess(this);
|
|
||||||
// m_d->agentProc->setOutputChannelMode(KProcess::SeparateChannels);
|
|
||||||
// connect(m_d->agentProc, SIGNAL(finished(int,QProcess::ExitStatus)), m_d->agentProc, SLOT(deleteLater()));
|
|
||||||
//
|
|
||||||
// m_d->agentProc->setProgram("ssh-agent", QStringList("-c"));
|
|
||||||
// m_d->agentProc->setReadChannel(QProcess::StandardOutput);
|
|
||||||
//
|
|
||||||
// kDebug(kdeconnect_kded()) << "1";
|
|
||||||
// m_d->agentProc->start();
|
|
||||||
// if (!m_d->agentProc->waitForFinished(5000))
|
|
||||||
// {
|
|
||||||
// kDebug(kdeconnect_kded()) << "2";
|
|
||||||
// m_d->agentProc->deleteLater();
|
|
||||||
// return;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// kDebug(kdeconnect_kded()) << "3";
|
|
||||||
// m_d->env = QProcessEnvironment::systemEnvironment();
|
|
||||||
// QRegExp envrx("setenv (.*) (.*);");
|
|
||||||
//
|
|
||||||
// kDebug(kdeconnect_kded()) << "4";
|
|
||||||
// QByteArray data = m_d->agentProc->readLine();
|
|
||||||
// kDebug(kdeconnect_kded()) << "line readed:" << data;
|
|
||||||
// if (envrx.indexIn(data) == -1)
|
|
||||||
// {
|
|
||||||
// kError(kdeconnect_kded()) << "can't start ssh-agent";
|
|
||||||
// return;
|
|
||||||
// }
|
|
||||||
// m_d->env.insert(envrx.cap(1), envrx.cap(2));
|
|
||||||
//
|
|
||||||
// KProcess add;
|
|
||||||
// add.setProgram("ssh-add", QStringList() << device()->privateKey());
|
|
||||||
// add.setProcessEnvironment(m_d->env);
|
|
||||||
// add.execute(5000);
|
|
||||||
// }
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/**
|
/**
|
||||||
* 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
|
* This program is free software; you can redistribute it and/or
|
||||||
* modify it under the terms of the GNU General Public License as
|
* modify it under the terms of the GNU General Public License as
|
||||||
|
@ -21,9 +21,6 @@
|
||||||
#ifndef SFTPPLUGIN_H
|
#ifndef SFTPPLUGIN_H
|
||||||
#define SFTPPLUGIN_H
|
#define SFTPPLUGIN_H
|
||||||
|
|
||||||
#include <KProcess>
|
|
||||||
#include <QEventLoop>
|
|
||||||
|
|
||||||
#include "../kdeconnectplugin.h"
|
#include "../kdeconnectplugin.h"
|
||||||
|
|
||||||
#define PACKAGE_TYPE_SFTP QLatin1String("kdeconnect.sftp")
|
#define PACKAGE_TYPE_SFTP QLatin1String("kdeconnect.sftp")
|
||||||
|
@ -48,9 +45,7 @@ public:
|
||||||
|
|
||||||
Q_SIGNALS:
|
Q_SIGNALS:
|
||||||
|
|
||||||
void mount_succesed(); //TODO make private - for internal use
|
void packageReceived(const NetworkPackage& np);
|
||||||
void mount_failed(); //TODO make private -for internal use
|
|
||||||
|
|
||||||
|
|
||||||
public Q_SLOTS:
|
public Q_SLOTS:
|
||||||
virtual bool receivePackage(const NetworkPackage& np);
|
virtual bool receivePackage(const NetworkPackage& np);
|
||||||
|
@ -64,24 +59,14 @@ public Q_SLOTS:
|
||||||
|
|
||||||
Q_SCRIPTABLE QString mountPoint();
|
Q_SCRIPTABLE QString mountPoint();
|
||||||
|
|
||||||
|
|
||||||
protected:
|
|
||||||
void timerEvent(QTimerEvent *event);
|
|
||||||
|
|
||||||
private Q_SLOTS:
|
private Q_SLOTS:
|
||||||
void onStarted();
|
void onMounted();
|
||||||
void onError(QProcess::ProcessError error);
|
void onUnmounted(bool idleTimeout);
|
||||||
void onFinished(int exitCode, QProcess::ExitStatus exitStatus);
|
void onFailed(const QString& message);
|
||||||
void mountTimeout();
|
|
||||||
|
|
||||||
void readProcessOut();
|
|
||||||
|
|
||||||
bool waitForMount();
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QString dbusPath() const { return "/modules/kdeconnect/devices/" + device()->id() + "/sftp"; }
|
QString dbusPath() const { return "/modules/kdeconnect/devices/" + device()->id() + "/sftp"; }
|
||||||
void knotify(int type, const QString& text, const QPixmap& icon) const;
|
void knotify(int type, const QString& text, const QPixmap& icon) const;
|
||||||
void cleanMountPoint();
|
|
||||||
void addToDolphin();
|
void addToDolphin();
|
||||||
void removeFromDolphin();
|
void removeFromDolphin();
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue