archivemodel.h 8.56 KB
Newer Older
Henrique Pinto's avatar
Henrique Pinto committed
1 2 3 4
/*
 * ark -- archiver for the KDE project
 *
 * Copyright (C) 2007 Henrique Pinto <henrique.pinto@kdemail.net>
5
 * Copyright (C) 2008-2009 Harald Hvaal <haraldhv@stud.ntnu.no>
6
 * Copyright (c) 2016 Vladyslav Batyrenko <mvlabat@gmail.com>
Henrique Pinto's avatar
Henrique Pinto committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * This program 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.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */
23 24 25
#ifndef ARCHIVEMODEL_H
#define ARCHIVEMODEL_H

Elvis Angelaccio's avatar
Elvis Angelaccio committed
26
#include "archiveentry.h"
27

28
#include <KMessageWidget>
Elvis Angelaccio's avatar
Elvis Angelaccio committed
29 30 31

#include <QAbstractItemModel>
#include <QScopedPointer>
32

33
using Kerfuffle::Archive;
34 35 36 37 38

namespace Kerfuffle
{
    class Query;
}
39

Ragnar Thomsen's avatar
Ragnar Thomsen committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
/**
 * Meta data related to one entry in a compressed archive.
 *
 * This is used for indexing entry properties as numbers
 * and for determining data displaying order in part's view.
 */
enum EntryMetaDataType {
    FullPath,            /**< The entry's file name */
    Size,                /**< The entry's original size */
    CompressedSize,      /**< The compressed size for the entry */
    Permissions,         /**< The entry's permissions */
    Owner,               /**< The user the entry belongs to */
    Group,               /**< The user group the entry belongs to */
    Ratio,               /**< The compression ratio for the entry */
    CRC,                 /**< The entry's CRC */
55
    BLAKE2,              /**< The entry's BLAKE2 */
Ragnar Thomsen's avatar
Ragnar Thomsen committed
56 57 58 59 60
    Method,              /**< The compression method used on the entry */
    Version,             /**< The archiver version needed to extract the entry */
    Timestamp            /**< The timestamp for the current entry */
};

61 62
class ArchiveModel: public QAbstractItemModel
{
63 64
    Q_OBJECT
public:
Elvis Angelaccio's avatar
Elvis Angelaccio committed
65
    explicit ArchiveModel(const QString &dbusPathName, QObject *parent = nullptr);
66
    ~ArchiveModel() override;
67

68 69
    QVariant data(const QModelIndex &index, int role) const override;
    Qt::ItemFlags flags(const QModelIndex &index) const override;
70
    QVariant headerData(int section, Qt::Orientation orientation,
71
                        int role = Qt::DisplayRole) const override;
72
    QModelIndex index(int row, int column,
73 74 75 76
                      const QModelIndex &parent = QModelIndex()) const override;
    QModelIndex parent(const QModelIndex &index) const override;
    int rowCount(const QModelIndex &parent = QModelIndex()) const override;
    int columnCount(const QModelIndex &parent = QModelIndex()) const override;
77 78

    //drag and drop related
79 80 81 82
    Qt::DropActions supportedDropActions() const override;
    QStringList mimeTypes() const override;
    QMimeData *mimeData(const QModelIndexList & indexes) const override;
    bool dropMimeData(const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent) override;
83

Elvis Angelaccio's avatar
Elvis Angelaccio committed
84 85 86
    void reset();
    void createEmptyArchive(const QString &path, const QString &mimeType, QObject *parent);
    KJob* loadArchive(const QString &path, const QString &mimeType, QObject *parent);
87
    Kerfuffle::Archive *archive() const;
88

Ragnar Thomsen's avatar
Ragnar Thomsen committed
89 90 91
    QList<int> shownColumns() const;
    QMap<int, QByteArray> propertiesMap() const;

92
    Archive::Entry *entryForIndex(const QModelIndex &index);
93

Laurent Montel's avatar
Laurent Montel committed
94 95
    Kerfuffle::ExtractJob* extractFile(Archive::Entry *file, const QString& destinationDir, Kerfuffle::ExtractionOptions options = Kerfuffle::ExtractionOptions()) const;
    Kerfuffle::ExtractJob* extractFiles(const QVector<Archive::Entry*>& files, const QString& destinationDir, Kerfuffle::ExtractionOptions options = Kerfuffle::ExtractionOptions()) const;
96

97 98 99
    Kerfuffle::PreviewJob* preview(Archive::Entry *file) const;
    Kerfuffle::OpenJob* open(Archive::Entry *file) const;
    Kerfuffle::OpenWithJob* openWith(Archive::Entry *file) const;
100

101 102 103 104
    Kerfuffle::AddJob* addFiles(QVector<Archive::Entry*> &entries, const Archive::Entry *destination, const Kerfuffle::CompressionOptions& options = Kerfuffle::CompressionOptions());
    Kerfuffle::MoveJob* moveFiles(QVector<Archive::Entry*> &entries, Archive::Entry *destination, const Kerfuffle::CompressionOptions& options = Kerfuffle::CompressionOptions());
    Kerfuffle::CopyJob* copyFiles(QVector<Archive::Entry*> &entries, Archive::Entry *destination, const Kerfuffle::CompressionOptions& options = Kerfuffle::CompressionOptions());
    Kerfuffle::DeleteJob* deleteFiles(QVector<Archive::Entry*> entries);
105

106 107 108 109 110
    /**
     * @param password The password to encrypt the archive with.
     * @param encryptHeader Whether to encrypt also the list of files.
     */
    void encryptArchive(const QString &password, bool encryptHeader);
111

112 113 114 115 116
    void countEntriesAndSize();
    qulonglong numberOfFiles() const;
    qulonglong numberOfFolders() const;
    qulonglong uncompressedSize() const;

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    /**
     * Constructs a list of conflicting entries.
     *
     * @param conflictingEntries Reference to the empty mutable entries list, which will be constructed.
     * If the method returns false, this list will contain only entries which produce a critical conflict.
     * @param entries New entries paths list.
     * @param allowMerging Boolean variable indicating whether merging is permitted.
     * If true, existing entries won't generate an error.
     *
     * @return Boolean variable indicating whether conflicts are not critical (true for not critical,
     * false for critical). For example, if there are both "some/file" (not a directory) and "some/file/" (a directory)
     * entries for both new and existing paths, the method will return false. Also, if merging is not allowed,
     * this method will return false for entries with the same path and types.
     */
    bool conflictingEntries(QList<const Archive::Entry*> &conflictingEntries, const QStringList &entries, bool allowMerging) const;

