|
1 /** |
|
2 This file is part of CWRT package ** |
|
3 |
|
4 Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). ** |
|
5 |
|
6 This program is free software: you can redistribute it and/or modify |
|
7 it under the terms of the GNU (Lesser) General Public License as |
|
8 published by the Free Software Foundation, version 2.1 of the License. |
|
9 This program is distributed in the hope that it will be useful, but |
|
10 WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
12 (Lesser) General Public License for more details. You should have |
|
13 received a copy of the GNU (Lesser) General Public License along |
|
14 with this program. If not, see <http://www.gnu.org/licenses/>. |
|
15 */ |
|
16 |
|
17 #include "clientdownload.h" |
|
18 #include "downloadmanager.h" |
|
19 #include "downloadcore.h" |
|
20 #include "downloadstore.h" |
|
21 #include "httpdownloadbackend.h" |
|
22 #include "filestorage.h" |
|
23 #include "dmcommoninternal.h" |
|
24 #include <QFileInfo> |
|
25 #include <QString> |
|
26 #include <QMap> |
|
27 #include <QIODevice> |
|
28 |
|
29 // private implementation |
|
30 class HttpDownloadBackendPrivate |
|
31 { |
|
32 DM_DECLARE_PUBLIC(HttpDownloadBackend); |
|
33 public: |
|
34 HttpDownloadBackendPrivate(); |
|
35 ~HttpDownloadBackendPrivate(); |
|
36 DownloadCore *m_downloadCore; // for network operations |
|
37 DownloadStore *m_storage; // responsible for handling the storage |
|
38 ClientDownload *m_download; // not owned |
|
39 }; |
|
40 |
|
41 HttpDownloadBackendPrivate::HttpDownloadBackendPrivate() |
|
42 { |
|
43 m_downloadCore = 0; |
|
44 m_storage = 0; |
|
45 m_download = 0; |
|
46 } |
|
47 |
|
48 HttpDownloadBackendPrivate::~HttpDownloadBackendPrivate() |
|
49 { |
|
50 if(m_storage) |
|
51 { |
|
52 delete m_storage; |
|
53 m_storage = 0; |
|
54 } |
|
55 } |
|
56 |
|
57 HttpDownloadBackend::HttpDownloadBackend(DownloadCore *dlCore, ClientDownload *dl, DownloadStore* store) |
|
58 :DownloadBackend(dlCore, dl) |
|
59 { |
|
60 DM_INITIALIZE(HttpDownloadBackend); |
|
61 priv->m_downloadCore = dlCore; |
|
62 priv->m_storage = store; |
|
63 priv->m_download = dl; |
|
64 setValue(DownloadInfo::EFinalPath, dl->attributes().value(DlDestPath).toString()); |
|
65 |
|
66 QString fileName; |
|
67 if(dl->isCreatedByDlInfo()) |
|
68 { |
|
69 getValue(DownloadInfo::EFileName, fileName); |
|
70 dl->attributes().insert(DlFileName, fileName); |
|
71 priv->m_storage->open(QIODevice::Append); |
|
72 } |
|
73 else |
|
74 { |
|
75 fileName = dl->attributes().value(DlFileName).toString(); |
|
76 // if filename is not set, take it from url |
|
77 if(fileName.length() == 0) { |
|
78 fileName = priv->m_downloadCore->fileNameFromContentDispositionHeader(); |
|
79 if(fileName.length() == 0) { |
|
80 QUrl url(priv->m_downloadCore->url()); |
|
81 QFileInfo fileUrl(url.path()); |
|
82 fileName = fileUrl.fileName(); |
|
83 } |
|
84 } |
|
85 dl->attributes().insert(DlFileName, fileName); |
|
86 // create the new storage |
|
87 priv->m_storage->createStore(); |
|
88 } |
|
89 setValue(DownloadInfo::EFileName, download()->attributes().value(DlFileName).toString()); |
|
90 } |
|
91 |
|
92 HttpDownloadBackend::~HttpDownloadBackend() |
|
93 { |
|
94 DM_UNINITIALIZE(HttpDownloadBackend); |
|
95 } |
|
96 |
|
97 int HttpDownloadBackend::resume() |
|
98 { |
|
99 DM_PRIVATE(HttpDownloadBackend); |
|
100 // Open the file in append mode as we need to append the received chunks |
|
101 if(downloadState() != DlCancelled) |
|
102 { |
|
103 priv->m_storage->open(QIODevice::Append); |
|
104 } |
|
105 else |
|
106 { |
|
107 QString fileName = download()->attributes().value(DlFileName).toString(); |
|
108 // if filename is not set, take it from url |
|
109 if(fileName.length() == 0) { |
|
110 fileName = priv->m_downloadCore->fileNameFromContentDispositionHeader(); |
|
111 if(fileName.length() == 0) { |
|
112 QUrl url(priv->m_downloadCore->url()); |
|
113 QFileInfo fileUrl(url.path()); |
|
114 fileName = fileUrl.fileName(); |
|
115 } |
|
116 } |
|
117 download()->attributes().insert(DlFileName, fileName); |
|
118 // create the new storage |
|
119 priv->m_storage->createStore(); |
|
120 } |
|
121 setValue(DownloadInfo::EFileName, download()->attributes().value(DlFileName).toString()); |
|
122 return DownloadBackend::resume(); |
|
123 } |
|
124 |
|
125 void HttpDownloadBackend::store(QByteArray data, bool lastChunk) |
|
126 { |
|
127 DM_PRIVATE(HttpDownloadBackend); |
|
128 // write the chunks to the storage |
|
129 priv->m_storage->write(data, lastChunk); |
|
130 } |
|
131 |
|
132 void HttpDownloadBackend::deleteStore() |
|
133 { |
|
134 DM_PRIVATE(HttpDownloadBackend); |
|
135 if(priv->m_storage) |
|
136 { |
|
137 priv->m_storage->deleteStore(); |
|
138 } |
|
139 } |
|
140 |
|
141 qint64 HttpDownloadBackend::storedDataSize() |
|
142 { |
|
143 DM_PRIVATE(HttpDownloadBackend); |
|
144 // size of stored data chunk |
|
145 return priv->m_storage->storedDataSize(); |
|
146 } |
|
147 |
|
148 QVariant HttpDownloadBackend::getAttribute(DownloadAttribute attr) |
|
149 { |
|
150 DM_PRIVATE(HttpDownloadBackend); |
|
151 switch(attr) |
|
152 { |
|
153 case DlFileName: |
|
154 { |
|
155 if(priv->m_download) |
|
156 return priv->m_download->attributes().value(attr); |
|
157 } |
|
158 case DlDestPath: |
|
159 { |
|
160 if(priv->m_download) |
|
161 return priv->m_download->attributes().value(attr); |
|
162 } |
|
163 default: |
|
164 break; |
|
165 } |
|
166 return DownloadBackend::getAttribute(attr); |
|
167 } |
|
168 |
|
169 int HttpDownloadBackend::setAttribute(DownloadAttribute attr, const QVariant& value) |
|
170 { |
|
171 return DownloadBackend::setAttribute(attr, value); |
|
172 } |
|
173 |
|
174 void HttpDownloadBackend::headerReceived() |
|
175 { |
|
176 DM_PRIVATE(HttpDownloadBackend); |
|
177 DlEventAttributeMap* attrMap = NULL; |
|
178 HttpStatusCode status = (HttpStatusCode) priv->m_downloadCore->reply()->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); |
|
179 switch(status) |
|
180 { |
|
181 case HttpOK: // OK |
|
182 case HttpCreated: // Created |
|
183 case HttpAccepted: // Accepted |
|
184 case HttpNonAuthorativeInfo: // Non-Authorative Information |
|
185 case HttpNoContent: // No Content |
|
186 case HttpResetContent: // Reset Conetent |
|
187 { |
|
188 // get the content type and save it in dl info |
|
189 QString contentType = priv->m_downloadCore->contentType(); |
|
190 setValue(DownloadInfo::EContentType, contentType); |
|
191 setDownloadState(DlInprogress); |
|
192 break; |
|
193 } |
|
194 |
|
195 case HttpPartialContent: // Partial Conetent Download |
|
196 { |
|
197 setDownloadState(DlInprogress); |
|
198 break; |
|
199 } |
|
200 |
|
201 case HttpPreconditionFailed: // Precondition Failed |
|
202 { |
|
203 // attrMap will be deleted in destructor of DownloadEventPrivate class |
|
204 attrMap = new DlEventAttributeMap ; |
|
205 attrMap->insert(HeaderReceivedStatusCode, QVariant(status)); |
|
206 break; |
|
207 } |
|
208 |
|
209 default: |
|
210 break; |
|
211 } |
|
212 |
|
213 postEvent(HeaderReceived, attrMap); |
|
214 } |
|
215 |
|
216 |
|
217 |