Commit b0af2127 authored by Jan Blackquill's avatar Jan Blackquill 🌈 Committed by Nate Graham

[kcms/users]: Resize provided image to always be smaller than 1MB

This prevents accounts service from complaining about too large files

BUG: 426932
parent 9b0b0bac
......@@ -28,6 +28,7 @@
#include <QtConcurrent>
#include <KLocalizedString>
#include <KWallet>
#include <QImage>
User::User(QObject* parent) : QObject(parent) {}
......@@ -279,7 +280,6 @@ void UserApplyJob::start()
{m_name, &OrgFreedesktopAccountsUserInterface::SetUserName},
{m_email, &OrgFreedesktopAccountsUserInterface::SetEmail},
{m_realname, &OrgFreedesktopAccountsUserInterface::SetRealName},
{m_icon, &OrgFreedesktopAccountsUserInterface::SetIconFile}
};
// Do our dbus invocations with blocking calls, since the accounts service
// will return permission denied if there's a polkit dialog open while a
......@@ -294,6 +294,43 @@ void UserApplyJob::start()
return;
}
}
// Icon is special, since we want to resize it.
{
QImage icon(m_icon);
// 256dp square is plenty big for an avatar and will definitely be smaller than 1MB
QImage scaled = icon.scaled(QSize(256, 256), Qt::KeepAspectRatio, Qt::SmoothTransformation);
QTemporaryFile file;
if (!file.open()) {
setErrorText(i18n("Failed to resize image: opening temp file failed"));
qCWarning(KCMUSERS) << i18n("Failed to resize image: opening temp file failed");
KJob::setError(static_cast<int>(Error::UserFacing));
emitResult();
return;
}
if (!scaled.save(&file, "png")) {
setErrorText(i18n("Failed to resize image: writing to temp file failed"));
qCWarning(KCMUSERS) << i18n("Failed to resize image: writing to temp file failed");
KJob::setError(static_cast<int>(Error::UserFacing));
emitResult();
return;
}
file.close();
auto resp = m_dbusIface->SetIconFile(file.fileName());
resp.waitForFinished();
if (resp.isError()) {
setError(resp.error());
qCWarning(KCMUSERS) << resp.error().name() << resp.error().message();
emitResult();
return;
}
}
auto setAccount = m_dbusIface->SetAccountType(m_type);
setAccount.waitForFinished();
if (setAccount.isError()) {
......
......@@ -41,7 +41,8 @@ public:
NoError = 0,
PermissionDenied,
Failed,
Unknown
Unknown,
UserFacing
};
private:
......
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