From 1130a8aa7e4ed3d97ba15461bd1cb707dc3e37dc Mon Sep 17 00:00:00 2001 From: Anatoly Mihalchenko Date: Fri, 14 Sep 2012 17:31:33 +0300 Subject: [PATCH] SANKORE-1074 Document navigator: "Add pages from file" isn't work with old UBZ --- src/adaptors/UBImportDocument.cpp | 86 ++++++++++++---------------- src/adaptors/UBImportDocument.h | 2 +- src/core/UBDocumentManager.cpp | 2 +- src/core/UBPersistenceManager.cpp | 43 +++++++++----- src/core/UBPersistenceManager.h | 7 ++- src/frameworks/UBFileSystemUtils.cpp | 3 +- 6 files changed, 74 insertions(+), 69 deletions(-) diff --git a/src/adaptors/UBImportDocument.cpp b/src/adaptors/UBImportDocument.cpp index a306285d..0bf32c82 100644 --- a/src/adaptors/UBImportDocument.cpp +++ b/src/adaptors/UBImportDocument.cpp @@ -56,7 +56,7 @@ QString UBImportDocument::importFileFilter() } -QString UBImportDocument::expandFileToDir(const QFile& pZipFile, const QString& pDir) +bool UBImportDocument::extractFileToDir(const QFile& pZipFile, const QString& pDir, QString& documentRoot) { QDir rootDir(pDir); @@ -65,63 +65,45 @@ QString UBImportDocument::expandFileToDir(const QFile& pZipFile, const QString& if(!zip.open(QuaZip::mdUnzip)) { qWarning() << "Import failed. Cause zip.open(): " << zip.getZipError(); - return ""; + return false; } 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; + documentRoot = UBPersistenceManager::persistenceManager()->generateUniqueDocumentPath(pDir); 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 ""; + return false; } if(!file.open(QIODevice::ReadOnly)) { qWarning() << "Import failed. Cause: file.open(): " << zip.getZipError(); - return ""; + return false; } if(file.getZipError()!= UNZ_OK) { qWarning() << "Import failed. Cause: file.getFileName(): " << zip.getZipError(); - return ""; + return false; } - QString newFileName = documentRootFolder + "/" + file.getActualFileName(); + QString newFileName = documentRoot + "/" + file.getActualFileName(); QFileInfo newFileInfo(newFileName); - rootDir.mkpath(newFileInfo.absolutePath()); + if (!rootDir.mkpath(newFileInfo.absolutePath())) + return false; out.setFileName(newFileName); - out.open(QIODevice::WriteOnly); + if (!out.open(QIODevice::WriteOnly)) + return false; // Slow like hell (on GNU/Linux at least), but it is not my fault. // Not ZIP/UNZIP package's fault either. @@ -131,7 +113,7 @@ QString UBImportDocument::expandFileToDir(const QFile& pZipFile, const QString& { qWarning() << "Import failed. Cause: Unable to write file"; out.close(); - return ""; + return false; } while(file.getChar(&c)) @@ -142,13 +124,13 @@ QString UBImportDocument::expandFileToDir(const QFile& pZipFile, const QString& if(file.getZipError()!=UNZ_OK) { qWarning() << "Import failed. Cause: " << zip.getZipError(); - return ""; + return false; } if(!file.atEnd()) { qWarning() << "Import failed. Cause: read all but not EOF"; - return ""; + return false; } file.close(); @@ -156,7 +138,7 @@ QString UBImportDocument::expandFileToDir(const QFile& pZipFile, const QString& if(file.getZipError()!=UNZ_OK) { qWarning() << "Import failed. Cause: file.close(): " << file.getZipError(); - return ""; + return false; } } @@ -166,11 +148,10 @@ QString UBImportDocument::expandFileToDir(const QFile& pZipFile, const QString& if(zip.getZipError()!=UNZ_OK) { qWarning() << "Import failed. Cause: zip.close(): " << zip.getZipError(); - return ""; + return false; } - - return documentRootFolder; + return true; } UBDocumentProxy* UBImportDocument::importFile(const QFile& pFile, const QString& pGroup) @@ -183,17 +164,17 @@ UBDocumentProxy* UBImportDocument::importFile(const QFile& pFile, const QString& // first unzip the file to the correct place QString path = UBSettings::userDocumentDirectory(); - QString documentRootFolder = expandFileToDir(pFile, path); - - if(!documentRootFolder.length()){ + QString documentRootFolder; + + if(!extractFileToDir(pFile, path, documentRootFolder)){ UBApplication::showMessage(tr("Import of file %1 failed.").arg(fi.baseName())); - return 0; - } - else{ - UBDocumentProxy* newDocument = UBPersistenceManager::persistenceManager()->createDocumentFromDir(documentRootFolder, pGroup); - UBApplication::showMessage(tr("Import successful.")); - return newDocument; + return NULL; } + + + UBDocumentProxy* newDocument = UBPersistenceManager::persistenceManager()->createDocumentFromDir(documentRootFolder, pGroup); + UBApplication::showMessage(tr("Import successful.")); + return newDocument; } bool UBImportDocument::addFileToDocument(UBDocumentProxy* pDocument, const QFile& pFile) @@ -203,9 +184,18 @@ bool UBImportDocument::addFileToDocument(UBDocumentProxy* pDocument, const QFile QString path = UBFileSystemUtils::createTempDir(); - QString documentRootFolder = expandFileToDir(pFile, path); - - UBPersistenceManager::persistenceManager()->addDirectoryContentToDocument(documentRootFolder, pDocument); + QString documentRootFolder; + if (!extractFileToDir(pFile, path, documentRootFolder)) + { + UBApplication::showMessage(tr("Import of file %1 failed.").arg(fi.baseName())); + return false; + } + + if (!UBPersistenceManager::persistenceManager()->addDirectoryContentToDocument(documentRootFolder, pDocument)) + { + UBApplication::showMessage(tr("Import of file %1 failed.").arg(fi.baseName())); + return false; + } UBFileSystemUtils::deleteDir(path); diff --git a/src/adaptors/UBImportDocument.h b/src/adaptors/UBImportDocument.h index a7530200..18abaea1 100644 --- a/src/adaptors/UBImportDocument.h +++ b/src/adaptors/UBImportDocument.h @@ -37,7 +37,7 @@ class UBImportDocument : public UBDocumentBasedImportAdaptor virtual bool addFileToDocument(UBDocumentProxy* pDocument, const QFile& pFile); private: - QString expandFileToDir(const QFile& pZipFile, const QString& pDir); + bool extractFileToDir(const QFile& pZipFile, const QString& pDir, QString& documentRoot); }; #endif /* UBIMPORTDOCUMENT_H_ */ diff --git a/src/core/UBDocumentManager.cpp b/src/core/UBDocumentManager.cpp index 6119a8b1..58430dbe 100644 --- a/src/core/UBDocumentManager.cpp +++ b/src/core/UBDocumentManager.cpp @@ -194,7 +194,7 @@ int UBDocumentManager::addFilesToDocument(UBDocumentProxy* document, QStringList int nImportedDocuments = 0; foreach(const QString& fileName, fileNames) { - UBApplication::showMessage(tr("Importing file").arg(fileName)); + UBApplication::showMessage(tr("Importing file %1").arg(fileName)); QFile file(fileName); QFileInfo fileInfo(file); diff --git a/src/core/UBPersistenceManager.cpp b/src/core/UBPersistenceManager.cpp index e7a609d8..21733038 100644 --- a/src/core/UBPersistenceManager.cpp +++ b/src/core/UBPersistenceManager.cpp @@ -667,11 +667,8 @@ void UBPersistenceManager::copyPage(UBDocumentProxy* pDocumentProxy, const int s int UBPersistenceManager::sceneCount(const UBDocumentProxy* proxy) { - return sceneCountInDir(proxy->persistencePath()); -} + const QString pPath = proxy->persistencePath(); -int UBPersistenceManager::sceneCountInDir(const QString& pPath) -{ int pageIndex = 0; bool moreToProcess = true; bool addedMissingZeroPage = false; @@ -709,15 +706,23 @@ int UBPersistenceManager::sceneCountInDir(const QString& pPath) return pageIndex; } - -QString UBPersistenceManager::generateUniqueDocumentPath() +QStringList UBPersistenceManager::getSceneFileNames(const QString& folder) { - QString ubPath = UBSettings::userDocumentDirectory(); + QDir dir(folder, "page???.svg", QDir::Name, QDir::Files); + return dir.entryList(); +} +QString UBPersistenceManager::generateUniqueDocumentPath(const QString& baseFolder) +{ QDateTime now = QDateTime::currentDateTime(); QString dirName = now.toString("yyyy-MM-dd hh-mm-ss.zzz"); - return ubPath + QString("/Sankore Document %1").arg(dirName); + return baseFolder + QString("/Sankore Document %1").arg(dirName); +} + +QString UBPersistenceManager::generateUniqueDocumentPath() +{ + return generateUniqueDocumentPath(UBSettings::userDocumentDirectory()); } @@ -730,34 +735,42 @@ void UBPersistenceManager::generatePathIfNeeded(UBDocumentProxy* pDocumentProxy) } -void UBPersistenceManager::addDirectoryContentToDocument(const QString& documentRootFolder, UBDocumentProxy* pDocument) +bool UBPersistenceManager::addDirectoryContentToDocument(const QString& documentRootFolder, UBDocumentProxy* pDocument) { - int sourcePageCount = sceneCountInDir(documentRootFolder); + QStringList sourceScenes = getSceneFileNames(documentRootFolder); + if (sourceScenes.empty()) + return false; int targetPageCount = pDocument->pageCount(); - for(int sourceIndex = 0 ; sourceIndex < sourcePageCount; sourceIndex++) + for(int sourceIndex = 0 ; sourceIndex < sourceScenes.size(); sourceIndex++) { int targetIndex = targetPageCount + sourceIndex; - QFile svg(documentRootFolder + UBFileSystemUtils::digitFileFormat("/page%1.svg", sourceIndex)); - svg.copy(pDocument->persistencePath() + UBFileSystemUtils::digitFileFormat("/page%1.svg", targetIndex)); + QFile svg(documentRootFolder + "/" + sourceScenes[sourceIndex]); + if (!svg.copy(pDocument->persistencePath() + UBFileSystemUtils::digitFileFormat("/page%1.svg", targetIndex))) + return false; UBSvgSubsetAdaptor::setSceneUuid(pDocument, targetIndex, QUuid::createUuid()); QFile thumb(documentRootFolder + UBFileSystemUtils::digitFileFormat("/page%1.thumbnail.jpg", sourceIndex)); + // We can ignore error in this case, thumbnail will be genarated thumb.copy(pDocument->persistencePath() + UBFileSystemUtils::digitFileFormat("/page%1.thumbnail.jpg", targetIndex)); } foreach(QString dir, mDocumentSubDirectories) { qDebug() << "copying " << documentRootFolder << "/" << dir << " to " << pDocument->persistencePath() << "/" + dir; - - UBFileSystemUtils::copyDir(documentRootFolder + "/" + dir, pDocument->persistencePath() + "/" + dir); + + QDir srcDir(documentRootFolder + "/" + dir); + if (srcDir.exists()) + if (!UBFileSystemUtils::copyDir(documentRootFolder + "/" + dir, pDocument->persistencePath() + "/" + dir)) + return false; } pDocument->setPageCount(sceneCount(pDocument)); + return false; } diff --git a/src/core/UBPersistenceManager.h b/src/core/UBPersistenceManager.h index ddd0c5db..c38e9895 100644 --- a/src/core/UBPersistenceManager.h +++ b/src/core/UBPersistenceManager.h @@ -79,13 +79,14 @@ class UBPersistenceManager : public QObject virtual QStringList allVideos(const QDir& dir); virtual QStringList allWidgets(const QDir& dir); - virtual QString generateUniqueDocumentPath(); + QString generateUniqueDocumentPath(); + QString generateUniqueDocumentPath(const QString& baseFolder); QString teacherGuideAbsoluteObjectPath(UBDocumentProxy* pDocumentProxy); QString addObjectToTeacherGuideDirectory(UBDocumentProxy* proxy, QString pPath); QString addWidgetToTeacherGuideDirectory(UBDocumentProxy* pDocumentProxy, QString pPath); - virtual void addDirectoryContentToDocument(const QString& documentRootFolder, UBDocumentProxy* pDocument); + bool addDirectoryContentToDocument(const QString& documentRootFolder, UBDocumentProxy* pDocument); virtual void upgradeDocumentIfNeeded(UBDocumentProxy* pDocumentProxy); @@ -128,7 +129,7 @@ class UBPersistenceManager : public QObject int sceneCount(const UBDocumentProxy* pDocumentProxy); - int sceneCountInDir(const QString& pPath); + static QStringList getSceneFileNames(const QString& folder); QList > allDocumentProxies(); diff --git a/src/frameworks/UBFileSystemUtils.cpp b/src/frameworks/UBFileSystemUtils.cpp index 1cd529cd..36a61945 100644 --- a/src/frameworks/UBFileSystemUtils.cpp +++ b/src/frameworks/UBFileSystemUtils.cpp @@ -273,7 +273,8 @@ bool UBFileSystemUtils::copyDir(const QString& pSourceDirPath, const QString& pT QDir dirSource(pSourceDirPath); QDir dirTarget(pTargetDirPath); - dirTarget.mkpath(pTargetDirPath); + if (!dirTarget.mkpath(pTargetDirPath)) + return false; bool successSoFar = true;