Commit 58f4b1e5 authored by David Faure's avatar David Faure
Browse files

Move KonqBookmarkMenu from KF5::Bookmarks to here, its only user.

parent 18a3a56e
......@@ -48,6 +48,7 @@ set(konqueror_KDEINIT_SRCS
konqapplication.cpp
konqdebug.cpp
konqguiclients.cpp
konqbookmarkmenu.cpp
konqrun.cpp
konqview.cpp
konqviewmanager.cpp
......
/* This file is part of the KDE project
Copyright (C) 1998, 1999 Torben Weis <weis@kde.org>
Copyright (C) 2006 Daniel Teske <teske@squorn.de>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "konqbookmarkmenu.h"
#include "kbookmarkowner.h"
#include "kbookmarkaction.h"
#include "kbookmarkcontextmenu.h"
#include <QMenu>
#include <QFile>
#include <kconfig.h>
#include <ksharedconfig.h>
#include <kconfiggroup.h>
#include <kactioncollection.h>
#include <kstringhandler.h>
#include "kbookmarkimporter.h"
#include "kbookmarkimporter_opera.h"
#include "kbookmarkimporter_ie.h"
#include "kbookmarkmanager.h"
#include "konqbookmarkmenu_p.h"
using namespace Konqueror;
KImportedBookmarkMenu::KImportedBookmarkMenu(KBookmarkManager *mgr,
KBookmarkOwner *owner, QMenu *parentMenu,
const QString &type, const QString &location)
: KBookmarkMenu(mgr, owner, parentMenu, QString()), m_type(type), m_location(location)
{
connect(parentMenu, &QMenu::aboutToShow, this, &KImportedBookmarkMenu::slotNSLoad);
}
KImportedBookmarkMenu::KImportedBookmarkMenu(KBookmarkManager *mgr,
KBookmarkOwner *owner, QMenu *parentMenu)
: KBookmarkMenu(mgr, owner, parentMenu, QString()), m_type(QString()), m_location(QString())
{
}
KImportedBookmarkMenu::~KImportedBookmarkMenu()
{
}
void KImportedBookmarkMenu::refill()
{
}
void KImportedBookmarkMenu::clear()
{
}
void KImportedBookmarkMenu::slotNSLoad()
{
// qCDebug(KBOOKMARKS_LOG)<<"**** slotNSLoad ****"<<m_type<<" "<<m_location;
// only fill menu once
disconnect(parentMenu(), &QMenu::aboutToShow, nullptr, nullptr);
// not NSImporter, but kept old name for BC reasons
KBookmarkMenuImporter importer(manager(), this);
importer.openBookmarks(m_location, m_type);
}
/********************************************************************/
void KBookmarkMenuImporter::openBookmarks(const QString &location, const QString &type)
{
mstack.push(m_menu);
KBookmarkImporterBase *importer = KBookmarkImporterBase::factory(type);
if (!importer) {
return;
}
importer->setFilename(location);
connectToImporter(*importer);
importer->parse();
delete importer;
}
void KBookmarkMenuImporter::connectToImporter(const QObject &importer)
{
connect(&importer, SIGNAL(newBookmark(QString,QString,QString)),
SLOT(newBookmark(QString,QString,QString)));
connect(&importer, SIGNAL(newFolder(QString,bool,QString)),
SLOT(newFolder(QString,bool,QString)));
connect(&importer, SIGNAL(newSeparator()), SLOT(newSeparator()));
connect(&importer, SIGNAL(endFolder()), SLOT(endFolder()));
}
void KBookmarkMenuImporter::newBookmark(const QString &text, const QString &url, const QString &)
{
KBookmark bm = KBookmark::standaloneBookmark(text, QUrl(url), QStringLiteral("html"));
QAction *action = new KBookmarkAction(bm, mstack.top()->owner(), this);
mstack.top()->parentMenu()->addAction(action);
mstack.top()->m_actions.append(action);
}
void KBookmarkMenuImporter::newFolder(const QString &text, bool, const QString &)
{
QString _text = KStringHandler::csqueeze(text).replace(QLatin1Char('&'), QLatin1String("&&"));
KActionMenu *actionMenu = new KImportedBookmarkActionMenu(QIcon::fromTheme(QStringLiteral("folder")), _text, this);
mstack.top()->parentMenu()->addAction(actionMenu);
mstack.top()->m_actions.append(actionMenu);
KImportedBookmarkMenu *subMenu = new KImportedBookmarkMenu(m_pManager, m_menu->owner(), actionMenu->menu());
mstack.top()->m_lstSubMenus.append(subMenu);
mstack.push(subMenu);
}
void KBookmarkMenuImporter::newSeparator()
{
mstack.top()->parentMenu()->addSeparator();
}
void KBookmarkMenuImporter::endFolder()
{
mstack.pop();
}
/********************************************************************/
KonqBookmarkContextMenu::KonqBookmarkContextMenu(const KBookmark &bm, KBookmarkManager *mgr, KBookmarkOwner *owner)
: KBookmarkContextMenu(bm, mgr, owner)
{
}
KonqBookmarkContextMenu::~KonqBookmarkContextMenu()
{
}
void KonqBookmarkContextMenu::addActions()
{
KConfigGroup config = KSharedConfig::openConfig(QStringLiteral("kbookmarkrc"), KConfig::NoGlobals)->group("Bookmarks");
bool filteredToolbar = config.readEntry("FilteredToolbar", false);
if (bookmark().isGroup()) {
addOpenFolderInTabs();
addBookmark();
if (filteredToolbar) {
QString text = bookmark().showInToolbar() ? tr("Hide in toolbar") : tr("Show in toolbar");
addAction(text, this, &KonqBookmarkContextMenu::toggleShowInToolbar);
}
addFolderActions();
} else {
if (owner()) {
addAction(QIcon::fromTheme(QStringLiteral("window-new")), tr("Open in New Window"), this, &KonqBookmarkContextMenu::openInNewWindow);
addAction(QIcon::fromTheme(QStringLiteral("tab-new")), tr("Open in New Tab"), this, &KonqBookmarkContextMenu::openInNewTab);
}
addBookmark();
if (filteredToolbar) {
QString text = bookmark().showInToolbar() ? tr("Hide in toolbar") : tr("Show in toolbar");
addAction(text, this, &KonqBookmarkContextMenu::toggleShowInToolbar);
}
addBookmarkActions();
}
}
void KonqBookmarkContextMenu::toggleShowInToolbar()
{
bookmark().setShowInToolbar(!bookmark().showInToolbar());
manager()->emitChanged(bookmark().parentGroup());
}
void KonqBookmarkContextMenu::openInNewTab()
{
owner()->openInNewTab(bookmark());
}
void KonqBookmarkContextMenu::openInNewWindow()
{
owner()->openInNewWindow(bookmark());
}
/******************************/
/******************************/
/******************************/
void KonqBookmarkMenu::fillDynamicBookmarks()
{
if (isDirty()
&& KBookmarkManager::userBookmarksManager()->path() == manager()->path()) {
bool haveSep = false;
const QStringList keys = KonqBookmarkMenu::dynamicBookmarksList();
for (QStringList::const_iterator it = keys.begin(); it != keys.end(); ++it) {
DynMenuInfo info;
info = showDynamicBookmarks((*it));
if (!info.show || !QFile::exists(info.location)) {
continue;
}
if (!haveSep) {
parentMenu()->addSeparator();
haveSep = true;
}
KActionMenu *actionMenu;
actionMenu = new KActionMenu(QIcon::fromTheme(info.type), info.name, this);
m_actionCollection->addAction(QStringLiteral("kbookmarkmenu"), actionMenu);
parentMenu()->addAction(actionMenu);
m_actions.append(actionMenu);
KImportedBookmarkMenu *subMenu =
new KImportedBookmarkMenu(manager(), owner(), actionMenu->menu(),
info.type, info.location);
m_lstSubMenus.append(subMenu);
}
}
}
void KonqBookmarkMenu::refill()
{
if (isRoot()) {
addActions();
}
fillDynamicBookmarks();
fillBookmarks();
if (!isRoot()) {
addActions();
}
}
QAction *KonqBookmarkMenu::actionForBookmark(const KBookmark &bm)
{
if (bm.isGroup()) {
// qCDebug(KBOOKMARKS_LOG) << "Creating Konq bookmark submenu named " << bm.text();
KBookmarkActionMenu *actionMenu = new KBookmarkActionMenu(bm, this);
m_actionCollection->addAction(QStringLiteral("kbookmarkmenu"), actionMenu);
m_actions.append(actionMenu);
KBookmarkMenu *subMenu = new KonqBookmarkMenu(manager(), owner(), actionMenu, bm.address());
m_lstSubMenus.append(subMenu);
return actionMenu;
} else if (bm.isSeparator()) {
return KBookmarkMenu::actionForBookmark(bm);
} else {
// qCDebug(KBOOKMARKS_LOG) << "Creating Konq bookmark action named " << bm.text();
KBookmarkAction *action = new KBookmarkAction(bm, owner(), this);
m_actionCollection->addAction(action->objectName(), action);
m_actions.append(action);
return action;
}
}
KonqBookmarkMenu::DynMenuInfo KonqBookmarkMenu::showDynamicBookmarks(const QString &id)
{
KConfig bookmarkrc(QStringLiteral("kbookmarkrc"), KConfig::NoGlobals);
KConfigGroup config(&bookmarkrc, "Bookmarks");
DynMenuInfo info;
info.show = false;
info.d = nullptr;
if (!config.hasKey("DynamicMenus")) {
const QString dynamicMenuGroupId = QLatin1String("DynamicMenu-") + id;
if (bookmarkrc.hasGroup(dynamicMenuGroupId)) {
KConfigGroup dynGroup(&bookmarkrc, dynamicMenuGroupId);
info.show = dynGroup.readEntry("Show", false);
info.location = dynGroup.readPathEntry("Location", QString());
info.type = dynGroup.readEntry("Type");
info.name = dynGroup.readEntry("Name");
}
}
return info;
}
QStringList KonqBookmarkMenu::dynamicBookmarksList()
{
KConfigGroup config = KSharedConfig::openConfig(QStringLiteral("kbookmarkrc"), KConfig::NoGlobals)->group("Bookmarks");
QStringList mlist;
if (config.hasKey("DynamicMenus")) {
mlist = config.readEntry("DynamicMenus", QStringList());
}
return mlist;
}
void KonqBookmarkMenu::setDynamicBookmarks(const QString &id, const DynMenuInfo &newMenu)
{
KSharedConfig::Ptr kbookmarkrc = KSharedConfig::openConfig(QStringLiteral("kbookmarkrc"), KConfig::NoGlobals);
KConfigGroup dynConfig = kbookmarkrc->group(QLatin1String("DynamicMenu-") + id);
// add group unconditionally
dynConfig.writeEntry("Show", newMenu.show);
dynConfig.writePathEntry("Location", newMenu.location);
dynConfig.writeEntry("Type", newMenu.type);
dynConfig.writeEntry("Name", newMenu.name);
QStringList elist;
KConfigGroup config = kbookmarkrc->group("Bookmarks");
if (config.hasKey("DynamicMenus")) {
elist = config.readEntry("DynamicMenus", QStringList());
}
// make sure list includes type
if (!elist.contains(id)) {
elist << id;
config.writeEntry("DynamicMenus", elist);
}
config.sync();
}
QMenu *KonqBookmarkMenu::contextMenu(QAction *action)
{
KBookmarkActionInterface *act = dynamic_cast<KBookmarkActionInterface *>(action);
if (!act) {
return nullptr;
}
return new KonqBookmarkContextMenu(act->bookmark(), manager(), owner());
}
#include "moc_konqbookmarkmenu.cpp"
/* This file is part of the KDE project
Copyright (C) 1998, 1999 Torben Weis <weis@kde.org>
Copyright (C) 2006 Daniel Teske <teske@squorn.de>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#ifndef __konqbookmarkmenu_h__
#define __konqbookmarkmenu_h__
#include "kbookmarkmenu.h"
#include "kbookmarkactionmenu.h"
#include "kbookmarkcontextmenu.h"
namespace Konqueror { // to avoid clashing with KF5::Bookmarks which had a KonqBookmarkMenu class. Remove once using KF6.
class KonqBookmarkMenu : public KBookmarkMenu
{
//friend class KBookmarkBar;
Q_OBJECT
public:
/**
* Fills a bookmark menu with konquerors bookmarks
* (one instance of KonqBookmarkMenu is created for the toplevel menu,
* but also one per submenu).
*
* @param mgr The bookmark manager to use (i.e. for reading and writing)
* @param owner implementation of the KBookmarkOwner callback interface.
* Note: If you pass a null KBookmarkOwner to the constructor, the
* URLs are openend by QDesktopServices::openUrl and "Add Bookmark" is disabled.
* @param parentMenu menu to be filled
* @param collec parent collection for the KActions.
*/
KonqBookmarkMenu(KBookmarkManager *mgr, KBookmarkOwner *owner, KBookmarkActionMenu *parentMenu, KActionCollection *collec)
: KBookmarkMenu(mgr, owner, parentMenu->menu(), collec)
{
}
~KonqBookmarkMenu() override
{}
/**
* Creates a bookmark submenu.
* Only used internally and for bookmark toolbar.
*/
KonqBookmarkMenu(KBookmarkManager *mgr, KBookmarkOwner *owner, KBookmarkActionMenu *parentMenu, QString parentAddress)
: KBookmarkMenu(mgr, owner, parentMenu->menu(), parentAddress)
{
}
protected:
/**
* Structure used for storing information about
* the dynamic menu setting
*/
struct DynMenuInfo {
bool show;
QString location;
QString type;
QString name;
class DynMenuInfoPrivate *d;
};
/**
* @return dynmenu info block for the given dynmenu name
*/
static DynMenuInfo showDynamicBookmarks(const QString &id);
/**
* Shows an extra menu for the given bookmarks file and type.
* Upgrades from option inside XBEL to option in rc file
* on first call of this function.
* @param id the unique identification for the dynamic menu
* @param info a DynMenuInfo struct containing the to be added/modified data
*/
static void setDynamicBookmarks(const QString &id, const DynMenuInfo &info);
/**
* @return list of dynamic menu ids
*/
static QStringList dynamicBookmarksList();
void refill() override;
QAction *actionForBookmark(const KBookmark &bm) override;
QMenu *contextMenu(QAction *action) override;
void fillDynamicBookmarks();
};
class KonqBookmarkContextMenu : public KBookmarkContextMenu
{
Q_OBJECT
public:
KonqBookmarkContextMenu(const KBookmark &bm, KBookmarkManager *mgr, KBookmarkOwner *owner);
~KonqBookmarkContextMenu() override;
void addActions() override;
public Q_SLOTS:
void openInNewTab();
void openInNewWindow();
void toggleShowInToolbar();
};
}
#endif
/* This file is part of the KDE project
Copyright (C) 2003 Alexander Kellett <lypanov@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "kbookmarkmenu.h"
#include "kbookmarkimporter.h"
#include "kbookmarkactioninterface.h"
#include <kactionmenu.h>
#include <QStack>
class QString;
class QMenu;
class KBookmark;
class KBookmarkGroup;
class KBookmarkOwner;
namespace Konqueror { // to avoid clashes with KF5::Bookmarks classes
class KImportedBookmarkMenu : public KBookmarkMenu
{
friend class KBookmarkMenuImporter;
Q_OBJECT
public:
//TODO simplfy
KImportedBookmarkMenu(KBookmarkManager *mgr,
KBookmarkOwner *owner, QMenu *parentMenu,
const QString &type, const QString &location);
KImportedBookmarkMenu(KBookmarkManager *mgr,
KBookmarkOwner *owner, QMenu *parentMenu);
~KImportedBookmarkMenu();
void clear() override;
void refill() override;
protected Q_SLOTS:
void slotNSLoad();
private:
QString m_type;
QString m_location;
};
/**
* A class connected to KNSBookmarkImporter, to fill KActionMenus.
*/
class KBookmarkMenuImporter : public QObject
{
Q_OBJECT
public:
KBookmarkMenuImporter(KBookmarkManager *mgr, KImportedBookmarkMenu *menu) :
m_menu(menu), m_pManager(mgr) {}
void openBookmarks(const QString &location, const QString &type);
void connectToImporter(const QObject &importer);
protected Q_SLOTS:
void newBookmark(const QString &text, const QString &url, const QString &);
void newFolder(const QString &text, bool, const QString &);
void newSeparator();
void endFolder();
protected:
QStack<KImportedBookmarkMenu *> mstack;
KImportedBookmarkMenu *m_menu;
KBookmarkManager *m_pManager;
};
class KImportedBookmarkActionMenu : public KActionMenu, public KBookmarkActionInterface
{
Q_OBJECT
public:
KImportedBookmarkActionMenu(const QIcon &icon, const QString &text, QObject *parent)
: KActionMenu(icon, text, parent),
KBookmarkActionInterface(KBookmark())
{
}
~KImportedBookmarkActionMenu()
{}
};
}
......@@ -22,6 +22,7 @@
#define KONQEXTENDEDBOOKMARKOWNER_H
#include <konqbookmarkmenu.h>
#include <kbookmarkowner.h>
class KonqExtendedBookmarkOwner : public KBookmarkOwner
{
......
......@@ -3748,7 +3748,7 @@ void KonqMainWindow::initActions()
// don't appear in kedittoolbar
m_bookmarksActionCollection = new KActionCollection(static_cast<QWidget *>(this));
m_pBookmarkMenu = new KonqBookmarkMenu(s_bookmarkManager, m_pBookmarksOwner, m_pamBookmarks, m_bookmarksActionCollection);
m_pBookmarkMenu = new Konqueror::KonqBookmarkMenu(s_bookmarkManager, m_pBookmarksOwner, m_pamBookmarks, m_bookmarksActionCollection);
QAction *addBookmark = m_bookmarksActionCollection->action(QStringLiteral("add_bookmark"));
if (addBookmark) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment