File indexing completed on 2024-07-07 14:13:42

0001 /*
0002     This file is part of the KDE project
0003     SPDX-FileCopyrightText: 1998, 1999 Torben Weis <weis@kde.org>
0004     SPDX-FileCopyrightText: 2006 Daniel Teske <teske@squorn.de>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #include "konqbookmarkmenu.h"
0010 
0011 #if KBOOKMARKS_BUILD_DEPRECATED_SINCE(5, 65)
0012 
0013 #include "kbookmarkaction.h"
0014 
0015 #include "kbookmarks_debug.h"
0016 #include <QFile>
0017 #include <QMenu>
0018 
0019 #include <KActionCollection>
0020 #include <KConfig>
0021 #include <KConfigGroup>
0022 #include <KSharedConfig>
0023 #include <KStringHandler>
0024 
0025 #include "kbookmarkimporter.h"
0026 #include "kbookmarkimporter_ie.h"
0027 #include "kbookmarkimporter_opera.h"
0028 #include "kbookmarkmanager.h"
0029 #include "konqbookmarkmenu_p.h"
0030 
0031 KImportedBookmarkMenu::KImportedBookmarkMenu(KBookmarkManager *mgr, KBookmarkOwner *owner, QMenu *parentMenu, const QString &type, const QString &location)
0032     : KBookmarkMenu(mgr, owner, parentMenu, QString())
0033     , m_type(type)
0034     , m_location(location)
0035 {
0036     connect(parentMenu, &QMenu::aboutToShow, this, &KImportedBookmarkMenu::slotNSLoad);
0037 }
0038 
0039 KImportedBookmarkMenu::KImportedBookmarkMenu(KBookmarkManager *mgr, KBookmarkOwner *owner, QMenu *parentMenu)
0040     : KBookmarkMenu(mgr, owner, parentMenu, QString())
0041     , m_type(QString())
0042     , m_location(QString())
0043 {
0044 }
0045 
0046 KImportedBookmarkMenu::~KImportedBookmarkMenu()
0047 {
0048 }
0049 
0050 void KImportedBookmarkMenu::refill()
0051 {
0052 }
0053 
0054 void KImportedBookmarkMenu::clear()
0055 {
0056 }
0057 
0058 void KImportedBookmarkMenu::slotNSLoad()
0059 {
0060     // qCDebug(KBOOKMARKS_LOG)<<"**** slotNSLoad  ****"<<m_type<<"  "<<m_location;
0061     // only fill menu once
0062     disconnect(parentMenu(), &QMenu::aboutToShow, nullptr, nullptr);
0063 
0064     // not NSImporter, but kept old name for BC reasons
0065     KBookmarkMenuImporter importer(manager(), this);
0066     importer.openBookmarks(m_location, m_type);
0067 }
0068 
0069 /********************************************************************/
0070 
0071 void KBookmarkMenuImporter::openBookmarks(const QString &location, const QString &type)
0072 {
0073     mstack.push(m_menu);
0074 
0075     KBookmarkImporterBase *importer = KBookmarkImporterBase::factory(type);
0076     if (!importer) {
0077         return;
0078     }
0079     importer->setFilename(location);
0080     connectToImporter(*importer);
0081     importer->parse();
0082 
0083     delete importer;
0084 }
0085 
0086 void KBookmarkMenuImporter::connectToImporter(const QObject &importer)
0087 {
0088     // clang-format off
0089     connect(&importer, SIGNAL(newBookmark(QString,QString,QString)), SLOT(newBookmark(QString,QString,QString)));
0090     connect(&importer, SIGNAL(newFolder(QString,bool,QString)), SLOT(newFolder(QString,bool,QString)));
0091     connect(&importer, SIGNAL(newSeparator()), SLOT(newSeparator()));
0092     connect(&importer, SIGNAL(endFolder()), SLOT(endFolder()));
0093     // clang-format on
0094 }
0095 
0096 void KBookmarkMenuImporter::newBookmark(const QString &text, const QString &url, const QString &)
0097 {
0098     KBookmark bm = KBookmark::standaloneBookmark(text, QUrl(url), QStringLiteral("html"));
0099     QAction *action = new KBookmarkAction(bm, mstack.top()->owner(), this);
0100     mstack.top()->parentMenu()->addAction(action);
0101     mstack.top()->m_actions.append(action);
0102 }
0103 
0104 void KBookmarkMenuImporter::newFolder(const QString &text, bool, const QString &)
0105 {
0106     QString _text = KStringHandler::csqueeze(text).replace(QLatin1Char('&'), QLatin1String("&&"));
0107     KActionMenu *actionMenu = new KImportedBookmarkActionMenu(QIcon::fromTheme(QStringLiteral("folder")), _text, this);
0108     mstack.top()->parentMenu()->addAction(actionMenu);
0109     mstack.top()->m_actions.append(actionMenu);
0110     KImportedBookmarkMenu *subMenu = new KImportedBookmarkMenu(m_pManager, m_menu->owner(), actionMenu->menu());
0111     mstack.top()->m_lstSubMenus.append(subMenu);
0112 
0113     mstack.push(subMenu);
0114 }
0115 
0116 void KBookmarkMenuImporter::newSeparator()
0117 {
0118     mstack.top()->parentMenu()->addSeparator();
0119 }
0120 
0121 void KBookmarkMenuImporter::endFolder()
0122 {
0123     mstack.pop();
0124 }
0125 
0126 /********************************************************************/
0127 
0128 KonqBookmarkContextMenu::KonqBookmarkContextMenu(const KBookmark &bm, KBookmarkManager *mgr, KBookmarkOwner *owner)
0129     : KBookmarkContextMenu(bm, mgr, owner)
0130 {
0131 }
0132 
0133 KonqBookmarkContextMenu::~KonqBookmarkContextMenu()
0134 {
0135 }
0136 
0137 void KonqBookmarkContextMenu::addActions()
0138 {
0139     KConfigGroup config = KSharedConfig::openConfig(QStringLiteral("kbookmarkrc"), KConfig::NoGlobals)->group("Bookmarks");
0140     bool filteredToolbar = config.readEntry("FilteredToolbar", false);
0141 
0142     if (bookmark().isGroup()) {
0143         addOpenFolderInTabs();
0144         addBookmark();
0145 
0146         if (filteredToolbar) {
0147             QString text = bookmark().showInToolbar() ? tr("Hide in Toolbar", "@action:inmenu") : tr("Show in Toolbar", "@action:inmenu");
0148             addAction(text, this, &KonqBookmarkContextMenu::toggleShowInToolbar);
0149         }
0150 
0151         addFolderActions();
0152     } else {
0153         if (owner()) {
0154             addAction(QIcon::fromTheme(QStringLiteral("window-new")),
0155                       tr("Open in New Window", "@action:inmenu"),
0156                       this,
0157                       &KonqBookmarkContextMenu::openInNewWindow);
0158             addAction(QIcon::fromTheme(QStringLiteral("tab-new")), tr("Open in New Tab", "@action:inmenu"), this, &KonqBookmarkContextMenu::openInNewTab);
0159         }
0160         addBookmark();
0161 
0162         if (filteredToolbar) {
0163             const QString text = bookmark().showInToolbar() ? tr("Hide in Toolbar", "@action:inmenu") : tr("Show in Toolbar", "@action:inmenu");
0164             addAction(text, this, &KonqBookmarkContextMenu::toggleShowInToolbar);
0165         }
0166 
0167         addBookmarkActions();
0168     }
0169 }
0170 
0171 void KonqBookmarkContextMenu::toggleShowInToolbar()
0172 {
0173     bookmark().setShowInToolbar(!bookmark().showInToolbar());
0174     manager()->emitChanged(bookmark().parentGroup());
0175 }
0176 
0177 void KonqBookmarkContextMenu::openInNewTab()
0178 {
0179     owner()->openInNewTab(bookmark());
0180 }
0181 
0182 void KonqBookmarkContextMenu::openInNewWindow()
0183 {
0184     owner()->openInNewWindow(bookmark());
0185 }
0186 
0187 /******************************/
0188 /******************************/
0189 /******************************/
0190 
0191 void KonqBookmarkMenu::fillDynamicBookmarks()
0192 {
0193     if (isDirty() && KBookmarkManager::userBookmarksManager()->path() == manager()->path()) {
0194         bool haveSep = false;
0195 
0196         const QStringList keys = KonqBookmarkMenu::dynamicBookmarksList();
0197         for (QStringList::const_iterator it = keys.begin(); it != keys.end(); ++it) {
0198             DynMenuInfo info;
0199             info = showDynamicBookmarks((*it));
0200 
0201             if (!info.show || !QFile::exists(info.location)) {
0202                 continue;
0203             }
0204 
0205             if (!haveSep) {
0206                 parentMenu()->addSeparator();
0207                 haveSep = true;
0208             }
0209 
0210             KActionMenu *actionMenu;
0211             actionMenu = new KActionMenu(QIcon::fromTheme(info.type), info.name, this);
0212             m_actionCollection->addAction(QStringLiteral("kbookmarkmenu"), actionMenu);
0213 
0214             parentMenu()->addAction(actionMenu);
0215             m_actions.append(actionMenu);
0216 
0217             KImportedBookmarkMenu *subMenu = new KImportedBookmarkMenu(manager(), owner(), actionMenu->menu(), info.type, info.location);
0218             m_lstSubMenus.append(subMenu);
0219         }
0220     }
0221 }
0222 
0223 void KonqBookmarkMenu::refill()
0224 {
0225     if (isRoot()) {
0226         addActions();
0227     }
0228     fillDynamicBookmarks();
0229     fillBookmarks();
0230     if (!isRoot()) {
0231         addActions();
0232     }
0233 }
0234 
0235 QAction *KonqBookmarkMenu::actionForBookmark(const KBookmark &bm)
0236 {
0237     if (bm.isGroup()) {
0238         // qCDebug(KBOOKMARKS_LOG) << "Creating Konq bookmark submenu named " << bm.text();
0239         KBookmarkActionMenu *actionMenu = new KBookmarkActionMenu(bm, this);
0240         m_actionCollection->addAction(QStringLiteral("kbookmarkmenu"), actionMenu);
0241         m_actions.append(actionMenu);
0242 
0243         KBookmarkMenu *subMenu = new KonqBookmarkMenu(manager(), owner(), actionMenu, bm.address());
0244 
0245         m_lstSubMenus.append(subMenu);
0246         return actionMenu;
0247     } else if (bm.isSeparator()) {
0248         return KBookmarkMenu::actionForBookmark(bm);
0249     } else {
0250         // qCDebug(KBOOKMARKS_LOG) << "Creating Konq bookmark action named " << bm.text();
0251         KBookmarkAction *action = new KBookmarkAction(bm, owner(), this);
0252         m_actionCollection->addAction(action->objectName(), action);
0253         m_actions.append(action);
0254         return action;
0255     }
0256 }
0257 
0258 KonqBookmarkMenu::DynMenuInfo KonqBookmarkMenu::showDynamicBookmarks(const QString &id)
0259 {
0260     KConfig bookmarkrc(QStringLiteral("kbookmarkrc"), KConfig::NoGlobals);
0261     KConfigGroup config(&bookmarkrc, "Bookmarks");
0262 
0263     DynMenuInfo info;
0264     info.show = false;
0265     info.d = nullptr;
0266 
0267     if (!config.hasKey("DynamicMenus")) {
0268         const QString dynamicMenuGroupId = QLatin1String("DynamicMenu-") + id;
0269         if (bookmarkrc.hasGroup(dynamicMenuGroupId)) {
0270             KConfigGroup dynGroup(&bookmarkrc, dynamicMenuGroupId);
0271             info.show = dynGroup.readEntry("Show", false);
0272             info.location = dynGroup.readPathEntry("Location", QString());
0273             info.type = dynGroup.readEntry("Type");
0274             info.name = dynGroup.readEntry("Name");
0275         }
0276     }
0277     return info;
0278 }
0279 
0280 QStringList KonqBookmarkMenu::dynamicBookmarksList()
0281 {
0282     KConfigGroup config = KSharedConfig::openConfig(QStringLiteral("kbookmarkrc"), KConfig::NoGlobals)->group("Bookmarks");
0283 
0284     QStringList mlist;
0285     if (config.hasKey("DynamicMenus")) {
0286         mlist = config.readEntry("DynamicMenus", QStringList());
0287     }
0288 
0289     return mlist;
0290 }
0291 
0292 void KonqBookmarkMenu::setDynamicBookmarks(const QString &id, const DynMenuInfo &newMenu)
0293 {
0294     KSharedConfig::Ptr kbookmarkrc = KSharedConfig::openConfig(QStringLiteral("kbookmarkrc"), KConfig::NoGlobals);
0295     KConfigGroup dynConfig = kbookmarkrc->group(QLatin1String("DynamicMenu-") + id);
0296 
0297     // add group unconditionally
0298     dynConfig.writeEntry("Show", newMenu.show);
0299     dynConfig.writePathEntry("Location", newMenu.location);
0300     dynConfig.writeEntry("Type", newMenu.type);
0301     dynConfig.writeEntry("Name", newMenu.name);
0302 
0303     QStringList elist;
0304     KConfigGroup config = kbookmarkrc->group("Bookmarks");
0305     if (config.hasKey("DynamicMenus")) {
0306         elist = config.readEntry("DynamicMenus", QStringList());
0307     }
0308 
0309     // make sure list includes type
0310     if (!elist.contains(id)) {
0311         elist << id;
0312         config.writeEntry("DynamicMenus", elist);
0313     }
0314 
0315     config.sync();
0316 }
0317 
0318 QMenu *KonqBookmarkMenu::contextMenu(QAction *action)
0319 {
0320     KBookmarkActionInterface *act = dynamic_cast<KBookmarkActionInterface *>(action);
0321     if (!act) {
0322         return nullptr;
0323     }
0324     return new KonqBookmarkContextMenu(act->bookmark(), manager(), owner());
0325 }
0326 
0327 #include "moc_konqbookmarkmenu.cpp"
0328 #include "moc_konqbookmarkmenu_p.cpp"
0329 
0330 #endif // KBOOKMARKS_BUILD_DEPRECATED_SINCE(5, 65)