2014-01-16 14:32:35 +00:00
|
|
|
/**
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-FileCopyrightText: 2013 Albert Vaca <albertvaka@gmail.com>
|
2014-01-16 14:32:35 +00:00
|
|
|
*
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
2014-01-16 14:32:35 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "kiokdeconnect.h"
|
|
|
|
|
|
|
|
#include <QDBusMetaType>
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <QThread>
|
2014-01-16 14:32:35 +00:00
|
|
|
|
2014-09-23 13:26:56 +01:00
|
|
|
#include <KLocalizedString>
|
|
|
|
|
2014-09-21 23:20:14 +01:00
|
|
|
#include <QDebug>
|
2018-11-06 13:19:44 +00:00
|
|
|
#include <QtPlugin>
|
2014-01-16 14:32:35 +00:00
|
|
|
|
2020-05-26 17:55:47 +01:00
|
|
|
#include "kdeconnectkio_debug.h"
|
2014-09-21 23:20:14 +01:00
|
|
|
|
2018-11-06 13:19:44 +00:00
|
|
|
class KIOPluginForMetaData : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
Q_PLUGIN_METADATA(IID "org.kde.kio.slave.kdeconnect" FILE "kdeconnect.json")
|
|
|
|
};
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
extern "C" int Q_DECL_EXPORT kdemain(int argc, char **argv)
|
2014-01-16 14:32:35 +00:00
|
|
|
{
|
2018-11-06 13:19:44 +00:00
|
|
|
QCoreApplication app(argc, argv);
|
|
|
|
app.setApplicationName(QStringLiteral("kio_kdeconnect"));
|
2019-07-17 22:37:19 +01:00
|
|
|
|
2014-01-16 14:32:35 +00:00
|
|
|
if (argc != 4) {
|
|
|
|
fprintf(stderr, "Usage: kio_kdeconnect protocol pool app\n");
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
2022-04-19 22:51:21 +01:00
|
|
|
KioKdeconnect worker(argv[2], argv[3]);
|
|
|
|
worker.dispatchLoop();
|
2014-01-16 14:32:35 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
// Some useful error mapping
|
2014-01-21 13:06:51 +00:00
|
|
|
KIO::Error toKioError(const QDBusError::ErrorType type)
|
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
switch (type) {
|
|
|
|
case QDBusError::NoError:
|
|
|
|
return KIO::Error(KJob::NoError);
|
|
|
|
case QDBusError::NoMemory:
|
|
|
|
return KIO::ERR_OUT_OF_MEMORY;
|
|
|
|
case QDBusError::Timeout:
|
|
|
|
return KIO::ERR_SERVER_TIMEOUT;
|
|
|
|
case QDBusError::TimedOut:
|
|
|
|
return KIO::ERR_SERVER_TIMEOUT;
|
|
|
|
default:
|
2022-10-17 22:17:49 +01:00
|
|
|
return KIO::ERR_WORKER_DEFINED;
|
2014-01-21 13:06:51 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
template<typename T>
|
|
|
|
KIO::WorkerResult handleDBusError(QDBusReply<T> &reply)
|
2014-01-21 13:06:51 +00:00
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
if (!reply.isValid()) {
|
2014-09-21 23:20:14 +01:00
|
|
|
qCDebug(KDECONNECT_KIO) << "Error in DBus request:" << reply.error();
|
2022-04-19 22:51:21 +01:00
|
|
|
return KIO::WorkerResult::fail(toKioError(reply.error().type()), reply.error().message());
|
2014-01-21 13:06:51 +00:00
|
|
|
}
|
2022-04-19 22:51:21 +01:00
|
|
|
return KIO::WorkerResult::pass();
|
2014-01-21 13:06:51 +00:00
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
KioKdeconnect::KioKdeconnect(const QByteArray &pool, const QByteArray &app)
|
|
|
|
: WorkerBase("kdeconnect", pool, app)
|
|
|
|
, m_dbusInterface(new DaemonDbusInterface(this))
|
2014-01-16 14:32:35 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-04-19 22:51:21 +01:00
|
|
|
KIO::WorkerResult KioKdeconnect::listAllDevices()
|
2014-01-16 14:32:35 +00:00
|
|
|
{
|
|
|
|
infoMessage(i18n("Listing devices..."));
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
// TODO: Change to all devices and show different icons for connected and disconnected?
|
2017-07-20 15:14:07 +01:00
|
|
|
const QStringList devices = m_dbusInterface->devices(true, true);
|
2014-01-16 14:32:35 +00:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
for (const QString &deviceId : devices) {
|
2014-01-16 14:32:35 +00:00
|
|
|
DeviceDbusInterface interface(deviceId);
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
if (!interface.hasPlugin(QStringLiteral("kdeconnect_sftp")))
|
|
|
|
continue;
|
2014-01-16 14:32:35 +00:00
|
|
|
|
2019-06-10 15:40:28 +01:00
|
|
|
const QString path = QStringLiteral("kdeconnect://").append(deviceId).append(QStringLiteral("/"));
|
2014-01-16 14:32:35 +00:00
|
|
|
const QString name = interface.name();
|
2016-11-26 14:38:08 +00:00
|
|
|
const QString icon = QStringLiteral("kdeconnect");
|
2014-01-16 14:32:35 +00:00
|
|
|
|
|
|
|
KIO::UDSEntry entry;
|
2019-05-08 22:29:11 +01:00
|
|
|
entry.reserve(6);
|
2019-07-17 22:37:19 +01:00
|
|
|
entry.fastInsert(KIO::UDSEntry::UDS_NAME, name);
|
|
|
|
entry.fastInsert(KIO::UDSEntry::UDS_ICON_NAME, icon);
|
2019-08-17 23:12:29 +01:00
|
|
|
entry.fastInsert(KIO::UDSEntry::UDS_FILE_TYPE, QT_STAT_DIR);
|
2022-09-10 22:23:52 +01:00
|
|
|
entry.fastInsert(KIO::UDSEntry::UDS_ACCESS,
|
|
|
|
QFileDevice::ReadOwner | QFileDevice::ExeOwner | QFileDevice::ReadGroup | QFileDevice::ExeGroup | QFileDevice::ReadOther
|
|
|
|
| QFileDevice::ExeOther);
|
2019-07-17 22:37:19 +01:00
|
|
|
entry.fastInsert(KIO::UDSEntry::UDS_MIME_TYPE, QLatin1String(""));
|
|
|
|
entry.fastInsert(KIO::UDSEntry::UDS_URL, path);
|
2014-09-23 13:26:56 +01:00
|
|
|
listEntry(entry);
|
2014-01-16 14:32:35 +00:00
|
|
|
}
|
|
|
|
|
2016-08-29 19:31:52 +01:00
|
|
|
// We also need a non-null and writable UDSentry for "."
|
|
|
|
KIO::UDSEntry entry;
|
2019-05-08 22:29:11 +01:00
|
|
|
entry.reserve(4);
|
2019-07-17 22:37:19 +01:00
|
|
|
entry.fastInsert(KIO::UDSEntry::UDS_NAME, QStringLiteral("."));
|
2019-08-17 23:12:29 +01:00
|
|
|
entry.fastInsert(KIO::UDSEntry::UDS_FILE_TYPE, QT_STAT_DIR);
|
2019-07-17 22:37:19 +01:00
|
|
|
entry.fastInsert(KIO::UDSEntry::UDS_SIZE, 0);
|
2022-09-10 22:23:52 +01:00
|
|
|
entry.fastInsert(KIO::UDSEntry::UDS_ACCESS,
|
|
|
|
QFileDevice::ReadOwner | QFileDevice::WriteOwner | QFileDevice::ExeOwner | QFileDevice::ReadGroup | QFileDevice::WriteGroup
|
|
|
|
| QFileDevice::ExeGroup | QFileDevice::ReadOther | QFileDevice::ExeOther);
|
2016-08-29 19:31:52 +01:00
|
|
|
listEntry(entry);
|
|
|
|
|
2016-11-26 14:38:08 +00:00
|
|
|
infoMessage(QLatin1String(""));
|
2022-04-19 22:51:21 +01:00
|
|
|
return KIO::WorkerResult::pass();
|
2014-01-16 14:32:35 +00:00
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
KIO::WorkerResult KioKdeconnect::listDevice(const QString &device)
|
2014-01-16 14:32:35 +00:00
|
|
|
{
|
|
|
|
infoMessage(i18n("Accessing device..."));
|
|
|
|
|
2019-03-09 14:52:29 +00:00
|
|
|
qCDebug(KDECONNECT_KIO) << "ListDevice" << device;
|
2014-01-16 14:32:35 +00:00
|
|
|
|
2019-03-09 14:52:29 +00:00
|
|
|
SftpDbusInterface interface(device);
|
2015-07-31 10:39:58 +01:00
|
|
|
|
2014-01-21 13:06:51 +00:00
|
|
|
QDBusReply<bool> mountreply = interface.mountAndWait();
|
2015-07-31 10:39:58 +01:00
|
|
|
|
2019-03-09 02:40:59 +00:00
|
|
|
if (mountreply.error().type() == QDBusError::UnknownObject) {
|
|
|
|
DaemonDbusInterface daemon;
|
|
|
|
|
|
|
|
auto devsRepl = daemon.devices(false, false);
|
|
|
|
devsRepl.waitForFinished();
|
|
|
|
|
|
|
|
if (!devsRepl.value().contains(device)) {
|
2022-10-17 22:17:49 +01:00
|
|
|
return KIO::WorkerResult::fail(KIO::ERR_WORKER_DEFINED, i18n("No such device: %0").arg(device));
|
2019-03-09 02:40:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DeviceDbusInterface dev(device);
|
|
|
|
|
2023-06-01 01:25:37 +01:00
|
|
|
if (!dev.isPaired()) {
|
2022-10-17 22:17:49 +01:00
|
|
|
return KIO::WorkerResult::fail(KIO::ERR_WORKER_DEFINED, i18n("%0 is not paired").arg(dev.name()));
|
2019-03-09 02:40:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!dev.isReachable()) {
|
2022-10-17 22:17:49 +01:00
|
|
|
return KIO::WorkerResult::fail(KIO::ERR_WORKER_DEFINED, i18n("%0 is not connected").arg(dev.name()));
|
2019-03-09 02:40:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!dev.hasPlugin(QStringLiteral("kdeconnect_sftp"))) {
|
2022-10-17 22:17:49 +01:00
|
|
|
return KIO::WorkerResult::fail(KIO::ERR_WORKER_DEFINED, i18n("%0 has no Remote Filesystem plugin").arg(dev.name()));
|
2019-03-09 02:40:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-19 22:51:21 +01:00
|
|
|
if (auto result = handleDBusError(mountreply); !result.success()) {
|
|
|
|
return result;
|
2014-01-21 13:06:51 +00:00
|
|
|
}
|
2015-07-31 10:39:58 +01:00
|
|
|
|
2014-10-10 23:01:21 +01:00
|
|
|
if (!mountreply.value()) {
|
2022-10-17 22:17:49 +01:00
|
|
|
return KIO::WorkerResult::fail(KIO::ERR_WORKER_DEFINED, interface.getMountError());
|
2014-01-21 13:06:51 +00:00
|
|
|
}
|
2015-07-31 10:39:58 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
QDBusReply<QVariantMap> urlreply = interface.getDirectories();
|
2015-07-31 10:39:58 +01:00
|
|
|
|
2022-04-19 22:51:21 +01:00
|
|
|
if (auto result = handleDBusError(urlreply); !result.success()) {
|
|
|
|
return result;
|
2014-01-21 13:06:51 +00:00
|
|
|
}
|
2015-07-31 10:39:58 +01:00
|
|
|
|
2014-10-10 23:01:21 +01:00
|
|
|
QVariantMap urls = urlreply.value();
|
2015-07-31 10:39:58 +01:00
|
|
|
|
2015-08-21 17:38:54 +01:00
|
|
|
for (QVariantMap::iterator it = urls.begin(); it != urls.end(); ++it) {
|
2016-08-29 19:31:52 +01:00
|
|
|
const QString path = it.key();
|
|
|
|
const QString name = it.value().toString();
|
2016-11-26 14:38:08 +00:00
|
|
|
const QString icon = QStringLiteral("folder");
|
2014-10-10 23:01:21 +01:00
|
|
|
|
|
|
|
KIO::UDSEntry entry;
|
2019-05-08 22:29:11 +01:00
|
|
|
entry.reserve(6);
|
2019-07-17 22:37:19 +01:00
|
|
|
entry.fastInsert(KIO::UDSEntry::UDS_NAME, name);
|
|
|
|
entry.fastInsert(KIO::UDSEntry::UDS_ICON_NAME, icon);
|
2019-08-17 23:12:29 +01:00
|
|
|
entry.fastInsert(KIO::UDSEntry::UDS_FILE_TYPE, QT_STAT_DIR);
|
2022-09-10 22:23:52 +01:00
|
|
|
entry.fastInsert(KIO::UDSEntry::UDS_ACCESS,
|
|
|
|
QFileDevice::ReadOwner | QFileDevice::ExeOwner | QFileDevice::ReadGroup | QFileDevice::ExeGroup | QFileDevice::ReadOther
|
|
|
|
| QFileDevice::ExeOther);
|
2019-07-17 22:37:19 +01:00
|
|
|
entry.fastInsert(KIO::UDSEntry::UDS_MIME_TYPE, QLatin1String(""));
|
|
|
|
entry.fastInsert(KIO::UDSEntry::UDS_URL, QUrl::fromLocalFile(path).toString());
|
2015-02-24 06:07:28 +00:00
|
|
|
listEntry(entry);
|
2014-10-10 23:01:21 +01:00
|
|
|
}
|
2014-01-16 14:32:35 +00:00
|
|
|
|
2016-08-29 19:31:52 +01:00
|
|
|
// We also need a non-null and writable UDSentry for "."
|
|
|
|
KIO::UDSEntry entry;
|
2019-05-08 22:29:11 +01:00
|
|
|
entry.reserve(4);
|
2019-07-17 22:37:19 +01:00
|
|
|
entry.fastInsert(KIO::UDSEntry::UDS_NAME, QStringLiteral("."));
|
2019-08-17 23:12:29 +01:00
|
|
|
entry.fastInsert(KIO::UDSEntry::UDS_FILE_TYPE, QT_STAT_DIR);
|
2019-07-17 22:37:19 +01:00
|
|
|
entry.fastInsert(KIO::UDSEntry::UDS_SIZE, 0);
|
2022-09-10 22:23:52 +01:00
|
|
|
entry.fastInsert(KIO::UDSEntry::UDS_ACCESS,
|
|
|
|
QFileDevice::ReadOwner | QFileDevice::WriteOwner | QFileDevice::ExeOwner | QFileDevice::ReadGroup | QFileDevice::WriteGroup
|
|
|
|
| QFileDevice::ExeGroup | QFileDevice::ReadOther | QFileDevice::ExeOther);
|
2019-01-16 13:55:12 +00:00
|
|
|
|
2016-08-29 19:31:52 +01:00
|
|
|
listEntry(entry);
|
|
|
|
|
2016-11-26 14:38:08 +00:00
|
|
|
infoMessage(QLatin1String(""));
|
2022-04-19 22:51:21 +01:00
|
|
|
return KIO::WorkerResult::pass();
|
2014-01-16 14:32:35 +00:00
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
KIO::WorkerResult KioKdeconnect::listDir(const QUrl &url)
|
2014-01-16 14:32:35 +00:00
|
|
|
{
|
2014-09-21 23:20:14 +01:00
|
|
|
qCDebug(KDECONNECT_KIO) << "Listing..." << url;
|
2014-01-16 14:32:35 +00:00
|
|
|
|
|
|
|
if (!m_dbusInterface->isValid()) {
|
2022-10-17 22:17:49 +01:00
|
|
|
return KIO::WorkerResult::fail(KIO::Error::ERR_WORKER_DEFINED, i18n("Could not contact background service."));
|
2014-01-16 14:32:35 +00:00
|
|
|
}
|
|
|
|
|
2019-03-09 14:52:29 +00:00
|
|
|
QString currentDevice = url.host();
|
|
|
|
|
|
|
|
if (currentDevice.isEmpty()) {
|
2022-04-19 22:51:21 +01:00
|
|
|
return listAllDevices();
|
2014-01-16 14:32:35 +00:00
|
|
|
} else {
|
2022-04-19 22:51:21 +01:00
|
|
|
return listDevice(currentDevice);
|
2014-01-16 14:32:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
KIO::WorkerResult KioKdeconnect::stat(const QUrl &url)
|
2014-01-16 14:32:35 +00:00
|
|
|
{
|
2014-09-21 23:20:14 +01:00
|
|
|
qCDebug(KDECONNECT_KIO) << "Stat: " << url;
|
2014-01-16 14:32:35 +00:00
|
|
|
|
|
|
|
KIO::UDSEntry entry;
|
2019-08-17 23:12:29 +01:00
|
|
|
entry.fastInsert(KIO::UDSEntry::UDS_FILE_TYPE, QT_STAT_DIR);
|
2019-03-12 13:38:49 +00:00
|
|
|
|
|
|
|
QString currentDevice = url.host();
|
|
|
|
if (!currentDevice.isEmpty()) {
|
|
|
|
SftpDbusInterface interface(currentDevice);
|
2019-05-14 21:36:44 +01:00
|
|
|
|
|
|
|
if (interface.isValid()) {
|
2019-07-17 22:37:19 +01:00
|
|
|
entry.fastInsert(KIO::UDSEntry::UDS_LOCAL_PATH, interface.mountPoint());
|
2019-05-14 21:36:44 +01:00
|
|
|
|
|
|
|
if (!interface.isMounted()) {
|
|
|
|
interface.mount();
|
|
|
|
}
|
|
|
|
}
|
2019-03-12 13:38:49 +00:00
|
|
|
}
|
|
|
|
|
2014-01-16 14:32:35 +00:00
|
|
|
statEntry(entry);
|
|
|
|
|
2022-04-19 22:51:21 +01:00
|
|
|
return KIO::WorkerResult::pass();
|
2014-01-16 14:32:35 +00:00
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
KIO::WorkerResult KioKdeconnect::get(const QUrl &url)
|
2014-01-16 14:32:35 +00:00
|
|
|
{
|
2014-09-21 23:20:14 +01:00
|
|
|
qCDebug(KDECONNECT_KIO) << "Get: " << url;
|
2016-11-26 14:38:08 +00:00
|
|
|
mimeType(QLatin1String(""));
|
2022-04-19 22:51:21 +01:00
|
|
|
return KIO::WorkerResult::pass();
|
2014-01-16 14:32:35 +00:00
|
|
|
}
|
|
|
|
|
2018-11-06 13:19:44 +00:00
|
|
|
#include "kiokdeconnect.moc"
|
2023-07-26 09:15:11 +01:00
|
|
|
#include "moc_kiokdeconnect.cpp"
|