    static bool hasDuplicatedEntries(const QStringList &entries);

135
    static QMap<QString, Archive::Entry*> entryMap(const QVector<Archive::Entry*> &entries);
136 137 138 139

    QMap<QString, Kerfuffle::Archive::Entry*> filesToMove;
    QMap<QString, Kerfuffle::Archive::Entry*> filesToCopy;

140
Q_SIGNALS:
141 142 143 144
    void loadingStarted();
    void loadingFinished(KJob *);
    void extractionFinished(bool success);
    void error(const QString& error, const QString& details);
145
    void droppedFiles(const QStringList& files, const Archive::Entry*);
146
    void messageWidget(KMessageWidget::MessageType type, const QString& msg);
147

148
private Q_SLOTS:
149
    void slotNewEntry(Archive::Entry *entry);
150
    void slotListEntry(Archive::Entry *entry);
151 152
    void slotLoadingFinished(KJob *job);
    void slotEntryRemoved(const QString & path);
153
    void slotUserQuery(Kerfuffle::Query *query);
154 155 156
    void slotCleanupEmptyDirs();

private:
157 158 159 160 161 162 163 164 165 166 167
    /**
     * Strips file names that start with './'.
     *
     * For more information, see bug 194241.
     *
     * @param fileName The file name that will be stripped.
     *
     * @return @p fileName without the leading './'
     */
    QString cleanFileName(const QString& fileName);

168 169
    void initRootEntry();

170 171
    enum InsertBehaviour { NotifyViews, DoNotNotifyViews };
    Archive::Entry *parentFor(const Kerfuffle::Archive::Entry *entry, InsertBehaviour behaviour = NotifyViews);
172
    QModelIndex indexForEntry(Archive::Entry *entry);
173 174 175 176 177 178
    static bool compareAscending(const QModelIndex& a, const QModelIndex& b);
    static bool compareDescending(const QModelIndex& a, const QModelIndex& b);
    /**
     * Insert the node @p node into the model, ensuring all views are notified
     * of the change.
     */
179

180 181
    void insertEntry(Archive::Entry *entry, InsertBehaviour behaviour = NotifyViews);
    void newEntry(Kerfuffle::Archive::Entry *receivedEntry, InsertBehaviour behaviour);
182

183
    void traverseAndCountDirNode(Archive::Entry *dir);
184

185
    QList<int> m_showColumns;
186
    QScopedPointer<Kerfuffle::Archive> m_archive;
187
    QScopedPointer<Archive::Entry> m_rootEntry;
188
    QHash<QString, QIcon> m_entryIcons;
189
    QMap<int, QByteArray> m_propertiesMap;
190 191

    QString m_dbusPathName;
192 193 194 195

    qulonglong m_numberOfFiles;
    qulonglong m_numberOfFolders;
    qulonglong m_uncompressedSize;
196

Yuri Chornoivan's avatar
Yuri Chornoivan committed
197
    // Whether a file entry has been listed. Used to ensure all relevant columns are shown,
198 199
    // since directories might have fewer columns than files.
    bool m_fileEntryListed;
200 201 202
};

#endif // ARCHIVEMODEL_H