aboutsummaryrefslogtreecommitdiffstats
path: root/ui/qt/models/profile_model.h
blob: 71ab76241943d94ff7708c0ed2c1da3f7b8ed427 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* profile_model.h
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#ifndef PROFILE_MODEL_H
#define PROFILE_MODEL_H

#include "config.h"
#include "glib.h"

#include <ui/profile.h>

#include <QAbstractTableModel>
#include <QSortFilterProxyModel>
#include <QLoggingCategory>
#include <QFileInfoList>

Q_DECLARE_LOGGING_CATEGORY(profileLogger)

class ProfileSortModel : public QSortFilterProxyModel
{
    Q_OBJECT
public:
    ProfileSortModel(QObject *parent = Q_NULLPTR);

    enum FilterType {
        AllProfiles = 0,
        PersonalProfiles,
        GlobalProfiles
    };

    void setFilterType(FilterType ft);
    void setFilterString(QString txt = QString());

    static QStringList filterTypes();

protected:
    virtual bool lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const;
    virtual bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const;

private:
    FilterType ft_;
    QString ftext_;
};

class ProfileModel : public QAbstractTableModel
{
    Q_OBJECT

public:
    explicit ProfileModel(QObject * parent = Q_NULLPTR);

    enum {
        COL_NAME,
        COL_TYPE,
        _LAST_ENTRY
    } columns_;

    enum {
        DATA_STATUS = Qt::UserRole,
        DATA_IS_DEFAULT,
        DATA_IS_GLOBAL,
        DATA_IS_SELECTED,
        DATA_PATH,
        DATA_PATH_IS_NOT_DESCRIPTION,
        DATA_INDEX_VALUE_IS_URL
    } data_values_;

    // QAbstractItemModel interface
    virtual int rowCount(const QModelIndex & parent = QModelIndex()) const;
    virtual int columnCount(const QModelIndex & parent = QModelIndex()) const;
    virtual QVariant data(const QModelIndex & idx, int role = Qt::DisplayRole) const;
    virtual bool setData(const QModelIndex &index, const QVariant &value, int role);
    virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
    virtual Qt::ItemFlags flags(const QModelIndex &index) const;

    void deleteEntry(QModelIndex idx);
    void deleteEntries(QModelIndexList idcs);

    int findByName(QString name);
    QModelIndex addNewProfile(QString name);
    QModelIndex duplicateEntry(QModelIndex idx, int new_status = PROF_STAT_COPY);

    void doResetModel(bool reset_import = false);
    bool resetDefault() const;

    QModelIndex activeProfile() const;

    GList * at(int row) const;

    bool changesPending() const;
    bool importPending() const;

    bool userProfilesExist() const;

#ifdef HAVE_MINIZIP
    bool exportProfiles(QString filename, QModelIndexList items, QString * err = Q_NULLPTR);
    int importProfilesFromZip(QString filename, int *skippedCnt = Q_NULLPTR, QStringList *result = Q_NULLPTR);
#endif
    int importProfilesFromDir(QString filename, int *skippedCnt = Q_NULLPTR, bool fromZip = false, QStringList *result = Q_NULLPTR);

    static bool checkNameValidity(QString name, QString *msg = Q_NULLPTR);
    QList<int> findAllByNameAndVisibility(QString name, bool isGlobal = false, bool searchReference = false);
    void markAsImported(QStringList importedItems);
    bool clearImported(QString *msg = Q_NULLPTR);

    int lastSetRow() const;

Q_SIGNALS:
    void itemChanged(const QModelIndex &idx);

private:
    QList<profile_def *> profiles_;
    QStringList profile_files_;
    QString set_profile_;
    bool reset_default_;
    bool profiles_imported_;

    int last_set_row_;

    void loadProfiles();
    profile_def * guard(const QModelIndex &index) const;
    profile_def * guard(int row) const;
    GList * entry(profile_def *) const;

    int findByNameAndVisibility(QString name, bool isGlobal = false, bool searchReference = false);

#ifdef HAVE_MINIZIP
    static bool acceptFile(QString fileName, int fileSize);
#endif

    QVariant dataDisplay(const QModelIndex & idx) const;
    QVariant dataFontRole(const QModelIndex & idx) const;
    QVariant dataBackgroundRole(const QModelIndex & idx) const;
    QVariant dataToolTipRole(const QModelIndex & idx) const;
    QVariant dataPath(const QModelIndex & idx) const;

#ifdef HAVE_MINIZIP
    QStringList exportFileList(QModelIndexList items);
#endif
    bool copyTempToProfile(QString tempPath, QString profilePath, bool *wasEmpty = Q_NULLPTR);
    QFileInfoList filterProfilePath(QString, QFileInfoList ent, bool fromZip);
    QFileInfoList uniquePaths(QFileInfoList lst);

};

#endif

/*
 * Editor modelines
 *
 * Local Variables:
 * c-basic-offset: 4
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * ex: set shiftwidth=4 tabstop=8 expandtab:
 * :indentSize=4:tabSize=8:noTabs=true:
 */