krarchandler.cpp 27.5 KB
Newer Older
1 2 3
/*****************************************************************************
 * Copyright (C) 2001 Shie Erlich <krusader@users.sourceforge.net>           *
 * Copyright (C) 2001 Rafi Yanai <krusader@users.sourceforge.net>            *
Nikita Melnichenko's avatar
Nikita Melnichenko committed
4
 * Copyright (C) 2004-2019 Krusader Krew [https://krusader.org]              *
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *                                                                           *
 * This file is part of Krusader [https://krusader.org].                     *
 *                                                                           *
 * Krusader 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 2 of the License, or         *
 * (at your option) any later version.                                       *
 *                                                                           *
 * Krusader 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 Krusader.  If not, see [http://www.gnu.org/licenses/].         *
 *****************************************************************************/
Fathi Boudra's avatar
Fathi Boudra committed
21 22 23

#include "krarchandler.h"

24 25 26 27 28 29
// QtCore
#include <QDebug>
#include <QDir>
#include <QFile>
// QtWidgets
#include <QApplication>
Fathi Boudra's avatar
Fathi Boudra committed
30

31
#include <KArchive/KTar>
32
#include <KConfigCore/KSharedConfig>
Simon Persson's avatar
Simon Persson committed
33
#include <KI18n/KLocalizedString>
34
#include <KIO/Global>
35
#include <KWallet/KWallet>
36
#include <KWidgetsAddons/KMessageBox>
37
#include <KWidgetsAddons/KPasswordDialog>
38
#include <utility>
Fathi Boudra's avatar
Fathi Boudra committed
39

40
#include "kr7zencryptionchecker.h"
41
#include "../krglobal.h"
Rafi Yanai's avatar
Rafi Yanai committed
42
#include "../defaults.h"
43
#include "../krservices.h"
44
#include "../Dialogs/krpleasewait.h"
45
#include "../../krArc/krlinecountingprocess.h"
Rafi Yanai's avatar
Rafi Yanai committed
46

47
#if 0
48 49 50
class DefaultKRarcObserver : public KRarcObserver
{
public:
Fathi Boudra's avatar
Fathi Boudra committed
51 52 53
    DefaultKRarcObserver() {}
    virtual ~DefaultKRarcObserver() {}

54
    virtual void processEvents() Q_DECL_OVERRIDE {
Fathi Boudra's avatar
Fathi Boudra committed
55 56 57 58
        usleep(1000);
        qApp->processEvents();
    }

59
    virtual void subJobStarted(const QString & jobTitle, int count) Q_DECL_OVERRIDE {
Fathi Boudra's avatar
Fathi Boudra committed
60 61 62
        krApp->startWaiting(jobTitle, count, true);
    }

63
    virtual void subJobStopped() Q_DECL_OVERRIDE {
Fathi Boudra's avatar
Fathi Boudra committed
64 65 66
        krApp->stopWait();
    }

67
    virtual bool wasCancelled() Q_DECL_OVERRIDE {
Fathi Boudra's avatar
Fathi Boudra committed
68 69 70
        return krApp->wasWaitingCancelled();
    }

71
    virtual void error(const QString & error) Q_DECL_OVERRIDE {
Fathi Boudra's avatar
Fathi Boudra committed
72 73 74
        KMessageBox::error(krApp, error, i18n("Error"));
    }

75
    virtual void detailedError(const QString & error, const QString & details) Q_DECL_OVERRIDE {
Fathi Boudra's avatar
Fathi Boudra committed
76 77 78
        KMessageBox::detailedError(krApp, error, details, i18n("Error"));
    }

79
    virtual void incrementProgress(int c) Q_DECL_OVERRIDE {
Fathi Boudra's avatar
Fathi Boudra committed
80 81
        krApp->plzWait->incProgress(c);
    }
82
};
83
#endif
84

85
static QStringList arcProtocols = QString("tar;bzip;bzip2;lzma;xz;gzip;krarc;zip").split(';');
86

87
KWallet::Wallet * KRarcHandler::wallet = nullptr;
88

