File: cdtpavatarupdate.cpp

package info (click to toggle)
contactsd 1.4.15-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,392 kB
  • sloc: cpp: 8,862; ansic: 3,793; sh: 361; xml: 75; python: 22; makefile: 13
file content (173 lines) | stat: -rw-r--r-- 6,372 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*********************************************************************************
 ** This file is part of QtContacts tracker storage plugin
 **
 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
 **
 ** Contact:  Nokia Corporation (info@qt.nokia.com)
 **
 ** GNU Lesser General Public License Usage
 ** This file may be used under the terms of the GNU Lesser General Public License
 ** version 2.1 as published by the Free Software Foundation and appearing in the
 ** file LICENSE.LGPL included in the packaging of this file.  Please review the
 ** following information to ensure the GNU Lesser General Public License version
 ** 2.1 requirements will be met:
 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 **
 ** In addition, as a special exception, Nokia gives you certain additional rights.
 ** These rights are described in the Nokia Qt LGPL Exception version 1.1, included
 ** in the file LGPL_EXCEPTION.txt in this package.
 **
 ** Other Usage
 ** Alternatively, this file may be used in accordance with the terms and
 ** conditions contained in a signed written agreement between you and Nokia.
 *********************************************************************************/


#include "cdtpavatarupdate.h"
#include "cdtpplugin.h"
#include "debug.h"

using namespace Contactsd;

const QString CDTpAvatarUpdate::Large = QLatin1String("large");
const QString CDTpAvatarUpdate::Square = QLatin1String("square");

void CDTpAvatarUpdate::updateContact(CDTpContact *contactWrapper, QNetworkReply *networkReply,
                                     const QString &filename, const QString &avatarType)
{
    (void) new CDTpAvatarUpdate(networkReply, contactWrapper, filename, avatarType);
}

CDTpAvatarUpdate::CDTpAvatarUpdate(QNetworkReply *networkReply,
                                   CDTpContact *contactWrapper,
                                   const QString &filename,
                                   const QString &avatarType)
    : QObject()
    , mNetworkReply(0)
    , mContactWrapper(contactWrapper)
    , mFilename(filename)
    , mAvatarType(avatarType)
{
    setNetworkReply(networkReply);
}

void CDTpAvatarUpdate::setNetworkReply(QNetworkReply *networkReply)
{
    if (mNetworkReply) {
        mNetworkReply->disconnect(this);
        mNetworkReply->deleteLater();
    }

    mNetworkReply = networkReply;

    if (mNetworkReply) {
        if (mNetworkReply->isRunning()) {
            connect(mNetworkReply, SIGNAL(finished()), this, SLOT(onRequestDone()));
            connect(mNetworkReply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(onRequestDone()));
        } else {
            onRequestDone();
        }
    } else {
        // This operation is complete
        this->deleteLater();
    }
}

void CDTpAvatarUpdate::onRequestDone()
{
    QNetworkReply *newReply = 0;

    if (mNetworkReply && mNetworkReply->error() == QNetworkReply::NoError) {
        newReply = updateContactAvatar();
    }

    setNetworkReply(newReply);
}

QString CDTpAvatarUpdate::writeAvatarFile(QFile &avatarFile, const QDir &cacheDir)
{
    if (not cacheDir.exists() && not QDir::root().mkpath(cacheDir.absolutePath())) {
        qCWarning(lcContactsd) << "Could not create large avatar cache dir:" << cacheDir.path();
        return QString();
    }

    QTemporaryFile tempFile(cacheDir.absoluteFilePath(QLatin1String("pinkpony")));
    const QByteArray data = mNetworkReply->readAll();

    if (tempFile.open() && data.count() == tempFile.write(data)) {
        tempFile.close();

        if (avatarFile.exists()) {
            avatarFile.remove();
        }

        if (tempFile.rename(avatarFile.fileName())) {
            tempFile.setAutoRemove(false);
            return avatarFile.fileName();
        }
    }

    return QString();
}

static bool acceptFileSize(qint64 actualFileSize, qint64 expectedFileSize)
{
    if (expectedFileSize > 0) {
        return (actualFileSize == expectedFileSize);
    }

    return actualFileSize > 0;
}

QNetworkReply *CDTpAvatarUpdate::updateContactAvatar()
{
    // Build filename from the image URL's SHA1 hash.
    const QUrl redirectionTarget = mNetworkReply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
    const QString avatarUrl = (not redirectionTarget.isEmpty() ? mNetworkReply->url().resolved(redirectionTarget)
                                                               : mNetworkReply->url()).toString();

    QString filename(mFilename);
    if (filename.isEmpty()) {
        QByteArray avatarHash = QCryptographicHash::hash(avatarUrl.toUtf8(), QCryptographicHash::Sha1);
        filename = QString::fromLatin1(avatarHash.toHex());
    }

    const QDir cacheDir(CDTpPlugin::cacheFileName(QLatin1String("avatars")));
    QFile avatarFile(cacheDir.absoluteFilePath(filename));

    // Check for existing avatar file and its size to see if we need to fetch from network.
    const qint64 contentLength = mNetworkReply->header(QNetworkRequest::ContentLengthHeader).toLongLong();

    QString avatarPath;
    if (avatarFile.exists() && acceptFileSize(avatarFile.size(), contentLength)) {
        // Seems we can reuse the existing avatar file.
        avatarPath = avatarFile.fileName();
    } else {
        // Follow redirections as done by Facebook's graph API.
        if (not redirectionTarget.isEmpty()) {
            return mNetworkReply->manager()->get(QNetworkRequest(redirectionTarget));
        }

        // Facebook delivers a distinct gif image if no avatar is set. Ignore that bugger.
        const QString contentType = mNetworkReply->header(QNetworkRequest::ContentTypeHeader).toString();

        static const QLatin1String contentTypeImageGif = QLatin1String("image/gif");
        static const QLatin1String contentTypeImage = QLatin1String("image/");

        if (contentType.startsWith(contentTypeImage) && contentType != contentTypeImageGif) {
            avatarPath = writeAvatarFile(avatarFile, cacheDir);
        }
    }

    // Update the contact if a new avatar is available.
    if (not avatarPath.isEmpty() && not mContactWrapper.isNull()) {
        if (mAvatarType == Square) {
            mContactWrapper->setSquareAvatarPath(avatarPath);
        } else if (mAvatarType == Large) {
            mContactWrapper->setLargeAvatarPath(avatarPath);
        }
    }

    // No further work to do
    return 0;
}