[SCM] ktp-accounts-kcm packaging branch, master, updated. debian/15.12.1-1-1157-gc4589c5

Maximiliano Curia maxy at moszumanska.debian.org
Sat May 28 00:04:25 UTC 2016


Gitweb-URL: http://git.debian.org/?p=pkg-kde/applications/ktp-accounts-kcm.git;a=commitdiff;h=aa2bedd

The following commit has been merged in the master branch:
commit aa2beddbfa05ed68af71f3ccbf0685e622670da7
Author: Dan Vrátil <dvratil at redhat.com>
Date:   Mon Jun 10 11:56:08 2013 +0200

    Add UI to enforce avatar size restrictions
    
    Add code to Accounts KCM that will allow users to crop avatar and will
    scale it down to maximum size allowed by server (or to some reasonable
    values if server/gabble does not report avatarRequirements())
    
    REVIEW: 110929
    BUG: 279112
    FIXED-IN: 0.7.0
---
 src/CMakeLists.txt              |   1 -
 src/account-identity-dialog.cpp |   1 +
 src/avatar-button.cpp           | 139 +++++++++++++++++++++++++++++++++-------
 src/avatar-button.h             |   7 +-
 src/fetch-avatar-job.cpp        | 113 --------------------------------
 src/fetch-avatar-job.h          |  51 ---------------
 src/kcm-telepathy-accounts.cpp  |   1 +
 7 files changed, 123 insertions(+), 190 deletions(-)

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 6ad7d6f..d27c7c0 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -10,7 +10,6 @@ project (ktp-accounts-kcm)
 
 set (kcm_ktp_accounts_SRCS
      avatar-button.cpp
-     fetch-avatar-job.cpp
      salut-enabler.cpp
      salut-message-widget.cpp
      kcm-telepathy-accounts.cpp
diff --git a/src/account-identity-dialog.cpp b/src/account-identity-dialog.cpp
index 7cf930b..df8b085 100644
--- a/src/account-identity-dialog.cpp
+++ b/src/account-identity-dialog.cpp
@@ -48,6 +48,7 @@ AccountIdentityDialog::AccountIdentityDialog(const Tp::AccountPtr &account, QWid
     onAvatarChanged(account->avatar());
 
     ui->accountId->setText(m_account->displayName());
+    ui->accountAvatar->setAccount(m_account);
 
     connect(this, SIGNAL(okClicked()), SLOT(apply()));
 }
diff --git a/src/avatar-button.cpp b/src/avatar-button.cpp
index 803fef8..745bdcc 100644
--- a/src/avatar-button.cpp
+++ b/src/avatar-button.cpp
@@ -21,13 +21,26 @@
 #include "avatar-button.h"
 
 #include <QtGui/QWidgetAction>
+#include <QDataStream>
 
 #include <KFileDialog>
 #include <KMenu>
 #include <KLocalizedString>
 #include <KMessageBox>
+#include <KImageFilePreview>
 
-#include "fetch-avatar-job.h"
+#include <TelepathyQt/Account>
+
+#include <KDE/KDebug>
+#include <KGlobalSettings>
+#include <KPixmapRegionSelectorDialog>
+#include <KPixmapRegionSelectorWidget>
+
+// It has been decided by a fair dice roll that 128px is a reasonable avatar
+// size limit in case the server (or Telepathy backend) does not provide
+// such information
+#define AVATAR_MIN_SIZE 64
+#define AVATAR_MAX_SIZE 128
 
 AvatarButton::AvatarButton(QWidget *parent)
     : QToolButton(parent)
@@ -49,7 +62,8 @@ AvatarButton::~AvatarButton()
 
 }
 