Fathi Boudra's avatar
Fathi Boudra committed
89 90 91 92 93 94 95 96 97
QStringList KRarcHandler::supportedPackers()
{
    QStringList packers;

    // we will simply try to find the packers here..
    if (KrServices::cmdExist("tar")) packers.append("tar");
    if (KrServices::cmdExist("gzip")) packers.append("gzip");
    if (KrServices::cmdExist("bzip2")) packers.append("bzip2");
    if (KrServices::cmdExist("lzma")) packers.append("lzma");
98
    if (KrServices::cmdExist("xz")) packers.append("xz");
Fathi Boudra's avatar
Fathi Boudra committed
99 100
    if (KrServices::cmdExist("unzip")) packers.append("unzip");
    if (KrServices::cmdExist("zip")) packers.append("zip");
Davide Gianforte's avatar
Davide Gianforte committed
101
    if (KrServices::cmdExist("zip")) packers.append("cbz");
Fathi Boudra's avatar
Fathi Boudra committed
102 103 104 105
    if (KrServices::cmdExist("lha")) packers.append("lha");
    if (KrServices::cmdExist("cpio")) packers.append("cpio");
    if (KrServices::cmdExist("unrar")) packers.append("unrar");
    if (KrServices::cmdExist("rar")) packers.append("rar");
Davide Gianforte's avatar
Davide Gianforte committed
106
    if (KrServices::cmdExist("rar")) packers.append("cbr");
Fathi Boudra's avatar
Fathi Boudra committed
107 108 109 110 111 112
    if (KrServices::cmdExist("arj")) packers.append("arj");
    if (KrServices::cmdExist("unarj")) packers.append("unarj");
    if (KrServices::cmdExist("unace")) packers.append("unace");
    if (KrServices::cmdExist("dpkg")) packers.append("dpkg");
    if (KrServices::cmdExist("7z") || KrServices::cmdExist("7za")) packers.append("7z");
    if (KrServices::cmdExist("rpm") && KrServices::cmdExist("rpm2cpio")) packers.append("rpm");
113
    // qDebug() << "Supported Packers:";
Fathi Boudra's avatar
Fathi Boudra committed
114 115
    //QStringList::Iterator it;
    //for( it = packers.begin(); it != packers.end(); ++it )
116
    // qDebug() << *it;
Fathi Boudra's avatar
Fathi Boudra committed
117 118 119 120 121 122 123

    return packers;
}

bool KRarcHandler::arcSupported(QString type)
{
    // lst will contain the supported unpacker list...
124 125
    const KConfigGroup group(krConfig, "Archives");
    const QStringList lst = group.readEntry("Supported Packers", QStringList());
Fathi Boudra's avatar
Fathi Boudra committed
126

127 128 129 130 131 132
    // Let's notice that in some cases the QString `type` that arrives here
    // represents a mimetype, and in some other cases it represents
    // a short identifier.
    // If `type` is not a short identifier then it's supposed that `type` is a mime type
    if (type.length() > maxLenType) {
        type = getShortTypeFromMime(type);
133 134
    }

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
    return (type == "zip" && lst.contains("unzip"))
           || (type == "tar" && lst.contains("tar"))
           || (type == "tbz" && lst.contains("tar"))
           || (type == "tgz" && lst.contains("tar"))
           || (type == "tlz" && lst.contains("tar"))
           || (type == "txz" && lst.contains("tar"))
           || (type == "tarz" && lst.contains("tar"))
           || (type == "gzip" && lst.contains("gzip"))
           || (type == "bzip2" && lst.contains("bzip2"))
           || (type == "lzma" && lst.contains("lzma"))
           || (type == "xz" && lst.contains("xz"))
           || (type == "lha" && lst.contains("lha"))
           || (type == "ace" && lst.contains("unace"))
           || (type == "rpm" && lst.contains("cpio"))
           || (type == "cpio" && lst.contains("cpio"))
           || (type == "rar" && (lst.contains("unrar") || lst.contains("rar")))
           || (type == "arj" && (lst.contains("unarj") || lst.contains("arj")))
           || (type == "deb" && (lst.contains("dpkg") && lst.contains("tar")))
           || (type == "7z" && lst.contains("7z"));
Fathi Boudra's avatar
Fathi Boudra committed
154 155
}

