2011-03-03 18:29:20 +01:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
2016-02-26 10:43:34 +01:00
|
|
|
* Copyright (C) 2010-2016 David Rosca <nowrep@gmail.com>
|
2011-03-03 18:29:20 +01:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
* ============================================================ */
|
2014-02-05 15:26:51 +01:00
|
|
|
#include "bookmarks.h"
|
2014-02-06 17:07:19 +01:00
|
|
|
#include "bookmarkitem.h"
|
2014-02-07 22:48:30 +01:00
|
|
|
#include "bookmarksmodel.h"
|
2014-02-10 16:58:05 +01:00
|
|
|
#include "bookmarkstools.h"
|
2014-03-06 16:12:36 +01:00
|
|
|
#include "autosaver.h"
|
2014-03-09 21:51:42 +01:00
|
|
|
#include "datapaths.h"
|
2012-02-29 18:33:50 +01:00
|
|
|
#include "settings.h"
|
2014-03-06 16:12:36 +01:00
|
|
|
#include "qztools.h"
|
2012-02-29 18:33:50 +01:00
|
|
|
|
2013-12-30 00:35:01 +01:00
|
|
|
#include <QFile>
|
2015-12-18 11:25:45 +01:00
|
|
|
#include <QJsonDocument>
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2014-03-09 21:51:42 +01:00
|
|
|
Bookmarks::Bookmarks(QObject* parent)
|
2011-04-15 20:45:22 +02:00
|
|
|
: QObject(parent)
|
2014-03-06 16:12:36 +01:00
|
|
|
, m_autoSaver(0)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2014-03-06 16:12:36 +01:00
|
|
|
m_autoSaver = new AutoSaver(this);
|
|
|
|
connect(m_autoSaver, SIGNAL(save()), this, SLOT(saveSettings()));
|
2014-03-09 21:51:42 +01:00
|
|
|
|
|
|
|
init();
|
|
|
|
loadSettings();
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2014-02-10 12:58:34 +01:00
|
|
|
Bookmarks::~Bookmarks()
|
|
|
|
{
|
2014-03-06 16:12:36 +01:00
|
|
|
m_autoSaver->saveIfNecessary();
|
2014-02-10 12:58:34 +01:00
|
|
|
delete m_root;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Bookmarks::loadSettings()
|
|
|
|
{
|
|
|
|
Settings settings;
|
|
|
|
settings.beginGroup("Bookmarks");
|
|
|
|
m_showOnlyIconsInToolbar = settings.value("showOnlyIconsInToolbar", false).toBool();
|
2015-12-11 01:49:32 +01:00
|
|
|
m_showOnlyTextInToolbar = settings.value("showOnlyTextInToolbar", false).toBool();
|
2014-02-10 12:58:34 +01:00
|
|
|
settings.endGroup();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Bookmarks::showOnlyIconsInToolbar() const
|
|
|
|
{
|
|
|
|
return m_showOnlyIconsInToolbar;
|
|
|
|
}
|
|
|
|
|
2015-12-11 01:49:32 +01:00
|
|
|
bool Bookmarks::showOnlyTextInToolbar() const
|
|
|
|
{
|
|
|
|
return m_showOnlyTextInToolbar;
|
|
|
|
}
|
|
|
|
|
2014-02-10 12:58:34 +01:00
|
|
|
BookmarkItem* Bookmarks::rootItem() const
|
|
|
|
{
|
|
|
|
return m_root;
|
|
|
|
}
|
|
|
|
|
|
|
|
BookmarkItem* Bookmarks::toolbarFolder() const
|
|
|
|
{
|
|
|
|
return m_folderToolbar;
|
|
|
|
}
|
|
|
|
|
|
|
|
BookmarkItem* Bookmarks::menuFolder() const
|
|
|
|
{
|
|
|
|
return m_folderMenu;
|
|
|
|
}
|
|
|
|
|
|
|
|
BookmarkItem* Bookmarks::unsortedFolder() const
|
|
|
|
{
|
|
|
|
return m_folderUnsorted;
|
|
|
|
}
|
|
|
|
|
|
|
|
BookmarkItem* Bookmarks::lastUsedFolder() const
|
|
|
|
{
|
|
|
|
return m_lastFolder;
|
|
|
|
}
|
|
|
|
|
|
|
|
BookmarksModel* Bookmarks::model() const
|
|
|
|
{
|
|
|
|
return m_model;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Bookmarks::isBookmarked(const QUrl &url)
|
|
|
|
{
|
|
|
|
return !searchBookmarks(url).isEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Bookmarks::canBeModified(BookmarkItem* item) const
|
|
|
|
{
|
|
|
|
Q_ASSERT(item);
|
|
|
|
|
|
|
|
return item != m_root &&
|
|
|
|
item != m_folderToolbar &&
|
|
|
|
item != m_folderMenu &&
|
|
|
|
item != m_folderUnsorted;
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<BookmarkItem*> Bookmarks::searchBookmarks(const QUrl &url) const
|
|
|
|
{
|
|
|
|
QList<BookmarkItem*> items;
|
|
|
|
search(&items, m_root, url);
|
|
|
|
return items;
|
|
|
|
}
|
|
|
|
|
2014-02-18 16:17:22 +01:00
|
|
|
QList<BookmarkItem*> Bookmarks::searchBookmarks(const QString &string, int limit, Qt::CaseSensitivity sensitive) const
|
2014-02-10 12:58:34 +01:00
|
|
|
{
|
|
|
|
QList<BookmarkItem*> items;
|
2014-02-18 16:17:22 +01:00
|
|
|
search(&items, m_root, string, limit, sensitive);
|
2014-02-10 12:58:34 +01:00
|
|
|
return items;
|
|
|
|
}
|
|
|
|
|
2014-04-24 11:56:03 +02:00
|
|
|
QList<BookmarkItem*> Bookmarks::searchKeyword(const QString &keyword) const
|
|
|
|
{
|
|
|
|
QList<BookmarkItem*> items;
|
|
|
|
searchKeyword(&items, m_root, keyword);
|
|
|
|
return items;
|
|
|
|
}
|
|
|
|
|
2014-02-10 12:58:34 +01:00
|
|
|
void Bookmarks::addBookmark(BookmarkItem* parent, BookmarkItem* item)
|
|
|
|
{
|
|
|
|
Q_ASSERT(parent);
|
|
|
|
Q_ASSERT(parent->isFolder());
|
|
|
|
Q_ASSERT(item);
|
|
|
|
|
2014-04-16 09:12:33 +02:00
|
|
|
insertBookmark(parent, parent->children().count(), item);
|
2014-02-10 12:58:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Bookmarks::insertBookmark(BookmarkItem* parent, int row, BookmarkItem* item)
|
|
|
|
{
|
|
|
|
Q_ASSERT(parent);
|
|
|
|
Q_ASSERT(parent->isFolder());
|
|
|
|
Q_ASSERT(item);
|
|
|
|
|
|
|
|
m_lastFolder = parent;
|
|
|
|
m_model->addBookmark(parent, row, item);
|
|
|
|
emit bookmarkAdded(item);
|
2014-03-06 16:12:36 +01:00
|
|
|
|
2016-02-26 10:43:34 +01:00
|
|
|
m_autoSaver->changeOccurred();
|
2014-02-10 12:58:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Bookmarks::removeBookmark(BookmarkItem* item)
|
|
|
|
{
|
|
|
|
if (!canBeModified(item)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_model->removeBookmark(item);
|
|
|
|
emit bookmarkRemoved(item);
|
2014-03-06 16:12:36 +01:00
|
|
|
|
2016-02-26 10:43:34 +01:00
|
|
|
m_autoSaver->changeOccurred();
|
2014-02-10 12:58:34 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-03-06 16:12:36 +01:00
|
|
|
void Bookmarks::changeBookmark(BookmarkItem* item)
|
2014-02-10 12:58:34 +01:00
|
|
|
{
|
|
|
|
Q_ASSERT(item);
|
|
|
|
emit bookmarkChanged(item);
|
2014-03-06 16:12:36 +01:00
|
|
|
|
2016-02-26 10:43:34 +01:00
|
|
|
m_autoSaver->changeOccurred();
|
2014-02-10 12:58:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Bookmarks::setShowOnlyIconsInToolbar(bool state)
|
|
|
|
{
|
|
|
|
m_showOnlyIconsInToolbar = state;
|
|
|
|
emit showOnlyIconsInToolbarChanged(state);
|
2016-02-26 10:43:34 +01:00
|
|
|
m_autoSaver->changeOccurred();
|
2014-02-10 12:58:34 +01:00
|
|
|
}
|
|
|
|
|
2015-12-11 01:49:32 +01:00
|
|
|
void Bookmarks::setShowOnlyTextInToolbar(bool state)
|
|
|
|
{
|
|
|
|
m_showOnlyTextInToolbar = state;
|
|
|
|
emit showOnlyTextInToolbarChanged(state);
|
2016-02-26 10:43:34 +01:00
|
|
|
m_autoSaver->changeOccurred();
|
2015-12-11 01:49:32 +01:00
|
|
|
}
|
|
|
|
|
2014-03-06 16:12:36 +01:00
|
|
|
void Bookmarks::saveSettings()
|
|
|
|
{
|
|
|
|
Settings settings;
|
|
|
|
settings.beginGroup("Bookmarks");
|
|
|
|
settings.setValue("showOnlyIconsInToolbar", m_showOnlyIconsInToolbar);
|
2015-12-11 01:49:32 +01:00
|
|
|
settings.setValue("showOnlyTextInToolbar", m_showOnlyTextInToolbar);
|
2014-03-06 16:12:36 +01:00
|
|
|
settings.endGroup();
|
|
|
|
|
|
|
|
saveBookmarks();
|
|
|
|
}
|
|
|
|
|
2014-02-09 17:27:55 +01:00
|
|
|
void Bookmarks::init()
|
2014-02-06 17:07:19 +01:00
|
|
|
{
|
|
|
|
m_root = new BookmarkItem(BookmarkItem::Root);
|
|
|
|
|
|
|
|
m_folderToolbar = new BookmarkItem(BookmarkItem::Folder, m_root);
|
2014-02-07 22:48:30 +01:00
|
|
|
m_folderToolbar->setTitle(tr("Bookmarks Toolbar"));
|
2014-02-06 17:07:19 +01:00
|
|
|
m_folderToolbar->setDescription(tr("Bookmarks located in Bookmarks Toolbar"));
|
|
|
|
|
|
|
|
m_folderMenu = new BookmarkItem(BookmarkItem::Folder, m_root);
|
2014-02-07 22:48:30 +01:00
|
|
|
m_folderMenu->setTitle(tr("Bookmarks Menu"));
|
2014-02-06 17:07:19 +01:00
|
|
|
m_folderMenu->setDescription(tr("Bookmarks located in Bookmarks Menu"));
|
|
|
|
|
|
|
|
m_folderUnsorted = new BookmarkItem(BookmarkItem::Folder, m_root);
|
|
|
|
m_folderUnsorted->setTitle(tr("Unsorted Bookmarks"));
|
|
|
|
m_folderUnsorted->setDescription(tr("All other bookmarks"));
|
|
|
|
|
2014-02-13 16:35:52 +01:00
|
|
|
if (BookmarksTools::migrateBookmarksIfNecessary(this)) {
|
|
|
|
// Bookmarks migrated just now, let's save them ASAP
|
|
|
|
saveBookmarks();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Bookmarks don't need to be migrated, just load them as usual
|
2014-02-10 16:58:05 +01:00
|
|
|
loadBookmarks();
|
|
|
|
}
|
|
|
|
|
|
|
|
m_lastFolder = m_folderUnsorted;
|
2014-02-14 21:13:08 +01:00
|
|
|
m_model = new BookmarksModel(m_root, this, this);
|
2014-02-10 16:58:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Bookmarks::loadBookmarks()
|
|
|
|
{
|
2014-03-09 21:51:42 +01:00
|
|
|
const QString bookmarksFile = DataPaths::currentProfilePath() + QLatin1String("/bookmarks.json");
|
2014-02-09 18:55:11 +01:00
|
|
|
const QString backupFile = bookmarksFile + QLatin1String(".old");
|
2014-02-06 17:07:19 +01:00
|
|
|
|
2015-12-18 11:25:45 +01:00
|
|
|
QJsonParseError err;
|
|
|
|
QJsonDocument json = QJsonDocument::fromJson(QzTools::readAllFileByteContents(bookmarksFile), &err);
|
|
|
|
const QVariant res = json.toVariant();
|
2014-02-06 17:07:19 +01:00
|
|
|
|
2015-12-18 11:25:45 +01:00
|
|
|
if (err.error != QJsonParseError::NoError || res.type() != QVariant::Map) {
|
2016-01-24 16:37:31 +01:00
|
|
|
if (QFile(bookmarksFile).exists()) {
|
|
|
|
qWarning() << "Bookmarks::init() Error parsing bookmarks! Using default bookmarks!";
|
|
|
|
qWarning() << "Bookmarks::init() Your bookmarks have been backed up in" << backupFile;
|
|
|
|
|
|
|
|
// Backup the user bookmarks
|
|
|
|
QFile::remove(backupFile);
|
|
|
|
QFile::copy(bookmarksFile, backupFile);
|
|
|
|
}
|
2014-02-09 18:55:11 +01:00
|
|
|
|
|
|
|
// Load default bookmarks
|
2015-12-18 11:25:45 +01:00
|
|
|
json = QJsonDocument::fromJson(QzTools::readAllFileByteContents(QSL(":data/bookmarks.json")), &err);
|
|
|
|
const QVariant data = json.toVariant();
|
2014-02-09 18:55:11 +01:00
|
|
|
|
2015-12-18 11:25:45 +01:00
|
|
|
Q_ASSERT(err.error == QJsonParseError::NoError);
|
2014-02-09 18:55:11 +01:00
|
|
|
Q_ASSERT(data.type() == QVariant::Map);
|
|
|
|
|
|
|
|
loadBookmarksFromMap(data.toMap().value("roots").toMap());
|
2014-03-07 23:21:06 +01:00
|
|
|
|
|
|
|
// Don't forget to save the bookmarks
|
2016-02-26 10:43:34 +01:00
|
|
|
m_autoSaver->changeOccurred();
|
2014-02-09 18:55:11 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
loadBookmarksFromMap(res.toMap().value("roots").toMap());
|
2014-02-06 17:07:19 +01:00
|
|
|
}
|
2014-02-09 18:55:11 +01:00
|
|
|
}
|
2014-02-06 17:07:19 +01:00
|
|
|
|
|
|
|
void Bookmarks::saveBookmarks()
|
|
|
|
{
|
2014-02-08 21:11:13 +01:00
|
|
|
QVariantMap bookmarksMap;
|
2014-02-06 17:07:19 +01:00
|
|
|
|
2014-02-08 21:11:13 +01:00
|
|
|
#define WRITE_FOLDER(name, mapName, folder) \
|
|
|
|
QVariantMap mapName; \
|
|
|
|
mapName.insert("children", writeBookmarks(folder)); \
|
|
|
|
mapName.insert("expanded", folder->isExpanded()); \
|
2014-02-09 11:02:51 +01:00
|
|
|
mapName.insert("expanded_sidebar", folder->isSidebarExpanded()); \
|
2014-02-08 21:11:13 +01:00
|
|
|
mapName.insert("name", folder->title()); \
|
|
|
|
mapName.insert("description", folder->description()); \
|
|
|
|
mapName.insert("type", "folder"); \
|
|
|
|
bookmarksMap.insert(name, mapName);
|
2014-02-06 17:07:19 +01:00
|
|
|
|
2014-02-08 21:11:13 +01:00
|
|
|
WRITE_FOLDER("bookmark_bar", toolbarMap, m_folderToolbar)
|
|
|
|
WRITE_FOLDER("bookmark_menu", menuMap, m_folderMenu)
|
|
|
|
WRITE_FOLDER("other", unsortedMap, m_folderUnsorted)
|
2014-02-08 23:01:01 +01:00
|
|
|
#undef WRITE_FOLDER
|
2014-02-06 17:07:19 +01:00
|
|
|
|
|
|
|
QVariantMap map;
|
|
|
|
map.insert("version", Qz::bookmarksVersion);
|
|
|
|
map.insert("roots", bookmarksMap);
|
|
|
|
|
2015-12-18 11:25:45 +01:00
|
|
|
const QJsonDocument json = QJsonDocument::fromVariant(map);
|
|
|
|
const QByteArray data = json.toJson();
|
2014-02-06 17:07:19 +01:00
|
|
|
|
2015-12-18 11:25:45 +01:00
|
|
|
if (data.isEmpty()) {
|
2014-02-06 17:07:19 +01:00
|
|
|
qWarning() << "Bookmarks::saveBookmarks() Error serializing bookmarks!";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-09 21:51:42 +01:00
|
|
|
QFile file(DataPaths::currentProfilePath() + QLatin1String("/bookmarks.json"));
|
2014-02-06 17:07:19 +01:00
|
|
|
|
2014-02-09 18:55:11 +01:00
|
|
|
if (!file.open(QFile::WriteOnly)) {
|
2014-02-06 17:07:19 +01:00
|
|
|
qWarning() << "Bookmarks::saveBookmarks() Error opening bookmarks file for writing!";
|
|
|
|
}
|
|
|
|
|
2015-12-18 11:25:45 +01:00
|
|
|
file.write(data);
|
2014-02-09 18:55:11 +01:00
|
|
|
file.close();
|
2014-02-06 17:07:19 +01:00
|
|
|
}
|
|
|
|
|
2014-02-10 12:58:34 +01:00
|
|
|
void Bookmarks::loadBookmarksFromMap(const QVariantMap &map)
|
|
|
|
{
|
|
|
|
#define READ_FOLDER(name, folder) \
|
|
|
|
readBookmarks(map.value(name).toMap().value("children").toList(), folder); \
|
|
|
|
folder->setExpanded(map.value(name).toMap().value("expanded").toBool()); \
|
|
|
|
folder->setSidebarExpanded(map.value(name).toMap().value("expanded_sidebar").toBool());
|
|
|
|
|
|
|
|
READ_FOLDER("bookmark_bar", m_folderToolbar)
|
|
|
|
READ_FOLDER("bookmark_menu", m_folderMenu)
|
|
|
|
READ_FOLDER("other", m_folderUnsorted)
|
|
|
|
#undef READ_FOLDER
|
|
|
|
}
|
|
|
|
|
2014-02-07 22:48:30 +01:00
|
|
|
void Bookmarks::readBookmarks(const QVariantList &list, BookmarkItem* parent)
|
2014-02-06 17:07:19 +01:00
|
|
|
{
|
2014-02-08 21:11:13 +01:00
|
|
|
Q_ASSERT(parent);
|
2014-02-06 17:07:19 +01:00
|
|
|
|
|
|
|
foreach (const QVariant &entry, list) {
|
|
|
|
const QVariantMap map = entry.toMap();
|
|
|
|
BookmarkItem::Type type = BookmarkItem::typeFromString(map.value("type").toString());
|
|
|
|
|
|
|
|
if (type == BookmarkItem::Invalid) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
BookmarkItem* item = new BookmarkItem(type, parent);
|
2014-02-08 21:11:13 +01:00
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case BookmarkItem::Url:
|
2014-02-09 11:02:51 +01:00
|
|
|
item->setUrl(QUrl::fromEncoded(map.value("url").toByteArray()));
|
2014-02-08 21:11:13 +01:00
|
|
|
item->setTitle(map.value("name").toString());
|
|
|
|
item->setDescription(map.value("description").toString());
|
|
|
|
item->setKeyword(map.value("keyword").toString());
|
2014-02-09 13:34:52 +01:00
|
|
|
item->setVisitCount(map.value("visit_count").toInt());
|
2014-02-08 21:11:13 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case BookmarkItem::Folder:
|
|
|
|
item->setTitle(map.value("name").toString());
|
|
|
|
item->setDescription(map.value("description").toString());
|
|
|
|
item->setExpanded(map.value("expanded").toBool());
|
2014-02-09 11:02:51 +01:00
|
|
|
item->setSidebarExpanded(map.value("expanded_sidebar").toBool());
|
2014-02-08 21:11:13 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2014-02-06 17:07:19 +01:00
|
|
|
|
|
|
|
if (map.contains("children")) {
|
|
|
|
readBookmarks(map.value("children").toList(), item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-07 22:48:30 +01:00
|
|
|
QVariantList Bookmarks::writeBookmarks(BookmarkItem* parent)
|
2014-02-06 17:07:19 +01:00
|
|
|
{
|
2014-02-08 21:11:13 +01:00
|
|
|
Q_ASSERT(parent);
|
2014-02-06 17:07:19 +01:00
|
|
|
|
2014-02-08 21:11:13 +01:00
|
|
|
QVariantList list;
|
2014-02-06 17:07:19 +01:00
|
|
|
|
|
|
|
foreach (BookmarkItem* child, parent->children()) {
|
|
|
|
QVariantMap map;
|
|
|
|
map.insert("type", BookmarkItem::typeToString(child->type()));
|
2014-02-08 21:11:13 +01:00
|
|
|
|
|
|
|
switch (child->type()) {
|
|
|
|
case BookmarkItem::Url:
|
2014-02-10 23:33:12 +01:00
|
|
|
map.insert("url", child->urlString());
|
2014-02-08 21:11:13 +01:00
|
|
|
map.insert("name", child->title());
|
|
|
|
map.insert("description", child->description());
|
|
|
|
map.insert("keyword", child->keyword());
|
2014-02-09 13:34:52 +01:00
|
|
|
map.insert("visit_count", child->visitCount());
|
2014-02-08 21:11:13 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case BookmarkItem::Folder:
|
|
|
|
map.insert("name", child->title());
|
|
|
|
map.insert("description", child->description());
|
|
|
|
map.insert("expanded", child->isExpanded());
|
2014-02-09 11:02:51 +01:00
|
|
|
map.insert("expanded_sidebar", child->isSidebarExpanded());
|
2014-02-08 21:11:13 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2014-02-06 17:07:19 +01:00
|
|
|
|
|
|
|
if (!child->children().isEmpty()) {
|
|
|
|
map.insert("children", writeBookmarks(child));
|
|
|
|
}
|
|
|
|
|
|
|
|
list.append(map);
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2014-02-09 12:18:52 +01:00
|
|
|
void Bookmarks::search(QList<BookmarkItem*>* items, BookmarkItem* parent, const QUrl &url) const
|
|
|
|
{
|
|
|
|
Q_ASSERT(items);
|
|
|
|
Q_ASSERT(parent);
|
|
|
|
|
|
|
|
switch (parent->type()) {
|
|
|
|
case BookmarkItem::Root:
|
|
|
|
case BookmarkItem::Folder:
|
|
|
|
foreach (BookmarkItem* child, parent->children()) {
|
|
|
|
search(items, child, url);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BookmarkItem::Url:
|
|
|
|
if (parent->url() == url) {
|
|
|
|
items->append(parent);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-18 16:17:22 +01:00
|
|
|
void Bookmarks::search(QList<BookmarkItem*>* items, BookmarkItem* parent, const QString &string, int limit, Qt::CaseSensitivity sensitive) const
|
2014-02-09 13:34:52 +01:00
|
|
|
{
|
|
|
|
Q_ASSERT(items);
|
|
|
|
Q_ASSERT(parent);
|
|
|
|
|
2014-02-18 16:17:22 +01:00
|
|
|
if (limit == items->count()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-09 13:34:52 +01:00
|
|
|
switch (parent->type()) {
|
|
|
|
case BookmarkItem::Root:
|
|
|
|
case BookmarkItem::Folder:
|
|
|
|
foreach (BookmarkItem* child, parent->children()) {
|
2014-02-18 16:17:22 +01:00
|
|
|
search(items, child, string, limit, sensitive);
|
2014-02-09 13:34:52 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BookmarkItem::Url:
|
|
|
|
if (parent->title().contains(string, sensitive) ||
|
2014-04-05 14:42:19 +02:00
|
|
|
parent->urlString().contains(string, sensitive) ||
|
|
|
|
parent->description().contains(string, sensitive) ||
|
|
|
|
parent->keyword().compare(string, sensitive) == 0
|
|
|
|
) {
|
2014-02-09 13:34:52 +01:00
|
|
|
items->append(parent);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-04-24 11:56:03 +02:00
|
|
|
|
|
|
|
void Bookmarks::searchKeyword(QList<BookmarkItem*>* items, BookmarkItem* parent, const QString &keyword) const
|
|
|
|
{
|
|
|
|
Q_ASSERT(items);
|
|
|
|
Q_ASSERT(parent);
|
|
|
|
|
|
|
|
switch (parent->type()) {
|
|
|
|
case BookmarkItem::Root:
|
|
|
|
case BookmarkItem::Folder:
|
|
|
|
foreach (BookmarkItem* child, parent->children())
|
|
|
|
searchKeyword(items, child, keyword);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BookmarkItem::Url:
|
|
|
|
if (parent->keyword() == keyword)
|
|
|
|
items->append(parent);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|