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
/*
* Copyright (C) 2015-2018 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"
#include "UBExportDocument.h"
#include "frameworks/UBPlatformUtils.h"
#include "core/UBDocumentManager.h"
#include "core/UBApplication.h"
#include "document/UBDocumentProxy.h"
#include "document/UBDocumentController.h"
#include "globals/UBGlobals.h"
#include "core/UBPersistenceManager.h"
#include "core/UBForeignObjectsHandler.h"
THIRD_PARTY_WARNINGS_DISABLE
#include "quazip.h"
#include "quazipfile.h"
THIRD_PARTY_WARNINGS_ENABLE
#include "core/memcheck.h"
UBExportDocumentSetAdaptor::UBExportDocumentSetAdaptor(QObject *parent)
: UBExportAdaptor(parent)
{
}
UBExportDocumentSetAdaptor::~UBExportDocumentSetAdaptor()
{
// NOOP
}
void UBExportDocumentSetAdaptor::persist(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);
UBDocumentProxy proxy;
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;
}
UBDocumentProxy *pDocumentProxy = model->proxyForIndex(parentIndex);
if (pDocumentProxy) {
// Q_ASSERT(QFileInfo(pDocumentProxy->persistencePath()).exists());
// UBForeighnObjectsHandler cleaner;
// cleaner.cure(pDocumentProxy->persistencePath());
//UniboardSankoreTransition document;
QString documentPath(pDocumentProxy->persistencePath());
//document.checkDocumentDirectory(documentPath);
QDir documentDir = QDir(pDocumentProxy->persistencePath());
QuaZipFile zipFile(&zip);
UBFileSystemUtils::compressDirInZip(documentDir, QFileInfo(documentPath).fileName() + "/", &zipFile, false);
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;
}