diff options
author | aqua <aqua@iserlohn-fortress.net> | 2022-09-13 10:08:28 +0300 |
---|---|---|
committer | aqua <aqua@iserlohn-fortress.net> | 2022-09-14 08:51:10 +0300 |
commit | 0576870ebdbe813b5a084e1a279cb77077e6373e (patch) | |
tree | 01cad50191f9418aeebd55dfdde5d12c1eb289a9 /src/bookmarks | |
parent | bugfix: don't wipe bookmarks if they're not modified (diff) | |
download | rekonq-0576870ebdbe813b5a084e1a279cb77077e6373e.tar.xz |
Rename BookmarkModel to BookmarksTreeModel
- remove BookmarkManager
- remove BookmarkOwner
Diffstat (limited to 'src/bookmarks')
-rw-r--r-- | src/bookmarks/CMakeLists.txt | 9 | ||||
-rw-r--r-- | src/bookmarks/bookmarkmanager.cpp | 177 | ||||
-rw-r--r-- | src/bookmarks/bookmarkmanager.hpp | 96 | ||||
-rw-r--r-- | src/bookmarks/bookmarkowner.cpp | 349 | ||||
-rw-r--r-- | src/bookmarks/bookmarkowner.h | 148 | ||||
-rw-r--r-- | src/bookmarks/bookmarksmenu.hpp | 6 | ||||
-rw-r--r-- | src/bookmarks/bookmarkstreeitem.hpp | 19 | ||||
-rw-r--r-- | src/bookmarks/bookmarkstreemodel.cpp | 44 | ||||
-rw-r--r-- | src/bookmarks/bookmarkstreemodel.hpp | 6 | ||||
-rw-r--r-- | src/bookmarks/test/bookmarkmanager.cpp | 21 | ||||
-rw-r--r-- | src/bookmarks/test/test_bookmarkstreemodel.cpp | 25 |
11 files changed, 75 insertions, 825 deletions
diff --git a/src/bookmarks/CMakeLists.txt b/src/bookmarks/CMakeLists.txt index 2ff32c80..1d0f846f 100644 --- a/src/bookmarks/CMakeLists.txt +++ b/src/bookmarks/CMakeLists.txt @@ -1,5 +1,4 @@ add_library(bookmarks STATIC -# bookmarkmanager.cpp bookmarkmanager.h # bookmarkowner.cpp bookmarkowner.h # bookmarkstoolbar.cpp bookmarkstoolbar.h # Bookmarks Model @@ -8,8 +7,6 @@ add_library(bookmarks STATIC bookmarkstreemodel.cpp bookmarkstreemodel.hpp bookmarkstreeformats.hpp bookmarkstreeformat_xbel_read.cpp bookmarkstreeformat_xbel_write.cpp - # Bookmark Manager - bookmarkmanager.cpp bookmarkmanager.hpp # UI bookmarksmenu.cpp bookmarksmenu.hpp ) @@ -17,9 +14,9 @@ target_include_directories(bookmarks PUBLIC ${CMAKE_SOURCE_DIR}/src) target_link_libraries(bookmarks PUBLIC Qt6::Core Qt6::Widgets) 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(test_bookmarkstreemodel test/test_bookmarkstreemodel.cpp) + target_link_libraries(test_bookmarkstreemodel GTest::gtest GTest::gtest_main bookmarks) + gtest_discover_tests(test_bookmarkstreemodel WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test) add_executable(xbel test/xbel.cpp) target_link_libraries(xbel GTest::gtest GTest::gtest_main bookmarks) diff --git a/src/bookmarks/bookmarkmanager.cpp b/src/bookmarks/bookmarkmanager.cpp deleted file mode 100644 index b5077111..00000000 --- a/src/bookmarks/bookmarkmanager.cpp +++ /dev/null @@ -1,177 +0,0 @@ -/* ============================================================ - * The rekonq project - * ============================================================ - * SPDX-License-Identifier: GPL-2.0-or-later - * 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> - * SPDX-License-Identifier: GPL-3.0-only - * Copyright (C) 2022 aqua <aqua@iserlohn-fortress.net> - * ============================================================ - * Description: rekonq bookmarks system interface - * ============================================================ */ - -#include "bookmarkmanager.hpp" -#include <QCoreApplication> -#include <QFile> -#include <QMenu> - -// ---------------------------------------------------------------------------------------------- -// Constructor and Destructor - -BookmarkManager::BookmarkManager(const QString &bookmarksFile, QObject *parent) - : QObject(parent), m_model(new BookmarkModel(this)) -{ - 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())); - - // bookmarks loading - // connect(this, SIGNAL(openUrl(KUrl, Rekonq::OpenType)), rApp, SLOT(loadUrl(KUrl, Rekonq::OpenType))); -} - -BookmarkManager::~BookmarkManager() { delete m_model; } - -// ---------------------------------------------------------------------------------------------- -/* -QList<Bookmark> BookmarkManager::find(const QString &text) -{ - BookmarkGroup list; - auto *root = m_model->item(); - Q_CHECK_PTR(root); - - - KBookmarkGroup root = rootGroup(); - if (!root.isNull()) - for (KBookmark bookmark = root.first(); !bookmark.isNull(); bookmark = root.next(bookmark)) - find(&list, bookmark, text); - - return list; -} - -KBookmark BookmarkManager::bookmarkForUrl(const KUrl &url) -{ - KBookmarkGroup root = rootGroup(); - if (root.isNull()) return KBookmark(); - - return bookmarkForUrl(root, url); -} - -void BookmarkManager::slotBookmarksChanged() -{ - 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(); -} - -KBookmark BookmarkManager::bookmarkCurrentPage(const KBookmark &bookmark) -{ - return m_owner->bookmarkCurrentPage(bookmark); -} - -void BookmarkManager::fillBookmarkBar(BookmarkToolBar *toolBar) -{ - 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()); - - 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); - } - } -} - -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; - } -} - -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; -} - -void BookmarkManager::slotEditBookmarks() { m_manager->slotEditBookmarks(); } - -void BookmarkManager::openFolderInTabs(const BookmarkGroup &group) { - for(const auto &bookmark : group) { - // TODO emit openUrl - } -} - -KActionMenu *BookmarkManager::bookmarkActionMenu(QWidget *parent) -{ - 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; -} -*/ diff --git a/src/bookmarks/bookmarkmanager.hpp b/src/bookmarks/bookmarkmanager.hpp deleted file mode 100644 index f76b8b3f..00000000 --- a/src/bookmarks/bookmarkmanager.hpp +++ /dev/null @@ -1,96 +0,0 @@ -/* ============================================================ - * The rekonq project - * ============================================================ - * SPDX-License-Identifier: GPL-2.0-or-later - * 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> - * SPDX-License-Identifier: GPL-3.0-only - * Copyright (C) 2022 aqua <aqua@iserlohn-fortress.net> - * ============================================================ - * Description: rekonq bookmarks system interface - * ============================================================ */ - -#pragma once - -#include "bookmark.hpp" -#include "bookmarkstreemodel.hpp" -#include "rekonq.hpp" -#include <QObject> - -class QToolBar; - -/** - * This class represent the interface to the rekonq bookmarks system. It provides bookmarks access to the - * Bookmarks Menu and Bookmarks Toolbar. - */ -class BookmarkManager final : public QObject { - Q_OBJECT - -public: - /** - * @short Class constructor. - * Connect BookmarksProvider with bookmarks source - * (actually konqueror's bookmarks). - * @param parent The WebWindow to provide bookmarks objects. - */ - explicit BookmarkManager(const QString &bookmarksFile, QObject *parent = nullptr); - ~BookmarkManager() override; - - /** - * Return a list of errors - * @return A copy of the error list - */ - auto errors() - { - auto e = m_errors; - m_errors.clear(); - return e; - } - - /** - * @short set the Bookmarks Toolbar Action - */ - void registerBookmarkBar(QToolBar *toolbar) - { - if (!m_bookmarkToolBars.contains(toolbar)) m_bookmarkToolBars.append(toolbar); - } - void removeBookmarkBar(QToolBar *toolbar) { m_bookmarkToolBars.removeOne(toolbar); } - - // QList<Bookmark> find(const QString &text); - // QList<Bookmark> findByUrl(const QUrl &url); - - // Bookmark bookmarkForUrl(const QUrl &url); - - // void openFolderInTabs(const BookmarkGroup &group); - - // QMenu *bookmarkActionMenu(QWidget *parent); - -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 - * @p groupAddress (e.g. "/4/5") has been modified by the caller @p caller. - * @param groupAddress bookmark group address - * @param caller caller that modified the bookmarks - * @see KBookmarkManager::changed - */ - // void slotBookmarksChanged(); - // void fillBookmarkBar(QToolBar *toolBar); - - // void slotEditBookmarks(); - - // Bookmark bookmarkCurrentPage(const Bookmark &bookmark = Bookmark()); - -signals: - /** - * @short This signal is emitted when a URL has to be loaded - */ - void openUrl(const QUrl &, rekonq::OpenType); - -private: - QList<QString> m_errors; - QList<QToolBar *> m_bookmarkToolBars; - BookmarkModel *m_model; -}; diff --git a/src/bookmarks/bookmarkowner.cpp b/src/bookmarks/bookmarkowner.cpp deleted file mode 100644 index 49e3dd87..00000000 --- a/src/bookmarks/bookmarkowner.cpp +++ /dev/null @@ -1,349 +0,0 @@ -/* ============================================================ - * - * 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" -#include "bookmarkowner.moc" - -// Local Includes -#include "bookmarkmanager.h" - -#include "application.h" -#include "rekonqwindow.h" -#include "tabwidget.h" -#include "webwindow.h" - -// KDE Includes -#include <KBookmarkDialog> -#include <KLocalizedString> -#include <KMessageBox> - -// Qt Includes -#include <QClipboard> - -// Nepomuk Includes -#ifdef HAVE_NEPOMUK -// Local Nepomuk Includes -#include "resourcelinkdialog.h" - -// Nepomuk Includes -#include <Nepomuk2/Resource> -#include <Nepomuk2/Vocabulary/NFO> -#endif - -BookmarkOwner::BookmarkOwner(KBookmarkManager *manager, QObject *parent) - : QObject(parent), KBookmarkOwner(), m_manager(manager) -{ -} - -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); -#ifdef HAVE_NEPOMUK - 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; - } -} - -QString BookmarkOwner::currentTitle() const { return rApp->rekonqWindow()->currentWebWindow()->title(); } - -QString BookmarkOwner::currentUrl() const { return rApp->rekonqWindow()->currentWebWindow()->url().url(); } - -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, - Qt::KeyboardModifiers keyboardModifiers) -{ - 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; - } - - 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::loadBookmarkInNewWindow(const KBookmark &bookmark) -{ - emit openUrl(bookmark.url(), Rekonq::NewWindow); -} - -void BookmarkOwner::loadBookmarkFolder(const KBookmark &bookmark) -{ - 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(); - } - else { - parent = BookmarkManager::self()->rootGroup(); -#ifdef HAVE_NEPOMUK - 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); - - 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); - } - - 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(); - } - - newBk.setIcon("edit-clear"); - - m_manager->emitChanged(newBk.parentGroup()); - return newBk; -} - -void BookmarkOwner::copyLink(const KBookmark &bookmark) -{ - if (bookmark.isNull()) return; - - QApplication::clipboard()->setText(bookmark.url().url()); -} - -void BookmarkOwner::editBookmark(KBookmark bookmark) -{ - if (bookmark.isNull()) return; - - KBookmarkDialog *dialog = bookmarkDialog(m_manager, 0); - dialog->editBookmark(bookmark); - - delete dialog; -} - -#ifdef HAVE_NEPOMUK -void BookmarkOwner::fancyBookmark(KBookmark bookmark) -{ - Nepomuk2::Resource nfoResource = (KUrl)bookmark.url(); - - QPointer<Nepomuk2::ResourceLinkDialog> r = new Nepomuk2::ResourceLinkDialog(nfoResource); - r->exec(); - - 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); -#ifdef HAVE_NEPOMUK - Nepomuk2::Resource nfoResource(bookmark.url()); - nfoResource.remove(); -#endif - m_manager->emitChanged(bmg); - return true; -} - -void BookmarkOwner::setToolBarFolder(KBookmark bookmark) -{ - if (!bookmark.isGroup()) return; - - unsetToolBarFolder(); - bookmark.internalElement().setAttribute("toolbar", "yes"); - bookmark.setIcon("bookmark-toolbar"); - - m_manager->emitChanged(); -} - -void BookmarkOwner::unsetToolBarFolder() -{ - 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, - 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::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())); -} - -void CustomBookmarkAction::onActionTriggered() { emit triggered(m_bookmark); } diff --git a/src/bookmarks/bookmarkowner.h b/src/bookmarks/bookmarkowner.h deleted file mode 100644 index 284c9459..00000000 --- a/src/bookmarks/bookmarkowner.h +++ /dev/null @@ -1,148 +0,0 @@ -/* ============================================================ -* -* 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/>. -* -* ============================================================ */ - - -#ifndef BOOKMARKOWNER_H -#define BOOKMARKOWNER_H - -#include "rekonq.hpp" -#include <QObject> - -/** - * This class allows to manage bookmarks as actions. - */ -class REKONQ_TESTS_EXPORT BookmarkOwner : public QObject { - Q_OBJECT - -public: - explicit BookmarkOwner(KBookmarkManager *manager, QObject *parent = 0); - - enum BookmarkAction - { - OPEN = 0, - OPEN_IN_TAB, - OPEN_IN_WINDOW, - OPEN_FOLDER, - BOOKMARK_PAGE, - NEW_FOLDER, - NEW_SEPARATOR, - COPY, - EDIT, -#ifdef HAVE_NEPOMUK - FANCYBOOKMARK, -#endif - DELETE, - NUM_ACTIONS, - SET_TOOLBAR_FOLDER, - UNSET_TOOLBAR_FOLDER - }; - - /** - * @return A new action for the given bookmark. - */ - KAction* createAction(const KBookmark &bookmark, const BookmarkAction &bmAction); - - // @{ - /** - * Functions to get current information. - */ - virtual QString currentTitle() const; - virtual QString currentUrl() const; - virtual QList< QPair<QString, QString> > currentBookmarkList() const; - // @} - - virtual bool supportsTabs() const - { - return true; - } - - // @{ - /** - * This functions emit signals that open the selected URLs - */ - virtual void openBookmark(const KBookmark &bookmark, - Qt::MouseButtons mouseButtons, - Qt::KeyboardModifiers keyboardModifiers); - virtual void openFolderinTabs(const KBookmarkGroup &bkGoup); - // @} - -public Q_SLOTS: - void loadBookmark(const KBookmark &bookmark); - void loadBookmarkInNewTab(const KBookmark &bookmark); - void loadBookmarkInNewWindow(const KBookmark &bookmark); - void loadBookmarkFolder(const KBookmark &bookmark); - - KBookmark bookmarkCurrentPage(const KBookmark &bookmark = KBookmark()); - KBookmarkGroup newBookmarkFolder(const KBookmark &bookmark = KBookmark(), const QString &name = QString()); - KBookmark newSeparator(const KBookmark &bookmark = KBookmark()); - - void copyLink(const KBookmark &bookmark); - void editBookmark(KBookmark bookmark); -#ifdef HAVE_NEPOMUK - void fancyBookmark(KBookmark bookmark); -#endif - bool deleteBookmark(const KBookmark &bookmark); - void setToolBarFolder(KBookmark bookmark = KBookmark()); - void unsetToolBarFolder(); - -Q_SIGNALS: - /** - * This signal is emitted when an url has to be loaded - * @param url the URL to load - */ - void openUrl(const KUrl &, const Rekonq::OpenType &); - -private: - KAction* createAction(const QString &text, const QString &icon, - const QString &help, const char *slot, - const KBookmark &bookmark); - - KBookmarkManager *m_manager; -}; - -// ----------------------------------------------------------------------------------------------- - - -class CustomBookmarkAction : public KAction -{ - Q_OBJECT - -public: - CustomBookmarkAction(const KBookmark &bookmark, const KIcon &icon, const QString &text, QObject *parent); - -Q_SIGNALS: - void triggered(const KBookmark &); - -private Q_SLOTS: - void onActionTriggered(); - -private: - KBookmark m_bookmark; -}; - -#endif // BOOKMARKOWNER_H diff --git a/src/bookmarks/bookmarksmenu.hpp b/src/bookmarks/bookmarksmenu.hpp index 6199708f..ad175991 100644 --- a/src/bookmarks/bookmarksmenu.hpp +++ b/src/bookmarks/bookmarksmenu.hpp @@ -15,7 +15,7 @@ #include "rekonq.hpp" #include <QMenu> -class BookmarkModel; +class BookmarksTreeModel; class BookmarksMenu : public QMenu { Q_OBJECT @@ -23,7 +23,7 @@ public: explicit BookmarksMenu(QWidget *parent = nullptr); ~BookmarksMenu() override = default; - void setModel(const BookmarkModel *ptr) + void setModel(const BookmarksTreeModel *ptr) { Q_CHECK_PTR(ptr); model = ptr; @@ -36,6 +36,6 @@ private slots: void refill(); private: - const BookmarkModel *model = nullptr; + const BookmarksTreeModel *model = nullptr; QList<QAction *> actions; }; diff --git a/src/bookmarks/bookmarkstreeitem.hpp b/src/bookmarks/bookmarkstreeitem.hpp index a0af91aa..1f87ecad 100644 --- a/src/bookmarks/bookmarkstreeitem.hpp +++ b/src/bookmarks/bookmarkstreeitem.hpp @@ -18,6 +18,25 @@ class BookmarksTreeItem { public: + enum BookmarkAction { + OPEN = 0, + OPEN_IN_TAB, + OPEN_IN_WINDOW, + OPEN_FOLDER, + BOOKMARK_PAGE, + NEW_FOLDER, + NEW_SEPARATOR, + COPY, + EDIT, +#ifdef HAVE_NEPOMUK + FANCYBOOKMARK, +#endif + DELETE, + NUM_ACTIONS, + SET_TOOLBAR_FOLDER, + UNSET_TOOLBAR_FOLDER + }; + enum Types { Root, Folder, Bookmark, Separator, Alias }; constexpr static int TypesCount = 5; diff --git a/src/bookmarks/bookmarkstreemodel.cpp b/src/bookmarks/bookmarkstreemodel.cpp index a4f322b8..d64f1588 100644 --- a/src/bookmarks/bookmarkstreemodel.cpp +++ b/src/bookmarks/bookmarkstreemodel.cpp @@ -13,20 +13,20 @@ #include <QDateTime> #include <QMimeData> -BookmarkModel::BookmarkModel(QObject *parent) : QAbstractItemModel(parent) +BookmarksTreeModel::BookmarksTreeModel(QObject *parent) : QAbstractItemModel(parent) { rootItem = new BookmarksTreeItem(BookmarksTreeItem::Root, {.title = tr("Title"), .href = tr("Address")}, nullptr); } -BookmarkModel::~BookmarkModel() { delete rootItem; } +BookmarksTreeModel::~BookmarksTreeModel() { delete rootItem; } -QVariant BookmarkModel::headerData(int section, Qt::Orientation, int role) const +QVariant BookmarksTreeModel::headerData(int section, Qt::Orientation, int role) const { if (role != Qt::DisplayRole) return {}; return rootItem->data(static_cast<BookmarksTreeItem::Attributes>(section)); } -QVariant BookmarkModel::data(const QModelIndex &index, int role) const +QVariant BookmarksTreeModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) return {}; @@ -61,7 +61,7 @@ QVariant BookmarkModel::data(const QModelIndex &index, int role) const return {}; } -bool BookmarkModel::setData(const QModelIndex &index, const QVariant &value, int role) +bool BookmarksTreeModel::setData(const QModelIndex &index, const QVariant &value, int role) { if (!index.isValid()) return false; @@ -80,7 +80,7 @@ bool BookmarkModel::setData(const QModelIndex &index, const QVariant &value, int return success; } -Qt::ItemFlags BookmarkModel::flags(const QModelIndex &index) const +Qt::ItemFlags BookmarksTreeModel::flags(const QModelIndex &index) const { switch (item(index)->type()) { case BookmarksTreeItem::Root: @@ -100,14 +100,14 @@ Qt::ItemFlags BookmarkModel::flags(const QModelIndex &index) const __builtin_unreachable(); } -int BookmarkModel::rowCount(const QModelIndex &index) const +int BookmarksTreeModel::rowCount(const QModelIndex &index) const { if (index.column() > 0) return 0; return static_cast<int>(item(index)->childCount()); } -QModelIndex BookmarkModel::appendItem(BookmarksTreeItem::Types type, BookmarksTreeItem::Attributes_t data, - const QModelIndex &parent) +QModelIndex BookmarksTreeModel::appendItem(BookmarksTreeItem::Types type, BookmarksTreeItem::Attributes_t data, + const QModelIndex &parent) { auto *parentItem = item(parent); const auto row = rowCount(parent); @@ -126,7 +126,7 @@ QModelIndex BookmarkModel::appendItem(BookmarksTreeItem::Types type, BookmarksTr return {}; } -bool BookmarkModel::removeRows(int position, int rows, const QModelIndex &parent) +bool BookmarksTreeModel::removeRows(int position, int rows, const QModelIndex &parent) { auto *parentItem = item(parent); @@ -138,7 +138,7 @@ bool BookmarkModel::removeRows(int position, int rows, const QModelIndex &parent return success; } -QModelIndex BookmarkModel::index(int row, int column, const QModelIndex &parent) const +QModelIndex BookmarksTreeModel::index(int row, int column, const QModelIndex &parent) const { if (!this->hasIndex(row, column, parent)) return {}; @@ -148,7 +148,7 @@ QModelIndex BookmarkModel::index(int row, int column, const QModelIndex &parent) return {}; } -QModelIndex BookmarkModel::parent(const QModelIndex &index) const +QModelIndex BookmarksTreeModel::parent(const QModelIndex &index) const { if (!index.isValid()) return {}; @@ -160,7 +160,7 @@ QModelIndex BookmarkModel::parent(const QModelIndex &index) const return createIndex(parentItem->row(), 0, parentItem); } -QModelIndex BookmarkModel::parentFolder(const QModelIndex &index) const +QModelIndex BookmarksTreeModel::parentFolder(const QModelIndex &index) const { // invalid index is the root index -> return it back if (!index.isValid()) return {}; @@ -170,7 +170,7 @@ QModelIndex BookmarkModel::parentFolder(const QModelIndex &index) const return index; } -BookmarksTreeItem *BookmarkModel::item(const QModelIndex &index) const +BookmarksTreeItem *BookmarksTreeModel::item(const QModelIndex &index) const { if (!index.isValid()) return rootItem; return static_cast<BookmarksTreeItem *>(index.internalPointer()); @@ -178,12 +178,12 @@ BookmarksTreeItem *BookmarkModel::item(const QModelIndex &index) const /* * Drag'n'Drop implementation - * How drag and drop actually works: the view encodes the data of the original item (using BookmarkModel::mimeData), and - * then uses BookmarkModel::dropMimeData to create the new item. If successful, the old item is removed (through - * BookmarkModel::removeRows). + * How drag and drop actually works: the view encodes the data of the original item (using + * BookmarksTreeModel::mimeData), and then uses BookmarksTreeModel::dropMimeData to create the new item. If successful, + * the old item is removed (through BookmarksTreeModel::removeRows). */ -QMimeData *BookmarkModel::mimeData(const QModelIndexList &indexes) const +QMimeData *BookmarksTreeModel::mimeData(const QModelIndexList &indexes) const { QByteArray data; QBuffer buffer(&data); @@ -201,8 +201,8 @@ QMimeData *BookmarkModel::mimeData(const QModelIndexList &indexes) const return mimeData; } -bool BookmarkModel::dropMimeData(const QMimeData *mimeData, Qt::DropAction action, int row, int column, - const QModelIndex &parent) +bool BookmarksTreeModel::dropMimeData(const QMimeData *mimeData, Qt::DropAction action, int row, int column, + const QModelIndex &parent) { if (action == Qt::IgnoreAction) return true; if (action != Qt::MoveAction) return false; // we only implement MoveAction's @@ -229,9 +229,9 @@ bool BookmarkModel::dropMimeData(const QMimeData *mimeData, Qt::DropAction actio return true; } -QList<QString> BookmarkModel::load(QIODevice *buffer) { return readFns[Formats::FormatXbel](buffer, rootItem); } +QList<QString> BookmarksTreeModel::load(QIODevice *buffer) { return readFns[Formats::FormatXbel](buffer, rootItem); } -void BookmarkModel::save(QIODevice *buffer) +void BookmarksTreeModel::save(QIODevice *buffer) { if (!buffer->isOpen() || !buffer->isWritable()) return; m_isModified = !writeFns[Formats::FormatXbel](buffer, {rootItem}); diff --git a/src/bookmarks/bookmarkstreemodel.hpp b/src/bookmarks/bookmarkstreemodel.hpp index f92166d3..23257040 100644 --- a/src/bookmarks/bookmarkstreemodel.hpp +++ b/src/bookmarks/bookmarkstreemodel.hpp @@ -13,14 +13,14 @@ #include <QAbstractItemModel> class QFile; -class BookmarkModel final : public QAbstractItemModel { +class BookmarksTreeModel final : public QAbstractItemModel { Q_OBJECT public: enum Roles { CompletionMatchingRole = Qt::UserRole + 1 }; - explicit BookmarkModel(QObject *parent = nullptr); - ~BookmarkModel() final; + explicit BookmarksTreeModel(QObject *parent = nullptr); + ~BookmarksTreeModel() final; [[nodiscard]] QVariant headerData(int section, Qt::Orientation orientation, int role) const override; [[nodiscard]] QVariant data(const QModelIndex &index, int role) const override; diff --git a/src/bookmarks/test/bookmarkmanager.cpp b/src/bookmarks/test/bookmarkmanager.cpp deleted file mode 100644 index 5bfaf649..00000000 --- a/src/bookmarks/test/bookmarkmanager.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#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/test_bookmarkstreemodel.cpp b/src/bookmarks/test/test_bookmarkstreemodel.cpp new file mode 100644 index 00000000..febd4cd4 --- /dev/null +++ b/src/bookmarks/test/test_bookmarkstreemodel.cpp @@ -0,0 +1,25 @@ +#include <QFile> +#include <QStandardPaths> +#include <gtest/gtest.h> + +#include "../bookmarkstreemodel.hpp" + +// clazy:excludeall=non-pod-global-static + +namespace { +TEST(bookmarks, QStandardPaths) +{ + const auto file = QStandardPaths::locate(QStandardPaths::AppLocalDataLocation, QString::fromLatin1("bookmarks.xbel")); + EXPECT_TRUE(file.isEmpty()) << qUtf8Printable(file); +} + +TEST(bookmarks, BookmarksTreeModel) +{ + BookmarksTreeModel model; + QFile f("bookmarks.xbel"); + ASSERT_TRUE(f.open(QIODevice::ReadOnly | QIODevice::Text)); + + EXPECT_TRUE(model.load(&f).isEmpty()); + f.close(); +} +} // namespace |