diff --git a/src/lib/app/mainapplication.cpp b/src/lib/app/mainapplication.cpp index cafcc9338..38ee62d32 100644 --- a/src/lib/app/mainapplication.cpp +++ b/src/lib/app/mainapplication.cpp @@ -1189,7 +1189,7 @@ bool MainApplication::saveStateSlot() // Save cookies & bookmarks m_cookiejar->saveCookies(); - m_bookmarks->saveBookmarks(); + m_bookmarks->saveSettings(); return true; } diff --git a/src/lib/bookmarks/bookmarks.cpp b/src/lib/bookmarks/bookmarks.cpp index 48ad9a265..a27cf1eb9 100644 --- a/src/lib/bookmarks/bookmarks.cpp +++ b/src/lib/bookmarks/bookmarks.cpp @@ -36,6 +36,217 @@ Bookmarks::Bookmarks(QObject* parent) loadSettings(); } +Bookmarks::~Bookmarks() +{ + delete m_root; +} + +void Bookmarks::loadSettings() +{ + Settings settings; + settings.beginGroup("Bookmarks"); + m_showOnlyIconsInToolbar = settings.value("showOnlyIconsInToolbar", false).toBool(); + settings.endGroup(); +} + +void Bookmarks::saveSettings() +{ + Settings settings; + settings.beginGroup("Bookmarks"); + settings.setValue("showOnlyIconsInToolbar", m_showOnlyIconsInToolbar); + settings.endGroup(); + + saveBookmarks(); +} + +bool Bookmarks::showOnlyIconsInToolbar() const +{ + return m_showOnlyIconsInToolbar; +} + +void Bookmarks::exportToHtml(const QString &fileName) +{ + Q_UNUSED(fileName) +#if 0 + QFile file(fileName); + + if (!file.open(QFile::WriteOnly | QFile::Truncate)) { + qWarning() << "Bookmarks::exportHtml Cannot open file for writing!" << file.errorString(); + } + + QTextStream out(&file); + + out << "" << endl; + out << "" << endl; + out << "" << endl; + out << "Bookmarks" << endl; + out << "

Bookmarks

" << endl; + + out << "

" << endl; + + QString indent = " "; + QList > allFolders; + + QPair menu; + menu.first = "bookmarksMenu"; + menu.second = false; + + QPair toolbar; + toolbar.first = "bookmarksToolbar"; + toolbar.second = false; + + allFolders.append(menu); + allFolders.append(toolbar); + + QSqlQuery query; + query.exec("SELECT name, subfolder FROM folders"); + + while (query.next()) { + QPair pair; + pair.first = query.value(0).toString(); + pair.second = query.value(1).toString() == QLatin1String("yes"); + + allFolders.append(pair); + } + + for (int i = 0; i < allFolders.size(); ++i) { + QPair pair = allFolders.at(i); + + out << indent << "

" << pair.first << "

" << endl; + out << indent << "

" << endl; + + QSqlQuery q; + q.prepare("SELECT title, url FROM bookmarks WHERE folder = ?"); + q.addBindValue(pair.first); + q.exec(); + + while (q.next()) { + QString title = q.value(0).toString(); + QString url = q.value(1).toString(); + + out << indent << indent << "

" << title << "" << endl; + } + + out << indent << "

" << endl; + } + + query.exec("SELECT title, url FROM bookmarks WHERE folder='' OR folder='unsorted'"); + + while (query.next()) { + QString title = query.value(0).toString(); + QString url = query.value(1).toString(); + + out << indent << "

" << title << "" << endl; + } + + out << "