156
long KRarcHandler::arcFileCount(const QString& archive, const QString& type, const QString& password, KRarcObserver *observer)
Fathi Boudra's avatar
Fathi Boudra committed
157 158 159 160
{
    int divideWith = 1;

    // first check if supported
161 162
    if (!arcSupported(type))
        return 0;
Fathi Boudra's avatar
Fathi Boudra committed
163

164 165 166
    // bzip2, gzip, etc. archives contain only one file
    if (type == "bzip2" || type == "gzip" || type == "lzma" || type == "xz")
        return 1L;
Fathi Boudra's avatar
Fathi Boudra committed
167 168 169 170

    // set the right lister to do the job
    QStringList lister;

171 172 173
    if (type == "zip") lister << KrServices::fullPathName("unzip") << "-ZTs";
    else if (type == "tar") lister << KrServices::fullPathName("tar") << "-tvf";
    else if (type == "tgz") lister << KrServices::fullPathName("tar") << "-tvzf";
Fathi Boudra's avatar
Fathi Boudra committed
174
    else if (type == "tarz") lister << KrServices::fullPathName("tar") << "-tvzf";
175 176 177 178 179 180 181
    else if (type == "tbz") lister << KrServices::fullPathName("tar") << "-tjvf";
    else if (type == "tlz") lister << KrServices::fullPathName("tar") << "--lzma" << "-tvf";
    else if (type == "txz") lister << KrServices::fullPathName("tar") << "--xz" << "-tvf";
    else if (type == "lha") lister << KrServices::fullPathName("lha") << "l";
    else if (type == "rar") lister << KrServices::fullPathName(KrServices::cmdExist("rar") ? "rar" : "unrar") << "l" << "-v";
    else if (type == "ace") lister << KrServices::fullPathName("unace") << "l";
    else if (type == "arj") {
Fathi Boudra's avatar
Fathi Boudra committed
182 183 184 185 186
        if (KrServices::cmdExist("arj"))
            lister << KrServices::fullPathName("arj") << "v" << "-y" << "-v",
            divideWith = 4;
        else
            lister << KrServices::fullPathName("unarj") << "l";
187 188 189
    } else if (type == "rpm") lister << KrServices::fullPathName("rpm") << "--dump" << "-lpq";
    else if (type == "deb") lister << KrServices::fullPathName("dpkg") << "-c";
    else if (type == "7z")  lister << KrServices::fullPathName("7z") << "-y" << "l";
Fathi Boudra's avatar
Fathi Boudra committed
190 191 192
    else return 0L;

    if (!password.isNull()) {
193
        if (type == "arj")
Fathi Boudra's avatar
Fathi Boudra committed
194
            lister << QString("-g%1").arg(password);
195
        if (type == "ace" || type == "rar" || type == "7z")
Fathi Boudra's avatar
Fathi Boudra committed
196
            lister << QString("-p%1").arg(password);
197
    }
Fathi Boudra's avatar
Fathi Boudra committed
198 199 200 201 202 203 204 205

    // tell the user to wait
    observer->subJobStarted(i18n("Counting files in archive"), 0);

    // count the number of files in the archive
    long count = 1;
    KProcess list;
    list << lister << archive;
206
    if (type == "ace" && QFile("/dev/ptmx").exists())     // Don't remove, unace crashes if missing!!!
Fathi Boudra's avatar
Fathi Boudra committed
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
        list.setStandardInputFile("/dev/ptmx");
    list.setOutputChannelMode(KProcess::SeparateChannels); // without this output redirection has no effect
    list.start();
    // TODO make use of asynchronous process starting. waitForStarted(int msec = 30000) is blocking
    // it would be better to connect to started(), error() and finished()
    if (list.waitForStarted()) while (list.state() == QProcess::Running) {
            observer->processEvents();
            if (observer->wasCancelled())
                list.kill();
        }
    ; // busy wait - need to find something better...

    observer->subJobStopped();

    if (list.exitStatus() != QProcess::NormalExit || !checkStatus(type, list.exitCode())) {
Pino Toscano's avatar
Pino Toscano committed
222
        observer->detailedError(i18n("Failed to list the content of the archive (%1).", archive),
Fathi Boudra's avatar
Fathi Boudra committed
223 224
                                QString::fromLocal8Bit(list.readAllStandardError()));
        return 0;
225 226
    }

Fathi Boudra's avatar
Fathi Boudra committed
227 228 229 230 231 232 233 234
    count = list.readAllStandardOutput().count('\n');

    //make sure you call stopWait after this function return...
    //  observer->subJobStopped();

    return count / divideWith;
}