-void AvatarButton::setAvatar(const Tp::Avatar &avatar) {
+void AvatarButton::setAvatar(const Tp::Avatar &avatar)
+{
     m_avatar = avatar;
 
     if (! avatar.avatarData.isNull()) {
@@ -63,44 +77,121 @@ void AvatarButton::setAvatar(const Tp::Avatar &avatar) {
     }
 }
 
-Tp::Avatar AvatarButton::avatar() const {
+Tp::Avatar AvatarButton::avatar() const
+{
     return m_avatar;
 }
 
+void AvatarButton::setAccount(const Tp::AccountPtr& account)
+{
+    m_account = account;
+}
 
 void AvatarButton::onLoadAvatarFromFile()
 {
-    KUrl fileUrl = KFileDialog::getImageOpenUrl(KUrl(), this,
-                                                i18n("Please choose your avatar"));
+    QStringList mimeTypes;
+    if (m_account) {
+        mimeTypes = m_account->avatarRequirements().supportedMimeTypes();
+    }
+    if (mimeTypes.isEmpty()) {
+        mimeTypes << QLatin1String("image/jpeg")
+                  << QLatin1String("image/png")
+                  << QLatin1String("imgae/gif");
+    }
+
+    QPointer<KFileDialog> dialog = new KFileDialog(KUrl::fromPath(KGlobalSettings::picturesPath()),
+                                                   mimeTypes.join(QLatin1String(" ")), this);
+    dialog->setOperationMode(KFileDialog::Opening);
+    dialog->setPreviewWidget(new KImageFilePreview(dialog));
+    dialog->setCaption(i18n("Please choose your avatar"));
+
+    KUrl fileUrl;
+    if (dialog->exec()) {
+        if (!dialog) {
+            return;
+        }
 
-    if (!fileUrl.isEmpty()) {
-        FetchAvatarJob *job = new FetchAvatarJob(fileUrl, this);
+        fileUrl = dialog->selectedUrl();
+    }
+
+    delete dialog;
+
+    if (fileUrl.isEmpty()) {
+        return;
+    }
 
-        connect(job, SIGNAL(result(KJob*)),
-                this, SLOT(onAvatarFetched(KJob*)));
+    const QPixmap pixmap(fileUrl.toLocalFile());
 
-        job->start();
+    const Tp::AvatarSpec spec = m_account->avatarRequirements();
+
+    const int maxWidth = spec.maximumWidth() > 0 ? spec.maximumWidth() : AVATAR_MAX_SIZE;
+    const int maxHeight = spec.maximumHeight() > 0 ? spec.maximumHeight() : AVATAR_MAX_SIZE;
+    const int minWidth = spec.minimumWidth() > 0 ? spec.minimumWidth() : AVATAR_MIN_SIZE;
+    const int minHeight = spec.minimumHeight() > 0 ? spec.minimumHeight() : AVATAR_MIN_SIZE;
+
+    QPixmap finalPixmap;
+    if (pixmap.width() > spec.maximumWidth() || pixmap.height() > spec.maximumHeight()) {
+        finalPixmap = cropPixmap(pixmap, maxWidth, maxHeight, minWidth, minHeight);
     } else {
+        finalPixmap = pixmap;
+
+        if (pixmap.width() < minWidth) {
+            finalPixmap = finalPixmap.scaledToWidth(minWidth, Qt::SmoothTransformation);
+        }
+
+        if (pixmap.height() < minHeight) {
+            finalPixmap = finalPixmap.scaledToHeight(minHeight, Qt::SmoothTransformation);
+        }
+    }
+
+    if (finalPixmap.isNull()) {
         return;
     }
-}
 
-void AvatarButton::onClearAvatar()
-{
-    setAvatar(Tp::Avatar());
+    Tp::Avatar avatar;
+    avatar.MIMEType = QLatin1String("image/png");
+    QDataStream stream(&avatar.avatarData, QIODevice::WriteOnly);
+    if (!finalPixmap.save(stream.device(), "PNG")) {
+        KMessageBox::error(this, i18n("Failed to load avatar."));
+        return;
+    }
+
+    setAvatar(avatar);
     Q_EMIT avatarChanged();
 }
 
-void AvatarButton::onAvatarFetched(KJob *job)
+QPixmap AvatarButton::cropPixmap(const QPixmap& pixmap, int maxWidth, int maxHeight,
+                                 int minWidth, int minHeight) const
 {
-    if (job->error()) {
-        KMessageBox::error(this, job->errorString());
-        return;
-    } else {
-        FetchAvatarJob *fetchJob = qobject_cast< FetchAvatarJob* >(job);
-
-        Q_ASSERT(fetchJob);
-        setAvatar(fetchJob->avatar());
-        Q_EMIT avatarChanged();
+    QPointer<KPixmapRegionSelectorDialog> regionDlg = new KPixmapRegionSelectorDialog();
+    KPixmapRegionSelectorWidget *widget = regionDlg->pixmapRegionSelectorWidget();
+    widget->setPixmap(pixmap);
+    widget->setSelectionAspectRatio(maxWidth, maxHeight);
+
+    if (regionDlg->exec()) {
+        if (!regionDlg) {
+            return QPixmap();
+        }
+
+        delete regionDlg;
+
+        QImage selectedImage = widget->selectedImage();
+        if (selectedImage.width() > maxWidth || selectedImage.height() > maxHeight) {
+            return QPixmap::fromImage(selectedImage.scaled(maxWidth, maxHeight, Qt::KeepAspectRatio));
+        } else if (selectedImage.width() < minWidth || selectedImage.height() < minHeight) {
+            return QPixmap::fromImage(selectedImage.scaled(minWidth, minHeight, Qt::KeepAspectRatio));
+        } else {
+            return QPixmap::fromImage(widget->selectedImage());
+        }
     }
+
+    delete regionDlg;
+
+    return QPixmap();
+}
+
+void AvatarButton::onClearAvatar()
+{
+    setAvatar(Tp::Avatar());
+    Q_EMIT avatarChanged();
 }
diff --git a/src/avatar-button.h b/src/avatar-button.h
index 19b953e..cdc04df 100644
--- a/src/avatar-button.h
+++ b/src/avatar-button.h
@@ -37,6 +37,8 @@ public:
     AvatarButton(QWidget* parent = 0);
     ~AvatarButton();
 
+    void setAccount(const Tp::AccountPtr &account);
+
     void setAvatar(const Tp::Avatar &avatar);
     Tp::Avatar avatar() const;
 
@@ -46,10 +48,13 @@ Q_SIGNALS:
 private Q_SLOTS:
     void onLoadAvatarFromFile();
     void onClearAvatar();
-    void onAvatarFetched(KJob*);
 
 private:
+    QPixmap cropPixmap(const QPixmap &pixmap, int maxWidth, int maxHeight,
+                       int minWidth, int minHeight) const;
+
     Tp::Avatar m_avatar;
+    Tp::AccountPtr m_account;
 
 };
 
diff --git a/src/fetch-avatar-job.cpp b/src/fetch-avatar-job.cpp
deleted file mode 100644
index e734ede..0000000
--- a/src/fetch-avatar-job.cpp
+++ /dev/null
@@ -1,113 +0,0 @@
-/*
- * This file is part of telepathy-contactslist-prototype
- *
- * Copyright (C) 2011 Collabora Ltd. <info at collabora.co.uk>
- *   @Author Dario Freddi <dario.freddi at collabora.co.uk>
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
- */
-
-#include "fetch-avatar-job.h"
-
-#include <KUrl>
-#include <KLocalizedString>
-
-#include <KIO/Job>
-
-class FetchAvatarJob::Private
-{
-public:
-    Private(FetchAvatarJob *q) : q(q) {}
-    ~Private() {}
-
-    void _k_onMimeTypeDetected(KIO::Job *job, const QString &mimetype);
-    void _k_onDataFromJob(KIO::Job *job, const QByteArray &data);
-    void _k_onJobFinished(KJob *job);
-
-    Tp::Avatar avatar;
-    KUrl url;
-
-    FetchAvatarJob *q;
-};
-
-FetchAvatarJob::FetchAvatarJob(const KUrl& url, QObject* parent)
-    : KJob(parent)
-    , d(new Private(this))
-{
-    d->url = url;
-}
-
-FetchAvatarJob::~FetchAvatarJob()
-{
-    delete d;
-}
-
-Tp::Avatar FetchAvatarJob::avatar() const
-{
-    return d->avatar;
-}
-
-void FetchAvatarJob::start()
-{
-    if (d->url.isEmpty() || !d->url.isValid()) {
-        setError(1);
-        emitResult();
-        return;
-    }
-
-    KIO::TransferJob *job = KIO::get(d->url);
-
-    connect(job, SIGNAL(mimetype(KIO::Job*,QString)),
-            this, SLOT(_k_onMimeTypeDetected(KIO::Job*,QString)));
-    connect(job, SIGNAL(data(KIO::Job*,QByteArray)),
-            this, SLOT(_k_onDataFromJob(KIO::Job*,QByteArray)));
-    connect(job, SIGNAL(result(KJob*)),
-            this, SLOT(_k_onJobFinished(KJob*)));
-}
-
-void FetchAvatarJob::Private::_k_onMimeTypeDetected(KIO::Job *job, const QString &mimetype)
-{
-    if (!mimetype.contains(QLatin1String("image/"))) {
-        q->setErrorText(i18n("The file you have selected does not seem to be an image.
"
-                             "Please select an image file."));
-        q->setError(1);
-        q->emitResult();
-
-        disconnect(job, SIGNAL(result(KJob*)),
-                   q, SLOT(_k_onJobFinished(KJob*)));
-        disconnect(job, SIGNAL(data(KIO::Job*,QByteArray)),
-                   q, SLOT(_k_onDataFromJob(KIO::Job*,QByteArray)));
-
-        job->kill();
-
-        return;
-    }
-
-    avatar.MIMEType = mimetype;
-}
-
-void FetchAvatarJob::Private::_k_onDataFromJob(KIO::Job *job, const QByteArray &data)
-{
-    Q_UNUSED(job)
-    avatar.avatarData.append(data);
-}
-
-void FetchAvatarJob::Private::_k_onJobFinished(KJob *job)
-{
-    q->setError(job->error());
-    q->emitResult();
-}
-
-#include "fetch-avatar-job.moc"
diff --git a/src/fetch-avatar-job.h b/src/fetch-avatar-job.h
deleted file mode 100644
index f9841e2..0000000
--- a/src/fetch-avatar-job.h
+++ /dev/null
@@ -1,51 +0,0 @@
-/*
- * This file is part of telepathy-contactslist-prototype
- *
- * Copyright (C) 2011 Collabora Ltd. <info at collabora.co.uk>
- *   @Author Dario Freddi <dario.freddi at collabora.co.uk>
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
- */
-
-#ifndef FETCH_AVATAR_JOB_H
-#define FETCH_AVATAR_JOB_H
-
-#include <KJob>
-
-#include <TelepathyQt/Types>
-
-class KUrl;
-class FetchAvatarJob : public KJob
-{
-    Q_OBJECT
-
-public:
-    explicit FetchAvatarJob(const KUrl &url, QObject *parent = 0);
-    virtual ~FetchAvatarJob();
-
-    void start();
-
-    Tp::Avatar avatar() const;
-
-private:
-    class Private;
-    Private * const d;
-
-    Q_PRIVATE_SLOT(d, void _k_onMimeTypeDetected(KIO::Job*,QString))
-    Q_PRIVATE_SLOT(d, void _k_onDataFromJob(KIO::Job*,QByteArray))
-    Q_PRIVATE_SLOT(d, void _k_onJobFinished(KJob*))
-};
-
-#endif // FETCH_AVATAR_JOB_H
diff --git a/src/kcm-telepathy-accounts.cpp b/src/kcm-telepathy-accounts.cpp
index e41ccb4..59e3d21 100644
--- a/src/kcm-telepathy-accounts.cpp
+++ b/src/kcm-telepathy-accounts.cpp
@@ -96,6 +96,7 @@ KCMTelepathyAccounts::KCMTelepathyAccounts(QWidget *parent, const QVariantList&
     Tp::AccountFactoryPtr  accountFactory = Tp::AccountFactory::create(QDBusConnection::sessionBus(),
                                                                        Tp::Features() << Tp::Account::FeatureCore
                                                                        << Tp::Account::FeatureAvatar
+                                                                       << Tp::Account::FeatureCapabilities
                                                                        << Tp::Account::FeatureProtocolInfo
                                                                        << Tp::Account::FeatureProfile);
 

-- 
ktp-accounts-kcm packaging



More information about the pkg-kde-commits mailing list