From 371306cd3c8419c9b590ce847f3e79b1d0c39ee2 Mon Sep 17 00:00:00 2001 From: nowrep Date: Wed, 5 Feb 2014 16:44:19 +0100 Subject: [PATCH] [Code] Various cleanups in Bookmarks code --- src/lib/app/qupzilla.cpp | 2 +- src/lib/bookmarks/bookmarks.cpp | 71 +++++++++----------------- src/lib/bookmarks/bookmarks.h | 29 ++++------- src/lib/bookmarks/bookmarkstoolbar.cpp | 4 +- src/lib/bookmarks/bookmarkswidget.cpp | 7 ++- src/lib/sidebar/bookmarkssidebar.cpp | 2 +- 6 files changed, 42 insertions(+), 73 deletions(-) diff --git a/src/lib/app/qupzilla.cpp b/src/lib/app/qupzilla.cpp index a0864f5e0..01baa869b 100644 --- a/src/lib/app/qupzilla.cpp +++ b/src/lib/app/qupzilla.cpp @@ -1507,7 +1507,7 @@ void QupZilla::loadFolderBookmarks(Menu* menu) return; } - foreach (const Bookmark &b, mApp->bookmarks()->folderBookmarks(folder)) { + foreach (const Bookmark &b, mApp->bookmarks()->getFolderBookmarks(folder)) { tabWidget()->addView(b.url, b.title, Qz::NT_NotSelectedTab); } } diff --git a/src/lib/bookmarks/bookmarks.cpp b/src/lib/bookmarks/bookmarks.cpp index a081768e6..2dd1815e5 100644 --- a/src/lib/bookmarks/bookmarks.cpp +++ b/src/lib/bookmarks/bookmarks.cpp @@ -48,6 +48,11 @@ void Bookmarks::loadSettings() settings.endGroup(); } +bool Bookmarks::isShowingMostVisited() const +{ + return m_showMostVisited; +} + void Bookmarks::setShowingMostVisited(bool state) { Settings settings; @@ -57,6 +62,11 @@ void Bookmarks::setShowingMostVisited(bool state) m_showMostVisited = state; } +bool Bookmarks::isShowingOnlyIconsInToolbar() const +{ + return m_showOnlyIconsInToolbar; +} + void Bookmarks::setShowingOnlyIconsInToolbar(bool state) { Settings settings; @@ -82,6 +92,11 @@ bool Bookmarks::isFolder(const QString &name) return query.next(); } +QString Bookmarks::lastFolder() const +{ + return m_lastFolder; +} + void Bookmarks::setLastFolder(const QString &folder) { Settings settings; @@ -106,45 +121,34 @@ bool Bookmarks::isBookmarked(const QUrl &url) // Bookmark search priority: // Bookmarks in menu > bookmarks in toolbar -> user folders and unsorted -int Bookmarks::bookmarkId(const QUrl &url) +Bookmarks::Bookmark Bookmarks::getBookmark(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(); + return getBookmark(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(); + return getBookmark(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 getBookmark(query.value(0).toInt()); } - return -1; -} - -int Bookmarks::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; + return Bookmark(); } Bookmarks::Bookmark Bookmarks::getBookmark(int id) @@ -260,16 +264,6 @@ void Bookmarks::removeBookmark(const QList list) mApp->sendMessages(Qz::AM_BookmarksChanged, true); } -void Bookmarks::removeBookmark(const QUrl &url) -{ - removeBookmark(bookmarkId(url)); -} - -void Bookmarks::removeBookmark(WebView* view) -{ - removeBookmark(bookmarkId(view->url())); -} - bool Bookmarks::editBookmark(int id, const QString &title, const QUrl &url, const QString &folder) { if (title.isEmpty() && url.isEmpty() && folder.isEmpty()) { @@ -511,7 +505,7 @@ void Bookmarks::exportToHtml(const QString &fileName) out << "

" << endl; } -QVector Bookmarks::folderBookmarks(const QString &name) +QVector Bookmarks::getFolderBookmarks(const QString &name) { QVector list; @@ -565,23 +559,6 @@ bool Bookmarks::isSubfolder(const QString &name) return query.value(0).toString() == QLatin1String("yes"); } -bool Bookmarks::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 Bookmarks::toTranslatedFolder(const QString &name) { QString trFolder; diff --git a/src/lib/bookmarks/bookmarks.h b/src/lib/bookmarks/bookmarks.h index 8558df2d6..277eddd39 100644 --- a/src/lib/bookmarks/bookmarks.h +++ b/src/lib/bookmarks/bookmarks.h @@ -47,10 +47,7 @@ public: QImage image; bool inSubfolder; - Bookmark() { - id = -1; - inSubfolder = false; - } + Bookmark() : id(-1) , inSubfolder(false) { } bool operator==(const Bookmark &other) const { return (this->title == other.title && @@ -62,26 +59,26 @@ public: void loadSettings(); - bool isShowingMostVisited() { return m_showMostVisited; } + bool isShowingMostVisited() const; void setShowingMostVisited(bool state); - bool isShowingOnlyIconsInToolbar() { return m_showOnlyIconsInToolbar; } + bool isShowingOnlyIconsInToolbar() const; void setShowingOnlyIconsInToolbar(bool state); bool isFolder(const QString &name); - QString lastFolder() { return m_lastFolder; } + + QString lastFolder() const; void setLastFolder(const QString &folder); bool isBookmarked(const QUrl &url); - int bookmarkId(const QUrl &url); - int bookmarkId(const QUrl &url, const QString &title, const QString &folder); - Bookmark getBookmark(int id); - bool saveBookmark(const QUrl &url, const QString &title, const QIcon &icon, const QString &folder = "unsorted"); + Bookmark getBookmark(int id); + Bookmark getBookmark(const QUrl &url); + QVector getFolderBookmarks(const QString &name); + + bool saveBookmark(const QUrl &url, const QString &title, const QIcon &icon, const QString &folder = QLatin1String("unsorted")); bool saveBookmark(WebView* view, QString folder = QString()); - void removeBookmark(const QUrl &url); - void removeBookmark(WebView* view); void removeBookmark(int id); void removeBookmark(const QList list); @@ -90,17 +87,13 @@ public: bool createFolder(const QString &name); void removeFolder(const QString &name); - - QVector folderBookmarks(const QString &name); + bool renameFolder(const QString &before, const QString &after); bool createSubfolder(const QString &name); bool isSubfolder(const QString &name); - bool renameFolder(const QString &before, const QString &after); - void exportToHtml(const QString &fileName); - static bool bookmarksEqual(const Bookmark &one, const Bookmark &two); static QString toTranslatedFolder(const QString &name); static QString fromTranslatedFolder(const QString &name); diff --git a/src/lib/bookmarks/bookmarkstoolbar.cpp b/src/lib/bookmarks/bookmarkstoolbar.cpp index 20fb8e892..4d100db51 100644 --- a/src/lib/bookmarks/bookmarkstoolbar.cpp +++ b/src/lib/bookmarks/bookmarkstoolbar.cpp @@ -342,7 +342,7 @@ void BookmarksToolbar::loadFolderBookmarksInTabs() return; } - foreach (const Bookmark &b, m_bookmarks->folderBookmarks(folder)) { + foreach (const Bookmark &b, m_bookmarks->getFolderBookmarks(folder)) { p_QupZilla->tabWidget()->addView(b.url, b.title, Qz::NT_NotSelectedTab); } } @@ -629,7 +629,7 @@ void BookmarksToolbar::aboutToShowFolderMenu() menu->clear(); QString folder = menu->title(); - foreach (const Bookmark &b, m_bookmarks->folderBookmarks(folder)) { + foreach (const Bookmark &b, m_bookmarks->getFolderBookmarks(folder)) { QString title = b.title; if (title.length() > 40) { title.truncate(40); diff --git a/src/lib/bookmarks/bookmarkswidget.cpp b/src/lib/bookmarks/bookmarkswidget.cpp index 283a37e01..1e5121068 100644 --- a/src/lib/bookmarks/bookmarkswidget.cpp +++ b/src/lib/bookmarks/bookmarkswidget.cpp @@ -74,11 +74,10 @@ void BookmarksWidget::loadBookmark() // Bookmark folders m_bookmarksTree->refreshTree(); - m_bookmarkId = m_bookmarks->bookmarkId(m_url); + Bookmarks::Bookmark bookmark = m_bookmarks->getBookmark(m_bookmarkId); + m_bookmarkId = bookmark.id; if (m_bookmarkId > 0) { - Bookmarks::Bookmark bookmark = m_bookmarks->getBookmark(m_bookmarkId); - int index = ui->folder->findData(bookmark.folder); // QComboBox::findData() returns index related to the item's parent if (index == -1) { // subfolder @@ -139,7 +138,7 @@ void BookmarksWidget::on_saveRemove_clicked(bool) m_bookmarks->editBookmark(m_bookmarkId, m_view->title(), QUrl(), Bookmarks::fromTranslatedFolder(ui->folder->currentText())); } else { - m_bookmarks->removeBookmark(m_url); + m_bookmarks->removeBookmark(m_bookmarkId); emit bookmarkDeleted(); } } diff --git a/src/lib/sidebar/bookmarkssidebar.cpp b/src/lib/sidebar/bookmarkssidebar.cpp index 735114f58..836b20fa5 100644 --- a/src/lib/sidebar/bookmarkssidebar.cpp +++ b/src/lib/sidebar/bookmarkssidebar.cpp @@ -313,7 +313,7 @@ void BookmarksSideBar::changeFolderParent(const QString &name, bool isSubfolder) } else { addFolder(name); - QVector bookmarksList = m_bookmarks->folderBookmarks(name); + QVector bookmarksList = m_bookmarks->getFolderBookmarks(name); foreach (const Bookmark &b, bookmarksList) { addBookmark(b); }