Skip to content

Commit

Permalink
Adress review comments.
Browse files Browse the repository at this point in the history
Signed-off-by: allexzander <blackslayer4@gmail.com>
  • Loading branch information
allexzander committed Jul 28, 2022
1 parent 601a55b commit fd30889
Show file tree
Hide file tree
Showing 24 changed files with 432 additions and 256 deletions.
13 changes: 0 additions & 13 deletions src/common/cfapishellextensionsipcconstants.h

This file was deleted.

2 changes: 1 addition & 1 deletion src/common/filesystembase.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include <QFileInfo>
#include <QLoggingCategory>

#include <ocsynclib.h>
#include <csync/ocsynclib.h>

class QFile;

Expand Down
36 changes: 36 additions & 0 deletions src/common/shellextensionutils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "shellextensionutils.h"
#include <QJsonDocument>
#include <QLoggingCategory>

namespace VfsShellExtensions {

Q_LOGGING_CATEGORY(lcShellExtensionUtils, "nextcloud.gui.shellextensionutils", QtInfoMsg)

QString VfsShellExtensions::serverNameForApplicationName(const QString &applicationName)
{
return applicationName + QStringLiteral(":VfsShellExtensionsServer");
}

QString VfsShellExtensions::serverNameForApplicationNameDefault()
{
return serverNameForApplicationName(APPLICATION_NAME);
}
namespace Protocol {
QByteArray createJsonMessage(const QVariantMap &message)
{
QVariantMap messageCopy = message;
messageCopy[QStringLiteral("version")] = Version;
return QJsonDocument::fromVariant((messageCopy)).toJson(QJsonDocument::Compact);
}

bool validatProtocolVersion(const QVariantMap &message)
{
const auto valid = message.value(QStringLiteral("version")) == Version;
if (!valid) {
qCWarning(lcShellExtensionUtils) << "Invalid shell extensions IPC protocol: " << message.value(QStringLiteral("version")) << " vs " << Version;
}
Q_ASSERT(valid);
return valid;
}
}
}
35 changes: 35 additions & 0 deletions src/common/shellextensionutils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (C) by Oleksandr Zolotov <alex@nextcloud.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) 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.
*/

#pragma once
#include "config.h"
#include <QByteArray>
#include <QString>
#include <QVariantMap>

namespace VfsShellExtensions {
QString serverNameForApplicationName(const QString &applicationName);
QString serverNameForApplicationNameDefault();

namespace Protocol {
static constexpr auto ThumbnailProviderRequestKey = "thumbnailProviderRequest";
static constexpr auto ThumbnailProviderRequestFilePathKey = "filePath";
static constexpr auto ThumbnailProviderRequestFileSizeKey = "size";
static constexpr auto ThumnailProviderDataKey = "thumbnailData";
static constexpr auto Version = "1.0";

QByteArray createJsonMessage(const QVariantMap &message);
bool validatProtocolVersion(const QVariantMap &message);
}
}
3 changes: 2 additions & 1 deletion src/common/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#define UTILITY_H


