diff --git a/src/designviewer.cpp b/src/designviewer.cpp index b24da8b94ac8f761fc0fcb0f547d6e8253c465c0..f9cd839e76d76f8a0d63daceec4a0d65ca34d2cb 100644 --- a/src/designviewer.cpp +++ b/src/designviewer.cpp @@ -163,7 +163,84 @@ QString DesignViewer::unpackProject(const QByteArray &project, bool extractZip) projectLocation = ":" + resourcePath; } - return projectLocation; + // create a temporary directory and copy the project there + QTemporaryDir tempDir; + tempDir.setAutoRemove(false); + if (!tempDir.isValid()) { + printErr("Could not create a temporary directory."); + return ""; + } + + const QString tempDirPath = tempDir.path(); + printLog("Temporary directory: " + tempDirPath); + + QDir tempDirDir(tempDirPath); + if (!tempDirDir.exists()) { + printErr("Temporary directory does not exist."); + return ""; + } + + if (!QDir(projectLocation).exists()) { + printErr("Project directory does not exist."); + return ""; + } + + // copy all files and directories from projectLocation to tempDirPath + QDirIterator it(projectLocation, + QDir::AllEntries | QDir::NoDotAndDotDot, + QDirIterator::Subdirectories); + printLog("Files in project location: " + projectLocation); + while (it.hasNext()) { + const QString filePath = it.next(); + printLog("Found file: " + filePath); + const QFileInfo fileInfo(filePath); + const QString relativeFilePath = fileInfo.filePath().remove(projectLocation); + const QString newFilePath = tempDirPath + relativeFilePath; + printLog("Copying file: " + filePath + " to " + newFilePath); + if (fileInfo.isDir()) { + QDir().mkpath(newFilePath); + } else if (!QFile::copy(filePath, newFilePath)) { + printErr("Could not copy file: " + filePath + " to " + newFilePath); + return ""; + } + } + + // find all .qml files in the temporary directory and remove the Qt versions from 6.5 to 6.9 + // at the end of the import statements. example "import <modulename> 6.5" -> "import <modulename>" + QDirIterator it2(tempDirPath, {"*.qml"}, QDir::Files, QDirIterator::Subdirectories); + + printLog("Files in temporary directory: " + tempDirPath); + while (it2.hasNext()) { + const QString filePath = it2.next(); + QFile file(filePath); + // set file permissions to read and write + file.setPermissions(QFile::ReadOwner | QFile::WriteOwner); + if (!file.open(QIODevice::ReadWrite)) { + printErr("Could not open file: " + filePath); + return ""; + } + + const QRegularExpression importRegExp("import\\s+\\S+\\s+6\\.[5-9]"); + QString content = QString::fromUtf8(file.readAll()); + while (content.contains(importRegExp)) { + const QRegularExpressionMatch match = importRegExp.match(content); + const QStringList textList = match.capturedTexts(); + + for (const QString &text : textList) { + const QString newText = QString(text).remove(QRegularExpression("\\s+6\\.[5-9]")); + printLog("Found text to replace: " + text + " with " + newText + " in file " + + filePath); + content.replace(text, newText); + } + } + + file.resize(0); + file.write(content.toUtf8()); + file.close(); + } + + // return the temporary directory path + return tempDirPath; } QString DesignViewer::findFile(const QString &dir, const QString &filter) @@ -277,9 +354,8 @@ bool DesignViewer::runProject(const QByteArray &projectData, const QString &proj printLog("Found mainQmlFile: " + mainQmlFilePath); QUrl mainQmlUrl = QUrl::fromUserInput(mainQmlFilePath); - QFile file(mainQmlUrl.path().prepend(":")); - if (!file.open(QIODevice::ReadOnly)) - { + QFile file(mainQmlUrl.path()); + if (!file.open(QIODevice::ReadOnly)) { printErr("Could not open mainQmlfile for reading! " + file.fileName() + ": " + file.errorString()); return false; }