235
bool KRarcHandler::unpack(QString archive, const QString& type, const QString& password, const QString& dest, KRarcObserver *observer)
Fathi Boudra's avatar
Fathi Boudra committed
236 237 238 239
{
    KConfigGroup group(krConfig, "Archives");
    if (group.readEntry("Test Before Unpack", _TestBeforeUnpack)) {
        // test first - or be sorry later...
240
        if (type != "rpm" && type != "deb" && !test(archive, type, password, observer, 0)) {
Pino Toscano's avatar
Pino Toscano committed
241
            observer->error(i18n("Failed to unpack %1.", archive));
Fathi Boudra's avatar
Fathi Boudra committed
242 243
            return false;
        }
244
    }
Fathi Boudra's avatar
Fathi Boudra committed
245 246 247

    // count the files in the archive
    long count = arcFileCount(archive, type, password, observer);
248 249 250 251
    if (count == 0)
        return false;   // not supported
    if (count == 1)
        count = 0;
Fathi Boudra's avatar
Fathi Boudra committed
252 253 254 255 256 257

    // choose the right packer for the job
    QString cpioName;
    QStringList packer;

    // set the right packer to do the job
258
    if (type == "zip") packer << KrServices::fullPathName("unzip") << "-o";
259 260
    else if (type == "tar") packer << KrServices::fullPathName("tar") << "-xvf";
    else if (type == "tgz") packer << KrServices::fullPathName("tar") << "-xvzf";
Fathi Boudra's avatar
Fathi Boudra committed
261
    else if (type == "tarz") packer << KrServices::fullPathName("tar") << "-xvzf";
262 263 264
    else if (type == "tbz") packer << KrServices::fullPathName("tar") << "-xjvf";
    else if (type == "tlz") packer << KrServices::fullPathName("tar") << "--lzma" << "-xvf";
    else if (type == "txz") packer << KrServices::fullPathName("tar") << "--xz" << "-xvf";
Fathi Boudra's avatar
Fathi Boudra committed
265
    else if (type == "gzip") packer << KrServices::fullPathName("gzip") << "-cd";
266
    else if (type == "bzip2") packer << KrServices::fullPathName("bzip2") << "-cdk";
Fathi Boudra's avatar
Fathi Boudra committed
267
    else if (type == "lzma") packer << KrServices::fullPathName("lzma") << "-cdk";
268 269 270 271 272
    else if (type == "xz")  packer << KrServices::fullPathName("xz") << "-cdk";
    else if (type == "lha") packer << KrServices::fullPathName("lha") << "xf";
    else if (type == "rar") packer << KrServices::fullPathName(KrServices::cmdExist("rar") ? "rar" : "unrar") << "-y" << "x";
    else if (type == "ace") packer << KrServices::fullPathName("unace") << "x";
    else if (type == "arj") {
Fathi Boudra's avatar
Fathi Boudra committed
273 274 275 276
        if (KrServices::cmdExist("arj"))
            packer << KrServices::fullPathName("arj") << "-y" << "-v" << "x";
        else
            packer << KrServices::fullPathName("unarj") << "x";
277 278
    } else if (type == "7z")  packer << KrServices::fullPathName("7z") << "-y" << "x";
    else if (type == "rpm") {
279 280
        // TODO use QTemporaryFile (setAutoRemove(false) when asynchrone)
        cpioName = QDir::tempPath() + QStringLiteral("/contents.cpio");
Fathi Boudra's avatar
Fathi Boudra committed
281 282 283 284 285 286

        KrLinecountingProcess cpio;
        cpio << KrServices::fullPathName("rpm2cpio") << archive;
        cpio.setStandardOutputFile(cpioName); // TODO maybe no tmpfile but a pipe (setStandardOutputProcess(packer))
        cpio.start();
        if (!cpio.waitForFinished() || cpio.exitStatus() != QProcess::NormalExit || !checkStatus("cpio", cpio.exitCode())) {
Pino Toscano's avatar
Pino Toscano committed
287
            observer->detailedError(i18n("Failed to convert rpm (%1) to cpio.", archive), cpio.getErrorMsg());
Fathi Boudra's avatar
Fathi Boudra committed
288 289 290 291 292
            return 0;
        }

        archive = cpioName;
        packer << KrServices::fullPathName("cpio") << "--force-local" << "--no-absolute-filenames" <<  "-iuvdF";
293
    } else if (type == "deb") {
294 295
        // TODO use QTemporaryFile (setAutoRemove(false) when asynchrone)
        cpioName = QDir::tempPath() + QStringLiteral("/contents.tar");
Fathi Boudra's avatar
Fathi Boudra committed
296 297 298 299 300

        KrLinecountingProcess dpkg;
        dpkg << KrServices::fullPathName("dpkg") << "--fsys-tarfile" << archive;
        dpkg.setStandardOutputFile(cpioName); // TODO maybe no tmpfile but a pipe (setStandardOutputProcess(packer))
        dpkg.start();
301
        if (!dpkg.waitForFinished() || dpkg.exitStatus() != QProcess::NormalExit || !checkStatus("deb", dpkg.exitCode())) {
Pino Toscano's avatar
Pino Toscano committed
302
            observer->detailedError(i18n("Failed to convert deb (%1) to tar.", archive), dpkg.getErrorMsg());
Fathi Boudra's avatar
Fathi Boudra committed
303 304 305 306 307 308 309 310
            return 0;
        }

        archive = cpioName;
        packer << KrServices::fullPathName("tar") << "xvf";
    } else return false;

    if (!password.isNull()) {
311
        if (type == "zip")
Fathi Boudra's avatar
Fathi Boudra committed
312
            packer << "-P" << password;
313
        if (type == "arj")
Fathi Boudra's avatar
Fathi Boudra committed
314
            packer << QString("-g%1").arg(password);
315
        if (type == "ace" || type == "rar" || type == "7z")
Fathi Boudra's avatar
Fathi Boudra committed
316
            packer << QString("-p%1").arg(password);
317
    }
Fathi Boudra's avatar
Fathi Boudra committed
318 319 320 321

    // unpack the files
    KrLinecountingProcess proc;
    proc << packer << archive;
322
    if (type == "bzip2" || type == "gzip" || type == "lzma" || type == "xz") {
Fathi Boudra's avatar
Fathi Boudra committed
323 324
        QString arcname = archive.mid(archive.lastIndexOf("/") + 1);
        if (arcname.contains(".")) arcname = arcname.left(arcname.lastIndexOf("."));
325
            proc.setStandardOutputFile(dest + '/' + arcname);
326
    }
327
    if (type == "ace" && QFile("/dev/ptmx").exists())    // Don't remove, unace crashes if missing!!!
Fathi Boudra's avatar
Fathi Boudra committed
328 329 330 331 332 333 334
        proc.setStandardInputFile("/dev/ptmx");

    proc.setWorkingDirectory(dest);

    // tell the user to wait
    observer->subJobStarted(i18n("Unpacking File(s)"), count);
    if (count != 0) {
335
        connect(&proc, &KrLinecountingProcess::newOutputLines, observer, &KRarcObserver::incrementProgress);
336
        if (type == "rpm")
337
            connect(&proc, &KrLinecountingProcess::newErrorLines, observer, &KRarcObserver::incrementProgress);
338
    }
Fathi Boudra's avatar
Fathi Boudra committed
339 340 341 342 343

    // start the unpacking process
    proc.start();
    // TODO make use of asynchronous process starting. waitForStarted(int msec = 30000) is blocking
    // it would be better to connect to started(), error() and finished()
344 345
    if (proc.waitForStarted())
        while (proc.state() == QProcess::Running) {
Fathi Boudra's avatar
Fathi Boudra committed
346 347 348 349 350 351 352 353 354 355 356 357
            observer->processEvents();
            if (observer->wasCancelled())
                proc.kill();
        }
    ; // busy wait - need to find something better...
    observer->subJobStopped();

    if (!cpioName.isEmpty())
        QFile(cpioName).remove();      /* remove the cpio file */

    // check the return value
    if (proc.exitStatus() != QProcess::NormalExit || !checkStatus(type, proc.exitCode())) {
Pino Toscano's avatar
Pino Toscano committed
358
        observer->detailedError(i18n("Failed to unpack %1.", archive),
Fathi Boudra's avatar
Fathi Boudra committed
359 360
                                observer->wasCancelled() ? i18n("User cancelled.") : proc.getErrorMsg());
        return false;
361
    }
Fathi Boudra's avatar
Fathi Boudra committed
362 363
    return true; // SUCCESS
}
364

