Skip to content

Commit

Permalink
Clean-ups before switching macOS to local sockets
Browse files Browse the repository at this point in the history
- introduce the Platform class, which can do RAII style set-up/tear-down
- macOS: move Finder integration into the Utility namespace
- move retrieve the socket path into the Utility namespace
  • Loading branch information
erikjv committed Nov 17, 2021
1 parent 4c726d7 commit 7fe14d0
Show file tree
Hide file tree
Showing 13 changed files with 379 additions and 139 deletions.
12 changes: 9 additions & 3 deletions src/gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ set(client_SRCS
quotainfo.cpp
accountstate.cpp
authenticationdialog.cpp
platform.cpp
proxyauthhandler.cpp
proxyauthdialog.cpp
tooltipupdater.cpp
Expand Down Expand Up @@ -151,10 +152,10 @@ IF( APPLE )
target_link_libraries(owncloudCore PUBLIC Qt5::MacExtras)

target_sources(owncloudCore PRIVATE
cocoainitializer_mac.mm
platform_mac.mm
systray.mm
settingsdialog_mac.mm

guiutility_mac.mm
folderwatcher_mac.cpp)

if(SPARKLE_FOUND)
Expand All @@ -167,6 +168,8 @@ IF( APPLE )
endif()
elseif( WIN32 )
target_sources(owncloudCore PRIVATE
platform_win.cpp
guiutility_win.cpp
folderwatcher_win.cpp
navigationpanehelper.cpp)
elseif(UNIX AND NOT APPLE )
Expand All @@ -176,7 +179,10 @@ elseif(UNIX AND NOT APPLE )
target_link_libraries(owncloudCore PUBLIC Qt5::DBus)
target_compile_definitions(owncloudCore PUBLIC "USE_FDO_NOTIFICATIONS")
endif()
target_sources(owncloudCore PRIVATE folderwatcher_linux.cpp)
target_sources(owncloudCore PRIVATE
folderwatcher_linux.cpp
platform_unix.cpp
guiutility_unix.cpp)
endif()


Expand Down
10 changes: 6 additions & 4 deletions src/gui/guiutility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@

#include "common/asserts.h"

using namespace OCC;
namespace OCC {
Q_LOGGING_CATEGORY(lcGuiUtility, "gui.utility", QtInfoMsg)
}

Q_LOGGING_CATEGORY(lcUtility, "gui.utility", QtInfoMsg)
using namespace OCC;

bool Utility::openBrowser(const QUrl &url, QWidget *errorWidgetParent)
{
Expand All @@ -42,7 +44,7 @@ bool Utility::openBrowser(const QUrl &url, QWidget *errorWidgetParent)
"URL %1. Maybe no default browser is configured?")
.arg(url.toString()));
}
qCWarning(lcUtility) << "QDesktopServices::openUrl failed for" << url;
qCWarning(lcGuiUtility) << "QDesktopServices::openUrl failed for" << url;
return false;
}
return true;
Expand All @@ -66,7 +68,7 @@ bool Utility::openEmailComposer(const QString &subject, const QString &body, QWi
"create a new message. Maybe no default email client is "
"configured?"));
}
qCWarning(lcUtility) << "QDesktopServices::openUrl failed for" << url;
qCWarning(lcGuiUtility) << "QDesktopServices::openUrl failed for" << url;
return false;
}
return true;
Expand Down
10 changes: 10 additions & 0 deletions src/gui/guiutility.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@
#ifndef GUIUTILITY_H
#define GUIUTILITY_H

#include <QLoggingCategory>
#include <QString>
#include <QUrl>
#include <QWidget>

#include "common/pinstate.h"

