krarchandler.cpp 27.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*****************************************************************************
 * Copyright (C) 2001 Shie Erlich <krusader@users.sourceforge.net>           *
 * Copyright (C) 2001 Rafi Yanai <krusader@users.sourceforge.net>            *
 * Copyright (C) 2004-2018 Krusader Krew [https://krusader.org]              *
 *                                                                           *
 * 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>
Fathi Boudra's avatar
Fathi Boudra committed
38

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

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

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

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

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

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

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

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

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

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

KWallet::Wallet * KRarcHandler::wallet = 0;
87

Fathi Boudra's avatar
Fathi Boudra committed
88 89 90 91 92 93 94 95 96
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");
97
    if (KrServices::cmdExist("xz")) packers.append("xz");
Fathi Boudra's avatar
Fathi Boudra committed
98 99
    if (KrServices::cmdExist("unzip")) packers.append("unzip");
    if (KrServices::cmdExist("zip")) packers.append("zip");
Davide Gianforte's avatar
Davide Gianforte committed
100
    if (KrServices::cmdExist("zip")) packers.append("cbz");
Fathi Boudra's avatar
Fathi Boudra committed
101 102 103 104
    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
105
    if (KrServices::cmdExist("rar")) packers.append("cbr");
Fathi Boudra's avatar
Fathi Boudra committed
106 107 108 109 110 111
    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");
112
    // qDebug() << "Supported Packers:";
Fathi Boudra's avatar
Fathi Boudra committed
113 114
    //QStringList::Iterator it;
    //for( it = packers.begin(); it != packers.end(); ++it )
115
    // qDebug() << *it;
Fathi Boudra's avatar
Fathi Boudra committed
116 117 118 119 120 121 122

    return packers;
}

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

126 127 128 129 130 131
    // 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);
132 133
    }

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
    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
153 154 155 156 157 158 159
}

long KRarcHandler::arcFileCount(QString archive, QString type, QString password, KRarcObserver *observer)
{
    int divideWith = 1;

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

163 164 165
    // 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
166 167 168 169

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

170 171 172
    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
173
    else if (type == "tarz") lister << KrServices::fullPathName("tar") << "-tvzf";
174 175 176 177 178 179 180
    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
181 182 183 184 185
        if (KrServices::cmdExist("arj"))
            lister << KrServices::fullPathName("arj") << "v" << "-y" << "-v",
            divideWith = 4;
        else
            lister << KrServices::fullPathName("unarj") << "l";
186 187 188
    } 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
189 190 191
    else return 0L;

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

    // 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;
205
    if (type == "ace" && QFile("/dev/ptmx").exists())     // Don't remove, unace crashes if missing!!!
Fathi Boudra's avatar
Fathi Boudra committed
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
        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
221
        observer->detailedError(i18n("Failed to list the content of the archive (%1).", archive),
Fathi Boudra's avatar
Fathi Boudra committed
222 223
                                QString::fromLocal8Bit(list.readAllStandardError()));
        return 0;
224 225
    }

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

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

    return count / divideWith;
}

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

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

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

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

        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
286
            observer->detailedError(i18n("Failed to convert rpm (%1) to cpio.", archive), cpio.getErrorMsg());
Fathi Boudra's avatar
Fathi Boudra committed
287 288 289 290 291
            return 0;
        }

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

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

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

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

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

    proc.setWorkingDirectory(dest);

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

    // 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()
343 344
    if (proc.waitForStarted())
        while (proc.state() == QProcess::Running) {
Fathi Boudra's avatar
Fathi Boudra committed
345 346 347 348 349 350 351 352 353 354 355 356
            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
357
        observer->detailedError(i18n("Failed to unpack %1.", archive),
Fathi Boudra's avatar
Fathi Boudra committed
358 359
                                observer->wasCancelled() ? i18n("User cancelled.") : proc.getErrorMsg());
        return false;
360
    }
Fathi Boudra's avatar
Fathi Boudra committed
361 362
    return true; // SUCCESS
}
363

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

    // set the right packer to do the job
370 371 372
    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
373
    else if (type == "tarz") packer << KrServices::fullPathName("tar") << "-tvzf";
