~ubuntu-branches/ubuntu/trusty/tomahawk/trusty-proposed

« back to all changes in this revision

Viewing changes to src/accounts/xmpp/sip/AvatarManager.cpp

  • Committer: Package Import Robot
  • Author(s): Harald Sitter
  • Date: 2013-03-07 21:50:13 UTC
  • Revision ID: package-import@ubuntu.com-20130307215013-6gdjkdds7i9uenvs
Tags: upstream-0.6.0+dfsg
ImportĀ upstreamĀ versionĀ 0.6.0+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
 
2
 *
 
3
 *   Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
 
4
 *   Copyright 2010-2011, Jeff Mitchell <jeff@tomahawk-player.org>
 
5
 *
 
6
 *   Tomahawk is free software: you can redistribute it and/or modify
 
7
 *   it under the terms of the GNU General Public License as published by
 
8
 *   the Free Software Foundation, either version 3 of the License, or
 
9
 *   (at your option) any later version.
 
10
 *
 
11
 *   Tomahawk is distributed in the hope that it will be useful,
 
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 *   GNU General Public License for more details.
 
15
 *
 
16
 *   You should have received a copy of the GNU General Public License
 
17
 *   along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
 
18
 */
 
19
 
 
20
#include "AvatarManager.h"
 
21
 
 
22
#include "utils/TomahawkUtils.h"
 
23
#include "utils/Logger.h"
 
24
 
 
25
#include <jreen/vcard.h>
 
26
#include <jreen/vcardupdate.h>
 
27
#include <jreen/presence.h>
 
28
#include <jreen/iqreply.h>
 
29
 
 
30
#include <QDir>
 
31
#include <QCryptographicHash>
 
32
#include <QPixmap>
 
33
 
 
34
 
 
35
AvatarManager::AvatarManager( Jreen::Client* client )
 
36
    : m_cacheDir( TomahawkUtils::appDataDir().absolutePath().append( "/jreen/" ) )
 
37
{
 
38
    m_client = client;
 
39
    m_cachedAvatars = m_cacheDir.entryList();
 
40
 
 
41
    connect( m_client, SIGNAL( serverFeaturesReceived( QSet<QString> ) ), SLOT( onNewConnection() ) );
 
42
    connect( m_client, SIGNAL( presenceReceived( Jreen::Presence ) ), SLOT( onNewPresence( Jreen::Presence ) ) );
 
43
    connect( m_client, SIGNAL( iqReceived( Jreen::IQ ) ), SLOT( onNewIq( Jreen::IQ ) ) );
 
44
 
 
45
    connect( this, SIGNAL( newAvatar( QString ) ), SLOT( onNewAvatar( QString ) ) );
 
46
}
 
47
 
 
48
 
 
49
AvatarManager::~AvatarManager()
 
50
{
 
51
}
 
52
 
 
53
 
 
54
void
 
55
AvatarManager::onNewConnection()
 
56
{
 
57
    fetchVCard( m_client->jid().bare() );
 
58
}
 
59
 
 
60
 
 
61
void
 
62
AvatarManager::fetchVCard( const QString& jid )
 
63
{
 
64
    Jreen::IQ iq( Jreen::IQ::Get, jid );
 
65
    iq.addExtension( new Jreen::VCard() );
 
66
    Jreen::IQReply *reply = m_client->send( iq );
 
67
 
 
68
    connect( reply, SIGNAL( received( Jreen::IQ ) ), SLOT( onNewIq( Jreen::IQ ) ) );
 
69
}
 
70
 
 
71
 
 
72
void
 
73
AvatarManager::onNewPresence( const Jreen::Presence& presence )
 
74
{
 
75
    if ( presence.error() )
 
76
    {
 
77
        return;
 
78
    }
 
79
 
 
80
    Jreen::VCardUpdate::Ptr update = presence.payload<Jreen::VCardUpdate>();
 
81
    if ( update )
 
82
    {
 
83
//        qDebug() << "vcard: found update for" << presence.from().full();
 
84
        if ( !isCached( update->photoHash() ) )
 
85
        {
 
86
//            qDebug() << presence.from().full() << "vcard: photo not cached, starting request..." << update->photoHash();
 
87
            fetchVCard( presence.from().bare() );
 
88
        }
 
89
        else
 
90
        {
 
91
//            qDebug() << presence.from().full() << "vcard: photo already cached no request necessary " << update->photoHash();
 
92
            m_JidsAvatarHashes.insert( update->photoHash(), presence.from().bare() );
 
93
 
 
94
            if ( !this->avatar( presence.from().bare() ).isNull() )
 
95
                emit newAvatar( presence.from().bare() );
 
96
        }
 
97
    }
 
98
    else
 
99
    {
 
100
        //TODO: do we want this? might fetch avatars for broken clients
 
101
        fetchVCard( presence.from().bare() );
 
102
    }
 
103
}
 
