diff options
author | aqua <aqua@iserlohn-fortress.net> | 2022-08-15 14:25:29 +0300 |
---|---|---|
committer | aqua <aqua@iserlohn-fortress.net> | 2022-09-14 08:51:09 +0300 |
commit | d9802226e443ecd7a7062fd7a0fb320a183e341f (patch) | |
tree | 1cfd97905fa7ba4de4beca9802ba749d62b5cc4e | |
parent | Import BookmarkModel from poi (diff) | |
download | rekonq-d9802226e443ecd7a7062fd7a0fb320a183e341f.tar.xz |
Migrate BookmarkManager
23 files changed, 997 insertions, 1331 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 43efafac..0ee5e07e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -69,13 +69,13 @@ ENDIF(SOPRANO_FOUND AND NepomukCore_FOUND) #MACRO_OPTIONAL_FIND_PACKAGE(QCA2) #MACRO_BOOL_TO_01(QCA2_FOUND HAVE_QCA2) -#configure_file(${CMAKE_CURRENT_SOURCE_DIR}/config-qca2.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/src/config-qca2.h ) +#configure_file(cmake/config-qca2.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/cmake/config-qca2.h ) #MACRO_OPTIONAL_FIND_PACKAGE(QtOAuth) #MACRO_BOOL_TO_01(QTOAUTH_FOUND HAVE_QTOAUTH) -#configure_file(${CMAKE_CURRENT_SOURCE_DIR}/config-qtoauth.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/src/config-qtoauth.h ) +#configure_file(cmake/config-qtoauth.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/cmake/config-qtoauth.h ) # ================================================================================== @@ -88,7 +88,7 @@ ENDIF(SOPRANO_FOUND AND NepomukCore_FOUND) # ================================================================================== # configure files -configure_file(${CMAKE_CURRENT_SOURCE_DIR}/config-version.h.cmake config-version.h) +configure_file(cmake/config-version.h.cmake config-version.h) # ================================================================================== # includes for all targets diff --git a/config-kactivities.h.cmake b/cmake/config-kactivities.h.cmake index e136858c..e136858c 100644 --- a/config-kactivities.h.cmake +++ b/cmake/config-kactivities.h.cmake diff --git a/config-qca2.h.cmake b/cmake/config-qca2.h.cmake index db503b24..db503b24 100644 --- a/config-qca2.h.cmake +++ b/cmake/config-qca2.h.cmake diff --git a/config-qtoauth.h.cmake b/cmake/config-qtoauth.h.cmake index 05a5b2b9..05a5b2b9 100644 --- a/config-qtoauth.h.cmake +++ b/cmake/config-qtoauth.h.cmake diff --git a/config-version.h.cmake b/cmake/config-version.h.cmake index a63ff0f5..a63ff0f5 100644 --- a/config-version.h.cmake +++ b/cmake/config-version.h.cmake diff --git a/src/bookmarks/CMakeLists.txt b/src/bookmarks/CMakeLists.txt index 517aa2f6..db0d9377 100644 --- a/src/bookmarks/CMakeLists.txt +++ b/src/bookmarks/CMakeLists.txt @@ -5,6 +5,7 @@ add_library(bookmarks STATIC # bookmarksmenu.cpp bookmarksmenu.h # bookmarkstoolbar.cpp bookmarkstoolbar.h # Bookmarks Model + bookmark.cpp bookmark.hpp bookmarkstreeitem.cpp bookmarkstreeitem.hpp bookmarkstreemodel.cpp bookmarkstreemodel.hpp bookmarkstreeformats.hpp @@ -15,8 +16,12 @@ add_library(bookmarks STATIC target_include_directories(bookmarks PUBLIC ${CMAKE_SOURCE_DIR}/src) target_link_libraries(bookmarks PUBLIC Qt6::Core Qt6::Widgets) -if(TESTING) +if(${CMAKE_BUILD_TYPE} STREQUAL "Debug") + add_executable(bookmarkmanager test/bookmarkmanager.cpp) + target_link_libraries(bookmarkmanager GTest::gtest GTest::gtest_main bookmarks) + gtest_discover_tests(bookmarkmanager WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test) + add_executable(xbel test/xbel.cpp) target_link_libraries(xbel GTest::gtest GTest::gtest_main bookmarks) - gtest_discover_tests(xbel) + gtest_discover_tests(xbel WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test) endif() diff --git a/src/bookmarks/bookmark.cpp b/src/bookmarks/bookmark.cpp new file mode 100644 index 00000000..665a10c8 --- /dev/null +++ b/src/bookmarks/bookmark.cpp @@ -0,0 +1 @@ +#include "bookmark.hpp" diff --git a/src/bookmarks/bookmark.hpp b/src/bookmarks/bookmark.hpp new file mode 100644 index 00000000..ea958f6d --- /dev/null +++ b/src/bookmarks/bookmark.hpp @@ -0,0 +1,13 @@ +#pragma once + +#include <QList> +#include <QObject> + +class Bookmark : public QObject { + Q_OBJECT + +public: + explicit Bookmark(QObject *parent = nullptr) : QObject(parent) {} +}; + +typedef QList<Bookmark> BookmarkGroup;
\ No newline at end of file diff --git a/src/bookmarks/bookmarkmanager.cpp b/src/bookmarks/bookmarkmanager.cpp index 1f406a04..b5077111 100644 --- a/src/bookmarks/bookmarkmanager.cpp +++ b/src/bookmarks/bookmarkmanager.cpp @@ -12,284 +12,166 @@ * Description: rekonq bookmarks system interface * ============================================================ */ -// Self Includes #include "bookmarkmanager.hpp" -#include "bookmarkowner.h" -#include "bookmarksmenu.h" -#include "bookmarkstoolbar.h" +#include <QCoreApplication> #include <QFile> -#include <QStandardPaths> -#include <memory> +#include <QMenu> // ---------------------------------------------------------------------------------------------- +// Constructor and Destructor -static std::unique_ptr<BookmarkManager> s_bookmarkManager = nullptr; - -BookmarkManager *BookmarkManager::self() +BookmarkManager::BookmarkManager(const QString &bookmarksFile, QObject *parent) + : QObject(parent), m_model(new BookmarkModel(this)) { - if (!s_bookmarkManager) { s_bookmarkManager = new BookmarkManager(qApp); } - return *s_bookmarkManager; -} - - -// ---------------------------------------------------------------------------------------------- - -BookmarkManager::BookmarkManager(QObject *parent) : QObject(parent) -{ - m_manager = KBookmarkManager::userBookmarksManager(); - const auto bookmarksFile = QStandardPaths::locate(AppLocalDataLocation, QString::fromLatin1("bookmarks.xbel")); - - if (!QFile::exists(bookmarksFile)) - { - kDebug() << "copying of defaultbookmarks.xbel ..."; - - QString bookmarksDefaultPath = KStandardDirs::locate("appdata" , "defaultbookmarks.xbel"); - KBookmarkManager *tempManager = KBookmarkManager::managerForExternalFile(bookmarksDefaultPath); - - copyBookmarkGroup(tempManager->root(), rootGroup()); - m_manager->emitChanged(); - delete tempManager; + if (!bookmarksFile.isEmpty()) { + QFile bf(bookmarksFile); + if (bf.open(QIODevice::ReadOnly | QIODevice::Text)) { + m_errors += m_model->load(&bf); + bf.close(); } + else + m_errors += + tr("%1 Bookmarks file '%2' cannot be opened: %3").arg(__PRETTY_FUNCTION__, bookmarksFile, bf.errorString()); + } - connect(m_manager, SIGNAL(changed(QString,QString)), this, SLOT(slotBookmarksChanged())); - - // setup menu - m_owner = new BookmarkOwner(m_manager, this); - connect(m_owner, SIGNAL(openUrl(KUrl,Rekonq::OpenType)), this, SIGNAL(openUrl(KUrl,Rekonq::OpenType))); - - // bookmarks loading - connect(this, SIGNAL(openUrl(KUrl,Rekonq::OpenType)), rApp, SLOT(loadUrl(KUrl,Rekonq::OpenType))); -} - - -BookmarkManager::~BookmarkManager() -{ - delete m_manager; -} - - -void BookmarkManager::registerBookmarkBar(BookmarkToolBar *toolbar) -{ - if (m_bookmarkToolBars.contains(toolbar)) - return; - - m_bookmarkToolBars.append(toolbar); -} - - -void BookmarkManager::removeBookmarkBar(BookmarkToolBar *toolbar) -{ - m_bookmarkToolBars.removeOne(toolbar); -} - + // connect(m_manager, SIGNAL(changed(QString, QString)), this, SLOT(slotBookmarksChanged())); -QAction* BookmarkManager::actionByName(const QString &name) -{ - QAction *action = m_actionCollection->action(name); - if (action) - return action; - return new QAction(this); + // bookmarks loading + // connect(this, SIGNAL(openUrl(KUrl, Rekonq::OpenType)), rApp, SLOT(loadUrl(KUrl, Rekonq::OpenType))); } +BookmarkManager::~BookmarkManager() { delete m_model; } -KBookmarkGroup BookmarkManager::rootGroup() +// ---------------------------------------------------------------------------------------------- +/* +QList<Bookmark> BookmarkManager::find(const QString &text) { - return m_manager->root(); -} + BookmarkGroup list; + auto *root = m_model->item(); + Q_CHECK_PTR(root); -QList<KBookmark> BookmarkManager::find(const QString &text) -{ - QList<KBookmark> list; - - KBookmarkGroup root = rootGroup(); - if (!root.isNull()) - for (KBookmark bookmark = root.first(); !bookmark.isNull(); bookmark = root.next(bookmark)) - find(&list, bookmark, text); + KBookmarkGroup root = rootGroup(); + if (!root.isNull()) + for (KBookmark bookmark = root.first(); !bookmark.isNull(); bookmark = root.next(bookmark)) + find(&list, bookmark, text); - return list; + return list; } - KBookmark BookmarkManager::bookmarkForUrl(const KUrl &url) { - KBookmarkGroup root = rootGroup(); - if (root.isNull()) - return KBookmark(); + KBookmarkGroup root = rootGroup(); + if (root.isNull()) return KBookmark(); - return bookmarkForUrl(root, url); + return bookmarkForUrl(root, url); } - void BookmarkManager::slotBookmarksChanged() { - Q_FOREACH(BookmarkToolBar * bookmarkToolBar, m_bookmarkToolBars) - { - if (bookmarkToolBar) - { - bookmarkToolBar->clear(); - fillBookmarkBar(bookmarkToolBar); - } + Q_FOREACH (BookmarkToolBar *bookmarkToolBar, m_bookmarkToolBars) { + if (bookmarkToolBar) { + bookmarkToolBar->clear(); + fillBookmarkBar(bookmarkToolBar); } + } - // NOTE with this signal, we should (eventual) update about:bookmarks page... - emit bookmarksUpdated(); + // NOTE with this signal, we should (eventual) update about:bookmarks page... + emit bookmarksUpdated(); } - KBookmark BookmarkManager::bookmarkCurrentPage(const KBookmark &bookmark) { - return m_owner->bookmarkCurrentPage(bookmark); + return m_owner->bookmarkCurrentPage(bookmark); } - void BookmarkManager::fillBookmarkBar(BookmarkToolBar *toolBar) { - KBookmarkGroup root = m_manager->toolbar(); - if (root.isNull()) - return; + KBookmarkGroup root = m_manager->toolbar(); + if (root.isNull()) return; - for (KBookmark bookmark = root.first(); !bookmark.isNull(); bookmark = root.next(bookmark)) - { - if (bookmark.isGroup()) - { - KBookmarkActionMenu *menuAction = new KBookmarkActionMenu(bookmark.toGroup(), toolBar); - menuAction->setDelayed(false); - BookmarkMenu *bMenu = new BookmarkMenu(m_manager, m_owner, menuAction->menu(), bookmark.address()); - bMenu->setParent(menuAction->menu()); + for (KBookmark bookmark = root.first(); !bookmark.isNull(); bookmark = root.next(bookmark)) { + if (bookmark.isGroup()) { + KBookmarkActionMenu *menuAction = new KBookmarkActionMenu(bookmark.toGroup(), toolBar); + menuAction->setDelayed(false); + BookmarkMenu *bMenu = new BookmarkMenu(m_manager, m_owner, menuAction->menu(), bookmark.address()); + bMenu->setParent(menuAction->menu()); - connect(menuAction->menu(), SIGNAL(aboutToShow()), toolBar, SLOT(menuDisplayed())); - connect(menuAction->menu(), SIGNAL(aboutToHide()), toolBar, SLOT(menuHidden())); + connect(menuAction->menu(), SIGNAL(aboutToShow()), toolBar, SLOT(menuDisplayed())); + connect(menuAction->menu(), SIGNAL(aboutToHide()), toolBar, SLOT(menuHidden())); - toolBar->addAction(menuAction); - toolBar->widgetForAction(menuAction)->installEventFilter(toolBar); - } - else if (bookmark.isSeparator()) - { - toolBar->addSeparator(); - } - else - { - KBookmarkAction *action = new KBookmarkAction(bookmark, m_owner, toolBar); - action->setIcon(IconManager::self()->iconForUrl(KUrl(bookmark.url()))); - toolBar->addAction(action); - toolBar->widgetForAction(action)->installEventFilter(toolBar); - } + toolBar->addAction(menuAction); + toolBar->widgetForAction(menuAction)->installEventFilter(toolBar); + } + else if (bookmark.isSeparator()) { + toolBar->addSeparator(); + } + else { + KBookmarkAction *action = new KBookmarkAction(bookmark, m_owner, toolBar); + action->setIcon(IconManager::self()->iconForUrl(KUrl(bookmark.url()))); + toolBar->addAction(action); + toolBar->widgetForAction(action)->installEventFilter(toolBar); } + } } - void BookmarkManager::find(QList<KBookmark> *list, const KBookmark &bookmark, const QString &text) { - if (bookmark.isGroup()) - { - KBookmarkGroup group = bookmark.toGroup(); - for (KBookmark bm = group.first(); !bm.isNull(); bm = group.next(bm)) - find(list, bm, text); - } - else - { - QStringList words = text.split(' '); - bool matches = true; - Q_FOREACH(const QString & word, words) - { - if (!bookmark.url().url().contains(word, Qt::CaseInsensitive) - && !bookmark.fullText().contains(word, Qt::CaseInsensitive)) - { - matches = false; - break; - } - } - if (matches) - *list << bookmark; + if (bookmark.isGroup()) { + KBookmarkGroup group = bookmark.toGroup(); + for (KBookmark bm = group.first(); !bm.isNull(); bm = group.next(bm)) find(list, bm, text); + } + else { + QStringList words = text.split(' '); + bool matches = true; + Q_FOREACH (const QString &word, words) { + if (!bookmark.url().url().contains(word, Qt::CaseInsensitive) && + !bookmark.fullText().contains(word, Qt::CaseInsensitive)) { + matches = false; + break; + } } + if (matches) *list << bookmark; + } } - KBookmark BookmarkManager::bookmarkForUrl(const KBookmark &bookmark, const KUrl &url) { - KBookmark found; - - if (bookmark.isGroup()) - { - KBookmarkGroup group = bookmark.toGroup(); - KBookmark bookmark = group.first(); - - while (!bookmark.isNull() && found.isNull()) - { - found = bookmarkForUrl(bookmark, url); - bookmark = group.next(bookmark); - } - } - else if (!bookmark.isSeparator() && bookmark.url() == url) - { - found = bookmark; - } - - return found; -} + KBookmark found; + if (bookmark.isGroup()) { + KBookmarkGroup group = bookmark.toGroup(); + KBookmark bookmark = group.first(); -void BookmarkManager::copyBookmarkGroup(const KBookmarkGroup &groupToCopy, KBookmarkGroup destGroup) -{ - KBookmark bookmark = groupToCopy.first(); - while (!bookmark.isNull()) - { - if (bookmark.isGroup()) - { - KBookmarkGroup newDestGroup = destGroup.createNewFolder(bookmark.text()); - if (bookmark.toGroup().isToolbarGroup()) - { - newDestGroup.internalElement().setAttribute("toolbar", "yes"); - newDestGroup.setIcon("bookmark-toolbar"); - } - copyBookmarkGroup(bookmark.toGroup(), newDestGroup); - } - else if (bookmark.isSeparator()) - { - destGroup.createNewSeparator(); - } - else - { - destGroup.addBookmark(bookmark.text(), bookmark.url()); - } - bookmark = groupToCopy.next(bookmark); + while (!bookmark.isNull() && found.isNull()) { + found = bookmarkForUrl(bookmark, url); + bookmark = group.next(bookmark); } -} - - -void BookmarkManager::slotEditBookmarks() -{ - m_manager->slotEditBookmarks(); -} + } + else if (!bookmark.isSeparator() && bookmark.url() == url) { + found = bookmark; + } - -KBookmark BookmarkManager::findByAddress(const QString &address) -{ - return m_manager->findByAddress(address); -} - - -void BookmarkManager::openFolderinTabs(const KBookmarkGroup &bm) -{ - m_owner->openFolderinTabs(bm); + return found; } +void BookmarkManager::slotEditBookmarks() { m_manager->slotEditBookmarks(); } -void BookmarkManager::emitChanged() -{ - m_manager->emitChanged(); +void BookmarkManager::openFolderInTabs(const BookmarkGroup &group) { + for(const auto &bookmark : group) { + // TODO emit openUrl + } } -KActionMenu* BookmarkManager::bookmarkActionMenu(QWidget *parent) +KActionMenu *BookmarkManager::bookmarkActionMenu(QWidget *parent) { - KMenu *menu = new KMenu(parent); - KActionMenu *bookmarkActionMenu = new KActionMenu(menu); - bookmarkActionMenu->setMenu(menu); - bookmarkActionMenu->setText(i18n("&Bookmarks")); - BookmarkMenu *bMenu = new BookmarkMenu(m_manager, m_owner, menu, m_actionCollection); - bMenu->setParent(menu); + auto *menu = new QMenu(parent); + KActionMenu *bookmarkActionMenu = new KActionMenu(menu); + bookmarkActionMenu->setMenu(menu); + bookmarkActionMenu->setText(tr("&Bookmarks")); + BookmarkMenu *bMenu = new BookmarkMenu(m_manager, m_owner, menu, m_actionCollection); + bMenu->setParent(menu); - return bookmarkActionMenu; + return bookmarkActionMenu; } - +*/ diff --git a/src/bookmarks/bookmarkmanager.hpp b/src/bookmarks/bookmarkmanager.hpp index 417eb83d..f76b8b3f 100644 --- a/src/bookmarks/bookmarkmanager.hpp +++ b/src/bookmarks/bookmarkmanager.hpp @@ -14,82 +14,60 @@ #pragma once +#include "bookmark.hpp" #include "bookmarkstreemodel.hpp" #include "rekonq.hpp" #include <QObject> -class BookmarkToolBar; -class QAction; +class QToolBar; /** - * This class represent the interface to rekonq bookmarks system. - * All rekonq needs (Bookmarks Menu, Bookmarks Toolbar) is provided - * from this class. - * So it implements code to have each one. + * This class represent the interface to the rekonq bookmarks system. It provides bookmarks access to the + * Bookmarks Menu and Bookmarks Toolbar. */ -class BookmarkManager : public QObject { +class BookmarkManager final : public QObject { Q_OBJECT public: /** - * Entry point. - * Access to BookmarkManager class by using - * BookmarkManager::self()->thePublicMethodYouNeed() - */ - static BookmarkManager *self(); - - ~BookmarkManager(); - - /** - * @short set the Bookmarks Toolbar Action + * @short Class constructor. + * Connect BookmarksProvider with bookmarks source + * (actually konqueror's bookmarks). + * @param parent The WebWindow to provide bookmarks objects. */ - void registerBookmarkBar(BookmarkToolBar *toolbar); - void removeBookmarkBar(BookmarkToolBar *toolbar); + explicit BookmarkManager(const QString &bookmarksFile, QObject *parent = nullptr); + ~BookmarkManager() override; /** - * @short Get action by name - * This method returns poiner bookmark action of given name. - * @pre m_actionCollection != NULL - * @param name Name of action you want to get - * @return It returns actions if one exists or empty object + * Return a list of errors + * @return A copy of the error list */ - QAction *actionByName(const QString &name); + auto errors() + { + auto e = m_errors; + m_errors.clear(); + return e; + } /** - * returns Bookmark Manager root group - * - * @return the root bookmark group + * @short set the Bookmarks Toolbar Action */ - KBookmarkGroup rootGroup(); - - inline KBookmarkManager *manager() { return m_manager; } - - inline BookmarkOwner *owner() { return m_owner; } - - QList<KBookmark> find(const QString &text); + void registerBookmarkBar(QToolBar *toolbar) + { + if (!m_bookmarkToolBars.contains(toolbar)) m_bookmarkToolBars.append(toolbar); + } + void removeBookmarkBar(QToolBar *toolbar) { m_bookmarkToolBars.removeOne(toolbar); } - KBookmark bookmarkForUrl(const KUrl &url); + // QList<Bookmark> find(const QString &text); + // QList<Bookmark> findByUrl(const QUrl &url); - KBookmark findByAddress(const QString &); + // Bookmark bookmarkForUrl(const QUrl &url); - void openFolderinTabs(const KBookmarkGroup &bm); + // void openFolderInTabs(const BookmarkGroup &group); - void emitChanged(); + // QMenu *bookmarkActionMenu(QWidget *parent); - static inline QString bookmark_mime_type() { return QL1S("application/x-rekonq-bookmark"); } - - KActionMenu *bookmarkActionMenu(QWidget *parent); - -private: - /** - * @short Class constructor. - * Connect BookmarksProvider with bookmarks source - * (actually konqueror's bookmarks). - * @param parent The WebWindow to provide bookmarks objects. - */ - explicit BookmarkManager(QObject *parent = nullptr); - -public Q_SLOTS: +public slots: /** * @short Waits for signal that the group with the address has been modified by the caller. * Waits for signal that the group (or any of its children) with the address @@ -98,26 +76,21 @@ public Q_SLOTS: * @param caller caller that modified the bookmarks * @see KBookmarkManager::changed */ - void slotBookmarksChanged(); - void fillBookmarkBar(BookmarkToolBar *toolBar); + // void slotBookmarksChanged(); + // void fillBookmarkBar(QToolBar *toolBar); - void slotEditBookmarks(); + // void slotEditBookmarks(); - KBookmark bookmarkCurrentPage(const KBookmark &bookmark = KBookmark()); + // Bookmark bookmarkCurrentPage(const Bookmark &bookmark = Bookmark()); -Q_SIGNALS: +signals: /** - * @short This signal is emitted when an url has to be loaded + * @short This signal is emitted when a URL has to be loaded */ - void openUrl(const KUrl &, const Rekonq::OpenType &); - - void bookmarksUpdated(); + void openUrl(const QUrl &, rekonq::OpenType); private: - void find(QList<KBookmark> *list, const KBookmark &bookmark, const QString &text); - KBookmark bookmarkForUrl(const KBookmark &bookmark, const KUrl &url); - void copyBookmarkGroup(const KBookmarkGroup &groupToCopy, KBookmarkGroup destGroup); - - QList<BookmarkToolBar *> m_bookmarkToolBars; + QList<QString> m_errors; + QList<QToolBar *> m_bookmarkToolBars; BookmarkModel *m_model; }; diff --git a/src/bookmarks/bookmarkowner.cpp b/src/bookmarks/bookmarkowner.cpp index b7fe443b..49e3dd87 100644 --- a/src/bookmarks/bookmarkowner.cpp +++ b/src/bookmarks/bookmarkowner.cpp @@ -1,31 +1,30 @@ /* ============================================================ -* -* This file is a part of the rekonq project -* -* Copyright (C) 2008-2012 by Andrea Diamantini <adjam7 at gmail dot com> -* Copyright (C) 2009 by Paweł Prażak <pawelprazak at gmail dot com> -* Copyright (C) 2009-2010 by Lionel Chauvin <megabigbug@yahoo.fr> -* Copyright (C) 2010 by Yoann Laissus <yoann dot laissus at gmail dot 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 2 of -* the License or (at your option) version 3 or any later version -* accepted by the membership of KDE e.V. (or its successor approved -* by the membership of KDE e.V.), which shall act as a proxy -* defined in Section 14 of version 3 of the license. -* -* 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/>. -* -* ============================================================ */ - + * + * This file is a part of the rekonq project + * + * Copyright (C) 2008-2012 by Andrea Diamantini <adjam7 at gmail dot com> + * Copyright (C) 2009 by Paweł Prażak <pawelprazak at gmail dot com> + * Copyright (C) 2009-2010 by Lionel Chauvin <megabigbug@yahoo.fr> + * Copyright (C) 2010 by Yoann Laissus <yoann dot laissus at gmail dot 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 2 of + * the License or (at your option) version 3 or any later version + * accepted by the membership of KDE e.V. (or its successor approved + * by the membership of KDE e.V.), which shall act as a proxy + * defined in Section 14 of version 3 of the license. + * + * 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/>. + * + * ============================================================ */ // Self Includes #include "bookmarkowner.h" @@ -57,378 +56,294 @@ #include <Nepomuk2/Vocabulary/NFO> #endif - BookmarkOwner::BookmarkOwner(KBookmarkManager *manager, QObject *parent) - : QObject(parent) - , KBookmarkOwner() - , m_manager(manager) + : QObject(parent), KBookmarkOwner(), m_manager(manager) { } - -KAction* BookmarkOwner::createAction(const KBookmark &bookmark, const BookmarkAction &bmAction) +KAction *BookmarkOwner::createAction(const KBookmark &bookmark, const BookmarkAction &bmAction) { - switch (bmAction) - { - case OPEN: - return createAction(i18n("Open"), "tab-new", - i18n("Open bookmark in current tab"), SLOT(loadBookmark(KBookmark)), bookmark); - case OPEN_IN_TAB: - return createAction(i18n("Open in New Tab"), "tab-new", - i18n("Open bookmark in new tab"), SLOT(loadBookmarkInNewTab(KBookmark)), bookmark); - case OPEN_IN_WINDOW: - return createAction(i18n("Open in New Window"), "window-new", - i18n("Open bookmark in new window"), SLOT(loadBookmarkInNewWindow(KBookmark)), bookmark); - case OPEN_FOLDER: - return createAction(i18n("Open Folder in Tabs"), "tab-new", - i18n("Open all the bookmarks in folder in tabs"), SLOT(loadBookmarkFolder(KBookmark)), bookmark); - case BOOKMARK_PAGE: - return createAction(i18n("Add Bookmark"), "bookmark-new", - i18n("Bookmark current page"), SLOT(bookmarkCurrentPage(KBookmark)), bookmark); - case NEW_FOLDER: - return createAction(i18n("New Folder"), "folder-new", - i18n("Create a new bookmark folder"), SLOT(newBookmarkFolder(KBookmark)), bookmark); - case NEW_SEPARATOR: - return createAction(i18n("New Separator"), "edit-clear", - i18n("Create a new bookmark separator"), SLOT(newSeparator(KBookmark)), bookmark); - case COPY: - return createAction(i18n("Copy Link"), "edit-copy", - i18n("Copy the bookmark's link address"), SLOT(copyLink(KBookmark)), bookmark); - case EDIT: - return createAction(i18n("Edit"), "configure", - i18n("Edit the bookmark"), SLOT(editBookmark(KBookmark)), bookmark); + switch (bmAction) { + case OPEN: + return createAction(i18n("Open"), "tab-new", i18n("Open bookmark in current tab"), SLOT(loadBookmark(KBookmark)), + bookmark); + case OPEN_IN_TAB: + return createAction(i18n("Open in New Tab"), "tab-new", i18n("Open bookmark in new tab"), + SLOT(loadBookmarkInNewTab(KBookmark)), bookmark); + case OPEN_IN_WINDOW: + return createAction(i18n("Open in New Window"), "window-new", i18n("Open bookmark in new window"), + SLOT(loadBookmarkInNewWindow(KBookmark)), bookmark); + case OPEN_FOLDER: + return createAction(i18n("Open Folder in Tabs"), "tab-new", i18n("Open all the bookmarks in folder in tabs"), + SLOT(loadBookmarkFolder(KBookmark)), bookmark); + case BOOKMARK_PAGE: + return createAction(i18n("Add Bookmark"), "bookmark-new", i18n("Bookmark current page"), + SLOT(bookmarkCurrentPage(KBookmark)), bookmark); + case NEW_FOLDER: + return createAction(i18n("New Folder"), "folder-new", i18n("Create a new bookmark folder"), + SLOT(newBookmarkFolder(KBookmark)), bookmark); + case NEW_SEPARATOR: + return createAction(i18n("New Separator"), "edit-clear", i18n("Create a new bookmark separator"), + SLOT(newSeparator(KBookmark)), bookmark); + case COPY: + return createAction(i18n("Copy Link"), "edit-copy", i18n("Copy the bookmark's link address"), + SLOT(copyLink(KBookmark)), bookmark); + case EDIT: + return createAction(i18n("Edit"), "configure", i18n("Edit the bookmark"), SLOT(editBookmark(KBookmark)), bookmark); #ifdef HAVE_NEPOMUK - case FANCYBOOKMARK: - return createAction(i18n("Fancy Bookmark"), "nepomuk", - i18n("Link Nepomuk resources"), SLOT(fancyBookmark(KBookmark)), bookmark); + case FANCYBOOKMARK: + return createAction(i18n("Fancy Bookmark"), "nepomuk", i18n("Link Nepomuk resources"), + SLOT(fancyBookmark(KBookmark)), bookmark); #endif - case DELETE: - return createAction(i18n("Delete"), "edit-delete", - i18n("Delete the bookmark"), SLOT(deleteBookmark(KBookmark)), bookmark); - case SET_TOOLBAR_FOLDER: - return createAction(i18n("Set as toolbar folder"), "bookmark-toolbar", - "", SLOT(setToolBarFolder(KBookmark)), bookmark); - case UNSET_TOOLBAR_FOLDER: - return createAction(i18n("Unset this folder as the toolbar folder"), "bookmark-toolbar", - "", SLOT(unsetToolBarFolder()), bookmark); - default: - ASSERT_NOT_REACHED(unknown BookmarkAction); - return 0; - } + case DELETE: + return createAction(i18n("Delete"), "edit-delete", i18n("Delete the bookmark"), SLOT(deleteBookmark(KBookmark)), + bookmark); + case SET_TOOLBAR_FOLDER: + return createAction(i18n("Set as toolbar folder"), "bookmark-toolbar", "", SLOT(setToolBarFolder(KBookmark)), + bookmark); + case UNSET_TOOLBAR_FOLDER: + return createAction(i18n("Unset this folder as the toolbar folder"), "bookmark-toolbar", "", + SLOT(unsetToolBarFolder()), bookmark); + default: + ASSERT_NOT_REACHED(unknown BookmarkAction); + return 0; + } } +QString BookmarkOwner::currentTitle() const { return rApp->rekonqWindow()->currentWebWindow()->title(); } -QString BookmarkOwner::currentTitle() const -{ - return rApp->rekonqWindow()->currentWebWindow()->title(); -} +QString BookmarkOwner::currentUrl() const { return rApp->rekonqWindow()->currentWebWindow()->url().url(); } - -QString BookmarkOwner::currentUrl() const +QList<QPair<QString, QString>> BookmarkOwner::currentBookmarkList() const { - return rApp->rekonqWindow()->currentWebWindow()->url().url(); + QList<QPair<QString, QString>> bkList; + TabWidget *view = rApp->rekonqWindow()->tabWidget(); + int tabNumber = view->count(); + + for (int i = 0; i < tabNumber; ++i) { + QPair<QString, QString> item; + item.first = view->webWindow(i)->title(); + item.second = view->webWindow(i)->url().url(); + bkList << item; + } + + return bkList; } - -QList< QPair<QString, QString> > BookmarkOwner::currentBookmarkList() const -{ - QList< QPair<QString, QString> > bkList; - TabWidget *view = rApp->rekonqWindow()->tabWidget(); - int tabNumber = view->count(); - - for (int i = 0; i < tabNumber; ++i) - { - QPair<QString, QString> item; - item.first = view->webWindow(i)->title(); - item.second = view->webWindow(i)->url().url(); - bkList << item; - } - - return bkList; -} - - -void BookmarkOwner::openBookmark(const KBookmark &bookmark, - Qt::MouseButtons mouseButtons, +void BookmarkOwner::openBookmark(const KBookmark &bookmark, Qt::MouseButtons mouseButtons, Qt::KeyboardModifiers keyboardModifiers) { - if (keyboardModifiers & Qt::ControlModifier || mouseButtons & Qt::MidButton) - loadBookmarkInNewTab(bookmark); - else - loadBookmark(bookmark); + if (keyboardModifiers & Qt::ControlModifier || mouseButtons & Qt::MidButton) loadBookmarkInNewTab(bookmark); + else + loadBookmark(bookmark); } - void BookmarkOwner::openFolderinTabs(const KBookmarkGroup &bkGoup) { - QList<KUrl> urlList = bkGoup.groupUrlList(); - - if (urlList.length() > 8) - { - if (KMessageBox::warningContinueCancel( - rApp->rekonqWindow(), - i18ncp("%1=Number of tabs. Value is always >=8", - "You are about to open %1 tabs.\nAre you sure?", - "You are about to open %1 tabs.\nAre you sure?", urlList.length())) - != KMessageBox::Continue - ) - return; - } + QList<KUrl> urlList = bkGoup.groupUrlList(); - Q_FOREACH(const KUrl & url, urlList) - { - emit openUrl(url, Rekonq::NewFocusedTab); - } -} + if (urlList.length() > 8) { + if (KMessageBox::warningContinueCancel(rApp->rekonqWindow(), i18ncp("%1=Number of tabs. Value is always >=8", + "You are about to open %1 tabs.\nAre you sure?", + "You are about to open %1 tabs.\nAre you sure?", + urlList.length())) != KMessageBox::Continue) + return; + } - -void BookmarkOwner::loadBookmark(const KBookmark &bookmark) -{ - emit openUrl(bookmark.url(), Rekonq::CurrentTab); + Q_FOREACH (const KUrl &url, urlList) { emit openUrl(url, Rekonq::NewFocusedTab); } } +void BookmarkOwner::loadBookmark(const KBookmark &bookmark) { emit openUrl(bookmark.url(), Rekonq::CurrentTab); } -void BookmarkOwner::loadBookmarkInNewTab(const KBookmark &bookmark) -{ - emit openUrl(bookmark.url(), Rekonq::NewTab); -} - +void BookmarkOwner::loadBookmarkInNewTab(const KBookmark &bookmark) { emit openUrl(bookmark.url(), Rekonq::NewTab); } void BookmarkOwner::loadBookmarkInNewWindow(const KBookmark &bookmark) { - emit openUrl(bookmark.url(), Rekonq::NewWindow); + emit openUrl(bookmark.url(), Rekonq::NewWindow); } - void BookmarkOwner::loadBookmarkFolder(const KBookmark &bookmark) { - Q_ASSERT(bookmark.isGroup()); - openFolderinTabs(bookmark.toGroup()); + Q_ASSERT(bookmark.isGroup()); + openFolderinTabs(bookmark.toGroup()); } - KBookmark BookmarkOwner::bookmarkCurrentPage(const KBookmark &bookmark) { - KBookmarkGroup parent; - - if (!bookmark.isNull()) - { - if (bookmark.isGroup()) - parent = bookmark.toGroup(); - else - parent = bookmark.parentGroup(); - } + KBookmarkGroup parent; + + if (!bookmark.isNull()) { + if (bookmark.isGroup()) parent = bookmark.toGroup(); else - { - parent = BookmarkManager::self()->rootGroup(); + parent = bookmark.parentGroup(); + } + else { + parent = BookmarkManager::self()->rootGroup(); #ifdef HAVE_NEPOMUK - Nepomuk2::Resource nfoResource; - nfoResource = ((QUrl)currentUrl()); - nfoResource.addType(Nepomuk2::Vocabulary::NFO::Website()); - nfoResource.setLabel(currentTitle()); + Nepomuk2::Resource nfoResource; + nfoResource = ((QUrl)currentUrl()); + nfoResource.addType(Nepomuk2::Vocabulary::NFO::Website()); + nfoResource.setLabel(currentTitle()); #endif - } + } - KBookmark newBk = parent.addBookmark(currentTitle(), KUrl(currentUrl())); - if (!bookmark.isNull()) - parent.moveBookmark(newBk, bookmark); + KBookmark newBk = parent.addBookmark(currentTitle(), KUrl(currentUrl())); + if (!bookmark.isNull()) parent.moveBookmark(newBk, bookmark); - m_manager->emitChanged(parent); - return newBk; + m_manager->emitChanged(parent); + return newBk; } - KBookmarkGroup BookmarkOwner::newBookmarkFolder(const KBookmark &bookmark, const QString &name) { - KBookmarkGroup newBk; - KBookmarkDialog *dialog = bookmarkDialog(m_manager, 0); - - QString folderName; - if (name.isEmpty()) - folderName = i18n("New folder"); - else - folderName = name; - - if (!bookmark.isNull()) - { - if (bookmark.isGroup()) - { - newBk = dialog->createNewFolder(folderName, bookmark); - } - else - { - newBk = dialog->createNewFolder(folderName, bookmark.parentGroup()); - if (!newBk.isNull()) - { - KBookmarkGroup parent = newBk.parentGroup(); - parent.moveBookmark(newBk, bookmark); - m_manager->emitChanged(parent); - } - } - } - else - { - newBk = dialog->createNewFolder(folderName); + KBookmarkGroup newBk; + KBookmarkDialog *dialog = bookmarkDialog(m_manager, 0); + + QString folderName; + if (name.isEmpty()) folderName = i18n("New folder"); + else + folderName = name; + + if (!bookmark.isNull()) { + if (bookmark.isGroup()) { newBk = dialog->createNewFolder(folderName, bookmark); } + else { + newBk = dialog->createNewFolder(folderName, bookmark.parentGroup()); + if (!newBk.isNull()) { + KBookmarkGroup parent = newBk.parentGroup(); + parent.moveBookmark(newBk, bookmark); + m_manager->emitChanged(parent); + } } + } + else { + newBk = dialog->createNewFolder(folderName); + } - delete dialog; - return newBk; + delete dialog; + return newBk; } - KBookmark BookmarkOwner::newSeparator(const KBookmark &bookmark) { - KBookmark newBk; - - if (!bookmark.isNull()) - { - if (bookmark.isGroup()) - { - newBk = bookmark.toGroup().createNewSeparator(); - } - else - { - newBk = bookmark.parentGroup().createNewSeparator(); - newBk.parentGroup().moveBookmark(newBk, bookmark); - } - } - else - { - newBk = BookmarkManager::self()->rootGroup().createNewSeparator(); + KBookmark newBk; + + if (!bookmark.isNull()) { + if (bookmark.isGroup()) { newBk = bookmark.toGroup().createNewSeparator(); } + else { + newBk = bookmark.parentGroup().createNewSeparator(); + newBk.parentGroup().moveBookmark(newBk, bookmark); } + } + else { + newBk = BookmarkManager::self()->rootGroup().createNewSeparator(); + } - newBk.setIcon("edit-clear"); + newBk.setIcon("edit-clear"); - m_manager->emitChanged(newBk.parentGroup()); - return newBk; + m_manager->emitChanged(newBk.parentGroup()); + return newBk; } - void BookmarkOwner::copyLink(const KBookmark &bookmark) { - if (bookmark.isNull()) - return; + if (bookmark.isNull()) return; - QApplication::clipboard()->setText(bookmark.url().url()); + QApplication::clipboard()->setText(bookmark.url().url()); } - void BookmarkOwner::editBookmark(KBookmark bookmark) { - if (bookmark.isNull()) - return; + if (bookmark.isNull()) return; - KBookmarkDialog *dialog = bookmarkDialog(m_manager, 0); - dialog->editBookmark(bookmark); + KBookmarkDialog *dialog = bookmarkDialog(m_manager, 0); + dialog->editBookmark(bookmark); - delete dialog; + delete dialog; } - #ifdef HAVE_NEPOMUK void BookmarkOwner::fancyBookmark(KBookmark bookmark) { - Nepomuk2::Resource nfoResource = (KUrl)bookmark.url(); + Nepomuk2::Resource nfoResource = (KUrl)bookmark.url(); - QPointer<Nepomuk2::ResourceLinkDialog> r = new Nepomuk2::ResourceLinkDialog(nfoResource); - r->exec(); + QPointer<Nepomuk2::ResourceLinkDialog> r = new Nepomuk2::ResourceLinkDialog(nfoResource); + r->exec(); - r->deleteLater(); + r->deleteLater(); } #endif bool BookmarkOwner::deleteBookmark(const KBookmark &bookmark) { - if (bookmark.isNull()) - return false; - - KBookmarkGroup bmg = bookmark.parentGroup(); - QString dialogCaption, dialogText; - - if (bookmark.isGroup()) - { - dialogCaption = i18n("Bookmark Folder Deletion"); - dialogText = i18n("Are you sure you wish to remove the bookmark folder\n\"%1\"?", bookmark.fullText()); - } - else if (bookmark.isSeparator()) - { - dialogCaption = i18n("Separator Deletion"); - dialogText = i18n("Are you sure you wish to remove this separator?"); - } - else - { - dialogCaption = i18n("Bookmark Deletion"); - dialogText = i18n("Are you sure you wish to remove the bookmark\n\"%1\"?", bookmark.fullText()); - } - - if (KMessageBox::warningContinueCancel( - 0, - dialogText, - dialogCaption, - KStandardGuiItem::del(), - KStandardGuiItem::cancel(), - "bookmarkDeletition_askAgain") - != KMessageBox::Continue - ) - return false; - - bmg.deleteBookmark(bookmark); + if (bookmark.isNull()) return false; + + KBookmarkGroup bmg = bookmark.parentGroup(); + QString dialogCaption, dialogText; + + if (bookmark.isGroup()) { + dialogCaption = i18n("Bookmark Folder Deletion"); + dialogText = i18n("Are you sure you wish to remove the bookmark folder\n\"%1\"?", bookmark.fullText()); + } + else if (bookmark.isSeparator()) { + dialogCaption = i18n("Separator Deletion"); + dialogText = i18n("Are you sure you wish to remove this separator?"); + } + else { + dialogCaption = i18n("Bookmark Deletion"); + dialogText = i18n("Are you sure you wish to remove the bookmark\n\"%1\"?", bookmark.fullText()); + } + + if (KMessageBox::warningContinueCancel(0, dialogText, dialogCaption, KStandardGuiItem::del(), + KStandardGuiItem::cancel(), + "bookmarkDeletition_askAgain") != KMessageBox::Continue) + return false; + + bmg.deleteBookmark(bookmark); #ifdef HAVE_NEPOMUK - Nepomuk2::Resource nfoResource(bookmark.url()); - nfoResource.remove(); + Nepomuk2::Resource nfoResource(bookmark.url()); + nfoResource.remove(); #endif - m_manager->emitChanged(bmg); - return true; + m_manager->emitChanged(bmg); + return true; } - void BookmarkOwner::setToolBarFolder(KBookmark bookmark) { - if (!bookmark.isGroup()) - return; + if (!bookmark.isGroup()) return; - unsetToolBarFolder(); - bookmark.internalElement().setAttribute("toolbar", "yes"); - bookmark.setIcon("bookmark-toolbar"); + unsetToolBarFolder(); + bookmark.internalElement().setAttribute("toolbar", "yes"); + bookmark.setIcon("bookmark-toolbar"); - m_manager->emitChanged(); + m_manager->emitChanged(); } - void BookmarkOwner::unsetToolBarFolder() { - KBookmarkGroup toolbar = m_manager->toolbar(); - if (!toolbar.isNull()) - { - toolbar.internalElement().setAttribute("toolbar", "no"); - toolbar.setIcon(""); - } - m_manager->emitChanged(); + KBookmarkGroup toolbar = m_manager->toolbar(); + if (!toolbar.isNull()) { + toolbar.internalElement().setAttribute("toolbar", "no"); + toolbar.setIcon(""); + } + m_manager->emitChanged(); } - -KAction* BookmarkOwner::createAction(const QString &text, const QString &icon, - const QString &help, const char *slot, +KAction *BookmarkOwner::createAction(const QString &text, const QString &icon, const QString &help, const char *slot, const KBookmark &bookmark) { - CustomBookmarkAction *act = new CustomBookmarkAction(bookmark, KIcon(icon), text, this); - act->setHelpText(help); - connect(act, SIGNAL(triggered(KBookmark)), this, slot); - return act; + CustomBookmarkAction *act = new CustomBookmarkAction(bookmark, KIcon(icon), text, this); + act->setHelpText(help); + connect(act, SIGNAL(triggered(KBookmark)), this, slot); + return act; } - // ------------------------------------------------------------------------------------------------- - -CustomBookmarkAction::CustomBookmarkAction(const KBookmark &bookmark, const KIcon &icon, const QString &text, QObject *parent) - : KAction(icon, text, parent) - , m_bookmark(bookmark) +CustomBookmarkAction::CustomBookmarkAction(const KBookmark &bookmark, const KIcon &icon, const QString &text, + QObject *parent) + : KAction(icon, text, parent), m_bookmark(bookmark) { - connect(this, SIGNAL(triggered()), this, SLOT(onActionTriggered())); + connect(this, SIGNAL(triggered()), this, SLOT(onActionTriggered())); } -void CustomBookmarkAction::onActionTriggered() -{ - emit triggered(m_bookmark); -} +void CustomBookmarkAction::onActionTriggered() { emit triggered(m_bookmark); } diff --git a/src/bookmarks/bookmarkscontextmenu.cpp b/src/bookmarks/bookmarkscontextmenu.cpp index 2ae74b5c..a9683708 100644 --- a/src/bookmarks/bookmarkscontextmenu.cpp +++ b/src/bookmarks/bookmarkscontextmenu.cpp @@ -1,159 +1,133 @@ /* ============================================================ -* -* This file is a part of the rekonq project -* -* Copyright (C) 2010 by Yoann Laissus <yoann dot laissus at gmail dot com> -* Copyright (c) 2011-2012 by Phaneendra Hegde <pnh.pes@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 2 of -* the License or (at your option) version 3 or any later version -* accepted by the membership of KDE e.V. (or its successor approved -* by the membership of KDE e.V.), which shall act as a proxy -* defined in Section 14 of version 3 of the license. -* -* 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/>. -* -* ============================================================ */ - + * + * This file is a part of the rekonq project + * + * Copyright (C) 2010 by Yoann Laissus <yoann dot laissus at gmail dot com> + * Copyright (c) 2011-2012 by Phaneendra Hegde <pnh.pes@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 2 of + * the License or (at your option) version 3 or any later version + * accepted by the membership of KDE e.V. (or its successor approved + * by the membership of KDE e.V.), which shall act as a proxy + * defined in Section 14 of version 3 of the license. + * + * 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/>. + * + * ============================================================ */ // Self Includes #include "bookmarkscontextmenu.h" // Local Includes -#include "bookmarkowner.h" #include "bookmarkmanager.h" +#include "bookmarkowner.h" // KDE Includes #include <KBookmarkManager> - -BookmarksContextMenu::BookmarksContextMenu(const KBookmark &bookmark, - KBookmarkManager *manager, - BookmarkOwner *owner, - bool nullForced, - QWidget *parent) - : KBookmarkContextMenu(bookmark, manager, owner, parent) - , m_bmOwner(owner) - , m_nullForced(nullForced) +BookmarksContextMenu::BookmarksContextMenu(const KBookmark &bookmark, KBookmarkManager *manager, BookmarkOwner *owner, + bool nullForced, QWidget *parent) + : KBookmarkContextMenu(bookmark, manager, owner, parent), m_bmOwner(owner), m_nullForced(nullForced) { } - void BookmarksContextMenu::addBookmarkActions() { - addAction(m_bmOwner->createAction(bookmark(), BookmarkOwner::OPEN_IN_TAB)); - addAction(m_bmOwner->createAction(bookmark(), BookmarkOwner::OPEN_IN_WINDOW)); + addAction(m_bmOwner->createAction(bookmark(), BookmarkOwner::OPEN_IN_TAB)); + addAction(m_bmOwner->createAction(bookmark(), BookmarkOwner::OPEN_IN_WINDOW)); - addSeparator(); + addSeparator(); - addAction(m_bmOwner->createAction(bookmark(), BookmarkOwner::BOOKMARK_PAGE)); - addAction(m_bmOwner->createAction(bookmark(), BookmarkOwner::NEW_FOLDER)); - addAction(m_bmOwner->createAction(bookmark(), BookmarkOwner::NEW_SEPARATOR)); + addAction(m_bmOwner->createAction(bookmark(), BookmarkOwner::BOOKMARK_PAGE)); + addAction(m_bmOwner->createAction(bookmark(), BookmarkOwner::NEW_FOLDER)); + addAction(m_bmOwner->createAction(bookmark(), BookmarkOwner::NEW_SEPARATOR)); - addSeparator(); + addSeparator(); - addAction(m_bmOwner->createAction(bookmark(), BookmarkOwner::COPY)); + addAction(m_bmOwner->createAction(bookmark(), BookmarkOwner::COPY)); - addSeparator(); + addSeparator(); - addAction(m_bmOwner->createAction(bookmark(), BookmarkOwner::EDIT)); + addAction(m_bmOwner->createAction(bookmark(), BookmarkOwner::EDIT)); #ifdef HAVE_NEPOMUK - addAction(m_bmOwner->createAction(bookmark(), BookmarkOwner::FANCYBOOKMARK)); + addAction(m_bmOwner->createAction(bookmark(), BookmarkOwner::FANCYBOOKMARK)); #endif - addAction(m_bmOwner->createAction(bookmark(), BookmarkOwner::DELETE)); + addAction(m_bmOwner->createAction(bookmark(), BookmarkOwner::DELETE)); } - void BookmarksContextMenu::addFolderActions() { - KBookmarkGroup group = bookmark().toGroup(); + KBookmarkGroup group = bookmark().toGroup(); - if (bookmark().internalElement().attributeNode("toolbar").value() == "yes") - { - addAction(m_bmOwner->createAction(bookmark(), BookmarkOwner::UNSET_TOOLBAR_FOLDER)); - } - else - { - addAction(m_bmOwner->createAction(bookmark(), BookmarkOwner::SET_TOOLBAR_FOLDER)); - } + if (bookmark().internalElement().attributeNode("toolbar").value() == "yes") { + addAction(m_bmOwner->createAction(bookmark(), BookmarkOwner::UNSET_TOOLBAR_FOLDER)); + } + else { + addAction(m_bmOwner->createAction(bookmark(), BookmarkOwner::SET_TOOLBAR_FOLDER)); + } - if (!group.first().isNull()) - { - KBookmark child = group.first(); + if (!group.first().isNull()) { + KBookmark child = group.first(); - while (child.isGroup() || child.isSeparator()) - { - child = group.next(child); - } + while (child.isGroup() || child.isSeparator()) { child = group.next(child); } - if (!child.isNull()) - { - addAction(m_bmOwner->createAction(bookmark(), BookmarkOwner::OPEN_FOLDER)); - addSeparator(); - } + if (!child.isNull()) { + addAction(m_bmOwner->createAction(bookmark(), BookmarkOwner::OPEN_FOLDER)); + addSeparator(); } + } - addAction(m_bmOwner->createAction(bookmark(), BookmarkOwner::BOOKMARK_PAGE)); - addAction(m_bmOwner->createAction(bookmark(), BookmarkOwner::NEW_FOLDER)); - addAction(m_bmOwner->createAction(bookmark(), BookmarkOwner::NEW_SEPARATOR)); + addAction(m_bmOwner->createAction(bookmark(), BookmarkOwner::BOOKMARK_PAGE)); + addAction(m_bmOwner->createAction(bookmark(), BookmarkOwner::NEW_FOLDER)); + addAction(m_bmOwner->createAction(bookmark(), BookmarkOwner::NEW_SEPARATOR)); - addSeparator(); + addSeparator(); - addAction(m_bmOwner->createAction(bookmark(), BookmarkOwner::EDIT)); - addAction(m_bmOwner->createAction(bookmark(), BookmarkOwner::DELETE)); + addAction(m_bmOwner->createAction(bookmark(), BookmarkOwner::EDIT)); + addAction(m_bmOwner->createAction(bookmark(), BookmarkOwner::DELETE)); } - void BookmarksContextMenu::addSeparatorActions() { - addAction(m_bmOwner->createAction(bookmark(), BookmarkOwner::BOOKMARK_PAGE)); - addAction(m_bmOwner->createAction(bookmark(), BookmarkOwner::NEW_FOLDER)); - addAction(m_bmOwner->createAction(bookmark(), BookmarkOwner::NEW_SEPARATOR)); + addAction(m_bmOwner->createAction(bookmark(), BookmarkOwner::BOOKMARK_PAGE)); + addAction(m_bmOwner->createAction(bookmark(), BookmarkOwner::NEW_FOLDER)); + addAction(m_bmOwner->createAction(bookmark(), BookmarkOwner::NEW_SEPARATOR)); - addSeparator(); + addSeparator(); - addAction(m_bmOwner->createAction(bookmark(), BookmarkOwner::DELETE)); + addAction(m_bmOwner->createAction(bookmark(), BookmarkOwner::DELETE)); } - void BookmarksContextMenu::addNullActions() { - KBookmarkManager *mngr = manager(); - if (mngr->toolbar().hasParent()) - { - addAction(m_bmOwner->createAction(bookmark(), BookmarkOwner::UNSET_TOOLBAR_FOLDER)); - } - addAction(m_bmOwner->createAction(bookmark(), BookmarkOwner::BOOKMARK_PAGE)); - addAction(m_bmOwner->createAction(bookmark(), BookmarkOwner::NEW_FOLDER)); - addAction(m_bmOwner->createAction(bookmark(), BookmarkOwner::NEW_SEPARATOR)); + KBookmarkManager *mngr = manager(); + if (mngr->toolbar().hasParent()) { + addAction(m_bmOwner->createAction(bookmark(), BookmarkOwner::UNSET_TOOLBAR_FOLDER)); + } + addAction(m_bmOwner->createAction(bookmark(), BookmarkOwner::BOOKMARK_PAGE)); + addAction(m_bmOwner->createAction(bookmark(), BookmarkOwner::NEW_FOLDER)); + addAction(m_bmOwner->createAction(bookmark(), BookmarkOwner::NEW_SEPARATOR)); } - void BookmarksContextMenu::addActions() { - if (bookmark().isNull() || m_nullForced) - { - addNullActions(); - } - else if (bookmark().isSeparator()) - { - addSeparatorActions(); - } - else if (bookmark().isGroup()) - { - addFolderActions(); - } - else - { - addBookmarkActions(); - } + if (bookmark().isNull() || m_nullForced) { addNullActions(); } + else if (bookmark().isSeparator()) { + addSeparatorActions(); + } + else if (bookmark().isGroup()) { + addFolderActions(); + } + else { + addBookmarkActions(); + } } diff --git a/src/bookmarks/bookmarksmenu.cpp b/src/bookmarks/bookmarksmenu.cpp index 1f89df04..445b81b7 100644 --- a/src/bookmarks/bookmarksmenu.cpp +++ b/src/bookmarks/bookmarksmenu.cpp @@ -1,159 +1,133 @@ /* ============================================================ -* -* This file is a part of the rekonq project -* -* Copyright (C) 2008-2013 by Andrea Diamantini <adjam7 at gmail dot com> -* Copyright (C) 2010 by Yoann Laissus <yoann dot laissus at gmail dot 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 2 of -* the License or (at your option) version 3 or any later version -* accepted by the membership of KDE e.V. (or its successor approved -* by the membership of KDE e.V.), which shall act as a proxy -* defined in Section 14 of version 3 of the license. -* -* 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/>. -* -* ============================================================ */ - + * + * This file is a part of the rekonq project + * + * Copyright (C) 2008-2013 by Andrea Diamantini <adjam7 at gmail dot com> + * Copyright (C) 2010 by Yoann Laissus <yoann dot laissus at gmail dot 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 2 of + * the License or (at your option) version 3 or any later version + * accepted by the membership of KDE e.V. (or its successor approved + * by the membership of KDE e.V.), which shall act as a proxy + * defined in Section 14 of version 3 of the license. + * + * 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/>. + * + * ============================================================ */ // Self Includes #include "bookmarksmenu.h" #include "bookmarksmenu.moc" // Local Includes -#include "bookmarkscontextmenu.h" #include "bookmarkmanager.h" #include "bookmarkowner.h" +#include "bookmarkscontextmenu.h" #include "iconmanager.h" #include "webwindow.h" // Qt Includes -#include <QFrame> #include <QActionEvent> #include <QApplication> +#include <QFrame> - -BookmarkMenu::BookmarkMenu(KBookmarkManager *manager, - KBookmarkOwner *owner, - KMenu *menu, - KActionCollection* actionCollection) +BookmarkMenu::BookmarkMenu(KBookmarkManager *manager, KBookmarkOwner *owner, KMenu *menu, + KActionCollection *actionCollection) : KBookmarkMenu(manager, owner, menu, actionCollection) { } - -BookmarkMenu::BookmarkMenu(KBookmarkManager *manager, - KBookmarkOwner *owner, - KMenu *parentMenu, +BookmarkMenu::BookmarkMenu(KBookmarkManager *manager, KBookmarkOwner *owner, KMenu *parentMenu, const QString &parentAddress) : KBookmarkMenu(manager, owner, parentMenu, parentAddress) { } - BookmarkMenu::~BookmarkMenu() { - kDebug() << "Deleting BookmarkMenu.. See http://svn.reviewboard.kde.org/r/5606/ about."; + kDebug() << "Deleting BookmarkMenu.. See http://svn.reviewboard.kde.org/r/5606/ about."; } - -KMenu * BookmarkMenu::contextMenu(QAction *act) +KMenu *BookmarkMenu::contextMenu(QAction *act) { - KBookmarkActionInterface* action = dynamic_cast<KBookmarkActionInterface *>(act); - if (!action) - return 0; - return new BookmarksContextMenu(action->bookmark(), manager(), static_cast<BookmarkOwner*>(owner())); + KBookmarkActionInterface *action = dynamic_cast<KBookmarkActionInterface *>(act); + if (!action) return 0; + return new BookmarksContextMenu(action->bookmark(), manager(), static_cast<BookmarkOwner *>(owner())); } - -QAction * BookmarkMenu::actionForBookmark(const KBookmark &bookmark) +QAction *BookmarkMenu::actionForBookmark(const KBookmark &bookmark) { - if (bookmark.isGroup()) - { - KBookmarkActionMenu *actionMenu = new KBookmarkActionMenu(bookmark, this); - BookmarkMenu *menu = new BookmarkMenu(manager(), owner(), actionMenu->menu(), bookmark.address()); - // An hack to get rid of bug 219274 - connect(actionMenu, SIGNAL(hovered()), menu, SLOT(slotAboutToShow())); - return actionMenu; - } - else if (bookmark.isSeparator()) - { - return KBookmarkMenu::actionForBookmark(bookmark); - } - else - { - KBookmarkAction *action = new KBookmarkAction(bookmark, owner(), this); - action->setIcon(IconManager::self()->iconForUrl(KUrl(bookmark.url()))); - return action; - } + if (bookmark.isGroup()) { + KBookmarkActionMenu *actionMenu = new KBookmarkActionMenu(bookmark, this); + BookmarkMenu *menu = new BookmarkMenu(manager(), owner(), actionMenu->menu(), bookmark.address()); + // An hack to get rid of bug 219274 + connect(actionMenu, SIGNAL(hovered()), menu, SLOT(slotAboutToShow())); + return actionMenu; + } + else if (bookmark.isSeparator()) { + return KBookmarkMenu::actionForBookmark(bookmark); + } + else { + KBookmarkAction *action = new KBookmarkAction(bookmark, owner(), this); + action->setIcon(IconManager::self()->iconForUrl(KUrl(bookmark.url()))); + return action; + } } - void BookmarkMenu::refill() { - clear(); - - if (isRoot()) - { - addAddBookmark(); - addAddBookmarksList(); - - if (parentMenu()->actions().count() > 0) - parentMenu()->addSeparator(); - - WebWindow *w = qobject_cast<WebWindow *>(parentMenu()->parent()); - QAction *a; - // bk page - a = w->actionByName(QL1S("open_bookmarks_page")); - parentMenu()->addAction(a); - a = w->actionByName(QL1S("show_bookmarks_toolbar")); - parentMenu()->addAction(a); - - addEditBookmarks(); - - if (parentMenu()->actions().count() > 0) - parentMenu()->addSeparator(); - } + clear(); - fillBookmarks(); - - if (!isRoot()) - { - if (parentMenu()->actions().count() > 0) - parentMenu()->addSeparator(); + if (isRoot()) { + addAddBookmark(); + addAddBookmarksList(); - addOpenFolderInTabs(); - addAddBookmarksList(); - } -} + if (parentMenu()->actions().count() > 0) parentMenu()->addSeparator(); + + WebWindow *w = qobject_cast<WebWindow *>(parentMenu()->parent()); + QAction *a; + // bk page + a = w->actionByName(QL1S("open_bookmarks_page")); + parentMenu()->addAction(a); + a = w->actionByName(QL1S("show_bookmarks_toolbar")); + parentMenu()->addAction(a); + addEditBookmarks(); + + if (parentMenu()->actions().count() > 0) parentMenu()->addSeparator(); + } + + fillBookmarks(); + + if (!isRoot()) { + if (parentMenu()->actions().count() > 0) parentMenu()->addSeparator(); + + addOpenFolderInTabs(); + addAddBookmarksList(); + } +} void BookmarkMenu::addOpenFolderInTabs() { - KBookmarkGroup group = manager()->findByAddress(parentAddress()).toGroup(); + KBookmarkGroup group = manager()->findByAddress(parentAddress()).toGroup(); - if (!group.first().isNull()) - { - KBookmark bookmark = group.first(); + if (!group.first().isNull()) { + KBookmark bookmark = group.first(); - while (bookmark.isGroup() || bookmark.isSeparator()) - { - bookmark = group.next(bookmark); - } + while (bookmark.isGroup() || bookmark.isSeparator()) { bookmark = group.next(bookmark); } - if (!bookmark.isNull()) - { - parentMenu()->addAction(BookmarkManager::self()->owner()->createAction(group, BookmarkOwner::OPEN_FOLDER)); - } + if (!bookmark.isNull()) { + parentMenu()->addAction(BookmarkManager::self()->owner()->createAction(group, BookmarkOwner::OPEN_FOLDER)); } + } } diff --git a/src/bookmarks/bookmarkstoolbar.cpp b/src/bookmarks/bookmarkstoolbar.cpp index 5c45daeb..658aa0c0 100644 --- a/src/bookmarks/bookmarkstoolbar.cpp +++ b/src/bookmarks/bookmarkstoolbar.cpp @@ -1,38 +1,37 @@ /* ============================================================ -* -* This file is a part of the rekonq project -* -* Copyright (C) 2008-2013 by Andrea Diamantini <adjam7 at gmail dot com> -* Copyright (C) 2010 by Yoann Laissus <yoann dot laissus at gmail dot 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 2 of -* the License or (at your option) version 3 or any later version -* accepted by the membership of KDE e.V. (or its successor approved -* by the membership of KDE e.V.), which shall act as a proxy -* defined in Section 14 of version 3 of the license. -* -* 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/>. -* -* ============================================================ */ - + * + * This file is a part of the rekonq project + * + * Copyright (C) 2008-2013 by Andrea Diamantini <adjam7 at gmail dot com> + * Copyright (C) 2010 by Yoann Laissus <yoann dot laissus at gmail dot 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 2 of + * the License or (at your option) version 3 or any later version + * accepted by the membership of KDE e.V. (or its successor approved + * by the membership of KDE e.V.), which shall act as a proxy + * defined in Section 14 of version 3 of the license. + * + * 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/>. + * + * ============================================================ */ // Self Includes #include "bookmarkstoolbar.h" #include "bookmarkstoolbar.moc" // Local Includes -#include "bookmarkscontextmenu.h" #include "bookmarkmanager.h" #include "bookmarkowner.h" +#include "bookmarkscontextmenu.h" #include "iconmanager.h" #include "webwindow.h" @@ -41,486 +40,365 @@ #include <KMenu> // Qt Includes -#include <QFrame> #include <QActionEvent> #include <QApplication> - +#include <QFrame> BookmarkToolBar::BookmarkToolBar(QWidget *parent) - : KToolBar(parent, false, false) - , m_currentMenu(0) - , m_dragAction(0) - , m_dropAction(0) - , m_checkedAction(0) - , m_filled(false) + : KToolBar(parent, false, false), m_currentMenu(0), m_dragAction(0), m_dropAction(0), m_checkedAction(0), + m_filled(false) { - setContextMenuPolicy(Qt::CustomContextMenu); + setContextMenuPolicy(Qt::CustomContextMenu); - connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextMenu(QPoint))); - connect(BookmarkManager::self()->manager(), SIGNAL(changed(QString,QString)), this, SLOT(hideMenu())); + connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextMenu(QPoint))); + connect(BookmarkManager::self()->manager(), SIGNAL(changed(QString, QString)), this, SLOT(hideMenu())); - setAcceptDrops(true); - installEventFilter(this); - setShortcutEnabled(false); + setAcceptDrops(true); + installEventFilter(this); + setShortcutEnabled(false); - if (isVisible()) - { - BookmarkManager::self()->fillBookmarkBar(this); - m_filled = true; - } -} - - -BookmarkToolBar::~BookmarkToolBar() -{ - clear(); + if (isVisible()) { + BookmarkManager::self()->fillBookmarkBar(this); + m_filled = true; + } } +BookmarkToolBar::~BookmarkToolBar() { clear(); } void BookmarkToolBar::contextMenu(const QPoint &point) { - KBookmarkActionInterface *action = dynamic_cast<KBookmarkActionInterface*>(actionAt(point)); - KBookmark bookmark = BookmarkManager::self()->manager()->toolbar(); - bool nullAction = true; - if (action) - { - bookmark = action->bookmark(); - nullAction = false; - } + KBookmarkActionInterface *action = dynamic_cast<KBookmarkActionInterface *>(actionAt(point)); + KBookmark bookmark = BookmarkManager::self()->manager()->toolbar(); + bool nullAction = true; + if (action) { + bookmark = action->bookmark(); + nullAction = false; + } - BookmarksContextMenu menu(bookmark, - BookmarkManager::self()->manager(), - BookmarkManager::self()->owner(), - nullAction); + BookmarksContextMenu menu(bookmark, BookmarkManager::self()->manager(), BookmarkManager::self()->owner(), nullAction); - menu.exec(mapToGlobal(point)); + menu.exec(mapToGlobal(point)); } - void BookmarkToolBar::menuDisplayed() { - qApp->installEventFilter(this); - m_currentMenu = qobject_cast<KMenu*>(sender()); + qApp->installEventFilter(this); + m_currentMenu = qobject_cast<KMenu *>(sender()); } - void BookmarkToolBar::menuHidden() { - qApp->removeEventFilter(this); - m_currentMenu = 0; + qApp->removeEventFilter(this); + m_currentMenu = 0; } - void BookmarkToolBar::hideMenu() { - if (m_currentMenu) - m_currentMenu->hide(); + if (m_currentMenu) m_currentMenu->hide(); } - bool BookmarkToolBar::eventFilter(QObject *watched, QEvent *event) { - if (m_currentMenu && m_currentMenu->isVisible() - && !m_currentMenu->rect().contains(m_currentMenu->mapFromGlobal(QCursor::pos()))) - { - // To switch root folders as in a menubar + if (m_currentMenu && m_currentMenu->isVisible() && + !m_currentMenu->rect().contains(m_currentMenu->mapFromGlobal(QCursor::pos()))) { + // To switch root folders as in a menubar - KBookmarkActionMenu* act = dynamic_cast<KBookmarkActionMenu *>(actionAt(mapFromGlobal(QCursor::pos()))); + KBookmarkActionMenu *act = dynamic_cast<KBookmarkActionMenu *>(actionAt(mapFromGlobal(QCursor::pos()))); - if (event->type() == QEvent::MouseMove && act && act->menu() != m_currentMenu) - { - m_currentMenu->hide(); - QPoint pos = mapToGlobal(widgetForAction(act)->pos()); - act->menu()->popup(QPoint(pos.x(), pos.y() + widgetForAction(act)->height())); - } - else if (event->type() == QEvent::MouseButtonPress && act) - { - m_currentMenu->hide(); - } - - return QObject::eventFilter(watched, event); + if (event->type() == QEvent::MouseMove && act && act->menu() != m_currentMenu) { + m_currentMenu->hide(); + QPoint pos = mapToGlobal(widgetForAction(act)->pos()); + act->menu()->popup(QPoint(pos.x(), pos.y() + widgetForAction(act)->height())); } - - switch (event->type()) - { - case QEvent::Show: - { - if (!m_filled) - { - BookmarkManager::self()->fillBookmarkBar(this); - m_filled = true; - } + else if (event->type() == QEvent::MouseButtonPress && act) { + m_currentMenu->hide(); } - break; - case QEvent::ActionRemoved: - { - QActionEvent *actionEvent = static_cast<QActionEvent*>(event); - if (actionEvent && actionEvent->action() != m_dropAction) - { - QWidget *widget = widgetForAction(actionEvent->action()); - if (widget) - { - widget->removeEventFilter(this); - } - } - } - break; + return QObject::eventFilter(watched, event); + } - case QEvent::ParentChange: - { - QActionEvent *actionEvent = static_cast<QActionEvent*>(event); - if (actionEvent && actionEvent->action() != m_dropAction) - { - QWidget *widget = widgetForAction(actionEvent->action()); - if (widget) - { - widget->removeEventFilter(this); - } - } + switch (event->type()) { + case QEvent::Show: { + if (!m_filled) { + BookmarkManager::self()->fillBookmarkBar(this); + m_filled = true; } - break; + } break; - case QEvent::DragEnter: - { - QDragEnterEvent *dragEvent = static_cast<QDragEnterEvent*>(event); - if (dragEvent->mimeData()->hasFormat(BookmarkManager::bookmark_mime_type()) - || dragEvent->mimeData()->hasFormat("text/uri-list") - || dragEvent->mimeData()->hasFormat("text/plain")) - { - QFrame* dropIndicatorWidget = new QFrame(this); - dropIndicatorWidget->setFrameShape(QFrame::VLine); - m_dropAction = insertWidget(actionAt(dragEvent->pos()), dropIndicatorWidget); - - dragEvent->accept(); - } + case QEvent::ActionRemoved: { + QActionEvent *actionEvent = static_cast<QActionEvent *>(event); + if (actionEvent && actionEvent->action() != m_dropAction) { + QWidget *widget = widgetForAction(actionEvent->action()); + if (widget) { widget->removeEventFilter(this); } } - break; + } break; - case QEvent::DragLeave: - { - QDragLeaveEvent *dragEvent = static_cast<QDragLeaveEvent*>(event); + case QEvent::ParentChange: { + QActionEvent *actionEvent = static_cast<QActionEvent *>(event); + if (actionEvent && actionEvent->action() != m_dropAction) { + QWidget *widget = widgetForAction(actionEvent->action()); + if (widget) { widget->removeEventFilter(this); } + } + } break; - if (m_checkedAction) - { - m_checkedAction->setCheckable(false); - m_checkedAction->setChecked(false); - } + case QEvent::DragEnter: { + QDragEnterEvent *dragEvent = static_cast<QDragEnterEvent *>(event); + if (dragEvent->mimeData()->hasFormat(BookmarkManager::bookmark_mime_type()) || + dragEvent->mimeData()->hasFormat("text/uri-list") || dragEvent->mimeData()->hasFormat("text/plain")) { + QFrame *dropIndicatorWidget = new QFrame(this); + dropIndicatorWidget->setFrameShape(QFrame::VLine); + m_dropAction = insertWidget(actionAt(dragEvent->pos()), dropIndicatorWidget); - delete m_dropAction; - m_dropAction = 0; - dragEvent->accept(); + dragEvent->accept(); } - break; + } break; - case QEvent::DragMove: - { - QDragMoveEvent *dragEvent = static_cast<QDragMoveEvent*>(event); - if (dragEvent->mimeData()->hasFormat(BookmarkManager::bookmark_mime_type()) - || dragEvent->mimeData()->hasFormat("text/uri-list") - || dragEvent->mimeData()->hasFormat("text/plain")) - { - QAction *overAction = actionAt(dragEvent->pos()); - KBookmarkActionInterface *overActionBK = dynamic_cast<KBookmarkActionInterface*>(overAction); - QWidget *widgetAction = widgetForAction(overAction); - - if (overAction != m_dropAction && overActionBK && widgetAction && m_dropAction) - { - removeAction(m_dropAction); - if (m_checkedAction) - { - m_checkedAction->setCheckable(false); - m_checkedAction->setChecked(false); - } - - if (!overActionBK->bookmark().isGroup()) - { - if ((dragEvent->pos().x() - widgetAction->pos().x()) > (widgetAction->width() / 2)) - { - if (actions().count() > actions().indexOf(overAction) + 1) - { - insertAction(actions().at(actions().indexOf(overAction) + 1), m_dropAction); - } - else - { - addAction(m_dropAction); - } - } - else - { - insertAction(overAction, m_dropAction); - } - } - else - { - if ((dragEvent->pos().x() - widgetAction->pos().x()) >= (widgetAction->width() * 0.75)) - { - if (actions().count() > actions().indexOf(overAction) + 1) - { - insertAction(actions().at(actions().indexOf(overAction) + 1), m_dropAction); - } - else - { - addAction(m_dropAction); - } - } - else if ((dragEvent->pos().x() - widgetAction->pos().x()) <= (widgetAction->width() * 0.25)) - { - insertAction(overAction, m_dropAction); - } - else - { - overAction->setCheckable(true); - overAction->setChecked(true); - m_checkedAction = overAction; - } - } - - dragEvent->accept(); - } - } - } - break; + case QEvent::DragLeave: { + QDragLeaveEvent *dragEvent = static_cast<QDragLeaveEvent *>(event); + if (m_checkedAction) { + m_checkedAction->setCheckable(false); + m_checkedAction->setChecked(false); + } - case QEvent::Drop: - { - QDropEvent *dropEvent = static_cast<QDropEvent*>(event); - KBookmark bookmark; - KBookmarkGroup root = BookmarkManager::self()->manager()->toolbar(); - - if (m_checkedAction) - { - m_checkedAction->setCheckable(false); - m_checkedAction->setChecked(false); + delete m_dropAction; + m_dropAction = 0; + dragEvent->accept(); + } break; + + case QEvent::DragMove: { + QDragMoveEvent *dragEvent = static_cast<QDragMoveEvent *>(event); + if (dragEvent->mimeData()->hasFormat(BookmarkManager::bookmark_mime_type()) || + dragEvent->mimeData()->hasFormat("text/uri-list") || dragEvent->mimeData()->hasFormat("text/plain")) { + QAction *overAction = actionAt(dragEvent->pos()); + KBookmarkActionInterface *overActionBK = dynamic_cast<KBookmarkActionInterface *>(overAction); + QWidget *widgetAction = widgetForAction(overAction); + + if (overAction != m_dropAction && overActionBK && widgetAction && m_dropAction) { + removeAction(m_dropAction); + if (m_checkedAction) { + m_checkedAction->setCheckable(false); + m_checkedAction->setChecked(false); } - if (dropEvent->mimeData()->hasFormat(BookmarkManager::bookmark_mime_type())) - { - QByteArray addresses = dropEvent->mimeData()->data(BookmarkManager::bookmark_mime_type()); - bookmark = BookmarkManager::self()->findByAddress(QString::fromLatin1(addresses.data())); - if (bookmark.isNull()) - return false; - } - else if (dropEvent->mimeData()->hasFormat("text/uri-list")) - { - // DROP is URL - QString url = dropEvent->mimeData()->urls().at(0).toString(); - WebWindow *w = qobject_cast<WebWindow *>(parent()); - QString title = url.contains(w->url().url()) - ? w->title() - : url; - bookmark = root.addBookmark(title, url); - } - else if (dropEvent->mimeData()->hasFormat("text/plain")) - { - // DROP is TEXT - QString url = dropEvent->mimeData()->text(); - KUrl u(url); - if (u.isValid()) - { - WebWindow *w = qobject_cast<WebWindow *>(parent()); - QString title = url.contains(w->url().url()) - ? w->title() - : url; - bookmark = root.addBookmark(title, url); + if (!overActionBK->bookmark().isGroup()) { + if ((dragEvent->pos().x() - widgetAction->pos().x()) > (widgetAction->width() / 2)) { + if (actions().count() > actions().indexOf(overAction) + 1) { + insertAction(actions().at(actions().indexOf(overAction) + 1), m_dropAction); } + else { + addAction(m_dropAction); + } + } + else { + insertAction(overAction, m_dropAction); + } } - else - { - return false; - } - - QAction *destAction = actionAt(dropEvent->pos()); - if (destAction && destAction == m_dropAction) - { - if (actions().indexOf(m_dropAction) > 0) - { - destAction = actions().at(actions().indexOf(m_dropAction) - 1); + else { + if ((dragEvent->pos().x() - widgetAction->pos().x()) >= (widgetAction->width() * 0.75)) { + if (actions().count() > actions().indexOf(overAction) + 1) { + insertAction(actions().at(actions().indexOf(overAction) + 1), m_dropAction); } - else - { - destAction = actions().at(1); + else { + addAction(m_dropAction); } + } + else if ((dragEvent->pos().x() - widgetAction->pos().x()) <= (widgetAction->width() * 0.25)) { + insertAction(overAction, m_dropAction); + } + else { + overAction->setCheckable(true); + overAction->setChecked(true); + m_checkedAction = overAction; + } } - if (destAction) - { - KBookmarkActionInterface *destBookmarkAction = dynamic_cast<KBookmarkActionInterface *>(destAction); - QWidget *widgetAction = widgetForAction(destAction); - - if (destBookmarkAction && !destBookmarkAction->bookmark().isNull() && widgetAction - && bookmark.address() != destBookmarkAction->bookmark().address()) - { - KBookmark destBookmark = destBookmarkAction->bookmark(); - - if (!destBookmark.isGroup()) - { - if ((dropEvent->pos().x() - widgetAction->pos().x()) >= (widgetAction->width() / 2)) - { - root.moveBookmark(bookmark, destBookmark); - } - else - { - root.moveBookmark(bookmark, destBookmark.parentGroup().previous(destBookmark)); - } - } - else - { - if ((dropEvent->pos().x() - widgetAction->pos().x()) >= (widgetAction->width() * 0.75)) - { - root.moveBookmark(bookmark, destBookmark); - } - else if ((dropEvent->pos().x() - widgetAction->pos().x()) <= (widgetAction->width() * 0.25)) - { - root.moveBookmark(bookmark, destBookmark.parentGroup().previous(destBookmark)); - } - else - { - destBookmark.toGroup().addBookmark(bookmark); - } - } - - - BookmarkManager::self()->emitChanged(); - } - } - else - { - root.deleteBookmark(bookmark); - bookmark = root.addBookmark(bookmark); - if (dropEvent->pos().x() < widgetForAction(actions().first())->pos().x()) - { - root.moveBookmark(bookmark, KBookmark()); - } + dragEvent->accept(); + } + } + } break; - BookmarkManager::self()->emitChanged(); - } - dropEvent->accept(); + case QEvent::Drop: { + QDropEvent *dropEvent = static_cast<QDropEvent *>(event); + KBookmark bookmark; + KBookmarkGroup root = BookmarkManager::self()->manager()->toolbar(); + + if (m_checkedAction) { + m_checkedAction->setCheckable(false); + m_checkedAction->setChecked(false); } - break; - default: - break; + if (dropEvent->mimeData()->hasFormat(BookmarkManager::bookmark_mime_type())) { + QByteArray addresses = dropEvent->mimeData()->data(BookmarkManager::bookmark_mime_type()); + bookmark = BookmarkManager::self()->findByAddress(QString::fromLatin1(addresses.data())); + if (bookmark.isNull()) return false; + } + else if (dropEvent->mimeData()->hasFormat("text/uri-list")) { + // DROP is URL + QString url = dropEvent->mimeData()->urls().at(0).toString(); + WebWindow *w = qobject_cast<WebWindow *>(parent()); + QString title = url.contains(w->url().url()) ? w->title() : url; + bookmark = root.addBookmark(title, url); + } + else if (dropEvent->mimeData()->hasFormat("text/plain")) { + // DROP is TEXT + QString url = dropEvent->mimeData()->text(); + KUrl u(url); + if (u.isValid()) { + WebWindow *w = qobject_cast<WebWindow *>(parent()); + QString title = url.contains(w->url().url()) ? w->title() : url; + bookmark = root.addBookmark(title, url); + } + } + else { + return false; } - QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event); + QAction *destAction = actionAt(dropEvent->pos()); + if (destAction && destAction == m_dropAction) { + if (actions().indexOf(m_dropAction) > 0) { destAction = actions().at(actions().indexOf(m_dropAction) - 1); } + else { + destAction = actions().at(1); + } + } - // These events need to be handled only for Bookmark actions and not the bar - if (watched != this && mouseEvent) - { - switch (event->type()) - { - case QEvent::MouseButtonPress: // drag handling - { - QPoint pos = mapFromGlobal(QCursor::pos()); - KBookmarkActionInterface *action = dynamic_cast<KBookmarkActionInterface *>(actionAt(pos)); - - if (action && mouseEvent->button() != Qt::MidButton) - { - m_dragAction = actionAt(pos); - m_startDragPos = pos; - - // The menu is displayed only when the mouse button is released - if (action->bookmark().isGroup()) - return true; - } + if (destAction) { + KBookmarkActionInterface *destBookmarkAction = dynamic_cast<KBookmarkActionInterface *>(destAction); + QWidget *widgetAction = widgetForAction(destAction); + + if (destBookmarkAction && !destBookmarkAction->bookmark().isNull() && widgetAction && + bookmark.address() != destBookmarkAction->bookmark().address()) { + KBookmark destBookmark = destBookmarkAction->bookmark(); + + if (!destBookmark.isGroup()) { + if ((dropEvent->pos().x() - widgetAction->pos().x()) >= (widgetAction->width() / 2)) { + root.moveBookmark(bookmark, destBookmark); + } + else { + root.moveBookmark(bookmark, destBookmark.parentGroup().previous(destBookmark)); + } } - break; - - case QEvent::MouseMove: - { - int distance = (mapFromGlobal(QCursor::pos()) - m_startDragPos).manhattanLength(); - if (!m_currentMenu && distance >= QApplication::startDragDistance()) - { - startDrag(); - } + else { + if ((dropEvent->pos().x() - widgetAction->pos().x()) >= (widgetAction->width() * 0.75)) { + root.moveBookmark(bookmark, destBookmark); + } + else if ((dropEvent->pos().x() - widgetAction->pos().x()) <= (widgetAction->width() * 0.25)) { + root.moveBookmark(bookmark, destBookmark.parentGroup().previous(destBookmark)); + } + else { + destBookmark.toGroup().addBookmark(bookmark); + } + } + + BookmarkManager::self()->emitChanged(); + } + } + else { + root.deleteBookmark(bookmark); + bookmark = root.addBookmark(bookmark); + if (dropEvent->pos().x() < widgetForAction(actions().first())->pos().x()) { + root.moveBookmark(bookmark, KBookmark()); + } + + BookmarkManager::self()->emitChanged(); + } + dropEvent->accept(); + } break; + + default: + break; + } + + QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event); + + // These events need to be handled only for Bookmark actions and not the bar + if (watched != this && mouseEvent) { + switch (event->type()) { + case QEvent::MouseButtonPress: // drag handling + { + QPoint pos = mapFromGlobal(QCursor::pos()); + KBookmarkActionInterface *action = dynamic_cast<KBookmarkActionInterface *>(actionAt(pos)); + + if (action && mouseEvent->button() != Qt::MidButton) { + m_dragAction = actionAt(pos); + m_startDragPos = pos; + + // The menu is displayed only when the mouse button is released + if (action->bookmark().isGroup()) return true; + } + } break; + + case QEvent::MouseMove: { + int distance = (mapFromGlobal(QCursor::pos()) - m_startDragPos).manhattanLength(); + if (!m_currentMenu && distance >= QApplication::startDragDistance()) { startDrag(); } + } break; + + case QEvent::MouseButtonRelease: { + QPoint destPos = mapFromGlobal(QCursor::pos()); + int distance = (destPos - m_startDragPos).manhattanLength(); + KBookmarkActionInterface *action = dynamic_cast<KBookmarkActionInterface *>(actionAt(destPos)); + + if (action) { + if (action->bookmark().isGroup()) { + if (mouseEvent->button() == Qt::MidButton) { + BookmarkManager::self()->owner()->loadBookmarkFolder(action->bookmark()); + } + else if (distance < QApplication::startDragDistance()) { + KBookmarkActionMenu *menu = dynamic_cast<KBookmarkActionMenu *>(actionAt(m_startDragPos)); + QPoint actionPos = mapToGlobal(widgetForAction(menu)->pos()); + menu->menu()->popup(QPoint(actionPos.x(), actionPos.y() + widgetForAction(menu)->height())); + } } - break; - - case QEvent::MouseButtonRelease: - { - QPoint destPos = mapFromGlobal(QCursor::pos()); - int distance = (destPos - m_startDragPos).manhattanLength(); - KBookmarkActionInterface *action = dynamic_cast<KBookmarkActionInterface *>(actionAt(destPos)); - - if (action) - { - if (action->bookmark().isGroup()) - { - if (mouseEvent->button() == Qt::MidButton) - { - BookmarkManager::self()->owner()->loadBookmarkFolder(action->bookmark()); - } - else if (distance < QApplication::startDragDistance()) - { - KBookmarkActionMenu *menu = dynamic_cast<KBookmarkActionMenu *>(actionAt(m_startDragPos)); - QPoint actionPos = mapToGlobal(widgetForAction(menu)->pos()); - menu->menu()->popup(QPoint(actionPos.x(), actionPos.y() + widgetForAction(menu)->height())); - } - } - else - { - if (!action->bookmark().isNull() && !action->bookmark().isSeparator()) - { - if (mouseEvent->button() == Qt::MidButton) - { - BookmarkManager::self()->owner()->loadBookmarkInNewTab(action->bookmark()); - } - } - } + else { + if (!action->bookmark().isNull() && !action->bookmark().isSeparator()) { + if (mouseEvent->button() == Qt::MidButton) { + BookmarkManager::self()->owner()->loadBookmarkInNewTab(action->bookmark()); } + } } - break; + } + } break; - default: - break; - } + default: + break; } + } - return QObject::eventFilter(watched, event); + return QObject::eventFilter(watched, event); } - void BookmarkToolBar::startDrag() { - KBookmarkActionInterface *action = dynamic_cast<KBookmarkActionInterface *>(m_dragAction); - if (action) - { - QMimeData *mimeData = new QMimeData; - KBookmark bookmark = action->bookmark(); + KBookmarkActionInterface *action = dynamic_cast<KBookmarkActionInterface *>(m_dragAction); + if (action) { + QMimeData *mimeData = new QMimeData; + KBookmark bookmark = action->bookmark(); - QByteArray address = bookmark.address().toLatin1(); - mimeData->setData(BookmarkManager::bookmark_mime_type(), address); - bookmark.populateMimeData(mimeData); + QByteArray address = bookmark.address().toLatin1(); + mimeData->setData(BookmarkManager::bookmark_mime_type(), address); + bookmark.populateMimeData(mimeData); - QDrag *drag = new QDrag(this); - drag->setMimeData(mimeData); - - if (bookmark.isGroup()) - { - drag->setPixmap(KIcon(bookmark.icon()).pixmap(24, 24)); - } - else - { - drag->setPixmap(IconManager::self()->iconForUrl(action->bookmark().url()).pixmap(24, 24)); - } + QDrag *drag = new QDrag(this); + drag->setMimeData(mimeData); - drag->start(Qt::MoveAction); - connect(drag, SIGNAL(destroyed()), this, SLOT(dragDestroyed())); + if (bookmark.isGroup()) { drag->setPixmap(KIcon(bookmark.icon()).pixmap(24, 24)); } + else { + drag->setPixmap(IconManager::self()->iconForUrl(action->bookmark().url()).pixmap(24, 24)); } -} + drag->start(Qt::MoveAction); + connect(drag, SIGNAL(destroyed()), this, SLOT(dragDestroyed())); + } +} void BookmarkToolBar::dragDestroyed() { - // A workaround to get rid of the checked state of the dragged action - if (m_dragAction) - { - m_dragAction->setVisible(false); - m_dragAction->setVisible(true); - m_dragAction = 0; - } - delete m_dropAction; - m_dropAction = 0; + // A workaround to get rid of the checked state of the dragged action + if (m_dragAction) { + m_dragAction->setVisible(false); + m_dragAction->setVisible(true); + m_dragAction = 0; + } + delete m_dropAction; + m_dropAction = 0; } diff --git a/src/bookmarks/bookmarkstreeformat_xbel.h b/src/bookmarks/bookmarkstreeformat_xbel.h index c156eb24..af7345c5 100644 --- a/src/bookmarks/bookmarkstreeformat_xbel.h +++ b/src/bookmarks/bookmarkstreeformat_xbel.h @@ -19,9 +19,9 @@ namespace xbel { * Parse an XBEL-formatted BookmarkItem tree * @param device QIODEvice pointer to read from * @param root BookmarkItem tree root pointer - * @return true if parsing was successful, false if errors occurred + * @return list of parse errors, if any */ -[[nodiscard]] bool read(QIODevice *device, BookmarksTreeItem *root); +[[nodiscard]] QList<QString> read(QIODevice *device, BookmarksTreeItem *root); /** * Write items into device diff --git a/src/bookmarks/bookmarkstreeformat_xbel_read.cpp b/src/bookmarks/bookmarkstreeformat_xbel_read.cpp index e16eda27..f224bd15 100644 --- a/src/bookmarks/bookmarkstreeformat_xbel_read.cpp +++ b/src/bookmarks/bookmarkstreeformat_xbel_read.cpp @@ -20,11 +20,7 @@ inline void readNodeAttributes(BookmarksTreeItem *item, const QXmlStreamAttribut // added if (attributes.hasAttribute(xbel::attr_added)) { -#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) - auto dateTime = QDateTime::fromString(attributes.value(xbel::attr_added), Qt::ISODate); -#else - auto dateTime = QDateTime::fromString(attributes.value(xbel::attr_added).toString(), Qt::ISODate); -#endif + const auto dateTime = QDateTime::fromString(attributes.value(xbel::attr_added), Qt::ISODate); if (!dateTime.isNull() && dateTime.isValid()) item->setData(BookmarksTreeItem::Added, dateTime); } } @@ -37,39 +33,29 @@ inline void readUrlAttributes(BookmarksTreeItem *item, const QXmlStreamAttribute // visited if (attributes.hasAttribute(xbel::attr_visited)) { -#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) - auto dateTime = QDateTime::fromString(attributes.value(xbel::attr_visited), Qt::ISODate); -#else - auto dateTime = QDateTime::fromString(attributes.value(xbel::attr_visited).toString(), Qt::ISODate); -#endif + const auto dateTime = QDateTime::fromString(attributes.value(xbel::attr_visited), Qt::ISODate); if (!dateTime.isNull() && dateTime.isValid()) item->setData(BookmarksTreeItem::Visited, dateTime); } // modified if (attributes.hasAttribute(xbel::attr_modified)) { -#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) - auto dateTime = QDateTime::fromString(attributes.value(xbel::attr_modified), Qt::ISODate); -#else - auto dateTime = QDateTime::fromString(attributes.value(xbel::attr_modified).toString(), Qt::ISODate); -#endif + const auto dateTime = QDateTime::fromString(attributes.value(xbel::attr_modified), Qt::ISODate); if (!dateTime.isNull() && dateTime.isValid()) item->setData(BookmarksTreeItem::Modified, dateTime); } } -[[nodiscard]] inline bool readItemAttributes(BookmarksTreeItem *item, const QXmlStreamAttributes &attr) +[[nodiscard]] inline QList<QString> readItemAttributes(BookmarksTreeItem *item, const QXmlStreamAttributes &attr) { - bool has_errors = false; + QList<QString> errors; switch (item->type()) { case BookmarksTreeItem::Root: if (!attr.hasAttribute(xbel::attr_version)) { - has_errors = true; - // spdlog::error("xbel::read: xbel has no version"); + errors.append({QObject::tr("%1 xbel has no version").arg(__PRETTY_FUNCTION__)}); } else if (attr.value(xbel::attr_version).compare(QLatin1String{xbel::attr_version_value}) != 0) { - has_errors = true; - // spdlog::error("xbel::read: unknown xbel version"); + errors.append({QObject::tr("%1 unknown xbel version").arg(__PRETTY_FUNCTION__)}); } readNodeAttributes(item, attr); break; @@ -90,18 +76,15 @@ inline void readUrlAttributes(BookmarksTreeItem *item, const QXmlStreamAttribute case BookmarksTreeItem::Alias: if (!attr.hasAttribute(xbel::attr_ref)) { - has_errors = true; - // spdlog::error("xbel::read: Alias has no ref"); + + errors.append({QObject::tr("%1 Alias has no ref").arg(__PRETTY_FUNCTION__)}); } else item->setData(BookmarksTreeItem::Id, attr.value(xbel::attr_ref).toString()); break; - - default: - return false; } - return has_errors; + return errors; } /** @@ -110,14 +93,13 @@ inline void readUrlAttributes(BookmarksTreeItem *item, const QXmlStreamAttribute * @param parent * @return true if there were errors during parsing */ -[[nodiscard]] inline bool readChildElements(QXmlStreamReader &reader, BookmarksTreeItem *parent) +[[nodiscard]] inline QList<QString> readChildElements(QXmlStreamReader &reader, BookmarksTreeItem *parent) { - bool has_errors = false; + QList<QString> errors; while (reader.readNextStartElement()) { if (reader.hasError()) { - has_errors = true; - // spdlog::warn("xbel::read: error during parsing: {}", qUtf8Printable(reader.errorString())); + errors.append({QObject::tr("%1 error during parsing: %2").arg(__PRETTY_FUNCTION__, reader.errorString())}); } const auto name = reader.name(); @@ -126,7 +108,7 @@ inline void readUrlAttributes(BookmarksTreeItem *item, const QXmlStreamAttribute parent->setData(BookmarksTreeItem::Description, reader.readElementText()); } else if (name == xbel::elem_info) { - has_errors += readChildElements(reader, parent); + errors += readChildElements(reader, parent); } else if (name == xbel::elem_metadata) { if (reader.attributes().value(xbel::elem_metadata_owner) == QLatin1String{xbel::elem_metadata_owner_value}) @@ -137,48 +119,50 @@ inline void readUrlAttributes(BookmarksTreeItem *item, const QXmlStreamAttribute else if (name == xbel::elem_bookmark) { auto *item = new BookmarksTreeItem(BookmarksTreeItem::Bookmark, {}, parent); - has_errors += readItemAttributes(item, reader.attributes()); - if (!parent->appendChild(item)) has_errors = true; - has_errors += readChildElements(reader, item); + errors += readItemAttributes(item, reader.attributes()); + parent->appendChild(item); + errors += readChildElements(reader, item); } else if (name == xbel::elem_folder) { auto *item = new BookmarksTreeItem(BookmarksTreeItem::Folder, {}, parent); - has_errors += readItemAttributes(item, reader.attributes()); - if (!parent->appendChild(item)) has_errors = true; - has_errors += readChildElements(reader, item); + errors += readItemAttributes(item, reader.attributes()); + parent->appendChild(item); + errors += readChildElements(reader, item); } else if (name == xbel::elem_separator) { auto *item = new BookmarksTreeItem(BookmarksTreeItem::Separator, {}, parent); - if (!parent->appendChild(item)) has_errors = true; + parent->appendChild(item); reader.skipCurrentElement(); } else if (name == xbel::elem_alias) { auto *item = new BookmarksTreeItem(BookmarksTreeItem::Alias, {}, parent); item->setData(BookmarksTreeItem::Id, reader.attributes().value(xbel::attr_ref).toString()); - if (!parent->appendChild(item)) has_errors = true; + parent->appendChild(item); reader.skipCurrentElement(); } else { - // spdlog::error("xbel::read: skipping unknown element {} on line {}", qUtf8Printable(name.toString()), - // reader.lineNumber()); - has_errors = true; + errors.append({QObject::tr("%1 skipping unknown element '%2' on line %3") + .arg(__PRETTY_FUNCTION__, name.toString(), QString::number(reader.lineNumber()))}); reader.skipCurrentElement(); } } - return has_errors; + return errors; } -bool xbel::read(QIODevice *device, BookmarksTreeItem *root) +QList<QString> xbel::read(QIODevice *device, BookmarksTreeItem *root) { - if (!device->isOpen()) return false; + if (!device->isOpen()) { return {QObject::tr("%1 buffer is not open").arg(__PRETTY_FUNCTION__)}; } QXmlStreamReader reader(device); - if (!reader.readNextStartElement()) return false; + if (!reader.readNextStartElement()) return {}; // parse the top-level item attributes - if (reader.name().compare(QLatin1String{elem_xbel}) != 0) return false; - if (readItemAttributes(root, reader.attributes())) return false; + if (reader.name().compare(QLatin1String{elem_xbel}) != 0) + return {QObject::tr("%1 Top-level item is not xbel").arg(__PRETTY_FUNCTION__)}; - return !readChildElements(reader, root); + QList<QString> errors; + errors += readItemAttributes(root, reader.attributes()); + errors += readChildElements(reader, root); + return errors; } diff --git a/src/bookmarks/bookmarkstreeformats.hpp b/src/bookmarks/bookmarkstreeformats.hpp index 380a7e64..2f001ed9 100644 --- a/src/bookmarks/bookmarkstreeformats.hpp +++ b/src/bookmarks/bookmarkstreeformats.hpp @@ -12,7 +12,7 @@ #include "bookmarkstreeformat_xbel.h" #include <array> -typedef bool (*t_readFn)(QIODevice *, BookmarksTreeItem *); +typedef QList<QString> (*t_readFn)(QIODevice *, BookmarksTreeItem *); typedef bool (*t_writeFn)(QIODevice *, const QVector<const BookmarksTreeItem *> &); enum Formats { FormatXbel = 0 }; diff --git a/src/bookmarks/bookmarkstreeitem.hpp b/src/bookmarks/bookmarkstreeitem.hpp index e8cd5a90..a0af91aa 100644 --- a/src/bookmarks/bookmarkstreeitem.hpp +++ b/src/bookmarks/bookmarkstreeitem.hpp @@ -9,7 +9,6 @@ #pragma once -#include "rekonq.hpp" #include <QDateTime> #include <QIcon> #include <QUrl> diff --git a/src/bookmarks/bookmarkstreemodel.cpp b/src/bookmarks/bookmarkstreemodel.cpp index 444d2e7b..8086ff64 100644 --- a/src/bookmarks/bookmarkstreemodel.cpp +++ b/src/bookmarks/bookmarkstreemodel.cpp @@ -11,33 +11,14 @@ #include "bookmarkstreeformats.hpp" #include <QBuffer> #include <QDateTime> -#include <QFile> #include <QMimeData> -BookmarkModel::BookmarkModel(const QString &path, QObject *parent) - : QAbstractItemModel(parent), bookmarksFile(new QFile(path)) +BookmarkModel::BookmarkModel(QObject *parent) : QAbstractItemModel(parent) { rootItem = new BookmarksTreeItem(BookmarksTreeItem::Root, {.title = tr("Title"), .href = tr("Address")}, nullptr); - - if (bookmarksFile->open(QIODevice::ReadOnly | QIODevice::Text)) { - if (path.endsWith(".xbel")) { readFns[Formats::FormatXbel](bookmarksFile, rootItem); } - bookmarksFile->close(); - } -} - -BookmarkModel::~BookmarkModel() -{ - save(); // save on exit - bookmarksFile->flush(); - - delete rootItem; - delete bookmarksFile; } -void BookmarkModel::load(const QIODevice *buffer) -{ - if (buffer->isOpen() && buffer->isReadable()) { readFns[Formats::FormatXbel](buffer, rootItem); } -} +BookmarkModel::~BookmarkModel() { delete rootItem; } QVariant BookmarkModel::headerData(int section, Qt::Orientation, int role) const { @@ -246,11 +227,10 @@ bool BookmarkModel::dropMimeData(const QMimeData *mimeData, Qt::DropAction actio return true; } -void BookmarkModel::save() +QList<QString> BookmarkModel::load(QIODevice *buffer) { return readFns[Formats::FormatXbel](buffer, rootItem); } + +void BookmarkModel::save(QIODevice *buffer) { - if (m_isModified) { - bookmarksFile->open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate); - m_isModified = !writeFns[Formats::FormatXbel](bookmarksFile, {rootItem}); - bookmarksFile->close(); - } + if (!buffer->isOpen() || !buffer->isWritable()) return; + if (m_isModified) m_isModified = !writeFns[Formats::FormatXbel](buffer, {rootItem}); } diff --git a/src/bookmarks/bookmarkstreemodel.hpp b/src/bookmarks/bookmarkstreemodel.hpp index ace596b8..f92166d3 100644 --- a/src/bookmarks/bookmarkstreemodel.hpp +++ b/src/bookmarks/bookmarkstreemodel.hpp @@ -13,16 +13,14 @@ #include <QAbstractItemModel> class QFile; -class BookmarkModel : public QAbstractItemModel { +class BookmarkModel final : public QAbstractItemModel { Q_OBJECT public: enum Roles { CompletionMatchingRole = Qt::UserRole + 1 }; - explicit BookmarkModel(const QString &path, QObject *parent = nullptr); - ~BookmarkModel() override; - - void load(const QIODevice *buffer); + explicit BookmarkModel(QObject *parent = nullptr); + ~BookmarkModel() final; [[nodiscard]] QVariant headerData(int section, Qt::Orientation orientation, int role) const override; [[nodiscard]] QVariant data(const QModelIndex &index, int role) const override; @@ -43,17 +41,17 @@ public: [[nodiscard]] QModelIndex parent(const QModelIndex &index) const override; [[nodiscard]] QModelIndex parentFolder(const QModelIndex &index) const; - [[nodiscard]] BookmarksTreeItem *item(const QModelIndex &index) const; + [[nodiscard]] BookmarksTreeItem *item(const QModelIndex &index = QModelIndex()) const; QModelIndex appendItem(BookmarksTreeItem::Types type, BookmarksTreeItem::Attributes_t data, const QModelIndex &parent); public slots: - void save(); + QList<QString> load(QIODevice *buffer); + void save(QIODevice *buffer); private: const QLatin1String mimeType = QLatin1String("application/xbel"); BookmarksTreeItem *rootItem; bool m_isModified = false; - QFile *bookmarksFile; }; diff --git a/src/bookmarks/test/bookmarkmanager.cpp b/src/bookmarks/test/bookmarkmanager.cpp new file mode 100644 index 00000000..5bfaf649 --- /dev/null +++ b/src/bookmarks/test/bookmarkmanager.cpp @@ -0,0 +1,21 @@ +#include "../bookmarkmanager.hpp" +#include <QStandardPaths> +#include <gtest/gtest.h> + +// clazy:excludeall=non-pod-global-static + +namespace { +TEST(BookmarkManager, QStandardPaths) +{ + const auto file = QStandardPaths::locate(QStandardPaths::AppLocalDataLocation, QString::fromLatin1("bookmarks.xbel")); + EXPECT_TRUE(file.isEmpty()) << qUtf8Printable(file); +} + +TEST(BookmarkManager, Manager) +{ + BookmarkManager manager("bookmarks.xbel"); + + const auto load_errors = manager.errors(); + EXPECT_EQ(load_errors.length(), 0) << qUtf8Printable(load_errors.join('\n')); +} +} // namespace
\ No newline at end of file diff --git a/src/bookmarks/test/bookmarks.xbel b/src/bookmarks/test/bookmarks.xbel new file mode 100644 index 00000000..2501c118 --- /dev/null +++ b/src/bookmarks/test/bookmarks.xbel @@ -0,0 +1,69 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE xbel> +<xbel version="1.0"> + <folder folded="no"> + <title>Qt Resources</title> + <bookmark href="http://qt.io/"> + <title>Qt home page</title> + </bookmark> + <bookmark href="https://www.qt.io/partners/"> + <title>Qt Partners</title> + </bookmark> + <bookmark href="https://www.qt.io/qt-training/"> + <title>Training</title> + </bookmark> + <bookmark href="http://doc.qt.io/"> + <title>Qt 5 documentation</title> + </bookmark> + <bookmark href="http://qt-project.org/faq/"> + <title>Frequently Asked Questions</title> + </bookmark> + <folder folded="yes"> + <title>Community Resources</title> + <bookmark href="http://www.qtcentre.org/content/"> + <title>Qt Centre</title> + </bookmark> + <bookmark href="http://www.qtforum.org/"> + <title>QtForum.org</title> + </bookmark> + <bookmark href="http://digitalfanatics.org/projects/qt_tutorial/"> + <title>The Independent Qt Tutorial</title> + </bookmark> + <bookmark href="http://www.qtforum.de/"> + <title>German Qt Forum</title> + </bookmark> + <bookmark href="http://www.korone.net/"> + <title>Korean Qt Community Site</title> + </bookmark> + <bookmark href="http://prog.org.ru/"> + <title>Russian Qt Forum</title> + </bookmark> + </folder> + </folder> + <folder folded="no"> + <title>Online Dictionaries</title> + <bookmark href="http://www.dictionary.com/"> + <title>Dictionary.com</title> + </bookmark> + <bookmark href="http://www.m-w.com/"> + <title>Merriam-Webster Online</title> + </bookmark> + <bookmark href="http://dictionary.cambridge.org/"> + <title>Cambridge Dictionaries Online</title> + </bookmark> + <bookmark href="http://www.onelook.com/"> + <title>OneLook Dictionary Search</title> + </bookmark> + <separator/> + <bookmark href="http://dict.tu-chemnitz.de/"> + <title>TU Chemnitz German-English Dictionary</title> + </bookmark> + <separator/> + <bookmark href="http://atilf.atilf.fr/tlf.htm"> + <title>Trésor de la Langue Française informatisé</title> + </bookmark> + <bookmark href="http://dictionnaires.atilf.fr/dictionnaires/ACADEMIE/"> + <title>Dictionnaire de l'Académie Française</title> + </bookmark> + </folder> +</xbel> diff --git a/src/bookmarks/test/xbel.cpp b/src/bookmarks/test/xbel.cpp index a836dd9a..03bd061a 100644 --- a/src/bookmarks/test/xbel.cpp +++ b/src/bookmarks/test/xbel.cpp @@ -55,7 +55,7 @@ bool operator==(const BookmarksTreeItem &lhs, const BookmarksTreeItem &rhs) { QBuffer buffer(&data); if (!buffer.open(QIODevice::ReadOnly | QIODevice::Text)) return false; - return xbel::read(&buffer, root); + return xbel::read(&buffer, root).isEmpty(); } namespace { |