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 #include "kbookmarkowner.h"
11 #include "kbookmarkaction.h"
12 #include "kbookmarkcontextmenu.h"
13 
14 #include "kbookmarks_debug.h"
15 #include <QMenu>
16 #include <QFile>
17 
18 #include <KConfig>
19 #include <KSharedConfig>
20 #include <KConfigGroup>
21 #include <KActionCollection>
22 #include <KStringHandler>
23 
24 #include "kbookmarkimporter.h"
25 #include "kbookmarkimporter_opera.h"
26 #include "kbookmarkimporter_ie.h"
27 #include "kbookmarkmanager.h"
28 #include "konqbookmarkmenu_p.h"
29 
30 #if KBOOKMARKS_BUILD_DEPRECATED_SINCE(5, 65)
31 
32 KImportedBookmarkMenu::KImportedBookmarkMenu(KBookmarkManager *mgr,
33  KBookmarkOwner *owner, QMenu *parentMenu,
34  const QString &type, const QString &location)
35  : KBookmarkMenu(mgr, owner, parentMenu, QString()), m_type(type), m_location(location)
36 {
37  connect(parentMenu, &QMenu::aboutToShow, this, &KImportedBookmarkMenu::slotNSLoad);
38 }
39 
40 KImportedBookmarkMenu::KImportedBookmarkMenu(KBookmarkManager *mgr,
41  KBookmarkOwner *owner, QMenu *parentMenu)
42  : KBookmarkMenu(mgr, owner, parentMenu, QString()), m_type(QString()), 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  connect(&importer, SIGNAL(newBookmark(QString,QString,QString)),
89  SLOT(newBookmark(QString,QString,QString)));
90  connect(&importer, SIGNAL(newFolder(QString,bool,QString)),
91  SLOT(newFolder(QString,bool,QString)));
92  connect(&importer, SIGNAL(newSeparator()), SLOT(newSeparator()));
93  connect(&importer, SIGNAL(endFolder()), SLOT(endFolder()));
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")), tr("Open in New Window", "@action:inmenu"), this, &KonqBookmarkContextMenu::openInNewWindow);
155  addAction(QIcon::fromTheme(QStringLiteral("tab-new")), tr("Open in New Tab", "@action:inmenu"), this, &KonqBookmarkContextMenu::openInNewTab);
156  }
157  addBookmark();
158 
159  if (filteredToolbar) {
160  const QString text = bookmark().showInToolbar() ? tr("Hide in Toolbar", "@action:inmenu") : tr("Show in Toolbar", "@action:inmenu");
161  addAction(text, this, &KonqBookmarkContextMenu::toggleShowInToolbar);
162  }
163 
164  addBookmarkActions();
165  }
166 }
167 
168 void KonqBookmarkContextMenu::toggleShowInToolbar()
169 {
170  bookmark().setShowInToolbar(!bookmark().showInToolbar());
171  manager()->emitChanged(bookmark().parentGroup());
172 }
173 
174 void KonqBookmarkContextMenu::openInNewTab()
175 {
176  owner()->openInNewTab(bookmark());
177 }
178 
179 void KonqBookmarkContextMenu::openInNewWindow()
180 {
181  owner()->openInNewWindow(bookmark());
182 }
183 
184 /******************************/
185 /******************************/
186 /******************************/
187 
188 void KonqBookmarkMenu::fillDynamicBookmarks()
189 {
190  if (isDirty()
191  && KBookmarkManager::userBookmarksManager()->path() == manager()->path()) {
192  bool haveSep = false;
193 
195  for (QStringList::const_iterator it = keys.begin(); it != keys.end(); ++it) {
196  DynMenuInfo info;
197  info = showDynamicBookmarks((*it));
198 
199  if (!info.show || !QFile::exists(info.location)) {
200  continue;
201  }
202 
203  if (!haveSep) {
204  parentMenu()->addSeparator();
205  haveSep = true;
206  }
207 
208  KActionMenu *actionMenu;
209  actionMenu = new KActionMenu(QIcon::fromTheme(info.type), info.name, this);
210  m_actionCollection->addAction(QStringLiteral("kbookmarkmenu"), actionMenu);
211 
212  parentMenu()->addAction(actionMenu);
213  m_actions.append(actionMenu);
214 
215  KImportedBookmarkMenu *subMenu =
216  new KImportedBookmarkMenu(manager(), owner(), actionMenu->menu(),
217  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 
329 #endif // KBOOKMARKS_BUILD_DEPRECATED_SINCE(5, 65)
QString readPathEntry(const QString &pKey, const QString &aDefault) const
bool sync() override
void writePathEntry(const QString &pKey, const QString &path, WriteConfigFlags pFlags=Normal)
static void setDynamicBookmarks(const QString &id, const DynMenuInfo &info)
Shows an extra menu for the given bookmarks file and type.
bool contains(const QString &str, Qt::CaseSensitivity cs) const const
bool hasGroup(const QString &group) const
void writeEntry(const QString &key, const QVariant &value, WriteConfigFlags pFlags=Normal)
This class implements the reading/writing of bookmarks in XML.
Bookmark menu with dynamic import features, only used by Konqueror.
static DynMenuInfo showDynamicBookmarks(const QString &id)
bool exists() const const
QString tr(const char *sourceText, const char *disambiguation, int n)
QAction * addAction(const QString &text)
KonqBookmarkContextMenu(const KBookmark &bm, KBookmarkManager *mgr, KBookmarkOwner *owner)
Browser-specific context menu.
void aboutToShow()
Structure used for storing information about the dynamic menu setting.
KCOREADDONS_EXPORT QString csqueeze(const QString &str, int maxlen=40)
The KBookmarkMenu and KBookmarkBar classes gives the user the ability to either edit bookmarks or add...
This class is a QAction for bookmarks.
static KBookmarkManager * userBookmarksManager()
Returns a pointer to the user&#39;s main (konqueror) bookmark collection.
QList::iterator end()
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
bool hasKey(const QString &key) const
KConfigGroup group(const QString &group)
QString & replace(int position, int n, QChar after)
QString name() const const
This class provides a bookmark menu.
Definition: kbookmarkmenu.h:48
A class for importing NS bookmarks KEditBookmarks uses it to insert bookmarks into its DOM tree...
QIcon fromTheme(const QString &name)
static QStringList dynamicBookmarksList()
QMenu * menu() const const
T readEntry(const QString &key, const T &aDefault) const
QList::iterator begin()
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Aug 10 2020 22:42:54 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.