365
bool KRarcHandler::test(const QString& archive, const QString& type, const QString& password, KRarcObserver *observer, long count)
Fathi Boudra's avatar
Fathi Boudra committed
366 367 368 369 370
{
    // choose the right packer for the job
    QStringList packer;

    // set the right packer to do the job
371 372 373
    if (type == "zip") packer << KrServices::fullPathName("unzip") << "-t";
    else if (type == "tar") packer << KrServices::fullPathName("tar") << "-tvf";
    else if (type == "tgz") packer << KrServices::fullPathName("tar") << "-tvzf";
Fathi Boudra's avatar
Fathi Boudra committed
374
    else if (type == "tarz") packer << KrServices::fullPathName("tar") << "-tvzf";
375 376 377
    else if (type == "tbz") packer << KrServices::fullPathName("tar") << "-tjvf";
    else if (type == "tlz") packer << KrServices::fullPathName("tar") << "--lzma" << "-tvf";
    else if (type == "txz") packer << KrServices::fullPathName("tar") << "--xz" << "-tvf";
Fathi Boudra's avatar
Fathi Boudra committed
378
    else if (type == "gzip") packer << KrServices::fullPathName("gzip") << "-tv";
379
    else if (type == "bzip2") packer << KrServices::fullPathName("bzip2") << "-tv";
Fathi Boudra's avatar
Fathi Boudra committed
380
    else if (type == "lzma") packer << KrServices::fullPathName("lzma") << "-tv";
381 382 383 384 385
    else if (type == "xz")  packer << KrServices::fullPathName("xz") << "-tv";
    else if (type == "rar") packer << KrServices::fullPathName(KrServices::cmdExist("rar") ? "rar" : "unrar") << "t";
    else if (type == "ace") packer << KrServices::fullPathName("unace") << "t";
    else if (type == "lha") packer << KrServices::fullPathName("lha") << "t";
    else if (type == "arj") packer << KrServices::fullPathName(KrServices::cmdExist("arj") ? "arj" : "unarj") << "t";
Fathi Boudra's avatar
Fathi Boudra committed
386
    else if (type == "cpio") packer << KrServices::fullPathName("cpio") << "--only-verify-crc" << "-tvF";
387
    else if (type == "7z")  packer << KrServices::fullPathName("7z") << "-y" << "t";
Fathi Boudra's avatar
Fathi Boudra committed
388 389 390
    else return false;

    if (!password.isNull()) {
391
        if (type == "zip")
Fathi Boudra's avatar
Fathi Boudra committed
392
            packer << "-P" << password;
393
        if (type == "arj")
Fathi Boudra's avatar
Fathi Boudra committed
394
            packer << QString("-g%1").arg(password);
395
        if (type == "ace" || type == "rar" || type == "7z")
Fathi Boudra's avatar
Fathi Boudra committed
396 397
            packer << QString("-p%1").arg(password);
    }
398

Fathi Boudra's avatar
Fathi Boudra committed
399 400 401 402
    // unpack the files
    KrLinecountingProcess proc;
    proc << packer << archive;

403
    if (type == "ace" && QFile("/dev/ptmx").exists())    // Don't remove, unace crashes if missing!!!
Fathi Boudra's avatar
Fathi Boudra committed
404 405 406 407 408
        proc.setStandardInputFile("/dev/ptmx");

    // tell the user to wait
    observer->subJobStarted(i18n("Testing Archive"), count);
    if (count != 0)
409
        connect(&proc, &KrLinecountingProcess::newOutputLines, observer, &KRarcObserver::incrementProgress);
Fathi Boudra's avatar
Fathi Boudra committed
410 411 412 413 414

    // start the unpacking process
    proc.start();
    // TODO make use of asynchronous process starting. waitForStarted(int msec = 30000) is blocking
    // it would be better to connect to started(), error() and finished()
415 416
    if (proc.waitForStarted())
        while (proc.state() == QProcess::Running) {
Fathi Boudra's avatar
Fathi Boudra committed
417 418 419 420 421 422 423 424 425 426 427 428 429 430
            observer->processEvents();
            if (observer->wasCancelled())
                proc.kill();
        }
    ; // busy wait - need to find something better...
    observer->subJobStopped();

    // check the return value
    if (proc.exitStatus() != QProcess::NormalExit || !checkStatus(type, proc.exitCode()))
        return false;

    return true; // SUCCESS
}

