16
|
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 "filestorage.h"
|
|
18 |
#include "storageutility.h"
|
|
19 |
#include <QFile>
|
|
20 |
#include <QDir>
|
|
21 |
#include <QVariant>
|
|
22 |
#include <qregexp.h>
|
|
23 |
|
|
24 |
class FileStoragePrivate
|
|
25 |
{
|
|
26 |
DM_DECLARE_PUBLIC(FileStorage);
|
|
27 |
public:
|
|
28 |
FileStoragePrivate();
|
|
29 |
~FileStoragePrivate();
|
|
30 |
// client name
|
|
31 |
QString m_clientName;
|
|
32 |
// folder paths
|
|
33 |
QString m_tempPath; // temporary path till dl completes
|
|
34 |
QFile *m_file;
|
|
35 |
ClientDownload* m_download;
|
|
36 |
};
|
|
37 |
|
|
38 |
FileStoragePrivate::FileStoragePrivate()
|
|
39 |
{
|
|
40 |
m_tempPath = "";
|
|
41 |
m_file = 0;
|
|
42 |
m_download = 0;
|
|
43 |
}
|
|
44 |
|
|
45 |
FileStoragePrivate::~FileStoragePrivate()
|
|
46 |
{
|
|
47 |
if(m_file)
|
|
48 |
{
|
|
49 |
// close the file if it is open
|
|
50 |
if(m_file->isOpen())
|
|
51 |
{
|
|
52 |
m_file->close();
|
|
53 |
}
|
|
54 |
delete m_file;
|
|
55 |
m_file = 0;
|
|
56 |
}
|
|
57 |
}
|
|
58 |
|
|
59 |
FileStorage::FileStorage(const QString& clientName, ClientDownload* download)
|
|
60 |
{
|
|
61 |
DM_INITIALIZE(FileStorage);
|
|
62 |
priv->m_clientName = clientName;
|
|
63 |
priv->m_file = new QFile();
|
|
64 |
priv->m_download = download;
|
|
65 |
priv->m_tempPath = StorageUtility::createTemporaryPath(clientName);
|
|
66 |
}
|
|
67 |
|
|
68 |
FileStorage::~FileStorage()
|
|
69 |
{
|
|
70 |
DM_UNINITIALIZE(FileStorage);
|
|
71 |
}
|
|
72 |
|
|
73 |
int FileStorage::createStore()
|
|
74 |
{
|
|
75 |
DM_PRIVATE(FileStorage);
|
|
76 |
|
|
77 |
// validate filename
|
|
78 |
QString validFilename;
|
|
79 |
QString filename = priv->m_download->attributes().value(DlFileName).toString();
|
|
80 |
StorageUtility::validateFileName(filename, validFilename);
|
|
81 |
// generate unique filename
|
|
82 |
QString permPath = priv->m_download->attributes().value(DlDestPath).toString();
|
|
83 |
QString uniqueFilename;
|
|
84 |
StorageUtility::generateUniqueFileName(validFilename, priv->m_tempPath, permPath, uniqueFilename);
|
|
85 |
|
|
86 |
// set the new filename
|
|
87 |
QDir filePath(priv->m_tempPath);
|
|
88 |
priv->m_download->attributes().insert(DlFileName, uniqueFilename);
|
|
89 |
QFileInfo newFileinfo(filePath, uniqueFilename);
|
|
90 |
QString newFilename = newFileinfo.filePath();
|
|
91 |
priv->m_file->setFileName(newFilename);
|
|
92 |
if (!priv->m_file->open(QIODevice::ReadWrite))
|
|
93 |
return -1;
|
|
94 |
else
|
|
95 |
return 0;
|
|
96 |
}
|
|
97 |
|
|
98 |
|
|
99 |
int FileStorage::open(QIODevice::OpenMode mode)
|
|
100 |
{
|
|
101 |
DM_PRIVATE(FileStorage);
|
|
102 |
|
|
103 |
QDir filePath(priv->m_tempPath);
|
|
104 |
QFileInfo fileinfo(filePath, priv->m_download->attributes().value(DlFileName).toString());
|
|
105 |
QString filename = fileinfo.filePath();
|
|
106 |
// if already exists, open the file
|
|
107 |
if(priv->m_file && (QFile::exists(filename)) && !priv->m_file->isOpen())
|
|
108 |
{
|
|
109 |
priv->m_file->setFileName(filename);
|
|
110 |
if (!priv->m_file->open(mode))
|
|
111 |
return -1;
|
|
112 |
else
|
|
113 |
return 0;
|
|
114 |
}
|
|
115 |
return -1;
|
|
116 |
}
|
|
117 |
|
|
118 |
int FileStorage::write(const QByteArray& data,bool lastChunk)
|
|
119 |
{
|
|
120 |
DM_PRIVATE(FileStorage);
|
|
121 |
// write the data chunk
|
|
122 |
int value = priv->m_file->write(data);
|
|
123 |
if(lastChunk)
|
|
124 |
{
|
|
125 |
close();
|
|
126 |
// move the file to permanent destination path
|
|
127 |
moveDlFile();
|
|
128 |
}
|
|
129 |
return value;
|
|
130 |
}
|
|
131 |
|
|
132 |
int FileStorage::close()
|
|
133 |
{
|
|
134 |
DM_PRIVATE(FileStorage);
|
|
135 |
if(priv->m_file && priv->m_file->isOpen())
|
|
136 |
{
|
|
137 |
priv->m_file->close();
|
|
138 |
}
|
|
139 |
return 0;
|
|
140 |
}
|
|
141 |
|
|
142 |
int FileStorage::deleteStore()
|
|
143 |
{
|
|
144 |
DM_PRIVATE(FileStorage);
|
|
145 |
// remove the temporary file
|
|
146 |
QDir tempFilePath(priv->m_tempPath);
|
|
147 |
QFileInfo tempFileinfo(tempFilePath, priv->m_download->attributes().value(DlFileName).toString());
|
|
148 |
QString tempFileName = tempFileinfo.filePath();
|
|
149 |
|
|
150 |
// remove the temporary file
|
|
151 |
if(QFile::exists(tempFileName))
|
|
152 |
{
|
|
153 |
close();
|
|
154 |
QFile::remove(tempFileName);
|
|
155 |
}
|
|
156 |
|
|
157 |
return 0;
|
|
158 |
}
|
|
159 |
|
|
160 |
int FileStorage::storedDataSize()
|
|
161 |
{
|
|
162 |
DM_PRIVATE(FileStorage);
|
|
163 |
QDir tempFilePath(priv->m_tempPath);
|
|
164 |
QFileInfo tempFileinfo(tempFilePath, priv->m_download->attributes().value(DlFileName).toString());
|
|
165 |
QString tempFilename = tempFileinfo.filePath();
|
|
166 |
QFile tempFile(tempFilename);
|
|
167 |
|
|
168 |
if(tempFile.exists())
|
|
169 |
return tempFile.size();
|
|
170 |
else
|
|
171 |
return 0;
|
|
172 |
}
|
|
173 |
|
|
174 |
void FileStorage::moveDlFile()
|
|
175 |
{
|
|
176 |
DM_PRIVATE(FileStorage);
|
|
177 |
QString permPath = priv->m_download->attributes().value(DlDestPath).toString();
|
|
178 |
QString filename = priv->m_download->attributes().value(DlFileName).toString();
|
|
179 |
QDir permDir(permPath);
|
|
180 |
if(!permDir.exists())
|
|
181 |
{
|
|
182 |
QDir tempDir;
|
|
183 |
if(!tempDir.mkpath (permPath))
|
|
184 |
{
|
|
185 |
return;
|
|
186 |
}
|
|
187 |
}
|
|
188 |
|
|
189 |
QDir newFilePath(permPath);
|
|
190 |
QFileInfo newFileinfo(newFilePath, filename);
|
|
191 |
QString newFileName = newFileinfo.filePath();
|
|
192 |
|
|
193 |
// remove the file if already exists
|
|
194 |
if(QFile::exists(newFileName)) {
|
|
195 |
QFile::remove(newFileName);
|
|
196 |
}
|
|
197 |
|
|
198 |
QDir tempFilePath(priv->m_tempPath);
|
|
199 |
QFileInfo tempFileinfo(tempFilePath, filename);
|
|
200 |
QString tempFilename = tempFileinfo.filePath();
|
|
201 |
|
|
202 |
QFile::copy(tempFilename, newFileName);
|
|
203 |
|
|
204 |
// remove the temporary file
|
|
205 |
QFile::remove(tempFilename);
|
|
206 |
return;
|
|
207 |
}
|