File indexing completed on 2025-01-05 05:18:56

0001 // SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
0002 //
0003 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0004 
0005 #include "controller.h"
0006 #include "kllmcoresettings.h"
0007 #include "systemtray.h"
0008 #include "windowcontroller.h"
0009 
0010 Controller::Controller(QObject *parent)
0011     : QObject{parent}
0012 {
0013 #ifndef Q_OS_ANDROID
0014     setQuitOnLastWindowClosed();
0015     connect(KLLMCoreSettings::self(), &KLLMCoreSettings::SystemTrayChanged, this, &Controller::setQuitOnLastWindowClosed);
0016 #endif
0017 }
0018 
0019 Controller::~Controller() = default;
0020 
0021 Controller &Controller::instance()
0022 {
0023     static Controller controller;
0024     return controller;
0025 }
0026 
0027 Controller *Controller::create(QQmlEngine *engine, QJSEngine *)
0028 {
0029     engine->setObjectOwnership(&instance(), QQmlEngine::CppOwnership);
0030     return &instance();
0031 }
0032 
0033 void Controller::setQuitOnLastWindowClosed()
0034 {
0035 #ifndef Q_OS_ANDROID
0036     if (KLLMCoreSettings::self()->systemTray()) {
0037         mTrayIcon = new SystemTray(this);
0038         mTrayIcon->show();
0039         connect(mTrayIcon, &SystemTray::toggleWindow, this, &Controller::toggleWindow);
0040     } else {
0041         if (mTrayIcon) {
0042             disconnect(mTrayIcon, &SystemTray::toggleWindow, this, &Controller::toggleWindow);
0043             delete mTrayIcon;
0044             mTrayIcon = nullptr;
0045         }
0046     }
0047 #endif
0048 }
0049 
0050 void Controller::toggleWindow()
0051 {
0052     auto &instance = WindowController::instance();
0053     auto window = instance.window();
0054     if (window->isVisible()) {
0055         if (window->windowStates() & Qt::WindowMinimized) {
0056             window->showNormal();
0057             window->requestActivate();
0058         } else {
0059             window->close();
0060         }
0061     } else {
0062         instance.showAndRaiseWindow({});
0063         instance.window()->requestActivate();
0064     }
0065 }
0066 
0067 bool Controller::supportSystemTray() const
0068 {
0069 #ifdef Q_OS_ANDROID
0070     return false;
0071 #else
0072     auto de = QString::fromLatin1(qgetenv("XDG_CURRENT_DESKTOP"));
0073     return de != QStringLiteral("GNOME") && de != QStringLiteral("Pantheon");
0074 #endif
0075 }
0076 
0077 void Controller::saveWindowGeometry()
0078 {
0079     WindowController::instance().saveGeometry();
0080 }
0081 
0082 #include "moc_controller.cpp"