Merge branch 'map' of https://vcs.maemo.org/git/situare into map
[situare] / src / map / mapfetcher.h
index bf1418f..1ecd1c2 100644 (file)
@@ -26,8 +26,6 @@
 #include <QtCore>
 #include <QNetworkAccessManager>
 
-#include "mapfetcher.h"
-
 class QNetworkReply;
 class QUrl;
 
@@ -40,6 +38,16 @@ class MapFetcher : public QObject
 {
     Q_OBJECT
 
+    /**
+    * @brief Struct for download requests
+    *
+    * @typedef Request
+    */
+    typedef struct _Request {
+        bool cacheChecked; ///< Is this request already checked from the cache
+        QUrl url; ///< URL
+    } Request;
+
 public:
     /**
     * @brief Constructor for MapFetcher.
@@ -52,6 +60,14 @@ public:
 /*******************************************************************************
  * CLASS SPECIFIC MEMBER FUNCTIONS AND SLOTS
  ******************************************************************************/
+public:
+    /**
+    * @brief Set size of the download queue
+    *
+    * @param size New size
+    */
+    void setDownloadQueueSize(int size);
+
 public slots:
     /**
     * @brief Enqueue fetching of map image
@@ -64,6 +80,13 @@ public slots:
     void enqueueFetchMapImage(QUrl url);
 
 private:
+
+    /**
+    * @brief Limit pending requests list size to m_pendingRequestsSize
+    *
+    */
+    void limitPendingRequestsListSize();
+
     /**
     * @brief Loads image from cache if it's available and emits imageReveived
     * signal with url and image. If image is in cache returns true, false
@@ -74,6 +97,18 @@ private:
     */
     bool loadImageFromCache(const QUrl &url);
 
+    /**
+    * @brief Find first item based on criteria if the request is already checked from the cache
+    *
+    * If cacheChecked is true, then returns index of the first request which is already
+    * checked from the cache. If cacheChecked is false then returns first item which
+    * isn't checked from the cache. Returns -1 if the item is not found.
+    *
+    * @param cacheChecked Search criteria
+    * @return Index of the first matching request, or -1 if not found.
+    */
+    int newestRequestIndex(bool cacheChecked);
+
 private slots:
 
     /**
@@ -86,11 +121,14 @@ private slots:
     void downloadFinished(QNetworkReply *reply);
 
     /**
-    * @brief Fetch next map image.
+    * @brief Check next request if it is found from cache
     *
-    * Next queued request is taken from the queue and fetching is started
+    * Next queued request, which is not already checked against cache, is taken
+    * from the queue and checked against cache. If not found from cache, then
+    * cache checked flag is set. New download is started if there aren't too
+    * many simultaneus downloads already running.
     */
-    void fetchMapImage();
+    void checkNextRequestFromCache();
 
     /**
     * @brief This slot is called when next download is started. Takes url
@@ -124,13 +162,12 @@ signals:
  ******************************************************************************/
 private:
     static const int MAX_PARALLEL_DOWNLOADS = 2; ///< Max simultaneous parallel downloads
-    static const int DOWNLOAD_QUEUE_SIZE = 50; ///< Max downloads waiting in queue
 
     QList<QNetworkReply*> m_currentDownloads; ///< List of current downloads
-    QQueue<QUrl> m_downloadQueue;             ///< Queue of pending requests
-    QList<QUrl> m_fetchMapImagesList; ///< "Queue" for map image fetching requests
-    bool m_isFetchMapImagesTimerRunning; ///< is the singleshot timer already running
-    QNetworkAccessManager *m_manager;       ///< Network access manager
+    int m_pendingRequestsSize; ///< Max number of pending requests
+    bool m_fetchMapImagesTimerRunning; ///< is the singleshot timer already running
+    QNetworkAccessManager *m_manager; ///< Network access manager
+    QList<Request> m_pendingRequests; ///< List of map image fetching requests
 };
 
 #endif