forked from sqlitebrowser/sqlitebrowser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoteModel.h
More file actions
117 lines (88 loc) · 4.33 KB
/
RemoteModel.h
File metadata and controls
117 lines (88 loc) · 4.33 KB
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
#ifndef REMOTEMODEL_H
#define REMOTEMODEL_H
#include <QAbstractItemModel>
#include <QStringList>
#include <json.hpp>
class RemoteDatabase;
// List of fields stored in the JSON data
enum RemoteModelColumns
{
RemoteModelColumnName,
RemoteModelColumnType,
RemoteModelColumnUrl,
RemoteModelColumnCommitId,
RemoteModelColumnSize,
RemoteModelColumnLastModified,
RemoteModelColumnCount
};
class RemoteModelItem
{
public:
explicit RemoteModelItem(RemoteModelItem* parent = nullptr);
~RemoteModelItem();
QVariant value(RemoteModelColumns column) const;
void setValue(RemoteModelColumns column, QVariant value);
bool fetchedDirectoryList() const;
void setFetchedDirectoryList(bool fetched);
void appendChild(RemoteModelItem* item);
RemoteModelItem* child(int row) const;
RemoteModelItem* parent() const;
int childCount() const;
int row() const;
// This function assumes the JSON value it's getting passed is an array ("[{...}, {...}, {...}, ...]"). It returns a list of model items, one
// per array entry and each with the specified parent set.
static std::vector<RemoteModelItem*> loadArray(const nlohmann::json& array, RemoteModelItem* parent = nullptr);
private:
// These are just the fields from the json objects returned by the dbhub.io server
QVariant m_values[RemoteModelColumnCount];
// Child items and parent item
std::vector<RemoteModelItem*> m_children;
RemoteModelItem* m_parent;
// Indicates whether we already tried fetching a directory listing for this item. This serves two purposes:
// 1) When having an empty directory this allows us to remove the expandable flag for this item.
// 2) Between sending a network request and getting the reply this flag is already set, avoiding a second or third request being sent in the meantime.
bool m_fetchedDirectoryList;
};
class RemoteModel : public QAbstractItemModel
{
Q_OBJECT
public:
explicit RemoteModel(QObject* parent, RemoteDatabase& remote);
~RemoteModel() override;
void setNewRootDir(const QString& url, const QString& cert);
QModelIndex index(int row, int column,const QModelIndex& parent = QModelIndex()) const override;
QModelIndex parent(const QModelIndex& index) const override;
QVariant data(const QModelIndex& index, int role) const override;
QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
int rowCount(const QModelIndex& parent = QModelIndex()) const override;
int columnCount(const QModelIndex& parent = QModelIndex()) const override;
bool hasChildren(const QModelIndex& parent) const override;
bool canFetchMore(const QModelIndex& parent) const override;
void fetchMore(const QModelIndex& parent) override;
// This helper function takes a model index and returns the according model item. An invalid model index is used to indicate the
// root item, so if the index is invalid the root item is returned. This means that if you need to check for actual invalid indices
// this needs to be done prior to calling this function.
const RemoteModelItem* modelIndexToItem(const QModelIndex& idx) const;
// Returns the current client certificate
const QString& currentClientCertificate() const;
signals:
// This signal is emitted whenever a directory listing has been received and parsed
void directoryListingParsed(QModelIndex parent);
private slots:
// This is called whenever a network reply containing a directory listing arrives. json contains the reply data, userdata
// contains some custom data passed to the request. In this case we expect this to be the model index of the parent tree item.
void parseDirectoryListing(const QString& text, const QVariant& userdata);
private:
// Pointer to the root item. This contains all the actual item data.
RemoteModelItem* rootItem;
// The header list is a list of column titles. It's a static list that's getting filled in the constructor.
QStringList headerList;
// Reference to the remote database object which is stored somewhere in the main window.
RemoteDatabase& remoteDatabase;
// This stores the currently used network identity so it can be used for further requests, e.g. for
// lazy population.
QString currentRootDirectory;
QString currentClientCert;
QString currentUserName;
};
#endif