• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.14.10 API Reference
  • KDE Home
  • Contact Us
 

Akonadi Social Utils Library

  • akonadi
  • socialutils
imageprovider.cpp
1/*
2 Avatar provider - based on Microblog dataengine implementation
3 Copyright (C) 2008 Aaron Seigo <aseigo@kde.org>
4 Copyright (C) 2012 Martin Klapetek <martin.klapetek@gmail.com>
5
6 This library is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or (at your
9 option) any later version.
10
11 This library is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
14 License for more details.
15
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301, USA.
20*/
21
22#include "imageprovider.h"
23
24#include <KDebug>
25#include <KIO/Job>
26#include <KImageCache>
27
28#include <QPainter>
29
30class Akonadi::ImageProviderPrivate
31{
32 Q_DECLARE_PUBLIC( ImageProvider )
33
34 public:
35
36 struct QueuedJobHelper {
37 QString who;
38 KUrl url;
39 bool polishImage;
40 };
41
42 ImageProviderPrivate( ImageProvider *q ) : q_ptr( q )
43 {
44 }
45
49 QImage polishImage( const QImage &img )
50 {
51 const int sz = 48 * 4;
52 QImage roundedImage = QImage( QSize( sz, sz ), QImage::Format_ARGB32_Premultiplied );
53 roundedImage.fill( Qt::transparent );
54 QPainter p;
55 p.begin( &roundedImage );
56 QPainterPath clippingPath;
57 QRectF imgRect = QRectF( QPoint( 0, 0 ), roundedImage.size() );
58 clippingPath.addRoundedRect( imgRect, 24, 24 );
59 p.setClipPath( clippingPath );
60 p.setClipping( true );
61 p.drawImage( QRectF( QPointF( 0, 0 ), roundedImage.size() ), img );
62 return roundedImage;
63 }
64
66 QHash<KJob *, QString> jobs;
67
69 QHash<KJob *, QByteArray> jobData;
70
72 int runningJobs;
73
75 QList<QueuedJobHelper> queuedJobs;
76
78 QStringList pendingPersons;
79
81 KImageCache *imageCache;
82
83 private:
87 void result( KJob *job );
88
92 void recv( KIO::Job *job, const QByteArray &data );
93
94 ImageProvider *q_ptr;
95};
96
97void Akonadi::ImageProviderPrivate::recv( KIO::Job *job, const QByteArray &data )
98{
99 jobData[job] += data;
100}
101
102void Akonadi::ImageProviderPrivate::result( KJob *job )
103{
104 Q_Q( ImageProvider );
105 if ( !jobs.contains( job ) ) {
106 kDebug() << "Tried to handle unknown job, returning...";
107 return;
108 }
109
110 runningJobs--;
111
112 if ( queuedJobs.count() > 0 ) {
113 QueuedJobHelper helper = queuedJobs.takeFirst();
114 q->loadImage( helper.who, helper.url, helper.polishImage );
115 }
116
117 if ( job->error() ) {
118 // TODO: error handling
119 KIO::TransferJob* kiojob = dynamic_cast<KIO::TransferJob*>( job );
120 kError() << "Image job for" << jobs.value( job ) << "returned error:" << kiojob->errorString();
121 } else {
122 const QString who = jobs.value( job );
123
124 QImage image;
125 image.loadFromData( jobData.value( job ) );
126 KIO::TransferJob* kiojob = dynamic_cast<KIO::TransferJob*>( job );
127 const QString cacheKey = who + QLatin1Char( '@' ) +
128 kiojob->property( "imageUrl" ).value<KUrl>().pathOrUrl();
129
130 kDebug() << "Downloaded image for" << who << "(key:" << cacheKey << ")";
131
132 imageCache->insertImage( cacheKey, image );
133 pendingPersons.removeAll( cacheKey );
134
135 bool polishImage = job->property( "polishImage" ).toBool();
136
137 Q_EMIT q->imageLoaded( who,
138 kiojob->property( "imageUrl" ).value<KUrl>(),
139 polishImage ? this->polishImage( image ) : image );
140 }
141
142 jobs.remove( job );
143 jobData.remove( job );
144}
145
146Akonadi::ImageProvider::ImageProvider( QObject *parent )
147 : QObject( parent ),
148 d_ptr( new ImageProviderPrivate( this ) )
149{
150 Q_D( ImageProvider );
151 d->imageCache = 0;
152 d->runningJobs = 0;
153}
154
155Akonadi::ImageProvider::~ImageProvider()
156{
157 Q_D( ImageProvider );
158 delete d;
159}
160
161QImage Akonadi::ImageProvider::loadImage( const QString &who, const KUrl &url,
162 bool polishImage, KImageCache *cache )
163{
164 Q_D( ImageProvider );
165
166 if ( who.isEmpty() ) {
167 return QImage();
168 }
169
170 if ( !d->imageCache && !cache ) {
171 //if no old cache and no passed cache, default to plasma_engine_preview
172 d->imageCache = new KImageCache( QLatin1String( "plasma_engine_preview" ),
173 10485760 ); // Re-use previewengine's cache
174 } else if ( !d->imageCache && cache ) {
175 //if there is no old cache, set the new one
176 d->imageCache = cache;
177 } else if ( d->imageCache && cache ) {
178 //delete old one and set new one
179 //delete d->imageCache; //FIXME: crashes
180 d->imageCache = cache;
181 }
182
183 const QString cacheKey = who + QLatin1Char( '@' ) + url.pathOrUrl();
184
185 // Make sure we only start one job per user
186 if ( d->pendingPersons.contains( cacheKey ) ) {
187 kDebug() << "Job for" << who << "already running, returning";
188 return QImage();
189 }
190
191 // Check if the image is in the cache, if so emit it and return
192 QImage preview;
193 preview.fill( Qt::transparent );
194
195 if ( d->imageCache->findImage( cacheKey, &preview ) ) {
196 // cache hit
197 kDebug() << "Image for" << who << "already in cache, returning it";
198 return polishImage ? d->polishImage( preview ) : preview;
199 }
200
201 if ( !url.isValid() ) {
202 kDebug() << "Invalid url, returning";
203 return QImage();
204 }
205
206 kDebug() << "No cache, fetching image for" << who;
207
208 d->pendingPersons << cacheKey;
209 //FIXME: since kio_http bombs the system with too many request put a temporary
210 // arbitrary limit here, revert as soon as BUG 192625 is fixed
211 // Note: seems fixed.
212 if ( d->runningJobs < 500 ) {
213 d->runningJobs++;
214 kDebug() << "Starting fetch job for" << who;
215 KIO::Job *job = KIO::get( url, KIO::NoReload, KIO::HideProgressInfo );
216 job->setAutoDelete( true );
217 d->jobs[job] = who;
218 connect( job, SIGNAL(data(KIO::Job*,QByteArray)),
219 this, SLOT(recv(KIO::Job*,QByteArray)) );
220 connect( job, SIGNAL(result(KJob*)),
221 this, SLOT(result(KJob*)) );
222 // The url needs to be stored explicitly because, for example, facebook redirects
223 // randomly between its servers causing the url to be different every time, which
224 // makes storing the job's url in cache impossible therefore we set it here and
225 // use this when saving to the cache instead of job->url()
226 job->setProperty( "imageUrl", url );
227 job->setProperty( "polishImage", polishImage );
228 job->start();
229 } else {
230 kDebug() << "Queuing job for" << who;
231 ImageProviderPrivate::QueuedJobHelper helper;
232 helper.who = who;
233 helper.url = url;
234 helper.polishImage = polishImage;
235 d->queuedJobs.append( helper );
236 }
237
238 return QImage();
239}
240
241void Akonadi::ImageProvider::abortAllJobs()
242{
243 Q_D( ImageProvider );
244 Q_FOREACH ( KJob *job, d->jobs.keys() ) {
245 job->kill();
246 d->jobs.remove( job );
247 d->jobData.remove( job );
248 }
249}
250
251#include "moc_imageprovider.cpp"
Akonadi::ImageProvider
Class fetching avatars/images from network and storing them in KImageCache.
Definition imageprovider.h:45
Akonadi::ImageProvider::loadImage
QImage loadImage(const QString &who, const KUrl &url, bool polishImage=false, KImageCache *cache=0)
Starts fetching the avatar/image from network.
Definition imageprovider.cpp:161
Akonadi::ImageProvider::abortAllJobs
void abortAllJobs()
Aborts all running jobs.
Definition imageprovider.cpp:241
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Wed Jan 24 2024 00:00:00 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Akonadi Social Utils Library

Skip menu "Akonadi Social Utils Library"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Members
  • File List
  • Related Pages

kdepimlibs-4.14.10 API Reference

Skip menu "kdepimlibs-4.14.10 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal