2
* Copyright (C) 2020 KeePassXC Team <team@keepassxc.org>
3
* Copyright (C) 2011 Felix Geyer <debfx@fobos.de>
5
* This program is free software: you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation, either version 2 or (at your option)
8
* version 3 of the License.
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.
15
* You should have received a copy of the GNU General Public License
16
* along with this program. If not, see <http://www.gnu.org/licenses/>.
23
#include <QImageReader>
24
#include <QPaintDevice>
27
#include "config-keepassx.h"
28
#include "core/Config.h"
29
#include "core/Database.h"
30
#include "gui/DatabaseIcons.h"
31
#include "gui/MainWindow.h"
32
#include "gui/osutils/OSUtils.h"
34
#ifdef WITH_XC_KEESHARE
35
#include "keeshare/KeeShare.h"
38
class AdaptiveIconEngine : public QIconEngine
41
explicit AdaptiveIconEngine(QIcon baseIcon, QColor overrideColor = {});
42
void paint(QPainter* painter, const QRect& rect, QIcon::Mode mode, QIcon::State state) override;
43
QPixmap pixmap(const QSize& size, QIcon::Mode mode, QIcon::State state) override;
44
QIconEngine* clone() const override;
48
QColor m_overrideColor;
51
Icons* Icons::m_instance(nullptr);
53
Icons::Icons() = default;
55
QString Icons::applicationIconName()
57
#ifdef KEEPASSXC_DIST_FLATPAK
58
return "org.keepassxc.KeePassXC";
64
QIcon Icons::applicationIcon()
66
return icon(applicationIconName(), false);
69
QString Icons::trayIconAppearance() const
71
auto iconAppearance = config()->get(Config::GUI_TrayIconAppearance).toString();
72
if (iconAppearance.isNull()) {
74
iconAppearance = osUtils->isDarkMode() ? "monochrome-light" : "monochrome-dark";
76
iconAppearance = "monochrome-light";
79
return iconAppearance;
82
QIcon Icons::trayIcon(bool unlocked)
89
auto iconApperance = trayIconAppearance();
90
if (!iconApperance.startsWith("monochrome")) {
91
return icon(QString("%1%2").arg(applicationIconName(), suffix), false);
95
#if defined(Q_OS_MACOS) || defined(Q_OS_WIN)
96
if (osUtils->isStatusBarDark()) {
97
i = icon(QString("keepassxc-monochrome-light%1").arg(suffix), false);
99
i = icon(QString("keepassxc-monochrome-dark%1").arg(suffix), false);
102
i = icon(QString("%1-%2%3").arg(applicationIconName(), iconApperance, suffix), false);
104
#if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
105
// Set as mask to allow the operating system to recolour the tray icon. This may look weird
106
// if we failed to detect the status bar background colour correctly, but it is certainly
107
// better than a barely visible icon and even if we did guess correctly, it allows for better
108
// integration should the system's preferred colours not be 100% black or white.
114
AdaptiveIconEngine::AdaptiveIconEngine(QIcon baseIcon, QColor overrideColor)
116
, m_baseIcon(std::move(baseIcon))
117
, m_overrideColor(overrideColor)
121
void AdaptiveIconEngine::paint(QPainter* painter, const QRect& rect, QIcon::Mode mode, QIcon::State state)
123
// Temporary image canvas to ensure that the background is transparent and alpha blending works.
124
#if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
125
auto scale = painter->device()->devicePixelRatioF();
127
auto scale = painter->device()->devicePixelRatio();
129
QImage img(rect.size() * scale, QImage::Format_ARGB32_Premultiplied);
133
m_baseIcon.paint(&p, img.rect(), Qt::AlignCenter, mode, state);
135
if (m_overrideColor.isValid()) {
136
p.setCompositionMode(QPainter::CompositionMode_SourceIn);
137
p.fillRect(img.rect(), m_overrideColor);
138
} else if (getMainWindow()) {
139
QPalette palette = getMainWindow()->palette();
140
p.setCompositionMode(QPainter::CompositionMode_SourceIn);
142
if (mode == QIcon::Active) {
143
p.fillRect(img.rect(), palette.color(QPalette::Active, QPalette::ButtonText));
144
} else if (mode == QIcon::Selected) {
145
p.fillRect(img.rect(), palette.color(QPalette::Active, QPalette::HighlightedText));
146
} else if (mode == QIcon::Disabled) {
147
p.fillRect(img.rect(), palette.color(QPalette::Disabled, QPalette::WindowText));
149
p.fillRect(img.rect(), palette.color(QPalette::Normal, QPalette::WindowText));
153
painter->drawImage(rect, img);
156
QPixmap AdaptiveIconEngine::pixmap(const QSize& size, QIcon::Mode mode, QIcon::State state)
158
QImage img(size, QImage::Format_ARGB32_Premultiplied);
160
QPainter painter(&img);
161
paint(&painter, QRect(0, 0, size.width(), size.height()), mode, state);
162
return QPixmap::fromImage(img, Qt::ImageConversionFlag::NoFormatConversion);
165
QIconEngine* AdaptiveIconEngine::clone() const
167
return new AdaptiveIconEngine(m_baseIcon);
170
QIcon Icons::icon(const QString& name, bool recolor, const QColor& overrideColor)
173
// Resetting the application theme name before calling QIcon::fromTheme() is required for hacky
174
// QPA platform themes such as qt5ct, which randomly mess with the configured icon theme.
175
// If we do not reset the theme name here, it will become empty at some point, causing
176
// Qt to look for icons at the user-level and global default locations.
178
// See issue #4963: https://github.com/keepassxreboot/keepassxc/issues/4963
179
// and qt5ct issue #80: https://sourceforge.net/p/qt5ct/tickets/80/
180
QIcon::setThemeName("application");
184
QString("%1:%2:%3").arg(recolor ? "1" : "0", overrideColor.isValid() ? overrideColor.name() : "#", name);
185
QIcon icon = m_iconCache.value(cacheName);
187
if (!icon.isNull() && !overrideColor.isValid()) {
191
icon = QIcon::fromTheme(name);
193
icon = QIcon(new AdaptiveIconEngine(icon, overrideColor));
194
#if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
195
icon.setIsMask(true);
199
m_iconCache.insert(cacheName, icon);
203
QIcon Icons::onOffIcon(const QString& name, bool on, bool recolor)
205
return icon(name + (on ? "-on" : "-off"), recolor);
208
Icons* Icons::instance()
211
m_instance = new Icons();
213
Q_INIT_RESOURCE(icons);
214
QIcon::setThemeSearchPaths(QStringList{":/icons"} << QIcon::themeSearchPaths());
215
QIcon::setThemeName("application");
221
QPixmap Icons::customIconPixmap(const Database* db, const QUuid& uuid, IconSize size)
223
if (!db->metadata()->hasCustomIcon(uuid)) {
226
// Generate QIcon with pre-baked resolutions
227
auto icon = QImage::fromData(db->metadata()->customIcon(uuid).data);
228
auto basePixmap = QPixmap::fromImage(icon.scaled(64, 64, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
229
return QIcon(basePixmap).pixmap(databaseIcons()->iconSize(size));
232
QHash<QUuid, QPixmap> Icons::customIconsPixmaps(const Database* db, IconSize size)
234
QHash<QUuid, QPixmap> result;
236
for (const QUuid& uuid : db->metadata()->customIconsOrder()) {
237
result.insert(uuid, Icons::customIconPixmap(db, uuid, size));
243
QPixmap Icons::entryIconPixmap(const Entry* entry, IconSize size)
245
QPixmap icon(size, size);
246
if (entry->iconUuid().isNull()) {
247
icon = databaseIcons()->icon(entry->iconNumber(), size);
249
if (entry->database()) {
250
icon = Icons::customIconPixmap(entry->database(), entry->iconUuid(), size);
254
if (entry->isExpired()) {
255
icon = databaseIcons()->applyBadge(icon, DatabaseIcons::Badges::Expired);
261
QPixmap Icons::groupIconPixmap(const Group* group, IconSize size)
263
QPixmap icon(size, size);
264
if (group->iconUuid().isNull()) {
265
icon = databaseIcons()->icon(group->iconNumber(), size);
267
if (group->database()) {
268
icon = Icons::customIconPixmap(group->database(), group->iconUuid(), size);
272
if (group->isExpired()) {
273
icon = databaseIcons()->applyBadge(icon, DatabaseIcons::Badges::Expired);
275
#ifdef WITH_XC_KEESHARE
276
else if (KeeShare::isShared(group)) {
277
icon = KeeShare::indicatorBadge(group, icon);
284
QString Icons::imageFormatsFilter()
286
const QList<QByteArray> formats = QImageReader::supportedImageFormats();
287
QStringList formatsStringList;
289
for (const QByteArray& format : formats) {
290
bool codePointClean = true;
291
for (char codePoint : format) {
292
if (!QChar(codePoint).isLetterOrNumber()) {
293
codePointClean = false;
297
if (codePointClean) {
298
formatsStringList.append("*." + QString::fromLatin1(format).toLower());
302
return formatsStringList.join(" ");
305
QByteArray Icons::saveToBytes(const QImage& image)
309
buffer.open(QIODevice::WriteOnly);
310
// TODO: check !icon.save()
311
image.save(&buffer, "PNG");