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-01-21 13:06:51 +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>
|
2014-01-06 20:55:50 +00:00
|
|
|
#include <KRun>
|
2014-01-14 21:03:20 +00:00
|
|
|
#include <KStandardDirs>
|
2014-02-14 16:12:15 +00:00
|
|
|
#include <KFilePlacesModel>
|
2014-01-15 22:07:58 +00:00
|
|
|
#include <kde_file.h>
|
2014-01-06 20:55:50 +00:00
|
|
|
|
2014-01-15 22:07:58 +00:00
|
|
|
#include "sftp_config.h"
|
2014-01-22 21:58:53 +00:00
|
|
|
#include "mounter.h"
|
2014-06-14 15:34:00 +01:00
|
|
|
#include <core/kdebugnamespace.h>
|
2014-01-06 20:55:50 +00:00
|
|
|
|
|
|
|
K_PLUGIN_FACTORY( KdeConnectPluginFactory, registerPlugin< SftpPlugin >(); )
|
2014-06-17 21:21:20 +01:00
|
|
|
K_EXPORT_PLUGIN( KdeConnectPluginFactory("kdeconnect_sftp", "kdeconnect-plugins") )
|
2014-01-06 20:55:50 +00:00
|
|
|
|
2014-01-17 08:57:07 +00:00
|
|
|
static const QSet<QString> fields_c = QSet<QString>() << "ip" << "port" << "user" << "port" << "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
|
|
|
{
|
2014-06-05 22:44:17 +01:00
|
|
|
Pimpl() : mounter(0) {}
|
2014-01-21 19:49:50 +00:00
|
|
|
|
2014-02-27 19:29:34 +00:00
|
|
|
//Add KIO entry to Dolphin's Places
|
|
|
|
KFilePlacesModel placesModel;
|
2014-06-05 22:44:17 +01:00
|
|
|
Mounter* mounter;
|
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)
|
|
|
|
{
|
2014-01-16 14:32:35 +00:00
|
|
|
addToDolphin();
|
2014-07-01 00:25:12 +01:00
|
|
|
kDebug(debugArea()) << "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-07-01 00:25:12 +01:00
|
|
|
kDebug(debugArea()) << "Destroyed device:" << device()->name();
|
2014-01-16 14:32:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SftpPlugin::addToDolphin()
|
|
|
|
{
|
|
|
|
removeFromDolphin();
|
|
|
|
KUrl kioUrl("kdeconnect://"+device()->id()+"/");
|
2014-02-27 19:29:34 +00:00
|
|
|
m_d->placesModel.addPlace(device()->name(), kioUrl, "kdeconnect");
|
2014-07-01 00:25:12 +01:00
|
|
|
kDebug(debugArea()) << "add to dolphin";
|
2014-01-16 14:32:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SftpPlugin::removeFromDolphin()
|
|
|
|
{
|
|
|
|
KUrl kioUrl("kdeconnect://"+device()->id()+"/");
|
2014-02-27 19:29:34 +00:00
|
|
|
QModelIndex index = m_d->placesModel.closestItem(kioUrl);
|
2014-01-16 14:32:35 +00:00
|
|
|
while (index.row() != -1) {
|
2014-02-27 19:29:34 +00:00
|
|
|
m_d->placesModel.removePlace(index);
|
|
|
|
index = m_d->placesModel.closestItem(kioUrl);
|
2014-01-16 14:32:35 +00:00
|
|
|
}
|
2014-01-06 20:55:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SftpPlugin::connected()
|
|
|
|
{
|
2014-06-07 17:27:08 +01:00
|
|
|
bool state = QDBusConnection::sessionBus().registerObject(dbusPath(), this, QDBusConnection::ExportScriptableContents);
|
2014-07-01 00:25:12 +01:00
|
|
|
kDebug(debugArea()) << "Exposing DBUS interface: " << state;
|
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-07-01 00:25:12 +01:00
|
|
|
kDebug(debugArea()) << "Mount device:" << device()->name();
|
2014-06-07 17:27:08 +01:00
|
|
|
if (m_d->mounter) {
|
2014-01-15 22:07:58 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-22 21:58:53 +00:00
|
|
|
KConfigGroup cfg = SftpConfig::config()->group("main");
|
|
|
|
|
|
|
|
const int idleTimeout = cfg.readEntry("idle", true)
|
|
|
|
? cfg.readEntry("idletimeout", 60) * 60 * 1000
|
|
|
|
: 0;
|
2014-01-21 19:49:50 +00:00
|
|
|
|
2014-01-22 21:58:53 +00:00
|
|
|
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)));
|
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
|
|
|
{
|
2014-06-07 17:27:08 +01:00
|
|
|
if (m_d->mounter)
|
|
|
|
{
|
|
|
|
m_d->mounter->deleteLater();
|
|
|
|
m_d->mounter = 0;
|
|
|
|
}
|
2014-01-27 20:39:24 +00:00
|
|
|
}
|
|
|
|
|
2014-01-21 13:06:51 +00:00
|
|
|
bool SftpPlugin::mountAndWait()
|
|
|
|
{
|
|
|
|
mount();
|
2014-01-22 21:58:53 +00:00
|
|
|
return m_d->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
|
|
|
{
|
2014-01-30 15:45:49 +00:00
|
|
|
return m_d->mounter && m_d->mounter->isMounted();
|
2014-01-15 22:07:58 +00:00
|
|
|
}
|
|
|
|
|
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-02-19 13:25:40 +00:00
|
|
|
//return new KRun(KUrl::fromLocalFile(mountPoint()), 0);
|
|
|
|
return new KRun(KUrl::fromPathOrUrl("kdeconnect://"+device()->id()), 0);
|
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::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-22 21:58:53 +00:00
|
|
|
Q_EMIT packageReceived(np);
|
2014-01-14 21:03:20 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-01-16 14:32:35 +00:00
|
|
|
QString SftpPlugin::mountPoint()
|
|
|
|
{
|
2014-06-16 19:02:07 +01:00
|
|
|
const QString mountDir = KStandardDirs::locateLocal("appdata", "", true);
|
2014-02-14 16:12:15 +00:00
|
|
|
return QDir(mountDir).absoluteFilePath(device()->id());
|
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
|
|
|
{
|
2014-07-01 00:25:12 +01:00
|
|
|
kDebug(debugArea()) << device()->name() << QString("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
|
|
|
|
2014-01-22 21:58:53 +00:00
|
|
|
void SftpPlugin::onUnmounted(bool idleTimeout)
|
2014-01-14 21:03:20 +00:00
|
|
|
{
|
2014-06-07 17:27:08 +01:00
|
|
|
if (idleTimeout) {
|
2014-07-01 00:25:12 +01:00
|
|
|
kDebug(debugArea()) << device()->name() << "Remote filesystem unmounted by idle timeout";
|
2014-06-07 17:27:08 +01:00
|
|
|
} else {
|
2014-07-01 00:25:12 +01:00
|
|
|
kDebug(debugArea()) << device()->name() << "Remote filesystem unmounted";
|
2014-01-14 21:03:20 +00:00
|
|
|
}
|
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
|
|
|
{
|
2014-01-15 22:07:58 +00:00
|
|
|
knotify(KNotification::Error
|
2014-01-22 21:58:53 +00:00
|
|
|
, message
|
2014-01-15 22:07:58 +00:00
|
|
|
, KIconLoader::global()->loadIcon("dialog-error", KIconLoader::Desktop)
|
|
|
|
);
|
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-01-22 21:58:53 +00:00
|
|
|
void SftpPlugin::knotify(int type, const QString& text, const QPixmap& icon) const
|
2014-01-21 13:06:51 +00:00
|
|
|
{
|
2014-01-22 21:58:53 +00:00
|
|
|
KNotification::event(KNotification::StandardEvent(type)
|
2014-02-28 16:35:51 +00:00
|
|
|
, i18n("Device %1", device()->name()), text, icon, 0
|
2014-01-22 21:58:53 +00:00
|
|
|
, KNotification::CloseOnTimeout);
|
2014-01-21 13:06:51 +00:00
|
|
|
}
|
|
|
|
|
2014-06-16 19:02:07 +01:00
|
|
|
#include "sftpplugin.moc"
|