|
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 "drmstorage.h" |
|
18 #include "storageutility.h" |
|
19 #include "dmcommoninternal.h" |
|
20 #include <QFile> |
|
21 #include <QDir> |
|
22 #include <QVariant> |
|
23 #include <qregexp.h> |
|
24 #include <f32file.h> |
|
25 |
|
26 |
|
27 DrmStorage::DrmStorage(const QString& clientName, ClientDownload* download) |
|
28 { |
|
29 m_file = 0; |
|
30 m_clientName = clientName; |
|
31 m_download = download; |
|
32 // create temporary path |
|
33 m_tempPath = StorageUtility::createTemporaryPath(clientName); |
|
34 } |
|
35 |
|
36 DrmStorage::~DrmStorage() |
|
37 { |
|
38 if (m_file) { |
|
39 close(); |
|
40 delete m_file; |
|
41 m_file = 0; |
|
42 } |
|
43 } |
|
44 |
|
45 int DrmStorage::createStore() |
|
46 { |
|
47 // validate filename |
|
48 QString validFilename; |
|
49 QString filename = m_download->attributes().value(DlFileName).toString(); |
|
50 StorageUtility::validateFileName(filename, validFilename); |
|
51 // generate unique filename |
|
52 QString permPath = m_download->attributes().value(DlDestPath).toString(); |
|
53 QString uniqueFilename; |
|
54 StorageUtility::generateUniqueFileName(validFilename, m_tempPath, permPath, uniqueFilename); |
|
55 |
|
56 // set the new filename |
|
57 m_download->attributes().insert(DlFileName, uniqueFilename); |
|
58 |
|
59 // convert to symbian descriptors |
|
60 QString path = m_tempPath; |
|
61 path.replace(QString("/"), QString("\\")); |
|
62 TPtrC ptrPath = SymbianStringToQtString(path); |
|
63 TPtrC ptrFileName = SymbianStringToQtString(uniqueFilename); |
|
64 _LIT8(KOmaDrm1DrmMessageContentType, OMA_DRM_MESSAGE_CONTENT_TYPE); |
|
65 |
|
66 // create supplier and get importfile which will actually write to the file |
|
67 QT_TRAP_THROWING(ContentAccess::CSupplier* supplier = ContentAccess::CSupplier::NewLC(); |
|
68 ContentAccess::CMetaDataArray* metaData = ContentAccess::CMetaDataArray::NewLC(); |
|
69 supplier->SetOutputDirectoryL(ptrPath); |
|
70 m_file = supplier->ImportFileL(KOmaDrm1DrmMessageContentType(), *metaData, ptrFileName)); |
|
71 |
|
72 // perform cleanup |
|
73 CleanupStack::PopAndDestroy(2); // metaData, supplier |
|
74 return 0; |
|
75 } |
|
76 |
|
77 |
|
78 int DrmStorage::open(QIODevice::OpenMode /*mode*/) |
|
79 { |
|
80 // we cannot open an existing file, so we delete the |
|
81 // store and create a new one. Download will start/resume |
|
82 // from beginning |
|
83 deleteStore(); |
|
84 if (!m_file) |
|
85 createStore(); |
|
86 return 0; |
|
87 } |
|
88 |
|
89 int DrmStorage::write(const QByteArray& data, bool lastChunk) |
|
90 { |
|
91 if (!m_file) |
|
92 return -1; |
|
93 |
|
94 // convert the data chunk to 8 bit descriptor |
|
95 QString strChunk(data); |
|
96 TPtrC ptrChunk = SymbianStringToQtString(strChunk); |
|
97 HBufC8* buf8 = q_check_ptr(HBufC8::New(ptrChunk.Length())); |
|
98 buf8->Des().Copy(ptrChunk); |
|
99 TPtrC8 ptrData8(*buf8); |
|
100 |
|
101 if (m_file) { |
|
102 // write the data chunk |
|
103 m_file->WriteData(ptrData8); |
|
104 if (lastChunk) { |
|
105 // close the file |
|
106 close(); |
|
107 // move the file to permanent destination path |
|
108 // note that moveDlFile uses m_file |
|
109 moveDlFile(); |
|
110 // clean up memory |
|
111 delete m_file; |
|
112 m_file = 0; |
|
113 } |
|
114 } |
|
115 |
|
116 delete buf8; |
|
117 buf8 = NULL; |
|
118 return 0; |
|
119 } |
|
120 |
|
121 int DrmStorage::close() |
|
122 { |
|
123 if (m_file) { |
|
124 // close storage and get filename |
|
125 m_file->WriteDataComplete(); |
|
126 } |
|
127 return 0; |
|
128 } |
|
129 |
|
130 int DrmStorage::deleteStore() |
|
131 { |
|
132 // create filename (with path) |
|
133 QDir tempFilePath(m_tempPath); |
|
134 QFileInfo tempFileInfo(tempFilePath, m_download->attributes().value(DlFileName).toString()); |
|
135 QString tempFileName = tempFileInfo.filePath(); |
|
136 |
|
137 // remove the temporary file |
|
138 if (QFile::exists(tempFileName)) { |
|
139 // close the store |
|
140 close(); |
|
141 if (m_file) { |
|
142 delete m_file; |
|
143 m_file = 0; |
|
144 } |
|
145 // remove file |
|
146 QFile::remove(tempFileName); |
|
147 } |
|
148 |
|
149 return 0; |
|
150 } |
|
151 |
|
152 int DrmStorage::storedDataSize() |
|
153 { |
|
154 // build temporary path with filename |
|
155 QDir tempFilePath(m_tempPath); |
|
156 QFileInfo tempFileinfo(tempFilePath, m_download->attributes().value(DlFileName).toString()); |
|
157 QString tempFilename = tempFileinfo.filePath(); |
|
158 QFile tempFile(tempFilename); |
|
159 |
|
160 // return file size if file exists |
|
161 if(tempFile.exists()) |
|
162 return tempFile.size(); |
|
163 else |
|
164 return 0; |
|
165 } |
|
166 |
|
167 void DrmStorage::moveDlFile() |
|
168 { |
|
169 // source file |
|
170 QString fileName = m_download->attributes().value(DlFileName).toString(); |
|
171 QFileInfo srcFileInfo(m_tempPath, fileName); |
|
172 QString srcFile = srcFileInfo.filePath(); |
|
173 |
|
174 // if source file does not exist, return |
|
175 if (!QFile::exists(srcFile)) |
|
176 return; |
|
177 |
|
178 // create the permanent directory |
|
179 QString permPath = m_download->attributes().value(DlDestPath).toString(); |
|
180 QDir permDir(permPath); |
|
181 if(!permDir.exists()) { |
|
182 if(!permDir.mkpath(permPath)) |
|
183 return; |
|
184 } |
|
185 |
|
186 if (m_file) { |
|
187 // name of the file to which |
|
188 TPtrC outputFileName(KNullDesC); |
|
189 QT_TRAP_THROWING(outputFileName.Set(m_file->OutputFileL(0).FileName())); |
|
190 // convert filename to Qt equivalent |
|
191 srcFile = SymbianStringToQtString(outputFileName); |
|
192 // check if file exists and update filename attribute |
|
193 if (!QFile::exists(srcFile)) |
|
194 return; |
|
195 |
|
196 // name without extension |
|
197 TParse p; |
|
198 p.SetNoWild(outputFileName, NULL, NULL); |
|
199 TPtrC namePtr = p.Name(); |
|
200 // suggested extension |
|
201 TBuf<4> extension; |
|
202 m_file->GetSuggestedOutputFileExtension(extension); |
|
203 |
|
204 // build filename with new extension and convert to Qt equivalent |
|
205 HBufC* name = q_check_ptr(HBufC::New(namePtr.Length() + extension.Length())); |
|
206 TPtr newNamePtr(name->Des()); |
|
207 newNamePtr.Format(_L("%S%S"), &namePtr, &extension); |
|
208 QString newFileName = SymbianStringToQtString(newNamePtr); |
|
209 delete name; |
|
210 name = NULL; |
|
211 |
|
212 // extension has changed, generate unique filename |
|
213 StorageUtility::generateUniqueFileName(newFileName, m_tempPath, permPath, fileName); |
|
214 } |
|
215 |
|
216 // remove the file if it already exists |
|
217 QFileInfo fileInfo(permPath, fileName); |
|
218 QString destFile = fileInfo.filePath(); |
|
219 if (QFile::exists(destFile)) |
|
220 QFile::remove(destFile); |
|
221 |
|
222 // Copy the file to destination and remove old file |
|
223 QFile::copy(srcFile, destFile); |
|
224 QFile::remove(srcFile); |
|
225 |
|
226 // set the new filename |
|
227 m_download->attributes().insert(DlFileName, fileName); |
|
228 } |
|
229 |
|
230 QString DrmStorage::SymbianStringToQtString(TDesC& symbianString) |
|
231 { |
|
232 // convert symbian string to Qt string |
|
233 return QString::fromUtf16(symbianString.Ptr(), symbianString.Length()); |
|
234 } |
|
235 |
|
236 TPtrC DrmStorage::SymbianStringToQtString(QString& qtString) |
|
237 { |
|
238 // convert QString string to symbian string |
|
239 return reinterpret_cast<const TText*> (qtString.constData()); |
|
240 } |