Commit a0d175aa authored by Kai Uwe Broulik's avatar Kai Uwe Broulik 🍇

Move conversion of data URL into QImage into base class

Removes code duplication
parent a0c57d41
......@@ -48,6 +48,29 @@ bool AbstractKRunnerPlugin::onUnload()
return true;
}
QImage AbstractKRunnerPlugin::imageFromDataUrl(const QString &dataUrl)
{
QImage image;
if (!dataUrl.startsWith(QLatin1String("data:"))) {
return image;
}
const int b64start = dataUrl.indexOf(QLatin1Char(','));
if (b64start == -1) {
qWarning() << "Invalid data URL format for" << dataUrl.left(30);
return image;
}
const QByteArray b64 = dataUrl.rightRef(dataUrl.count() - b64start - 1).toLatin1();
const QByteArray data = QByteArray::fromBase64(b64);
if (!image.loadFromData(data)) {
qWarning() << "Failed to load favicon image from" << dataUrl.left(30);
}
return image;
}
RemoteImage AbstractKRunnerPlugin::serializeImage(const QImage &image)
{
QImage convertedImage = image.convertToFormat(QImage::Format_RGBA8888);
......
......@@ -34,6 +34,7 @@ protected:
int protocolVersion,
QObject *parent);
static QImage imageFromDataUrl(const QString &dataUrl);
static RemoteImage serializeImage(const QImage &image);
public:
......
......@@ -165,19 +165,10 @@ void HistoryRunnerPlugin::handleData(const QString& event, const QJsonObject& js
urlWithoutPassword.setPassword({});
match.properties.insert(QStringLiteral("urls"), QUrl::toStringList(QList<QUrl>{urlWithoutPassword}));
if (favIconUrl.startsWith(QLatin1String("data:"))) {
const int b64start = favIconUrl.indexOf(QLatin1Char(','));
if (b64start != -1) {
QByteArray b64 = favIconUrl.rightRef(favIconUrl.count() - b64start - 1).toLatin1();
QByteArray data = QByteArray::fromBase64(b64);
QImage image;
if (image.loadFromData(data)) {
const RemoteImage remoteImage = serializeImage(image);
match.properties.insert(QStringLiteral("icon-data"), QVariant::fromValue(remoteImage));
} else {
qWarning() << "Failed to load favicon image for" << match.id << match.text;
}
}
const QImage favIcon = imageFromDataUrl(favIconUrl);
if (!favIcon.isNull()) {
const RemoteImage remoteImage = serializeImage(favIcon);
match.properties.insert(QStringLiteral("icon-data"), QVariant::fromValue(remoteImage));
}
qreal relevance = 0;
......
......@@ -187,20 +187,10 @@ void TabsRunnerPlugin::handleData(const QString& event, const QJsonObject& json)
} else {
match.iconName = Settings::self().environmentDescription().iconName;
const QString favIconData = tab.value(QStringLiteral("favIconData")).toString();
if (favIconData.startsWith(QLatin1String("data:"))) {
const int b64start = favIconData.indexOf(QLatin1Char(','));
if (b64start != -1) {
QByteArray b64 = favIconData.rightRef(favIconData.count() - b64start - 1).toLatin1();
QByteArray data = QByteArray::fromBase64(b64);
QImage image;
if (image.loadFromData(data)) {
const RemoteImage remoteImage = serializeImage(image);
match.properties.insert(QStringLiteral("icon-data"), QVariant::fromValue(remoteImage));
} else {
qWarning() << "Failed to load favicon image for" << match.id << match.text;
}
}
const QImage favIcon = imageFromDataUrl(tab.value(QStringLiteral("favIconData")).toString());
if (!favIcon.isNull()) {
const RemoteImage remoteImage = serializeImage(favIcon);
match.properties.insert(QStringLiteral("icon-data"), QVariant::fromValue(remoteImage));
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment