Add OSUtils for platform-specific functionality.

Moves MacUtils into a separate sub folder and adds
WinUtils, NixUtils, and OSUtils for platform-native
code on Windows, Unix-like, and generic/all systems.
This commit is contained in:
Janek Bevendorff 2020-01-28 21:42:57 +01:00
parent b9daed2055
commit 6d2ca74878
21 changed files with 375 additions and 23 deletions

View File

@ -19,7 +19,7 @@ set(EXCLUDED_DIRS
# objective-c directories
src/touchid/
src/autotype/mac/
src/gui/macutils/)
src/gui/osutils/macutils/)
set(EXCLUDED_FILES
# third-party files

View File

@ -156,6 +156,7 @@ set(keepassx_SOURCES
gui/reports/ReportsPageHealthcheck.cpp
gui/reports/ReportsWidgetStatistics.cpp
gui/reports/ReportsPageStatistics.cpp
gui/osutils/OSUtilsBase.cpp
gui/settings/SettingsWidget.cpp
gui/widgets/ElidedLabel.cpp
gui/widgets/PopupHelpWidget.cpp
@ -181,20 +182,22 @@ if(APPLE)
${keepassx_SOURCES}
core/ScreenLockListenerMac.cpp
core/MacPasteboard.cpp
gui/macutils/MacUtils.cpp
gui/macutils/AppKitImpl.mm
gui/macutils/AppKit.h)
gui/osutils/macutils/MacUtils.cpp
gui/osutils/macutils/AppKitImpl.mm
gui/osutils/macutils/AppKit.h)
endif()
if(UNIX AND NOT APPLE)
set(keepassx_SOURCES
${keepassx_SOURCES}
core/ScreenLockListenerDBus.cpp
gui/MainWindowAdaptor.cpp)
gui/MainWindowAdaptor.cpp
gui/osutils/nixutils/NixUtils.cpp)
endif()
if(MINGW)
set(keepassx_SOURCES
${keepassx_SOURCES}
core/ScreenLockListenerWin.cpp)
core/ScreenLockListenerWin.cpp
gui/osutils/winutils/WinUtils.cpp)
endif()
if(MINGW OR (UNIX AND NOT APPLE))
set(keepassx_SOURCES

View File

@ -38,7 +38,7 @@
#include "gui/MessageBox.h"
#ifdef Q_OS_MAC
#include "gui/macutils/MacUtils.h"
#include "gui/osutils/macutils/MacUtils.h"
#endif
AutoType* AutoType::m_instance = nullptr;

View File

@ -17,7 +17,7 @@
*/
#include "AutoTypeMac.h"
#include "gui/macutils/MacUtils.h"
#include "gui/osutils/macutils/MacUtils.h"
#include "gui/MessageBox.h"
#include <ApplicationServices/ApplicationServices.h>

View File

@ -39,7 +39,7 @@
#include "gui/MainWindow.h"
#include "gui/MessageBox.h"
#ifdef Q_OS_MACOS
#include "gui/macutils/MacUtils.h"
#include "gui/osutils/macutils/MacUtils.h"
#endif
const QString BrowserService::KEEPASSXCBROWSER_NAME = QStringLiteral("KeePassXC-Browser Settings");

View File

@ -41,7 +41,7 @@
#include "gui/entry/EntryView.h"
#include "gui/group/GroupView.h"
#ifdef Q_OS_MACOS
#include "gui/macutils/MacUtils.h"
#include "gui/osutils/macutils/MacUtils.h"
#endif
#include "gui/wizard/NewDatabaseWizard.h"

View File

@ -28,7 +28,7 @@
#include "core/Tools.h"
#include "gui/IconModels.h"
#ifdef Q_OS_MACOS
#include "gui/macutils/MacUtils.h"
#include "gui/osutils/macutils/MacUtils.h"
#endif
#include <QMutexLocker>

View File

@ -43,7 +43,7 @@
#include "keys/PasswordKey.h"
#ifdef Q_OS_MACOS
#include "macutils/MacUtils.h"
#include "gui/osutils/macutils/MacUtils.h"
#endif
#ifdef WITH_XC_UPDATECHECK

View File

@ -31,7 +31,7 @@
#include "core/Group.h"
#include "core/Metadata.h"
#ifdef Q_OS_MACOS
#include "gui/macutils/MacUtils.h"
#include "gui/osutils/macutils/MacUtils.h"
#endif
EntryModel::EntryModel(QObject* parent)

41
src/gui/osutils/OSUtils.h Normal file
View File

@ -0,0 +1,41 @@
/*
* Copyright (C) 2020 KeePassXC Team <team@keepassxc.org>
*
* 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 or (at your option)
* version 3 of the License.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef KEEPASSXC_OSUTILS_H
#define KEEPASSXC_OSUTILS_H
#include "OSUtilsBase.h"
#include <QtCore>
#if defined(Q_OS_WIN)
#include "winutils/WinUtils.h"
#define osUtils static_cast<OSUtilsBase*>(winUtils())
#elif defined(Q_OS_MACOS)
#include "macutils/MacUtils.h"
#define osUtils static_cast<OSUtilsBase*>(macUtils())
#elif defined(Q_OS_UNIX)
#include "nixutils/NixUtils.h"
#define osUtils static_cast<OSUtilsBase*>(nixUtils())
#endif
#endif // KEEPASSXC_OSUTILS_H

View File

@ -0,0 +1,27 @@
/*
* Copyright (C) 2020 KeePassXC Team <team@keepassxc.org>
*
* 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 or (at your option)
* version 3 of the License.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "OSUtilsBase.h"
OSUtilsBase::OSUtilsBase(QObject* parent)
: QObject(parent)
{
}
OSUtilsBase::~OSUtilsBase()
{
}

View File

@ -0,0 +1,40 @@
/*
* Copyright (C) 2020 KeePassXC Team <team@keepassxc.org>
*
* 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 or (at your option)
* version 3 of the License.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef KEEPASSXC_OSUTILSBASE_H
#define KEEPASSXC_OSUTILSBASE_H
#include <QObject>
#include <QPointer>
/**
* Abstract base class for generic OS-specific functionality
* which can be reasonably expected to be available on all platforms.
*/
class OSUtilsBase : public QObject
{
Q_OBJECT
public:
virtual bool isDarkMode() = 0;
protected:
explicit OSUtilsBase(QObject* parent = nullptr);
virtual ~OSUtilsBase();
};
#endif // KEEPASSXC_OSUTILSBASE_H

View File

@ -19,10 +19,10 @@
#include "MacUtils.h"
#include <QApplication>
MacUtils* MacUtils::m_instance = nullptr;
QPointer<MacUtils> MacUtils::m_instance = nullptr;
MacUtils::MacUtils(QObject* parent)
: QObject(parent)
: OSUtils(parent)
, m_appkit(new AppKit())
{
connect(m_appkit.data(), SIGNAL(lockDatabases()), SIGNAL(lockDatabases()));

View File

@ -19,17 +19,16 @@
#ifndef KEEPASSXC_MACUTILS_H
#define KEEPASSXC_MACUTILS_H
#include "gui/osutils/OSUtilsBase.h"
#include "AppKit.h"
#include <QObject>
#include <QWidget>
#include <QPointer>
class MacUtils : public QObject
class MacUtils : public OSUtils
{
Q_OBJECT
public:
static MacUtils* instance();
static void createTestInstance();
WId activeWindow();
bool raiseWindow(WId pid);
@ -37,20 +36,20 @@ public:
bool raiseOwnWindow();
bool hideOwnWindow();
bool isHidden();
bool isDarkMode();
bool isDarkMode() override;
bool enableAccessibility();
bool enableScreenRecording();
signals:
void lockDatabases();
private:
protected:
explicit MacUtils(QObject* parent = nullptr);
~MacUtils();
~MacUtils() override;
private:
QScopedPointer<AppKit> m_appkit;
static MacUtils* m_instance;
static QPointer<MacUtils> m_instance;
Q_DISABLE_COPY(MacUtils)
};

View File

@ -0,0 +1,50 @@
/*
* Copyright (C) 2020 KeePassXC Team <team@keepassxc.org>
*
* 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 or (at your option)
* version 3 of the License.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "NixUtils.h"
#include <QApplication>
#include <QColor>
#include <QPalette>
#include <QStyle>
QPointer<NixUtils> NixUtils::m_instance = nullptr;
NixUtils* NixUtils::instance()
{
if (!m_instance) {
m_instance = new NixUtils(qApp);
}
return m_instance;
}
NixUtils::NixUtils(QObject* parent)
: OSUtilsBase(parent)
{
}
NixUtils::~NixUtils()
{
}
bool NixUtils::isDarkMode()
{
if (!qApp || !qApp->style()) {
return false;
}
return qApp->style()->standardPalette().color(QPalette::Window).toHsl().lightness() < 110;
}

View File

@ -0,0 +1,48 @@
/*
* Copyright (C) 2020 KeePassXC Team <team@keepassxc.org>
*
* 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 or (at your option)
* version 3 of the License.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef KEEPASSXC_NIXUTILS_H
#define KEEPASSXC_NIXUTILS_H
#include "gui/osutils/OSUtilsBase.h"
#include <QPointer>
class NixUtils : public OSUtilsBase
{
Q_OBJECT
public:
static NixUtils* instance();
bool isDarkMode() override;
private:
explicit NixUtils(QObject* parent = nullptr);
~NixUtils() override;
private:
static QPointer<NixUtils> m_instance;
Q_DISABLE_COPY(NixUtils)
};
inline NixUtils* nixUtils()
{
return NixUtils::instance();
}
#endif // KEEPASSXC_NIXUTILS_H

View File

@ -0,0 +1,85 @@
/*
* Copyright (C) 2020 KeePassXC Team <team@keepassxc.org>
*
* 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 or (at your option)
* version 3 of the License.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "WinUtils.h"
#include <QAbstractNativeEventFilter>
#include <QApplication>
#include <QSettings>
#include <windows.h>
QPointer<WinUtils> WinUtils::m_instance = nullptr;
QScopedPointer<WinUtils::DWMEventFilter> WinUtils::m_eventFilter;
WinUtils* WinUtils::instance()
{
if (!m_instance) {
m_instance = new WinUtils(qApp);
}
return m_instance;
}
WinUtils::WinUtils(QObject* parent)
: OSUtilsBase(parent)
{
}
WinUtils::~WinUtils()
{
}
/**
* Register event filters to handle native platform events such as theme changes.
*/
void WinUtils::registerEventFilters()
{
if (!m_eventFilter) {
m_eventFilter.reset(new DWMEventFilter);
qApp->installNativeEventFilter(m_eventFilter.data());
}
}
bool WinUtils::DWMEventFilter::nativeEventFilter(const QByteArray& eventType, void* message, long*)
{
if (eventType != "windows_generic_MSG") {
return false;
}
auto* msg = static_cast<MSG*>(message);
if (!msg->hwnd) {
return false;
}
switch (msg->message) {
case WM_CREATE:
case WM_INITDIALOG: {
if (winUtils()->isDarkMode()) {
// TODO: indicate dark mode support for black title bar
}
break;
}
}
return false;
}
bool WinUtils::isDarkMode()
{
QSettings settings(R"(HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize)",
QSettings::NativeFormat);
return settings.value("AppsUseLightTheme", 1).toInt() == 0;
}

View File

@ -0,0 +1,59 @@
/*
* Copyright (C) 2020 KeePassXC Team <team@keepassxc.org>
*
* 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 or (at your option)
* version 3 of the License.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef KEEPASSXC_WINUTILS_H
#define KEEPASSXC_WINUTILS_H
#include "gui/osutils/OSUtilsBase.h"
#include <QAbstractNativeEventFilter>
#include <QPointer>
#include <QScopedPointer>
class WinUtils : public OSUtilsBase
{
Q_OBJECT
public:
static WinUtils* instance();
static void registerEventFilters();
bool isDarkMode() override;
protected:
explicit WinUtils(QObject* parent = nullptr);
~WinUtils() override;
private:
class DWMEventFilter : public QAbstractNativeEventFilter
{
public:
bool nativeEventFilter(const QByteArray& eventType, void* message, long*) override;
};
static QPointer<WinUtils> m_instance;
static QScopedPointer<DWMEventFilter> m_eventFilter;
Q_DISABLE_COPY(WinUtils)
};
inline WinUtils* winUtils()
{
return WinUtils::instance();
}
#endif // KEEPASSXC_WINUTILS_H