431
bool KRarcHandler::pack(QStringList fileNames, QString type, const QString& dest, long count, QMap<QString, QString> extraProps, KRarcObserver *observer)
Fathi Boudra's avatar
Fathi Boudra committed
432 433 434 435 436
{
    // set the right packer to do the job
    QStringList packer;

    if (type == "zip") {
437
        packer << KrServices::fullPathName("zip") << "-ry";
Davide Gianforte's avatar
Davide Gianforte committed
438
    } else if (type == "cbz") {
439 440
        packer << KrServices::fullPathName("zip") << "-ry";
        type = "zip";
Fathi Boudra's avatar
Fathi Boudra committed
441
    } else if (type == "tar") {
442
        packer << KrServices::fullPathName("tar") << "-cvf";
Fathi Boudra's avatar
Fathi Boudra committed
443
    } else if (type == "tar.gz") {
444 445
        packer << KrServices::fullPathName("tar") << "-cvzf";
        type = "tgz";
Fathi Boudra's avatar
Fathi Boudra committed
446
    } else if (type == "tar.bz2") {
447 448
        packer << KrServices::fullPathName("tar") << "-cvjf";
        type = "tbz";
Fathi Boudra's avatar
Fathi Boudra committed
449
    } else if (type == "tar.lzma") {
450 451
        packer << KrServices::fullPathName("tar") << "--lzma" << "-cvf";
        type = "tlz";
452
    } else if (type == "tar.xz") {
453 454
        packer << KrServices::fullPathName("tar") << "--xz" << "-cvf";
        type = "txz";
Fathi Boudra's avatar
Fathi Boudra committed
455
    } else if (type == "rar") {
456
        packer << KrServices::fullPathName("rar") << "-r" << "a";
Davide Gianforte's avatar
Davide Gianforte committed
457
    } else if (type == "cbr") {
458 459
        packer << KrServices::fullPathName("rar") << "-r" << "a";
        type = "rar";
Fathi Boudra's avatar
Fathi Boudra committed
460
    } else if (type == "lha") {
461
        packer << KrServices::fullPathName("lha") << "a";
Fathi Boudra's avatar
Fathi Boudra committed
462
    } else if (type == "arj") {
463
        packer << KrServices::fullPathName("arj") << "-r" << "-y" << "a";
Fathi Boudra's avatar
Fathi Boudra committed
464
    } else if (type == "7z") {
465
        packer << KrServices::fullPathName("7z") << "-y" << "a";
Fathi Boudra's avatar
Fathi Boudra committed
466 467
    } else return false;

Fathi Boudra's avatar
Fathi Boudra committed
468
    QString password;
Fathi Boudra's avatar
Fathi Boudra committed
469 470 471 472 473

    if (extraProps.count("Password") > 0) {
        password = extraProps[ "Password" ];

        if (!password.isNull()) {
474
            if (type == "zip")
Fathi Boudra's avatar
Fathi Boudra committed
475
                packer << "-P" << password;
476
            else if (type == "arj")
Fathi Boudra's avatar
Fathi Boudra committed
477
                packer << QString("-g%1").arg(password);
478
            else if (type == "ace" || type == "7z")
Fathi Boudra's avatar
Fathi Boudra committed
479
                packer << QString("-p%1").arg(password);
480
            else if (type == "rar") {
Fathi Boudra's avatar
Fathi Boudra committed
481 482 483 484 485
                if (extraProps.count("EncryptHeaders") > 0)
                    packer << QString("-hp%1").arg(password);
                else
                    packer << QString("-p%1").arg(password);
            } else
Fathi Boudra's avatar
Fathi Boudra committed
486
                password.clear();
Fathi Boudra's avatar
Fathi Boudra committed
487
        }
Csaba Karai's avatar
Csaba Karai committed
488
    }
Fathi Boudra's avatar
Fathi Boudra committed
489 490 491 492 493 494

    if (extraProps.count("VolumeSize") > 0) {
        QString sizeStr = extraProps[ "VolumeSize" ];
        KIO::filesize_t size = sizeStr.toLongLong();

        if (size >= 10000) {
495
            if (type == "arj" || type == "rar")
Fathi Boudra's avatar
Fathi Boudra committed
496 497
                packer << QString("-v%1b").arg(sizeStr);
        }
498 499
    }

Fathi Boudra's avatar
Fathi Boudra committed
500 501 502 503 504 505 506
    if (extraProps.count("CompressionLevel") > 0) {
        int level = extraProps[ "CompressionLevel" ].toInt() - 1;
        if (level < 0)
            level = 0;
        if (level > 8)
            level = 8;

507
        if (type == "rar") {
Fathi Boudra's avatar
Fathi Boudra committed
508 509
            static const int rarLevels[] = { 0, 1, 2, 2, 3, 3, 4, 4, 5 };
            packer << QString("-m%1").arg(rarLevels[ level ]);
510
        } else if (type == "arj") {
Fathi Boudra's avatar
Fathi Boudra committed
511 512
            static const int arjLevels[] = { 0, 4, 4, 3, 3, 2, 2, 1, 1 };
            packer << QString("-m%1").arg(arjLevels[ level ]);
513
        } else if (type == "zip") {
Fathi Boudra's avatar
Fathi Boudra committed
514 515
            static const int zipLevels[] = { 0, 1, 2, 4, 5, 6, 7, 8, 9 };
            packer << QString("-%1").arg(zipLevels[ level ]);
516
        } else if (type == "7z") {
Fathi Boudra's avatar
Fathi Boudra committed
517 518 519
            static const int sevenZipLevels[] = { 0, 1, 2, 4, 5, 6, 7, 8, 9 };
            packer << QString("-mx%1").arg(sevenZipLevels[ level ]);
        }
520 521
    }

Fathi Boudra's avatar
Fathi Boudra committed
522 523 524 525 526 527 528
    if (extraProps.count("CommandLineSwitches") > 0)
        packer << QString("%1").arg(extraProps[ "CommandLineSwitches" ]);

    // prepare to pack
    KrLinecountingProcess proc;
    proc << packer << dest;

529 530
    for (auto & fileName : fileNames) {
        proc << fileName;
531 532
    }

Fathi Boudra's avatar
Fathi Boudra committed
533 534 535
    // tell the user to wait
    observer->subJobStarted(i18n("Packing File(s)"), count);
    if (count != 0)
536
        connect(&proc, &KrLinecountingProcess::newOutputLines, observer, &KRarcObserver::incrementProgress);
Fathi Boudra's avatar
Fathi Boudra committed
537 538 539 540 541

    // start the packing process
    proc.start();
    // TODO make use of asynchronous process starting. waitForStarted(int msec = 30000) is blocking
    // it would be better to connect to started(), error() and finished()
542 543
    if (proc.waitForStarted())
        while (proc.state() == QProcess::Running) {
Fathi Boudra's avatar
Fathi Boudra committed
544 545 546 547 548 549 550 551 552
            observer->processEvents();
            if (observer->wasCancelled())
                proc.kill();
        }
    ; // busy wait - need to find something better...
    observer->subJobStopped();

    // check the return value
    if (proc.exitStatus() != QProcess::NormalExit || !checkStatus(type, proc.exitCode())) {
Pino Toscano's avatar
Pino Toscano committed
553
        observer->detailedError(i18n("Failed to pack %1.", dest),
Fathi Boudra's avatar
Fathi Boudra committed
554 555 556 557 558 559
                                observer->wasCancelled() ? i18n("User cancelled.") : proc.getErrorMsg());
        return false;
    }

    KConfigGroup group(krConfig, "Archives");
    if (group.readEntry("Test Archives", _TestArchives) &&
560
            !test(dest, type, password, observer, count)) {
Pino Toscano's avatar
Pino Toscano committed
561
        observer->error(i18n("Failed to pack %1.", dest));
Fathi Boudra's avatar
Fathi Boudra committed
562
        return false;
563
    }
Fathi Boudra's avatar
Fathi Boudra committed
564
    return true; // SUCCESS
Rafi Yanai's avatar
Rafi Yanai committed
565
}
566