104
 
 
105
 
 
106
void
 
107
AvatarManager::onNewIq( const Jreen::IQ& iq )
 
108
{
 
109
    Jreen::VCard::Ptr vcard = iq.payload<Jreen::VCard>();
 
110
    if ( vcard )
 
111
    {
 
112
        iq.accept();
 
113
//        qDebug() << Q_FUNC_INFO << "Got vcard from " << iq.from().full();
 
114
 
 
115
        QString id = iq.from().full();
 
116
        QString avatarHash;
 
117
 
 
118
        const Jreen::VCard::Photo &photo = vcard->photo();
 
119
        if ( !photo.data().isEmpty() )
 
120
        {
 
121
//            qDebug() << "vcard: got photo data" << id;
 
122
 
 
123
            avatarHash = QCryptographicHash::hash( photo.data(), QCryptographicHash::Sha1 ).toHex();
 
124
 
 
125
            if ( !m_cacheDir.exists() )
 
126
                m_cacheDir.mkpath( avatarDir( avatarHash ).absolutePath() );
 
127
 
 
128
            QFile file( avatarPath( avatarHash ) );
 
129
            if ( file.open( QIODevice::WriteOnly ) )
 
130
            {
 
131
                file.write( photo.data() );
 
132
                file.close();
 
133
            }
 
134
 
 
135
            m_cachedAvatars.append( avatarHash );
 
136
            m_JidsAvatarHashes.insert( avatarHash, iq.from().bare() );
 
137
 
 
138
            Q_ASSERT( !this->avatar( iq.from().bare() ).isNull() );
 
139
            emit newAvatar( iq.from().bare() );
 
140
        }
 
141
 
 
142
        // got own presence
 
143
        if ( m_client->jid().bare() == id )
 
144
        {
 
145
            Jreen::Presence presence = m_client->presence();
 
146
            Jreen::VCardUpdate::Ptr update = presence.payload<Jreen::VCardUpdate>();
 
147
            if ( update->photoHash() != avatarHash )
 
148
            {
 
149
                update->setPhotoHash( avatarHash );
 
150
                m_client->send( presence );
 
151
            }
 
152
        }
 
153
    }
 
154
}
 
155
 
 
156
 
 
157
QPixmap
 
158
AvatarManager::avatar( const QString& jid ) const
 
159
{
 
160
    if ( isCached( avatarHash( jid ) ) )
 
161
    {
 
162
        return QPixmap( avatarPath( avatarHash( jid ) ) );
 
163
    }
 
164
    else
 
165
    {
 
166
        return QPixmap();
 
167
    }
 
168
}
 
169
 
 
170
 
 
171
QString
 
172
AvatarManager::avatarHash( const QString& jid ) const
 
173
{
 
174
    //qDebug() << Q_FUNC_INFO << jid << m_JidsAvatarHashes.key( jid );
 
175
    return m_JidsAvatarHashes.key( jid );
 
176
}
 
177
 
 
178
 
 
179
QDir
 
180
AvatarManager::avatarDir( const QString& /* avatarHash */ ) const
 
181
{
 
182
    return m_cacheDir;
 
183
}
 
184
 
 
185
 
 
186
QString
 
187
AvatarManager::avatarPath( const QString& avatarHash ) const
 
188
{
 
189
    Q_ASSERT( !avatarHash.contains( "@" ) );
 
190
    return avatarDir( avatarHash ).absoluteFilePath( avatarHash );
 
191
}
 
192
 
 
193
 
 
194
bool
 
195
AvatarManager::isCached( const QString& avatarHash ) const
 
196
{
 
197
    return m_cachedAvatars.contains( avatarHash );
 
198
}
 
199
 
 
200
 
 
201
void
 
202
AvatarManager::onNewAvatar( const QString& /* jid */ )
 
203
{
 
204
//    qDebug() << Q_FUNC_INFO <<  "Found new Avatar..." << jid;
 
205
}