This repository has been archived by the owner on Aug 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
UBUpgradeToOefDelegate.cpp
176 lines (146 loc) · 5.98 KB
/
UBUpgradeToOefDelegate.cpp
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
/*
* Copyright (C) 2013-2014 Open Education Foundation
*
*
* 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 "UBUpgradeToOefDelegate.h"
#include <QDir>
#include <QDebug>
#include <QCoreApplication>
#include <QStack>
#include "UBDirectories.h"
SizeCalculator::SizeCalculator()
{
}
SizeCalculator::~SizeCalculator()
{
}
void SizeCalculator::process()
{
qint64 totalSize = 0;
for(int i = 0; i < MAX_POSSIBLES_IMPORT; i +=1 ){
if(i != USER_PATHS_COUNTER){
QString directoryPath = UBDirectories::basicPath((PossiblesImport)i) + QDir::separator() + "Sankore" + QDir::separator() + UBDirectories::subPathFor((PossiblesImport)i);
qint64 eachSize = UBDirectories::sizeOfDirectory(directoryPath);
totalSize += eachSize;
}
}
emit returnNecessarySize(totalSize);
emit finished();
}
Renamer::Renamer()
{
}
Renamer::~Renamer()
{
}
void Renamer::process()
{
foreach(PossiblesImport eachId, mIdsToImport){
QString originPath = UBDirectories::basicPath(eachId) + QDir::separator() + "Sankore" + QDir::separator() + UBDirectories::subPathFor(eachId);
QString destinationPath = UBDirectories::basicPath(eachId) + QDir::separator() + "OpenBoard" + QDir::separator() + UBDirectories::subPathFor(eachId);
qDebug() << "originPath " << originPath << " destinationPath " << destinationPath;
if(eachId == UniboardUserConfig){
emit log(originPath);
destinationPath = destinationPath.replace("Uniboard","OpenBoard");
#ifdef DRY_RUN
qDebug() << QString("from %1\nto %2\n\n").arg(originPath).arg(destinationPath);
SleeperThread::msleep(1*200);
#else
QFile(destinationPath).remove();
QFile(originPath).copy(destinationPath);
#endif
continue;
}
foreach(QString eachItem, QDir(originPath).entryList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot)){
if(eachItem == "folders.xml")
continue;
QString eachItemFullPath = originPath + eachItem;
emit log(eachItem);
QString absolutePath = QFileInfo(destinationPath).absolutePath();
if(!QDir(absolutePath).exists()){
qDebug() << "dir not exists. Creating " << absolutePath;
QDir().mkpath(absolutePath);
}
if(!QFileInfo(eachItemFullPath).isDir()){
QString destinationFile = destinationPath + eachItem;
#ifdef DRY_RUN
qDebug() << QString("from %1\nto %2\n\n").arg(eachItemFullPath).arg(destinationFile);
SleeperThread::msleep(1*200);
#else
if(QFileInfo(destinationFile).exists()){
QFile(destinationFile).remove();
}
qDebug() << "eachItemFullPath " << eachItemFullPath << " destinationFile " << destinationFile;
QFile file(eachItemFullPath);
if(!file.copy(destinationFile))
qDebug() << file.error();
#endif
}
else {
QString itemName = destinationPath + eachItem;
if(eachId==Documents){
itemName = itemName.replace("Sankore Document", "OpenBoard Document");
if(QDir(itemName).exists())
UBDirectories::removeDir(itemName);
}
#ifdef DRY_RUN
qDebug() << QString("from %1\nto %2\n\n").arg(eachItemFullPath).arg(itemName);
SleeperThread::msleep(1*200);
#else
qDebug() << "copyFolder eachItemFullPath " << eachItemFullPath << " itemName " << itemName;
UBDirectories::copyFolder(eachItemFullPath,itemName);
#endif
}
}
}
emit finished();
}
UBUpgradeToOefDelegate::UBUpgradeToOefDelegate(QObject *parent) :
QObject(parent)
{
}
int UBUpgradeToOefDelegate::getNumberOfItemToImport(PossiblesImport id)
{
if(id==UniboardUserConfig)
return QFile(UBDirectories::basicPath(id) + QDir::separator()+ "Sankore" + QDir::separator() + UBDirectories::subPathFor(id)).exists() ? 1 : 0;
QString path = UBDirectories::basicPath(id) + QDir::separator() + "Sankore" + QDir::separator() + UBDirectories::subPathFor(id);
QDir documentDir = QDir(path);
return documentDir.entryList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot).count();
}
void UBUpgradeToOefDelegate::initImportThread()
{
mThread = new QThread();
mWorker = new Renamer();
mWorker->moveToThread(mThread);
connect(mThread, SIGNAL(started()), mWorker, SLOT(process()));
connect(mWorker, SIGNAL(finished()), mThread, SLOT(quit()));
connect(mWorker, SIGNAL(finished()), mWorker, SLOT(deleteLater()));
connect(mThread, SIGNAL(finished()), mThread, SLOT(deleteLater()));
}
void UBUpgradeToOefDelegate::initSizeCalculationThread()
{
mCalculatorThread = new QThread();
mCalculator = new SizeCalculator();
mCalculator->moveToThread(mCalculatorThread);
connect(mCalculatorThread, SIGNAL(started()), mCalculator, SLOT(process()));
connect(mCalculator, SIGNAL(finished()), mCalculatorThread, SLOT(quit()));
connect(mCalculator, SIGNAL(finished()), mCalculator, SLOT(deleteLater()));
connect(mCalculatorThread, SIGNAL(finished()), mCalculatorThread, SLOT(deleteLater()));
}