Skip to content

Commit 0047a56

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

2 files changed

Lines changed: 36 additions & 9 deletions

File tree

panels/notification/bubble/bubbleitem.cpp

Lines changed: 33 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,24 @@ 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+
if (file.open()) {
149+
QString filePath = file.fileName();
150+
if (img.save(filePath)) {
151+
file.setAutoRemove(false);
152+
file.close();
153+
qDebug(notifyLog) << "Created temporary icon file:" << filePath;
154+
return filePath;
155+
}
156+
157+
file.close();
158+
return {};
159+
}
147160
}
148161

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

154165

@@ -166,6 +177,14 @@ BubbleItem::BubbleItem(const NotifyEntity &entity, QObject *parent)
166177
setEntity(entity);
167178
}
168179

180+
BubbleItem::~BubbleItem()
181+
{
182+
// clean up temporary icon file.
183+
if (!m_tempIconPath.isEmpty() && QFile::exists(m_tempIconPath)) {
184+
QFile::remove(m_tempIconPath);
185+
}
186+
}
187+
169188
void BubbleItem::setEntity(const NotifyEntity &entity)
170189
{
171190
m_entity = entity;
@@ -192,13 +211,19 @@ QString BubbleItem::appName() const
192211
return m_entity.appName();
193212
}
194213

195-
QString BubbleItem::appIcon() const
214+
QString BubbleItem::appIcon()
196215
{
197216
if (!m_entity.appIcon().isEmpty()) {
198217
return m_entity.appIcon();
199218
}
200219

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

204229
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)