summaryrefslogtreecommitdiffstats
path: root/src/declarative/util/qdeclarativepixmapcache.cpp
blob: 9221d78782c2533a03c1744ad75931d04ab49661 (plain)
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtDeclarative module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, 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.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "private/qdeclarativepixmapcache_p.h"
#include "qdeclarativenetworkaccessmanagerfactory.h"
#include "qdeclarativeimageprovider.h"

#include <qdeclarativeengine.h>
#include <private/qdeclarativeglobal_p.h>
#include <private/qdeclarativeengine_p.h>

#include <QCoreApplication>
#include <QImageReader>
#include <QHash>
#include <QNetworkReply>
#include <QPixmapCache>
#include <QFile>
#include <QThread>
#include <QMutex>
#include <QMutexLocker>
#include <QWaitCondition>
#include <QBuffer>
#include <QWaitCondition>
#include <QtCore/qdebug.h>
#include <private/qobject_p.h>
#include <QSslError>

#define IMAGEREQUEST_MAX_REQUEST_COUNT       8
#define IMAGEREQUEST_MAX_REDIRECT_RECURSION 16
#define CACHE_EXPIRE_TIME 30
#define CACHE_REMOVAL_FRACTION 4

QT_BEGIN_NAMESPACE

// The cache limit describes the maximum "junk" in the cache.
// These are the same defaults as QPixmapCache
#if defined(Q_OS_SYMBIAN)
static int cache_limit = 1024 * 1024; // 1048 KB cache limit for symbian
#elif defined(Q_WS_QWS) || defined(Q_WS_WINCE)
static int cache_limit = 2048 * 1024; // 2048 KB cache limit for embedded
#else
static int cache_limit = 10240 * 1024; // 10 MB cache limit for desktop
#endif

class QDeclarativePixmapReader;
class QDeclarativePixmapData;
class QDeclarativePixmapReply : public QObject
{
    Q_OBJECT
public:
    enum ReadError { NoError, Loading, Decoding };

    QDeclarativePixmapReply(QDeclarativePixmapData *);
    ~QDeclarativePixmapReply();

    QDeclarativePixmapData *data;
    QDeclarativePixmapReader *reader;
    QSize requestSize;

    bool loading;
    int redirectCount;

    class Event : public QEvent {
    public:
        Event(ReadError, const QString &, const QSize &, const QImage &);

        ReadError error;
        QString errorString;
        QSize implicitSize;
        QImage image;
    };
    void postReply(ReadError, const QString &, const QSize &, const QImage &);


Q_SIGNALS:
    void finished();
    void downloadProgress(qint64, qint64);

protected:
    bool event(QEvent *event);

private:
    Q_DISABLE_COPY(QDeclarativePixmapReply)

public:
    static int finishedIndex;
    static int downloadProgressIndex;
};

class QDeclarativePixmapReaderThreadObject : public QObject {
    Q_OBJECT
public:
    QDeclarativePixmapReaderThreadObject(QDeclarativePixmapReader *);
    void processJobs();
    virtual bool event(QEvent *e);
private slots:
    void networkRequestDone();
private:
    QDeclarativePixmapReader *reader;
};

class QDeclarativePixmapData;
class QDeclarativePixmapReader : public QThread
{
    Q_OBJECT
public:
    QDeclarativePixmapReader(QDeclarativeEngine *eng);
    ~QDeclarativePixmapReader();

    QDeclarativePixmapReply *getImage(QDeclarativePixmapData *);
    void cancel(QDeclarativePixmapReply *rep);

    static QDeclarativePixmapReader *instance(QDeclarativeEngine *engine);

protected:
    void run();

private:
    friend class QDeclarativePixmapReaderThreadObject;
    void processJobs();
    void processJob(QDeclarativePixmapReply *, const QUrl &, const QSize &);
    void networkRequestDone(QNetworkReply *);

    QList<QDeclarativePixmapReply*> jobs;
    QList<QDeclarativePixmapReply*> cancelled;
    QDeclarativeEngine *engine;
    QObject *eventLoopQuitHack;

    QMutex mutex;
    QDeclarativePixmapReaderThreadObject *threadObject;
    QWaitCondition waitCondition;

