Skip to content

Commit 203db7a

Browse files
yixinsharkdeepin-bot[bot]
authored andcommitted
feat: enhance notification icon handling
as title Log: as title
1 parent f1ec858 commit 203db7a

1 file changed

Lines changed: 19 additions & 19 deletions

File tree

panels/notification/bubble/bubbleitem.cpp

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
#include <QUrl>
88
#include <QTimer>
99
#include <QImage>
10+
#include <QBuffer>
1011
#include <QDBusArgument>
1112
#include <QTemporaryFile>
1213
#include <QLoggingCategory>
14+
#include <QRegularExpression>
1315

1416
#include <DIconTheme>
1517

@@ -91,17 +93,14 @@ if (!(condition)) { \
9193
return image;
9294
}
9395

94-
static QImage decodeImageFromBase64(const QString &arg)
96+
static QString decodeImageToBase64(const QImage &image, const char *format = "PNG")
9597
{
96-
if (arg.startsWith("data:image/")) {
97-
// iconPath is a string representing an inline image.
98-
QStringList strs = arg.split("base64,");
99-
if (strs.length() == 2) {
100-
QByteArray data = QByteArray::fromBase64(strs.at(1).toLatin1());
101-
return QImage::fromData(data);
102-
}
103-
}
104-
return QImage();
98+
QByteArray ba;
99+
QBuffer buffer(&ba);
100+
buffer.open(QIODevice::WriteOnly);
101+
image.save(&buffer, format);
102+
103+
return QString("data:image/%1;base64,%2").arg(QString::fromLatin1(format).toLower()).arg(QString::fromLatin1(ba.toBase64()));
105104
}
106105

107106
static QIcon decodeIconFromPath(const QString &arg, const QString &fallback)
@@ -138,17 +137,18 @@ static QString imagePathOfNotification(const QVariantMap &hints, const QString &
138137
imageData = source.toString();
139138
}
140139
if (img.isNull()) {
141-
img = decodeImageFromBase64(imageData);
142-
}
143-
if (!img.isNull()) {
144-
QTemporaryFile file("notification_icon");
145-
img.save(file.fileName());
146-
return file.fileName();
140+
// check if imageData is a base64 image data.
141+
QRegularExpression dataUriPattern("^data:image/[a-zA-Z0-9+\\-]+;base64,");
142+
QRegularExpressionMatch match = dataUriPattern.match(imageData);
143+
if (match.hasMatch()) {
144+
return imageData;
145+
}
146+
} else {
147+
return decodeImageToBase64(img);
147148
}
148149

149-
DGUI_USE_NAMESPACE;
150-
auto icon = DIconTheme::findQIcon(appName, DIconTheme::findQIcon("application-x-desktop"));
151-
return icon.name();
150+
// ui can fallback to application-x-desktop icon.
151+
return {};
152152
}
153153

154154

0 commit comments

Comments
 (0)