2014-01-16 14:32:35 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2013 Albert Vaca <albertvaka@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 "kiokdeconnect.h"
|
|
|
|
|
|
|
|
#include <QtCore/QThread>
|
|
|
|
#include <QDBusMetaType>
|
|
|
|
|
2014-09-23 13:26:56 +01:00
|
|
|
#include <KLocalizedString>
|
|
|
|
|
2014-09-21 23:20:14 +01:00
|
|
|
#include <QDebug>
|
2014-01-16 14:32:35 +00:00
|
|
|
|
2014-09-21 23:20:14 +01:00
|
|
|
Q_LOGGING_CATEGORY(KDECONNECT_KIO, "kdeconnect.kio")
|
|
|
|
|
2014-09-23 13:26:56 +01:00
|
|
|
extern "C" int Q_DECL_EXPORT kdemain(int argc, char **argv)
|
2014-01-16 14:32:35 +00:00
|
|
|
{
|
|
|
|
if (argc != 4) {
|
|
|
|
fprintf(stderr, "Usage: kio_kdeconnect protocol pool app\n");
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
KioKdeconnect slave(argv[2], argv[3]);
|
|
|
|
slave.dispatchLoop();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-01-21 13:06:51 +00:00
|
|
|
//Some useful error mapping
|
|
|
|
KIO::Error toKioError(const QDBusError::ErrorType type)
|
|
|
|
{
|
|
|
|
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:
|
|
|
|
return KIO::ERR_INTERNAL;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool handleDBusError(QDBusReply<T>& reply, KIO::SlaveBase* slave)
|
|
|
|
{
|
|
|
|
if (!reply.isValid())
|
|
|
|
{
|
2014-09-21 23:20:14 +01:00
|
|
|
qCDebug(KDECONNECT_KIO) << "Error in DBus request:" << reply.error();
|
2014-01-21 13:06:51 +00:00
|
|
|
slave->error(toKioError(reply.error().type()),reply.error().message());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-01-16 14:32:35 +00:00
|
|
|
KioKdeconnect::KioKdeconnect(const QByteArray &pool, const QByteArray &app)
|
|
|
|
: SlaveBase("kdeconnect", pool, app),
|
|
|
|
m_dbusInterface(new DaemonDbusInterface(this))
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void KioKdeconnect::listAllDevices()
|
|
|
|
{
|
|
|
|
infoMessage(i18n("Listing devices..."));
|
|
|
|
|
2014-10-10 19:47:35 +01:00
|
|
|
//TODO: Change to all devices and show different icons for connected and disconnected?
|
|
|
|
QStringList devices = m_dbusInterface->devices(true, true);
|
2014-01-16 14:32:35 +00:00
|
|
|
|
|
|
|
Q_FOREACH(const QString& deviceId, devices) {
|
|
|
|
|
|
|
|
DeviceDbusInterface interface(deviceId);
|
|
|
|
|
|
|
|
if (!interface.hasPlugin("kdeconnect_sftp")) continue;
|
|
|
|
|
2016-08-29 19:31:52 +01:00
|
|
|
const QString path = QString("kdeconnect://").append(deviceId).append("/");
|
2014-01-16 14:32:35 +00:00
|
|
|
const QString name = interface.name();
|
2014-02-19 20:10:41 +00:00
|
|
|
const QString icon = "kdeconnect";
|
2014-01-16 14:32:35 +00:00
|
|
|
|
|
|
|
KIO::UDSEntry entry;
|
|
|
|
entry.insert(KIO::UDSEntry::UDS_NAME, name);
|
|
|
|
entry.insert(KIO::UDSEntry::UDS_ICON_NAME, icon);
|
|
|
|
entry.insert(KIO::UDSEntry::UDS_FILE_TYPE, S_IFDIR);
|
2016-08-29 19:31:52 +01:00
|
|
|
entry.insert(KIO::UDSEntry::UDS_ACCESS, S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
|
2014-01-16 14:32:35 +00:00
|
|
|
entry.insert(KIO::UDSEntry::UDS_MIME_TYPE, "");
|
2016-08-29 19:31:52 +01:00
|
|
|
entry.insert(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;
|
|
|
|
entry.insert(KIO::UDSEntry::UDS_NAME, QStringLiteral("."));
|
|
|
|
entry.insert(KIO::UDSEntry::UDS_FILE_TYPE, S_IFDIR);
|
|
|
|
entry.insert(KIO::UDSEntry::UDS_SIZE, 0);
|
|
|
|
entry.insert(KIO::UDSEntry::UDS_ACCESS, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH);
|
|
|
|
listEntry(entry);
|
|
|
|
|
2014-01-16 14:32:35 +00:00
|
|
|
infoMessage("");
|
|
|
|
finished();
|
|
|
|
}
|
|
|
|
|
|
|
|
void KioKdeconnect::listDevice()
|
|
|
|
{
|
|
|
|
infoMessage(i18n("Accessing device..."));
|
|
|
|
|
2014-09-21 23:20:14 +01:00
|
|
|
qCDebug(KDECONNECT_KIO) << "ListDevice" << m_currentDevice;
|
2014-01-16 14:32:35 +00:00
|
|
|
|
|
|
|
SftpDbusInterface interface(m_currentDevice);
|
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
|
|
|
|
2014-10-10 23:01:21 +01:00
|
|
|
if (handleDBusError(mountreply, this)) {
|
2014-01-21 13:06:51 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-07-31 10:39:58 +01:00
|
|
|
|
2014-10-10 23:01:21 +01:00
|
|
|
if (!mountreply.value()) {
|
2014-01-21 13:06:51 +00:00
|
|
|
error(KIO::ERR_COULD_NOT_MOUNT, i18n("Could not mount device filesystem"));
|
|
|
|
return;
|
|
|
|
}
|
2015-07-31 10:39:58 +01:00
|
|
|
|
2014-10-10 23:01:21 +01:00
|
|
|
QDBusReply< QVariantMap > urlreply = interface.getDirectories();
|
2015-07-31 10:39:58 +01:00
|
|
|
|
2014-10-10 23:01:21 +01:00
|
|
|
if (handleDBusError(urlreply, this)) {
|
2014-01-21 13:06:51 +00:00
|
|
|
return;
|
|
|
|
}
|
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) {
|
2014-10-10 23:01:21 +01:00
|
|
|
|
2016-08-29 19:31:52 +01:00
|
|
|
const QString path = it.key();
|
|
|
|
const QString name = it.value().toString();
|
|
|
|
const QString icon = QLatin1Literal("folder");
|
2014-10-10 23:01:21 +01:00
|
|
|
|
|
|
|
KIO::UDSEntry entry;
|
|
|
|
entry.insert(KIO::UDSEntry::UDS_NAME, "files");
|
|
|
|
entry.insert(KIO::UDSEntry::UDS_DISPLAY_NAME, name);
|
2016-08-29 19:31:52 +01:00
|
|
|
entry.insert(KIO::UDSEntry::UDS_ICON_NAME, icon);
|
2014-10-10 23:01:21 +01:00
|
|
|
entry.insert(KIO::UDSEntry::UDS_FILE_TYPE, S_IFDIR);
|
2016-08-29 19:31:52 +01:00
|
|
|
entry.insert(KIO::UDSEntry::UDS_ACCESS, S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
|
2014-10-10 23:01:21 +01:00
|
|
|
entry.insert(KIO::UDSEntry::UDS_MIME_TYPE, "");
|
2015-07-31 11:59:53 +01:00
|
|
|
entry.insert(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;
|
|
|
|
entry.insert(KIO::UDSEntry::UDS_NAME, QStringLiteral("."));
|
|
|
|
entry.insert(KIO::UDSEntry::UDS_FILE_TYPE, S_IFDIR);
|
|
|
|
entry.insert(KIO::UDSEntry::UDS_SIZE, 0);
|
|
|
|
entry.insert(KIO::UDSEntry::UDS_ACCESS, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH);
|
|
|
|
listEntry(entry);
|
|
|
|
|
2014-01-16 14:32:35 +00:00
|
|
|
infoMessage("");
|
|
|
|
finished();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-06-16 19:02:07 +01:00
|
|
|
void 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
|
|
|
|
2015-08-21 17:38:54 +01:00
|
|
|
/// Url is not used here because all we could care about the url is the host, and that's already
|
2014-01-16 14:32:35 +00:00
|
|
|
/// handled in @p setHost
|
|
|
|
Q_UNUSED(url);
|
|
|
|
|
|
|
|
if (!m_dbusInterface->isValid()) {
|
|
|
|
infoMessage(i18n("Could not contact background service."));
|
|
|
|
finished();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_currentDevice.isEmpty()) {
|
|
|
|
listAllDevices();
|
|
|
|
} else {
|
|
|
|
listDevice();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-16 19:02:07 +01:00
|
|
|
void 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;
|
|
|
|
entry.insert(KIO::UDSEntry::UDS_FILE_TYPE, S_IFDIR);
|
|
|
|
statEntry(entry);
|
|
|
|
|
|
|
|
finished();
|
|
|
|
}
|
|
|
|
|
2014-06-16 19:02:07 +01:00
|
|
|
void 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;
|
2014-01-16 14:32:35 +00:00
|
|
|
mimeType("");
|
|
|
|
finished();
|
|
|
|
}
|
|
|
|
|
|
|
|
void KioKdeconnect::setHost(const QString &hostName, quint16 port, const QString &user, const QString &pass)
|
|
|
|
{
|
|
|
|
|
|
|
|
//This is called before everything else to set the file we want to show
|
|
|
|
|
2014-09-21 23:20:14 +01:00
|
|
|
qCDebug(KDECONNECT_KIO) << "Setting host: " << hostName;
|
2014-01-16 14:32:35 +00:00
|
|
|
|
|
|
|
// In this kio only the hostname is used
|
|
|
|
Q_UNUSED(port)
|
|
|
|
Q_UNUSED(user)
|
|
|
|
Q_UNUSED(pass)
|
|
|
|
|
|
|
|
m_currentDevice = hostName;
|
|
|
|
|
|
|
|
}
|
|
|
|
|