forked from QupZilla/qupzilla
/
bookmarksmodel.cpp
558 lines (478 loc) · 14.7 KB
/
bookmarksmodel.cpp
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/* ============================================================
* QupZilla - WebKit based browser
* Copyright (C) 2010-2012 David Rosca <nowrep@gmail.com>
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
* ============================================================ */
#include "bookmarksmodel.h"
#include "tabbedwebview.h"
#include "iconprovider.h"
#include "databasewriter.h"
#include "mainapplication.h"
#include "settings.h"
#include <QBuffer>
// SQLite DB -> table bookmarks + folders
// Unique in bookmarks table is id
// However from bookmark icon, it is not possible to add more than one bookmark
// Only from Ctrl+D dialog it is possible
BookmarksModel::BookmarksModel(QObject* parent)
: QObject(parent)
{
loadSettings();
}
void BookmarksModel::loadSettings()
{
Settings settings;
settings.beginGroup("Bookmarks");
m_showMostVisited = settings.value("showMostVisited", true).toBool();
m_showOnlyIconsInToolbar = settings.value("showOnlyIconsInToolbar", false).toBool();
m_lastFolder = settings.value("LastFolder", "unsorted").toString();
settings.endGroup();
}
void BookmarksModel::setShowingMostVisited(bool state)
{
Settings settings;
settings.beginGroup("Bookmarks");
settings.setValue("showMostVisited", state);
settings.endGroup();
m_showMostVisited = state;
}
void BookmarksModel::setShowingOnlyIconsInToolbar(bool state)
{
Settings settings;
settings.beginGroup("Bookmarks");
settings.setValue("showOnlyIconsInToolbar", state);
settings.endGroup();
m_showOnlyIconsInToolbar = state;
}
bool BookmarksModel::isFolder(const QString &name)
{
if (name == "bookmarksToolbar" || name == "bookmarksMenu" || name == "unsorted") {
return true;
}
QSqlQuery query;
query.prepare("SELECT name FROM folders WHERE name = ?");
query.bindValue(0, name);
query.exec();
return query.next();
}
void BookmarksModel::setLastFolder(const QString &folder)
{
Settings settings;
settings.beginGroup("Bookmarks");
settings.setValue("lastFolder", folder);
settings.endGroup();
m_lastFolder = folder;
}
bool BookmarksModel::isBookmarked(const QUrl &url)
{
QSqlQuery query;
query.prepare("SELECT count(id) FROM bookmarks WHERE url=?");
query.bindValue(0, url.toString());
query.exec();
query.next();
return query.value(0).toInt() > 0;
}
// Bookmark search priority:
// Bookmarks in menu > bookmarks in toolbar -> user folders and unsorted
int BookmarksModel::bookmarkId(const QUrl &url)
{
QSqlQuery query;
query.prepare("SELECT id FROM bookmarks WHERE url=? AND folder='bookmarksMenu' ");
query.bindValue(0, url.toString());
query.exec();
if (query.next()) {
return query.value(0).toInt();
}
query.prepare("SELECT id FROM bookmarks WHERE url=? AND folder='bookmarksToolbar' ");
query.bindValue(0, url.toString());
query.exec();
if (query.next()) {
return query.value(0).toInt();
}
query.prepare("SELECT id FROM bookmarks WHERE url=? ");
query.bindValue(0, url.toString());
query.exec();
if (query.next()) {
return query.value(0).toInt();
}
return -1;
}
int BookmarksModel::bookmarkId(const QUrl &url, const QString &title, const QString &folder)
{
QSqlQuery query;
query.prepare("SELECT id FROM bookmarks WHERE url=? AND title=? AND folder=? ");
query.bindValue(0, url.toString());
query.bindValue(1, title);
query.bindValue(2, folder);
query.exec();
if (query.next()) {
return query.value(0).toInt();
}
return -1;
}
BookmarksModel::Bookmark BookmarksModel::getBookmark(int id)
{
Bookmark bookmark;
QSqlQuery query;
query.prepare("SELECT url, title, folder, icon FROM bookmarks WHERE id=?");
query.bindValue(0, id);
query.exec();
if (query.next()) {
bookmark.id = id;
bookmark.url = query.value(0).toUrl();
bookmark.title = query.value(1).toString();
bookmark.folder = query.value(2).toString();
bookmark.image = QImage::fromData(query.value(3).toByteArray());
bookmark.inSubfolder = isSubfolder(bookmark.folder);
}
return bookmark;
}
bool BookmarksModel::saveBookmark(const QUrl &url, const QString &title, const QIcon &icon, const QString &folder)
{
if (url.isEmpty() || title.isEmpty() || folder.isEmpty()) {
return false;
}
QImage image = icon.pixmap(16, 16).toImage();
if (image.isNull()) {
image = qIconProvider->emptyWebImage();
}
if (!isFolder(folder)) {
createFolder(folder);
}
QSqlQuery query;
query.prepare("INSERT INTO bookmarks (url, title, folder, icon) VALUES (?,?,?,?)");
query.bindValue(0, url.toString());
query.bindValue(1, title);
query.bindValue(2, folder);
QByteArray ba;
QBuffer buffer(&ba);
buffer.open(QIODevice::WriteOnly);
image.save(&buffer, "PNG");
query.bindValue(3, buffer.data());
query.exec();
Bookmark bookmark;
bookmark.id = query.lastInsertId().toInt();
bookmark.url = url;
bookmark.title = title;
bookmark.folder = folder;
bookmark.image = image;
bookmark.inSubfolder = isSubfolder(bookmark.folder);
setLastFolder(folder);
emit bookmarkAdded(bookmark);
mApp->sendMessages(Qz::AM_BookmarksChanged, true);
return true;
}
bool BookmarksModel::saveBookmark(WebView* view, QString folder)
{
if (folder.isEmpty()) {
folder = m_lastFolder;
}
return saveBookmark(view->url(), view->title(), view->icon(), folder);
}
void BookmarksModel::removeBookmark(int id)
{
QList<int> list;
list.append(id);
return removeBookmark(list);
}
void BookmarksModel::removeBookmark(const QList<int> list)
{
QSqlDatabase db = QSqlDatabase::database();
db.transaction();
foreach(int id, list) {
QSqlQuery query;
query.prepare("SELECT url, title, folder FROM bookmarks WHERE id = ?");
query.bindValue(0, id);
query.exec();
if (!query.next()) {
continue;
}
Bookmark bookmark;
bookmark.id = id;
bookmark.url = query.value(0).toUrl();
bookmark.title = query.value(1).toString();
bookmark.folder = query.value(2).toString();
bookmark.image = QImage::fromData(query.value(3).toByteArray());
bookmark.inSubfolder = isSubfolder(bookmark.folder);
query.prepare("DELETE FROM bookmarks WHERE id=?");
query.addBindValue(id);
query.exec();
if (!query.exec()) {
continue;
}
emit bookmarkDeleted(bookmark);
}
db.commit();
mApp->sendMessages(Qz::AM_BookmarksChanged, true);
}
void BookmarksModel::removeBookmark(const QUrl &url)
{
removeBookmark(bookmarkId(url));
}
void BookmarksModel::removeBookmark(WebView* view)
{
removeBookmark(bookmarkId(view->url()));
}
//bool BookmarksModel::editBookmark(int id, const QString &title, const QString &folder)
//{
// QSqlQuery query;
// query.prepare("UPDATE bookmarks SET title=?, folder=? WHERE id=?");
// query.bindValue(0, title);
// query.bindValue(1, folder);
// query.bindValue(2, id);
// return query.exec();
//}
//bool BookmarksModel::editBookmark(int id, const QUrl &url, const QString &title)
//{
// QSqlQuery query;
// query.prepare("UPDATE bookmarks SET title=?, url=? WHERE id=?");
// query.bindValue(0, title);
// query.bindValue(1, url.toString());
// query.bindValue(2, id);
// return query.exec();
//}
bool BookmarksModel::editBookmark(int id, const QString &title, const QUrl &url, const QString &folder)
{
if (title.isEmpty() && url.isEmpty() && folder.isEmpty()) {
return false;
}
QSqlQuery query;
query.prepare("SELECT title, url, folder, icon FROM bookmarks WHERE id=?");
query.addBindValue(id);
query.exec();
if (!query.next()) {
return false;
}
Bookmark before;
before.id = id;
before.title = query.value(0).toString();
before.url = query.value(1).toUrl();
before.folder = query.value(2).toString();
before.image = QImage::fromData(query.value(3).toByteArray());
before.inSubfolder = isSubfolder(before.folder);
Bookmark after;
after.id = id;
after.title = title.isEmpty() ? before.title : title;
after.url = url.isEmpty() ? before.url : url;
after.folder = folder.isEmpty() ? before.folder : folder;
after.image = before.image;
after.inSubfolder = isSubfolder(after.folder);
query.prepare("UPDATE bookmarks SET title=?, url=?, folder=? WHERE id = ?");
query.bindValue(0, after.title);
query.bindValue(1, after.url.toString());
query.bindValue(2, after.folder);
query.bindValue(3, id);
if (!query.exec()) {
return false;
}
emit bookmarkEdited(before, after);
mApp->sendMessages(Qz::AM_BookmarksChanged, true);
return true;
}
bool BookmarksModel::changeIcon(int id, const QIcon &icon)
{
QSqlQuery query;
query.prepare("SELECT title, url, folder, icon FROM bookmarks WWHERE id=?");
query.addBindValue(id);
query.exec();
if (!query.next()) {
return false;
}
Bookmark before;
before.id = id;
before.title = query.value(0).toString();
before.url = query.value(1).toUrl();
before.folder = query.value(2).toString();
before.image = QImage::fromData(query.value(3).toByteArray());
before.inSubfolder = isSubfolder(before.folder);
Bookmark after = before;
after.image = icon.pixmap(16).toImage();
query.prepare("UPDATE bookmarks SET icon = ? WHERE id = ?");
QByteArray ba;
QBuffer buffer(&ba);
buffer.open(QIODevice::WriteOnly);
after.image.save(&buffer, "PNG");
query.bindValue(0, buffer.data());
query.bindValue(1, id);
if (!query.exec()) {
return false;
}
emit bookmarkEdited(before, after);
mApp->sendMessages(Qz::AM_BookmarksChanged, true);
return true;
}
void BookmarksModel::countUpBookmark(int id)
{
QSqlQuery query;
query.prepare("UPDATE bookmarks SET count = count + 1 WHERE id=?");
query.addBindValue(id);
query.exec();
}
bool BookmarksModel::createFolder(const QString &name)
{
if (isFolder(name)) {
return false;
}
QSqlQuery query;
query.prepare("INSERT INTO folders (name, subfolder) VALUES (?, 'no')");
query.bindValue(0, name);
if (!query.exec()) {
return false;
}
emit folderAdded(name);
mApp->sendMessages(Qz::AM_BookmarksChanged, true);
return true;
}
void BookmarksModel::removeFolder(const QString &name)
{
if (name == _bookmarksMenu || name == _bookmarksToolbar) {
return;
}
QSqlQuery query;
query.prepare("SELECT id FROM bookmarks WHERE folder = ? ");
query.bindValue(0, name);
if (!query.exec()) {
return;
}
QList<int> list;
while (query.next()) {
list.append(query.value(0).toInt());
}
removeBookmark(list);
query.prepare("DELETE FROM folders WHERE name=?");
query.bindValue(0, name);
query.exec();
emit folderDeleted(name);
mApp->sendMessages(Qz::AM_BookmarksChanged, true);
}
bool BookmarksModel::renameFolder(const QString &before, const QString &after)
{
QSqlQuery query;
query.prepare("SELECT name FROM folders WHERE name = ?");
query.bindValue(0, after);
query.exec();
if (query.next()) {
return false;
}
query.prepare("UPDATE folders SET name=? WHERE name=?");
query.bindValue(0, after);
query.bindValue(1, before);
if (!query.exec()) {
return false;
}
query.prepare("UPDATE bookmarks SET folder=? WHERE folder=?");
query.bindValue(0, after);
query.bindValue(1, before);
if (!query.exec()) {
return false;
}
emit folderRenamed(before, after);
return true;
}
QList<Bookmark> BookmarksModel::folderBookmarks(const QString &name)
{
QList<Bookmark> list;
QSqlQuery query;
query.prepare("SELECT id, url, title, folder, icon FROM bookmarks WHERE folder=?");
query.addBindValue(name);
query.exec();
while (query.next()) {
Bookmark bookmark;
bookmark.id = query.value(0).toInt();
bookmark.url = query.value(1).toUrl();
bookmark.title = query.value(2).toString();
bookmark.folder = query.value(3).toString();
bookmark.image = QImage::fromData(query.value(4).toByteArray());
bookmark.inSubfolder = isSubfolder(bookmark.folder);
list.append(bookmark);
}
return list;
}
bool BookmarksModel::createSubfolder(const QString &name)
{
if (isFolder(name)) {
return false;
}
QSqlQuery query;
query.prepare("INSERT INTO folders (name, subfolder) VALUES (?, 'yes')");
query.bindValue(0, name);
if (!query.exec()) {
return false;
}
emit subfolderAdded(name);
mApp->sendMessages(Qz::AM_BookmarksChanged, true);
return true;
}
bool BookmarksModel::isSubfolder(const QString &name)
{
QSqlQuery query;
query.prepare("SELECT subfolder FROM folders WHERE name = ?");
query.bindValue(0, name);
query.exec();
if (!query.next()) {
return false;
}
return query.value(0).toString() == "yes";
}
bool BookmarksModel::bookmarksEqual(const Bookmark &one, const Bookmark &two)
{
if (one.id != two.id) {
return false;
}
if (one.title != two.title) {
return false;
}
if (one.folder != two.folder) {
return false;
}
if (one.url != two.url) {
return false;
}
return true;
}
QString BookmarksModel::toTranslatedFolder(const QString &name)
{
QString trFolder;
if (name == "bookmarksMenu") {
trFolder = tr("Bookmarks In Menu");
}
else if (name == "bookmarksToolbar") {
trFolder = tr("Bookmarks In ToolBar");
}
else if (name == "unsorted") {
trFolder = tr("Unsorted Bookmarks");
}
else {
trFolder = name;
}
return trFolder;
}
QString BookmarksModel::fromTranslatedFolder(const QString &name)
{
QString folder;
if (name == tr("Bookmarks In Menu")) {
folder = "bookmarksMenu";
}
else if (name == tr("Bookmarks In ToolBar")) {
folder = "bookmarksToolbar";
}
else if (name == tr("Unsorted Bookmarks")) {
folder = "unsorted";
}
else {
folder = name;
}
return folder;
}