WebImageCache.cc 4.78 KB
Newer Older
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
/*=====================================================================

QGroundControl Open Source Ground Control Station

(c) 2009, 2010 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>

This file is part of the QGROUNDCONTROL project

    QGROUNDCONTROL is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    QGROUNDCONTROL 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.

======================================================================*/

/**
 * @file
 *   @brief Definition of the class WebImageCache.
 *
28
 *   @author Lionel Heng <hengli@inf.ethz.ch>
29 30 31 32 33
 *
 */

#include "WebImageCache.h"

34
#include <cstdio>
35 36 37
#include <QNetworkReply>
#include <QPixmap>

38
WebImageCache::WebImageCache(QObject* parent, int cacheSize)
39
    : QObject(parent)
40 41 42
    , mCacheSize(cacheSize)
    , mCurrentReference(0)
    , mNetworkManager(new QNetworkAccessManager)
43
{
44 45
    for (int i = 0; i < mCacheSize; ++i)
    {
46 47
        WebImagePtr image(new WebImage);

48
        mWebImages.push_back(image);
49 50
    }

51
    connect(mNetworkManager.data(), SIGNAL(finished(QNetworkReply*)),
52 53 54
            this, SLOT(downloadFinished(QNetworkReply*)));
}

55
QPair<WebImagePtr, int>
56
WebImageCache::lookup(const QString& url)
57
{
58
    QPair<WebImagePtr, int> cacheEntry;
59

60 61 62 63 64 65 66 67
    for (int i = 0; i < mWebImages.size(); ++i)
    {
        WebImagePtr& image = mWebImages[i];

        if (image->getState() != WebImage::UNINITIALIZED &&
            image->getSourceURL() == url)
        {
            cacheEntry.first = image;
68 69 70 71 72
            cacheEntry.second = i;
            break;
        }
    }

73 74 75 76 77 78
    if (cacheEntry.first.isNull())
    {
        for (int i = 0; i < mWebImages.size(); ++i)
        {
            WebImagePtr& image = mWebImages[i];

79
            // get uninitialized image
80 81 82
            if (image->getState() == WebImage::UNINITIALIZED)
            {
                cacheEntry.first = image;
83 84 85 86
                cacheEntry.second = i;
                break;
            }
            // get oldest image
87
            else if (image->getState() == WebImage::READY &&
88
                     (cacheEntry.first.isNull() ||
89 90 91 92
                      image->getLastReference() <
                      cacheEntry.first->getLastReference()))
            {
                cacheEntry.first = image;
93 94 95 96
                cacheEntry.second = i;
            }
        }

97 98 99 100 101 102 103 104
        if (cacheEntry.first.isNull())
        {
           return qMakePair(WebImagePtr(), -1);
        }
        else
        {
            if (cacheEntry.first->getState() == WebImage::READY)
            {
105 106 107
                cacheEntry.first->clear();
            }
            cacheEntry.first->setSourceURL(url);
108 109
            cacheEntry.first->setLastReference(mCurrentReference);
            ++mCurrentReference;
110 111
            cacheEntry.first->setState(WebImage::REQUESTED);

112 113 114 115 116 117 118 119
            if (url.left(4).compare("http") == 0)
            {
                mNetworkManager->get(QNetworkRequest(QUrl(url)));
            }
            else
            {
                if (cacheEntry.first->setData(url))
                {
120 121
                    cacheEntry.first->setSyncFlag(true);
                    cacheEntry.first->setState(WebImage::READY);
122 123 124
                }
                else
                {
125 126 127 128 129 130
                    cacheEntry.first->setState(WebImage::UNINITIALIZED);
                }
            }

            return cacheEntry;
        }
131 132 133 134 135 136 137
    }
    else
    {
        if (cacheEntry.first->getState() == WebImage::READY)
        {
            cacheEntry.first->setLastReference(mCurrentReference);
            ++mCurrentReference;
138
            return cacheEntry;
139 140 141
        }
        else
        {
142 143 144 145 146 147
            return qMakePair(WebImagePtr(), -1);
        }
    }
}

WebImagePtr
148
WebImageCache::at(int index) const
149
{
150
    return mWebImages[index];
151 152 153 154 155 156 157
}

void
WebImageCache::downloadFinished(QNetworkReply* reply)
{
    reply->deleteLater();

158 159
    if (reply->error() != QNetworkReply::NoError)
    {
160 161 162
        return;
    }
    QVariant attribute = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
163 164
    if (attribute.isValid())
    {
165 166 167 168
        return;
    }

    WebImagePtr image;
169 170 171 172
    foreach(image, mWebImages)
    {
        if (reply->url().toString() == image->getSourceURL())
        {
173 174 175 176 177 178 179 180
            image->setData(reply->readAll());
            image->setSyncFlag(true);
            image->setState(WebImage::READY);

            return;
        }
    }
}