summaryrefslogtreecommitdiff
path: root/view/sharedcache/ui/dscwidget.h
blob: 0d5b9e0b6ce77838107acd0f04b1543672a1ba19 (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//
// by kat // 9/15/22.
//

#ifndef SHAREDCACHE_DSCSIDEBARWIDGET_H
#define SHAREDCACHE_DSCSIDEBARWIDGET_H

#include <QtCore/QAbstractItemModel>
#include <QtCore/QSortFilterProxyModel>
#include <QtWidgets/QTreeView>

#include "ui/filter.h"
#include "ui/sidebar.h"
#include "ui/uitypes.h"
#include <binaryninjaapi.h>
#include <sharedcacheapi.h>

#include <mutex>


class DSCContentsModel;

class DSCFilterModel;

class DSCSidebarView;

enum ModelItemType {
    FolderModelItem,
    ImageModelItem
};

class DSCContentsModelItem {
    friend class ComponentModel;

    friend class ComponentFilterModel;

    friend class DSCSidebarView;

    ModelItemType m_type;

    DSCContentsModelItem *m_parent;
    std::vector<DSCContentsModelItem *> m_children;

    BinaryViewRef m_bv;

    std::string m_name;
    std::string m_installName; // only set on images, not dirs

    bool m_hasDataVar = false;
    BinaryNinja::DataVariable m_dataVar;

public:
    explicit DSCContentsModelItem(DSCContentsModelItem *parent = nullptr);

    explicit DSCContentsModelItem(BinaryViewRef, std::string, std::string, DSCContentsModelItem *parent = nullptr);

    /// Get the "name" that should be displayed for an item.
    QString displayName() const;

    size_t childCount() const;

    DSCContentsModelItem *child(size_t);

    void addChild(DSCContentsModelItem *);

    DSCContentsModelItem *parent() const;

    size_t row() const;

    QVariant data(int column) const;

    QImage icon() const;
};

class DSCContentsModel : public QAbstractItemModel {
Q_OBJECT

    BinaryViewRef m_bv;
    SharedCacheAPI::SCRef<SharedCacheAPI::SharedCache> m_cache;
    DSCContentsModelItem *m_root;

    std::unordered_map<std::string, DSCContentsModelItem *> m_dscItems;

    std::mutex m_updateMutex;

    void refresh();

public:
    enum Column : int {
        NameColumn = 0,
        AddressColumn,
        KindColumn,
    };

    DSCContentsModel(BinaryViewRef, QObject *parent = nullptr);

    QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;

    QModelIndex parent(const QModelIndex &) const override;

    QVariant headerData(int, Qt::Orientation, int role = Qt::DisplayRole) const override;

    QVariant data(const QModelIndex &, int role = Qt::DisplayRole) const override;

    bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;

    Qt::ItemFlags flags(const QModelIndex &) const override;

    int rowCount(const QModelIndex &parent = QModelIndex()) const override;

    int columnCount(const QModelIndex &parent = QModelIndex()) const override;

    Qt::DropActions supportedDropActions() const override;

};

/// Filtering model to wrap a `ComponentModel`.
class DSCFilterModel : public QSortFilterProxyModel {
Q_OBJECT

    DSCContentsModel *m_model;

public:
    DSCFilterModel(BinaryViewRef, QObject *parent = nullptr);

    [[nodiscard]] bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override;
};

class DSCSidebarView : public QTreeView {
    BinaryViewRef m_data;
    ViewFrame *m_frame;
    QWidget *m_parent;

    void navigateToIndex(const QModelIndex &);

    QMenu *createContextMenu();

public:
    DSCSidebarView(ViewFrame *, BinaryViewRef, QWidget *parent = nullptr);
};

class DSCSidebarWidget : public SidebarWidget, public FilterTarget {
Q_OBJECT

    friend DSCSidebarView;

    BinaryViewRef m_data;
    ViewFrame *m_frame;
    QWidget *m_header;

    DSCSidebarView *m_tree;

    QSortFilterProxyModel *m_model;

    FilterEdit *m_filterEdit;
    FilteredView *m_filterView;

public:
    DSCSidebarWidget(ViewFrame *, BinaryViewRef);

    QWidget *headerWidget() override;

    void focus() override;

    void setFilter(const std::string &) override;

    void scrollToFirstItem() override;

    void scrollToCurrentItem() override;

    void selectFirstItem() override;

    void activateFirstItem() override;
};

class DSCSidebarWidgetType : public SidebarWidgetType {
public:
    DSCSidebarWidgetType();

    bool ValidForView(BinaryNinja::BinaryView* view)
    {
        if (!view)
            return false;
        return (view->GetTypeName() == VIEW_NAME);
    }

    SidebarWidget *createWidget(ViewFrame *, BinaryViewRef) override;
};

#endif //SHAREDCACHE_DSCSIDEBARWIDGET_H