namespace OCC {

Q_DECLARE_LOGGING_CATEGORY(lcGuiUtility)

namespace Utility {

/** Open an url in the browser.
Expand Down Expand Up @@ -51,6 +55,12 @@ namespace Utility {

QIcon getCoreIcon(const QString &icon_name);

void tweakUIStyle();

void startShellIntegration();

QString socketApiSocketPath();

} // namespace Utility
} // namespace OCC

Expand Down
62 changes: 62 additions & 0 deletions src/gui/guiutility_mac.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (C) by Daniel Molkentin <[email protected]>
* Copyright (C) by Erik Verbruggen <[email protected]>
*
* 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.
*/

#include "application.h"
#include "guiutility.h"

#include <QProcess>

#import <Foundation/NSBundle.h>

namespace OCC {

void Utility::tweakUIStyle()
{
}

void Utility::startShellIntegration()
{
QString bundlePath = QUrl::fromNSURL([NSBundle mainBundle].bundleURL).path();

auto _system = [](const QString &cmd, const QStringList &args) {
QProcess process;
process.setProcessChannelMode(QProcess::MergedChannels);
process.start(cmd, args);
if (!process.waitForFinished()) {
qCWarning(lcGuiUtility) << "Failed to load shell extension:" << cmd
<< args.join(QLatin1Char(' ')) << process.errorString();
} else {
qCInfo(lcGuiUtility) << (process.exitCode() != 0 ? "Failed to load" : "Loaded")
<< "shell extension:" << cmd << args.join(QLatin1Char(' '))
<< process.readAll();
}
};

// Add it again. This was needed for Mojave to trigger a load.
_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") });
}

QString Utility::socketApiSocketPath()
{
// 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"
return QLatin1String(SOCKETAPI_TEAM_IDENTIFIER_PREFIX APPLICATION_REV_DOMAIN ".socketApi");
}

} // namespace OCC
36 changes: 36 additions & 0 deletions src/gui/guiutility_unix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (C) by Erik Verbruggen <[email protected]>
*
* 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.
*/

#include "guiutility.h"
#include "theme.h"

#include <QStandardPaths>

namespace OCC {

void Utility::tweakUIStyle()
{
}

void Utility::startShellIntegration()
{
}

QString Utility::socketApiSocketPath()
{
QString runtimeDir = QStandardPaths::writableLocation(QStandardPaths::RuntimeLocation);
return runtimeDir + "/" + Theme::instance()->appName() + "/socket";
}

} // namespace OCC
47 changes: 47 additions & 0 deletions src/gui/guiutility_win.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (C) by Erik Verbruggen <[email protected]>
*
* 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.
*/

#include "application.h"
#include "guiutility.h"

#include <QCoreApplication>

namespace OCC {

void Utility::tweakUIStyle()
{
// The Windows style still has pixelated elements with Qt 5.6,
// it's recommended to use the Fusion style in this case, even
// though it looks slightly less native. Check here after the
// QApplication was constructed, but before any QWidget is
// constructed.
if (qGuiApp->devicePixelRatio() > 1) {
QApplication::setStyle(QStringLiteral("fusion"));
}
}

void Utility::startShellIntegration()
{
}

QString Utility::socketApiSocketPath()
{
return QLatin1String("\\\\.\\pipe\\") + QLatin1String("ownCloud-") + qEnvironmentVariable("USERNAME");
// TODO: once the windows extension supports multiple
// client connections, switch back to the theme name
// See issue #2388
// + Theme::instance()->appName();
}

} // namespace OCC
53 changes: 10 additions & 43 deletions src/gui/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,11 @@
*/
#include <QtGlobal>

#include <signal.h>

#ifdef Q_OS_UNIX
#include <sys/time.h>
#include <sys/resource.h>
#endif

#include "application.h"
#include "theme.h"
#include "common/utility.h"
#include "cocoainitializer.h"
#include "guiutility.h"
#include "platform.h"
#include "theme.h"

#include "updater/updater.h"

Expand All @@ -48,43 +42,16 @@ int main(int argc, char **argv)
Q_INIT_RESOURCE(client);

QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps, true);
#ifdef Q_OS_WIN
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling, true);
#endif // !Q_OS_WIN

#ifdef Q_OS_MAC
Mac::CocoaInitializer cocoaInit; // RIIA
#endif
// Create a `Platform` instance so it can set-up/tear-down stuff for us, and do any
// initialisation that needs to be done before creating a QApplication
auto platform = Platform::create();

// Create the (Q)Application instance:
OCC::Application app(argc, argv);

#ifdef Q_OS_WIN
// The Windows style still has pixelated elements with Qt 5.6,
// it's recommended to use the Fusion style in this case, even
// though it looks slightly less native. Check here after the
// QApplication was constructed, but before any QWidget is
// constructed.
if (app.devicePixelRatio() > 1)
QApplication::setStyle(QStringLiteral("fusion"));
#endif // Q_OS_WIN

#ifndef Q_OS_WIN
signal(SIGPIPE, SIG_IGN);
#endif

// check a environment variable for core dumps
#ifdef Q_OS_UNIX
if (!qEnvironmentVariableIsEmpty("OWNCLOUD_CORE_DUMP")) {
struct rlimit core_limit;
core_limit.rlim_cur = RLIM_INFINITY;
core_limit.rlim_max = RLIM_INFINITY;

if (setrlimit(RLIMIT_CORE, &core_limit) < 0) {
fprintf(stderr, "Unable to set core dump limit\n");
} else {
qCInfo(lcApplication) << "Core dumps enabled";
}
}
#endif
Utility::tweakUIStyle();

// if handleStartup returns true, main()
// needs to terminate here, e.g. because
// the updater is triggered
Expand Down
23 changes: 23 additions & 0 deletions src/gui/platform.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (C) by Erik Verbruggen <[email protected]>
*
* 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.
*/

#include "platform.h"

namespace OCC {

Platform::~Platform()
{
}

} // OCC namespace
29 changes: 13 additions & 16 deletions src/gui/cocoainitializer.h → src/gui/platform.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) by Daniel Molkentin <[email protected]>
* Copyright (C) by Erik Verbruggen <[email protected]>
*
* 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
Expand All @@ -12,23 +12,20 @@
* for more details.
*/

#include <memory>

namespace OCC {
namespace Mac {

/**
* @brief CocoaInitializer provides an AutoRelease Pool via RIIA for use in main()
* @ingroup gui
/**
* @brief The Platform is the baseclass for all platform classes, which in turn implement platform
* specific functionality for the GUI.
*/
class CocoaInitializer
{
public:
CocoaInitializer();
~CocoaInitializer();
class Platform
{
public:
virtual ~Platform() = 0;

private:
class Private;
Private *d;
};
static std::unique_ptr<Platform> create();
};

} // namespace Mac
} // namespace OCC
} // OCC namespace
Loading

0 comments on commit 7fe14d0

Please sign in to comment.