" << endl; +#endif +} + +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 Bookmarks::searchBookmarks(const QUrl &url) const +{ + QList items; + search(&items, m_root, url); + return items; +} + +QList Bookmarks::searchBookmarks(const QString &string, Qt::CaseSensitivity sensitive) const +{ + QList items; + search(&items, m_root, string, sensitive); + return items; +} + +void Bookmarks::addBookmark(BookmarkItem* parent, BookmarkItem* item) +{ + Q_ASSERT(parent); + Q_ASSERT(parent->isFolder()); + Q_ASSERT(item); + + insertBookmark(parent, 0, item); +} + +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); +} + +bool Bookmarks::removeBookmark(BookmarkItem* item) +{ + if (!canBeModified(item)) { + return false; + } + + m_model->removeBookmark(item); + emit bookmarkRemoved(item); + return true; +} + +void Bookmarks::notifyBookmarkChanged(BookmarkItem* item) +{ + Q_ASSERT(item); + emit bookmarkChanged(item); +} + +void Bookmarks::setShowOnlyIconsInToolbar(bool state) +{ + m_showOnlyIconsInToolbar = state; + emit showOnlyIconsInToolbarChanged(state); +} + void Bookmarks::init() { m_root = new BookmarkItem(BookmarkItem::Root); @@ -87,19 +298,6 @@ void Bookmarks::init() m_model = new BookmarksModel(this, this); } -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 -} - void Bookmarks::saveBookmarks() { QVariantMap bookmarksMap; @@ -141,6 +339,19 @@ void Bookmarks::saveBookmarks() file.close(); } +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 +} + void Bookmarks::readBookmarks(const QVariantList &list, BookmarkItem* parent) { Q_ASSERT(parent); @@ -271,260 +482,3 @@ void Bookmarks::search(QList* items, BookmarkItem* parent, const break; } } - -void Bookmarks::loadSettings() -{ - Settings settings; - settings.beginGroup("Bookmarks"); - m_showMostVisited = settings.value("showMostVisited", true).toBool(); - m_showOnlyIconsInToolbar = settings.value("showOnlyIconsInToolbar", false).toBool(); - settings.endGroup(); -} - -bool Bookmarks::isShowingMostVisited() const -{ - return m_showMostVisited; -} - -void Bookmarks::setShowingMostVisited(bool state) -{ - Settings settings; - settings.beginGroup("Bookmarks"); - settings.setValue("showMostVisited", state); - settings.endGroup(); - m_showMostVisited = state; -} - -bool Bookmarks::isShowingOnlyIconsInToolbar() const -{ - return m_showOnlyIconsInToolbar; -} - -void Bookmarks::setShowingOnlyIconsInToolbar(bool state) -{ - Settings settings; - settings.beginGroup("Bookmarks"); - settings.setValue("showOnlyIconsInToolbar", state); - settings.endGroup(); - m_showOnlyIconsInToolbar = state; -} - -void Bookmarks::setLastFolder(const QString &folder) -{ - Settings settings; - settings.beginGroup("Bookmarks"); - settings.setValue("lastFolder", folder); - settings.endGroup(); -} - -void Bookmarks::exportToHtml(const QString &fileName) -{ - QFile file(fileName); - - if (!file.open(QFile::WriteOnly | QFile::Truncate)) { - qWarning() << "Bookmarks::exportHtml Cannot open file for writing!" << file.errorString(); - } - - QTextStream out(&file); - - out << "" << endl; - out << "" << endl; - out << "" << endl; - out << "Bookmarks" << endl; - out << "

Bookmarks

" << endl; - - out << "

" << endl; - - QString indent = " "; - QList > allFolders; - - QPair menu; - menu.first = "bookmarksMenu"; - menu.second = false; - - QPair toolbar; - toolbar.first = "bookmarksToolbar"; - toolbar.second = false; - - allFolders.append(menu); - allFolders.append(toolbar); - - QSqlQuery query; - query.exec("SELECT name, subfolder FROM folders"); - - while (query.next()) { - QPair pair; - pair.first = query.value(0).toString(); - pair.second = query.value(1).toString() == QLatin1String("yes"); - - allFolders.append(pair); - } - - for (int i = 0; i < allFolders.size(); ++i) { - QPair pair = allFolders.at(i); - - out << indent << "

" << pair.first << "

" << endl; - out << indent << "

" << endl; - - QSqlQuery q; - q.prepare("SELECT title, url FROM bookmarks WHERE folder = ?"); - q.addBindValue(pair.first); - q.exec(); - - while (q.next()) { - QString title = q.value(0).toString(); - QString url = q.value(1).toString(); - - out << indent << indent << "

" << title << "" << endl; - } - - out << indent << "

" << endl; - } - - query.exec("SELECT title, url FROM bookmarks WHERE folder='' OR folder='unsorted'"); - - while (query.next()) { - QString title = query.value(0).toString(); - QString url = query.value(1).toString(); - - out << indent << "

" << title << "" << endl; - } - - out << "

