|
7 | 7 | #include <QUrl> |
8 | 8 | #include <QTimer> |
9 | 9 | #include <QImage> |
| 10 | +#include <QBuffer> |
10 | 11 | #include <QDBusArgument> |
11 | 12 | #include <QTemporaryFile> |
12 | 13 | #include <QLoggingCategory> |
| 14 | +#include <QRegularExpression> |
13 | 15 |
|
14 | 16 | #include <DIconTheme> |
15 | 17 |
|
@@ -91,17 +93,14 @@ if (!(condition)) { \ |
91 | 93 | return image; |
92 | 94 | } |
93 | 95 |
|
94 | | -static QImage decodeImageFromBase64(const QString &arg) |
| 96 | +static QString decodeImageToBase64(const QImage &image, const char *format = "PNG") |
95 | 97 | { |
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())); |
105 | 104 | } |
106 | 105 |
|
107 | 106 | static QIcon decodeIconFromPath(const QString &arg, const QString &fallback) |
@@ -138,17 +137,18 @@ static QString imagePathOfNotification(const QVariantMap &hints, const QString & |
138 | 137 | imageData = source.toString(); |
139 | 138 | } |
140 | 139 | 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); |
147 | 148 | } |
148 | 149 |
|
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 {}; |
152 | 152 | } |
153 | 153 |
|
154 | 154 |
|
|
0 commit comments