KBookmarks

konqbookmarkmenu.cpp
1 /*
2  This file is part of the KDE project
3  SPDX-FileCopyrightText: 1998, 1999 Torben Weis <[email protected]>
4  SPDX-FileCopyrightText: 2006 Daniel Teske <[email protected]>
5 
6  SPDX-License-Identifier: LGPL-2.0-or-later
7 */
8 
9 #include "konqbookmarkmenu.h"
10 
11 #if KBOOKMARKS_BUILD_DEPRECATED_SINCE(5, 65)
12 
13 #include "kbookmarkaction.h"
14 
15 #include "kbookmarks_debug.h"
16 #include <QFile>
17 #include <QMenu>
18 
19 #include <KActionCollection>
20 #include <KConfig>
21 #include <KConfigGroup>
22 #include <KSharedConfig>
23 #include <KStringHandler>
24 
25 #include "kbookmarkimporter.h"
26 #include "kbookmarkimporter_ie.h"
27 #include "kbookmarkimporter_opera.h"
28 #include "kbookmarkmanager.h"
29 #include "konqbookmarkmenu_p.h"
30 
31 KImportedBookmarkMenu::KImportedBookmarkMenu(KBookmarkManager *mgr, KBookmarkOwner *owner, QMenu *parentMenu, const QString &type, const QString &location)
32  : KBookmarkMenu(mgr, owner, parentMenu, QString())
33  , m_type(type)
34  , m_location(location)
35 {
36  connect(parentMenu, &QMenu::aboutToShow, this, &KImportedBookmarkMenu::slotNSLoad);
37 }
38 
39 KImportedBookmarkMenu::KImportedBookmarkMenu(KBookmarkManager *mgr, KBookmarkOwner *owner, QMenu *parentMenu)
40  : KBookmarkMenu(mgr, owner, parentMenu, QString())
41  , m_type(QString())
42  , m_location(QString())
43 {
44 }
45 
46 KImportedBookmarkMenu::~KImportedBookmarkMenu()
47 {
48 }
49 
50 void KImportedBookmarkMenu::refill()
51 {
52 }
53 
54 void KImportedBookmarkMenu::clear()
55 {
56 }
57 
58 void KImportedBookmarkMenu::slotNSLoad()
59 {
60  // qCDebug(KBOOKMARKS_LOG)<<"**** slotNSLoad ****"<<m_type<<" "<<m_location;
61  // only fill menu once
62  disconnect(parentMenu(), &QMenu::aboutToShow, nullptr, nullptr);
63 
64  // not NSImporter, but kept old name for BC reasons
65  KBookmarkMenuImporter importer(manager(), this);
66  importer.openBookmarks(m_location, m_type);
67 }
68 
69 /********************************************************************/
70 
71 void KBookmarkMenuImporter::openBookmarks(const QString &location, const QString &type)
72 {
73  mstack.push(m_menu);
74 
75  KBookmarkImporterBase *importer = KBookmarkImporterBase::factory(type);
76  if (!importer) {
77  return;
78  }
79  importer->setFilename(location);
80  connectToImporter(*importer);
81  importer->parse();
82 
83  delete importer;
84 }
85 
86 void KBookmarkMenuImporter::connectToImporter(const QObject &importer)
87 {
88  // clang-format off
89  connect(&importer, SIGNAL(newBookmark(QString,QString,QString)), SLOT(newBookmark(QString,QString,QString)));
90  connect(&importer, SIGNAL(newFolder(QString,bool,QString)), SLOT(newFolder(QString,bool,QString)));
91  connect(&importer, SIGNAL(newSeparator()), SLOT(newSeparator()));
92  connect(&importer, SIGNAL(endFolder()), SLOT(endFolder()));
93  // clang-format on
94 }
95 
96 void KBookmarkMenuImporter::newBookmark(const QString &text, const QString &url, const QString &)
97 {
98  KBookmark bm = KBookmark::standaloneBookmark(text, QUrl(url), QStringLiteral("html"));
99  QAction *action = new KBookmarkAction(bm, mstack.top()->owner(), this);
100  mstack.top()->parentMenu()->addAction(action);
101  mstack.top()->m_actions.append(action);
102 }
103 
104 void KBookmarkMenuImporter::newFolder(const QString &text, bool, const QString &)
105 {
107  KActionMenu *actionMenu = new KImportedBookmarkActionMenu(QIcon::fromTheme(QStringLiteral("folder")), _text, this);
108  mstack.top()->parentMenu()->addAction(actionMenu);
109  mstack.top()->m_actions.append(actionMenu);
110  KImportedBookmarkMenu *subMenu = new KImportedBookmarkMenu(m_pManager, m_menu->owner(), actionMenu->menu());
111  mstack.top()->m_lstSubMenus.append(subMenu);
112 
113  mstack.push(subMenu);
114 }
115 
116 void KBookmarkMenuImporter::newSeparator()
117 {
118  mstack.top()->parentMenu()->addSeparator();
119 }
120 
121 void KBookmarkMenuImporter::endFolder()
122 {
123  mstack.pop();
124 }
125 
126 /********************************************************************/
127 
129  : KBookmarkContextMenu(bm, mgr, owner)
130 {
131 }
132 
133 KonqBookmarkContextMenu::~KonqBookmarkContextMenu()
134 {
135 }
136 
137 void KonqBookmarkContextMenu::addActions()
138 {
139  KConfigGroup config = KSharedConfig::openConfig(QStringLiteral("kbookmarkrc"), KConfig::NoGlobals)->group("Bookmarks");
140  bool filteredToolbar = config.readEntry("FilteredToolbar", false);
141 
142  if (bookmark().isGroup()) {
143  addOpenFolderInTabs();
144  addBookmark();
145 
146  if (filteredToolbar) {
147  QString text = bookmark().showInToolbar() ? tr("Hide in Toolbar", "@action:inmenu") : tr("Show in Toolbar", "@action:inmenu");
148  addAction(text, this, &KonqBookmarkContextMenu::toggleShowInToolbar);
149  }
150 
151  addFolderActions();
152  } else {
153  if (owner()) {
154  addAction(QIcon::fromTheme(QStringLiteral("window-new")),
155  tr("Open in New Window", "@action:inmenu"),
156  this,
157  &KonqBookmarkContextMenu::openInNewWindow);
158  addAction(QIcon::fromTheme(QStringLiteral("tab-new")), tr("Open in New Tab", "@action:inmenu"), this, &KonqBookmarkContextMenu::openInNewTab);
159  }
160  addBookmark();
161 
162  if (filteredToolbar) {
163  const QString text = bookmark().showInToolbar() ? tr("Hide in Toolbar", "@action:inmenu") : tr("Show in Toolbar", "@action:inmenu");
164  addAction(text, this, &KonqBookmarkContextMenu::toggleShowInToolbar);
165  }
166 
167  addBookmarkActions();
168  }
169 }
170 
171 void KonqBookmarkContextMenu::toggleShowInToolbar()
172 {
173  bookmark().setShowInToolbar(!bookmark().showInToolbar());
174  manager()->emitChanged(bookmark().parentGroup());
175 }
176 
177 void KonqBookmarkContextMenu::openInNewTab()
178 {
179  owner()->openInNewTab(bookmark());
180 }
181 
182 void KonqBookmarkContextMenu::openInNewWindow()
183 {
184  owner()->openInNewWindow(bookmark());
185 }
186 
187 /******************************/
188 /******************************/
189 /******************************/
190 
191 void KonqBookmarkMenu::fillDynamicBookmarks()
192 {
193  if (isDirty() && KBookmarkManager::userBookmarksManager()->path() == manager()->path()) {
194  bool haveSep = false;
195 
197  for (QStringList::const_iterator it = keys.begin(); it != keys.end(); ++it) {
198  DynMenuInfo info;
199  info = showDynamicBookmarks((*it));
200 
201  if (!info.show || !QFile::exists(info.location)) {
202  continue;
203  }
204 
205  if (!haveSep) {
207  haveSep = true;
208  }
209 
210  KActionMenu *actionMenu;
211  actionMenu = new KActionMenu(QIcon::fromTheme(info.type), info.name, this);
212  m_actionCollection->addAction(QStringLiteral("kbookmarkmenu"), actionMenu);
213 
214  parentMenu()->addAction(actionMenu);
215  m_actions.append(actionMenu);
216 
217  KImportedBookmarkMenu *subMenu = new KImportedBookmarkMenu(manager(), owner(), actionMenu->menu(), info.type, info.location);
218  m_lstSubMenus.append(subMenu);
219  }
220  }
221 }
222 
223 void KonqBookmarkMenu::refill()
224 {
225  if (isRoot()) {
226  addActions();
227  }
228  fillDynamicBookmarks();
229  fillBookmarks();
230  if (!isRoot()) {
231  addActions();
232  }
233 }
234 
235 QAction *KonqBookmarkMenu::actionForBookmark(const KBookmark &bm)
236 {
237  if (bm.isGroup()) {
238  // qCDebug(KBOOKMARKS_LOG) << "Creating Konq bookmark submenu named " << bm.text();
239  KBookmarkActionMenu *actionMenu = new KBookmarkActionMenu(bm, this);
240  m_actionCollection->addAction(QStringLiteral("kbookmarkmenu"), actionMenu);
241  m_actions.append(actionMenu);
242 
243  KBookmarkMenu *subMenu = new KonqBookmarkMenu(manager(), owner(), actionMenu, bm.address());
244 
245  m_lstSubMenus.append(subMenu);
246  return actionMenu;
247  } else if (bm.isSeparator()) {
248  return KBookmarkMenu::actionForBookmark(bm);
249  } else {
250  // qCDebug(KBOOKMARKS_LOG) << "Creating Konq bookmark action named " << bm.text();
251  KBookmarkAction *action = new KBookmarkAction(bm, owner(), this);
252  m_actionCollection->addAction(action->objectName(), action);
253  m_actions.append(action);
254  return action;
255  }
256 }
257 
259 {
260  KConfig bookmarkrc(QStringLiteral("kbookmarkrc"), KConfig::NoGlobals);
261  KConfigGroup config(&bookmarkrc, "Bookmarks");
262 
263  DynMenuInfo info;
264  info.show = false;
265  info.d = nullptr;
266 
267  if (!config.hasKey("DynamicMenus")) {
268  const QString dynamicMenuGroupId = QLatin1String("DynamicMenu-") + id;
269  if (bookmarkrc.hasGroup(dynamicMenuGroupId)) {
270  KConfigGroup dynGroup(&bookmarkrc, dynamicMenuGroupId);
271  info.show = dynGroup.readEntry("Show", false);
272  info.location = dynGroup.readPathEntry("Location", QString());
273  info.type = dynGroup.readEntry("Type");
274  info.name = dynGroup.readEntry("Name");
275  }
276  }
277  return info;
278 }
279 
281 {
282  KConfigGroup config = KSharedConfig::openConfig(QStringLiteral("kbookmarkrc"), KConfig::NoGlobals)->group("Bookmarks");
283 
284  QStringList mlist;
285  if (config.hasKey("DynamicMenus")) {
286  mlist = config.readEntry("DynamicMenus", QStringList());
287  }
288 
289  return mlist;
290 }
291 
293 {
294  KSharedConfig::Ptr kbookmarkrc = KSharedConfig::openConfig(QStringLiteral("kbookmarkrc"), KConfig::NoGlobals);
295  KConfigGroup dynConfig = kbookmarkrc->group(QLatin1String("DynamicMenu-") + id);
296 
297  // add group unconditionally
298  dynConfig.writeEntry("Show", newMenu.show);
299  dynConfig.writePathEntry("Location", newMenu.location);
300  dynConfig.writeEntry("Type", newMenu.type);
301  dynConfig.writeEntry("Name", newMenu.name);
302 
303  QStringList elist;
304  KConfigGroup config = kbookmarkrc->group("Bookmarks");
305  if (config.hasKey("DynamicMenus")) {
306  elist = config.readEntry("DynamicMenus", QStringList());
307  }
308 
309  // make sure list includes type
310  if (!elist.contains(id)) {
311  elist << id;
312  config.writeEntry("DynamicMenus", elist);
313  }
314 
315  config.sync();
316 }
317 
318 QMenu *KonqBookmarkMenu::contextMenu(QAction *action)
319 {
320  KBookmarkActionInterface *act = dynamic_cast<KBookmarkActionInterface *>(action);
321  if (!act) {
322  return nullptr;
323  }
324  return new KonqBookmarkContextMenu(act->bookmark(), manager(), owner());
325 }
326 
327 #include "moc_konqbookmarkmenu.cpp"
328 #include "moc_konqbookmarkmenu_p.cpp"
329 
330 #endif // KBOOKMARKS_BUILD_DEPRECATED_SINCE(5, 65)
void append(const T &value)
KonqBookmarkMenu(KBookmarkManager *mgr, KBookmarkOwner *owner, KBookmarkActionMenu *parentMenu, KActionCollection *collec)
Fills a bookmark menu with konquerors bookmarks (one instance of KonqBookmarkMenu is created for the ...
QAction * addAction(const QString &name, const QObject *receiver=nullptr, const char *member=nullptr)
QString readEntry(const char *key, const char *aDefault=nullptr) const
void writeEntry(const char *key, const char *value, WriteConfigFlags pFlags=Normal)
QString name() const const
static KBookmark standaloneBookmark(const QString &text, const QUrl &url, const QString &icon)
Creates a stand alone bookmark.
Definition: kbookmark.cpp:507
QVariant location(const QVariant &res)
static QStringList dynamicBookmarksList()
Type type(const QSqlDatabase &db)
void emitChanged()
Saves the bookmark file and notifies everyone.
bool contains(const QString &str, Qt::CaseSensitivity cs) const const
bool isSeparator() const
Whether the bookmark is a separator.
Definition: kbookmark.cpp:294
QIcon fromTheme(const QString &name)
QAction * addSeparator()
static KBookmarkManager * userBookmarksManager()
Returns a pointer to the user's main (konqueror) bookmark collection.
static void setDynamicBookmarks(const QString &id, const DynMenuInfo &info)
Shows an extra menu for the given bookmarks file and type.
QMenu * parentMenu() const
The menu in which we insert our actions Supplied in the constructor.
bool exists() const const
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
QString address() const
Return the "address" of this bookmark in the whole tree.
Definition: kbookmark.cpp:479
QAction * addAction(const QString &text)
QMenu * menu() const const
Browser-specific context menu.
KCOREADDONS_EXPORT QString csqueeze(const QString &str, int maxlen=40)
bool showInToolbar() const
Definition: kbookmark.cpp:453
void setShowInToolbar(bool show)
Set whether this bookmark is show in a filterd toolbar.
Definition: kbookmark.cpp:463
static DynMenuInfo showDynamicBookmarks(const QString &id)
KConfigGroup group(const char *group)
KonqBookmarkContextMenu(const KBookmark &bm, KBookmarkManager *mgr, KBookmarkOwner *owner)
Browser-specific context menu.
virtual void openInNewTab(const KBookmark &bm)
Called when a bookmark should be opened in a new tab.
KSharedConfigPtr config()
QString & replace(int position, int n, QChar after)
QString path(const QString &relativePath)
bool hasGroup(const char *group) const
QList< KBookmarkMenu * > m_lstSubMenus
List of our sub menus.
void writePathEntry(const char *Key, const QString &path, WriteConfigFlags pFlags=Normal)
QList::iterator begin()
virtual void openInNewWindow(const KBookmark &bm)
Called when a bookmark should be opened in a new window.
QString readPathEntry(const char *key, const QString &aDefault) const
bool isGroup() const
Whether the bookmark is a group or a normal bookmark.
Definition: kbookmark.cpp:287
Structure used for storing information about the dynamic menu setting.
QList::iterator end()
QString tr(const char *sourceText, const char *disambiguation, int n)
QList< QAction * > m_actions
List of our actions.
void aboutToShow()
A class for importing NS bookmarks KEditBookmarks uses it to insert bookmarks into its DOM tree,...
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sat Sep 30 2023 03:56:28 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.