" << endl; -} - -QString Bookmarks::toTranslatedFolder(const QString &name) -{ - QString trFolder; - if (name == QLatin1String("bookmarksMenu")) { - trFolder = tr("Bookmarks In Menu"); - } - else if (name == QLatin1String("bookmarksToolbar")) { - trFolder = tr("Bookmarks In ToolBar"); - } - else if (name == QLatin1String("unsorted")) { - trFolder = tr("Unsorted Bookmarks"); - } - else { - trFolder = name; - } - return trFolder; -} - -QString Bookmarks::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; -} - -BookmarksModel* Bookmarks::model() const -{ - return m_model; -} - -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; -} - -bool Bookmarks::isBookmarked(const QUrl &url) -{ - return !searchBookmarks(url).isEmpty(); -} - -QList Bookmarks::searchBookmarks(const QUrl &url) const -{ - QList items; - search(&items, m_root, url); - return items; -} - -QList Bookmarks::searchBookmarks(const QString &string, Qt::CaseSensitivity sensitive) const -{ - QList items; - search(&items, m_root, string, sensitive); - return items; -} - -bool Bookmarks::removeBookmark(BookmarkItem* item) -{ - if (!canBeModified(item)) { - return false; - } - - m_model->removeBookmark(item); - emit bookmarkRemoved(item); - - return true; -} - -void Bookmarks::notifyBookmarkChanged(BookmarkItem* item) -{ - Q_ASSERT(item); - - emit bookmarkChanged(item); -} - -bool Bookmarks::canBeModified(BookmarkItem* item) const -{ - Q_ASSERT(item); - - return item != m_root && - item != m_folderToolbar && - item != m_folderMenu && - item != m_folderUnsorted; -} - -void Bookmarks::addBookmark(BookmarkItem* parent, BookmarkItem* item) -{ - Q_ASSERT(parent); - Q_ASSERT(parent->isFolder()); - Q_ASSERT(item); - - insertBookmark(parent, 0, item); -} - -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); -} diff --git a/src/lib/bookmarks/bookmarks.h b/src/lib/bookmarks/bookmarks.h index 71f8e9593..794d4533b 100644 --- a/src/lib/bookmarks/bookmarks.h +++ b/src/lib/bookmarks/bookmarks.h @@ -18,20 +18,13 @@ #ifndef BOOKMARKS_H #define BOOKMARKS_H -#define _bookmarksToolbar Bookmarks::toTranslatedFolder("bookmarksToolbar") -#define _bookmarksMenu Bookmarks::toTranslatedFolder("bookmarksMenu") -#define _bookmarksUnsorted Bookmarks::toTranslatedFolder("unsorted") - #include -#include -#include #include #include "qz_namespace.h" -class QIcon; +class QUrl; -class WebView; class BookmarkItem; class BookmarksModel; @@ -40,63 +33,39 @@ class QT_QUPZILLA_EXPORT Bookmarks : public QObject Q_OBJECT public: explicit Bookmarks(QObject* parent = 0); - - struct Bookmark { - int id; - QString title; - QString folder; - QUrl url; - QImage image; - bool inSubfolder; - - Bookmark() : id(-1) , inSubfolder(false) { } - - bool operator==(const Bookmark &other) const { - return (this->title == other.title && - this->folder == other.folder && - this->url == other.url && - this->inSubfolder == other.inSubfolder); - } - }; + ~Bookmarks(); void loadSettings(); - void saveBookmarks(); + void saveSettings(); - bool isShowingMostVisited() const; - void setShowingMostVisited(bool state); - - bool isShowingOnlyIconsInToolbar() const; - void setShowingOnlyIconsInToolbar(bool state); - - void setLastFolder(const QString &folder); + bool showOnlyIconsInToolbar() const; void exportToHtml(const QString &fileName); - static QString toTranslatedFolder(const QString &name); - static QString fromTranslatedFolder(const QString &name); - BookmarkItem* rootItem() const; BookmarkItem* toolbarFolder() const; BookmarkItem* menuFolder() const; BookmarkItem* unsortedFolder() const; - BookmarkItem* lastUsedFolder() const; + BookmarksModel* model() const; bool isBookmarked(const QUrl &url); + bool canBeModified(BookmarkItem* item) const; // Search bookmarks (urls only) for exact url match QList searchBookmarks(const QUrl &url) const; // Search bookmarks (urls only) for contains match through all properties QList searchBookmarks(const QString &string, Qt::CaseSensitivity sensitive = Qt::CaseInsensitive) const; - bool canBeModified(BookmarkItem* item) const; - void addBookmark(BookmarkItem* parent, BookmarkItem* item); void insertBookmark(BookmarkItem* parent, int row, BookmarkItem* item); bool removeBookmark(BookmarkItem* item); void notifyBookmarkChanged(BookmarkItem* item); +public slots: + void setShowOnlyIconsInToolbar(bool state); + signals: // Item was added to bookmarks void bookmarkAdded(BookmarkItem* item); @@ -105,8 +74,11 @@ signals: // Item data has changed void bookmarkChanged(BookmarkItem* item); + void showOnlyIconsInToolbarChanged(bool show); + private: void init(); + void saveBookmarks(); void loadBookmarksFromMap(const QVariantMap &map); void readBookmarks(const QVariantList &list, BookmarkItem* parent); @@ -123,15 +95,7 @@ private: BookmarkItem* m_lastFolder; BookmarksModel* m_model; - bool m_showMostVisited; bool m_showOnlyIconsInToolbar; }; -typedef Bookmarks::Bookmark Bookmark; - -// Hint to QVector to use std::realloc on item moving -Q_DECLARE_TYPEINFO(Bookmark, Q_MOVABLE_TYPE); - -Q_DECLARE_METATYPE(Bookmark) - #endif // BOOKMARKS_H diff --git a/src/lib/bookmarks/bookmarkstoolbar.cpp b/src/lib/bookmarks/bookmarkstoolbar.cpp index 74152d839..22c448475 100644 --- a/src/lib/bookmarks/bookmarkstoolbar.cpp +++ b/src/lib/bookmarks/bookmarkstoolbar.cpp @@ -70,8 +70,8 @@ void BookmarksToolbar::contextMenuRequested(const QPoint &pos) menu.addSeparator(); QAction* act = menu.addAction(tr("Show Only Icons")); act->setCheckable(true); - act->setChecked(m_bookmarks->isShowingOnlyIconsInToolbar()); - connect(act, SIGNAL(toggled(bool)), this, SLOT(setShowOnlyIcons(bool))); + act->setChecked(m_bookmarks->showOnlyIconsInToolbar()); + connect(act, SIGNAL(toggled(bool)), m_bookmarks, SLOT(setShowOnlyIconsInToolbar(bool))); connect(actNewTab, SIGNAL(triggered()), this, SLOT(openBookmarkInNewTab())); connect(actNewWindow, SIGNAL(triggered()), this, SLOT(openBookmarkInNewWindow())); @@ -86,19 +86,6 @@ void BookmarksToolbar::contextMenuRequested(const QPoint &pos) m_clickedBookmark = 0; } -void BookmarksToolbar::setShowOnlyIcons(bool show) -{ - m_bookmarks->setShowingOnlyIconsInToolbar(show); - - for (int i = 0; i < m_layout->count(); ++i) { - BookmarksToolbarButton* b = qobject_cast(m_layout->itemAt(i)->widget()); - - if (b) { - b->setShowOnlyIcon(show); - } - } -} - void BookmarksToolbar::addItem(BookmarkItem* item) { Q_ASSERT(item); @@ -108,7 +95,7 @@ void BookmarksToolbar::addItem(BookmarkItem* item) case BookmarkItem::Url: { BookmarksToolbarButton* button = new BookmarksToolbarButton(item, this); button->setMainWindow(m_window); - button->setShowOnlyIcon(m_bookmarks->isShowingOnlyIconsInToolbar()); + button->setShowOnlyIcon(m_bookmarks->showOnlyIconsInToolbar()); m_layout->addWidget(button); break; } @@ -157,6 +144,17 @@ void BookmarksToolbar::bookmarksChanged() m_updateTimer->start(); } +void BookmarksToolbar::showOnlyIconsChanged(bool state) +{ + for (int i = 0; i < m_layout->count(); ++i) { + BookmarksToolbarButton* b = qobject_cast(m_layout->itemAt(i)->widget()); + + if (b) { + b->setShowOnlyIcon(state); + } + } +} + void BookmarksToolbar::openBookmarkInNewTab() { if (m_clickedBookmark) { diff --git a/src/lib/bookmarks/bookmarkstoolbar.h b/src/lib/bookmarks/bookmarkstoolbar.h index ee006dabb..58c08e174 100644 --- a/src/lib/bookmarks/bookmarkstoolbar.h +++ b/src/lib/bookmarks/bookmarkstoolbar.h @@ -37,10 +37,10 @@ public: private slots: void contextMenuRequested(const QPoint &pos); - void setShowOnlyIcons(bool show); void refresh(); void bookmarksChanged(); + void showOnlyIconsChanged(bool state); void openBookmarkInNewTab(); void openBookmarkInNewWindow(); diff --git a/src/lib/bookmarks/bookmarkstools.cpp b/src/lib/bookmarks/bookmarkstools.cpp index 7052b61d9..e398fc11e 100644 --- a/src/lib/bookmarks/bookmarkstools.cpp +++ b/src/lib/bookmarks/bookmarkstools.cpp @@ -337,6 +337,8 @@ void BookmarksTools::addUrlToMenu(QObject* receiver, Menu* menu, BookmarkItem* b void BookmarksTools::addSeparatorToMenu(Menu* menu, BookmarkItem* separator) { + Q_UNUSED(separator) + Q_ASSERT(menu); Q_ASSERT(separator->isSeparator());