567 568 569 570 571 572 573 574 575 576
bool KRarcHandler::openWallet()
{
    if (!wallet) {
        // find a suitable parent window
        QWidget *actWindow = QApplication::activeWindow();
        if (!actWindow)
            actWindow = (QWidget*) QApplication::desktop();

        wallet = KWallet::Wallet::openWallet(KWallet::Wallet::NetworkWallet(), actWindow->effectiveWinId());
    }
577
    return (wallet != nullptr);
578 579
}

580
QString KRarcHandler::getPassword(const QString& path)
Fathi Boudra's avatar
Fathi Boudra committed
581 582 583 584 585 586
{
    QString password;

    QString key = "krarc-" + path;

    if (!KWallet::Wallet::keyDoesNotExist(KWallet::Wallet::NetworkWallet(), KWallet::Wallet::PasswordFolder(), key)) {
587
        if (!KWallet::Wallet::isOpen(KWallet::Wallet::NetworkWallet())  && wallet != nullptr) {
Fathi Boudra's avatar
Fathi Boudra committed
588
            delete wallet;
589
            wallet = nullptr;
Fathi Boudra's avatar
Fathi Boudra committed
590
        }
591
        if (openWallet() && wallet->hasFolder(KWallet::Wallet::PasswordFolder())) {
Fathi Boudra's avatar
Fathi Boudra committed
592 593 594 595 596 597 598 599 600 601 602 603
            wallet->setFolder(KWallet::Wallet::PasswordFolder());
            QMap<QString, QString> map;
            if (wallet->readMap(key, map) == 0) {
                QMap<QString, QString>::const_iterator it = map.constFind("password");
                if (it != map.constEnd())
                    password = it.value();
            }
        }
    }

    bool keep = true;
    QString user = "archive";
604
    QPointer<KPasswordDialog> passDlg = new KPasswordDialog(nullptr, KPasswordDialog::ShowKeepPassword);
605 606
            passDlg->setPrompt(i18n("This archive is encrypted, please supply the password:") ),
            passDlg->setUsername(user);
607
    passDlg->setPassword(password);
608
    if (passDlg->exec() == KPasswordDialog::Accepted) {
609
        password = passDlg->password();
Fathi Boudra's avatar
Fathi Boudra committed
610
        if (keep) {
611
            if (!KWallet::Wallet::isOpen(KWallet::Wallet::NetworkWallet()) && wallet != nullptr) {
Fathi Boudra's avatar
Fathi Boudra committed
612
                delete wallet;
613
                wallet = nullptr;
Fathi Boudra's avatar
Fathi Boudra committed
614
            }
615
            if (openWallet()) {
Fathi Boudra's avatar
Fathi Boudra committed
616 617 618 619 620 621 622 623 624 625 626 627
                bool ok = true;
                if (!wallet->hasFolder(KWallet::Wallet::PasswordFolder()))
                    ok = wallet->createFolder(KWallet::Wallet::PasswordFolder());
                if (ok) {
                    wallet->setFolder(KWallet::Wallet::PasswordFolder());
                    QMap<QString, QString> map;
                    map.insert("login", "archive");
                    map.insert("password", password);
                    wallet->writeMap(key, map);
                }
            }
        }
Csaba Karai's avatar
Csaba Karai committed
628
        delete passDlg;
Fathi Boudra's avatar
Fathi Boudra committed
629 630
        return password;
    }
631
    delete passDlg;
Fathi Boudra's avatar
Fathi Boudra committed
632 633

    return "";
Rafi Yanai's avatar
Rafi Yanai committed
634
}
635

