UniboardSankoreTransition.cpp 10.7 KB
Newer Older
Claudio Valerio's avatar
Claudio Valerio committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * 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/>.
 */
15

Claudio Valerio's avatar
Claudio Valerio committed
16 17 18
#include "UniboardSankoreTransition.h"
#include "core/UBSettings.h"
#include "frameworks/UBFileSystemUtils.h"
19
#include "core/UBApplication.h"
20
#include "core/UBPersistenceManager.h"
21
#include "gui/UBMainWindow.h"
Claudio Valerio's avatar
Claudio Valerio committed
22

23 24
#include "core/memcheck.h"

Claudio Valerio's avatar
Claudio Valerio committed
25 26
UniboardSankoreTransition::UniboardSankoreTransition(QObject *parent) :
    QObject(parent)
27 28
    , mTransitionDlg(NULL)
    , mThread(NULL)
Claudio Valerio's avatar
Claudio Valerio committed
29
{
30
    mOldSankoreDirectory = UBFileSystemUtils::normalizeFilePath(QDesktopServices::storageLocation(QDesktopServices::DataLocation));
31

32
    mUniboardSourceDirectory = UBFileSystemUtils::normalizeFilePath(QDesktopServices::storageLocation(QDesktopServices::DataLocation));
33
#if defined(Q_WS_MACX)
34
    mOldSankoreDirectory.replace("Sankore/Sankore 3.1", "Sankore");
35 36 37 38 39
    mUniboardSourceDirectory.replace("Sankore/Sankore 3.1", "Uniboard");
#else
    mUniboardSourceDirectory.replace("Sankore/Sankore 3.1", "Mnemis/Uniboard");
#endif
    connect(this, SIGNAL(docAdded(UBDocumentProxy*)), UBPersistenceManager::persistenceManager(), SIGNAL(documentCreated(UBDocumentProxy*)));
Claudio Valerio's avatar
Claudio Valerio committed
40
}
41

42
UniboardSankoreTransition::~UniboardSankoreTransition()
Claudio Valerio's avatar
Claudio Valerio committed
43
{
44 45 46 47 48
    if(NULL != mTransitionDlg)
    {
        delete mTransitionDlg;
        mTransitionDlg = NULL;
    }
49 50 51 52 53

    if(mThread){
        delete mThread;
        mThread = NULL;
    }
Claudio Valerio's avatar
Claudio Valerio committed
54 55 56 57 58 59 60 61 62
}

void UniboardSankoreTransition::rollbackDocumentsTransition(QFileInfoList& fileInfoList)
{
    QFileInfoList::iterator fileInfo;
    for (fileInfo = fileInfoList.begin(); fileInfo != fileInfoList.end(); fileInfo += 1) {
        if (fileInfo->isDir() && fileInfo->fileName().startsWith("Uniboard Document ")){
            QString sankoreDocumentName = fileInfo->fileName();
            sankoreDocumentName.replace("Uniboard","Sankore");
63
            QString sankoreDocumentDirectoryPath = UBSettings::userDocumentDirectory() + "/" + sankoreDocumentName;
Claudio Valerio's avatar
Claudio Valerio committed
64 65 66 67 68 69 70
            if (QFileInfo(sankoreDocumentDirectoryPath).exists()){
                UBFileSystemUtils::deleteDir(sankoreDocumentDirectoryPath);
            }
        }
    }
}

71 72 73 74 75 76 77 78 79
bool UniboardSankoreTransition::checkDocumentDirectory(QString& documentDirectoryPath)
{
    bool result = true;
    result = updateSankoreHRef(documentDirectoryPath);
    QString sankoreWidgetPath = documentDirectoryPath + "/widgets";
    result &= updateIndexWidget(sankoreWidgetPath);
    return result;
}

Claudio Valerio's avatar
Claudio Valerio committed
80 81
void UniboardSankoreTransition::documentTransition()
{
82
    if (QFileInfo(mUniboardSourceDirectory).exists() || QFileInfo(mOldSankoreDirectory).exists()){
83 84 85
        QString uniboardDocumentDirectory = mUniboardSourceDirectory + "/document";

        QFileInfoList fileInfoList = UBFileSystemUtils::allElementsInDirectory(uniboardDocumentDirectory);
86
        fileInfoList << UBFileSystemUtils::allElementsInDirectory(mOldSankoreDirectory + "/document");
87

88
        QString backupDirectoryPath = UBFileSystemUtils::normalizeFilePath(QDesktopServices::storageLocation(QDesktopServices::DesktopLocation));
89

90
        if (fileInfoList.count() != 0){
91
            mTransitionDlg = new UBUpdateDlg(NULL, fileInfoList.count(), backupDirectoryPath);
92 93 94 95
            connect(mTransitionDlg, SIGNAL(updateFiles()), this, SLOT(startDocumentTransition()));
            connect(this, SIGNAL(transitionFinished(bool)), mTransitionDlg, SLOT(onFilesUpdated(bool)));
            mTransitionDlg->show();
        }
96
    }
97 98 99
    else{
        UBApplication::mainWindow->information(tr("Import old Uniboard/Sankore documents"), tr("There are no documents that should be imported"));
    }
100 101 102
}

void UniboardSankoreTransition::startDocumentTransition()
103 104 105 106 107 108
{
    mThread = new UniboardSankoreThread(this);
    mThread->start();
    connect(this,SIGNAL(transitioningFile(QString)),mTransitionDlg,SLOT(transitioningFile(QString)));
}

109 110 111 112 113 114 115 116 117 118 119

bool UniboardSankoreTransition::checkPage(QString& sankorePagePath)
{
    QFile file(sankorePagePath);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        return false;

    QByteArray documentByteArray = file.readAll();
    file.close();

    QString sankoreDirectory = sankorePagePath.left(sankorePagePath.indexOf("/page"));
120

121 122 123
    sankoreDirectory = QUrl::fromLocalFile(sankoreDirectory).toString();
    QString documentString(documentByteArray);

124 125
    QRegExp videoRegExp("<video(.*)xlink:href=\"(.*)videos/(.*)/>");
    videoRegExp.setMinimal(true);
126

127
    documentString.replace(videoRegExp,"<video\\1xlink:href=\"videos/\\3/>");
128

129 130 131 132 133

    QRegExp audioRegExp("<audio(.*)xlink:href=\"(.*)audios/(.*)/>");
    audioRegExp.setMinimal(true);

    documentString.replace(audioRegExp,"<audio\\1xlink:href=\"audios/\\3/>");
134

Didier's avatar
Didier committed
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
        return false;

    file.write(documentString.toAscii());
    file.close();

    return true;
}


bool UniboardSankoreTransition::checkWidget(QString& sankoreWidgetIndexPath)
{
    QFile file(sankoreWidgetIndexPath);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        return false;

    QByteArray documentByteArray = file.readAll();
    file.close();

    QString documentString(documentByteArray);

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
    QRegExp swfOriginFilePathRegExp("<param name=\"movie\" value=\"(.*)\">");
    swfOriginFilePathRegExp.setMinimal(true);
    swfOriginFilePathRegExp.indexIn(documentString);
    QString origin = swfOriginFilePathRegExp.cap(1);
    if(origin.contains("http://")){
        // an url is the source of the swf. The source is kept as is.
        return true;
    }

    //changing the path
    QRegExp swfDataPathRegExp("<object(.*)data=\"(.*)interactive content/Web/(.*)\"(.*)>");
    swfDataPathRegExp.setMinimal(true);
    documentString.replace(swfDataPathRegExp,"<object\\1data=\"\\3\">");

    QRegExp swfMoviePathRegExp("<param name=\"movie\" value=\"(.*)interactive content/Web/(.*)\">");
    swfMoviePathRegExp.setMinimal(true);
    documentString.replace(swfMoviePathRegExp,"<param name=\"movie\" value=\"\\2\">");

    //copy the swf on the right place
    QRegExp swfFileNameRegExp("<param name=\"movie\" value=\"(.*)\">");
    swfFileNameRegExp.setMinimal(true);
    swfFileNameRegExp.indexIn(documentString);
    QString swfFileName = swfFileNameRegExp.cap(1);
    int lastDirectoryLevel = sankoreWidgetIndexPath.lastIndexOf("/");
    if (lastDirectoryLevel == -1)
        lastDirectoryLevel = sankoreWidgetIndexPath.lastIndexOf("\\");


    QString destination(sankoreWidgetIndexPath.left(lastDirectoryLevel) + "/" + swfFileName);
    QFile(origin).copy(destination);
186 187 188 189 190 191 192 193 194 195

    if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
        return false;

    file.write(documentString.toAscii());
    file.close();

    return true;
}

Didier's avatar
Didier committed
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217

