Commit 6b94e7fa authored by Albert Vaca Cintora's avatar Albert Vaca Cintora
Browse files

Merge branch 'qmlsftp'

Conflicts:
	kded/daemon.cpp
parents a64d6cd2 a389ce41
......@@ -15,7 +15,7 @@ include_directories(${CMAKE_CURRENT_BINARY_DIR})
add_subdirectory(kded)
add_subdirectory(libkdeconnect)
add_subdirectory(kcm)
#add_subdirectory(kioslave)
add_subdirectory(kio)
add_subdirectory(plasmoid)
add_subdirectory(tests)
......@@ -73,6 +73,8 @@ KdeConnectKcm::KdeConnectKcm(QWidget *parent, const QVariantList&)
this, SLOT(unpair()));
connect(kcmUi->ping_button, SIGNAL(pressed()),
this, SLOT(sendPing()));
connect(kcmUi->browse_button, SIGNAL(clicked(bool)),
this, SLOT(browseFilesystem()));
}
......@@ -252,3 +254,8 @@ void KdeConnectKcm::sendPing()
currentDevice->sendPing();
}
void KdeConnectKcm::browseFilesystem()
{
if (!currentDevice) return;
SftpDbusInterface(currentDevice->id(), this).startBrowsing();
}
......@@ -58,6 +58,7 @@ private Q_SLOTS:
void requestPair();
void pluginsConfigChanged();
void sendPing();
void browseFilesystem();
void resetSelection();
void pairingSuccesful();
void pairingFailed(const QString& error);
......
......@@ -169,6 +169,13 @@
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="browse_button">
<property name="text">
<string>Browse</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
......
......@@ -28,7 +28,18 @@ set(kded_kdeconnect_SRCS
kdebugnamespace.cpp
)
kde4_add_plugin(kded_kdeconnect ${kded_kdeconnect_SRCS})
kde4_add_executable(kdeconnectd kdeconnectd.cpp ${kded_kdeconnect_SRCS})
target_link_libraries(kdeconnectd
${KDE4_KDECORE_LIBS}
${KDE4_KDEUI_LIBS}
${KDE4_KIO_LIBS}
${QT_QTNETWORK_LIBRARY}
${QJSON_LIBRARIES}
${QCA2_LIBRARIES}
)
kde4_add_plugin(kded_kdeconnect kded.cpp ${kded_kdeconnect_SRCS})
target_link_libraries(kded_kdeconnect
${KDE4_KDECORE_LIBS}
......@@ -42,19 +53,27 @@ target_link_libraries(kded_kdeconnect
include(../macros.cmake)
generate_and_install_dbus_interface(
kded_kdeconnect
kdeconnectd
daemon.h
org.kde.kdeconnect.daemon.xml
OPTIONS -a
)
generate_and_install_dbus_interface(
kded_kdeconnect
kdeconnectd
device.h
org.kde.kdeconnect.device.xml
OPTIONS -a
)
generate_and_install_dbus_interface(
kded_kdeconnect
kded.h
org.kde.kded.kdeconnect.xml
OPTIONS -a
)
install(TARGETS kdeconnectd DESTINATION ${LIBEXEC_INSTALL_DIR})
install(TARGETS kded_kdeconnect DESTINATION ${PLUGIN_INSTALL_DIR})
install(FILES kdeconnect.desktop DESTINATION ${SERVICES_INSTALL_DIR}/kded)
install(FILES kdeconnect.notifyrc DESTINATION ${DATA_INSTALL_DIR}/kdeconnect)
......
......@@ -21,25 +21,23 @@
#include "daemon.h"
#include <QUuid>
#include <QFile>
#include <QFileInfo>
#include <QDBusConnection>
#include <QNetworkSession>
#include <QNetworkConfigurationManager>
#include <KConfig>
#include <KConfigGroup>
#include <KStandardDirs>
#include "kdebugnamespace.h"
#include "networkpackage.h"
#include "backends/lan/lanlinkprovider.h"
#include "backends/loopback/loopbacklinkprovider.h"
K_PLUGIN_FACTORY(KdeConnectFactory, registerPlugin<Daemon>();)
K_EXPORT_PLUGIN(KdeConnectFactory("kdeconnect", "kdeconnect-kded"))
Daemon::Daemon(QObject *parent, const QList<QVariant>&)
: KDEDModule(parent)
Daemon::Daemon(QObject *parent) : QObject(parent)
{
KSharedConfigPtr config = KSharedConfig::openConfig("kdeconnectrc");
if (!config->group("myself").hasKey("id")) {
......@@ -53,21 +51,42 @@ Daemon::Daemon(QObject *parent, const QList<QVariant>&)
//kDebug(kdeconnect_kded()) << "QCA supported capabilities:" << QCA::supportedFeatures().join(",");
if(!QCA::isSupported("rsa")) {
//TODO: Maybe display this in a more visible way?
qWarning() << "Error: KDE Connect could not find support for RSA in your QCA installation, if your distribution provides"
kWarning(kdeconnect_kded()) << "Error: KDE Connect could not find support for RSA in your QCA installation, if your distribution provides"
<< "separate packages for QCA-ossl and QCA-gnupg plugins, make sure you have them installed and try again";
return;
}
if (!config->group("myself").hasKey("privateKey") || !config->group("myself").hasKey("publicKey")) {
//http://delta.affinix.com/docs/qca/rsatest_8cpp-example.html
QCA::PrivateKey privateKey = QCA::KeyGenerator().createRSA(2048);
config->group("myself").writeEntry("privateKey", privateKey.toPEM());
QCA::PublicKey publicKey = privateKey.toPublicKey();
config->group("myself").writeEntry("publicKey", publicKey.toPEM());
//TODO: Store key in a PEM file instead (use something like KStandardDirs::locate("appdata", "private.pem"))
const QFile::Permissions strict = QFile::ReadOwner | QFile::WriteOwner | QFile::ReadUser | QFile::WriteUser;
if (!config->group("myself").hasKey("privateKeyPath"))
{
const QString privateKeyPath = KStandardDirs::locateLocal("appdata", "key.pem", true, KComponentData("kdeconnect", "kdeconnect"));
QFile privKey(privateKeyPath);
if (!privKey.open(QIODevice::ReadWrite | QIODevice::Truncate))
{
kWarning(kdeconnect_kded()) << "Error: KDE Connect could not create private keys file: " << privateKeyPath;
return;
}
if (!privKey.setPermissions(strict))
{
kWarning(kdeconnect_kded()) << "Error: KDE Connect could not set permissions for private file: " << privateKeyPath;
//return;
}
//http://delta.affinix.com/docs/qca/rsatest_8cpp-example.html
privKey.write(QCA::KeyGenerator().createRSA(2048).toPEM().toAscii());
privKey.close();
config->group("myself").writeEntry("privateKeyPath", privateKeyPath);
}
if (QFile::permissions(config->group("myself").readEntry("privateKeyPath")) != strict)
{
kWarning(kdeconnect_kded()) << "Error: KDE Connect detects wrong permissions for private file " << config->group("myself").readEntry("privateKeyPath");
//FIXME: Do not silently fail, because user won't notice the problem
//return;
}
//Debugging
......@@ -75,13 +94,13 @@ Daemon::Daemon(QObject *parent, const QList<QVariant>&)
//Load backends (hardcoded by now, should be plugins in a future)
mLinkProviders.insert(new LanLinkProvider());
//mLinkProviders.insert(new LoopbackLinkProvider());
mLinkProviders.insert(new LoopbackLinkProvider());
//Read remebered paired devices
const KConfigGroup& known = config->group("trusted_devices");
const QStringList& list = known.groupList();
Q_FOREACH(const QString& id, list) {
Device* device = new Device(id);
Device* device = new Device(this, id);
connect(device, SIGNAL(reachableStatusChanged()),
this, SLOT(onDeviceReachableStatusChanged()));
mDevices[id] = device;
......@@ -148,7 +167,7 @@ void Daemon::onNewDeviceLink(const NetworkPackage& identityPackage, DeviceLink*
} else {
//kDebug(kdeconnect_kded()) << "It is a new device";
Device* device = new Device(identityPackage, dl);
Device* device = new Device(this, identityPackage, dl);
connect(device, SIGNAL(reachableStatusChanged()), this, SLOT(onDeviceReachableStatusChanged()));
mDevices[id] = device;
......
......@@ -18,8 +18,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef DAEMON_H
#define DAEMON_H
#ifndef KDECONNECT_DAEMON_H
#define KDECONNECT_DAEMON_H
#include <QObject>
#include <QSet>
......@@ -35,13 +35,13 @@
#include "backends/linkprovider.h"
class Daemon
: public KDEDModule
: public QObject
{
Q_OBJECT
Q_CLASSINFO("D-Bus Interface", "org.kde.kdeconnect.daemon")
public:
Daemon(QObject *parent, const QList<QVariant>&);
Daemon(QObject *parent);
~Daemon();
public Q_SLOTS:
......@@ -73,7 +73,6 @@ private:
// The Initializer object sets things up, and also does cleanup when it goes out of scope
QCA::Initializer mQcaInitializer;
};
#endif
......@@ -19,9 +19,11 @@
#undef interface
#endif
#include <QDBusConnection>
#include <QFile>
Device::Device(const QString& id)
: m_deviceId(id)
Device::Device(QObject* parent, const QString& id)
: QObject(parent)
, m_deviceId(id)
, m_pairStatus(Device::Paired)
, m_protocolVersion(NetworkPackage::ProtocolVersion) //We don't know it yet
{
......@@ -33,21 +35,33 @@ Device::Device(const QString& id)
const QString& key = data.readEntry<QString>("publicKey", QString());
m_publicKey = QCA::RSAPublicKey::fromPEM(key);
//TODO: It is redundant to have our own private key in every instance of Device, move this to a signleton somewhere (Daemon?)
const QString privateKeyPath = KStandardDirs::locateLocal("appdata", "key.pem", true, KComponentData("kdeconnect", "kdeconnect"));
QFile privKey(privateKeyPath);
privKey.open(QIODevice::ReadOnly);
m_privateKey = QCA::PrivateKey::fromPEM(privKey.readAll());
//Register in bus
QDBusConnection::sessionBus().registerObject(dbusPath(), this, QDBusConnection::ExportScriptableContents | QDBusConnection::ExportAdaptors);
}
Device::Device(const NetworkPackage& identityPackage, DeviceLink* dl)
: m_deviceId(identityPackage.get<QString>("deviceId"))
Device::Device(QObject* parent, const NetworkPackage& identityPackage, DeviceLink* dl)
: QObject(parent)
, m_deviceId(identityPackage.get<QString>("deviceId"))
, m_deviceName(identityPackage.get<QString>("deviceName"))
, m_deviceType(str2type(identityPackage.get<QString>("deviceType")))
, m_pairStatus(Device::NotPaired)
, m_protocolVersion(identityPackage.get<int>("protocolVersion"))
{
addLink(identityPackage, dl);
//TODO: It is redundant to have our own private key in every instance of Device, move this to a signleton somewhere (Daemon?)
const QString privateKeyPath = KStandardDirs::locateLocal("appdata", "key.pem", true, KComponentData("kdeconnect", "kdeconnect"));
QFile privKey(privateKeyPath);
privKey.open(QIODevice::ReadOnly);
m_privateKey = QCA::PrivateKey::fromPEM(privKey.readAll());
addLink(identityPackage, dl);
//Register in bus
QDBusConnection::sessionBus().registerObject(dbusPath(), this, QDBusConnection::ExportScriptableContents | QDBusConnection::ExportAdaptors);
}
......@@ -205,11 +219,8 @@ void Device::addLink(const NetworkPackage& identityPackage, DeviceLink* link)
m_deviceName = identityPackage.get<QString>("deviceName");
m_deviceType = str2type(identityPackage.get<QString>("deviceType"));
//TODO: Do not read the key every time from config, store somewhere
KSharedConfigPtr config = KSharedConfig::openConfig("kdeconnectrc");
const QString& key = config->group("myself").readEntry<QString>("privateKey", QString());
QCA::PrivateKey privateKey = QCA::PrivateKey::fromPEM(key);
link->setPrivateKey(privateKey);
Q_ASSERT(!m_privateKey.isNull());
link->setPrivateKey(m_privateKey);
//Theoretically we will never add two links from the same provider (the provider should destroy
//the old one before this is called), so we do not have to worry about destroying old links.
......@@ -247,6 +258,11 @@ void Device::removeLink(DeviceLink* link)
}
}
QString Device::privateKeyPath() const
{
return KSharedConfig::openConfig("kdeconnectrc")->group("myself").readEntry("privateKeyPath");
}
bool Device::sendPackage(NetworkPackage& np)
{
if (np.type() != PACKAGE_TYPE_PAIR && isPaired()) {
......@@ -354,9 +370,7 @@ bool Device::sendOwnPublicKey()
{
NetworkPackage np(PACKAGE_TYPE_PAIR);
np.set("pair", true);
KSharedConfigPtr config = KSharedConfig::openConfig("kdeconnectrc");
const QString& key = config->group("myself").readEntry<QString>("publicKey", QString());
np.set("publicKey",key);
np.set("publicKey", m_privateKey.toPublicKey().toPEM());
bool success = sendPackage(np);
return success;
}
......
......@@ -64,14 +64,14 @@ public:
*
* We already know it but we need to wait for an incoming DeviceLink to communicate
*/
Device(const QString& id);
Device(QObject* parent, const QString& id);
/**
* Device known via an incoming connection sent to us via a devicelink.
*
* We know everything but we don't trust it yet
*/
Device(const NetworkPackage& np, DeviceLink* dl);
Device(QObject* parent, const NetworkPackage& np, DeviceLink* dl);
virtual ~Device();
......@@ -83,6 +83,8 @@ public:
void addLink(const NetworkPackage& identityPackage, DeviceLink*);
void removeLink(DeviceLink*);
QString privateKeyPath() const;
Q_SCRIPTABLE bool isPaired() const { return m_pairStatus==Device::Paired; }
Q_SCRIPTABLE bool pairRequested() const { return m_pairStatus==Device::Requested; }
......@@ -118,10 +120,10 @@ Q_SIGNALS:
Q_SCRIPTABLE void unpaired();
private:
//TODO: Replace device id by public key
const QString m_deviceId;
QString m_deviceName;
DeviceType m_deviceType;
QCA::PrivateKey m_privateKey;
QCA::PublicKey m_publicKey;
PairStatus m_pairStatus;
int m_protocolVersion;
......
/**
* Copyright 2014 Yuri Samoilenko <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 <sys/socket.h>
#include <unistd.h>
#include <signal.h>
#include <unistd.h>
#include <QSocketNotifier>
#include <KApplication>
#include <KAboutData>
#include <KCmdLineArgs>
#include "daemon.h"
static int sigtermfd[2];
const static char deadbeef = 1;
struct sigaction action;
void sighandler(int signum)
{
if( signum == SIGTERM || signum == SIGINT)
{
ssize_t unused = ::write(sigtermfd[0], &deadbeef, sizeof(deadbeef));
Q_UNUSED(unused);
}
}
void initializeTermHandlers(QCoreApplication* app)
{
::socketpair(AF_UNIX, SOCK_STREAM, 0, sigtermfd);
QSocketNotifier* snTerm = new QSocketNotifier(sigtermfd[1], QSocketNotifier::Read, app);
QObject::connect(snTerm, SIGNAL(activated(int)), app, SLOT(quit()));
action.sa_handler = sighandler;
sigemptyset(&action.sa_mask);
action.sa_flags = 0;
sigaction(SIGTERM, &action, NULL);
sigaction(SIGINT, &action, NULL);
}
int main(int argc, char* argv[])
{
KAboutData aboutData("kdeconnectd", "kdeconnectd",
ki18n("kdeconnect"),
"0.1",
ki18n("connect devices"),
KAboutData::License_GPL,
KLocalizedString(),
KLocalizedString(),
"http://albertvaka.wordpress.com");
KCmdLineArgs::init(argc, argv, &aboutData);
KApplication app(true); // WARNING GUI required for QClipboard access
app.disableSessionManagement();
initializeTermHandlers(&app);
new Daemon(&app);
return app.exec();
}
/**
* Copyright 2014 Yuri Samoilenko <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 "kded.h"
#include <QTimer>
#include <KPluginFactory>
#include <KStandardDirs>
#include "kdebugnamespace.h"
K_PLUGIN_FACTORY(KdeConnectFactory, registerPlugin<Kded>();)
K_EXPORT_PLUGIN(KdeConnectFactory("kdeconnect", "kdeconnect"))
Kded::Kded(QObject *parent, const QList<QVariant>&)
: KDEDModule(parent)
, m_daemon(0)
{
start();
kDebug(kdeconnect_kded()) << "kded_kdeconnect started";
}
Kded::~Kded()
{
stop();
kDebug(kdeconnect_kded()) << "kded_kdeconnect stopped";
}
bool Kded::start()
{
if (m_daemon)
{
return true;
}
const QString daemon = KStandardDirs::locate("exe", "kdeconnectd");
kDebug(kdeconnect_kded()) << "Starting daemon " << daemon;
m_daemon = new KProcess(this);
connect(m_daemon, SIGNAL(error(QProcess::ProcessError)), this, SLOT(onError(QProcess::ProcessError)));
connect(m_daemon, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(onFinished(int,QProcess::ExitStatus)));
connect(m_daemon, SIGNAL(finished(int,QProcess::ExitStatus)), m_daemon, SLOT(deleteLater()));
m_daemon->setProgram(daemon);
m_daemon->setOutputChannelMode(KProcess::SeparateChannels);
m_daemon->start();
if (!m_daemon->waitForStarted(2000)) //FIXME: KDEDs should be non-blocking, do we really need to wait for it to start?
{
kError(kdeconnect_kded()) << "Can't start " << daemon;
return false;
}
m_daemon->closeReadChannel(KProcess::StandardOutput);
kDebug(kdeconnect_kded()) << "Daemon successfuly started";
return true;
}
void Kded::stop()
{
if (m_daemon)
{
m_daemon->terminate();
if (m_daemon->waitForFinished(10000))
{
kDebug(kdeconnect_kded()) << "Daemon successfuly stopped";
}
else
{
m_daemon->kill();
kWarning(kdeconnect_kded()) << "Daemon killed";
}
m_daemon = 0;
}
}
bool Kded::restart()
{
stop();
return start();
}
void Kded::onError(QProcess::ProcessError errorCode)
{
kError(kdeconnect_kded()) << "Process error code=" << errorCode;
}
void Kded::onFinished(int exitCode, QProcess::ExitStatus status)
{
if (status == QProcess::CrashExit)
{
kError(kdeconnect_kded()) << "Process crashed with code=" << exitCode;
kError(kdeconnect_kded()) << m_daemon->readAllStandardError();
kWarning(kdeconnect_kded()) << "Restarting in 5 sec...";
QTimer::singleShot(5000, this, SLOT(start()));
}
else
{
kWarning(kdeconnect_kded()) << "Process finished with code=" << exitCode;
}
m_daemon = 0;
}
/**
* Copyright 2014 Yuri Samoilenko <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 KDECONNECT_KDED_H
#define KDECONNECT_KDED_H
#include <KDEDModule>
#include <KProcess>
class Kded
: public KDEDModule
{
Q_OBJECT
Q_CLASSINFO("D-Bus Interface", "org.kde.kded.kdeconnect")
public:
Kded(QObject *parent, const QList<QVariant>&);
~Kded();
public Q_SLOTS:
Q_SCRIPTABLE bool start();
Q_SCRIPTABLE void stop();
Q_SCRIPTABLE bool restart();
private Q_SLOTS:
void onError(QProcess::ProcessError);
void onFinished(int, QProcess::ExitStatus);
private:
KProcess* m_daemon;
};