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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
|
/*
* This file is part of smolbote. It's copyrighted by the contributors recorded
* in the version control history of the file, available from its original
* location: https://neueland.iserlohn-fortress.net/gitea/aqua/smolbote
*
* SPDX-License-Identifier: GPL-3.0
*/
#include "bookmarkmodel.h"
#include "bookmarkitem.h"
#include "xbel.h"
#include <QBuffer>
#include <QMimeData>
#include <QRegularExpression>
BookmarkModel::BookmarkModel(QObject *parent)
: QAbstractItemModel(parent)
{
rootItem = new BookmarkItem({ tr("Title"), tr("Address") }, BookmarkItem::Root, nullptr);
}
BookmarkModel::~BookmarkModel()
{
delete rootItem;
}
QVariant BookmarkModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if(orientation == Qt::Horizontal && role == Qt::DisplayRole)
return rootItem->data(static_cast<BookmarkItem::Fields>(section));
return QVariant();
}
QVariant BookmarkModel::data(const QModelIndex &index, int role) const
{
if(!index.isValid())
return QVariant();
if(role == Qt::DecorationRole && index.column() == 0)
return static_cast<BookmarkItem *>(index.internalPointer())->icon();
else if(role == Qt::ToolTipRole)
return static_cast<BookmarkItem *>(index.internalPointer())->tooltip();
else if(role == Qt::DisplayRole)
return static_cast<BookmarkItem *>(index.internalPointer())->data(static_cast<BookmarkItem::Fields>(index.column()));
else
return QVariant();
}
QVariant BookmarkModel::data(const QModelIndex &index, int column, int role) const
{
if(!index.isValid())
return QVariant();
if(role == Qt::DisplayRole)
return static_cast<BookmarkItem *>(index.internalPointer())->data(static_cast<BookmarkItem::Fields>(column));
return QVariant();
}
bool BookmarkModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if(!index.isValid())
return false;
bool success = false;
if(role == Qt::DisplayRole) {
success = static_cast<BookmarkItem *>(index.internalPointer())->setData(static_cast<BookmarkItem::Fields>(index.column()), value);
}
if(success)
emit dataChanged(index, index, { role });
return success;
}
bool BookmarkModel::setData(const QModelIndex &index, const QVariant &value, BookmarkItem::Fields column, int role)
{
if(!index.isValid() || role != Qt::DisplayRole)
return false;
bool success = static_cast<BookmarkItem *>(index.internalPointer())->setData(column, value);
if(success)
emit dataChanged(index, index, { role });
return success;
}
Qt::ItemFlags BookmarkModel::flags(const QModelIndex &index) const
{
if(getItem(index)->type() == BookmarkItem::Folder)
return QAbstractItemModel::flags(index) | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
else
return QAbstractItemModel::flags(index) | Qt::ItemIsDragEnabled | Qt::ItemNeverHasChildren;
}
bool BookmarkModel::isItemExpanded(const QModelIndex &index) const
{
if(!index.isValid())
return false;
return static_cast<BookmarkItem *>(index.internalPointer())->isExpanded();
}
int BookmarkModel::rowCount(const QModelIndex &index) const
{
if(index.column() > 0)
return 0;
return getItem(index)->childCount();
}
bool BookmarkModel::appendBookmark(const QString &title, const QString &url, const QModelIndex &parent)
{
auto *parentItem = getItem(parent);
int row = parentItem->childCount();
beginInsertRows(parent, row, row);
parentItem->appendChild(new BookmarkItem({ title, url }, BookmarkItem::Bookmark, parentItem));
endInsertRows();
return true;
}
bool BookmarkModel::removeRows(int position, int rows, const QModelIndex &parent)
{
qDebug("removeRows: pos=%i rows=%i", position, rows);
auto *parentItem = getItem(parent);
beginRemoveRows(parent, position, position + rows - 1);
bool success = parentItem->removeChildAt(position, rows);
endRemoveRows();
return success;
}
int BookmarkModel::columnCount(const QModelIndex &index) const
{
Q_UNUSED(index);
return 2;
}
QModelIndex BookmarkModel::index(int row, int column, const QModelIndex &parent) const
{
if(!this->hasIndex(row, column, parent))
return QModelIndex();
BookmarkItem *parentItem = getItem(parent);
BookmarkItem *childItem = parentItem->child(row);
if(childItem)
return createIndex(row, column, childItem);
else
return QModelIndex();
}
QModelIndex BookmarkModel::parent(const QModelIndex &index) const
{
if(!index.isValid())
return QModelIndex();
auto *childItem = static_cast<BookmarkItem *>(index.internalPointer());
auto *parentItem = childItem->parent();
if(parentItem == rootItem)
return QModelIndex();
return createIndex(parentItem->row(), 0, parentItem);
}
inline bool has(const QStringList &terms, const QStringList &where)
{
for(const QString &term : terms) {
if(where.contains(term))
return true;
}
return false;
}
inline QStringList searchThrough(const QString &term, const QStringList &tags, BookmarkItem *item)
{
QStringList results;
for(int i = 0; i < item->childCount(); ++i) {
auto *child = item->child(i);
if(child->type() == BookmarkItem::Bookmark) {
if((!term.isEmpty() && child->data(BookmarkItem::Href).toString().contains(term)) || has(tags, child->data(BookmarkItem::Tags).toStringList()))
results.append(child->data(BookmarkItem::Href).toString());
}
else if(child->type() == BookmarkItem::Folder) {
if(has(tags, child->data(BookmarkItem::Tags).toStringList())) {
// append all bookmarks
for(int i = 0; i < child->childCount(); ++i) {
auto *subChild = child->child(i);
if(subChild->type() == BookmarkItem::Bookmark)
results.append(subChild->data(BookmarkItem::Href).toString());
}
}
results.append(searchThrough(term, tags, child));
}
}
return results;
}
QStringList BookmarkModel::search(const QString &term) const
{
QString searchTerm = term;
QStringList tags;
const QRegularExpression tagRE(":\\w+\\s?", QRegularExpression::CaseInsensitiveOption);
auto i = tagRE.globalMatch(term);
while(i.hasNext()) {
auto match = i.next();
QString tag = match.captured();
searchTerm.remove(tag);
tag = tag.remove(0, 1).trimmed();
tags.append(tag);
}
return searchThrough(searchTerm, tags, rootItem);
}
BookmarkItem *BookmarkModel::getItem(const QModelIndex &index) const
{
if(!index.isValid())
return rootItem;
else
return static_cast<BookmarkItem *>(index.internalPointer());
}
/*
* Drag'n'Drop implementation
* How drag and drop actually works: the view encodes the data of the original
* item (through ::mimeData), and then uses ::dropMimeData to create the new
* item. If successful, the old item is removed (through ::removeRows).
* This means that the encoding and decoding needs to be provided. In this case,
* this is done through xbel.
*/
Qt::DropActions BookmarkModel::supportedDropActions() const
{
return Qt::MoveAction;
}
QStringList BookmarkModel::mimeTypes() const
{
return { mimeType };
}
QMimeData *BookmarkModel::mimeData(const QModelIndexList &indexes) const
{
auto *mimeData = new QMimeData();
QByteArray data;
QDataStream stream(&data, QIODevice::WriteOnly);
for(const QModelIndex &index : indexes) {
if(index.isValid() && index.column() == 0) {
QByteArray encodedData;
QBuffer buffer(&encodedData);
buffer.open(QIODevice::WriteOnly);
Xbel::write(&buffer, getItem(index));
stream << encodedData;
}
}
mimeData->setData(mimeType, data);
return mimeData;
}
bool BookmarkModel::dropMimeData(const QMimeData *mimeData, Qt::DropAction action, int row, int column, const QModelIndex &parent)
{
if(action == Qt::IgnoreAction)
return true;
if(action != Qt::MoveAction)
return false;
if(!mimeData->hasFormat(mimeType) || column > 0)
return false;
QByteArray data = mimeData->data(mimeType);
QDataStream stream(&data, QIODevice::ReadOnly);
if(stream.atEnd())
return false;
while(!stream.atEnd()) {
QByteArray encodedData;
stream >> encodedData;
QBuffer buffer(&encodedData);
buffer.open(QIODevice::ReadOnly);
auto *fakeRoot = new BookmarkItem({}, BookmarkItem::Folder, nullptr);
auto *parentItem = getItem(parent);
Xbel::read(&buffer, fakeRoot);
beginInsertRows(parent, row, row + fakeRoot->childCount() - 1);
for(int i = 0; i < fakeRoot->childCount(); ++i) {
auto *child = fakeRoot->takeChild(0, parentItem);
parentItem->insertChild(row, child);
}
endInsertRows();
delete fakeRoot;
}
return true;
}
|