Don't reuse file names
[harmattan/cameraplus] / src / qmlfileengine.cpp
1 // -*- c++ -*-
2
3 /*!
4  * This file is part of CameraPlus.
5  *
6  * Copyright (C) 2012-2013 Mohammed Sameer <msameer@foolab.org>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  */
22
23 #include "qmlfileengine.h"
24 #include <QResource>
25 #include <QDateTime>
26
27 // TODO: support mmap extension
28
29 QmlFileEngine::QmlFileEngine(const QString& fileName) :
30   QFSFileEngine(fileName),
31   m_res(new QResource(fileName)),
32   m_off(0) {
33
34 }
35
36 void QmlFileEngine::setFileName(const QString& file) {
37   m_res->setFileName(file);
38 }
39
40 bool QmlFileEngine::open(QIODevice::OpenMode flags) {
41   if (!m_res->isValid() || m_res->fileName().isEmpty()) {
42     return false;
43   }
44
45   if (flags & QIODevice::WriteOnly) {
46     return false;
47   }
48
49   m_off = 0;
50
51   if (m_res->isCompressed()) {
52     m_data = qUncompress(m_res->data(), m_res->size());
53   }
54   else {
55     m_data = QByteArray::fromRawData((const char *)m_res->data(), m_res->size());
56   }
57
58   m_data.replace("import QtQuick 2.0", "import QtQuick 1.1");
59
60   return true;
61 }
62
63 bool QmlFileEngine::close() {
64   m_data.clear();
65   m_off = 0;
66
67   return true;
68 }
69
70 bool QmlFileEngine::flush() {
71   return true;
72 }
73
74 qint64 QmlFileEngine::size() const {
75   return m_data.size();
76 }
77
78 qint64 QmlFileEngine::pos() const {
79   return m_off;
80 }
81
82 bool QmlFileEngine::atEnd() const {
83   return m_off == m_data.size();
84 }
85
86 bool QmlFileEngine::seek(qint64 pos) {
87   if (pos > m_data.size()) {
88     return false;
89   }
90
91   return true;
92 }
93
94 qint64 QmlFileEngine::read(char *data, qint64 len) {
95   if (len > m_data.size() - m_off) {
96     len = m_data.size() - m_off;
97   }
98
99   if (len <= 0) {
100     return 0;
101   }
102
103   memcpy(data, m_data.data() + m_off, len);
104
105   m_off += len;
106
107   return len;
108 }
109
110 qint64 QmlFileEngine::write(const char *data, qint64 len) {
111   Q_UNUSED(data);
112   Q_UNUSED(len);
113
114   return false;
115 }
116
117 bool QmlFileEngine::remove() {
118   return false;
119 }
120
121 bool QmlFileEngine::copy(const QString& newName) {
122   Q_UNUSED(newName);
123
124   return false;
125 }
126
127 bool QmlFileEngine::rename(const QString& newName) {
128   Q_UNUSED(newName);
129
130   return false;
131 }
132
133 bool QmlFileEngine::link(const QString& newName) {
134   Q_UNUSED(newName);
135
136   return false;
137 }
138
139 bool QmlFileEngine::isSequential() const {
140   return false;
141 }
142
143 bool QmlFileEngine::isRelativePath() const {
144   return false;
145 }
146
147 bool QmlFileEngine::mkdir(const QString& dirName, bool createParentDirectories) const {
148   Q_UNUSED(dirName);
149   Q_UNUSED(createParentDirectories);
150
151   return false;
152 }
153
154 bool QmlFileEngine::rmdir(const QString& dirName, bool recurseParentDirectories) const {
155   Q_UNUSED(dirName);
156   Q_UNUSED(recurseParentDirectories);
157
158   return false;
159 }
160
161 bool QmlFileEngine::setSize(qint64 size) {
162   Q_UNUSED(size);
163
164   return false;
165 }
166
167 QStringList QmlFileEngine::entryList(QDir::Filters filters, const QStringList& filterNames) const {
168   Q_UNUSED(filters);
169   Q_UNUSED(filterNames);
170
171   return QStringList();
172 }
173
174 bool QmlFileEngine::caseSensitive() const {
175   return true;
176 }
177
178 QFSFileEngine::FileFlags QmlFileEngine::fileFlags(FileFlags type) const {
179   QFSFileEngine::FileFlags flags = 0;
180
181   if (!m_res->isValid()) {
182     return flags;
183   }
184
185   if (type & TypesMask) {
186     flags |= FileType;
187   }
188
189   if (type & FlagsMask) {
190     flags |= ExistsFlag;
191   }
192
193   return flags;
194 }
195
196 bool QmlFileEngine::setPermissions(uint perms) {
197   Q_UNUSED(perms);
198
199   return false;
200 }
201
202 QString QmlFileEngine::fileName(QAbstractFileEngine::FileName file) const {
203   switch (file) {
204   case QAbstractFileEngine::AbsoluteName:
205     return m_res->fileName();
206   default:
207     break;
208   }
209
210   // TODO:
211   return QString();
212 }
213
214 uint QmlFileEngine::ownerId(FileOwner owner) const {
215   Q_UNUSED(owner);
216
217   return -2;
218 }
219
220 QString QmlFileEngine::owner(FileOwner owner) const {
221   Q_UNUSED(owner);
222
223   return QString();
224 }
225
226 QDateTime QmlFileEngine::fileTime(FileTime time) const {
227   Q_UNUSED(time);
228
229   return QDateTime();
230 }