-
Notifications
You must be signed in to change notification settings - Fork 510
Expand file tree
/
Copy pathUBExportDocumentSetAdaptor.cpp
More file actions
213 lines (169 loc) · 6.43 KB
/
Copy pathUBExportDocumentSetAdaptor.cpp
File metadata and controls
213 lines (169 loc) · 6.43 KB
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
/*
* Copyright (C) 2015-2022 Département de l'Instruction Publique (DIP-SEM)
*
* Copyright (C) 2013 Open Education Foundation
*
* Copyright (C) 2010-2013 Groupement d'Intérêt Public pour
* l'Education Numérique en Afrique (GIP ENA)
*
* This file is part of OpenBoard.
*
* OpenBoard 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, version 3 of the License,
* with a specific linking exception for the OpenSSL project's
* "OpenSSL" library (or with modified versions of it that use the
* same license as the "OpenSSL" library).
*
* OpenBoard 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 OpenBoard. If not, see <http://www.gnu.org/licenses/>.
*/
#include "UBExportDocumentSetAdaptor.h"
#ifdef Q_OS_OSX
#include <quazip.h>
#include <quazipfile.h>
#else
#include "quazip.h"
#include "quazipfile.h"
#endif
#include "adaptors/UBPageMapper.h"
#include "core/memcheck.h"
#include "core/UBApplication.h"
#include "core/UBDocumentManager.h"
#include "core/UBPersistenceManager.h"
#include "core/UBSettings.h"
#include "document/UBDocumentProxy.h"
#include "document/UBDocumentController.h"
#include "document/UBDocumentToc.h"
#include "frameworks/UBPlatformUtils.h"
#include "UBExportDocument.h"
UBExportDocumentSetAdaptor::UBExportDocumentSetAdaptor(QObject *parent)
: UBExportAdaptor(parent)
{
}
UBExportDocumentSetAdaptor::~UBExportDocumentSetAdaptor()
{
// NOOP
}
void UBExportDocumentSetAdaptor::persist(std::shared_ptr<UBDocumentProxy> pDocumentProxy)
{
QModelIndex treeViewParentIndex;
UBPersistenceManager *persistenceManager = UBPersistenceManager::persistenceManager();
UBDocumentTreeModel *treeModel = persistenceManager->mDocumentTreeStructureModel;
QString filename;
if (pDocumentProxy) {
treeViewParentIndex = treeModel->indexForProxy(pDocumentProxy);
if (!treeViewParentIndex.isValid()) {
qDebug() << "failed to export";
UBApplication::showMessage(tr("Failed to export..."));
return;
}
filename = askForFileName(pDocumentProxy, tr("Export as UBX File"));
} else {
treeViewParentIndex = UBApplication::documentController->firstSelectedTreeIndex();
if (!treeViewParentIndex.isValid()) {
qDebug() << "failed to export";
UBApplication::showMessage(tr("Failed to export..."));
return;
}
UBDocumentTreeNode* node = treeModel->nodeFromIndex(treeViewParentIndex);
std::shared_ptr<UBDocumentProxy> proxy = std::make_shared<UBDocumentProxy>();
proxy->setMetaData(UBSettings::documentName, node->displayName());
filename = askForFileName(proxy, tr("Export as UBX File"));
}
if (filename.length() > 0)
{
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
if (mIsVerbose)
UBApplication::showMessage(tr("Exporting document..."));
if (persistData(treeViewParentIndex, filename)) {
if (mIsVerbose) {
UBApplication::showMessage(tr("Export successful."));
}
} else {
if (mIsVerbose) {
UBApplication::showMessage(tr("Export failed."));
}
}
QApplication::restoreOverrideCursor();
}
}
bool UBExportDocumentSetAdaptor::persistData(const QModelIndex &pRootIndex, QString filename)
{
UBPersistenceManager *persistenceManager = UBPersistenceManager::persistenceManager();
UBDocumentTreeModel *treeModel = persistenceManager->mDocumentTreeStructureModel;
QModelIndex index = pRootIndex;
if (!index.isValid()) {
return false;
}
QuaZip zip(filename);
zip.setFileNameCodec("UTF-8");
if(!zip.open(QuaZip::mdCreate))
{
qWarning("Export failed. Cause: zip.open(): %d", zip.getZipError());
return false;
}
if (!addDocumentToZip(pRootIndex, treeModel, zip)) {
zip.close();
return false;
}
zip.close();
UBPlatformUtils::setFileType(filename, 0x5542647A /* UBdz */);
return true;
}
QString UBExportDocumentSetAdaptor::exportExtention()
{
return QString(".ubx");
}
QString UBExportDocumentSetAdaptor::exportName()
{
return tr("Export to OpenBoard UBX Format");
}
bool UBExportDocumentSetAdaptor::addDocumentToZip(const QModelIndex &pIndex, UBDocumentTreeModel *model, QuaZip &zip)
{
static int i = 0;
i++;
QModelIndex parentIndex = pIndex;
if (!parentIndex.isValid()) {
return false;
}
std::shared_ptr<UBDocumentProxy>pDocumentProxy = model->proxyForIndex(parentIndex);
if (pDocumentProxy) {
QString documentPath(pDocumentProxy->persistencePath());
QDir documentDir = QDir(pDocumentProxy->persistencePath());
// try to load a TOC for mapping and check version number
UBDocumentToc toc{pDocumentProxy->persistencePath()};
std::unique_ptr<UBPageMapper> mapper{nullptr};
const auto version = QVersionNumber::fromString(pDocumentProxy->metaData(UBSettings::documentVersion).toString());
if (toc.load() && version >= QVersionNumber::fromString(UBSettings::currentFileVersion))
{
mapper = std::unique_ptr<UBPageMapper>{new UBPageMapper{pDocumentProxy->persistencePath(), &toc}};
}
QuaZipFile zipFile(&zip);
UBFileSystemUtils::compressDirInZip(documentDir, QFileInfo(documentPath).fileName() + "/", &zipFile, false, mapper.get());
if(zip.getZipError() != 0)
{
qWarning("Export failed. Cause: zip.close(): %d", zip.getZipError());
}
}
for (int i = 0; i < model->rowCount(parentIndex); ++i) {
QModelIndex curIndex = model->index(i, 0, parentIndex);
if (!addDocumentToZip(curIndex, model, zip)) {
return false;
}
}
return true;
}
bool UBExportDocumentSetAdaptor::associatedActionactionAvailableFor(const QModelIndex &selectedIndex)
{
const UBDocumentTreeModel *docModel = qobject_cast<const UBDocumentTreeModel*>(selectedIndex.model());
if (!selectedIndex.isValid() || docModel->isDocument(selectedIndex)) {
return false;
}
return true;
}