374 375 376
    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
377
    else if (type == "gzip") packer << KrServices::fullPathName("gzip") << "-tv";
378
    else if (type == "bzip2") packer << KrServices::fullPathName("bzip2") << "-tv";
Fathi Boudra's avatar
Fathi Boudra committed
379
    else if (type == "lzma") packer << KrServices::fullPathName("lzma") << "-tv";
380 381 382 383 384
    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
385
    else if (type == "cpio") packer << KrServices::fullPathName("cpio") << "--only-verify-crc" << "-tvF";
386
    else if (type == "7z")  packer << KrServices::fullPathName("7z") << "-y" << "t";
Fathi Boudra's avatar
Fathi Boudra committed
387 388 389
    else return false;

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

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

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

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

    // 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()
414 415
    if (proc.waitForStarted())
        while (proc.state() == QProcess::Running) {
Fathi Boudra's avatar
Fathi Boudra committed
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
            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
}

bool KRarcHandler::pack(QStringList fileNames, QString type, QString dest, long count, QMap<QString, QString> extraProps, KRarcObserver *observer)
{
    // set the right packer to do the job
    QStringList packer;

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

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

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

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

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

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

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

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

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

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

    for (QStringList::Iterator file = fileNames.begin(); file != fileNames.end(); ++file) {
        proc << *file;
530 531
    }

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

    // 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()
541 542
    if (proc.waitForStarted())
        while (proc.state() == QProcess::Running) {
Fathi Boudra's avatar
Fathi Boudra committed
543 544 545 546 547 548 549 550 551
            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
552
        observer->detailedError(i18n("Failed to pack %1.", dest),
Fathi Boudra's avatar
Fathi Boudra committed
553 554 555 556 557 558
                                observer->wasCancelled() ? i18n("User cancelled.") : proc.getErrorMsg());
        return false;
    }

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

566 567 568 569 570 571 572 573 574 575 576 577 578
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());
    }
    return (wallet != 0);
}

Fathi Boudra's avatar
Fathi Boudra committed
579 580 581 582 583 584 585 586 587 588 589
QString KRarcHandler::getPassword(QString path)
{
    QString password;

    QString key = "krarc-" + path;

    if (!KWallet::Wallet::keyDoesNotExist(KWallet::Wallet::NetworkWallet(), KWallet::Wallet::PasswordFolder(), key)) {
        if (!KWallet::Wallet::isOpen(KWallet::Wallet::NetworkWallet())  && wallet != 0) {
            delete wallet;
            wallet = 0;
        }
590
        if (openWallet() && wallet->hasFolder(KWallet::Wallet::PasswordFolder())) {
Fathi Boudra's avatar
Fathi Boudra committed
591 592 593 594 595 596 597 598 599 600 601 602
            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";
603 604 605
    QPointer<KPasswordDialog> passDlg = new KPasswordDialog(0L, KPasswordDialog::ShowKeepPassword);
            passDlg->setPrompt(i18n("This archive is encrypted, please supply the password:") ),
            passDlg->setUsername(user);
606
    passDlg->setPassword(password);
607
    if (passDlg->exec() == KPasswordDialog::Accepted) {
608
        password = passDlg->password();
Fathi Boudra's avatar
Fathi Boudra committed
609 610 611 612 613
        if (keep) {
            if (!KWallet::Wallet::isOpen(KWallet::Wallet::NetworkWallet()) && wallet != 0) {
                delete wallet;
                wallet = 0;
            }
614
            if (openWallet()) {
Fathi Boudra's avatar
Fathi Boudra committed
615 616 617 618 619 620 621 622 623 624 625 626
                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
627
        delete passDlg;
Fathi Boudra's avatar
Fathi Boudra committed
628 629
        return password;
    }
630
    delete passDlg;
Fathi Boudra's avatar
Fathi Boudra committed
631 632

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

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

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

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

658
void KRarcHandler::checkIf7zIsEncrypted(bool &encrypted, QString fileName)
Fathi Boudra's avatar
Fathi Boudra committed
659
{
660 661 662 663 664 665 666
    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
667 668
}