Use of gcsDebug instead of qDebug
[meego-garage:garage-client-services.git] / src / image.cpp
1 #include "image.h"
2 #include <QFile>
3 #include <QDir>
4 #include <QStringList>
5 #include <QtDebug>
6 #include <QNetworkRequest>
7 #include <QNetworkReply>
8
9 #include "garageclientservices.h"
10 #include "errorhandler.h"
11 #include "catalog.h"
12 #include "utils.h"
13
14 using namespace MeeGoGarage;
15
16 QString Image::convertUrltoLocalPath(const QString& url)
17 {
18     QStringList list = url.split("/");
19     QString path = m_localCacheFolderPath + list.last();
20
21     gcsDebug() << "Image url: " << url << "path: " << path;
22
23     return path;
24 }
25
26 QString Image::setLocalCacheFolderPath(const QString& cachePath)
27 {
28     m_localCacheFolderPath = cachePath;
29     return m_localCacheFolderPath;
30 }
31
32 Image::Image(Catalog* cat, const QString& url, const QString& cacheFolder, QObject *parent, Image::ImageType type) :
33         QObject(parent),
34         m_catalog(cat)
35 {
36     m_localImageIsReady = FALSE;
37     m_imageUrl = url;
38     m_localCacheFolderPath = cacheFolder;
39     m_imageLocalPath = convertUrltoLocalPath(url);
40     m_iconType = type;
41
42     QFileInfo fileInfo(m_imageLocalPath);
43     if (fileInfo.fileName().isEmpty()) {
44         qWarning() << "Try to create image with a non-File path:" << m_imageLocalPath;
45         return;
46     }
47
48     QFile file(m_imageLocalPath);
49     if (file.exists() && file.size() > 0) {
50         m_localImageIsReady = TRUE;
51     }
52 }
53
54 bool Image::requestContent()
55 {
56     if (m_localImageIsReady) {
57         return false;
58     }
59
60     QFileInfo fileInfo(m_imageLocalPath);
61     if (fileInfo.fileName().isEmpty()) {
62         //qWarning() << "Try to create image with a non-File path:" << m_imageLocalPath;
63         return false;
64     }
65
66     QFile file(m_imageLocalPath);
67     if (file.exists() && file.size() > 0) {
68         m_localImageIsReady = true;
69         return false;
70     }
71
72     m_localImageIsReady = false;
73
74     GarageClientServices *gcs = GarageClientServices::instance();
75     QNetworkAccessManager *networkAccessManager = m_catalog->networkAccessManager();
76     QNetworkReply *networkReply = networkAccessManager->get(QNetworkRequest(m_imageUrl));
77
78     connect(networkReply, SIGNAL(finished()), SLOT(onImageDownloaded()));
79
80     QObject::connect(networkReply, SIGNAL(error(QNetworkReply::NetworkError)),
81             gcs->errorHandler(), SLOT(onNetworkReplyError(QNetworkReply::NetworkError)));
82
83 //        gcsDebug() << "Downloading image: " << m_imageUrl << " ......";
84     return true;
85 }
86
87 const QString& Image::imageUrl() const
88 {
89     return m_imageUrl;
90 }
91
92 const QString& Image::imageLocalPath() const
93 {
94     return m_imageLocalPath;
95 }
96
97 bool Image::isLocalImageReady() const
98 {
99     return m_localImageIsReady;
100 }
101
102 void Image::onImageDownloaded()
103 {
104     QNetworkReply *networkReply = qobject_cast<QNetworkReply*>(sender());
105     networkReply->deleteLater();
106
107     if (networkReply->error() != QNetworkReply::NoError)
108         return;
109
110     QDir dir(m_localCacheFolderPath);
111     if (!dir.exists()) {
112         if (!dir.mkpath(m_localCacheFolderPath)) {
113             gcsDebug() << "Create local cache folder " << m_localCacheFolderPath << " failed: ";
114             return;
115         }
116     }
117
118     QFile file(m_imageLocalPath);
119     if (!file.open(QFile::WriteOnly)) {
120         gcsDebug() << "Create local cache file " << m_imageLocalPath << " failed: " << file.errorString();
121         return;
122     }
123
124     file.write(networkReply->readAll());
125     file.close();
126
127     m_localImageIsReady = TRUE;
128
129     networkReply->deleteLater();
130
131 //    gcsDebug() << "Downloaded image: " << m_imageUrl << "to cache:" << m_imageLocalPath;
132
133     emit localImageAvailable(this);
134 }
135
136 Image::ImageType Image::iconType() const
137 {
138     return m_iconType;
139 }