1 /*
2  * Bittorrent Client using Qt and libtorrent.
3  * Copyright (C) 2019  Prince Gupta <jagannatharjun11@gmail.com>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
18  * USA.
19  *
20  * In addition, as a special exception, the copyright holders give permission to
21  * link this program with the OpenSSL project's "OpenSSL" library (or with
22  * modified versions of it that use the same license as the "OpenSSL" library),
23  * and distribute the linked executables. You must obey the GNU General Public
24  * License in all respects for all of the code used other than "OpenSSL".  If
25  * you modify file(s), you may extend this exception to your version of the
26  * file(s), but you are not obligated to do so. If you do not wish to do so,
27  * delete this exception statement from your version.
28  */
29 
30 #pragma once
31 
32 #include <QColor>
33 #include <QHash>
34 #include <QIcon>
35 #include <QObject>
36 #include <QString>
37 
38 class UIThemeManager : public QObject
39 {
40     Q_OBJECT
41     Q_DISABLE_COPY(UIThemeManager)
42 
43 public:
44     static void initInstance();
45     static void freeInstance();
46     static UIThemeManager *instance();
47 
48     QString getIconPath(const QString &iconId) const;
49     QIcon getIcon(const QString &iconId, const QString &fallback = {}) const;
50     QIcon getFlagIcon(const QString &countryIsoCode) const;
51 
52     QColor getColor(const QString &id, const QColor &defaultColor) const;
53 
54 private:
55     UIThemeManager(); // singleton class
56     QString getIconPathFromResources(const QString &iconId, const QString &fallback = {}) const;
57     void loadColorsFromJSONConfig();
58     void applyPalette() const;
59     void applyStyleSheet() const;
60 
61     static UIThemeManager *m_instance;
62     QHash<QString, QColor> m_colors;
63     mutable QHash<QString, QIcon> m_iconCache;
64     mutable QHash<QString, QIcon> m_flagCache;
65     const bool m_useCustomTheme;
66 #if (defined(Q_OS_UNIX) && !defined(Q_OS_MACOS))
67     const bool m_useSystemTheme;
68 #endif
69 };
70