    QNetworkAccessManager *networkAccessManager();
    QNetworkAccessManager *accessManager;

    QHash<QNetworkReply*,QDeclarativePixmapReply*> replies;

    static int replyDownloadProgress;
    static int replyFinished;
    static int downloadProgress;
    static int threadNetworkRequestDone;
    static QHash<QDeclarativeEngine *,QDeclarativePixmapReader*> readers;
    static QMutex readerMutex;
};

class QDeclarativePixmapData
{
public:
    QDeclarativePixmapData(const QUrl &u, const QSize &s, const QString &e)
    : refCount(1), inCache(false), pixmapStatus(QDeclarativePixmap::Error), 
      url(u), errorString(e), requestSize(s), reply(0), prevUnreferenced(0),
      prevUnreferencedPtr(0), nextUnreferenced(0)
    {
    }

    QDeclarativePixmapData(const QUrl &u, const QSize &r)
    : refCount(1), inCache(false), pixmapStatus(QDeclarativePixmap::Loading), 
      url(u), requestSize(r), reply(0), prevUnreferenced(0), prevUnreferencedPtr(0), 
      nextUnreferenced(0)
    {
    }

    QDeclarativePixmapData(const QUrl &u, const QPixmap &p, const QSize &s, const QSize &r)
    : refCount(1), inCache(false), privatePixmap(false), pixmapStatus(QDeclarativePixmap::Ready), 
      url(u), pixmap(p), implicitSize(s), requestSize(r), reply(0), prevUnreferenced(0),
      prevUnreferencedPtr(0), nextUnreferenced(0)
    {
    }

    QDeclarativePixmapData(const QPixmap &p)
    : refCount(1), inCache(false), privatePixmap(true), pixmapStatus(QDeclarativePixmap::Ready),
      pixmap(p), implicitSize(p.size()), requestSize(p.size()), reply(0), prevUnreferenced(0),
      prevUnreferencedPtr(0), nextUnreferenced(0)
    {
    }

    int cost() const;
    void addref();
    void release();
    void addToCache();
    void removeFromCache();

    uint refCount;

    bool inCache:1;
    bool privatePixmap:1;
    
    QDeclarativePixmap::Status pixmapStatus;
    QUrl url;
    QString errorString;
    QPixmap pixmap;
    QSize implicitSize;
    QSize requestSize;

    QDeclarativePixmapReply *reply;

    QDeclarativePixmapData *prevUnreferenced;
    QDeclarativePixmapData**prevUnreferencedPtr;
    QDeclarativePixmapData *nextUnreferenced;
};

int QDeclarativePixmapReply::finishedIndex = -1;
int QDeclarativePixmapReply::downloadProgressIndex = -1;

// XXX
QHash<QDeclarativeEngine *,QDeclarativePixmapReader*> QDeclarativePixmapReader::readers;
QMutex QDeclarativePixmapReader::readerMutex;

int QDeclarativePixmapReader::replyDownloadProgress = -1;
int QDeclarativePixmapReader::replyFinished = -1;
int QDeclarativePixmapReader::downloadProgress = -1;
int QDeclarativePixmapReader::threadNetworkRequestDone = -1;


void QDeclarativePixmapReply::postReply(ReadError error, const QString &errorString, 
                                        const QSize &implicitSize, const QImage &image)
{
    loading = false;
    QCoreApplication::postEvent(this, new Event(error, errorString, implicitSize, image));
}

QDeclarativePixmapReply::Event::Event(ReadError e, const QString &s, const QSize &iSize, const QImage &i)
: QEvent(QEvent::User), error(e), errorString(s), implicitSize(iSize), image(i)
{
}

QNetworkAccessManager *QDeclarativePixmapReader::networkAccessManager()
{
    if (!accessManager) {
        Q_ASSERT(threadObject);
        accessManager = QDeclarativeEnginePrivate::get(engine)->createNetworkAccessManager(threadObject);
    }
    return accessManager;
}

static bool readImage(const QUrl& url, QIODevice *dev, QImage *image, QString *errorString, QSize *impsize, 
                      const QSize &requestSize)
{
    QImageReader imgio(dev);

    bool force_scale = false;
    if (url.path().endsWith(QLatin1String(".svg"),Qt::CaseInsensitive)) {
        imgio.setFormat("svg"); // QSvgPlugin::capabilities bug QTBUG-9053
        force_scale = true;
    }

    bool scaled = false;
    if (requestSize.width() > 0 || requestSize.height() > 0) {
        QSize s = imgio.size();
        if (requestSize.width() && (force_scale || requestSize.width() < s.width())) {
            if (requestSize.height() <= 0)
                s.setHeight(s.height()*requestSize.width()/s.width());
            s.setWidth(requestSize.width()); scaled = true;
        }
        if (requestSize.height() && (force_scale || requestSize.height() < s.height())) {
            if (requestSize.width() <= 0)
                s.setWidth(s.width()*requestSize.height()/s.height());
            s.setHeight(requestSize.height()); scaled = true;
        }
        if (scaled) { imgio.setScaledSize(s); }
    }

    if (impsize)
        *impsize = imgio.size();

    if (imgio.read(image)) {
        if (impsize && impsize->width() < 0)
            *impsize = image->size();
        return true;
    } else {
        if (errorString)
            *errorString = QDeclarativePixmap::tr("Error decoding: %1: %2").arg(url.toString())
                                .arg(imgio.errorString());
        return false;
    }
}

QDeclarativePixmapReader::QDeclarativePixmapReader(QDeclarativeEngine *eng)
: QThread(eng), engine(eng), threadObject(0), accessManager(0)
{
    eventLoopQuitHack = new QObject;
    eventLoopQuitHack->moveToThread(this);
    connect(eventLoopQuitHack, SIGNAL(destroyed(QObject*)), SLOT(quit()), Qt::DirectConnection);
    start(QThread::IdlePriority);
}

QDeclarativePixmapReader::~QDeclarativePixmapReader()
{
    readerMutex.lock();
    readers.remove(engine);
    readerMutex.unlock();

    eventLoopQuitHack->deleteLater();
    wait();
}

void QDeclarativePixmapReader::networkRequestDone(QNetworkReply *reply)
{
    QDeclarativePixmapReply *job = replies.take(reply);

    if (job) {
        job->redirectCount++;
        if (job->redirectCount < IMAGEREQUEST_MAX_REDIRECT_RECURSION) {
            QVariant redirect = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
            if (redirect.isValid()) {
                QUrl url = reply->url().resolved(redirect.toUrl());
                QNetworkRequest req(url);
                req.setAttribute(QNetworkRequest::HttpPipeliningAllowedAttribute, true);

                reply->deleteLater();
                reply = networkAccessManager()->get(req);

                QMetaObject::connect(reply, replyDownloadProgress, job, downloadProgress);
                QMetaObject::connect(reply, replyFinished, threadObject, threadNetworkRequestDone);

                replies.insert(reply, job);
                return;
            }
        }

        QImage image;
        QDeclarativePixmapReply::ReadError error = QDeclarativePixmapReply::NoError;
        QString errorString;
        QSize readSize;
        if (reply->error()) {
            error = QDeclarativePixmapReply::Loading;
            errorString = reply->errorString();
        } else {
            QByteArray all = reply->readAll();
            QBuffer buff(&all);
            buff.open(QIODevice::ReadOnly);
            if (!readImage(reply->url(), &buff, &image, &errorString, &readSize, job->requestSize)) {
                error = QDeclarativePixmapReply::Decoding;
            }
        }
        // send completion event to the QDeclarativePixmapReply
        mutex.lock();
        if (!cancelled.contains(job)) job->postReply(error, errorString, readSize, image);
        mutex.unlock();
    }
    reply->deleteLater();

    // kick off event loop again incase we have dropped below max request count
    threadObject->processJobs();
}

QDeclarativePixmapReaderThreadObject::QDeclarativePixmapReaderThreadObject(QDeclarativePixmapReader *i)
: reader(i)
{
}

void QDeclarativePixmapReaderThreadObject::processJobs() 
{ 
    QCoreApplication::postEvent(this, new QEvent(QEvent::User)); 
}

bool QDeclarativePixmapReaderThreadObject::event(QEvent *e) 
{
    if (e->type() == QEvent::User) { 
        reader->processJobs(); 
        return true; 
    } else { 
        return QObject::event(e);
    }
}

void QDeclarativePixmapReaderThreadObject::networkRequestDone()
{
    QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
    reader->networkRequestDone(reply);
}

void QDeclarativePixmapReader::processJobs()
{
    QMutexLocker locker(&mutex);

    while (true) {
        if (cancelled.isEmpty() && (jobs.isEmpty() || replies.count() >= IMAGEREQUEST_MAX_REQUEST_COUNT)) 
            return; // Nothing else to do

        // Clean cancelled jobs
        if (cancelled.count()) {
            for (int i = 0; i < cancelled.count(); ++i) {
                QDeclarativePixmapReply *job = cancelled.at(i);
                QNetworkReply *reply = replies.key(job, 0);
                if (reply && reply->isRunning()) {
                    // cancel any jobs already started
                    replies.remove(reply);
                    reply->close();
                }
                // deleteLater, since not owned by this thread
                job->deleteLater();
            }
            cancelled.clear();
        }

        if (!jobs.isEmpty() && replies.count() < IMAGEREQUEST_MAX_REQUEST_COUNT) {
            QDeclarativePixmapReply *runningJob = jobs.takeLast();
            runningJob->loading = true;

            QUrl url = runningJob->data->url;
            QSize requestSize = runningJob->data->requestSize;
            locker.unlock();
            processJob(runningJob, url, requestSize);
            locker.relock();
        }
    }
}

void QDeclarativePixmapReader::processJob(QDeclarativePixmapReply *runningJob, const QUrl &url, 
                                          const QSize &requestSize)
{
    // fetch
    if (url.scheme() == QLatin1String("image")) {
        // Use QmlImageProvider
        QSize readSize;
        QDeclarativeEnginePrivate *ep = QDeclarativeEnginePrivate::get(engine);
        QImage image = ep->getImageFromProvider(url, &readSize, requestSize);

        QDeclarativePixmapReply::ReadError errorCode = QDeclarativePixmapReply::NoError;
        QString errorStr;
        if (image.isNull()) {
            errorCode = QDeclarativePixmapReply::Loading;
            errorStr = QDeclarativePixmap::tr("Failed to get image from provider: %1").arg(url.toString());
        }

        mutex.lock();
        if (!cancelled.contains(runningJob)) runningJob->postReply(errorCode, errorStr, readSize, image);
        mutex.unlock();
    } else {
        QString lf = QDeclarativeEnginePrivate::urlToLocalFileOrQrc(url);
        if (!lf.isEmpty()) {
            // Image is local - load/decode immediately
            QImage image;
            QDeclarativePixmapReply::ReadError errorCode = QDeclarativePixmapReply::NoError;
            QString errorStr;
            QFile f(lf);
            QSize readSize;
            if (f.open(QIODevice::ReadOnly)) {
                if (!readImage(url, &f, &image, &errorStr, &readSize, requestSize))
                    errorCode = QDeclarativePixmapReply::Loading;
            } else {
                errorStr = QDeclarativePixmap::tr("Cannot open: %1").arg(url.toString());
                errorCode = QDeclarativePixmapReply::Loading;
            }
            mutex.lock();
            if (!cancelled.contains(runningJob)) runningJob->postReply(errorCode, errorStr, readSize, image);
            mutex.unlock();
        } else {
            // Network resource
            QNetworkRequest req(url);
            req.setAttribute(QNetworkRequest::HttpPipeliningAllowedAttribute, true);
            QNetworkReply *reply = networkAccessManager()->get(req);

            QMetaObject::connect(reply, replyDownloadProgress, runningJob, downloadProgress);
            QMetaObject::connect(reply, replyFinished, threadObject, threadNetworkRequestDone);

            replies.insert(reply, runningJob);
        }
    }
}

QDeclarativePixmapReader *QDeclarativePixmapReader::instance(QDeclarativeEngine *engine)
{
    readerMutex.lock();
    QDeclarativePixmapReader *reader = readers.value(engine);
    if (!reader) {
        reader = new QDeclarativePixmapReader(engine);
        readers.insert(engine, reader);
    }
    readerMutex.unlock();

    return reader;
}

QDeclarativePixmapReply *QDeclarativePixmapReader::getImage(QDeclarativePixmapData *data)
{
    mutex.lock();
    QDeclarativePixmapReply *reply = new QDeclarativePixmapReply(data);
    reply->reader = this;
    jobs.append(reply);
    // XXX 
    if (threadObject) threadObject->processJobs();
    mutex.unlock();
    return reply;
}

void QDeclarativePixmapReader::cancel(QDeclarativePixmapReply *reply)
{
    mutex.lock();
    if (reply->loading) {
        cancelled.append(reply);
        reply->data = 0;
        // XXX 
        if (threadObject) threadObject->processJobs();
    } else {
        jobs.removeAll(reply);
        delete reply;
    }
    mutex.unlock();
}

void QDeclarativePixmapReader::run()
{
    if (replyDownloadProgress == -1) {
        const QMetaObject *nr = &QNetworkReply::staticMetaObject;
        const QMetaObject *pr = &QDeclarativePixmapReply::staticMetaObject;
        const QMetaObject *ir = &QDeclarativePixmapReaderThreadObject::staticMetaObject;
        replyDownloadProgress = nr->indexOfSignal("downloadProgress(qint64,qint64)");
        replyFinished = nr->indexOfSignal("finished()");
        downloadProgress = pr->indexOfSignal("downloadProgress(qint64,qint64)");
        threadNetworkRequestDone = ir->indexOfSlot("networkRequestDone()");
    }

    mutex.lock();
    threadObject = new QDeclarativePixmapReaderThreadObject(this);
    mutex.unlock();

    processJobs();
    exec();

    delete threadObject;
    threadObject = 0;
}

class QDeclarativePixmapKey
{
public:
    const QUrl *url;
    const QSize *size;
};

inline bool operator==(const QDeclarativePixmapKey &lhs, const QDeclarativePixmapKey &rhs)
{
    return *lhs.size == *rhs.size && *lhs.url == *rhs.url;
}

inline uint qHash(const QDeclarativePixmapKey &key)
{
    return qHash(*key.url) ^ key.size->width() ^ key.size->height();
}

class QDeclarativePixmapStore : public QObject
{
    Q_OBJECT
public:
    QDeclarativePixmapStore();

    void unreferencePixmap(QDeclarativePixmapData *);
    void referencePixmap(QDeclarativePixmapData *);

protected:
    virtual void timerEvent(QTimerEvent *);

public:
    QHash<QDeclarativePixmapKey, QDeclarativePixmapData *> m_cache;

private:
    void shrinkCache(int remove);

    QDeclarativePixmapData *m_unreferencedPixmaps;
    QDeclarativePixmapData *m_lastUnreferencedPixmap;

    int m_unreferencedCost;
    int m_timerId;
};
Q_GLOBAL_STATIC(QDeclarativePixmapStore, pixmapStore);

QDeclarativePixmapStore::QDeclarativePixmapStore()
: m_unreferencedPixmaps(0), m_lastUnreferencedPixmap(0), m_unreferencedCost(0), m_timerId(-1)
{
}

void QDeclarativePixmapStore::unreferencePixmap(QDeclarativePixmapData *data)
{
    Q_ASSERT(data->prevUnreferenced == 0);
    Q_ASSERT(data->prevUnreferencedPtr == 0);
    Q_ASSERT(data->nextUnreferenced == 0);

    data->nextUnreferenced = m_unreferencedPixmaps;
    data->prevUnreferencedPtr = &m_unreferencedPixmaps;

    m_unreferencedPixmaps = data;
    if (m_unreferencedPixmaps->nextUnreferenced) {
        m_unreferencedPixmaps->nextUnreferenced->prevUnreferenced = m_unreferencedPixmaps;
        m_unreferencedPixmaps->nextUnreferenced->prevUnreferencedPtr = &m_unreferencedPixmaps->nextUnreferenced;
    }

    if (!m_lastUnreferencedPixmap)
        m_lastUnreferencedPixmap = data;

    m_unreferencedCost += data->cost();

    shrinkCache(-1); // Shrink the cache incase it has become larger than cache_limit

    if (m_timerId == -1 && m_unreferencedPixmaps) 
        m_timerId = startTimer(CACHE_EXPIRE_TIME * 1000);
}

void QDeclarativePixmapStore::referencePixmap(QDeclarativePixmapData *data)
{
    Q_ASSERT(data->prevUnreferencedPtr);

    *data->prevUnreferencedPtr = data->nextUnreferenced;
    if (data->nextUnreferenced) { 
        data->nextUnreferenced->prevUnreferencedPtr = data->prevUnreferencedPtr;
        data->nextUnreferenced->prevUnreferenced = data->prevUnreferenced;
    }
    if (m_lastUnreferencedPixmap == data)
        m_lastUnreferencedPixmap = data->prevUnreferenced;

    data->nextUnreferenced = 0;
    data->prevUnreferencedPtr = 0;
    data->prevUnreferenced = 0;

    m_unreferencedCost -= data->cost();
}

void QDeclarativePixmapStore::shrinkCache(int remove)
{
    while ((remove > 0 || m_unreferencedCost > cache_limit) && m_lastUnreferencedPixmap) {
        QDeclarativePixmapData *data = m_lastUnreferencedPixmap;
        Q_ASSERT(data->nextUnreferenced == 0);

        *data->prevUnreferencedPtr = 0;
        m_lastUnreferencedPixmap = data->prevUnreferenced;
        data->prevUnreferencedPtr = 0;
        data->prevUnreferenced = 0;

        remove -= data->cost();
        m_unreferencedCost -= data->cost();
        data->removeFromCache();
        delete data;
    }
}

void QDeclarativePixmapStore::timerEvent(QTimerEvent *)
{
    int removalCost = m_unreferencedCost / CACHE_REMOVAL_FRACTION;

    shrinkCache(removalCost);

    if (m_unreferencedPixmaps == 0) {
        killTimer(m_timerId);
        m_timerId = -1;
    }
}

QDeclarativePixmapReply::QDeclarativePixmapReply(QDeclarativePixmapData *d)
: data(d), reader(0), requestSize(d->requestSize), loading(false), redirectCount(0)
{
    if (finishedIndex == -1) {
        finishedIndex = QDeclarativePixmapReply::staticMetaObject.indexOfSignal("finished()");
        downloadProgressIndex = QDeclarativePixmapReply::staticMetaObject.indexOfSignal("downloadProgress(qint64,qint64)");
    }
}

QDeclarativePixmapReply::~QDeclarativePixmapReply()
{
}

bool QDeclarativePixmapReply::event(QEvent *event)
{
    if (event->type() == QEvent::User) {

        if (data) {
            Event *de = static_cast<Event *>(event);
            data->pixmapStatus = (de->error == NoError) ? QDeclarativePixmap::Ready : QDeclarativePixmap::Error;
            
            if (data->pixmapStatus == QDeclarativePixmap::Ready) {
                data->pixmap = QPixmap::fromImage(de->image);
                data->implicitSize = de->implicitSize;
            } else {
                data->errorString = de->errorString;
                data->removeFromCache(); // We don't continue to cache error'd pixmaps
            }

            data->reply = 0;
            emit finished();
        }

        delete this;
        return true;
    } else {
        return QObject::event(event);
    }
}

int QDeclarativePixmapData::cost() const
{
    return (pixmap.width() * pixmap.height() * pixmap.depth()) / 8;
}

void QDeclarativePixmapData::addref()
{
    ++refCount;
    if (prevUnreferencedPtr) 
        pixmapStore()->referencePixmap(this);
}

void QDeclarativePixmapData::release()
{
    Q_ASSERT(refCount > 0);
    --refCount;

    if (refCount == 0) {
        if (reply) {
            reply->reader->cancel(reply);
            reply = 0;
        }

        if (pixmapStatus == QDeclarativePixmap::Ready) {
            pixmapStore()->unreferencePixmap(this);
        } else {
            removeFromCache();
            delete this;
        }
    }
}

void QDeclarativePixmapData::addToCache()
{
    if (!inCache) {
        QDeclarativePixmapKey key = { &url, &requestSize };
        pixmapStore()->m_cache.insert(key, this);
        inCache = true;
    }
}

void QDeclarativePixmapData::removeFromCache()
{
    if (inCache) {
        QDeclarativePixmapKey key = { &url, &requestSize };
        pixmapStore()->m_cache.remove(key);
        inCache = false;
    }
}

static QDeclarativePixmapData* createPixmapDataSync(QDeclarativeEngine *engine, const QUrl &url, const QSize &requestSize, bool *ok)
{
    if (url.scheme() == QLatin1String("image")) {
        QSize readSize;
        QDeclarativeEnginePrivate *ep = QDeclarativeEnginePrivate::get(engine);
        QDeclarativeImageProvider::ImageType imageType = ep->getImageProviderType(url);

        switch (imageType) {
            case QDeclarativeImageProvider::Image:
            {
                QImage image = ep->getImageFromProvider(url, &readSize, requestSize);
                if (!image.isNull()) {
                    *ok = true;
                    return new QDeclarativePixmapData(url, QPixmap::fromImage(image), readSize, requestSize);
                }
            }
            case QDeclarativeImageProvider::Pixmap:
            {
                QPixmap pixmap = ep->getPixmapFromProvider(url, &readSize, requestSize);
                if (!pixmap.isNull()) {
                    *ok = true;
                    return new QDeclarativePixmapData(url, pixmap, readSize, requestSize);
                }
            }
        }

        // no matching provider, or provider has bad image type, or provider returned null image
        return new QDeclarativePixmapData(url, requestSize,
            QDeclarativePixmap::tr("Failed to get image from provider: %1").arg(url.toString()));
    }

    QString localFile = QDeclarativeEnginePrivate::urlToLocalFileOrQrc(url);
    if (localFile.isEmpty()) 
        return 0;

    QFile f(localFile);
    QSize readSize;
    QString errorString;

    if (f.open(QIODevice::ReadOnly)) {
        QImage image;
        if (readImage(url, &f, &image, &errorString, &readSize, requestSize)) {
            *ok = true;
            return new QDeclarativePixmapData(url, QPixmap::fromImage(image), readSize, requestSize);
        }
    } else {
        errorString = QDeclarativePixmap::tr("Cannot open: %1").arg(url.toString());
    }
    return new QDeclarativePixmapData(url, requestSize, errorString);
}


struct QDeclarativePixmapNull {
    QUrl url;
    QPixmap pixmap;
    QSize size;
};
Q_GLOBAL_STATIC(QDeclarativePixmapNull, nullPixmap);

QDeclarativePixmap::QDeclarativePixmap()
: d(0)
{
}

QDeclarativePixmap::QDeclarativePixmap(QDeclarativeEngine *engine, const QUrl &url)
: d(0)
{
    load(engine, url);
}

QDeclarativePixmap::QDeclarativePixmap(QDeclarativeEngine *engine, const QUrl &url, const QSize &size)
: d(0)
{
    load(engine, url, size);
}

QDeclarativePixmap::~QDeclarativePixmap()
{
    if (d) {
        d->release();
        d = 0;
    }
}

bool QDeclarativePixmap::isNull() const
{
    return d == 0;
}

bool QDeclarativePixmap::isReady() const
{
    return status() == Ready;
}

bool QDeclarativePixmap::isError() const
{
    return status() == Error;
}

bool QDeclarativePixmap::isLoading() const
{
    return status() == Loading;
}

QString QDeclarativePixmap::error() const
{
    if (d)
        return d->errorString;
    else
        return QString();
}

QDeclarativePixmap::Status QDeclarativePixmap::status() const
{
    if (d)
        return d->pixmapStatus;
    else
        return Null;
}

const QUrl &QDeclarativePixmap::url() const
{
    if (d)
        return d->url;
    else
        return nullPixmap()->url;
}

const QSize &QDeclarativePixmap::implicitSize() const
{
    if (d) 
        return d->implicitSize;
    else
        return nullPixmap()->size;
}

const QSize &QDeclarativePixmap::requestSize() const
{
    if (d)
        return d->requestSize;
    else
        return nullPixmap()->size;
}

const QPixmap &QDeclarativePixmap::pixmap() const
{
    if (d) 
        return d->pixmap;
    else
        return nullPixmap()->pixmap;
}

void QDeclarativePixmap::setPixmap(const QPixmap &p) 
{
    clear();

    if (!p.isNull())
        d = new QDeclarativePixmapData(p);
}

int QDeclarativePixmap::width() const
{
    if (d) 
        return d->pixmap.width();
    else
        return 0;
}

int QDeclarativePixmap::height() const
{
    if (d) 
        return d->pixmap.height();
    else
        return 0;
}

QRect QDeclarativePixmap::rect() const
{
    if (d)
        return d->pixmap.rect();
    else
        return QRect();
}

void QDeclarativePixmap::load(QDeclarativeEngine *engine, const QUrl &url)
{
    load(engine, url, QSize(), QDeclarativePixmap::Cache);
}

void QDeclarativePixmap::load(QDeclarativeEngine *engine, const QUrl &url, QDeclarativePixmap::Options options)
{
    load(engine, url, QSize(), options);
}

void QDeclarativePixmap::load(QDeclarativeEngine *engine, const QUrl &url, const QSize &size)
{
    load(engine, url, size, QDeclarativePixmap::Cache);
}

void QDeclarativePixmap::load(QDeclarativeEngine *engine, const QUrl &url, const QSize &requestSize, QDeclarativePixmap::Options options)
{
    if (d) { d->release(); d = 0; }

    QDeclarativePixmapKey key = { &url, &requestSize };
    QDeclarativePixmapStore *store = pixmapStore();

    QHash<QDeclarativePixmapKey, QDeclarativePixmapData *>::Iterator iter = store->m_cache.find(key);

    if (iter == store->m_cache.end()) {
        if (options & QDeclarativePixmap::Asynchronous) {
            // pixmaps can only be loaded synchronously
            if (url.scheme() == QLatin1String("image") 
                    && QDeclarativeEnginePrivate::get(engine)->getImageProviderType(url) == QDeclarativeImageProvider::Pixmap) {
                options &= ~QDeclarativePixmap::Asynchronous;
            }
        }

        if (!(options & QDeclarativePixmap::Asynchronous)) {
            bool ok = false;
            d = createPixmapDataSync(engine, url, requestSize, &ok);
            if (ok) {
                if (options & QDeclarativePixmap::Cache)
                    d->addToCache();
                return;
            }
            if (d)  // loadable, but encountered error while loading
                return;
        } 

        if (!engine)
            return;

        QDeclarativePixmapReader *reader = QDeclarativePixmapReader::instance(engine);

        d = new QDeclarativePixmapData(url, requestSize);
        if (options & QDeclarativePixmap::Cache)
            d->addToCache();

        d->reply = reader->getImage(d);
    } else {
        d = *iter;
        d->addref();
    }
}

void QDeclarativePixmap::clear()
{
    if (d) {
        d->release();
        d = 0;
    }
}

void QDeclarativePixmap::clear(QObject *obj)
{
    if (d) {
        if (d->reply) 
            QObject::disconnect(d->reply, 0, obj, 0);
        d->release();
        d = 0;
    }
}

bool QDeclarativePixmap::connectFinished(QObject *object, const char *method)
{
    if (!d || !d->reply) {
        qWarning("QDeclarativePixmap: connectFinished() called when not loading.");
        return false;
    }

    return QObject::connect(d->reply, SIGNAL(finished()), object, method);
}

bool QDeclarativePixmap::connectFinished(QObject *object, int method)
{
    if (!d || !d->reply) {
        qWarning("QDeclarativePixmap: connectFinished() called when not loading.");
        return false;
    }

    return QMetaObject::connect(d->reply, QDeclarativePixmapReply::finishedIndex, object, method);
}

bool QDeclarativePixmap::connectDownloadProgress(QObject *object, const char *method)
{
    if (!d || !d->reply) {
        qWarning("QDeclarativePixmap: connectDownloadProgress() called when not loading.");
        return false;
    }

    return QObject::connect(d->reply, SIGNAL(downloadProgress(qint64,qint64)), object, method);
}

bool QDeclarativePixmap::connectDownloadProgress(QObject *object, int method)
{
    if (!d || !d->reply) {
        qWarning("QDeclarativePixmap: connectDownloadProgress() called when not loading.");
        return false;
    }

    return QMetaObject::connect(d->reply, QDeclarativePixmapReply::downloadProgressIndex, object, method);
}

QT_END_NAMESPACE

#include <qdeclarativepixmapcache.moc>