bool UniboardSankoreTransition::updateIndexWidget(QString& sankoreWidgetPath)
{
    bool result = true;
    QFileInfoList fileInfoList = UBFileSystemUtils::allElementsInDirectory(sankoreWidgetPath);

    QFileInfoList::iterator fileInfo;
    for (fileInfo = fileInfoList.begin(); fileInfo != fileInfoList.end() && result; fileInfo += 1) {
        if (fileInfo->fileName().endsWith("wgt")){
            QString path = fileInfo->absolutePath() + "/" + fileInfo->fileName() + "/index.html";
            if (QFile(path).exists())
                result = checkWidget(path);

            path = fileInfo->absolutePath() + "/" + fileInfo->fileName() + "/index.htm";
            if (QFile(path).exists())
                result &= checkWidget(path);
        }
    }

    return result;
}

218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
bool UniboardSankoreTransition::updateSankoreHRef(QString& sankoreDocumentPath)
{
    bool result = true;
    QFileInfoList fileInfoList = UBFileSystemUtils::allElementsInDirectory(sankoreDocumentPath);

    QFileInfoList::iterator fileInfo;

    for (fileInfo = fileInfoList.begin(); fileInfo != fileInfoList.end() && result; fileInfo += 1) {
        if (fileInfo->fileName().endsWith("svg")){
            QString path = fileInfo->absolutePath() + "/" + fileInfo->fileName();
            result = checkPage(path);
        }
    }

    return result;
}

235
void UniboardSankoreTransition::executeTransition()
236 237
{
    bool result = false;
238 239 240
    QString backupDestinationPath = mTransitionDlg->backupPath() + "/OldSankoreAndUniboardVersionsBackup";
    result = UBFileSystemUtils::copyDir(mUniboardSourceDirectory + "/document", backupDestinationPath);
    result &= UBFileSystemUtils::copyDir(mOldSankoreDirectory + "/document", backupDestinationPath);
Claudio Valerio's avatar
Claudio Valerio committed
241

242
    QString uniboardDocumentDirectory = mUniboardSourceDirectory + "/document";
Claudio Valerio's avatar
Claudio Valerio committed
243
    QFileInfoList fileInfoList = UBFileSystemUtils::allElementsInDirectory(uniboardDocumentDirectory);
244
    fileInfoList.append(UBFileSystemUtils::allElementsInDirectory(mOldSankoreDirectory + "/document"));
Claudio Valerio's avatar
Claudio Valerio committed
245 246

    QFileInfoList::iterator fileInfo;
247
    QString sankoreDocumentDirectory = UBSettings::userDocumentDirectory();
248

Claudio Valerio's avatar
Claudio Valerio committed
249
    for (fileInfo = fileInfoList.begin(); fileInfo != fileInfoList.end() && result; fileInfo += 1) {
250
        if (fileInfo->isDir() && (fileInfo->fileName().startsWith("Uniboard Document ") || fileInfo->fileName().startsWith("Sankore Document "))){
Claudio Valerio's avatar
Claudio Valerio committed
251
            QString sankoreDocumentName = fileInfo->fileName();
252
            emit transitioningFile(sankoreDocumentName);
Claudio Valerio's avatar
Claudio Valerio committed
253
            sankoreDocumentName.replace("Uniboard","Sankore");
254 255 256
            QString sankoreDocumentPath = sankoreDocumentDirectory + "/" + sankoreDocumentName;
            result = UBFileSystemUtils::copyDir(fileInfo->filePath(),sankoreDocumentPath);
            result &= updateSankoreHRef(sankoreDocumentPath);
Didier's avatar
Didier committed
257 258
            QString sankoreWidgetPath = sankoreDocumentDirectory +  "/" + sankoreDocumentName + "/widgets";
            result &= updateIndexWidget(sankoreWidgetPath);
Claudio Valerio's avatar
Claudio Valerio committed
259 260 261 262
        }
    }

    if (!result){
263
        qWarning() << "The transition has failed";
Claudio Valerio's avatar
Claudio Valerio committed
264
        rollbackDocumentsTransition(fileInfoList);
265
        UBFileSystemUtils::deleteDir(backupDestinationPath);
Claudio Valerio's avatar
Claudio Valerio committed
266
    }
267 268
    else{
        UBFileSystemUtils::deleteDir(mOldSankoreDirectory);
Claudio Valerio's avatar
Claudio Valerio committed
269 270
        UBFileSystemUtils::deleteDir(mUniboardSourceDirectory);
    }
271 272

    emit transitionFinished(result);
273
}
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292


UniboardSankoreThread::UniboardSankoreThread(QObject* parent):QThread(parent)
{

}

UniboardSankoreThread::~UniboardSankoreThread()
{

}

void UniboardSankoreThread::run()
{
    UniboardSankoreTransition* pTransition = dynamic_cast<UniboardSankoreTransition*>(parent());

    pTransition->executeTransition();
}