2013-10-03 19:04:55 +04:00
|
|
|
/*
|
|
|
|
* Copyright (C) by Dominik Schmidt <dev@dominik-schmidt.de>
|
2014-07-14 17:28:26 +04:00
|
|
|
* Copyright (C) by Klaas Freitag <freitag@owncloud.com>
|
2015-03-16 18:26:35 +03:00
|
|
|
* Copyright (C) by Roeland Jago Douma <roeland@famdouma.nl>
|
2013-10-03 19:04:55 +04: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) any later version.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2014-07-11 02:31:24 +04:00
|
|
|
#include "socketapi.h"
|
2013-10-03 19:04:55 +04:00
|
|
|
|
2015-06-22 14:53:05 +03:00
|
|
|
#include "config.h"
|
2014-11-10 01:25:57 +03:00
|
|
|
#include "configfile.h"
|
2014-07-11 02:31:24 +04:00
|
|
|
#include "folderman.h"
|
|
|
|
#include "folder.h"
|
|
|
|
#include "theme.h"
|
2017-09-01 19:11:43 +03:00
|
|
|
#include "common/syncjournalfilerecord.h"
|
2016-03-17 14:26:44 +03:00
|
|
|
#include "syncengine.h"
|
2014-07-11 02:31:24 +04:00
|
|
|
#include "syncfileitem.h"
|
2014-09-18 19:08:53 +04:00
|
|
|
#include "filesystem.h"
|
2014-08-27 14:02:47 +04:00
|
|
|
#include "version.h"
|
2015-09-10 16:39:37 +03:00
|
|
|
#include "account.h"
|
2015-03-16 18:26:35 +03:00
|
|
|
#include "accountstate.h"
|
2015-10-06 10:39:24 +03:00
|
|
|
#include "account.h"
|
|
|
|
#include "capabilities.h"
|
2017-09-01 19:11:43 +03:00
|
|
|
#include "common/asserts.h"
|
2017-05-10 10:37:10 +03:00
|
|
|
#include "guiutility.h"
|
2013-10-03 19:04:55 +04:00
|
|
|
|
2017-05-10 10:37:10 +03:00
|
|
|
#include <array>
|
2017-01-11 20:27:22 +03:00
|
|
|
#include <QBitArray>
|
2013-10-03 19:04:55 +04:00
|
|
|
#include <QUrl>
|
2017-01-11 20:27:22 +03:00
|
|
|
#include <QMetaMethod>
|
2013-10-03 19:04:55 +04:00
|
|
|
#include <QMetaObject>
|
|
|
|
#include <QStringList>
|
2014-06-02 14:08:06 +04:00
|
|
|
#include <QScopedPointer>
|
2013-10-03 19:04:55 +04:00
|
|
|
#include <QFile>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QApplication>
|
2014-09-29 14:19:33 +04:00
|
|
|
#include <QLocalSocket>
|
2015-10-08 19:26:30 +03:00
|
|
|
#include <QStringBuilder>
|
2013-10-03 19:04:55 +04:00
|
|
|
|
2017-05-10 10:37:10 +03:00
|
|
|
#include <QClipboard>
|
|
|
|
|
2014-10-13 16:14:43 +04:00
|
|
|
#include <sqlite3.h>
|
|
|
|
|
2014-09-29 15:54:13 +04:00
|
|
|
#include <QStandardPaths>
|
|
|
|
|
2013-10-03 19:04:55 +04:00
|
|
|
|
2014-08-27 14:02:47 +04:00
|
|
|
// This is the version that is returned when the client asks for the VERSION.
|
|
|
|
// The first number should be changed if there is an incompatible change that breaks old clients.
|
|
|
|
// The second number should be changed when there are new features.
|
|
|
|
#define MIRALL_SOCKET_API_VERSION "1.0"
|
|
|
|
|
2016-05-06 13:32:01 +03:00
|
|
|
static inline QString removeTrailingSlash(QString path)
|
|
|
|
{
|
|
|
|
Q_ASSERT(path.endsWith(QLatin1Char('/')));
|
|
|
|
path.truncate(path.length() - 1);
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2017-01-11 20:27:22 +03:00
|
|
|
static QString buildMessage(const QString &verb, const QString &path, const QString &status = QString::null)
|
|
|
|
{
|
|
|
|
QString msg(verb);
|
|
|
|
|
|
|
|
if (!status.isEmpty()) {
|
|
|
|
msg.append(QLatin1Char(':'));
|
|
|
|
msg.append(status);
|
|
|
|
}
|
|
|
|
if (!path.isEmpty()) {
|
|
|
|
msg.append(QLatin1Char(':'));
|
|
|
|
QFileInfo fi(path);
|
|
|
|
msg.append(QDir::toNativeSeparators(fi.absoluteFilePath()));
|
|
|
|
}
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2014-11-10 00:34:07 +03:00
|
|
|
namespace OCC {
|
2013-10-03 19:04:55 +04:00
|
|
|
|
2017-05-09 15:24:11 +03:00
|
|
|
Q_LOGGING_CATEGORY(lcSocketApi, "gui.socketapi", QtInfoMsg)
|
|
|
|
|
2017-01-11 20:27:22 +03:00
|
|
|
class BloomFilter
|
|
|
|
{
|
|
|
|
// Initialize with m=1024 bits and k=2 (high and low 16 bits of a qHash).
|
|
|
|
// For a client navigating in less than 100 directories, this gives us a probability less than (1-e^(-2*100/1024))^2 = 0.03147872136 false positives.
|
|
|
|
const static int NumBits = 1024;
|
|
|
|
|
|
|
|
public:
|
|
|
|
BloomFilter()
|
|
|
|
: hashBits(NumBits)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void storeHash(uint hash)
|
|
|
|
{
|
|
|
|
hashBits.setBit((hash & 0xFFFF) % NumBits);
|
|
|
|
hashBits.setBit((hash >> 16) % NumBits);
|
|
|
|
}
|
|
|
|
bool isHashMaybeStored(uint hash) const
|
|
|
|
{
|
|
|
|
return hashBits.testBit((hash & 0xFFFF) % NumBits)
|
|
|
|
&& hashBits.testBit((hash >> 16) % NumBits);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
QBitArray hashBits;
|
|
|
|
};
|
|
|
|
|
|
|
|
class SocketListener
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
QIODevice *socket;
|
|
|
|
|
2017-02-08 13:39:28 +03:00
|
|
|
SocketListener(QIODevice *socket = 0)
|
|
|
|
: socket(socket)
|
|
|
|
{
|
|
|
|
}
|
2017-01-11 20:27:22 +03:00
|
|
|
|
|
|
|
void sendMessage(const QString &message, bool doWait = false) const
|
|
|
|
{
|
2017-07-04 17:41:40 +03:00
|
|
|
qCInfo(lcSocketApi) << "Sending SocketAPI message -->" << message << "to" << socket;
|
2017-01-11 20:27:22 +03:00
|
|
|
QString localMessage = message;
|
|
|
|
if (!localMessage.endsWith(QLatin1Char('\n'))) {
|
|
|
|
localMessage.append(QLatin1Char('\n'));
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray bytesToSend = localMessage.toUtf8();
|
|
|
|
qint64 sent = socket->write(bytesToSend);
|
|
|
|
if (doWait) {
|
|
|
|
socket->waitForBytesWritten(1000);
|
|
|
|
}
|
|
|
|
if (sent != bytesToSend.length()) {
|
2017-03-30 14:46:20 +03:00
|
|
|
qCWarning(lcSocketApi) << "Could not send all data on socket for " << localMessage;
|
2017-01-11 20:27:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void sendMessageIfDirectoryMonitored(const QString &message, uint systemDirectoryHash) const
|
|
|
|
{
|
|
|
|
if (_monitoredDirectoriesBloomFilter.isHashMaybeStored(systemDirectoryHash))
|
|
|
|
sendMessage(message, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void registerMonitoredDirectory(uint systemDirectoryHash)
|
|
|
|
{
|
|
|
|
_monitoredDirectoriesBloomFilter.storeHash(systemDirectoryHash);
|
|
|
|
}
|
2017-05-17 11:55:42 +03:00
|
|
|
|
2017-01-11 20:27:22 +03:00
|
|
|
private:
|
|
|
|
BloomFilter _monitoredDirectoriesBloomFilter;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ListenerHasSocketPred
|
|
|
|
{
|
|
|
|
QIODevice *socket;
|
|
|
|
ListenerHasSocketPred(QIODevice *socket)
|
|
|
|
: socket(socket)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
bool operator()(const SocketListener &listener) const { return listener.socket == socket; }
|
|
|
|
};
|
2013-10-03 19:04:55 +04:00
|
|
|
|
2014-07-17 17:00:21 +04:00
|
|
|
SocketApi::SocketApi(QObject *parent)
|
2013-10-03 19:04:55 +04:00
|
|
|
: QObject(parent)
|
2014-06-19 16:08:30 +04:00
|
|
|
{
|
2014-09-29 14:19:33 +04:00
|
|
|
QString socketPath;
|
2014-09-29 15:54:13 +04:00
|
|
|
|
2014-09-29 14:19:33 +04:00
|
|
|
if (Utility::isWindows()) {
|
|
|
|
socketPath = QLatin1String("\\\\.\\pipe\\")
|
2016-06-27 16:13:08 +03:00
|
|
|
+ QLatin1String("ownCloud-")
|
2016-04-20 17:51:17 +03:00
|
|
|
+ QString::fromLocal8Bit(qgetenv("USERNAME"));
|
2014-10-24 01:46:17 +04:00
|
|
|
// TODO: once the windows extension supports multiple
|
|
|
|
// client connections, switch back to the theme name
|
|
|
|
// See issue #2388
|
|
|
|
// + Theme::instance()->appName();
|
2014-09-30 09:36:20 +04:00
|
|
|
} else if (Utility::isMac()) {
|
2015-06-22 14:53:05 +03:00
|
|
|
// This must match the code signing Team setting of the extension
|
|
|
|
// Example for developer builds (with ad-hoc signing identity): "" "com.owncloud.desktopclient" ".socketApi"
|
|
|
|
// Example for official signed packages: "9B5WD74GWJ." "com.owncloud.desktopclient" ".socketApi"
|
|
|
|
socketPath = SOCKETAPI_TEAM_IDENTIFIER_PREFIX APPLICATION_REV_DOMAIN ".socketApi";
|
2015-01-09 00:40:47 +03:00
|
|
|
} else if (Utility::isLinux() || Utility::isBSD()) {
|
2014-09-29 15:54:13 +04:00
|
|
|
QString runtimeDir;
|
|
|
|
runtimeDir = QStandardPaths::writableLocation(QStandardPaths::RuntimeLocation);
|
|
|
|
socketPath = runtimeDir + "/" + Theme::instance()->appName() + "/socket";
|
2014-09-30 13:15:27 +04:00
|
|
|
} else {
|
2017-03-30 14:46:20 +03:00
|
|
|
qCWarning(lcSocketApi) << "An unexpected system detected, this probably won't work.";
|
2014-06-19 17:35:29 +04:00
|
|
|
}
|
2014-06-19 16:08:30 +04:00
|
|
|
|
2015-06-15 15:51:11 +03:00
|
|
|
SocketApiServer::removeServer(socketPath);
|
2014-09-29 15:54:13 +04:00
|
|
|
QFileInfo info(socketPath);
|
|
|
|
if (!info.dir().exists()) {
|
|
|
|
bool result = info.dir().mkpath(".");
|
2017-05-09 15:24:11 +03:00
|
|
|
qCDebug(lcSocketApi) << "creating" << info.dir().path() << result;
|
2014-09-30 13:16:49 +04:00
|
|
|
if (result) {
|
|
|
|
QFile::setPermissions(socketPath,
|
|
|
|
QFile::Permissions(QFile::ReadOwner + QFile::WriteOwner + QFile::ExeOwner));
|
|
|
|
}
|
2014-06-19 16:08:30 +04:00
|
|
|
}
|
2014-09-29 14:19:33 +04:00
|
|
|
if (!_localServer.listen(socketPath)) {
|
2017-03-30 14:46:20 +03:00
|
|
|
qCWarning(lcSocketApi) << "can't start server" << socketPath;
|
2014-06-19 16:08:30 +04:00
|
|
|
} else {
|
2017-03-30 14:46:20 +03:00
|
|
|
qCInfo(lcSocketApi) << "server started, listening at " << socketPath;
|
2014-06-19 16:08:30 +04:00
|
|
|
}
|
2014-06-19 17:35:29 +04:00
|
|
|
|
2017-09-23 14:42:39 +03:00
|
|
|
connect(&_localServer, &SocketApiServer::newConnection, this, &SocketApi::slotNewConnection);
|
2013-10-03 19:04:55 +04:00
|
|
|
|
|
|
|
// folder watcher
|
2017-09-20 11:14:48 +03:00
|
|
|
connect(FolderMan::instance(), &FolderMan::folderSyncStateChange, this, &SocketApi::slotUpdateFolderView);
|
2013-10-03 19:04:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
SocketApi::~SocketApi()
|
|
|
|
{
|
2017-05-09 15:24:11 +03:00
|
|
|
qCDebug(lcSocketApi) << "dtor";
|
2014-09-29 14:19:33 +04:00
|
|
|
_localServer.close();
|
2015-04-01 15:40:34 +03:00
|
|
|
// All remaining sockets will be destroyed with _localServer, their parent
|
2017-02-07 15:52:15 +03:00
|
|
|
ASSERT(_listeners.isEmpty() || _listeners.first().socket->parent() == &_localServer);
|
2015-04-01 15:40:34 +03:00
|
|
|
_listeners.clear();
|
2013-10-03 19:04:55 +04:00
|
|
|
}
|
|
|
|
|
2014-06-02 14:08:06 +04:00
|
|
|
void SocketApi::slotNewConnection()
|
2013-10-03 19:04:55 +04:00
|
|
|
{
|
2015-06-15 15:51:11 +03:00
|
|
|
QIODevice *socket = _localServer.nextPendingConnection();
|
2014-07-10 17:50:24 +04:00
|
|
|
|
2014-06-02 14:08:06 +04:00
|
|
|
if (!socket) {
|
|
|
|
return;
|
|
|
|
}
|
2017-03-30 14:46:20 +03:00
|
|
|
qCInfo(lcSocketApi) << "New connection" << socket;
|
2017-09-20 11:14:48 +03:00
|
|
|
connect(socket, &QIODevice::readyRead, this, &SocketApi::slotReadSocket);
|
2013-10-03 19:04:55 +04:00
|
|
|
connect(socket, SIGNAL(disconnected()), this, SLOT(onLostConnection()));
|
2017-09-20 11:14:48 +03:00
|
|
|
connect(socket, &QObject::destroyed, this, &SocketApi::slotSocketDestroyed);
|
2017-02-07 15:52:15 +03:00
|
|
|
ASSERT(socket->readAll().isEmpty());
|
2013-10-03 19:04:55 +04:00
|
|
|
|
2017-01-11 20:27:22 +03:00
|
|
|
_listeners.append(SocketListener(socket));
|
|
|
|
SocketListener &listener = _listeners.last();
|
2014-07-15 19:55:55 +04:00
|
|
|
|
2015-03-13 20:30:45 +03:00
|
|
|
foreach (Folder *f, FolderMan::instance()->map()) {
|
2016-04-28 23:43:53 +03:00
|
|
|
if (f->canSync()) {
|
2016-05-06 13:32:01 +03:00
|
|
|
QString message = buildRegisterPathMessage(removeTrailingSlash(f->path()));
|
2017-01-11 20:27:22 +03:00
|
|
|
listener.sendMessage(message);
|
2016-04-28 23:43:53 +03:00
|
|
|
}
|
2014-07-15 19:55:55 +04:00
|
|
|
}
|
2013-10-03 19:04:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void SocketApi::onLostConnection()
|
|
|
|
{
|
2017-03-30 14:46:20 +03:00
|
|
|
qCInfo(lcSocketApi) << "Lost connection " << sender();
|
2017-01-11 20:27:22 +03:00
|
|
|
sender()->deleteLater();
|
2013-10-03 19:04:55 +04:00
|
|
|
}
|
|
|
|
|
2017-01-11 20:27:22 +03:00
|
|
|
void SocketApi::slotSocketDestroyed(QObject *obj)
|
|
|
|
{
|
|
|
|
QIODevice *socket = static_cast<QIODevice *>(obj);
|
|
|
|
_listeners.erase(std::remove_if(_listeners.begin(), _listeners.end(), ListenerHasSocketPred(socket)), _listeners.end());
|
|
|
|
}
|
2013-10-03 19:04:55 +04:00
|
|
|
|
2014-06-02 14:08:06 +04:00
|
|
|
void SocketApi::slotReadSocket()
|
2013-10-03 19:04:55 +04:00
|
|
|
{
|
2015-06-15 15:51:11 +03:00
|
|
|
QIODevice *socket = qobject_cast<QIODevice *>(sender());
|
2017-02-07 15:52:15 +03:00
|
|
|
ASSERT(socket);
|
2017-01-11 20:27:22 +03:00
|
|
|
SocketListener *listener = &*std::find_if(_listeners.begin(), _listeners.end(), ListenerHasSocketPred(socket));
|
2013-10-03 19:04:55 +04:00
|
|
|
|
2014-06-02 14:08:06 +04:00
|
|
|
while (socket->canReadLine()) {
|
2016-06-15 20:37:35 +03:00
|
|
|
// Make sure to normalize the input from the socket to
|
|
|
|
// make sure that the path will match, especially on OS X.
|
|
|
|
QString line = QString::fromUtf8(socket->readLine()).normalized(QString::NormalizationForm_C);
|
2015-10-26 12:06:10 +03:00
|
|
|
line.chop(1); // remove the '\n'
|
2017-07-04 17:41:40 +03:00
|
|
|
qCInfo(lcSocketApi) << "Received SocketAPI message <--" << line << "from" << socket;
|
2017-01-11 20:31:08 +03:00
|
|
|
QByteArray command = line.split(":").value(0).toAscii();
|
|
|
|
QByteArray functionWithArguments = "command_" + command + "(QString,SocketListener*)";
|
|
|
|
int indexOfMethod = staticMetaObject.indexOfMethod(functionWithArguments);
|
2013-10-03 19:04:55 +04:00
|
|
|
|
2015-10-26 12:06:10 +03:00
|
|
|
QString argument = line.remove(0, command.length() + 1);
|
2014-06-02 14:08:06 +04:00
|
|
|
if (indexOfMethod != -1) {
|
2017-01-11 20:27:22 +03:00
|
|
|
staticMetaObject.method(indexOfMethod).invoke(this, Q_ARG(QString, argument), Q_ARG(SocketListener *, listener));
|
2014-06-02 14:08:06 +04:00
|
|
|
} else {
|
2017-03-30 14:46:20 +03:00
|
|
|
qCWarning(lcSocketApi) << "The command is not supported by this version of the client:" << command << "with argument:" << argument;
|
2013-10-03 19:04:55 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-25 14:10:45 +04:00
|
|
|
void SocketApi::slotRegisterPath(const QString &alias)
|
2013-10-03 19:04:55 +04:00
|
|
|
{
|
2016-04-28 23:43:53 +03:00
|
|
|
// Make sure not to register twice to each connected client
|
|
|
|
if (_registeredAliases.contains(alias))
|
|
|
|
return;
|
|
|
|
|
2014-07-25 14:10:45 +04:00
|
|
|
Folder *f = FolderMan::instance()->folder(alias);
|
|
|
|
if (f) {
|
2016-05-06 13:32:01 +03:00
|
|
|
QString message = buildRegisterPathMessage(removeTrailingSlash(f->path()));
|
2017-01-11 20:27:22 +03:00
|
|
|
foreach (auto &listener, _listeners) {
|
|
|
|
listener.sendMessage(message);
|
2015-03-13 20:30:45 +03:00
|
|
|
}
|
2014-07-25 14:10:45 +04:00
|
|
|
}
|
2016-04-28 23:43:53 +03:00
|
|
|
|
|
|
|
_registeredAliases.insert(alias);
|
2014-07-25 14:10:45 +04:00
|
|
|
}
|
2014-07-11 13:30:47 +04:00
|
|
|
|
2014-07-25 14:10:45 +04:00
|
|
|
void SocketApi::slotUnregisterPath(const QString &alias)
|
|
|
|
{
|
2016-04-28 23:43:53 +03:00
|
|
|
if (!_registeredAliases.contains(alias))
|
|
|
|
return;
|
|
|
|
|
2014-07-11 13:30:47 +04:00
|
|
|
Folder *f = FolderMan::instance()->folder(alias);
|
2016-03-17 22:58:51 +03:00
|
|
|
if (f)
|
2017-01-11 20:27:22 +03:00
|
|
|
broadcastMessage(buildMessage(QLatin1String("UNREGISTER_PATH"), removeTrailingSlash(f->path()), QString::null), true);
|
2016-04-28 23:43:53 +03:00
|
|
|
|
|
|
|
_registeredAliases.remove(alias);
|
2014-07-25 14:10:45 +04:00
|
|
|
}
|
2014-07-11 13:30:47 +04:00
|
|
|
|
2015-05-12 16:50:38 +03:00
|
|
|
void SocketApi::slotUpdateFolderView(Folder *f)
|
2014-07-25 14:10:45 +04:00
|
|
|
{
|
2014-10-21 17:26:51 +04:00
|
|
|
if (_listeners.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-25 14:10:45 +04:00
|
|
|
if (f) {
|
|
|
|
// do only send UPDATE_VIEW for a couple of status
|
2017-05-17 11:54:57 +03:00
|
|
|
if (f->syncResult().status() == SyncResult::SyncPrepare
|
|
|
|
|| f->syncResult().status() == SyncResult::Success
|
|
|
|
|| f->syncResult().status() == SyncResult::Paused
|
|
|
|
|| f->syncResult().status() == SyncResult::Problem
|
|
|
|
|| f->syncResult().status() == SyncResult::Error
|
|
|
|
|| f->syncResult().status() == SyncResult::SetupError) {
|
2016-05-06 13:32:01 +03:00
|
|
|
QString rootPath = removeTrailingSlash(f->path());
|
2017-01-11 20:27:22 +03:00
|
|
|
broadcastStatusPushMessage(rootPath, f->syncEngine().syncFileStatusTracker().fileStatus(""));
|
2014-10-21 17:26:51 +04:00
|
|
|
|
2017-01-11 20:27:22 +03:00
|
|
|
broadcastMessage(buildMessage(QLatin1String("UPDATE_VIEW"), rootPath));
|
2014-10-13 19:23:42 +04:00
|
|
|
} else {
|
2017-05-09 15:24:11 +03:00
|
|
|
qCDebug(lcSocketApi) << "Not sending UPDATE_VIEW for" << f->alias() << "because status() is" << f->syncResult().status();
|
2014-07-25 14:10:45 +04:00
|
|
|
}
|
|
|
|
}
|
2013-10-03 19:04:55 +04:00
|
|
|
}
|
|
|
|
|
2017-01-11 20:27:22 +03:00
|
|
|
void SocketApi::broadcastMessage(const QString &msg, bool doWait)
|
2013-10-03 19:04:55 +04:00
|
|
|
{
|
2017-01-11 20:27:22 +03:00
|
|
|
foreach (auto &listener, _listeners) {
|
|
|
|
listener.sendMessage(msg, doWait);
|
2014-07-25 14:10:45 +04:00
|
|
|
}
|
2013-10-03 19:04:55 +04:00
|
|
|
}
|
|
|
|
|
2017-01-11 20:27:22 +03:00
|
|
|
void SocketApi::broadcastStatusPushMessage(const QString &systemPath, SyncFileStatus fileStatus)
|
2013-10-03 19:04:55 +04:00
|
|
|
{
|
2017-01-11 20:27:22 +03:00
|
|
|
QString msg = buildMessage(QLatin1String("STATUS"), systemPath, fileStatus.toSocketAPIString());
|
|
|
|
Q_ASSERT(!systemPath.endsWith('/'));
|
|
|
|
uint directoryHash = qHash(systemPath.left(systemPath.lastIndexOf('/')));
|
|
|
|
foreach (auto &listener, _listeners) {
|
|
|
|
listener.sendMessageIfDirectoryMonitored(msg, directoryHash);
|
2013-10-03 19:04:55 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-11 20:27:22 +03:00
|
|
|
void SocketApi::command_RETRIEVE_FOLDER_STATUS(const QString &argument, SocketListener *listener)
|
2013-10-03 19:04:55 +04:00
|
|
|
{
|
2014-06-19 17:02:27 +04:00
|
|
|
// This command is the same as RETRIEVE_FILE_STATUS
|
2017-01-11 20:27:22 +03:00
|
|
|
command_RETRIEVE_FILE_STATUS(argument, listener);
|
2014-06-06 17:37:04 +04:00
|
|
|
}
|
|
|
|
|
2017-01-11 20:27:22 +03:00
|
|
|
void SocketApi::command_RETRIEVE_FILE_STATUS(const QString &argument, SocketListener *listener)
|
2014-06-06 17:37:04 +04:00
|
|
|
{
|
|
|
|
QString statusString;
|
|
|
|
|
2014-07-10 16:27:52 +04:00
|
|
|
Folder *syncFolder = FolderMan::instance()->folderForPath(argument);
|
|
|
|
if (!syncFolder) {
|
|
|
|
// this can happen in offline mode e.g.: nothing to worry about
|
2016-04-28 23:43:53 +03:00
|
|
|
statusString = QLatin1String("NOP");
|
2014-07-10 16:27:52 +04:00
|
|
|
} else {
|
2017-01-11 20:27:22 +03:00
|
|
|
QString systemPath = QDir::cleanPath(argument);
|
|
|
|
if (systemPath.endsWith(QLatin1Char('/'))) {
|
|
|
|
systemPath.truncate(systemPath.length() - 1);
|
2017-05-09 15:24:11 +03:00
|
|
|
qCWarning(lcSocketApi) << "Removed trailing slash for directory: " << systemPath << "Status pushes won't have one.";
|
2016-05-06 13:32:01 +03:00
|
|
|
}
|
2017-01-11 20:27:22 +03:00
|
|
|
// The user probably visited this directory in the file shell.
|
|
|
|
// Let the listener know that it should now send status pushes for sibblings of this file.
|
|
|
|
QString directory = systemPath.left(systemPath.lastIndexOf('/'));
|
|
|
|
listener->registerMonitoredDirectory(qHash(directory));
|
2014-06-06 17:37:04 +04:00
|
|
|
|
2017-01-11 20:27:22 +03:00
|
|
|
QString relativePath = systemPath.mid(syncFolder->cleanPath().length() + 1);
|
|
|
|
SyncFileStatus fileStatus = syncFolder->syncEngine().syncFileStatusTracker().fileStatus(relativePath);
|
2016-04-28 23:43:53 +03:00
|
|
|
statusString = fileStatus.toSocketAPIString();
|
2013-10-03 19:04:55 +04:00
|
|
|
}
|
2014-06-06 17:37:04 +04:00
|
|
|
|
2015-10-08 19:26:30 +03:00
|
|
|
const QString message = QLatin1String("STATUS:") % statusString % QLatin1Char(':') % QDir::toNativeSeparators(argument);
|
2017-01-11 20:27:22 +03:00
|
|
|
listener->sendMessage(message);
|
2013-10-03 19:04:55 +04:00
|
|
|
}
|
|
|
|
|
2017-01-11 20:27:22 +03:00
|
|
|
void SocketApi::command_SHARE(const QString &localFile, SocketListener *listener)
|
2014-09-03 18:12:21 +04:00
|
|
|
{
|
2016-02-22 15:53:45 +03:00
|
|
|
auto theme = Theme::instance();
|
|
|
|
|
2015-01-21 17:03:01 +03:00
|
|
|
Folder *shareFolder = FolderMan::instance()->folderForPath(localFile);
|
2014-12-23 18:30:37 +03:00
|
|
|
if (!shareFolder) {
|
2015-01-29 20:09:46 +03:00
|
|
|
const QString message = QLatin1String("SHARE:NOP:") + QDir::toNativeSeparators(localFile);
|
|
|
|
// files that are not within a sync folder are not synced.
|
2017-01-11 20:27:22 +03:00
|
|
|
listener->sendMessage(message);
|
2015-03-16 18:26:35 +03:00
|
|
|
} else if (!shareFolder->accountState()->isConnected()) {
|
|
|
|
const QString message = QLatin1String("SHARE:NOTCONNECTED:") + QDir::toNativeSeparators(localFile);
|
|
|
|
// if the folder isn't connected, don't open the share dialog
|
2017-01-11 20:27:22 +03:00
|
|
|
listener->sendMessage(message);
|
2017-03-14 17:57:57 +03:00
|
|
|
} else if (!theme->linkSharing() && (!theme->userGroupSharing() || shareFolder->accountState()->account()->serverVersionInt() < Account::makeServerVersion(8, 2, 0))) {
|
2016-02-22 15:53:45 +03:00
|
|
|
const QString message = QLatin1String("SHARE:NOP:") + QDir::toNativeSeparators(localFile);
|
2017-01-11 20:27:22 +03:00
|
|
|
listener->sendMessage(message);
|
2014-12-23 18:30:37 +03:00
|
|
|
} else {
|
2016-01-04 13:57:51 +03:00
|
|
|
const QString localFileClean = QDir::cleanPath(localFile);
|
|
|
|
const QString file = localFileClean.mid(shareFolder->cleanPath().length() + 1);
|
2016-03-17 14:26:44 +03:00
|
|
|
SyncFileStatus fileStatus = shareFolder->syncEngine().syncFileStatusTracker().fileStatus(file);
|
2015-11-20 11:26:45 +03:00
|
|
|
|
|
|
|
// Verify the file is on the server (to our knowledge of course)
|
2016-03-24 20:20:49 +03:00
|
|
|
if (fileStatus.tag() != SyncFileStatus::StatusUpToDate) {
|
2015-11-20 11:26:45 +03:00
|
|
|
const QString message = QLatin1String("SHARE:NOTSYNCED:") + QDir::toNativeSeparators(localFile);
|
2017-01-11 20:27:22 +03:00
|
|
|
listener->sendMessage(message);
|
2015-11-20 11:26:45 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-21 16:10:37 +03:00
|
|
|
const QString remotePath = QDir(shareFolder->remotePath()).filePath(file);
|
2015-03-11 16:09:31 +03:00
|
|
|
|
2015-07-24 11:02:18 +03:00
|
|
|
// Can't share root folder
|
2016-01-04 13:57:51 +03:00
|
|
|
if (remotePath == "/") {
|
2015-11-20 11:26:45 +03:00
|
|
|
const QString message = QLatin1String("SHARE:CANNOTSHAREROOT:") + QDir::toNativeSeparators(localFile);
|
2017-01-11 20:27:22 +03:00
|
|
|
listener->sendMessage(message);
|
2015-07-24 11:02:18 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-21 17:03:01 +03:00
|
|
|
const QString message = QLatin1String("SHARE:OK:") + QDir::toNativeSeparators(localFile);
|
2017-01-11 20:27:22 +03:00
|
|
|
listener->sendMessage(message);
|
2015-01-14 12:35:09 +03:00
|
|
|
|
2017-05-10 10:37:10 +03:00
|
|
|
emit shareCommandReceived(remotePath, localFileClean);
|
2014-12-23 18:30:37 +03:00
|
|
|
}
|
2014-09-03 18:12:21 +04:00
|
|
|
}
|
|
|
|
|
2017-01-11 20:27:22 +03:00
|
|
|
void SocketApi::command_VERSION(const QString &, SocketListener *listener)
|
2014-08-27 14:02:47 +04:00
|
|
|
{
|
2017-01-11 20:27:22 +03:00
|
|
|
listener->sendMessage(QLatin1String("VERSION:" MIRALL_VERSION_STRING ":" MIRALL_SOCKET_API_VERSION));
|
2014-08-27 14:02:47 +04:00
|
|
|
}
|
|
|
|
|
2017-01-11 20:27:22 +03:00
|
|
|
void SocketApi::command_SHARE_STATUS(const QString &localFile, SocketListener *listener)
|
2015-10-06 10:39:24 +03:00
|
|
|
{
|
|
|
|
Folder *shareFolder = FolderMan::instance()->folderForPath(localFile);
|
|
|
|
|
|
|
|
if (!shareFolder) {
|
|
|
|
const QString message = QLatin1String("SHARE_STATUS:NOP:") + QDir::toNativeSeparators(localFile);
|
2017-01-11 20:27:22 +03:00
|
|
|
listener->sendMessage(message);
|
2015-10-06 10:39:24 +03:00
|
|
|
} else {
|
2015-11-25 11:40:21 +03:00
|
|
|
const QString file = QDir::cleanPath(localFile).mid(shareFolder->cleanPath().length() + 1);
|
2016-03-17 14:26:44 +03:00
|
|
|
SyncFileStatus fileStatus = shareFolder->syncEngine().syncFileStatusTracker().fileStatus(file);
|
2015-11-25 11:40:21 +03:00
|
|
|
|
|
|
|
// Verify the file is on the server (to our knowledge of course)
|
2016-03-24 20:20:49 +03:00
|
|
|
if (fileStatus.tag() != SyncFileStatus::StatusUpToDate) {
|
2015-11-25 11:40:21 +03:00
|
|
|
const QString message = QLatin1String("SHARE_STATUS:NOTSYNCED:") + QDir::toNativeSeparators(localFile);
|
2017-01-11 20:27:22 +03:00
|
|
|
listener->sendMessage(message);
|
2015-11-25 11:40:21 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-06 10:39:24 +03:00
|
|
|
const Capabilities capabilities = shareFolder->accountState()->account()->capabilities();
|
|
|
|
|
|
|
|
if (!capabilities.shareAPI()) {
|
|
|
|
const QString message = QLatin1String("SHARE_STATUS:DISABLED:") + QDir::toNativeSeparators(localFile);
|
2017-01-11 20:27:22 +03:00
|
|
|
listener->sendMessage(message);
|
2015-10-06 10:39:24 +03:00
|
|
|
} else {
|
2016-02-22 15:53:45 +03:00
|
|
|
auto theme = Theme::instance();
|
|
|
|
QString available;
|
2015-10-06 10:39:24 +03:00
|
|
|
|
2016-02-22 15:53:45 +03:00
|
|
|
if (theme->userGroupSharing()) {
|
|
|
|
available = "USER,GROUP";
|
2015-10-06 10:39:24 +03:00
|
|
|
}
|
|
|
|
|
2016-02-22 15:53:45 +03:00
|
|
|
if (theme->linkSharing() && capabilities.sharePublicLink()) {
|
|
|
|
if (available.isEmpty()) {
|
|
|
|
available = "LINK";
|
|
|
|
} else {
|
|
|
|
available += ",LINK";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (available.isEmpty()) {
|
|
|
|
const QString message = QLatin1String("SHARE_STATUS:DISABLED") + ":" + QDir::toNativeSeparators(localFile);
|
2017-01-11 20:27:22 +03:00
|
|
|
listener->sendMessage(message);
|
2016-02-22 15:53:45 +03:00
|
|
|
} else {
|
|
|
|
const QString message = QLatin1String("SHARE_STATUS:") + available + ":" + QDir::toNativeSeparators(localFile);
|
2017-01-11 20:27:22 +03:00
|
|
|
listener->sendMessage(message);
|
2016-02-22 15:53:45 +03:00
|
|
|
}
|
2015-10-06 10:39:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-11 20:27:22 +03:00
|
|
|
void SocketApi::command_SHARE_MENU_TITLE(const QString &, SocketListener *listener)
|
2015-01-23 18:09:38 +03:00
|
|
|
{
|
2017-01-11 20:27:22 +03:00
|
|
|
listener->sendMessage(QLatin1String("SHARE_MENU_TITLE:") + tr("Share with %1", "parameter is ownCloud").arg(Theme::instance()->appNameGUI()));
|
2015-01-23 18:09:38 +03:00
|
|
|
}
|
|
|
|
|
2017-09-15 15:24:34 +03:00
|
|
|
// Fetches the private link url asynchronously and then calls the target slot
|
|
|
|
void fetchPrivateLinkUrl(const QString &localFile, SocketApi *target, void (SocketApi::*targetFun)(const QString &url) const)
|
2017-05-10 10:37:10 +03:00
|
|
|
{
|
2017-09-15 15:24:34 +03:00
|
|
|
Folder *shareFolder = FolderMan::instance()->folderForPath(localFile);
|
|
|
|
if (!shareFolder) {
|
|
|
|
qCWarning(lcSocketApi) << "Unknown path" << localFile;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString localFileClean = QDir::cleanPath(localFile);
|
|
|
|
const QString file = localFileClean.mid(shareFolder->cleanPath().length() + 1);
|
|
|
|
|
2017-11-10 11:22:41 +03:00
|
|
|
AccountPtr account = shareFolder->accountState()->account();
|
|
|
|
|
2017-09-15 15:24:34 +03:00
|
|
|
// Generate private link ourselves: used as a fallback
|
2017-09-13 20:02:38 +03:00
|
|
|
SyncJournalFileRecord rec;
|
|
|
|
if (!shareFolder->journalDb()->getFileRecord(file, &rec) || !rec.isValid())
|
2017-09-15 15:24:34 +03:00
|
|
|
return;
|
|
|
|
const QString oldUrl =
|
2017-11-10 11:22:41 +03:00
|
|
|
account->deprecatedPrivateLinkUrl(rec.numericFileId()).toString(QUrl::FullyEncoded);
|
|
|
|
|
|
|
|
// Retrieve the new link or numeric file id by PROPFIND
|
|
|
|
PropfindJob *job = new PropfindJob(account, file, target);
|
|
|
|
job->setProperties(
|
|
|
|
QList<QByteArray>()
|
|
|
|
<< "http://owncloud.org/ns:fileid" // numeric file id for fallback private link generation
|
|
|
|
<< "http://owncloud.org/ns:privatelink");
|
2017-09-15 15:24:34 +03:00
|
|
|
job->setTimeout(10 * 1000);
|
|
|
|
QObject::connect(job, &PropfindJob::result, target, [=](const QVariantMap &result) {
|
|
|
|
auto privateLinkUrl = result["privatelink"].toString();
|
2017-11-10 11:22:41 +03:00
|
|
|
auto numericFileId = result["fileid"].toByteArray();
|
2017-09-15 15:24:34 +03:00
|
|
|
if (!privateLinkUrl.isEmpty()) {
|
|
|
|
(target->*targetFun)(privateLinkUrl);
|
2017-11-10 11:22:41 +03:00
|
|
|
} else if (!numericFileId.isEmpty()) {
|
|
|
|
(target->*targetFun)(account->deprecatedPrivateLinkUrl(numericFileId).toString(QUrl::FullyEncoded));
|
2017-09-15 15:24:34 +03:00
|
|
|
} else {
|
|
|
|
(target->*targetFun)(oldUrl);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
QObject::connect(job, &PropfindJob::finishedWithError, target, [=](QNetworkReply *) {
|
|
|
|
(target->*targetFun)(oldUrl);
|
|
|
|
});
|
|
|
|
job->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SocketApi::command_COPY_PRIVATE_LINK(const QString &localFile, SocketListener *)
|
|
|
|
{
|
|
|
|
fetchPrivateLinkUrl(localFile, this, &SocketApi::copyPrivateLinkToClipboard);
|
2017-05-10 10:37:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void SocketApi::command_EMAIL_PRIVATE_LINK(const QString &localFile, SocketListener *)
|
|
|
|
{
|
2017-09-15 15:24:34 +03:00
|
|
|
fetchPrivateLinkUrl(localFile, this, &SocketApi::emailPrivateLink);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SocketApi::copyPrivateLinkToClipboard(const QString &link) const
|
|
|
|
{
|
|
|
|
QApplication::clipboard()->setText(link);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SocketApi::emailPrivateLink(const QString &link) const
|
|
|
|
{
|
|
|
|
Utility::openEmailComposer(
|
|
|
|
tr("I shared something with you"),
|
|
|
|
link,
|
|
|
|
0);
|
2017-05-10 10:37:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void SocketApi::command_GET_STRINGS(const QString &, SocketListener *listener)
|
|
|
|
{
|
|
|
|
static std::array<std::pair<const char *, QString>, 5> strings { {
|
2017-07-06 12:33:38 +03:00
|
|
|
{ "SHARE_MENU_TITLE", tr("Share...") },
|
2017-05-10 10:37:10 +03:00
|
|
|
{ "CONTEXT_MENU_TITLE", Theme::instance()->appNameGUI() },
|
2017-07-06 12:32:58 +03:00
|
|
|
{ "COPY_PRIVATE_LINK_MENU_TITLE", tr("Copy private link to clipboard") },
|
|
|
|
{ "EMAIL_PRIVATE_LINK_MENU_TITLE", tr("Send private link by email...") },
|
2017-05-10 10:37:10 +03:00
|
|
|
} };
|
2017-07-04 17:45:48 +03:00
|
|
|
listener->sendMessage(QString("GET_STRINGS:BEGIN"));
|
2017-05-10 10:37:10 +03:00
|
|
|
for (auto key_value : strings) {
|
|
|
|
listener->sendMessage(QString("STRING:%1:%2").arg(key_value.first, key_value.second));
|
|
|
|
}
|
2017-07-04 17:45:48 +03:00
|
|
|
listener->sendMessage(QString("GET_STRINGS:END"));
|
2017-05-10 10:37:10 +03:00
|
|
|
}
|
|
|
|
|
2015-03-13 20:30:45 +03:00
|
|
|
QString SocketApi::buildRegisterPathMessage(const QString &path)
|
|
|
|
{
|
|
|
|
QFileInfo fi(path);
|
|
|
|
QString message = QLatin1String("REGISTER_PATH:");
|
|
|
|
message.append(QDir::toNativeSeparators(fi.absoluteFilePath()));
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
2014-11-10 00:34:07 +03:00
|
|
|
} // namespace OCC
|