File indexing completed on 2024-05-12 05:50:22

0001 /*
0002     SPDX-FileCopyrightText: 2007 Henrique Pinto <henrique.pinto@kdemail.net>
0003     SPDX-FileCopyrightText: 2008-2009 Harald Hvaal <haraldhv@stud.ntnu.no>
0004     SPDX-FileCopyrightText: 2016 Vladyslav Batyrenko <mvlabat@gmail.com>
0005 
0006     SPDX-License-Identifier: BSD-2-Clause
0007 */
0008 
0009 #ifndef READWRITELIBARCHIVEPLUGIN_H
0010 #define READWRITELIBARCHIVEPLUGIN_H
0011 
0012 #include "libarchiveplugin.h"
0013 
0014 #include <QSaveFile>
0015 #include <QStringList>
0016 
0017 using namespace Kerfuffle;
0018 
0019 class ReadWriteLibarchivePlugin : public LibarchivePlugin
0020 {
0021     Q_OBJECT
0022 
0023 public:
0024     explicit ReadWriteLibarchivePlugin(QObject *parent, const QVariantList &args);
0025     ~ReadWriteLibarchivePlugin() override;
0026 
0027     bool addFiles(const QVector<Archive::Entry *> &files,
0028                   const Archive::Entry *destination,
0029                   const CompressionOptions &options,
0030                   uint numberOfEntriesToAdd = 0) override;
0031     bool moveFiles(const QVector<Archive::Entry *> &files, Archive::Entry *destination, const CompressionOptions &options) override;
0032     bool copyFiles(const QVector<Archive::Entry *> &files, Archive::Entry *destination, const CompressionOptions &options) override;
0033     bool deleteFiles(const QVector<Archive::Entry *> &files) override;
0034 
0035 protected:
0036     bool initializeWriter(const bool creatingNewFile = false, const CompressionOptions &options = CompressionOptions());
0037     bool initializeWriterFilters();
0038     bool initializeNewFileWriterFilters(const CompressionOptions &options);
0039     void finish(const bool isSuccessful);
0040 
0041 private:
0042     /**
0043      * Processes all the existing entries and does manipulations to them
0044      * based on the OperationMode (Add/Move/Copy/Delete).
0045      *
0046      * @param entriesCounter Counter of added/moved/copied/deleted entries.
0047      *
0048      * @return bool indicating whether the operation was successful.
0049      */
0050     bool processOldEntries(uint &entriesCounter, OperationMode mode, uint totalCount);
0051 
0052     /**
0053      * Writes entry being read into memory.
0054      *
0055      * @return bool indicating whether the operation was successful.
0056      */
0057     bool writeEntry(struct archive_entry *entry);
0058 
0059     /**
0060      * Writes entry from physical disk.
0061      *
0062      * @return bool indicating whether the operation was successful.
0063      */
0064     bool writeFile(const QString &relativeName, const QString &destination);
0065 
0066     QSaveFile m_tempFile;
0067     ArchiveWrite m_archiveWriter;
0068 
0069     // New added files by addFiles methods. It's assigned to m_filesPaths
0070     // and then is used by processOldEntries method (in Add mode) for skipping already written entries.
0071     QStringList m_writtenFiles;
0072 
0073     // Passed argument from job which is used by processOldEntries method.
0074     QStringList m_filesPaths;
0075     int m_entriesWithoutChildren = 0;
0076     const Archive::Entry *m_destination = nullptr;
0077 };
0078 
0079 #endif // READWRITELIBARCHIVEPLUGIN_H