Example #1
0
void AddToArchive::slotStartJob(void)
{
    kDebug();

    Kerfuffle::CompressionOptions options;

    if (!m_inputs.size()) {
        KMessageBox::error(NULL, i18n("No input files were given."));
        return;
    }

    Kerfuffle::Archive *archive;
    if (!m_filename.isEmpty()) {
        archive = Kerfuffle::Archive::create(m_filename, m_mimeType, this);
        kDebug() << "Set filename to " << m_filename;
    } else {
        if (m_autoFilenameSuffix.isEmpty()) {
            KMessageBox::error(NULL, i18n("You need to either supply a filename for the archive or a suffix (such as rar, tar.gz) with the <command>--autofilename</command> argument."));
            emitResult();
            return;
        }

        if (m_firstPath.isEmpty()) {
            kDebug() << "Weird, this should not happen. no firstpath defined. aborting";
            emitResult();
            return;
        }

        QString base = QFileInfo(m_inputs.first()).absoluteFilePath();
        if (base.endsWith(QLatin1Char('/'))) {
            base.chop(1);
        }

        QString finalName = base + QLatin1Char( '.' ) + m_autoFilenameSuffix;

        //if file already exists, append a number to the base until it doesn't
        //exist
        int appendNumber = 0;
        while (QFileInfo(finalName).exists()) {
            ++appendNumber;
            finalName = base + QLatin1Char( '_' ) + QString::number(appendNumber) + QLatin1Char( '.' ) + m_autoFilenameSuffix;
        }

        kDebug() << "Autoset filename to "<< finalName;
        archive = Kerfuffle::Archive::create(finalName, m_mimeType, this);
    }

    if (archive == NULL) {
        KMessageBox::error(NULL, i18n("Failed to create the new archive. Permissions might not be sufficient."));
        emitResult();
        return;
    } else if (archive->isReadOnly()) {
        KMessageBox::error(NULL, i18n("It is not possible to create archives of this type."));
        emitResult();
        return;
    }

    if (m_changeToFirstPath) {
        if (m_firstPath.isEmpty()) {
            kDebug() << "Weird, this should not happen. no firstpath defined. aborting";
            emitResult();
            return;
        }

        const QDir stripDir(m_firstPath);

        for (int i = 0; i < m_inputs.size(); ++i) {
            m_inputs[i] = stripDir.absoluteFilePath(m_inputs.at(i));
        }

        options[QLatin1String( "GlobalWorkDir" )] = stripDir.path();
        kDebug() << "Setting GlobalWorkDir to " << stripDir.path();
    }

    Kerfuffle::AddJob *job =
        archive->addFiles(m_inputs, options);

    KIO::getJobTracker()->registerJob(job);

    connect(job, SIGNAL(result(KJob*)),
            this, SLOT(slotFinished(KJob*)));

    job->start();
}
Example #2
0
void AddToArchive::slotStartJob()
{
    Kerfuffle::CompressionOptions options;

    if (m_inputs.isEmpty()) {
        KMessageBox::error(NULL, i18n("No input files were given."));
        emitResult();
        return;
    }

    Kerfuffle::Archive *archive;
    if (!m_filename.isEmpty()) {
        archive = Kerfuffle::Archive::create(m_filename, m_mimeType, this);
        qCDebug(ARK) << "Set filename to " << m_filename;
    } else {
        if (m_autoFilenameSuffix.isEmpty()) {
            KMessageBox::error(Q_NULLPTR, xi18n("You need to either supply a filename for the archive or a suffix (such as rar, tar.gz) with the <command>--autofilename</command> argument."));
            emitResult();
            return;
        }

        if (m_firstPath.isEmpty()) {
            qCWarning(ARK) << "Weird, this should not happen. no firstpath defined. aborting";
            emitResult();
            return;
        }

        const QString base = detectBaseName(m_inputs);

        QString finalName = base + QLatin1Char( '.' ) + m_autoFilenameSuffix;

        //if file already exists, append a number to the base until it doesn't
        //exist
        int appendNumber = 0;
        while (QFileInfo::exists(finalName)) {
            ++appendNumber;
            finalName = base + QLatin1Char( '_' ) + QString::number(appendNumber) + QLatin1Char( '.' ) + m_autoFilenameSuffix;
        }

        qCDebug(ARK) << "Autoset filename to "<< finalName;
        archive = Kerfuffle::Archive::create(finalName, m_mimeType, this);
    }

    Q_ASSERT(archive);

    if (!archive->isValid()) {
        if (archive->error() == NoPlugin) {
            KMessageBox::error(Q_NULLPTR, i18n("Failed to create the new archive. No suitable plugin found."));
            emitResult();
            return;
        }
        if (archive->error() == FailedPlugin) {
            KMessageBox::error(Q_NULLPTR, i18n("Failed to create the new archive. Could not load a suitable plugin."));
            emitResult();
            return;
        }
    } else if (archive->isReadOnly()) {
        KMessageBox::error(Q_NULLPTR, i18n("It is not possible to create archives of this type."));
        emitResult();
        return;
    }

    if (!m_password.isEmpty()) {
        archive->encrypt(m_password, m_enableHeaderEncryption);
    }

    if (m_changeToFirstPath) {
        if (m_firstPath.isEmpty()) {
            qCWarning(ARK) << "Weird, this should not happen. no firstpath defined. aborting";
            emitResult();
            return;
        }

        const QDir stripDir(m_firstPath);

        for (int i = 0; i < m_inputs.size(); ++i) {
            m_inputs[i] = stripDir.absoluteFilePath(m_inputs.at(i));
        }


        options[QStringLiteral( "GlobalWorkDir" )] = stripDir.path();
        qCDebug(ARK) << "Setting GlobalWorkDir to " << stripDir.path();
    }

    Kerfuffle::AddJob *job =
        archive->addFiles(m_inputs, options);

    KIO::getJobTracker()->registerJob(job);

    connect(job, &Kerfuffle::AddJob::result, this, &AddToArchive::slotFinished);

    job->start();
}