Skip to content

Commit bc7b1c7

Browse files
committed
feat: enhance notification icon handling
as title Log: as title
1 parent f1ec858 commit bc7b1c7

2 files changed

Lines changed: 33 additions & 9 deletions

File tree

panels/notification/bubble/bubbleitem.cpp

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include <QUrl>
88
#include <QTimer>
99
#include <QImage>
10+
#include <QDir>
11+
#include <QFile>
1012
#include <QDBusArgument>
1113
#include <QTemporaryFile>
1214
#include <QLoggingCategory>
@@ -140,15 +142,21 @@ static QString imagePathOfNotification(const QVariantMap &hints, const QString &
140142
if (img.isNull()) {
141143
img = decodeImageFromBase64(imageData);
142144
}
145+
143146
if (!img.isNull()) {
144-
QTemporaryFile file("notification_icon");
145-
img.save(file.fileName());
146-
return file.fileName();
147+
QTemporaryFile file(QDir::temp().filePath("notification_icon_XXXXXX.png"));
148+
file.setAutoRemove(false);
149+
if (file.open()) {
150+
QString filePath = file.fileName();
151+
file.close();
152+
if (img.save(filePath)) {
153+
qDebug(notifyLog) << "Created temporary icon file:" << filePath;
154+
return filePath;
155+
}
156+
}
147157
}
148158

149-
DGUI_USE_NAMESPACE;
150-
auto icon = DIconTheme::findQIcon(appName, DIconTheme::findQIcon("application-x-desktop"));
151-
return icon.name();
159+
return {};
152160
}
153161

154162

@@ -166,6 +174,14 @@ BubbleItem::BubbleItem(const NotifyEntity &entity, QObject *parent)
166174
setEntity(entity);
167175
}
168176

177+
BubbleItem::~BubbleItem()
178+
{
179+
// clean up temporary icon file.
180+
if (!m_tempIconPath.isEmpty() && QFile::exists(m_tempIconPath)) {
181+
QFile::remove(m_tempIconPath);
182+
}
183+
}
184+
169185
void BubbleItem::setEntity(const NotifyEntity &entity)
170186
{
171187
m_entity = entity;
@@ -192,13 +208,19 @@ QString BubbleItem::appName() const
192208
return m_entity.appName();
193209
}
194210

195-
QString BubbleItem::appIcon() const
211+
QString BubbleItem::appIcon()
196212
{
197213
if (!m_entity.appIcon().isEmpty()) {
198214
return m_entity.appIcon();
199215
}
200216

201-
return imagePathOfNotification(m_entity.hints(), m_entity.appIcon(), m_entity.appName());
217+
if (!m_tempIconPath.isEmpty() && QFile::exists(m_tempIconPath)) {
218+
return m_tempIconPath;
219+
}
220+
221+
m_tempIconPath = imagePathOfNotification(m_entity.hints(), m_entity.appIcon(), m_entity.appName());
222+
// if this is empty path, UI can fallback to application-x-desktop icon.
223+
return m_tempIconPath;
202224
}
203225

204226
QString BubbleItem::summary() const

panels/notification/bubble/bubbleitem.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class BubbleItem : public QObject
1818

1919
explicit BubbleItem(QObject *parent = nullptr);
2020
explicit BubbleItem(const NotifyEntity &entity, QObject *parent = nullptr);
21+
~BubbleItem();
2122

2223
public:
2324
void setEntity(const NotifyEntity &entity);
@@ -26,7 +27,7 @@ class BubbleItem : public QObject
2627
qint64 id() const;
2728
uint bubbleId() const;
2829
QString appName() const;
29-
QString appIcon() const;
30+
QString appIcon();
3031
QString summary() const;
3132
QString body() const;
3233
uint replacesId() const;
@@ -65,6 +66,7 @@ class BubbleItem : public QObject
6566
bool m_enablePreview = true;
6667
QVariantList m_actions;
6768
QString m_defaultAction;
69+
QString m_tempIconPath;
6870
};
6971

7072
}

0 commit comments

Comments
 (0)