1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "UBImportDocument.h"
#include "UBMetadataDcSubsetAdaptor.h"
#include "document/UBDocumentProxy.h"
#include "frameworks/UBFileSystemUtils.h"
#include "core/UBApplication.h"
#include "core/UBSettings.h"
#include "core/UBPersistenceManager.h"
#include "quazip.h"
#include "quazipfile.h"
#include "quazipfileinfo.h"
#include "core/memcheck.h"
UBImportDocument::UBImportDocument(QObject *parent)
:UBImportAdaptor(parent)
{
// NOOP
}
UBImportDocument::~UBImportDocument()
{
// NOOP
}
QStringList UBImportDocument::supportedExtentions()
{
return QStringList("ubz");
}
QString UBImportDocument::importFileFilter()
{
return tr("Sankore 3.1 (*.ubz)");
}
QString UBImportDocument::expandFileToDir(const QFile& pZipFile, const QString& pDir)
{
QDir rootDir(pDir);
QuaZip zip(pZipFile.fileName());
if(!zip.open(QuaZip::mdUnzip))
{
qWarning() << "Import failed. Cause zip.open(): " << zip.getZipError();
return "";
}
zip.setFileNameCodec("UTF-8");
QuaZipFileInfo info;
QuaZipFile file(&zip);
// TODO UB 4.x implement a mechanism that can replace an existing
// document based on the UID of the document.
bool createNewDocument = true;
QString documentRootFolder;
// first we search the metadata.rdf to check the document properties
for(bool more = zip.goToFirstFile(); more; more = zip.goToNextFile())
{
if(!zip.getCurrentFileInfo(&info))
{
qWarning() << "Import failed. Cause: getCurrentFileInfo(): " << zip.getZipError();
return "";
}
QFileInfo currentFileInfo(pDir + "/" + file.getActualFileName());
}
if (createNewDocument)
documentRootFolder = UBPersistenceManager::persistenceManager()->generateUniqueDocumentPath();
QFile out;
char c;
for(bool more=zip.goToFirstFile(); more; more=zip.goToNextFile())
{
if(!zip.getCurrentFileInfo(&info))
{
//TOD UB 4.3 O display error to user or use crash reporter
qWarning() << "Import failed. Cause: getCurrentFileInfo(): " << zip.getZipError();
return "";
}
if(!file.open(QIODevice::ReadOnly))
{
qWarning() << "Import failed. Cause: file.open(): " << zip.getZipError();
return "";
}
if(file.getZipError()!= UNZ_OK)
{
qWarning() << "Import failed. Cause: file.getFileName(): " << zip.getZipError();
return "";
}
QString newFileName = documentRootFolder + "/" + file.getActualFileName();
QFileInfo newFileInfo(newFileName);
rootDir.mkpath(newFileInfo.absolutePath());
out.setFileName(newFileName);
out.open(QIODevice::WriteOnly);
// Slow like hell (on GNU/Linux at least), but it is not my fault.
// Not ZIP/UNZIP package's fault either.
// The slowest thing here is out.putChar(c).
QByteArray outFileContent = file.readAll();
if (out.write(outFileContent) == -1)
{
qWarning() << "Import failed. Cause: Unable to write file";
out.close();
return "";
}
while(file.getChar(&c))
out.putChar(c);
out.close();
if(file.getZipError()!=UNZ_OK)
{
qWarning() << "Import failed. Cause: " << zip.getZipError();
return "";
}
if(!file.atEnd())
{
qWarning() << "Import failed. Cause: read all but not EOF";
return "";
}
file.close();
if(file.getZipError()!=UNZ_OK)
{
qWarning() << "Import failed. Cause: file.close(): " << file.getZipError();
return "";
}
}
zip.close();
if(zip.getZipError()!=UNZ_OK)
{
qWarning() << "Import failed. Cause: zip.close(): " << zip.getZipError();
return "";
}
return documentRootFolder;
}
UBDocumentProxy* UBImportDocument::importFile(const QFile& pFile, const QString& pGroup)
{
Q_UNUSED(pGroup); // group is defined in the imported file
QFileInfo fi(pFile);
UBApplication::showMessage(tr("Importing file %1...").arg(fi.baseName()), true);
// first unzip the file to the correct place
QString path = UBSettings::settings()->uniboardDocumentDirectory();
QString documentRootFolder = expandFileToDir(pFile, path);
if(!documentRootFolder.length()){
UBApplication::showMessage(tr("Import of file %1 failed.").arg(fi.baseName()));
return 0;
}
else{
UBDocumentProxy* newDocument = UBPersistenceManager::persistenceManager()->createDocumentFromDir(documentRootFolder);
UBApplication::showMessage(tr("Import successful."));
return newDocument;
}
}
bool UBImportDocument::addFileToDocument(UBDocumentProxy* pDocument, const QFile& pFile)
{
QFileInfo fi(pFile);
UBApplication::showMessage(tr("Importing file %1...").arg(fi.baseName()), true);
QString path = UBFileSystemUtils::createTempDir();
QString documentRootFolder = expandFileToDir(pFile, path);
UBPersistenceManager::persistenceManager()->addDirectoryContentToDocument(documentRootFolder, pDocument);
UBFileSystemUtils::deleteDir(path);
UBApplication::showMessage(tr("Import successful."));
return true;
}