File indexing completed on 2024-12-22 04:57:38

0001 /*  This file is part of the KDE project
0002     SPDX-FileCopyrightText: 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>
0003     SPDX-FileContributor: Kevin Krammer <krake@kdab.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "testdatautil.h"
0009 
0010 #include "mixedmaildirresource_debug.h"
0011 
0012 #include <QDir>
0013 #include <QFile>
0014 
0015 using namespace TestDataUtil;
0016 
0017 // use this instead of QFile::copy() because we want overwrite in case it exists
0018 static bool copyFile(const QString &sourceFileName, const QString &targetFileName)
0019 {
0020     QFile sourceFile(sourceFileName);
0021     QFile targetFile(targetFileName);
0022 
0023     if (!sourceFile.open(QIODevice::ReadOnly)) {
0024         qCritical() << "Cannot open source file" << sourceFileName;
0025         return false;
0026     }
0027 
0028     if (!targetFile.open(QIODevice::WriteOnly)) {
0029         qCritical() << "Cannot open target file" << targetFileName;
0030         return false;
0031     }
0032 
0033     return targetFile.write(sourceFile.readAll()) != -1;
0034 }
0035 
0036 static bool copyFiles(const QDir &sourceDir, const QDir &targetDir)
0037 {
0038     const QStringList files = sourceDir.entryList(QStringList(), QDir::Files);
0039     for (const QString &file : files) {
0040         const QFileInfo sourceFileInfo(sourceDir, file);
0041         const QFileInfo targetFileInfo(targetDir, file);
0042         if (!copyFile(sourceFileInfo.absoluteFilePath(), targetFileInfo.absoluteFilePath())) {
0043             qCritical() << "Failed to copy" << sourceFileInfo.absoluteFilePath() << "to" << targetFileInfo.absoluteFilePath();
0044             return false;
0045         }
0046     }
0047 
0048     return true;
0049 }
0050 
0051 FolderType TestDataUtil::folderType(const QString &testDataName)
0052 {
0053     const QDir dir(QStringLiteral(":/data"));
0054     const QString indexFilePattern = QStringLiteral(".%1.index");
0055 
0056     if (!dir.exists(testDataName) || !dir.exists(indexFilePattern.arg(testDataName))) {
0057         return InvalidFolder;
0058     }
0059 
0060     const QFileInfo fileInfo(dir, testDataName);
0061     return fileInfo.isDir() ? MaildirFolder : MBoxFolder;
0062 }
0063 
0064 QStringList TestDataUtil::testDataNames()
0065 {
0066     const QDir dir(QStringLiteral(":/data"));
0067     const QFileInfoList dirEntries = dir.entryInfoList();
0068 
0069     const QString indexFilePattern = QStringLiteral(".%1.index");
0070 
0071     QStringList result;
0072     for (const QFileInfo &fileInfo : dirEntries) {
0073         if (dir.exists(indexFilePattern.arg(fileInfo.fileName()))) {
0074             result << fileInfo.fileName();
0075         }
0076     }
0077 
0078     result.sort();
0079     return result;
0080 }
0081 
0082 bool TestDataUtil::installFolder(const QString &testDataName, const QString &installPath, const QString &folderName)
0083 {
0084     const FolderType type = TestDataUtil::folderType(testDataName);
0085     if (type == InvalidFolder) {
0086         qCritical() << "testDataName" << testDataName << "is not a valid mail folder type";
0087         return false;
0088     }
0089 
0090     if (!QDir::current().mkpath(installPath)) {
0091         qCritical() << "Couldn't create installPath" << installPath;
0092         return false;
0093     }
0094 
0095     const QDir installDir(installPath);
0096     const QFileInfo installFileInfo(installDir, folderName);
0097     if (installDir.exists(folderName)) {
0098         switch (type) {
0099         case MaildirFolder:
0100             if (!installFileInfo.isDir()) {
0101                 qCritical() << "Target file name" << folderName << "already exists but is not a directory";
0102                 return false;
0103             }
0104             break;
0105 
0106         case MBoxFolder:
0107             if (!installFileInfo.isFile()) {
0108                 qCritical() << "Target file name" << folderName << "already exists but is not a directory";
0109                 return false;
0110             }
0111             break;
0112 
0113         default:
0114             // already handled at beginning
0115             Q_ASSERT(false);
0116             return false;
0117         }
0118     }
0119 
0120     const QDir testDataDir(QStringLiteral(":/data"));
0121 
0122     switch (type) {
0123     case MaildirFolder: {
0124         const QString subPathPattern = QStringLiteral("%1/%2");
0125         if (!installDir.mkpath(subPathPattern.arg(folderName, QStringLiteral("new")))
0126             || !installDir.mkpath(subPathPattern.arg(folderName, QStringLiteral("cur")))
0127             || !installDir.mkpath(subPathPattern.arg(folderName, QStringLiteral("tmp")))) {
0128             qCritical() << "Couldn't create maildir directory structure";
0129             return false;
0130         }
0131 
0132         QDir sourceDir = testDataDir;
0133         QDir targetDir = installDir;
0134 
0135         sourceDir.cd(testDataName);
0136         targetDir.cd(folderName);
0137 
0138         if (sourceDir.cd(QStringLiteral("new"))) {
0139             targetDir.cd(QStringLiteral("new"));
0140             if (!copyFiles(sourceDir, targetDir)) {
0141                 return false;
0142             }
0143             sourceDir.cdUp();
0144             targetDir.cdUp();
0145         }
0146 
0147         if (sourceDir.cd(QStringLiteral("cur"))) {
0148             targetDir.cd(QStringLiteral("cur"));
0149             if (!copyFiles(sourceDir, targetDir)) {
0150                 return false;
0151             }
0152             sourceDir.cdUp();
0153             targetDir.cdUp();
0154         }
0155 
0156         if (sourceDir.cd(QStringLiteral("tmp"))) {
0157             targetDir.cd(QStringLiteral("tmp"));
0158             if (!copyFiles(sourceDir, targetDir)) {
0159                 return false;
0160             }
0161         }
0162         break;
0163     }
0164 
0165     case MBoxFolder: {
0166         const QFileInfo mboxFileInfo(testDataDir, testDataName);
0167         if (!copyFile(mboxFileInfo.absoluteFilePath(), installFileInfo.absoluteFilePath())) {
0168             qCritical() << "Failed to copy" << mboxFileInfo.absoluteFilePath() << "to" << installFileInfo.absoluteFilePath();
0169             return false;
0170         }
0171         break;
0172     }
0173 
0174     default:
0175         // already handled at beginning
0176         Q_ASSERT(false);
0177         return false;
0178     }
0179 
0180     const QString indexFilePattern = QStringLiteral(".%1.index");
0181     const QFileInfo indexFileInfo(testDataDir, indexFilePattern.arg(testDataName));
0182     const QFileInfo indexInstallFileInfo(installDir, indexFilePattern.arg(folderName));
0183 
0184     return copyFile(indexFileInfo.absoluteFilePath(), indexInstallFileInfo.absoluteFilePath());
0185 }