File indexing completed on 2024-04-21 16:32:41

0001 /*
0002     SPDX-FileCopyrightText: 2009 Csaba Karai <krusader@users.sourceforge.net>
0003     SPDX-FileCopyrightText: 2009-2022 Krusader Krew <https://krusader.org>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "packjob.h"
0009 #include "../krglobal.h"
0010 #include "krarchandler.h"
0011 
0012 // QtCore
0013 #include <QDir>
0014 #include <QMimeDatabase>
0015 #include <QMimeType>
0016 #include <QTimer>
0017 
0018 #include <KI18n/KLocalizedString>
0019 
0020 PackJob::PackJob(const QUrl &srcUrl, const QUrl &destUrl, const QStringList &fileNames, const QString &type, const QMap<QString, QString> &packProps)
0021 {
0022     startAbstractJobThread(new PackThread(srcUrl, destUrl, fileNames, type, packProps));
0023 }
0024 
0025 PackJob *
0026 PackJob::createPacker(const QUrl &srcUrl, const QUrl &destUrl, const QStringList &fileNames, const QString &type, const QMap<QString, QString> &packProps)
0027 {
0028     return new PackJob(srcUrl, destUrl, fileNames, type, packProps);
0029 }
0030 
0031 PackThread::PackThread(const QUrl &srcUrl, const QUrl &destUrl, const QStringList &fileNames, const QString &type, const QMap<QString, QString> &packProps)
0032     : _sourceUrl(srcUrl)
0033     , _destUrl(destUrl)
0034     , _fileNames(fileNames)
0035     , _type(type)
0036     , _packProperties(packProps)
0037 {
0038 }
0039 
0040 void PackThread::slotStart()
0041 {
0042     QUrl newSource = downloadIfRemote(_sourceUrl, _fileNames);
0043     if (newSource.isEmpty())
0044         return;
0045 
0046     unsigned long totalFiles = 0;
0047 
0048     countLocalFiles(newSource, _fileNames, totalFiles);
0049 
0050     QString arcFile = tempFileIfRemote(_destUrl, _type);
0051     QString arcDir = newSource.adjusted(QUrl::StripTrailingSlash).path();
0052 
0053     setProgressTitle(i18n("Processed files"));
0054 
0055     QString save = QDir::currentPath();
0056     QDir::setCurrent(arcDir);
0057     bool result = krArcMan.pack(_fileNames, _type, arcFile, totalFiles, _packProperties, observer());
0058     QDir::setCurrent(save);
0059 
0060     if (isExited())
0061         return;
0062     if (!result) {
0063         sendError(KIO::ERR_INTERNAL, i18n("Error while packing"));
0064         return;
0065     }
0066 
0067     if (!uploadTempFiles())
0068         return;
0069 
0070     sendSuccess();
0071 }
0072 
0073 TestArchiveJob::TestArchiveJob(const QUrl &srcUrl, const QStringList &fileNames)
0074 {
0075     startAbstractJobThread(new TestArchiveThread(srcUrl, fileNames));
0076 }
0077 
0078 TestArchiveJob *TestArchiveJob::testArchives(const QUrl &srcUrl, const QStringList &fileNames)
0079 {
0080     return new TestArchiveJob(srcUrl, fileNames);
0081 }
0082 
0083 TestArchiveThread::TestArchiveThread(const QUrl &srcUrl, const QStringList &fileNames)
0084     : _sourceUrl(srcUrl)
0085     , _fileNames(fileNames)
0086 {
0087 }
0088 
0089 void TestArchiveThread::slotStart()
0090 {
0091     // Gets a QUrl of the source folder, which may be remote
0092     QUrl newSource = downloadIfRemote(_sourceUrl, _fileNames);
0093     if (newSource.isEmpty())
0094         return;
0095 
0096     for (int i = 0; i < _fileNames.count(); ++i) {
0097         QString path, type, password, arcName = _fileNames[i];
0098         if (!getArchiveInformation(path, type, password, arcName, newSource))
0099             return;
0100 
0101         // test the archive
0102         if (!krArcMan.test(path, type, password, observer(), 0)) {
0103             sendError(KIO::ERR_NO_CONTENT, i18nc("%1=archive filename", "%1, test failed.", arcName));
0104             return;
0105         }
0106     }
0107 
0108     sendMessage(i18n("Archive tests passed."));
0109     sendSuccess();
0110 }
0111 
0112 UnpackJob::UnpackJob(const QUrl &srcUrl, const QUrl &destUrl, const QStringList &fileNames)
0113 {
0114     startAbstractJobThread(new UnpackThread(srcUrl, destUrl, fileNames));
0115 }
0116 
0117 UnpackJob *UnpackJob::createUnpacker(const QUrl &srcUrl, const QUrl &destUrl, const QStringList &fileNames)
0118 {
0119     return new UnpackJob(srcUrl, destUrl, fileNames);
0120 }
0121 
0122 UnpackThread::UnpackThread(const QUrl &srcUrl, const QUrl &destUrl, const QStringList &fileNames)
0123     : _sourceUrl(srcUrl)
0124     , _destUrl(destUrl)
0125     , _fileNames(fileNames)
0126 {
0127 }
0128 
0129 void UnpackThread::slotStart()
0130 {
0131     // Gets a QUrl of the source folder, which may be remote
0132     QUrl newSource = downloadIfRemote(_sourceUrl, _fileNames);
0133     if (newSource.isEmpty())
0134         return;
0135 
0136     QString localDest = tempDirIfRemote(_destUrl);
0137 
0138     for (int i = 0; i < _fileNames.count(); ++i) {
0139         QString path, type, password, arcName = _fileNames[i];
0140         if (!getArchiveInformation(path, type, password, arcName, newSource))
0141             return;
0142 
0143         setProgressTitle(i18n("Processed files"));
0144         // unpack the files
0145         bool result = krArcMan.unpack(path, type, password, localDest, observer());
0146 
0147         if (isExited())
0148             return;
0149         if (!result) {
0150             sendError(KIO::ERR_INTERNAL, i18n("Error while unpacking"));
0151             return;
0152         }
0153     }
0154 
0155     if (!uploadTempFiles())
0156         return;
0157 
0158     sendSuccess();
0159 }