archivemodel.h 5.36 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>
Henrique Pinto's avatar
Henrique Pinto committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * 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.
 *
 */
22 23 24 25
#ifndef ARCHIVEMODEL_H
#define ARCHIVEMODEL_H

#include <QAbstractItemModel>
26 27
#include <QScopedPointer>

28
#include <kjobtrackerinterface.h>
29
#include "kerfuffle/archive_kerfuffle.h"
30

31
using Kerfuffle::ArchiveEntry;
32 33 34 35 36

namespace Kerfuffle
{
    class Query;
}
37

38
class ArchiveNode;
39 40 41 42
class ArchiveDirNode;

class ArchiveModel: public QAbstractItemModel
{
43 44
    Q_OBJECT
public:
45
    explicit ArchiveModel(const QString &dbusPathName, QObject *parent = 0);
46 47
    ~ArchiveModel();

48 49
    QVariant data(const QModelIndex &index, int role) const Q_DECL_OVERRIDE;
    Qt::ItemFlags flags(const QModelIndex &index) const Q_DECL_OVERRIDE;
50
    QVariant headerData(int section, Qt::Orientation orientation,
51
                        int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
52
    QModelIndex index(int row, int column,
53 54 55 56
                      const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
    QModelIndex parent(const QModelIndex &index) const Q_DECL_OVERRIDE;
    int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
    int columnCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
57

58
    virtual void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) Q_DECL_OVERRIDE;
59 60

    //drag and drop related
61 62 63 64
    Qt::DropActions supportedDropActions() const Q_DECL_OVERRIDE;
    QStringList mimeTypes() const Q_DECL_OVERRIDE;
    QMimeData * mimeData(const QModelIndexList & indexes) const Q_DECL_OVERRIDE;
    bool dropMimeData(const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent) Q_DECL_OVERRIDE;
65

66
    KJob* setArchive(Kerfuffle::Archive *archive);
67
    Kerfuffle::Archive *archive() const;
68

69
    Kerfuffle::ArchiveEntry entryForIndex(const QModelIndex &index);
70
    int childCount(const QModelIndex &index, int &dirs, int &files) const;
71

72 73
    Kerfuffle::ExtractJob* extractFile(const QVariant& fileName, const QString& destinationDir, const Kerfuffle::ExtractionOptions& options = Kerfuffle::ExtractionOptions()) const;
    Kerfuffle::ExtractJob* extractFiles(const QList<QVariant>& files, const QString& destinationDir, const Kerfuffle::ExtractionOptions& options = Kerfuffle::ExtractionOptions()) const;
74

75 76 77 78
    Kerfuffle::PreviewJob* preview(const QString& file) const;
    Kerfuffle::OpenJob* open(const QString& file) const;
    Kerfuffle::OpenWithJob* openWith(const QString& file) const;

79 80
    Kerfuffle::AddJob* addFiles(const QStringList & paths, const Kerfuffle::CompressionOptions& options = Kerfuffle::CompressionOptions());
    Kerfuffle::DeleteJob* deleteFiles(const QList<QVariant> & files);
81

82 83 84 85 86
    /**
     * @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);
87

88 89 90 91 92 93 94 95
signals:
    void loadingStarted();
    void loadingFinished(KJob *);
    void extractionFinished(bool success);
    void error(const QString& error, const QString& details);
    void droppedFiles(const QStringList& files, const QString& path = QString());

private slots:
96 97
    void slotNewEntryFromSetArchive(const ArchiveEntry& entry);
    void slotNewEntry(const ArchiveEntry& entry);
98 99
    void slotLoadingFinished(KJob *job);
    void slotEntryRemoved(const QString & path);
100
    void slotUserQuery(Kerfuffle::Query *query);
101 102 103
    void slotCleanupEmptyDirs();

private:
104 105 106 107 108 109 110 111 112 113 114
    /**
     * 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);

115
    ArchiveDirNode* parentFor(const Kerfuffle::ArchiveEntry& entry);
116 117 118 119 120 121 122 123 124
    QModelIndex indexForNode(ArchiveNode *node);
    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.
     */
    enum InsertBehaviour { NotifyViews, DoNotNotifyViews };
    void insertNode(ArchiveNode *node, InsertBehaviour behaviour = NotifyViews);
125
    void newEntry(const Kerfuffle::ArchiveEntry& entry, InsertBehaviour behaviour);
126

127
    QList<Kerfuffle::ArchiveEntry> m_newArchiveEntries; // holds entries from opening a new archive until it's totally open
128
    QList<int> m_showColumns;
129
    QScopedPointer<Kerfuffle::Archive> m_archive;
130
    ArchiveDirNode *m_rootNode;
131 132

    QString m_dbusPathName;
133 134 135
};

#endif // ARCHIVEMODEL_H