636
bool KRarcHandler::isArchive(const QUrl &url)
Fathi Boudra's avatar
Fathi Boudra committed
637
{
638
    QString protocol = url.scheme();
Fathi Boudra's avatar
Fathi Boudra committed
639 640 641
    if (arcProtocols.indexOf(protocol) != -1)
        return true;
    else return false;
Rafi Yanai's avatar
Rafi Yanai committed
642
}
643

644
QString KRarcHandler::getType(bool &encrypted, QString fileName, const QString& mime, bool checkEncrypted, bool fast)
Fathi Boudra's avatar
Fathi Boudra committed
645
{
646
    QString result = detectArchive(encrypted, std::move(fileName), checkEncrypted, fast);
647
    if (result.isNull()) {
648 649
        // Then the type is based on the mime type
        return getShortTypeFromMime(mime);
650
    }
651
    return result;
Fathi Boudra's avatar
Fathi Boudra committed
652 653
}

654
bool KRarcHandler::checkStatus(const QString& type, int exitCode)
Fathi Boudra's avatar
Fathi Boudra committed
655
{
656
    return KrArcBaseManager::checkStatus(type, exitCode);
Rafi Yanai's avatar
Rafi Yanai committed
657
}
658

659
void KRarcHandler::checkIf7zIsEncrypted(bool &encrypted, QString fileName)
Fathi Boudra's avatar
Fathi Boudra committed
660
{
661 662 663 664 665 666 667
    Kr7zEncryptionChecker proc;
    // TODO incorporate all this in Kr7zEncryptionChecker
    proc << KrServices::fullPathName("7z") << "-y" << "t";
    proc << fileName;
    proc.start();
    proc.waitForFinished();
    encrypted = proc.isEncrypted();
Rafi Yanai's avatar
Rafi Yanai committed
668 669
}