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"
|
2017-03-30 14:24:04 +03:00
|
|
|
#include "socketapi_p.h"
|
2013-10-03 19:04:55 +04:00
|
|
|
|
2020-10-07 18:51:57 +03:00
|
|
|
#include "conflictdialog.h"
|
2020-10-07 17:30:43 +03:00
|
|
|
#include "conflictsolver.h"
|
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"
|
2018-04-06 18:13:29 +03:00
|
|
|
#ifndef OWNCLOUD_TEST
|
|
|
|
#include "sharemanager.h"
|
|
|
|
#endif
|
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>
|
2018-04-06 18:13:29 +03:00
|
|
|
#include <QMessageBox>
|
2020-01-09 17:10:01 +03:00
|
|
|
#include <QInputDialog>
|
2020-10-07 15:51:04 +03:00
|
|
|
#include <QFileDialog>
|
2017-03-30 14:24:04 +03:00
|
|
|
|
|
|
|
|
|
|
|
#include <QAction>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QWidget>
|
|
|
|
|
2017-05-10 10:37:10 +03:00
|
|
|
#include <QClipboard>
|
2020-03-03 20:45:58 +03:00
|
|
|
#include <QDesktopServices>
|
2017-05-10 10:37:10 +03:00
|
|
|
|
2020-05-12 15:10:02 +03:00
|
|
|
#include <QProcess>
|
2014-09-29 15:54:13 +04:00
|
|
|
#include <QStandardPaths>
|
|
|
|
|
2018-08-31 16:03:33 +03:00
|
|
|
#ifdef Q_OS_MAC
|
|
|
|
#include <CoreFoundation/CoreFoundation.h>
|
|
|
|
#endif
|
2013-10-03 19:04:55 +04:00
|
|
|
|
2017-03-30 14:24:04 +03: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.
|
2018-01-18 17:17:29 +03:00
|
|
|
#define MIRALL_SOCKET_API_VERSION "1.1"
|
2017-03-30 14:24:04 +03:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
#if GUI_TESTING
|
2019-06-05 22:04:16 +03:00
|
|
|
|
2019-06-06 12:48:31 +03:00
|
|
|
using namespace OCC;
|
|
|
|
|
2019-06-05 22:04:16 +03:00
|
|
|
QList<QObject*> allObjects(const QList<QWidget*> &widgets) {
|
|
|
|
QList<QObject*> objects;
|
|
|
|
std::copy(widgets.constBegin(), widgets.constEnd(), std::back_inserter(objects));
|
|
|
|
|
|
|
|
objects << qApp;
|
|
|
|
|
|
|
|
return objects;
|
|
|
|
}
|
|
|
|
|
|
|
|
QObject *findWidget(const QString &queryString, const QList<QWidget*> &widgets = QApplication::allWidgets())
|
2017-03-30 14:24:04 +03:00
|
|
|
{
|
2019-06-05 22:04:16 +03:00
|
|
|
auto objects = allObjects(widgets);
|
2017-03-30 14:24:04 +03:00
|
|
|
|
2019-06-05 22:04:16 +03:00
|
|
|
QList<QObject*>::const_iterator foundWidget;
|
|
|
|
|
|
|
|
if (queryString.contains('>')) {
|
2019-06-06 12:48:31 +03:00
|
|
|
qCDebug(lcSocketApi) << "queryString contains >";
|
2017-03-30 14:24:04 +03:00
|
|
|
|
2019-06-05 22:04:16 +03:00
|
|
|
auto subQueries = queryString.split('>', QString::SkipEmptyParts);
|
|
|
|
Q_ASSERT(subQueries.count() == 2);
|
|
|
|
|
|
|
|
auto parentQueryString = subQueries[0].trimmed();
|
2019-06-06 12:48:31 +03:00
|
|
|
qCDebug(lcSocketApi) << "Find parent: " << parentQueryString;
|
2019-06-05 22:04:16 +03:00
|
|
|
auto parent = findWidget(parentQueryString);
|
|
|
|
|
|
|
|
if(!parent) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto childQueryString = subQueries[1].trimmed();
|
|
|
|
auto child = findWidget(childQueryString, parent->findChildren<QWidget*>());
|
2019-06-06 12:48:31 +03:00
|
|
|
qCDebug(lcSocketApi) << "found child: " << !!child;
|
2019-06-05 22:04:16 +03:00
|
|
|
return child;
|
|
|
|
|
|
|
|
} else if(queryString.startsWith('#')) {
|
|
|
|
auto objectName = queryString.mid(1);
|
2019-06-06 12:48:31 +03:00
|
|
|
qCDebug(lcSocketApi) << "find objectName: " << objectName;
|
2019-06-05 22:04:16 +03:00
|
|
|
foundWidget = std::find_if(objects.constBegin(), objects.constEnd(), [&](QObject *widget) {
|
|
|
|
return widget->objectName() == objectName;
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
QList<QObject*> matches;
|
|
|
|
std::copy_if(objects.constBegin(), objects.constEnd(), std::back_inserter(matches), [&](QObject* widget) {
|
|
|
|
return widget->inherits(queryString.toLatin1());
|
|
|
|
});
|
|
|
|
|
|
|
|
std::for_each(matches.constBegin(), matches.constEnd(), [](QObject* w) {
|
|
|
|
if(!w) return;
|
2019-06-06 12:48:31 +03:00
|
|
|
qCDebug(lcSocketApi) << "WIDGET: " << w->objectName() << w->metaObject()->className();
|
2019-06-05 22:04:16 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
if(matches.empty()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return matches[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (foundWidget == objects.constEnd()) {
|
2017-03-30 14:24:04 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return *foundWidget;
|
|
|
|
}
|
|
|
|
#endif
|
2014-08-27 14:02:47 +04:00
|
|
|
|
2016-05-06 13:32:01 +03:00
|
|
|
static inline QString removeTrailingSlash(QString path)
|
|
|
|
{
|
|
|
|
Q_ASSERT(path.endsWith(QLatin1Char('/')));
|
2017-05-17 11:55:42 +03:00
|
|
|
path.truncate(path.length() - 1);
|
2016-05-06 13:32:01 +03:00
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2017-12-08 12:58:55 +03:00
|
|
|
static QString buildMessage(const QString &verb, const QString &path, const QString &status = QString())
|
2017-01-11 20:27:22 +03:00
|
|
|
{
|
|
|
|
QString msg(verb);
|
|
|
|
|
2017-05-17 11:55:42 +03:00
|
|
|
if (!status.isEmpty()) {
|
2017-01-11 20:27:22 +03:00
|
|
|
msg.append(QLatin1Char(':'));
|
|
|
|
msg.append(status);
|
|
|
|
}
|
2017-05-17 11:55:42 +03:00
|
|
|
if (!path.isEmpty()) {
|
2017-01-11 20:27:22 +03:00
|
|
|
msg.append(QLatin1Char(':'));
|
|
|
|
QFileInfo fi(path);
|
|
|
|
msg.append(QDir::toNativeSeparators(fi.absoluteFilePath()));
|
|
|
|
}
|
|
|
|
return msg;
|
|
|
|
}
|
2017-03-30 14:24:04 +03:00
|
|
|
}
|
2017-01-11 20:27:22 +03:00
|
|
|
|
2014-11-10 00:34:07 +03:00
|
|
|
namespace OCC {
|
2013-10-03 19:04:55 +04:00
|
|
|
|
2020-07-01 16:30:44 +03:00
|
|
|
Q_LOGGING_CATEGORY(lcSocketApi, "nextcloud.gui.socketapi", QtInfoMsg)
|
|
|
|
Q_LOGGING_CATEGORY(lcPublicLink, "nextcloud.gui.socketapi.publiclink", QtInfoMsg)
|
2018-04-06 18:13:29 +03:00
|
|
|
|
2017-05-09 15:24:11 +03:00
|
|
|
|
2017-03-30 14:24:04 +03:00
|
|
|
void SocketListener::sendMessage(const QString &message, bool doWait) const
|
2017-05-17 11:55:42 +03:00
|
|
|
{
|
2017-03-30 14:24:04 +03:00
|
|
|
if (!socket) {
|
2021-04-02 11:04:42 +03:00
|
|
|
qCWarning(lcSocketApi) << "Not sending message to dead socket:" << message;
|
2017-03-30 14:24:04 +03:00
|
|
|
return;
|
2017-05-17 11:55:42 +03:00
|
|
|
}
|
2017-01-11 20:27:22 +03:00
|
|
|
|
2021-04-02 11:04:42 +03:00
|
|
|
qCDebug(lcSocketApi) << "Sending SocketAPI message -->" << message << "to" << socket;
|
2017-03-30 14:24:04 +03:00
|
|
|
QString localMessage = message;
|
|
|
|
if (!localMessage.endsWith(QLatin1Char('\n'))) {
|
|
|
|
localMessage.append(QLatin1Char('\n'));
|
2017-01-11 20:27:22 +03:00
|
|
|
}
|
|
|
|
|
2017-03-30 14:24:04 +03:00
|
|
|
QByteArray bytesToSend = localMessage.toUtf8();
|
|
|
|
qint64 sent = socket->write(bytesToSend);
|
|
|
|
if (doWait) {
|
|
|
|
socket->waitForBytesWritten(1000);
|
2017-01-11 20:27:22 +03:00
|
|
|
}
|
2017-03-30 14:24:04 +03:00
|
|
|
if (sent != bytesToSend.length()) {
|
|
|
|
qCWarning(lcSocketApi) << "Could not send all data on socket for " << localMessage;
|
2017-01-11 20:27:22 +03:00
|
|
|
}
|
2017-03-30 14:24:04 +03:00
|
|
|
}
|
2017-01-11 20:27:22 +03:00
|
|
|
|
2017-05-17 11:55:42 +03:00
|
|
|
struct ListenerHasSocketPred
|
|
|
|
{
|
2017-01-11 20:27:22 +03:00
|
|
|
QIODevice *socket;
|
2017-05-17 11:55:42 +03:00
|
|
|
ListenerHasSocketPred(QIODevice *socket)
|
|
|
|
: socket(socket)
|
|
|
|
{
|
|
|
|
}
|
2017-01-11 20:27:22 +03:00
|
|
|
bool operator()(const SocketListener &listener) const { return listener.socket == socket; }
|
|
|
|
};
|
2013-10-03 19:04:55 +04:00
|
|
|
|
2017-05-17 11:55:42 +03: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
|
|
|
|
2017-03-30 14:24:04 +03:00
|
|
|
qRegisterMetaType<SocketListener *>("SocketListener*");
|
|
|
|
qRegisterMetaType<QSharedPointer<SocketApiJob>>("QSharedPointer<SocketApiJob>");
|
|
|
|
|
2014-09-29 14:19:33 +04:00
|
|
|
if (Utility::isWindows()) {
|
2020-05-26 21:13:51 +03:00
|
|
|
socketPath = QLatin1String(R"(\\.\pipe\)")
|
2020-08-18 20:31:11 +03:00
|
|
|
+ QLatin1String(APPLICATION_EXECUTABLE)
|
|
|
|
+ QLatin1String("-")
|
2017-05-17 11:55:42 +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";
|
2018-04-10 16:51:06 +03:00
|
|
|
#ifdef Q_OS_MAC
|
2018-08-31 16:03:33 +03:00
|
|
|
CFURLRef url = (CFURLRef)CFAutorelease((CFURLRef)CFBundleCopyBundleURL(CFBundleGetMainBundle()));
|
|
|
|
QString bundlePath = QUrl::fromCFURL(url).path();
|
|
|
|
|
2020-05-12 15:10:02 +03:00
|
|
|
auto _system = [](const QString &cmd, const QStringList &args){
|
|
|
|
QProcess process;
|
|
|
|
process.setProcessChannelMode(QProcess::MergedChannels);
|
|
|
|
process.start(cmd, args);
|
|
|
|
if (!process.waitForFinished())
|
|
|
|
{
|
|
|
|
qCWarning(lcSocketApi) << "Failed to load shell extension:" << cmd << args.join(" ") << process.errorString();
|
|
|
|
} else {
|
|
|
|
qCInfo(lcSocketApi) << (process.exitCode() != 0 ? "Failed to load" : "Loaded") << "shell extension:" << cmd << args.join(" ") << process.readAll();
|
|
|
|
}
|
|
|
|
};
|
2018-08-31 16:03:33 +03:00
|
|
|
// Add it again. This was needed for Mojave to trigger a load.
|
2020-05-12 15:10:02 +03:00
|
|
|
_system(QStringLiteral("pluginkit"), {QStringLiteral("-a"),QStringLiteral("%1Contents/PlugIns/FinderSyncExt.appex/").arg(bundlePath)});
|
|
|
|
// Tell Finder to use the Extension (checking it from System Preferences -> Extensions)
|
|
|
|
_system(QStringLiteral("pluginkit"), {QStringLiteral("-e"), QStringLiteral("use"), QStringLiteral("-i"), QStringLiteral(APPLICATION_REV_DOMAIN ".FinderSyncExt")});
|
2018-08-31 16:03:33 +03:00
|
|
|
|
2018-04-10 16:51:06 +03:00
|
|
|
#endif
|
2017-05-17 11:55:42 +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-05-17 11:55:42 +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;
|
2017-05-17 11:55:42 +03:00
|
|
|
if (result) {
|
2014-09-30 13:16:49 +04:00
|
|
|
QFile::setPermissions(socketPath,
|
2017-05-17 11:55:42 +03:00
|
|
|
QFile::Permissions(QFile::ReadOwner + QFile::WriteOwner + QFile::ExeOwner));
|
2014-09-30 13:16:49 +04:00
|
|
|
}
|
2014-06-19 16:08:30 +04:00
|
|
|
}
|
2017-05-17 11:55:42 +03: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
|
|
|
{
|
2018-04-11 14:40:27 +03:00
|
|
|
// Note that on macOS this is not actually a line-based QIODevice, it's a SocketApiSocket which is our
|
|
|
|
// custom message based macOS IPC.
|
2017-05-17 11:55:42 +03:00
|
|
|
QIODevice *socket = _localServer.nextPendingConnection();
|
2014-07-10 17:50:24 +04:00
|
|
|
|
2017-05-17 11:55:42 +03:00
|
|
|
if (!socket) {
|
2014-06-02 14:08:06 +04:00
|
|
|
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
|
|
|
|
2017-05-17 11:55:42 +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()));
|
2021-04-02 11:04:42 +03:00
|
|
|
qCInfo(lcSocketApi) << "Trying to send SocketAPI Register Path Message -->" << message << "to" << listener.socket;
|
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();
|
2019-01-17 12:21:22 +03:00
|
|
|
|
|
|
|
auto socket = qobject_cast<QIODevice *>(sender());
|
|
|
|
ASSERT(socket);
|
|
|
|
_listeners.erase(std::remove_if(_listeners.begin(), _listeners.end(), ListenerHasSocketPred(socket)), _listeners.end());
|
2013-10-03 19:04:55 +04:00
|
|
|
}
|
|
|
|
|
2017-05-17 11:55:42 +03:00
|
|
|
void SocketApi::slotSocketDestroyed(QObject *obj)
|
2017-01-11 20:27:22 +03:00
|
|
|
{
|
2020-05-18 21:54:23 +03:00
|
|
|
auto *socket = static_cast<QIODevice *>(obj);
|
2017-01-11 20:27:22 +03:00
|
|
|
_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
|
|
|
{
|
2020-05-18 21:54:23 +03:00
|
|
|
auto *socket = qobject_cast<QIODevice *>(sender());
|
2017-02-07 15:52:15 +03:00
|
|
|
ASSERT(socket);
|
2019-02-18 12:53:41 +03:00
|
|
|
|
|
|
|
// Find the SocketListener
|
|
|
|
//
|
|
|
|
// It's possible for the disconnected() signal to be triggered before
|
|
|
|
// the readyRead() signals are received - in that case there won't be a
|
|
|
|
// valid listener. We execute the handler anyway, but it will work with
|
|
|
|
// a SocketListener that doesn't send any messages.
|
|
|
|
static auto noListener = SocketListener(nullptr);
|
|
|
|
SocketListener *listener = &noListener;
|
|
|
|
auto listenerIt = std::find_if(_listeners.begin(), _listeners.end(), ListenerHasSocketPred(socket));
|
|
|
|
if (listenerIt != _listeners.end()) {
|
|
|
|
listener = &*listenerIt;
|
|
|
|
}
|
2013-10-03 19:04:55 +04:00
|
|
|
|
2017-05-17 11:55:42 +03: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-12-08 13:13:44 +03:00
|
|
|
QByteArray command = line.split(":").value(0).toLatin1();
|
2017-03-30 14:24:04 +03:00
|
|
|
|
|
|
|
QByteArray functionWithArguments = "command_" + command;
|
|
|
|
if (command.startsWith("ASYNC_")) {
|
|
|
|
functionWithArguments += "(QSharedPointer<SocketApiJob>)";
|
|
|
|
} else {
|
|
|
|
functionWithArguments += "(QString,SocketListener*)";
|
|
|
|
}
|
|
|
|
|
2017-01-11 20:31:08 +03:00
|
|
|
int indexOfMethod = staticMetaObject.indexOfMethod(functionWithArguments);
|
2013-10-03 19:04:55 +04:00
|
|
|
|
2017-05-17 11:55:42 +03:00
|
|
|
QString argument = line.remove(0, command.length() + 1);
|
2017-03-30 14:24:04 +03:00
|
|
|
if (command.startsWith("ASYNC_")) {
|
|
|
|
|
|
|
|
auto arguments = argument.split('|');
|
|
|
|
if (arguments.size() != 2) {
|
|
|
|
listener->sendMessage(QLatin1String("argument count is wrong"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto json = QJsonDocument::fromJson(arguments[1].toUtf8()).object();
|
2020-03-03 20:45:58 +03:00
|
|
|
|
2017-03-30 14:24:04 +03:00
|
|
|
auto jobId = arguments[0];
|
|
|
|
|
|
|
|
auto socketApiJob = QSharedPointer<SocketApiJob>(
|
|
|
|
new SocketApiJob(jobId, listener, json), &QObject::deleteLater);
|
|
|
|
if (indexOfMethod != -1) {
|
|
|
|
staticMetaObject.method(indexOfMethod)
|
|
|
|
.invoke(this, Qt::QueuedConnection,
|
|
|
|
Q_ARG(QSharedPointer<SocketApiJob>, socketApiJob));
|
|
|
|
} else {
|
|
|
|
qCWarning(lcSocketApi) << "The command is not supported by this version of the client:" << command
|
|
|
|
<< "with argument:" << argument;
|
|
|
|
socketApiJob->reject("command not found");
|
|
|
|
}
|
2014-06-02 14:08:06 +04:00
|
|
|
} else {
|
2017-03-30 14:24:04 +03:00
|
|
|
if (indexOfMethod != -1) {
|
2020-03-18 18:07:50 +03:00
|
|
|
// to ensure that listener is still valid we need to call it with Qt::DirectConnection
|
|
|
|
ASSERT(thread() == QThread::currentThread())
|
2017-03-30 14:24:04 +03:00
|
|
|
staticMetaObject.method(indexOfMethod)
|
2020-03-18 18:07:50 +03:00
|
|
|
.invoke(this, Qt::DirectConnection, Q_ARG(QString, argument),
|
2017-03-30 14:24:04 +03:00
|
|
|
Q_ARG(SocketListener *, listener));
|
|
|
|
} else {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:55:42 +03: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) {
|
2021-04-02 11:04:42 +03:00
|
|
|
qCInfo(lcSocketApi) << "Trying to send SocketAPI Register Path Message -->" << message << "to" << listener.socket;
|
2017-01-11 20:27:22 +03:00
|
|
|
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
|
|
|
|
2017-05-17 11:55:42 +03:00
|
|
|
void SocketApi::slotUnregisterPath(const QString &alias)
|
2014-07-25 14:10:45 +04:00
|
|
|
{
|
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-12-08 12:58:55 +03:00
|
|
|
broadcastMessage(buildMessage(QLatin1String("UNREGISTER_PATH"), removeTrailingSlash(f->path()), QString()), 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:55:42 +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-05-17 11:55:42 +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
|
|
|
}
|
|
|
|
|
2018-04-06 18:13:29 +03:00
|
|
|
void SocketApi::processShareRequest(const QString &localFile, SocketListener *listener, ShareDialogStartPage startPage)
|
|
|
|
{
|
|
|
|
auto theme = Theme::instance();
|
|
|
|
|
|
|
|
auto fileData = FileData::get(localFile);
|
|
|
|
auto shareFolder = fileData.folder;
|
|
|
|
if (!shareFolder) {
|
|
|
|
const QString message = QLatin1String("SHARE:NOP:") + QDir::toNativeSeparators(localFile);
|
|
|
|
// files that are not within a sync folder are not synced.
|
|
|
|
listener->sendMessage(message);
|
|
|
|
} 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
|
|
|
|
listener->sendMessage(message);
|
|
|
|
} else if (!theme->linkSharing() && (!theme->userGroupSharing() || shareFolder->accountState()->account()->serverVersionInt() < Account::makeServerVersion(8, 2, 0))) {
|
|
|
|
const QString message = QLatin1String("SHARE:NOP:") + QDir::toNativeSeparators(localFile);
|
|
|
|
listener->sendMessage(message);
|
|
|
|
} else {
|
2018-04-23 12:03:34 +03:00
|
|
|
// If the file doesn't have a journal record, it might not be uploaded yet
|
|
|
|
if (!fileData.journalRecord().isValid()) {
|
2018-04-06 18:13:29 +03:00
|
|
|
const QString message = QLatin1String("SHARE:NOTSYNCED:") + QDir::toNativeSeparators(localFile);
|
|
|
|
listener->sendMessage(message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-30 11:38:29 +03:00
|
|
|
auto &remotePath = fileData.serverRelativePath;
|
2018-04-06 18:13:29 +03:00
|
|
|
|
|
|
|
// Can't share root folder
|
|
|
|
if (remotePath == "/") {
|
|
|
|
const QString message = QLatin1String("SHARE:CANNOTSHAREROOT:") + QDir::toNativeSeparators(localFile);
|
|
|
|
listener->sendMessage(message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString message = QLatin1String("SHARE:OK:") + QDir::toNativeSeparators(localFile);
|
|
|
|
listener->sendMessage(message);
|
|
|
|
|
|
|
|
emit shareCommandReceived(remotePath, fileData.localPath, startPage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:55:42 +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-05-17 11:55:42 +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-05-17 11:55:42 +03:00
|
|
|
void SocketApi::command_RETRIEVE_FILE_STATUS(const QString &argument, SocketListener *listener)
|
2014-06-06 17:37:04 +04:00
|
|
|
{
|
|
|
|
QString statusString;
|
|
|
|
|
2018-04-06 18:13:29 +03:00
|
|
|
auto fileData = FileData::get(argument);
|
|
|
|
if (!fileData.folder) {
|
2014-07-10 16:27:52 +04:00
|
|
|
// 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
|
|
|
// 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.
|
2018-04-06 18:13:29 +03:00
|
|
|
QString directory = fileData.localPath.left(fileData.localPath.lastIndexOf('/'));
|
2017-01-11 20:27:22 +03:00
|
|
|
listener->registerMonitoredDirectory(qHash(directory));
|
2014-06-06 17:37:04 +04:00
|
|
|
|
2018-04-06 18:13:29 +03:00
|
|
|
SyncFileStatus fileStatus = fileData.syncFileStatus();
|
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
|
|
|
|
2017-05-17 11:55:42 +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-05-17 11:55:42 +03:00
|
|
|
void SocketApi::command_SHARE(const QString &localFile, SocketListener *listener)
|
2014-09-03 18:12:21 +04:00
|
|
|
{
|
2018-04-06 18:13:29 +03:00
|
|
|
processShareRequest(localFile, listener, ShareDialogStartPage::UsersAndGroups);
|
|
|
|
}
|
2016-02-22 15:53:45 +03:00
|
|
|
|
2018-04-06 18:13:29 +03:00
|
|
|
void SocketApi::command_MANAGE_PUBLIC_LINKS(const QString &localFile, SocketListener *listener)
|
|
|
|
{
|
|
|
|
processShareRequest(localFile, listener, ShareDialogStartPage::PublicLinks);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SocketApi::command_VERSION(const QString &, SocketListener *listener)
|
|
|
|
{
|
|
|
|
listener->sendMessage(QLatin1String("VERSION:" MIRALL_VERSION_STRING ":" MIRALL_SOCKET_API_VERSION));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SocketApi::command_SHARE_MENU_TITLE(const QString &, SocketListener *listener)
|
2014-08-27 14:02:47 +04:00
|
|
|
{
|
2020-01-18 17:12:16 +03:00
|
|
|
//listener->sendMessage(QLatin1String("SHARE_MENU_TITLE:") + tr("Share with %1", "parameter is Nextcloud").arg(Theme::instance()->appNameGUI()));
|
|
|
|
listener->sendMessage(QLatin1String("SHARE_MENU_TITLE:") + Theme::instance()->appNameGUI());
|
|
|
|
}
|
|
|
|
|
|
|
|
void SocketApi::command_EDIT(const QString &localFile, SocketListener *listener)
|
|
|
|
{
|
2020-08-19 17:32:57 +03:00
|
|
|
Q_UNUSED(listener)
|
2020-01-18 17:12:16 +03:00
|
|
|
auto fileData = FileData::get(localFile);
|
|
|
|
if (!fileData.folder) {
|
|
|
|
qCWarning(lcSocketApi) << "Unknown path" << localFile;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto record = fileData.journalRecord();
|
|
|
|
if (!record.isValid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
DirectEditor* editor = getDirectEditorForLocalFile(fileData.localPath);
|
|
|
|
if (!editor)
|
|
|
|
return;
|
|
|
|
|
2020-05-18 21:54:23 +03:00
|
|
|
auto *job = new JsonApiJob(fileData.folder->accountState()->account(), QLatin1String("ocs/v2.php/apps/files/api/v1/directEditing/open"), this);
|
2020-01-18 17:12:16 +03:00
|
|
|
|
|
|
|
QUrlQuery params;
|
2018-05-30 11:38:29 +03:00
|
|
|
params.addQueryItem("path", fileData.serverRelativePath);
|
2020-01-18 17:12:16 +03:00
|
|
|
params.addQueryItem("editorId", editor->id());
|
|
|
|
job->addQueryParams(params);
|
|
|
|
job->usePOST();
|
|
|
|
|
|
|
|
QObject::connect(job, &JsonApiJob::jsonReceived, [](const QJsonDocument &json){
|
|
|
|
auto data = json.object().value("ocs").toObject().value("data").toObject();
|
|
|
|
auto url = QUrl(data.value("url").toString());
|
|
|
|
|
|
|
|
if(!url.isEmpty())
|
2021-02-05 11:06:25 +03:00
|
|
|
Utility::openBrowser(url);
|
2020-01-18 17:12:16 +03:00
|
|
|
});
|
|
|
|
job->start();
|
2014-08-27 14:02:47 +04:00
|
|
|
}
|
|
|
|
|
2018-04-06 18:13:29 +03:00
|
|
|
// don't pull the share manager into socketapi unittests
|
|
|
|
#ifndef OWNCLOUD_TEST
|
|
|
|
|
|
|
|
class GetOrCreatePublicLinkShare : public QObject
|
2015-10-06 10:39:24 +03:00
|
|
|
{
|
2018-04-06 18:13:29 +03:00
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
GetOrCreatePublicLinkShare(const AccountPtr &account, const QString &localFile,
|
|
|
|
std::function<void(const QString &link)> targetFun, QObject *parent)
|
|
|
|
: QObject(parent)
|
|
|
|
, _shareManager(account)
|
|
|
|
, _localFile(localFile)
|
|
|
|
, _targetFun(targetFun)
|
|
|
|
{
|
|
|
|
connect(&_shareManager, &ShareManager::sharesFetched,
|
|
|
|
this, &GetOrCreatePublicLinkShare::sharesFetched);
|
|
|
|
connect(&_shareManager, &ShareManager::linkShareCreated,
|
|
|
|
this, &GetOrCreatePublicLinkShare::linkShareCreated);
|
|
|
|
connect(&_shareManager, &ShareManager::serverError,
|
|
|
|
this, &GetOrCreatePublicLinkShare::serverError);
|
2020-01-09 17:10:01 +03:00
|
|
|
connect(&_shareManager, &ShareManager::linkShareRequiresPassword,
|
|
|
|
this, &GetOrCreatePublicLinkShare::passwordRequired);
|
2018-04-06 18:13:29 +03:00
|
|
|
}
|
2015-10-06 10:39:24 +03:00
|
|
|
|
2018-04-06 18:13:29 +03:00
|
|
|
void run()
|
|
|
|
{
|
|
|
|
qCDebug(lcPublicLink) << "Fetching shares";
|
|
|
|
_shareManager.fetchShares(_localFile);
|
|
|
|
}
|
2015-11-25 11:40:21 +03:00
|
|
|
|
2018-04-06 18:13:29 +03:00
|
|
|
private slots:
|
|
|
|
void sharesFetched(const QList<QSharedPointer<Share>> &shares)
|
|
|
|
{
|
|
|
|
auto shareName = SocketApi::tr("Context menu share");
|
|
|
|
// If there already is a context menu share, reuse it
|
|
|
|
for (const auto &share : shares) {
|
|
|
|
const auto linkShare = qSharedPointerDynamicCast<LinkShare>(share);
|
|
|
|
if (!linkShare)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (linkShare->getName() == shareName) {
|
|
|
|
qCDebug(lcPublicLink) << "Found existing share, reusing";
|
|
|
|
return success(linkShare->getLink().toString());
|
|
|
|
}
|
2015-11-25 11:40:21 +03:00
|
|
|
}
|
|
|
|
|
2018-04-06 18:13:29 +03:00
|
|
|
// otherwise create a new one
|
|
|
|
qCDebug(lcPublicLink) << "Creating new share";
|
|
|
|
_shareManager.createLinkShare(_localFile, shareName, QString());
|
|
|
|
}
|
2015-10-06 10:39:24 +03:00
|
|
|
|
2018-04-06 18:13:29 +03:00
|
|
|
void linkShareCreated(const QSharedPointer<LinkShare> &share)
|
|
|
|
{
|
|
|
|
qCDebug(lcPublicLink) << "New share created";
|
|
|
|
success(share->getLink().toString());
|
|
|
|
}
|
2015-10-06 10:39:24 +03:00
|
|
|
|
2020-01-09 17:10:01 +03:00
|
|
|
void passwordRequired() {
|
2020-05-29 16:07:05 +03:00
|
|
|
bool ok = false;
|
2020-01-09 17:10:01 +03:00
|
|
|
QString password = QInputDialog::getText(nullptr,
|
|
|
|
tr("Password for share required"),
|
|
|
|
tr("Please enter a password for your link share:"),
|
|
|
|
QLineEdit::Normal,
|
|
|
|
QString(),
|
|
|
|
&ok);
|
|
|
|
|
|
|
|
if (!ok) {
|
|
|
|
// The dialog was canceled so no need to do anything
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to create the link share again with the newly entered password
|
|
|
|
_shareManager.createLinkShare(_localFile, QString(), password);
|
|
|
|
}
|
|
|
|
|
2018-04-06 18:13:29 +03:00
|
|
|
void serverError(int code, const QString &message)
|
|
|
|
{
|
|
|
|
qCWarning(lcPublicLink) << "Share fetch/create error" << code << message;
|
|
|
|
QMessageBox::warning(
|
2018-11-11 12:56:22 +03:00
|
|
|
nullptr,
|
2018-04-06 18:13:29 +03:00
|
|
|
tr("Sharing error"),
|
|
|
|
tr("Could not retrieve or create the public link share. Error:\n\n%1").arg(message),
|
|
|
|
QMessageBox::Ok,
|
|
|
|
QMessageBox::NoButton);
|
|
|
|
deleteLater();
|
|
|
|
}
|
2015-10-06 10:39:24 +03:00
|
|
|
|
2018-04-06 18:13:29 +03:00
|
|
|
private:
|
|
|
|
void success(const QString &link)
|
|
|
|
{
|
|
|
|
_targetFun(link);
|
|
|
|
deleteLater();
|
|
|
|
}
|
2016-02-22 15:53:45 +03:00
|
|
|
|
2018-04-06 18:13:29 +03:00
|
|
|
ShareManager _shareManager;
|
|
|
|
QString _localFile;
|
|
|
|
std::function<void(const QString &url)> _targetFun;
|
|
|
|
};
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
class GetOrCreatePublicLinkShare : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
GetOrCreatePublicLinkShare(const AccountPtr &, const QString &,
|
|
|
|
std::function<void(const QString &link)>, QObject *)
|
|
|
|
{
|
2015-10-06 10:39:24 +03:00
|
|
|
}
|
|
|
|
|
2018-04-06 18:13:29 +03:00
|
|
|
void run()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void SocketApi::command_COPY_PUBLIC_LINK(const QString &localFile, SocketListener *)
|
2015-01-23 18:09:38 +03:00
|
|
|
{
|
2018-04-06 18:13:29 +03:00
|
|
|
auto fileData = FileData::get(localFile);
|
|
|
|
if (!fileData.folder)
|
|
|
|
return;
|
|
|
|
|
|
|
|
AccountPtr account = fileData.folder->accountState()->account();
|
2018-05-30 11:38:29 +03:00
|
|
|
auto job = new GetOrCreatePublicLinkShare(account, fileData.serverRelativePath, [](const QString &url) { copyUrlToClipboard(url); }, this);
|
2018-04-06 18:13:29 +03:00
|
|
|
job->run();
|
2015-01-23 18:09:38 +03:00
|
|
|
}
|
|
|
|
|
2020-03-03 20:45:58 +03:00
|
|
|
// Windows Shell / Explorer pinning fallbacks, see issue: https://github.com/nextcloud/desktop/issues/1599
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
void SocketApi::command_COPYASPATH(const QString &localFile, SocketListener *)
|
|
|
|
{
|
|
|
|
QApplication::clipboard()->setText(localFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SocketApi::command_OPENNEWWINDOW(const QString &localFile, SocketListener *)
|
|
|
|
{
|
|
|
|
QDesktopServices::openUrl(QUrl::fromLocalFile(localFile));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SocketApi::command_OPEN(const QString &localFile, SocketListener *socketListener)
|
|
|
|
{
|
|
|
|
command_OPENNEWWINDOW(localFile, socketListener);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-09-15 15:24:34 +03:00
|
|
|
// Fetches the private link url asynchronously and then calls the target slot
|
2018-04-06 18:13:29 +03:00
|
|
|
void SocketApi::fetchPrivateLinkUrlHelper(const QString &localFile, const std::function<void(const QString &url)> &targetFun)
|
2017-05-10 10:37:10 +03:00
|
|
|
{
|
2018-04-06 18:13:29 +03:00
|
|
|
auto fileData = FileData::get(localFile);
|
|
|
|
if (!fileData.folder) {
|
2017-09-15 15:24:34 +03:00
|
|
|
qCWarning(lcSocketApi) << "Unknown path" << localFile;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-06 18:13:29 +03:00
|
|
|
auto record = fileData.journalRecord();
|
|
|
|
if (!record.isValid())
|
2017-09-15 15:24:34 +03:00
|
|
|
return;
|
2017-11-07 18:12:07 +03:00
|
|
|
|
2018-04-06 18:13:29 +03:00
|
|
|
fetchPrivateLinkUrl(
|
|
|
|
fileData.folder->accountState()->account(),
|
2018-05-30 11:38:29 +03:00
|
|
|
fileData.serverRelativePath,
|
2018-04-06 18:13:29 +03:00
|
|
|
record.numericFileId(),
|
|
|
|
this,
|
|
|
|
targetFun);
|
2017-09-15 15:24:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void SocketApi::command_COPY_PRIVATE_LINK(const QString &localFile, SocketListener *)
|
|
|
|
{
|
2018-04-06 18:13:29 +03:00
|
|
|
fetchPrivateLinkUrlHelper(localFile, &SocketApi::copyUrlToClipboard);
|
2017-05-10 10:37:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void SocketApi::command_EMAIL_PRIVATE_LINK(const QString &localFile, SocketListener *)
|
|
|
|
{
|
2018-04-06 18:13:29 +03:00
|
|
|
fetchPrivateLinkUrlHelper(localFile, &SocketApi::emailPrivateLink);
|
2017-09-15 15:24:34 +03:00
|
|
|
}
|
|
|
|
|
2018-01-23 18:38:47 +03:00
|
|
|
void SocketApi::command_OPEN_PRIVATE_LINK(const QString &localFile, SocketListener *)
|
|
|
|
{
|
2018-04-06 18:13:29 +03:00
|
|
|
fetchPrivateLinkUrlHelper(localFile, &SocketApi::openPrivateLink);
|
2018-01-23 18:38:47 +03:00
|
|
|
}
|
|
|
|
|
2020-11-27 01:06:03 +03:00
|
|
|
void SocketApi::command_MAKE_AVAILABLE_LOCALLY(const QString &filesArg, SocketListener *)
|
2018-01-26 10:56:50 +03:00
|
|
|
{
|
|
|
|
QStringList files = filesArg.split(QLatin1Char('\x1e')); // Record Separator
|
|
|
|
|
|
|
|
for (const auto &file : files) {
|
2018-08-15 11:46:16 +03:00
|
|
|
auto data = FileData::get(file);
|
2018-11-27 12:31:13 +03:00
|
|
|
if (!data.folder)
|
|
|
|
continue;
|
|
|
|
|
2020-11-27 01:06:03 +03:00
|
|
|
// Update the pin state on all items
|
2019-06-27 16:47:04 +03:00
|
|
|
data.folder->vfs().setPinState(data.folderRelativePath, PinState::AlwaysLocal);
|
2018-11-27 12:31:13 +03:00
|
|
|
|
2019-04-03 14:32:05 +03:00
|
|
|
// Trigger sync
|
|
|
|
data.folder->schedulePathForLocalDiscovery(data.folderRelativePath);
|
|
|
|
data.folder->scheduleThisFolderSoon();
|
2018-01-26 10:56:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-15 11:46:16 +03:00
|
|
|
/* Go over all the files and replace them by a virtual file */
|
2020-11-27 01:06:03 +03:00
|
|
|
void SocketApi::command_MAKE_ONLINE_ONLY(const QString &filesArg, SocketListener *)
|
2018-08-28 11:35:26 +03:00
|
|
|
{
|
|
|
|
QStringList files = filesArg.split(QLatin1Char('\x1e')); // Record Separator
|
|
|
|
|
|
|
|
for (const auto &file : files) {
|
2018-08-15 11:46:16 +03:00
|
|
|
auto data = FileData::get(file);
|
2018-11-27 12:31:13 +03:00
|
|
|
if (!data.folder)
|
|
|
|
continue;
|
|
|
|
|
2020-11-27 01:06:03 +03:00
|
|
|
// Update the pin state on all items
|
2019-06-27 16:47:04 +03:00
|
|
|
data.folder->vfs().setPinState(data.folderRelativePath, PinState::OnlineOnly);
|
2018-11-27 12:31:13 +03:00
|
|
|
|
2019-04-03 14:32:05 +03:00
|
|
|
// Trigger sync
|
|
|
|
data.folder->schedulePathForLocalDiscovery(data.folderRelativePath);
|
|
|
|
data.folder->scheduleThisFolderSoon();
|
2018-08-28 11:35:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-06 18:13:29 +03:00
|
|
|
void SocketApi::copyUrlToClipboard(const QString &link)
|
2017-09-15 15:24:34 +03:00
|
|
|
{
|
|
|
|
QApplication::clipboard()->setText(link);
|
|
|
|
}
|
|
|
|
|
2020-10-07 18:51:57 +03:00
|
|
|
void SocketApi::command_RESOLVE_CONFLICT(const QString &localFile, SocketListener *)
|
|
|
|
{
|
|
|
|
const auto fileData = FileData::get(localFile);
|
|
|
|
if (!fileData.folder || !Utility::isConflictFile(fileData.folderRelativePath))
|
|
|
|
return; // should not have shown menu item
|
|
|
|
|
|
|
|
const auto conflictedRelativePath = fileData.folderRelativePath;
|
|
|
|
const auto baseRelativePath = fileData.folder->journalDb()->conflictFileBaseName(fileData.folderRelativePath.toUtf8());
|
|
|
|
|
|
|
|
const auto dir = QDir(fileData.folder->path());
|
|
|
|
const auto conflictedPath = dir.filePath(conflictedRelativePath);
|
|
|
|
const auto basePath = dir.filePath(baseRelativePath);
|
|
|
|
|
|
|
|
const auto baseName = QFileInfo(basePath).fileName();
|
|
|
|
|
|
|
|
#ifndef OWNCLOUD_TEST
|
|
|
|
ConflictDialog dialog;
|
|
|
|
dialog.setBaseFilename(baseName);
|
|
|
|
dialog.setLocalVersionFilename(conflictedPath);
|
|
|
|
dialog.setRemoteVersionFilename(basePath);
|
2020-12-15 11:50:26 +03:00
|
|
|
if (dialog.exec() == ConflictDialog::Accepted) {
|
|
|
|
fileData.folder->scheduleThisFolderSoon();
|
|
|
|
}
|
2020-10-07 18:51:57 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-10-07 15:51:04 +03:00
|
|
|
void SocketApi::command_DELETE_ITEM(const QString &localFile, SocketListener *)
|
|
|
|
{
|
2020-10-07 17:30:43 +03:00
|
|
|
ConflictSolver solver;
|
|
|
|
solver.setLocalVersionFilename(localFile);
|
|
|
|
solver.exec(ConflictSolver::KeepRemoteVersion);
|
2020-10-07 15:51:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void SocketApi::command_MOVE_ITEM(const QString &localFile, SocketListener *)
|
|
|
|
{
|
|
|
|
const auto fileData = FileData::get(localFile);
|
|
|
|
const auto parentDir = fileData.parentFolder();
|
|
|
|
if (!fileData.folder)
|
|
|
|
return; // should not have shown menu item
|
|
|
|
|
|
|
|
QString defaultDirAndName = fileData.folderRelativePath;
|
|
|
|
|
|
|
|
// If it's a conflict, we want to save it under the base name by default
|
|
|
|
if (Utility::isConflictFile(defaultDirAndName)) {
|
|
|
|
defaultDirAndName = fileData.folder->journalDb()->conflictFileBaseName(fileData.folderRelativePath.toUtf8());
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the parent doesn't accept new files, go to the root of the sync folder
|
|
|
|
QFileInfo fileInfo(localFile);
|
|
|
|
const auto parentRecord = parentDir.journalRecord();
|
|
|
|
if ((fileInfo.isFile() && !parentRecord._remotePerm.hasPermission(RemotePermissions::CanAddFile))
|
|
|
|
|| (fileInfo.isDir() && !parentRecord._remotePerm.hasPermission(RemotePermissions::CanAddSubDirectories))) {
|
|
|
|
defaultDirAndName = QFileInfo(defaultDirAndName).fileName();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add back the folder path
|
|
|
|
defaultDirAndName = QDir(fileData.folder->path()).filePath(defaultDirAndName);
|
|
|
|
|
|
|
|
const auto target = QFileDialog::getSaveFileName(
|
|
|
|
nullptr,
|
2020-10-23 16:02:29 +03:00
|
|
|
tr("Select new location …"),
|
2020-10-07 15:51:04 +03:00
|
|
|
defaultDirAndName,
|
|
|
|
QString(), nullptr, QFileDialog::HideNameFilterDetails);
|
|
|
|
if (target.isEmpty())
|
|
|
|
return;
|
|
|
|
|
2020-10-07 17:30:43 +03:00
|
|
|
ConflictSolver solver;
|
|
|
|
solver.setLocalVersionFilename(localFile);
|
|
|
|
solver.setRemoteVersionFilename(target);
|
2020-10-07 15:51:04 +03:00
|
|
|
}
|
|
|
|
|
2018-04-06 18:13:29 +03:00
|
|
|
void SocketApi::emailPrivateLink(const QString &link)
|
2017-09-15 15:24:34 +03:00
|
|
|
{
|
|
|
|
Utility::openEmailComposer(
|
|
|
|
tr("I shared something with you"),
|
|
|
|
link,
|
2018-11-11 12:56:22 +03:00
|
|
|
nullptr);
|
2017-05-10 10:37:10 +03:00
|
|
|
}
|
|
|
|
|
2018-04-06 18:13:29 +03:00
|
|
|
void OCC::SocketApi::openPrivateLink(const QString &link)
|
2018-01-23 18:38:47 +03:00
|
|
|
{
|
2021-02-05 11:06:25 +03:00
|
|
|
Utility::openBrowser(link);
|
2018-01-23 18:38:47 +03:00
|
|
|
}
|
|
|
|
|
2018-01-18 17:17:29 +03:00
|
|
|
void SocketApi::command_GET_STRINGS(const QString &argument, SocketListener *listener)
|
2017-05-10 10:37:10 +03:00
|
|
|
{
|
|
|
|
static std::array<std::pair<const char *, QString>, 5> strings { {
|
2019-09-25 20:54:22 +03:00
|
|
|
{ "SHARE_MENU_TITLE", tr("Share options") },
|
2020-01-18 17:12:16 +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") },
|
2019-11-15 01:23:43 +03:00
|
|
|
{ "EMAIL_PRIVATE_LINK_MENU_TITLE", tr("Send private link by email …") },
|
2020-03-21 04:04:11 +03:00
|
|
|
{ "CONTEXT_MENU_ICON", APPLICATION_ICON_NAME},
|
2017-05-10 10:37:10 +03:00
|
|
|
} };
|
2017-07-04 17:45:48 +03:00
|
|
|
listener->sendMessage(QString("GET_STRINGS:BEGIN"));
|
2019-10-07 00:10:26 +03:00
|
|
|
for (const auto& key_value : strings) {
|
2018-01-18 17:17:29 +03:00
|
|
|
if (argument.isEmpty() || argument == QLatin1String(key_value.first)) {
|
|
|
|
listener->sendMessage(QString("STRING:%1:%2").arg(key_value.first, key_value.second));
|
|
|
|
}
|
2017-05-10 10:37:10 +03:00
|
|
|
}
|
2017-07-04 17:45:48 +03:00
|
|
|
listener->sendMessage(QString("GET_STRINGS:END"));
|
2017-05-10 10:37:10 +03:00
|
|
|
}
|
|
|
|
|
2020-08-20 15:13:56 +03:00
|
|
|
void SocketApi::sendSharingContextMenuOptions(const FileData &fileData, SocketListener *listener, bool enabled)
|
2018-01-18 17:17:29 +03:00
|
|
|
{
|
2018-04-06 18:13:29 +03:00
|
|
|
auto record = fileData.journalRecord();
|
|
|
|
bool isOnTheServer = record.isValid();
|
2020-08-20 15:13:56 +03:00
|
|
|
auto flagString = isOnTheServer && enabled ? QLatin1String("::") : QLatin1String(":d:");
|
2018-01-18 17:17:29 +03:00
|
|
|
|
2018-04-06 18:13:29 +03:00
|
|
|
auto capabilities = fileData.folder->accountState()->account()->capabilities();
|
|
|
|
auto theme = Theme::instance();
|
|
|
|
if (!capabilities.shareAPI() || !(theme->userGroupSharing() || (theme->linkSharing() && capabilities.sharePublicLink())))
|
|
|
|
return;
|
2018-01-23 18:38:47 +03:00
|
|
|
|
2018-04-06 18:13:29 +03:00
|
|
|
// If sharing is globally disabled, do not show any sharing entries.
|
|
|
|
// If there is no permission to share for this file, add a disabled entry saying so
|
|
|
|
if (isOnTheServer && !record._remotePerm.isNull() && !record._remotePerm.hasPermission(RemotePermissions::CanReshare)) {
|
2019-06-04 13:50:30 +03:00
|
|
|
listener->sendMessage(QLatin1String("MENU_ITEM:DISABLED:d:") + (!record.isDirectory()
|
|
|
|
? tr("Resharing this file is not allowed") : tr("Resharing this folder is not allowed")));
|
2018-04-06 18:13:29 +03:00
|
|
|
} else {
|
2019-09-25 20:54:22 +03:00
|
|
|
listener->sendMessage(QLatin1String("MENU_ITEM:SHARE") + flagString + tr("Share options"));
|
2018-04-06 18:13:29 +03:00
|
|
|
|
|
|
|
// Do we have public links?
|
|
|
|
bool publicLinksEnabled = theme->linkSharing() && capabilities.sharePublicLink();
|
|
|
|
|
|
|
|
// Is is possible to create a public link without user choices?
|
|
|
|
bool canCreateDefaultPublicLink = publicLinksEnabled
|
|
|
|
&& !capabilities.sharePublicLinkEnforceExpireDate()
|
2020-10-06 13:45:45 +03:00
|
|
|
&& !capabilities.sharePublicLinkAskOptionalPassword()
|
2018-04-06 18:13:29 +03:00
|
|
|
&& !capabilities.sharePublicLinkEnforcePassword();
|
|
|
|
|
|
|
|
if (canCreateDefaultPublicLink) {
|
2019-09-25 20:54:22 +03:00
|
|
|
listener->sendMessage(QLatin1String("MENU_ITEM:COPY_PUBLIC_LINK") + flagString + tr("Copy public link"));
|
2018-04-06 18:13:29 +03:00
|
|
|
} else if (publicLinksEnabled) {
|
2019-09-25 20:54:22 +03:00
|
|
|
listener->sendMessage(QLatin1String("MENU_ITEM:MANAGE_PUBLIC_LINKS") + flagString + tr("Copy public link"));
|
2018-01-18 17:17:29 +03:00
|
|
|
}
|
2018-04-06 18:13:29 +03:00
|
|
|
}
|
|
|
|
|
2019-09-25 20:54:22 +03:00
|
|
|
listener->sendMessage(QLatin1String("MENU_ITEM:COPY_PRIVATE_LINK") + flagString + tr("Copy internal link"));
|
2018-04-06 18:13:29 +03:00
|
|
|
|
|
|
|
// Disabled: only providing email option for private links would look odd,
|
|
|
|
// and the copy option is more general.
|
2019-11-15 01:23:43 +03:00
|
|
|
//listener->sendMessage(QLatin1String("MENU_ITEM:EMAIL_PRIVATE_LINK") + flagString + tr("Send private link by email …"));
|
2018-04-06 18:13:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
SocketApi::FileData SocketApi::FileData::get(const QString &localFile)
|
|
|
|
{
|
|
|
|
FileData data;
|
|
|
|
|
|
|
|
data.localPath = QDir::cleanPath(localFile);
|
|
|
|
if (data.localPath.endsWith(QLatin1Char('/')))
|
|
|
|
data.localPath.chop(1);
|
|
|
|
|
|
|
|
data.folder = FolderMan::instance()->folderForPath(data.localPath);
|
|
|
|
if (!data.folder)
|
|
|
|
return data;
|
|
|
|
|
|
|
|
data.folderRelativePath = data.localPath.mid(data.folder->cleanPath().length() + 1);
|
2018-05-30 11:38:29 +03:00
|
|
|
data.serverRelativePath = QDir(data.folder->remotePath()).filePath(data.folderRelativePath);
|
2018-05-28 18:14:57 +03:00
|
|
|
QString virtualFileExt = QStringLiteral(APPLICATION_DOTVIRTUALFILE_SUFFIX);
|
2018-05-30 11:38:29 +03:00
|
|
|
if (data.serverRelativePath.endsWith(virtualFileExt)) {
|
|
|
|
data.serverRelativePath.chop(virtualFileExt.size());
|
2018-05-28 18:14:57 +03:00
|
|
|
}
|
2018-04-06 18:13:29 +03:00
|
|
|
return data;
|
|
|
|
}
|
2018-01-23 18:38:47 +03:00
|
|
|
|
2020-11-27 01:06:03 +03:00
|
|
|
QString SocketApi::FileData::folderRelativePathNoVfsSuffix() const
|
|
|
|
{
|
|
|
|
auto result = folderRelativePath;
|
|
|
|
QString virtualFileExt = QStringLiteral(APPLICATION_DOTVIRTUALFILE_SUFFIX);
|
|
|
|
if (result.endsWith(virtualFileExt)) {
|
|
|
|
result.chop(virtualFileExt.size());
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-04-06 18:13:29 +03:00
|
|
|
SyncFileStatus SocketApi::FileData::syncFileStatus() const
|
|
|
|
{
|
|
|
|
if (!folder)
|
|
|
|
return SyncFileStatus::StatusNone;
|
|
|
|
return folder->syncEngine().syncFileStatusTracker().fileStatus(folderRelativePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
SyncJournalFileRecord SocketApi::FileData::journalRecord() const
|
|
|
|
{
|
|
|
|
SyncJournalFileRecord record;
|
|
|
|
if (!folder)
|
|
|
|
return record;
|
|
|
|
folder->journalDb()->getFileRecord(folderRelativePath, &record);
|
|
|
|
return record;
|
|
|
|
}
|
|
|
|
|
2020-10-07 15:51:04 +03:00
|
|
|
SocketApi::FileData SocketApi::FileData::parentFolder() const
|
|
|
|
{
|
|
|
|
return FileData::get(QFileInfo(localPath).dir().path().toUtf8());
|
|
|
|
}
|
|
|
|
|
2018-04-06 18:13:29 +03:00
|
|
|
void SocketApi::command_GET_MENU_ITEMS(const QString &argument, OCC::SocketListener *listener)
|
|
|
|
{
|
|
|
|
listener->sendMessage(QString("GET_MENU_ITEMS:BEGIN"));
|
2018-01-26 10:56:50 +03:00
|
|
|
QStringList files = argument.split(QLatin1Char('\x1e')); // Record Separator
|
|
|
|
|
|
|
|
// Find the common sync folder.
|
|
|
|
// syncFolder will be null if files are in different folders.
|
|
|
|
Folder *syncFolder = nullptr;
|
|
|
|
for (const auto &file : files) {
|
|
|
|
auto folder = FolderMan::instance()->folderForPath(file);
|
|
|
|
if (folder != syncFolder) {
|
|
|
|
if (!syncFolder) {
|
|
|
|
syncFolder = folder;
|
|
|
|
} else {
|
|
|
|
syncFolder = nullptr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sharing actions show for single files only
|
|
|
|
if (syncFolder && files.size() == 1 && syncFolder->accountState()->isConnected()) {
|
|
|
|
QString systemPath = QDir::cleanPath(argument);
|
|
|
|
if (systemPath.endsWith(QLatin1Char('/'))) {
|
|
|
|
systemPath.truncate(systemPath.length() - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
FileData fileData = FileData::get(argument);
|
|
|
|
const auto record = fileData.journalRecord();
|
|
|
|
const bool isOnTheServer = record.isValid();
|
|
|
|
const auto isE2eEncryptedPath = fileData.journalRecord()._isE2eEncrypted || !fileData.journalRecord()._e2eMangledName.isEmpty();
|
|
|
|
auto flagString = isOnTheServer && !isE2eEncryptedPath ? QLatin1String("::") : QLatin1String(":d:");
|
2020-01-18 17:12:16 +03:00
|
|
|
|
|
|
|
DirectEditor* editor = getDirectEditorForLocalFile(fileData.localPath);
|
|
|
|
if (editor) {
|
|
|
|
//listener->sendMessage(QLatin1String("MENU_ITEM:EDIT") + flagString + tr("Edit via ") + editor->name());
|
|
|
|
listener->sendMessage(QLatin1String("MENU_ITEM:EDIT") + flagString + tr("Edit"));
|
|
|
|
} else {
|
|
|
|
listener->sendMessage(QLatin1String("MENU_ITEM:OPEN_PRIVATE_LINK") + flagString + tr("Open in browser"));
|
|
|
|
}
|
|
|
|
|
2020-08-20 15:13:56 +03:00
|
|
|
sendSharingContextMenuOptions(fileData, listener, !isE2eEncryptedPath);
|
2020-10-07 15:51:04 +03:00
|
|
|
|
|
|
|
// Conflict files get conflict resolution actions
|
|
|
|
bool isConflict = Utility::isConflictFile(fileData.folderRelativePath);
|
|
|
|
if (isConflict || !isOnTheServer) {
|
|
|
|
// Check whether this new file is in a read-only directory
|
|
|
|
QFileInfo fileInfo(fileData.localPath);
|
|
|
|
const auto parentDir = fileData.parentFolder();
|
|
|
|
const auto parentRecord = parentDir.journalRecord();
|
|
|
|
const bool canAddToDir =
|
2020-10-07 18:47:48 +03:00
|
|
|
!parentRecord.isValid() // We're likely at the root of the sync folder, got to assume we can add there
|
|
|
|
|| (fileInfo.isFile() && parentRecord._remotePerm.hasPermission(RemotePermissions::CanAddFile))
|
|
|
|
|| (fileInfo.isDir() && parentRecord._remotePerm.hasPermission(RemotePermissions::CanAddSubDirectories));
|
2020-10-07 15:51:04 +03:00
|
|
|
const bool canChangeFile =
|
|
|
|
!isOnTheServer
|
|
|
|
|| (record._remotePerm.hasPermission(RemotePermissions::CanDelete)
|
|
|
|
&& record._remotePerm.hasPermission(RemotePermissions::CanMove)
|
|
|
|
&& record._remotePerm.hasPermission(RemotePermissions::CanRename));
|
|
|
|
|
|
|
|
if (isConflict && canChangeFile) {
|
|
|
|
if (canAddToDir) {
|
2020-10-23 16:02:29 +03:00
|
|
|
listener->sendMessage(QLatin1String("MENU_ITEM:RESOLVE_CONFLICT::") + tr("Resolve conflict …"));
|
2020-10-07 15:51:04 +03:00
|
|
|
} else {
|
|
|
|
if (isOnTheServer) {
|
|
|
|
// Uploaded conflict file in read-only directory
|
2020-10-23 16:02:29 +03:00
|
|
|
listener->sendMessage(QLatin1String("MENU_ITEM:MOVE_ITEM::") + tr("Move and rename …"));
|
2020-10-07 15:51:04 +03:00
|
|
|
} else {
|
|
|
|
// Local-only conflict file in a read-only dir
|
2020-10-23 16:02:29 +03:00
|
|
|
listener->sendMessage(QLatin1String("MENU_ITEM:MOVE_ITEM::") + tr("Move, rename and upload …"));
|
2020-10-07 15:51:04 +03:00
|
|
|
}
|
2020-10-07 18:51:57 +03:00
|
|
|
listener->sendMessage(QLatin1String("MENU_ITEM:DELETE_ITEM::") + tr("Delete local changes"));
|
2020-10-07 15:51:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// File in a read-only directory?
|
|
|
|
if (!isConflict && !isOnTheServer && !canAddToDir) {
|
2020-10-23 16:02:29 +03:00
|
|
|
listener->sendMessage(QLatin1String("MENU_ITEM:MOVE_ITEM::") + tr("Move and upload …"));
|
2020-10-07 15:51:04 +03:00
|
|
|
listener->sendMessage(QLatin1String("MENU_ITEM:DELETE_ITEM::") + tr("Delete"));
|
|
|
|
}
|
|
|
|
}
|
2018-01-18 17:17:29 +03:00
|
|
|
}
|
2018-01-26 10:56:50 +03:00
|
|
|
|
2020-11-27 01:06:03 +03:00
|
|
|
// File availability actions
|
2019-02-06 12:17:35 +03:00
|
|
|
if (syncFolder
|
2020-10-21 13:31:21 +03:00
|
|
|
&& syncFolder->virtualFilesEnabled()
|
2019-02-06 12:17:35 +03:00
|
|
|
&& syncFolder->vfs().socketApiPinStateActionsShown()) {
|
2019-04-03 11:53:04 +03:00
|
|
|
ENFORCE(!files.isEmpty());
|
|
|
|
|
|
|
|
// Determine the combined availability status of the files
|
|
|
|
auto combined = Optional<VfsItemAvailability>();
|
|
|
|
auto merge = [](VfsItemAvailability lhs, VfsItemAvailability rhs) {
|
|
|
|
if (lhs == rhs)
|
|
|
|
return lhs;
|
2019-04-25 12:10:52 +03:00
|
|
|
if (int(lhs) > int(rhs))
|
|
|
|
std::swap(lhs, rhs); // reduce cases ensuring lhs < rhs
|
|
|
|
if (lhs == VfsItemAvailability::AlwaysLocal && rhs == VfsItemAvailability::AllHydrated)
|
2019-04-16 11:17:21 +03:00
|
|
|
return VfsItemAvailability::AllHydrated;
|
2019-04-25 12:10:52 +03:00
|
|
|
if (lhs == VfsItemAvailability::AllDehydrated && rhs == VfsItemAvailability::OnlineOnly)
|
2019-04-16 11:17:21 +03:00
|
|
|
return VfsItemAvailability::AllDehydrated;
|
|
|
|
return VfsItemAvailability::Mixed;
|
2019-04-03 11:53:04 +03:00
|
|
|
};
|
2018-01-26 10:56:50 +03:00
|
|
|
for (const auto &file : files) {
|
2019-01-15 12:52:44 +03:00
|
|
|
auto fileData = FileData::get(file);
|
2019-04-03 11:53:04 +03:00
|
|
|
auto availability = syncFolder->vfs().availability(fileData.folderRelativePath);
|
2019-04-25 12:10:52 +03:00
|
|
|
if (!availability) {
|
|
|
|
if (availability.error() == Vfs::AvailabilityError::DbError)
|
|
|
|
availability = VfsItemAvailability::Mixed;
|
|
|
|
if (availability.error() == Vfs::AvailabilityError::NoSuchItem)
|
|
|
|
continue;
|
|
|
|
}
|
2019-04-03 11:53:04 +03:00
|
|
|
if (!combined) {
|
2019-04-25 12:10:52 +03:00
|
|
|
combined = *availability;
|
2019-04-03 11:53:04 +03:00
|
|
|
} else {
|
|
|
|
combined = merge(*combined, *availability);
|
2018-08-28 11:35:26 +03:00
|
|
|
}
|
2018-01-26 10:56:50 +03:00
|
|
|
}
|
2019-04-03 11:53:04 +03:00
|
|
|
|
|
|
|
// TODO: Should be a submenu, should use icons
|
|
|
|
auto makePinContextMenu = [&](bool makeAvailableLocally, bool freeSpace) {
|
|
|
|
listener->sendMessage(QLatin1String("MENU_ITEM:CURRENT_PIN:d:")
|
2019-04-16 11:17:21 +03:00
|
|
|
+ Utility::vfsCurrentAvailabilityText(*combined));
|
2019-04-03 11:53:04 +03:00
|
|
|
listener->sendMessage(QLatin1String("MENU_ITEM:MAKE_AVAILABLE_LOCALLY:")
|
|
|
|
+ (makeAvailableLocally ? QLatin1String(":") : QLatin1String("d:"))
|
2019-04-16 10:47:55 +03:00
|
|
|
+ Utility::vfsPinActionText());
|
2019-04-03 11:53:04 +03:00
|
|
|
listener->sendMessage(QLatin1String("MENU_ITEM:MAKE_ONLINE_ONLY:")
|
|
|
|
+ (freeSpace ? QLatin1String(":") : QLatin1String("d:"))
|
2019-04-16 10:47:55 +03:00
|
|
|
+ Utility::vfsFreeSpaceActionText());
|
2019-01-15 12:52:44 +03:00
|
|
|
};
|
|
|
|
|
2019-04-25 12:10:52 +03:00
|
|
|
if (combined) {
|
|
|
|
switch (*combined) {
|
|
|
|
case VfsItemAvailability::AlwaysLocal:
|
|
|
|
makePinContextMenu(false, true);
|
|
|
|
break;
|
|
|
|
case VfsItemAvailability::AllHydrated:
|
|
|
|
case VfsItemAvailability::Mixed:
|
|
|
|
makePinContextMenu(true, true);
|
|
|
|
break;
|
|
|
|
case VfsItemAvailability::AllDehydrated:
|
|
|
|
case VfsItemAvailability::OnlineOnly:
|
|
|
|
makePinContextMenu(true, false);
|
|
|
|
break;
|
|
|
|
}
|
2020-11-27 01:06:03 +03:00
|
|
|
}
|
2018-01-26 10:56:50 +03:00
|
|
|
}
|
|
|
|
|
2018-01-18 17:17:29 +03:00
|
|
|
listener->sendMessage(QString("GET_MENU_ITEMS:END"));
|
|
|
|
}
|
|
|
|
|
2020-01-18 17:12:16 +03:00
|
|
|
DirectEditor* SocketApi::getDirectEditorForLocalFile(const QString &localFile)
|
|
|
|
{
|
|
|
|
FileData fileData = FileData::get(localFile);
|
|
|
|
auto capabilities = fileData.folder->accountState()->account()->capabilities();
|
|
|
|
|
|
|
|
if (fileData.folder && fileData.folder->accountState()->isConnected()) {
|
2020-12-29 17:11:33 +03:00
|
|
|
const auto record = fileData.journalRecord();
|
|
|
|
const auto mimeMatchMode = record.isVirtualFile() ? QMimeDatabase::MatchExtension : QMimeDatabase::MatchDefault;
|
|
|
|
|
2020-01-18 17:12:16 +03:00
|
|
|
QMimeDatabase db;
|
2020-12-29 17:11:33 +03:00
|
|
|
QMimeType type = db.mimeTypeForFile(localFile, mimeMatchMode);
|
2020-01-18 17:12:16 +03:00
|
|
|
|
|
|
|
DirectEditor* editor = capabilities.getDirectEditorForMimetype(type);
|
|
|
|
if (!editor) {
|
|
|
|
editor = capabilities.getDirectEditorForOptionalMimetype(type);
|
|
|
|
}
|
|
|
|
return editor;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-03-30 14:24:04 +03:00
|
|
|
#if GUI_TESTING
|
|
|
|
void SocketApi::command_ASYNC_LIST_WIDGETS(const QSharedPointer<SocketApiJob> &job)
|
|
|
|
{
|
|
|
|
QString response;
|
2019-06-05 22:04:16 +03:00
|
|
|
for (auto &widget : allObjects(QApplication::allWidgets())) {
|
2017-03-30 14:24:04 +03:00
|
|
|
auto objectName = widget->objectName();
|
|
|
|
if (!objectName.isEmpty()) {
|
|
|
|
response += objectName + ":" + widget->property("text").toString() + ", ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
job->resolve(response);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SocketApi::command_ASYNC_INVOKE_WIDGET_METHOD(const QSharedPointer<SocketApiJob> &job)
|
|
|
|
{
|
|
|
|
auto &arguments = job->arguments();
|
|
|
|
|
|
|
|
auto widget = findWidget(arguments["objectName"].toString());
|
|
|
|
if (!widget) {
|
|
|
|
job->reject(QLatin1String("widget not found"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-23 15:22:45 +03:00
|
|
|
QMetaObject::invokeMethod(widget, arguments["method"].toString().toUtf8().constData());
|
2017-03-30 14:24:04 +03:00
|
|
|
job->resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SocketApi::command_ASYNC_GET_WIDGET_PROPERTY(const QSharedPointer<SocketApiJob> &job)
|
|
|
|
{
|
2019-06-06 12:48:31 +03:00
|
|
|
QString widgetName = job->arguments()[QLatin1String("objectName")].toString();
|
|
|
|
auto widget = findWidget(widgetName);
|
2017-03-30 14:24:04 +03:00
|
|
|
if (!widget) {
|
2019-06-06 12:48:31 +03:00
|
|
|
QString message = QString(QLatin1String("Widget not found: 2: %1")).arg(widgetName);
|
2019-06-05 22:04:16 +03:00
|
|
|
job->reject(message);
|
2017-03-30 14:24:04 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto propertyName = job->arguments()[QLatin1String("property")].toString();
|
|
|
|
|
2019-06-05 22:04:16 +03:00
|
|
|
auto segments = propertyName.split('.');
|
|
|
|
|
|
|
|
QObject* currentObject = widget;
|
|
|
|
QString value;
|
|
|
|
for(int i = 0;i<segments.count(); i++) {
|
|
|
|
auto segment = segments.at(i);
|
|
|
|
auto var = currentObject->property(segment.toUtf8().constData());
|
|
|
|
|
|
|
|
if(var.canConvert<QString>()) {
|
|
|
|
var.convert(QMetaType::QString);
|
|
|
|
value = var.value<QString>();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto tmpObject = var.value<QObject*>();
|
|
|
|
if(tmpObject) {
|
|
|
|
currentObject = tmpObject;
|
|
|
|
} else {
|
2019-06-06 12:48:31 +03:00
|
|
|
QString message = QString(QLatin1String("Widget not found: 3: %1")).arg(widgetName);
|
|
|
|
job->reject(message);
|
|
|
|
return;
|
2019-06-05 22:04:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-06 12:48:44 +03:00
|
|
|
job->resolve(value);
|
2017-03-30 14:24:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void SocketApi::command_ASYNC_SET_WIDGET_PROPERTY(const QSharedPointer<SocketApiJob> &job)
|
|
|
|
{
|
|
|
|
auto &arguments = job->arguments();
|
2019-06-06 12:48:31 +03:00
|
|
|
QString widgetName = arguments["objectName"].toString();
|
|
|
|
auto widget = findWidget(widgetName);
|
2017-03-30 14:24:04 +03:00
|
|
|
if (!widget) {
|
2019-06-06 12:48:31 +03:00
|
|
|
QString message = QString(QLatin1String("Widget not found: 4: %1")).arg(widgetName);
|
|
|
|
job->reject(message);
|
2017-03-30 14:24:04 +03:00
|
|
|
return;
|
|
|
|
}
|
2019-04-23 15:22:45 +03:00
|
|
|
widget->setProperty(arguments["property"].toString().toUtf8().constData(),
|
2019-06-06 12:48:44 +03:00
|
|
|
arguments["value"]);
|
|
|
|
|
2017-03-30 14:24:04 +03:00
|
|
|
job->resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SocketApi::command_ASYNC_WAIT_FOR_WIDGET_SIGNAL(const QSharedPointer<SocketApiJob> &job)
|
|
|
|
{
|
|
|
|
auto &arguments = job->arguments();
|
2019-06-06 12:48:31 +03:00
|
|
|
QString widgetName = arguments["objectName"].toString();
|
2017-03-30 14:24:04 +03:00
|
|
|
auto widget = findWidget(arguments["objectName"].toString());
|
|
|
|
if (!widget) {
|
2019-06-06 12:48:31 +03:00
|
|
|
QString message = QString(QLatin1String("Widget not found: 5: %1")).arg(widgetName);
|
|
|
|
job->reject(message);
|
2017-03-30 14:24:04 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ListenerClosure *closure = new ListenerClosure([job]() { job->resolve("signal emitted"); });
|
|
|
|
|
|
|
|
auto signalSignature = arguments["signalSignature"].toString();
|
|
|
|
signalSignature.prepend("2");
|
2019-04-23 15:22:45 +03:00
|
|
|
auto utf8 = signalSignature.toUtf8();
|
|
|
|
auto signalSignatureFinal = utf8.constData();
|
2017-03-30 14:24:04 +03:00
|
|
|
connect(widget, signalSignatureFinal, closure, SLOT(closureSlot()), Qt::QueuedConnection);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SocketApi::command_ASYNC_TRIGGER_MENU_ACTION(const QSharedPointer<SocketApiJob> &job)
|
|
|
|
{
|
|
|
|
auto &arguments = job->arguments();
|
|
|
|
|
|
|
|
auto objectName = arguments["objectName"].toString();
|
|
|
|
auto widget = findWidget(objectName);
|
|
|
|
if (!widget) {
|
2019-06-06 12:48:31 +03:00
|
|
|
QString message = QString(QLatin1String("Object not found: 1: %1")).arg(objectName);
|
|
|
|
job->reject(message);
|
2017-03-30 14:24:04 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto children = widget->findChildren<QWidget *>();
|
|
|
|
for (auto childWidget : children) {
|
|
|
|
// foo is the popupwidget!
|
|
|
|
auto actions = childWidget->actions();
|
|
|
|
for (auto action : actions) {
|
|
|
|
if (action->objectName() == arguments["actionName"].toString()) {
|
|
|
|
action->trigger();
|
|
|
|
|
|
|
|
job->resolve("action found");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-06 12:48:31 +03:00
|
|
|
QString message = QString(QLatin1String("Action not found: 1: %1")).arg(arguments["actionName"].toString());
|
|
|
|
job->reject(message);
|
2017-03-30 14:24:04 +03:00
|
|
|
}
|
2019-06-05 22:04:40 +03:00
|
|
|
|
|
|
|
void SocketApi::command_ASYNC_ASSERT_ICON_IS_EQUAL(const QSharedPointer<SocketApiJob> &job)
|
|
|
|
{
|
|
|
|
auto widget = findWidget(job->arguments()[QLatin1String("queryString")].toString());
|
|
|
|
if (!widget) {
|
2019-06-06 12:48:31 +03:00
|
|
|
QString message = QString(QLatin1String("Object not found: 6: %1")).arg(job->arguments()["queryString"].toString());
|
2019-06-05 22:04:40 +03:00
|
|
|
job->reject(message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto propertyName = job->arguments()[QLatin1String("propertyPath")].toString();
|
|
|
|
|
|
|
|
auto segments = propertyName.split('.');
|
|
|
|
|
|
|
|
QObject* currentObject = widget;
|
|
|
|
QIcon value;
|
|
|
|
for(int i = 0;i<segments.count(); i++) {
|
|
|
|
auto segment = segments.at(i);
|
|
|
|
auto var = currentObject->property(segment.toUtf8().constData());
|
|
|
|
|
|
|
|
if(var.canConvert<QIcon>()) {
|
|
|
|
var.convert(QMetaType::QIcon);
|
|
|
|
value = var.value<QIcon>();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto tmpObject = var.value<QObject*>();
|
|
|
|
if(tmpObject) {
|
|
|
|
currentObject = tmpObject;
|
|
|
|
} else {
|
2019-06-06 12:48:31 +03:00
|
|
|
job->reject(QString(QLatin1String("Icon not found: %1")).arg(propertyName));
|
2019-06-05 22:04:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto iconName = job->arguments()[QLatin1String("iconName")].toString();
|
|
|
|
if (value.name() == iconName) {
|
|
|
|
job->resolve();
|
|
|
|
} else {
|
|
|
|
job->reject("iconName " + iconName + " does not match: " + value.name());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2017-03-30 14:24:04 +03:00
|
|
|
#endif
|
|
|
|
|
2017-05-17 11:55:42 +03:00
|
|
|
QString SocketApi::buildRegisterPathMessage(const QString &path)
|
2015-03-13 20:30:45 +03:00
|
|
|
{
|
|
|
|
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
|
2018-04-06 18:13:29 +03:00
|
|
|
|
|
|
|
#include "socketapi.moc"
|