From 857df97f4091195ecc8314dfd24aae48375c2e32 Mon Sep 17 00:00:00 2001 From: Claudio Cambra Date: Wed, 15 Mar 2023 15:54:10 +0100 Subject: [PATCH] Improve initialisation of C++ FileProvider class Signed-off-by: Claudio Cambra --- src/gui/application.cpp | 2 +- src/gui/macOS/fileprovider.h | 10 ++++++++-- src/gui/macOS/fileprovider_mac.mm | 9 ++++++++- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/gui/application.cpp b/src/gui/application.cpp index ea6a796c96424..b6c06ee017da4 100644 --- a/src/gui/application.cpp +++ b/src/gui/application.cpp @@ -387,7 +387,7 @@ Application::Application(int &argc, char **argv) } #ifdef Q_OS_MACOS - _fileProvider.reset(Mac::FileProvider::instance()); + _fileProvider.reset(new Mac::FileProvider); #endif FolderMan::instance()->setSyncEnabled(true); diff --git a/src/gui/macOS/fileprovider.h b/src/gui/macOS/fileprovider.h index c974afb765b2c..d45ba987d2a5b 100644 --- a/src/gui/macOS/fileprovider.h +++ b/src/gui/macOS/fileprovider.h @@ -21,6 +21,8 @@ namespace OCC { +class Application; + namespace Mac { // NOTE: For the file provider extension to work, the app bundle will @@ -32,14 +34,18 @@ class FileProvider : public QObject public: static FileProvider *instance(); - ~FileProvider() = default; + ~FileProvider() override; static bool fileProviderAvailable(); private: - explicit FileProvider(QObject * const parent = nullptr); std::unique_ptr _domainManager; std::unique_ptr _socketServer; + + static FileProvider *_instance; + explicit FileProvider(QObject * const parent = nullptr); + + friend class OCC::Application; }; } // namespace Mac diff --git a/src/gui/macOS/fileprovider_mac.mm b/src/gui/macOS/fileprovider_mac.mm index 879a39098a4f8..9cb5b739bd5e3 100644 --- a/src/gui/macOS/fileprovider_mac.mm +++ b/src/gui/macOS/fileprovider_mac.mm @@ -24,11 +24,13 @@ namespace Mac { -static FileProvider *_instance = nullptr; +FileProvider* FileProvider::_instance = nullptr; FileProvider::FileProvider(QObject * const parent) : QObject(parent) { + Q_ASSERT(!_instance); + if (!fileProviderAvailable()) { qCDebug(lcMacFileProvider) << "File provider system is not available on this version of macOS."; return; @@ -61,6 +63,11 @@ return _instance; } +FileProvider::~FileProvider() +{ + _instance = nullptr; +} + bool FileProvider::fileProviderAvailable() { if (@available(macOS 11.0, *)) {