#include "ocsynclib.h"
#include "csync/ocsynclib.h"
#include <QString>
#include <QByteArray>
#include <QDateTime>
Expand Down Expand Up @@ -254,6 +254,7 @@ namespace Utility {
OCSYNC_EXPORT bool registryDeleteKeyTree(HKEY hRootKey, const QString &subKey);
OCSYNC_EXPORT bool registryDeleteKeyValue(HKEY hRootKey, const QString &subKey, const QString &valueName);
OCSYNC_EXPORT bool registryWalkSubKeys(HKEY hRootKey, const QString &subKey, const std::function<void(HKEY, const QString &)> &callback);
OCSYNC_EXPORT bool registryWalkValues(HKEY hRootKey, const QString &subKey, const std::function<void(const QString &, bool *)> &callback);
OCSYNC_EXPORT QRect getTaskbarDimensions();

// Possibly refactor to share code with UnixTimevalToFileTime in c_time.c
Expand Down
49 changes: 48 additions & 1 deletion src/common/utility_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@
#include <winbase.h>
#include <windows.h>
#include <winerror.h>

#include <QCoreApplication>
#include <QDir>
#include <QFile>
#include <QLibrary>
#include <QSettings>

extern Q_CORE_EXPORT int qt_ntfs_permission_lookup;

Expand Down Expand Up @@ -354,6 +357,50 @@ bool Utility::registryWalkSubKeys(HKEY hRootKey, const QString &subKey, const st
return retCode != ERROR_NO_MORE_ITEMS;
}

bool Utility::registryWalkValues(HKEY hRootKey, const QString &subKey, const std::function<void(const QString &, bool *)> &callback)
{
HKEY hKey;
REGSAM sam = KEY_QUERY_VALUE;
LONG result = RegOpenKeyEx(hRootKey, reinterpret_cast<LPCWSTR>(subKey.utf16()), 0, sam, &hKey);
ASSERT(result == ERROR_SUCCESS);
if (result != ERROR_SUCCESS) {
return false;
}

DWORD maxValueNameSize = 0;
result = RegQueryInfoKey(hKey, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, &maxValueNameSize, nullptr, nullptr, nullptr);
ASSERT(result == ERROR_SUCCESS);
if (result != ERROR_SUCCESS) {
RegCloseKey(hKey);
return false;
}

QString valueName;
valueName.reserve(maxValueNameSize + 1);

DWORD retCode = ERROR_SUCCESS;
bool done = false;
for (DWORD i = 0; retCode == ERROR_SUCCESS; ++i) {
Q_ASSERT(unsigned(valueName.capacity()) > maxValueNameSize);
valueName.resize(valueName.capacity());
DWORD valueNameSize = valueName.size();
retCode = RegEnumValue(hKey, i, reinterpret_cast<LPWSTR>(valueName.data()), &valueNameSize, nullptr, nullptr, nullptr, nullptr);

ASSERT(result == ERROR_SUCCESS || retCode == ERROR_NO_MORE_ITEMS);
if (retCode == ERROR_SUCCESS) {
valueName.resize(valueNameSize);
callback(valueName, &done);

if (done) {
break;
}
}
}

RegCloseKey(hKey);
return retCode != ERROR_NO_MORE_ITEMS;
}

DWORD Utility::convertSizeToDWORD(size_t &convertVar)
{
if( convertVar > UINT_MAX ) {
Expand Down
2 changes: 2 additions & 0 deletions src/gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ set(client_SRCS
profilepagewidget.cpp
sharee.h
sharee.cpp
shellextensionsserver.cpp
sslbutton.h
sslbutton.cpp
sslerrordialog.h
Expand Down Expand Up @@ -182,6 +183,7 @@ set(client_SRCS
iconutils.cpp
remotewipe.h
remotewipe.cpp
${CMAKE_SOURCE_DIR}/src/common/shellextensionutils.cpp
userstatusselectormodel.h
userstatusselectormodel.cpp
emojimodel.h
Expand Down
2 changes: 2 additions & 0 deletions src/gui/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "accountmanager.h"
#include "creds/abstractcredentials.h"
#include "pushnotifications.h"
#include "shellextensionsserver.h"

#if defined(BUILD_UPDATER)
#include "updater/ocupdater.h"
Expand Down Expand Up @@ -319,6 +320,7 @@ Application::Application(int &argc, char **argv)
qCInfo(lcApplication) << "VFS suffix plugin is available";

_folderManager.reset(new FolderMan);
_shellExtensionsServer.reset(new ShellExtensionsServer);

connect(this, &SharedTools::QtSingleApplication::messageReceived, this, &Application::slotParseMessage);

Expand Down
2 changes: 2 additions & 0 deletions src/gui/application.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Q_DECLARE_LOGGING_CATEGORY(lcApplication)

class Theme;
class Folder;
class ShellExtensionsServer;
class SslErrorDialog;

/**
Expand Down Expand Up @@ -144,6 +145,7 @@ protected slots:
QScopedPointer<CrashReporter::Handler> _crashHandler;
#endif
QScopedPointer<FolderMan> _folderManager;
QScopedPointer<ShellExtensionsServer> _shellExtensionsServer;
};

} // namespace OCC
Expand Down
90 changes: 0 additions & 90 deletions src/gui/folder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
#include "localdiscoverytracker.h"
#include "csync_exclude.h"
#include "common/vfs.h"
#include "common/cfapishellextensionsipcconstants.h"
#include "creds/abstractcredentials.h"
#include "settingsdialog.h"

Expand All @@ -47,8 +46,6 @@
#include <QPushButton>
#include <QApplication>

#include <QLocalSocket>

static const char versionC[] = "version";

namespace OCC {
Expand Down Expand Up @@ -147,7 +144,6 @@ Folder::Folder(const FolderDefinition &definition,

Folder::~Folder()
{
stopShellExtensionServer();
// If wipeForRemoval() was called the vfs has already shut down.
if (_vfs)
_vfs->stop();
Expand Down Expand Up @@ -1223,74 +1219,6 @@ void Folder::slotHydrationDone()
emit syncStateChange();
}

void Folder::slotNewShellExtensionConnection()
{
const auto newConnection = _shellExtensionsServer.nextPendingConnection();

connect(newConnection, &QLocalSocket::errorOccurred, this, [newConnection](QLocalSocket::LocalSocketError socketError) {
qCCritical(lcFolder) << "Shell extension socket error: " << socketError << " : " << newConnection->errorString();
});

const auto disconnectAndCloseSocket = [newConnection, this]() {
connect(newConnection, &QLocalSocket::disconnected, this, [newConnection] {
newConnection->close();
newConnection->deleteLater();
});
newConnection->disconnectFromServer();
};

const auto sendEmptyData = [newConnection]() {
newConnection->write({});
newConnection->waitForBytesWritten();
};

if (!newConnection->waitForReadyRead()) {
disconnectAndCloseSocket();
return;
}

const auto receivedMessage = QJsonDocument::fromJson(newConnection->readAll()).toVariant().toMap();

const auto thumbnailRequestMessage = receivedMessage.value(CfApiShellExtensions::Protocol::ThumbnailProviderRequestKey).toMap();

const auto thumbnailFilePath = thumbnailRequestMessage.value(CfApiShellExtensions::Protocol::ThumbnailProviderRequestFilePathKey).toString();
const auto thumbnailFileSize = thumbnailRequestMessage.value(CfApiShellExtensions::Protocol::ThumbnailProviderRequestFileSizeKey).toMap();

if (thumbnailFilePath.isEmpty() || thumbnailFileSize.isEmpty()) {
disconnectAndCloseSocket();
return;
}

const auto fileInfo = QFileInfo(thumbnailFilePath);
const auto filePathRelative = QFileInfo(thumbnailFilePath).canonicalFilePath().remove(path());

SyncJournalFileRecord record;
if (!_journal.getFileRecord(filePathRelative, &record)) {
sendEmptyData();
disconnectAndCloseSocket();
return;
}

QUrlQuery queryItems;
queryItems.addQueryItem("fileId", record._fileId);
queryItems.addQueryItem("x", thumbnailFileSize.value("x").toString());
queryItems.addQueryItem("y", thumbnailFileSize.value("y").toString());
const QUrl jobUrl = Utility::concatUrlPath(accountState()->account()->url(), "core/preview", queryItems);
const auto job = new SimpleNetworkJob(accountState()->account());
job->startRequest("GET", jobUrl);
connect(job, &SimpleNetworkJob::finishedSignal, this, [newConnection, disconnectAndCloseSocket, sendEmptyData](QNetworkReply *reply) {
const auto contentType = reply->header(QNetworkRequest::ContentTypeHeader).toByteArray();
if (!contentType.startsWith("image/")) {
sendEmptyData();
disconnectAndCloseSocket();
return;
}
newConnection->write(reply->readAll());
newConnection->waitForBytesWritten();
disconnectAndCloseSocket();
});
}

void Folder::scheduleThisFolderSoon()
{
if (!_scheduleSelfTimer.isActive()) {
Expand Down Expand Up @@ -1376,24 +1304,6 @@ QString Folder::fileFromLocalPath(const QString &localPath) const
return localPath.mid(cleanPath().length() + 1);
}

void Folder::startShellExtensionServer(const QString &serverName)
{
if (_shellExtensionsServer.isListening()) {
return;
}
if (_shellExtensionsServer.listen(serverName)) {
connect(&_shellExtensionsServer, &QLocalServer::newConnection, this, &Folder::slotNewShellExtensionConnection);
}
}

void Folder::stopShellExtensionServer()
{
if (!_shellExtensionsServer.isListening()) {
return;
}
_shellExtensionsServer.close();
}

void FolderDefinition::save(QSettings &settings, const FolderDefinition &folder)
{
settings.setValue(QLatin1String("localPath"), folder.localPath);
Expand Down
9 changes: 0 additions & 9 deletions src/gui/folder.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
#include <chrono>
#include <memory>

#include <QLocalServer>

class QThread;
class QSettings;

Expand Down Expand Up @@ -300,9 +298,6 @@ class Folder : public QObject

QString fileFromLocalPath(const QString &localPath) const;

void startShellExtensionServer(const QString &serverName);
void stopShellExtensionServer();

signals:
void syncStateChange();
void syncStarted();
Expand Down Expand Up @@ -426,8 +421,6 @@ private slots:
/** Unblocks normal sync operation */
void slotHydrationDone();

void slotNewShellExtensionConnection();

private:
void connectSyncRoot();

Expand Down Expand Up @@ -531,8 +524,6 @@ private slots:
* The vfs mode instance (created by plugin) to use. Never null.
*/
QSharedPointer<Vfs> _vfs;

QLocalServer _shellExtensionsServer;
};
}

Expand Down
Loading

0 comments on commit fd30889

Please sign in to comment.