diff --git a/src/kbookmarkcontextmenu.h b/src/kbookmarkcontextmenu.h index 64f0fa5..9c283c1 100644 --- a/src/kbookmarkcontextmenu.h +++ b/src/kbookmarkcontextmenu.h @@ -1,68 +1,68 @@ /* This file is part of the KDE project Copyright (C) 1998, 1999 Torben Weis Copyright (C) 2006 Daniel Teske 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 KBOOKMARKCONTEXTMENU_H #define KBOOKMARKCONTEXTMENU_H #include #include "kbookmark.h" class KBookmarkManager; class KBookmarkOwner; class KBOOKMARKS_EXPORT KBookmarkContextMenu : public QMenu { Q_OBJECT public: - KBookmarkContextMenu(const KBookmark &bm, KBookmarkManager *manager, KBookmarkOwner *owner, QWidget *parent = 0); + KBookmarkContextMenu(const KBookmark &bm, KBookmarkManager *manager, KBookmarkOwner *owner, QWidget *parent = nullptr); virtual ~KBookmarkContextMenu(); virtual void addActions(); public Q_SLOTS: void slotEditAt(); void slotProperties(); void slotInsert(); void slotRemove(); void slotCopyLocation(); void slotOpenFolderInTabs(); protected: void addBookmark(); void addFolderActions(); void addProperties(); void addBookmarkActions(); void addOpenFolderInTabs(); KBookmarkManager *manager() const; KBookmarkOwner *owner() const; KBookmark bookmark() const; private Q_SLOTS: void slotAboutToShow(); private: KBookmark bm; KBookmarkManager *m_pManager; KBookmarkOwner *m_pOwner; }; #endif diff --git a/src/kbookmarkdialog.cpp b/src/kbookmarkdialog.cpp index 3d8afec..5a9d446 100644 --- a/src/kbookmarkdialog.cpp +++ b/src/kbookmarkdialog.cpp @@ -1,409 +1,409 @@ // -*- c-basic-offset:4; indent-tabs-mode:nil -*- /* This file is part of the KDE libraries Copyright 2007 Daniel Teske This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License version 2 as published by the Free Software Foundation. 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 "kbookmarkdialog.h" #include "kbookmarkdialog_p.h" #include "kbookmarkmanager.h" #include "kbookmarkmenu.h" #include "kbookmarkmenu_p.h" #include #include #include #include #include #include #include #include #include #include KBookmarkDialogPrivate::KBookmarkDialogPrivate(KBookmarkDialog *q) : q(q) - , folderTree(0) + , folderTree(nullptr) , layout(false) { } KBookmarkDialogPrivate::~KBookmarkDialogPrivate() { } void KBookmarkDialogPrivate::initLayout() { QBoxLayout *vbox = new QVBoxLayout; QFormLayout *form = new QFormLayout(); vbox->addLayout(form); form->addRow(titleLabel, title); form->addRow(urlLabel, url); form->addRow(commentLabel, comment); vbox->addWidget(folderTree); vbox->addWidget(buttonBox); q->setLayout(vbox); } void KBookmarkDialogPrivate::initLayoutPrivate() { title = new QLineEdit(q); title->setMinimumWidth(300); titleLabel = new QLabel(KBookmarkDialog::tr("Name:", "@label:textbox"), q); titleLabel->setBuddy(title); url = new QLineEdit(q); url->setMinimumWidth(300); urlLabel = new QLabel(KBookmarkDialog::tr("Location:", "@label:textbox"), q); urlLabel->setBuddy(url); comment = new QLineEdit(q); comment->setMinimumWidth(300); commentLabel = new QLabel(KBookmarkDialog::tr("Comment:", "@label:textbox"), q); commentLabel->setBuddy(comment); folderTree = new QTreeWidget(q); folderTree->setColumnCount(1); folderTree->header()->hide(); folderTree->setSortingEnabled(false); folderTree->setSelectionMode(QTreeWidget::SingleSelection); folderTree->setSelectionBehavior(QTreeWidget::SelectRows); folderTree->setMinimumSize(60, 100); QTreeWidgetItem *root = new KBookmarkTreeItem(folderTree); fillGroup(root, mgr->root()); buttonBox = new QDialogButtonBox(q); buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); q->connect(buttonBox, SIGNAL(accepted()), q, SLOT(accept())); q->connect(buttonBox, &QDialogButtonBox::rejected, q, &QDialog::reject); initLayout(); layout = true; } void KBookmarkDialogPrivate::fillGroup(QTreeWidgetItem *parentItem, const KBookmarkGroup &group, const KBookmarkGroup &selectGroup) { for (KBookmark bk = group.first(); !bk.isNull(); bk = group.next(bk)) { if (bk.isGroup()) { const KBookmarkGroup bkGroup = bk.toGroup(); QTreeWidgetItem* item = new KBookmarkTreeItem(parentItem, folderTree, bkGroup); if (selectGroup == bkGroup) { folderTree->setCurrentItem(item); } fillGroup(item, bkGroup, selectGroup); } } } void KBookmarkDialogPrivate::setParentBookmark(const KBookmark &bm) { QString address = bm.address(); KBookmarkTreeItem *item = static_cast(folderTree->topLevelItem(0)); while (true) { if (item->address() == bm.address()) { folderTree->setCurrentItem(item); return; } for (int i = 0; i < item->childCount(); ++i) { KBookmarkTreeItem *child = static_cast(item->child(i)); if (KBookmark::commonParent(child->address(), address) == child->address()) { item = child; break; } } } } KBookmarkGroup KBookmarkDialogPrivate::parentBookmark() { KBookmarkTreeItem *item = dynamic_cast(folderTree->currentItem()); if (!item) { return mgr->root(); } const QString &address = item->address(); return mgr->findByAddress(address).toGroup(); } void KBookmarkDialog::accept() { if (d->mode == KBookmarkDialogPrivate::NewFolder) { KBookmarkGroup parent = d->parentBookmark(); if (d->title->text().isEmpty()) { d->title->setText(QStringLiteral("New Folder")); } d->bm = parent.createNewFolder(d->title->text()); d->bm.setDescription(d->comment->text()); d->mgr->emitChanged(parent); } else if (d->mode == KBookmarkDialogPrivate::NewBookmark) { KBookmarkGroup parent = d->parentBookmark(); if (d->title->text().isEmpty()) { d->title->setText(QStringLiteral("New Bookmark")); } d->bm = parent.addBookmark(d->title->text(), QUrl(d->url->text()), d->icon); d->bm.setDescription(d->comment->text()); d->mgr->emitChanged(parent); } else if (d->mode == KBookmarkDialogPrivate::NewMultipleBookmarks) { KBookmarkGroup parent = d->parentBookmark(); if (d->title->text().isEmpty()) { d->title->setText(QStringLiteral("New Folder")); } d->bm = parent.createNewFolder(d->title->text()); d->bm.setDescription(d->comment->text()); foreach (const KBookmarkOwner::FutureBookmark &fb, d->list) { d->bm.toGroup().addBookmark(fb.title(), fb.url(), fb.icon()); } d->mgr->emitChanged(parent); } else if (d->mode == KBookmarkDialogPrivate::EditBookmark) { d->bm.setFullText(d->title->text()); d->bm.setUrl(QUrl(d->url->text())); d->bm.setDescription(d->comment->text()); d->mgr->emitChanged(d->bm.parentGroup()); } else if (d->mode == d->SelectFolder) { d->bm = d->parentBookmark(); } QDialog::accept(); } KBookmark KBookmarkDialog::editBookmark(const KBookmark &bm) { if (!d->layout) { d->initLayoutPrivate(); } KGuiItem::assign(d->buttonBox->button(QDialogButtonBox::Ok), KGuiItem(tr("Update", "@action:button"))); setWindowTitle(tr("Bookmark Properties", "@title:window")); d->url->setVisible(!bm.isGroup()); d->urlLabel->setVisible(!bm.isGroup()); d->bm = bm; d->title->setText(bm.fullText()); d->url->setText(bm.url().toString()); d->comment->setVisible(true); d->commentLabel->setVisible(true); d->comment->setText(bm.description()); d->folderTree->setVisible(false); d->mode = KBookmarkDialogPrivate::EditBookmark; if (exec() == QDialog::Accepted) { return d->bm; } else { return KBookmark(); } } KBookmark KBookmarkDialog::addBookmark(const QString &title, const QUrl &url, const QString &icon, KBookmark parent) { if (!d->layout) { d->initLayoutPrivate(); } if (parent.isNull()) { parent = d->mgr->root(); } QPushButton *newButton = new QPushButton; KGuiItem::assign(newButton, KGuiItem(tr("&New Folder...", "@action:button"), QStringLiteral("folder-new"))); d->buttonBox->addButton(newButton, QDialogButtonBox::ActionRole); connect(newButton, &QAbstractButton::clicked, this, &KBookmarkDialog::newFolderButton); KGuiItem::assign(d->buttonBox->button(QDialogButtonBox::Ok), KGuiItem(tr("Add", "@action:button"), QStringLiteral("bookmark-new"))); setWindowTitle(tr("Add Bookmark", "@title:window")); d->url->setVisible(true); d->urlLabel->setVisible(true); d->title->setText(title); d->url->setText(url.toString()); d->comment->setText(QString()); d->comment->setVisible(true); d->commentLabel->setVisible(true); d->setParentBookmark(parent); d->folderTree->setVisible(true); d->icon = icon; d->mode = KBookmarkDialogPrivate::NewBookmark; if (exec() == QDialog::Accepted) { return d->bm; } else { return KBookmark(); } } KBookmarkGroup KBookmarkDialog::addBookmarks(const QList &list, const QString &name, KBookmarkGroup parent) { if (!d->layout) { d->initLayoutPrivate(); } if (parent.isNull()) { parent = d->mgr->root(); } d->list = list; QPushButton *newButton = new QPushButton; KGuiItem::assign(newButton, KGuiItem(tr("&New Folder...", "@action:button"), QStringLiteral("folder-new"))); d->buttonBox->addButton(newButton, QDialogButtonBox::ActionRole); connect(newButton, &QAbstractButton::clicked, this, &KBookmarkDialog::newFolderButton); KGuiItem::assign(d->buttonBox->button(QDialogButtonBox::Ok), KGuiItem(tr("Add", "@action:button"), QStringLiteral("bookmark-new"))); setWindowTitle(tr("Add Bookmarks", "@title:window")); d->url->setVisible(false); d->urlLabel->setVisible(false); d->title->setText(name); d->comment->setVisible(true); d->commentLabel->setVisible(true); d->comment->setText(QString()); d->setParentBookmark(parent); d->folderTree->setVisible(true); d->mode = KBookmarkDialogPrivate::NewMultipleBookmarks; if (exec() == QDialog::Accepted) { return d->bm.toGroup(); } else { return KBookmarkGroup(); } } KBookmarkGroup KBookmarkDialog::selectFolder(KBookmark parent) { if (!d->layout) { d->initLayoutPrivate(); } if (parent.isNull()) { parent = d->mgr->root(); } QPushButton *newButton = new QPushButton; KGuiItem::assign(newButton, KGuiItem(tr("&New Folder...", "@action:button"), QStringLiteral("folder-new"))); d->buttonBox->addButton(newButton, QDialogButtonBox::ActionRole); connect(newButton, &QAbstractButton::clicked, this, &KBookmarkDialog::newFolderButton); setWindowTitle(tr("Select Folder", "@title:window")); d->url->setVisible(false); d->urlLabel->setVisible(false); d->title->setVisible(false); d->titleLabel->setVisible(false); d->comment->setVisible(false); d->commentLabel->setVisible(false); d->setParentBookmark(parent); d->folderTree->setVisible(true); d->mode = d->SelectFolder; if (exec() == QDialog::Accepted) { return d->bm.toGroup(); } else { return KBookmarkGroup(); } } KBookmarkGroup KBookmarkDialog::createNewFolder(const QString &name, KBookmark parent) { if (!d->layout) { d->initLayoutPrivate(); } if (parent.isNull()) { parent = d->mgr->root(); } setWindowTitle(tr("New Folder", "@title:window")); d->url->setVisible(false); d->urlLabel->setVisible(false); d->comment->setVisible(true); d->commentLabel->setVisible(true); d->comment->setText(QString()); d->title->setText(name); d->setParentBookmark(parent); d->folderTree->setVisible(true); d->mode = KBookmarkDialogPrivate::NewFolder; if (exec() == QDialog::Accepted) { return d->bm.toGroup(); } else { return KBookmarkGroup(); } } KBookmarkDialog::KBookmarkDialog(KBookmarkManager *mgr, QWidget *parent) : QDialog(parent) , d(new KBookmarkDialogPrivate(this)) { d->mgr = mgr; } KBookmarkDialog::~KBookmarkDialog() { delete d; } void KBookmarkDialog::newFolderButton() { QString caption = d->parentBookmark().fullText().isEmpty() ? tr("Create New Bookmark Folder", "@title:window") : tr("Create New Bookmark Folder in %1", "@title:window").arg(d->parentBookmark().text()); bool ok; QString text = QInputDialog::getText(this, caption, tr("New folder:", "@label:textbox"), QLineEdit::Normal, QString(), &ok); if (!ok) { return; } KBookmarkGroup group = d->parentBookmark().createNewFolder(text); if (!group.isNull()) { KBookmarkGroup parentGroup = group.parentGroup(); d->mgr->emitChanged(parentGroup); d->folderTree->clear(); QTreeWidgetItem *root = new KBookmarkTreeItem(d->folderTree); d->fillGroup(root, d->mgr->root(), group); } } /********************************************************************/ KBookmarkTreeItem::KBookmarkTreeItem(QTreeWidget *tree) : QTreeWidgetItem(tree), m_address(QLatin1String("")) { setText(0, KBookmarkDialog::tr("Bookmarks", "name of the container of all browser bookmarks")); setIcon(0, SmallIcon(QStringLiteral("bookmarks"))); tree->expandItem(this); tree->setCurrentItem(this); tree->setItemSelected(this, true); } KBookmarkTreeItem::KBookmarkTreeItem(QTreeWidgetItem *parent, QTreeWidget *tree, const KBookmarkGroup &bk) : QTreeWidgetItem(parent) { setIcon(0, SmallIcon(bk.icon())); setText(0, bk.fullText()); tree->expandItem(this); m_address = bk.address(); } KBookmarkTreeItem::~KBookmarkTreeItem() { } QString KBookmarkTreeItem::address() { return m_address; } diff --git a/src/kbookmarkimporter.cpp b/src/kbookmarkimporter.cpp index 31b8249..5eb72b1 100644 --- a/src/kbookmarkimporter.cpp +++ b/src/kbookmarkimporter.cpp @@ -1,98 +1,98 @@ // -*- c-basic-offset:4; indent-tabs-mode:nil -*- /* This file is part of the KDE libraries Copyright (C) 2003 Alexander Kellett This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License version 2 as published by the Free Software Foundation. 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 "kbookmarkimporter.h" #include #include #include #include #include #include #include "kbookmarkmanager.h" #include "kbookmarkimporter_ns.h" #include "kbookmarkimporter_opera.h" #include "kbookmarkimporter_ie.h" void KXBELBookmarkImporterImpl::parse() { // qDebug() << "KXBELBookmarkImporterImpl::parse()"; KBookmarkManager *manager = KBookmarkManager::managerForFile(m_fileName, QString()); KBookmarkGroup root = manager->root(); traverse(root); // FIXME delete it! // delete manager; } void KXBELBookmarkImporterImpl::visit(const KBookmark &bk) { // qDebug() << "KXBELBookmarkImporterImpl::visit"; if (bk.isSeparator()) { emit newSeparator(); } else { emit newBookmark(bk.fullText(), bk.url().toString(), QLatin1String("")); } } void KXBELBookmarkImporterImpl::visitEnter(const KBookmarkGroup &grp) { // qDebug() << "KXBELBookmarkImporterImpl::visitEnter"; emit newFolder(grp.fullText(), false, QLatin1String("")); } void KXBELBookmarkImporterImpl::visitLeave(const KBookmarkGroup &) { // qDebug() << "KXBELBookmarkImporterImpl::visitLeave"; emit endFolder(); } void KBookmarkImporterBase::setupSignalForwards(QObject *src, QObject *dst) { connect(src, SIGNAL(newBookmark(QString,QString,QString)), dst, SIGNAL(newBookmark(QString,QString,QString))); connect(src, SIGNAL(newFolder(QString,bool,QString)), dst, SIGNAL(newFolder(QString,bool,QString))); connect(src, SIGNAL(newSeparator()), dst, SIGNAL(newSeparator())); connect(src, SIGNAL(endFolder()), dst, SIGNAL(endFolder())); } KBookmarkImporterBase *KBookmarkImporterBase::factory(const QString &type) { if (type == QLatin1String("netscape")) { return new KNSBookmarkImporterImpl; } else if (type == QLatin1String("mozilla")) { return new KMozillaBookmarkImporterImpl; } else if (type == QLatin1String("xbel")) { return new KXBELBookmarkImporterImpl; } else if (type == QLatin1String("ie")) { return new KIEBookmarkImporterImpl; } else if (type == QLatin1String("opera")) { return new KOperaBookmarkImporterImpl; } else { - return 0; + return nullptr; } } #include "moc_kbookmarkimporter.cpp" diff --git a/src/kbookmarkimporter_ie.cpp b/src/kbookmarkimporter_ie.cpp index d06fe9e..4ae55ce 100644 --- a/src/kbookmarkimporter_ie.cpp +++ b/src/kbookmarkimporter_ie.cpp @@ -1,223 +1,223 @@ // -*- c-basic-offset:4; indent-tabs-mode:nil -*- /* This file is part of the KDE libraries Copyright (C) 2002-2003 Alexander Kellett This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License version 2 as published by the Free Software Foundation. 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 "kbookmarkimporter_ie.h" #include #include #include #include #include #include #include "kbookmarkimporter.h" /** * A class for importing IE bookmarks * @deprecated */ class KIEBookmarkImporter : public QObject { Q_OBJECT public: KIEBookmarkImporter(const QString &fileName) : m_fileName(fileName) {} ~KIEBookmarkImporter() {} void parseIEBookmarks(); // Usual place for IE bookmarks static QString IEBookmarksDir(); Q_SIGNALS: void newBookmark(const QString &text, const QString &url, const QString &additionalInfo); void newFolder(const QString &text, bool open, const QString &additionalInfo); void newSeparator(); void endFolder(); protected: void parseIEBookmarks_dir(const QString &dirname, const QString &name = QString()); void parseIEBookmarks_url_file(const QString &filename, const QString &name); QString m_fileName; }; void KIEBookmarkImporter::parseIEBookmarks_url_file(const QString &filename, const QString &name) { static const int g_lineLimit = 16 * 1024; QFile f(filename); if (f.open(QIODevice::ReadOnly)) { QByteArray s(g_lineLimit, 0); while (f.readLine(s.data(), g_lineLimit) >= 0) { if (s[s.length() - 1] != '\n') { // Gosh, this line is longer than g_lineLimit. Skipping. qWarning() << "IE bookmarks contain a line longer than " << g_lineLimit << ". Skipping."; continue; } QByteArray t = s.trimmed(); QRegularExpression rx(QStringLiteral("URL=(.*)")); auto match = rx.match(t); if (match.hasMatch()) { emit newBookmark(name, match.captured(1), QLatin1String("")); } } f.close(); } } void KIEBookmarkImporter::parseIEBookmarks_dir(const QString &dirname, const QString &foldername) { QDir dir(dirname); dir.setFilter(QDir::Files | QDir::Dirs | QDir::AllDirs); dir.setSorting(QFlags(QDir::Name | QDir::DirsFirst)); dir.setNameFilters(QStringList(QStringLiteral("*.url"))); // AK - possibly add ";index.ini" ? const QFileInfoList list = dir.entryInfoList(); if (list.isEmpty()) { return; } if (dirname != m_fileName) { emit newFolder(foldername, false, QLatin1String("")); } foreach (const QFileInfo &fi, list) { if (fi.fileName() == QLatin1String(".") || fi.fileName() == QLatin1String("..")) { continue; } if (fi.isDir()) { parseIEBookmarks_dir(fi.absoluteFilePath(), fi.fileName()); } else if (fi.isFile()) { if (fi.fileName().endsWith(QLatin1String(".url"))) { QString name = fi.fileName(); name.truncate(name.length() - 4); // .url parseIEBookmarks_url_file(fi.absoluteFilePath(), name); } // AK - add index.ini } } if (dirname != m_fileName) { emit endFolder(); } } void KIEBookmarkImporter::parseIEBookmarks() { parseIEBookmarks_dir(m_fileName); } QString KIEBookmarkImporter::IEBookmarksDir() { - static KIEBookmarkImporterImpl *p = 0; + static KIEBookmarkImporterImpl *p = nullptr; if (!p) { p = new KIEBookmarkImporterImpl; } return p->findDefaultLocation(); } void KIEBookmarkImporterImpl::parse() { KIEBookmarkImporter importer(m_fileName); setupSignalForwards(&importer, this); importer.parseIEBookmarks(); } QString KIEBookmarkImporterImpl::findDefaultLocation(bool) const { // notify user that they must give a new dir such // as "Favourites" as otherwise it'll just place // lots of .url files in the given dir and gui // stuff in the exporter is ugly so that exclues // the possibility of just writing to Favourites // and checking if overwriting... return QFileDialog::getExistingDirectory(QApplication::activeWindow()); } ///////////////////////////////////////////////// class IEExporter : private KBookmarkGroupTraverser { public: IEExporter(const QString &); void write(const KBookmarkGroup &grp) { traverse(grp); } private: void visit(const KBookmark &) Q_DECL_OVERRIDE; void visitEnter(const KBookmarkGroup &) Q_DECL_OVERRIDE; void visitLeave(const KBookmarkGroup &) Q_DECL_OVERRIDE; private: QDir m_currentDir; }; static QString ieStyleQuote(const QString &str) { QString s(str); s.replace(QRegularExpression(QStringLiteral("[/\\:*?\"<>|]")), QStringLiteral("_")); return s; } IEExporter::IEExporter(const QString &dname) { m_currentDir.setPath(dname); } void IEExporter::visit(const KBookmark &bk) { QString fname = m_currentDir.path() + '/' + ieStyleQuote(bk.fullText()) + ".url"; // qDebug() << "visit(" << bk.text() << "), fname == " << fname; QFile file(fname); if (file.open(QIODevice::WriteOnly)) { QTextStream ts(&file); ts << "[InternetShortcut]\r\n"; ts << "URL=" << bk.url().toString().toUtf8() << "\r\n"; } } void IEExporter::visitEnter(const KBookmarkGroup &grp) { QString dname = m_currentDir.path() + '/' + ieStyleQuote(grp.fullText()); // qDebug() << "visitEnter(" << grp.text() << "), dname == " << dname; m_currentDir.mkdir(dname); m_currentDir.cd(dname); } void IEExporter::visitLeave(const KBookmarkGroup &) { // qDebug() << "visitLeave()"; m_currentDir.cdUp(); } void KIEBookmarkExporterImpl::write(const KBookmarkGroup &parent) { IEExporter exporter(m_fileName); exporter.write(parent); } #include "kbookmarkimporter_ie.moc" diff --git a/src/kbookmarkimporter_ns.cpp b/src/kbookmarkimporter_ns.cpp index 87cb513..029b8d0 100644 --- a/src/kbookmarkimporter_ns.cpp +++ b/src/kbookmarkimporter_ns.cpp @@ -1,203 +1,203 @@ // -*- c-basic-offset:4; indent-tabs-mode:nil -*- /* This file is part of the KDE libraries Copyright (C) 1996-1998 Martin R. Jones Copyright (C) 2000 David Faure Copyright (C) 2003 Alexander Kellett This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License version 2 as published by the Free Software Foundation. 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 "kbookmarkimporter_ns.h" #include "kbookmarkimporter.h" #include "kbookmarkexporter.h" #include "kbookmarkmanager.h" #include #include #include #include #include #include void KNSBookmarkImporterImpl::parse() { QFile f(m_fileName); QTextCodec *codec = m_utf8 ? QTextCodec::codecForName("UTF-8") : QTextCodec::codecForLocale(); Q_ASSERT(codec); if (!codec) { return; } if (f.open(QIODevice::ReadOnly)) { static const int g_lineLimit = 16 * 1024; QByteArray s(g_lineLimit, 0); // skip header while (f.readLine(s.data(), g_lineLimit) >= 1 && !s.contains("
")) { ; } while (int size = f.readLine(s.data(), g_lineLimit) >= 1) { if (size == g_lineLimit) { // Gosh, this line is longer than g_lineLimit. Skipping. qWarning() << "Netscape bookmarks contain a line longer than " << g_lineLimit << ". Skipping."; continue; } QByteArray t = s.trimmed(); if (t.left(4).toUpper() == "
") { emit newSeparator(); t = t.mid(4).trimmed(); if (t.isEmpty()) { continue; } } if (t.left(12).toUpper() == "

', secondQuotes + 1); int closeTag = t.indexOf('<', endTag + 1); QByteArray name = t.mid(endTag + 1, closeTag - endTag - 1); QString qname = KCharsets::resolveEntities(codec->toUnicode(name)); QByteArray additionalInfo = t.mid(secondQuotes + 1, endTag - secondQuotes - 1); emit newBookmark(qname, codec->toUnicode(link), QByteArray()); } } else if (t.left(7).toUpper() == "
', 7); QByteArray name = t.mid(endTag + 1); name = name.left(name.indexOf('<')); QString qname = KCharsets::resolveEntities(codec->toUnicode(name)); QByteArray additionalInfo = t.mid(8, endTag - 8); bool folded = (additionalInfo.left(6) == "FOLDED"); if (folded) { additionalInfo.remove(0, 7); } emit newFolder(qname, !folded, QByteArray()); } else if (t.left(8).toUpper() == "

") { emit endFolder(); } } f.close(); } } QString KNSBookmarkImporterImpl::findDefaultLocation(bool forSaving) const { if (m_utf8) { if (forSaving) return QFileDialog::getSaveFileName(QApplication::activeWindow(), QString(), QDir::homePath() + "/.mozilla", tr("HTML Files (*.html)")); else return QFileDialog::getOpenFileName(QApplication::activeWindow(), QString(), QDir::homePath() + "/.mozilla", tr("*.html|HTML Files (*.html)")); } else { return QDir::homePath() + "/.netscape/bookmarks.html"; } } //////////////////////////////////////////////////////////////// void KNSBookmarkExporterImpl::setUtf8(bool utf8) { m_utf8 = utf8; } void KNSBookmarkExporterImpl::write(const KBookmarkGroup &parent) { if (!QFile::exists(m_fileName)) { QString errorMsg = KNSBookmarkImporterImpl::tr("Could not find %1. Netscape is probably not installed. " "Aborting the export.").arg(m_fileName); - QMessageBox::critical(0, KNSBookmarkImporterImpl::tr("Netscape not found"), errorMsg); + QMessageBox::critical(nullptr, KNSBookmarkImporterImpl::tr("Netscape not found"), errorMsg); return; } if (QFile::exists(m_fileName)) { (void)QFile::rename(m_fileName, m_fileName + ".beforekde"); } QFile file(m_fileName); if (!file.open(QIODevice::WriteOnly)) { qCritical() << "Can't write to file " << m_fileName << endl; return; } QTextStream fstream(&file); fstream.setCodec(m_utf8 ? QTextCodec::codecForName("UTF-8") : QTextCodec::codecForLocale()); QString charset = m_utf8 ? QStringLiteral("UTF-8") : QString::fromLatin1(QTextCodec::codecForLocale()->name()).toUpper(); fstream << "" << endl << KNSBookmarkImporterImpl::tr("") << endl << "" << endl << "" << KNSBookmarkImporterImpl::tr("Bookmarks") << "" << endl << "

" << KNSBookmarkImporterImpl::tr("Bookmarks") << "

" << endl << "

" << endl << folderAsString(parent) << "

" << endl; } QString KNSBookmarkExporterImpl::folderAsString(const KBookmarkGroup &parent) const { QString str; QTextStream fstream(&str, QIODevice::WriteOnly); for (KBookmark bk = parent.first(); !bk.isNull(); bk = parent.next(bk)) { if (bk.isSeparator()) { fstream << "


" << endl; continue; } QString text = bk.fullText().toHtmlEscaped(); if (bk.isGroup()) { fstream << "

" << text << "

" << endl << "

" << endl << folderAsString(bk.toGroup()) << "

" << endl; continue; } else { // note - netscape seems to use local8bit for url... fstream << "

" << text << "" << endl; continue; } } return str; } //// diff --git a/src/kbookmarkimporter_opera.cpp b/src/kbookmarkimporter_opera.cpp index d1c2768..3dcb3de 100644 --- a/src/kbookmarkimporter_opera.cpp +++ b/src/kbookmarkimporter_opera.cpp @@ -1,189 +1,189 @@ // -*- c-basic-offset:4; indent-tabs-mode:nil -*- /* This file is part of the KDE libraries Copyright (C) 2002-2003 Alexander Kellett This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License version 2 as published by the Free Software Foundation. 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 "kbookmarkimporter_opera.h" #include "kbookmarkimporter_opera_p.h" #include #include #include #include #include #include "kbookmarkimporter.h" void KOperaBookmarkImporter::parseOperaBookmarks() { QFile file(m_fileName); if (!file.open(QIODevice::ReadOnly)) { return; } QTextCodec *codec = QTextCodec::codecForName("UTF-8"); Q_ASSERT(codec); if (!codec) { return; } QString url, name, type; int lineno = 0; QTextStream stream(&file); stream.setCodec(codec); while (! stream.atEnd()) { lineno++; QString line = stream.readLine().trimmed(); // first two headers lines contain details about the format if (lineno <= 2) { if (line.toLower().startsWith(QLatin1String("options:"))) { foreach (const QString &ba, line.mid(8).split(',')) { const int pos = ba.indexOf('='); if (pos < 1) { continue; } const QString key = ba.left(pos).trimmed().toLower(); const QString value = ba.mid(pos + 1).trimmed(); } } continue; } // at least up till version<=3 the following is valid if (line.isEmpty()) { // end of data block if (type.isNull()) { continue; } else if (type == QLatin1String("URL")) { emit newBookmark(name, url, QLatin1String("")); } else if (type == QLatin1String("FOLDER")) { emit newFolder(name, false, QLatin1String("")); } type.clear(); name.clear(); url.clear(); } else if (line == QLatin1String("-")) { // end of folder emit endFolder(); } else { // data block line QString tag; if (tag = '#', line.startsWith(tag)) { type = line.remove(0, tag.length()); } else if (tag = QStringLiteral("NAME="), line.startsWith(tag)) { name = line.remove(0, tag.length()); } else if (tag = QStringLiteral("URL="), line.startsWith(tag)) { url = line.remove(0, tag.length()); } } } } QString KOperaBookmarkImporter::operaBookmarksFile() { - static KOperaBookmarkImporterImpl *p = 0; + static KOperaBookmarkImporterImpl *p = nullptr; if (!p) { p = new KOperaBookmarkImporterImpl; } return p->findDefaultLocation(); } void KOperaBookmarkImporterImpl::parse() { KOperaBookmarkImporter importer(m_fileName); setupSignalForwards(&importer, this); importer.parseOperaBookmarks(); } QString KOperaBookmarkImporterImpl::findDefaultLocation(bool saving) const { return saving ? QFileDialog::getSaveFileName(QApplication::activeWindow(), QString(), QDir::homePath() + "/.opera", tr("Opera Bookmark Files (*.adr)")) : QFileDialog::getOpenFileName(QApplication::activeWindow(), QString(), QDir::homePath() + "/.opera", tr("*.adr|Opera Bookmark Files (*.adr)")); } ///////////////////////////////////////////////// class OperaExporter : private KBookmarkGroupTraverser { public: OperaExporter(); QString generate(const KBookmarkGroup &grp) { traverse(grp); return m_string; } private: void visit(const KBookmark &) Q_DECL_OVERRIDE; void visitEnter(const KBookmarkGroup &) Q_DECL_OVERRIDE; void visitLeave(const KBookmarkGroup &) Q_DECL_OVERRIDE; private: QString m_string; QTextStream m_out; }; OperaExporter::OperaExporter() : m_out(&m_string, QIODevice::WriteOnly) { m_out << "Opera Hotlist version 2.0" << endl; m_out << "Options: encoding = utf8, version=3" << endl; } void OperaExporter::visit(const KBookmark &bk) { // qDebug() << "visit(" << bk.text() << ")"; m_out << "#URL" << endl; m_out << "\tNAME=" << bk.fullText() << endl; m_out << "\tURL=" << bk.url().toString().toUtf8() << endl; m_out << endl; } void OperaExporter::visitEnter(const KBookmarkGroup &grp) { // qDebug() << "visitEnter(" << grp.text() << ")"; m_out << "#FOLDER" << endl; m_out << "\tNAME=" << grp.fullText() << endl; m_out << endl; } void OperaExporter::visitLeave(const KBookmarkGroup &) { // qDebug() << "visitLeave()"; m_out << "-" << endl; m_out << endl; } void KOperaBookmarkExporterImpl::write(const KBookmarkGroup &parent) { OperaExporter exporter; QString content = exporter.generate(parent); QFile file(m_fileName); if (!file.open(QIODevice::WriteOnly)) { qCritical() << "Can't write to file " << m_fileName << endl; return; } QTextStream fstream(&file); fstream.setCodec(QTextCodec::codecForName("UTF-8")); fstream << content; } #include "moc_kbookmarkimporter_opera_p.cpp" diff --git a/src/kbookmarkmanager.cpp b/src/kbookmarkmanager.cpp index 7905e8d..adda249 100644 --- a/src/kbookmarkmanager.cpp +++ b/src/kbookmarkmanager.cpp @@ -1,729 +1,729 @@ // -*- c-basic-offset:4; indent-tabs-mode:nil -*- /* This file is part of the KDE libraries Copyright (C) 2000 David Faure Copyright (C) 2003 Alexander Kellett Copyright (C) 2008 Norbert Frese This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License version 2 as published by the Free Software Foundation. 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 "kbookmarkmanager.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "kbookmarkmenu.h" #include "kbookmarkmenu_p.h" #include "kbookmarkimporter.h" #include "kbookmarkdialog.h" #include "kbookmarkmanageradaptor_p.h" #define BOOKMARK_CHANGE_NOTIFY_INTERFACE "org.kde.KIO.KBookmarkManager" class KBookmarkManagerList : public QList { public: KBookmarkManagerList(); ~KBookmarkManagerList() { cleanup(); } void cleanup() { QList copy = *this; qDeleteAll(copy); // auto-delete functionality clear(); } QReadWriteLock lock; }; Q_GLOBAL_STATIC(KBookmarkManagerList, s_pSelf) static void deleteManagers() { if (s_pSelf.exists()) { s_pSelf->cleanup(); } } KBookmarkManagerList::KBookmarkManagerList() { // Delete the KBookmarkManagers while qApp exists, since we interact with the DBus thread qAddPostRoutine(deleteManagers); } class KBookmarkMap : private KBookmarkGroupTraverser { public: KBookmarkMap() : m_mapNeedsUpdate(true) {} void setNeedsUpdate() { m_mapNeedsUpdate = true; } void update(KBookmarkManager *); QList find(const QString &url) const { return m_bk_map.value(url); } private: void visit(const KBookmark &) Q_DECL_OVERRIDE; void visitEnter(const KBookmarkGroup &) Q_DECL_OVERRIDE { ; } void visitLeave(const KBookmarkGroup &) Q_DECL_OVERRIDE { ; } private: typedef QList KBookmarkList; QMap m_bk_map; bool m_mapNeedsUpdate; }; void KBookmarkMap::update(KBookmarkManager *manager) { if (m_mapNeedsUpdate) { m_mapNeedsUpdate = false; m_bk_map.clear(); KBookmarkGroup root = manager->root(); traverse(root); } } void KBookmarkMap::visit(const KBookmark &bk) { if (!bk.isSeparator()) { // add bookmark to url map m_bk_map[bk.internalElement().attribute(QStringLiteral("href"))].append(bk); } } // ######################### // KBookmarkManagerPrivate class KBookmarkManagerPrivate { public: KBookmarkManagerPrivate(bool bDocIsloaded, const QString &dbusObjectName = QString()) : m_doc(QStringLiteral("xbel")) , m_dbusObjectName(dbusObjectName) , m_docIsLoaded(bDocIsloaded) , m_update(false) , m_dialogAllowed(true) - , m_dialogParent(0) + , m_dialogParent(nullptr) , m_browserEditor(false) , m_typeExternal(false) - , m_dirWatch(0) + , m_dirWatch(nullptr) {} ~KBookmarkManagerPrivate() { delete m_dirWatch; } mutable QDomDocument m_doc; mutable QDomDocument m_toolbarDoc; QString m_bookmarksFile; QString m_dbusObjectName; mutable bool m_docIsLoaded; bool m_update; bool m_dialogAllowed; QWidget *m_dialogParent; bool m_browserEditor; QString m_editorCaption; bool m_typeExternal; KDirWatch *m_dirWatch; // for external bookmark files KBookmarkMap m_map; }; // ################ // KBookmarkManager static KBookmarkManager *lookupExisting(const QString &bookmarksFile) { for (KBookmarkManagerList::ConstIterator bmit = s_pSelf()->constBegin(), bmend = s_pSelf()->constEnd(); bmit != bmend; ++bmit) { if ((*bmit)->path() == bookmarksFile) { return *bmit; } } - return 0; + return nullptr; } KBookmarkManager *KBookmarkManager::managerForFile(const QString &bookmarksFile, const QString &dbusObjectName) { - KBookmarkManager *mgr(0); + KBookmarkManager *mgr(nullptr); { QReadLocker readLock(&s_pSelf()->lock); mgr = lookupExisting(bookmarksFile); if (mgr) { return mgr; } } QWriteLocker writeLock(&s_pSelf()->lock); mgr = lookupExisting(bookmarksFile); if (mgr) { return mgr; } mgr = new KBookmarkManager(bookmarksFile, dbusObjectName); s_pSelf()->append(mgr); return mgr; } KBookmarkManager *KBookmarkManager::managerForExternalFile(const QString &bookmarksFile) { - KBookmarkManager *mgr(0); + KBookmarkManager *mgr(nullptr); { QReadLocker readLock(&s_pSelf()->lock); mgr = lookupExisting(bookmarksFile); if (mgr) { return mgr; } } QWriteLocker writeLock(&s_pSelf()->lock); mgr = lookupExisting(bookmarksFile); if (mgr) { return mgr; } mgr = new KBookmarkManager(bookmarksFile); s_pSelf()->append(mgr); return mgr; } // principally used for filtered toolbars KBookmarkManager *KBookmarkManager::createTempManager() { KBookmarkManager *mgr = new KBookmarkManager(); s_pSelf()->append(mgr); return mgr; } #define PI_DATA "version=\"1.0\" encoding=\"UTF-8\"" static QDomElement createXbelTopLevelElement(QDomDocument &doc) { QDomElement topLevel = doc.createElement(QStringLiteral("xbel")); topLevel.setAttribute(QStringLiteral("xmlns:mime"), QStringLiteral("http://www.freedesktop.org/standards/shared-mime-info")); topLevel.setAttribute(QStringLiteral("xmlns:bookmark"), QStringLiteral("http://www.freedesktop.org/standards/desktop-bookmarks")); topLevel.setAttribute(QStringLiteral("xmlns:kdepriv"), QStringLiteral("http://www.kde.org/kdepriv")); doc.appendChild(topLevel); doc.insertBefore(doc.createProcessingInstruction(QStringLiteral("xml"), PI_DATA), topLevel); return topLevel; } KBookmarkManager::KBookmarkManager(const QString &bookmarksFile, const QString &dbusObjectName) : d(new KBookmarkManagerPrivate(false, dbusObjectName)) { if (dbusObjectName.isNull()) // get dbusObjectName from file if (QFile::exists(d->m_bookmarksFile)) { parse(); //sets d->m_dbusObjectName } init("/KBookmarkManager/" + d->m_dbusObjectName); d->m_update = true; Q_ASSERT(!bookmarksFile.isEmpty()); d->m_bookmarksFile = bookmarksFile; if (!QFile::exists(d->m_bookmarksFile)) { QDomElement topLevel = createXbelTopLevelElement(d->m_doc); topLevel.setAttribute(QStringLiteral("dbusName"), dbusObjectName); d->m_docIsLoaded = true; } } KBookmarkManager::KBookmarkManager(const QString &bookmarksFile) : d(new KBookmarkManagerPrivate(false)) { // use QFileSystemWatcher to monitor this bookmarks file d->m_typeExternal = true; d->m_update = true; Q_ASSERT(!bookmarksFile.isEmpty()); d->m_bookmarksFile = bookmarksFile; if (!QFile::exists(d->m_bookmarksFile)) { createXbelTopLevelElement(d->m_doc); } else { parse(); } d->m_docIsLoaded = true; // start KDirWatch d->m_dirWatch = new KDirWatch; d->m_dirWatch->addFile(d->m_bookmarksFile); QObject::connect(d->m_dirWatch, &KDirWatch::dirty, this, &KBookmarkManager::slotFileChanged); QObject::connect(d->m_dirWatch, &KDirWatch::created, this, &KBookmarkManager::slotFileChanged); QObject::connect(d->m_dirWatch, &KDirWatch::deleted, this, &KBookmarkManager::slotFileChanged); // qDebug() << "starting KDirWatch for" << d->m_bookmarksFile; } KBookmarkManager::KBookmarkManager() : d(new KBookmarkManagerPrivate(true)) { init(QStringLiteral("/KBookmarkManager/generated")); d->m_update = false; // TODO - make it read/write createXbelTopLevelElement(d->m_doc); } void KBookmarkManager::init(const QString &dbusPath) { // A KBookmarkManager without a dbus name is a temporary one, like those used by importers; // no need to register them to dbus if (dbusPath != QLatin1String("/KBookmarkManager/") && dbusPath != QLatin1String("/KBookmarkManager/generated")) { new KBookmarkManagerAdaptor(this); QDBusConnection::sessionBus().registerObject(dbusPath, this); QDBusConnection::sessionBus().connect(QString(), dbusPath, BOOKMARK_CHANGE_NOTIFY_INTERFACE, QStringLiteral("bookmarksChanged"), this, SLOT(notifyChanged(QString,QDBusMessage))); QDBusConnection::sessionBus().connect(QString(), dbusPath, BOOKMARK_CHANGE_NOTIFY_INTERFACE, QStringLiteral("bookmarkConfigChanged"), this, SLOT(notifyConfigChanged())); } } void KBookmarkManager::slotFileChanged(const QString &path) { if (path == d->m_bookmarksFile) { // qDebug() << "file changed (KDirWatch) " << path ; // Reparse parse(); // Tell our GUI // (emit where group is "" to directly mark the root menu as dirty) emit changed(QLatin1String(""), QString()); } } KBookmarkManager::~KBookmarkManager() { if (!s_pSelf.isDestroyed()) { s_pSelf()->removeAll(this); } delete d; } bool KBookmarkManager::autoErrorHandlingEnabled() const { return d->m_dialogAllowed; } void KBookmarkManager::setAutoErrorHandlingEnabled(bool enable, QWidget *parent) { d->m_dialogAllowed = enable; d->m_dialogParent = parent; } void KBookmarkManager::setUpdate(bool update) { d->m_update = update; } QDomDocument KBookmarkManager::internalDocument() const { if (!d->m_docIsLoaded) { parse(); d->m_toolbarDoc.clear(); } return d->m_doc; } void KBookmarkManager::parse() const { d->m_docIsLoaded = true; // qDebug() << "KBookmarkManager::parse " << d->m_bookmarksFile; QFile file(d->m_bookmarksFile); if (!file.open(QIODevice::ReadOnly)) { qWarning() << "Can't open " << d->m_bookmarksFile; return; } d->m_doc = QDomDocument(QStringLiteral("xbel")); d->m_doc.setContent(&file); if (d->m_doc.documentElement().isNull()) { qWarning() << "KBookmarkManager::parse : main tag is missing, creating default " << d->m_bookmarksFile; QDomElement element = d->m_doc.createElement(QStringLiteral("xbel")); d->m_doc.appendChild(element); } QDomElement docElem = d->m_doc.documentElement(); QString mainTag = docElem.tagName(); if (mainTag != QLatin1String("xbel")) { qWarning() << "KBookmarkManager::parse : unknown main tag " << mainTag; } if (d->m_dbusObjectName.isNull()) { d->m_dbusObjectName = docElem.attribute(QStringLiteral("dbusName")); } else if (docElem.attribute(QStringLiteral("dbusName")) != d->m_dbusObjectName) { docElem.setAttribute(QStringLiteral("dbusName"), d->m_dbusObjectName); save(); } QDomNode n = d->m_doc.documentElement().previousSibling(); if (n.isProcessingInstruction()) { QDomProcessingInstruction pi = n.toProcessingInstruction(); pi.parentNode().removeChild(pi); } QDomProcessingInstruction pi; pi = d->m_doc.createProcessingInstruction(QStringLiteral("xml"), PI_DATA); d->m_doc.insertBefore(pi, docElem); file.close(); d->m_map.setNeedsUpdate(); } bool KBookmarkManager::save(bool toolbarCache) const { return saveAs(d->m_bookmarksFile, toolbarCache); } bool KBookmarkManager::saveAs(const QString &filename, bool toolbarCache) const { // qDebug() << "KBookmarkManager::save " << filename; // Save the bookmark toolbar folder for quick loading // but only when it will actually make things quicker const QString cacheFilename = filename + QLatin1String(".tbcache"); if (toolbarCache && !root().isToolbarGroup()) { QSaveFile cacheFile(cacheFilename); if (cacheFile.open(QIODevice::WriteOnly)) { QString str; QTextStream stream(&str, QIODevice::WriteOnly); stream << root().findToolbar(); const QByteArray cstr = str.toUtf8(); cacheFile.write(cstr.data(), cstr.length()); cacheFile.commit(); } } else { // remove any (now) stale cache QFile::remove(cacheFilename); } // Create parent dirs QFileInfo info(filename); QDir().mkpath(info.absolutePath()); QSaveFile file(filename); if (file.open(QIODevice::WriteOnly)) { KBackup::simpleBackupFile(file.fileName(), QString(), QStringLiteral(".bak")); QTextStream stream(&file); stream.setCodec(QTextCodec::codecForName("UTF-8")); stream << internalDocument().toString(); stream.flush(); if (file.commit()) { return true; } } static int hadSaveError = false; if (!hadSaveError) { QString err = tr("Unable to save bookmarks in %1. Reported error was: %2. " "This error message will only be shown once. The cause " "of the error needs to be fixed as quickly as possible, " "which is most likely a full hard drive.").arg(filename).arg(file.errorString()); if (d->m_dialogAllowed && qobject_cast(qApp) && QThread::currentThread() == qApp->thread()) { QMessageBox::critical(QApplication::activeWindow(), QApplication::applicationName(), err); } qCritical() << QStringLiteral("Unable to save bookmarks in %1. File reported the following error-code: %2.").arg(filename).arg(file.error()); emit const_cast(this)->error(err); } hadSaveError = true; return false; } QString KBookmarkManager::path() const { return d->m_bookmarksFile; } KBookmarkGroup KBookmarkManager::root() const { return KBookmarkGroup(internalDocument().documentElement()); } KBookmarkGroup KBookmarkManager::toolbar() { // qDebug() << "KBookmarkManager::toolbar begin"; // Only try to read from a toolbar cache if the full document isn't loaded if (!d->m_docIsLoaded) { // qDebug() << "KBookmarkManager::toolbar trying cache"; const QString cacheFilename = d->m_bookmarksFile + QLatin1String(".tbcache"); QFileInfo bmInfo(d->m_bookmarksFile); QFileInfo cacheInfo(cacheFilename); if (d->m_toolbarDoc.isNull() && QFile::exists(cacheFilename) && bmInfo.lastModified() < cacheInfo.lastModified()) { // qDebug() << "KBookmarkManager::toolbar reading file"; QFile file(cacheFilename); if (file.open(QIODevice::ReadOnly)) { d->m_toolbarDoc = QDomDocument(QStringLiteral("cache")); d->m_toolbarDoc.setContent(&file); // qDebug() << "KBookmarkManager::toolbar opened"; } } if (!d->m_toolbarDoc.isNull()) { // qDebug() << "KBookmarkManager::toolbar returning element"; QDomElement elem = d->m_toolbarDoc.firstChild().toElement(); return KBookmarkGroup(elem); } } // Fallback to the normal way if there is no cache or if the bookmark file // is already loaded QDomElement elem = root().findToolbar(); if (elem.isNull()) { // Root is the bookmark toolbar if none has been set. // Make it explicit to speed up invocations of findToolbar() root().internalElement().setAttribute(QStringLiteral("toolbar"), QStringLiteral("yes")); return root(); } else { return KBookmarkGroup(elem); } } KBookmark KBookmarkManager::findByAddress(const QString &address) { // qDebug() << "KBookmarkManager::findByAddress " << address; KBookmark result = root(); // The address is something like /5/10/2+ const QStringList addresses = address.split(QRegularExpression(QStringLiteral("[/+]")), QString::SkipEmptyParts); // qWarning() << addresses.join(","); for (QStringList::const_iterator it = addresses.begin(); it != addresses.end();) { bool append = ((*it) == QLatin1String("+")); uint number = (*it).toUInt(); Q_ASSERT(result.isGroup()); KBookmarkGroup group = result.toGroup(); KBookmark bk = group.first(), lbk = bk; // last non-null bookmark for (uint i = 0; ((i < number) || append) && !bk.isNull(); ++i) { lbk = bk; bk = group.next(bk); // qWarning() << i; } it++; // qWarning() << "found section"; result = bk; } if (result.isNull()) { qWarning() << "KBookmarkManager::findByAddress: couldn't find item " << address; } // qWarning() << "found " << result.address(); return result; } void KBookmarkManager::emitChanged() { emitChanged(root()); } void KBookmarkManager::emitChanged(const KBookmarkGroup &group) { (void) save(); // KDE5 TODO: emitChanged should return a bool? Maybe rename it to saveAndEmitChanged? // Tell the other processes too // qDebug() << "KBookmarkManager::emitChanged : broadcasting change " << group.address(); emit bookmarksChanged(group.address()); // We do get our own broadcast, so no need for this anymore //emit changed( group ); } void KBookmarkManager::emitConfigChanged() { emit bookmarkConfigChanged(); } void KBookmarkManager::notifyCompleteChange(const QString &caller) // DBUS call { if (!d->m_update) { return; } // qDebug() << "KBookmarkManager::notifyCompleteChange"; // The bk editor tells us we should reload everything // Reparse parse(); // Tell our GUI // (emit where group is "" to directly mark the root menu as dirty) emit changed(QLatin1String(""), caller); } void KBookmarkManager::notifyConfigChanged() // DBUS call { // qDebug() << "reloaded bookmark config!"; KBookmarkSettings::self()->readSettings(); parse(); // reload, and thusly recreate the menus emit configChanged(); } void KBookmarkManager::notifyChanged(const QString &groupAddress, const QDBusMessage &msg) // DBUS call { // qDebug() << "KBookmarkManager::notifyChanged ( "<m_update) { return; } // Reparse (the whole file, no other choice) // if someone else notified us if (msg.service() != QDBusConnection::sessionBus().baseService()) { parse(); } // qDebug() << "KBookmarkManager::notifyChanged " << groupAddress; //KBookmarkGroup group = findByAddress( groupAddress ).toGroup(); //Q_ASSERT(!group.isNull()); emit changed(groupAddress, QString()); } void KBookmarkManager::setEditorOptions(const QString &caption, bool browser) { d->m_editorCaption = caption; d->m_browserEditor = browser; } void KBookmarkManager::slotEditBookmarks() { QStringList args; if (!d->m_editorCaption.isEmpty()) { args << QStringLiteral("--customcaption") << d->m_editorCaption; } if (!d->m_browserEditor) { args << QStringLiteral("--nobrowser"); } if (!d->m_dbusObjectName.isEmpty()) { args << QStringLiteral("--dbusObjectName") << d->m_dbusObjectName; } args << d->m_bookmarksFile; QProcess::startDetached(QStringLiteral("keditbookmarks"), args); } void KBookmarkManager::slotEditBookmarksAtAddress(const QString &address) { QStringList args; if (!d->m_editorCaption.isEmpty()) { args << QStringLiteral("--customcaption") << d->m_editorCaption; } if (!d->m_browserEditor) { args << QStringLiteral("--nobrowser"); } if (!d->m_dbusObjectName.isEmpty()) { args << QStringLiteral("--dbusObjectName") << d->m_dbusObjectName; } args << QStringLiteral("--address") << address << d->m_bookmarksFile; QProcess::startDetached(QStringLiteral("keditbookmarks"), args); } /////// bool KBookmarkManager::updateAccessMetadata(const QString &url) { d->m_map.update(this); QList list = d->m_map.find(url); if (list.count() == 0) { return false; } for (QList::iterator it = list.begin(); it != list.end(); ++it) { (*it).updateAccessMetadata(); } return true; } void KBookmarkManager::updateFavicon(const QString &url, const QString &/*faviconurl*/) { d->m_map.update(this); QList list = d->m_map.find(url); for (QList::iterator it = list.begin(); it != list.end(); ++it) { // TODO - update favicon data based on faviconurl // but only when the previously used icon // isn't a manually set one. } } KBookmarkManager *KBookmarkManager::userBookmarksManager() { const QString bookmarksFile = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1Char('/') + QLatin1String("konqueror/bookmarks.xml"); KBookmarkManager *bookmarkManager = KBookmarkManager::managerForFile(bookmarksFile, QStringLiteral("konqueror")); QString caption = QGuiApplication::applicationDisplayName(); if (caption.isEmpty()) { caption = QCoreApplication::applicationName(); } bookmarkManager->setEditorOptions(caption, true); return bookmarkManager; } -KBookmarkSettings *KBookmarkSettings::s_self = 0; +KBookmarkSettings *KBookmarkSettings::s_self = nullptr; void KBookmarkSettings::readSettings() { KConfig config(QStringLiteral("kbookmarkrc"), KConfig::NoGlobals); KConfigGroup cg(&config, "Bookmarks"); // add bookmark dialog usage - no reparse s_self->m_advancedaddbookmark = cg.readEntry("AdvancedAddBookmarkDialog", false); // this one alters the menu, therefore it needs a reparse s_self->m_contextmenu = cg.readEntry("ContextMenuActions", true); } KBookmarkSettings *KBookmarkSettings::self() { if (!s_self) { s_self = new KBookmarkSettings; readSettings(); } return s_self; } #include "moc_kbookmarkmanager.cpp" diff --git a/src/kbookmarkmenu.cpp b/src/kbookmarkmenu.cpp index 95c51ea..9f8516a 100644 --- a/src/kbookmarkmenu.cpp +++ b/src/kbookmarkmenu.cpp @@ -1,530 +1,530 @@ /* This file is part of the KDE project Copyright (C) 1998, 1999 Torben Weis Copyright (C) 2006 Daniel Teske 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 "kbookmarkmenu_p.h" #include "kbookmarkaction.h" #include "kbookmarkactionmenu.h" #include "kbookmarkcontextmenu.h" #include "kbookmarkdialog.h" #include "kbookmarkowner.h" #include #include #include #include #include #include #include #include #include /********************************************************************/ /********************************************************************/ /********************************************************************/ class KBookmarkMenuPrivate { public: KBookmarkMenuPrivate() - : newBookmarkFolder(0), - addAddBookmark(0), - bookmarksToFolder(0) + : newBookmarkFolder(nullptr), + addAddBookmark(nullptr), + bookmarksToFolder(nullptr) { } QAction *newBookmarkFolder; QAction *addAddBookmark; QAction *bookmarksToFolder; }; KBookmarkMenu::KBookmarkMenu(KBookmarkManager *mgr, KBookmarkOwner *_owner, QMenu *_parentMenu, KActionCollection *actionCollection) : QObject(), m_actionCollection(actionCollection), d(new KBookmarkMenuPrivate()), m_bIsRoot(true), m_pManager(mgr), m_pOwner(_owner), m_parentMenu(_parentMenu), m_parentAddress(QLatin1String("")) //TODO KBookmarkAdress::root { // TODO KDE5 find a QMenu equvalnet for this one //m_parentMenu->setKeyboardShortcutsEnabled( true ); // qDebug() << "KBookmarkMenu::KBookmarkMenu " << this << " address : " << m_parentAddress; connect(_parentMenu, &QMenu::aboutToShow, this, &KBookmarkMenu::slotAboutToShow); if (KBookmarkSettings::self()->m_contextmenu) { m_parentMenu->setContextMenuPolicy(Qt::CustomContextMenu); connect(m_parentMenu, &QWidget::customContextMenuRequested, this, &KBookmarkMenu::slotCustomContextMenu); } connect(m_pManager, &KBookmarkManager::changed, this, &KBookmarkMenu::slotBookmarksChanged); m_bDirty = true; addActions(); } void KBookmarkMenu::addActions() { if (m_bIsRoot) { addAddBookmark(); addAddBookmarksList(); addNewFolder(); addEditBookmarks(); } else { if (!m_parentMenu->actions().isEmpty()) { m_parentMenu->addSeparator(); } addOpenInTabs(); addAddBookmark(); addAddBookmarksList(); addNewFolder(); } } KBookmarkMenu::KBookmarkMenu(KBookmarkManager *mgr, KBookmarkOwner *_owner, QMenu *_parentMenu, const QString &parentAddress) : QObject(), m_actionCollection(new KActionCollection(this)), d(new KBookmarkMenuPrivate()), m_bIsRoot(false), m_pManager(mgr), m_pOwner(_owner), m_parentMenu(_parentMenu), m_parentAddress(parentAddress) { // TODO KDE5 find a QMenu equvalnet for this one //m_parentMenu->setKeyboardShortcutsEnabled( true ); connect(_parentMenu, &QMenu::aboutToShow, this, &KBookmarkMenu::slotAboutToShow); if (KBookmarkSettings::self()->m_contextmenu) { m_parentMenu->setContextMenuPolicy(Qt::CustomContextMenu); connect(m_parentMenu, &QWidget::customContextMenuRequested, this, &KBookmarkMenu::slotCustomContextMenu); } m_bDirty = true; } KBookmarkMenu::~KBookmarkMenu() { qDeleteAll(m_lstSubMenus); qDeleteAll(m_actions); delete d; } void KBookmarkMenu::ensureUpToDate() { slotAboutToShow(); } void KBookmarkMenu::slotAboutToShow() { // Did the bookmarks change since the last time we showed them ? if (m_bDirty) { m_bDirty = false; clear(); refill(); m_parentMenu->adjustSize(); } } void KBookmarkMenu::slotCustomContextMenu(const QPoint &pos) { QAction *action = m_parentMenu->actionAt(pos); QMenu *menu = contextMenu(action); if (!menu) { return; } menu->setAttribute(Qt::WA_DeleteOnClose); menu->popup(m_parentMenu->mapToGlobal(pos)); } QMenu *KBookmarkMenu::contextMenu(QAction *action) { KBookmarkActionInterface *act = dynamic_cast(action); if (!act) { - return 0; + return nullptr; } return new KBookmarkContextMenu(act->bookmark(), m_pManager, m_pOwner); } bool KBookmarkMenu::isRoot() const { return m_bIsRoot; } bool KBookmarkMenu::isDirty() const { return m_bDirty; } QString KBookmarkMenu::parentAddress() const { return m_parentAddress; } KBookmarkManager *KBookmarkMenu::manager() const { return m_pManager; } KBookmarkOwner *KBookmarkMenu::owner() const { return m_pOwner; } QMenu *KBookmarkMenu::parentMenu() const { return m_parentMenu; } /********************************************************************/ /********************************************************************/ /********************************************************************/ /********************************************************************/ /********************************************************************/ /********************************************************************/ void KBookmarkMenu::slotBookmarksChanged(const QString &groupAddress) { qDebug() << "KBookmarkMenu::slotBookmarksChanged groupAddress: " << groupAddress; if (groupAddress == m_parentAddress) { //qDebug() << "KBookmarkMenu::slotBookmarksChanged -> setting m_bDirty on " << groupAddress; m_bDirty = true; } else { // Iterate recursively into child menus for (QList::iterator it = m_lstSubMenus.begin(), end = m_lstSubMenus.end(); it != end; ++it) { (*it)->slotBookmarksChanged(groupAddress); } } } void KBookmarkMenu::clear() { qDeleteAll(m_lstSubMenus); m_lstSubMenus.clear(); for (QList::iterator it = m_actions.begin(), end = m_actions.end(); it != end; ++it) { m_parentMenu->removeAction(*it); delete *it; } m_parentMenu->clear(); m_actions.clear(); } void KBookmarkMenu::refill() { //qDebug() << "KBookmarkMenu::refill()"; if (m_bIsRoot) { addActions(); } fillBookmarks(); if (!m_bIsRoot) { addActions(); } } void KBookmarkMenu::addOpenInTabs() { if (!m_pOwner || !m_pOwner->supportsTabs() || !KAuthorized::authorizeKAction(QStringLiteral("bookmarks"))) { return; } QString title = tr("Open Folder in Tabs"); QAction *paOpenFolderInTabs = new QAction(title, this); paOpenFolderInTabs->setIcon(QIcon::fromTheme(QStringLiteral("tab-new"))); paOpenFolderInTabs->setToolTip(tr("Open all bookmarks in this folder as a new tab.")); paOpenFolderInTabs->setStatusTip(paOpenFolderInTabs->toolTip()); connect(paOpenFolderInTabs, &QAction::triggered, this, &KBookmarkMenu::slotOpenFolderInTabs); m_parentMenu->addAction(paOpenFolderInTabs); m_actions.append(paOpenFolderInTabs); } void KBookmarkMenu::addAddBookmarksList() { if (!m_pOwner || !m_pOwner->enableOption(KBookmarkOwner::ShowAddBookmark) || !m_pOwner->supportsTabs() || !KAuthorized::authorizeKAction(QStringLiteral("bookmarks"))) { return; } - if (d->bookmarksToFolder == 0) { + if (d->bookmarksToFolder == nullptr) { QString title = tr("Bookmark Tabs as Folder..."); d->bookmarksToFolder = new QAction(title, this); - m_actionCollection->addAction(m_bIsRoot ? "add_bookmarks_list" : 0, d->bookmarksToFolder); + m_actionCollection->addAction(m_bIsRoot ? "add_bookmarks_list" : nullptr, d->bookmarksToFolder); d->bookmarksToFolder->setIcon(QIcon::fromTheme(QStringLiteral("bookmark-new-list"))); d->bookmarksToFolder->setToolTip(tr("Add a folder of bookmarks for all open tabs.")); d->bookmarksToFolder->setStatusTip(d->bookmarksToFolder->toolTip()); connect(d->bookmarksToFolder, &QAction::triggered, this, &KBookmarkMenu::slotAddBookmarksList); } m_parentMenu->addAction(d->bookmarksToFolder); } void KBookmarkMenu::addAddBookmark() { if (!m_pOwner || !m_pOwner->enableOption(KBookmarkOwner::ShowAddBookmark) || !KAuthorized::authorizeKAction(QStringLiteral("bookmarks"))) { return; } - if (d->addAddBookmark == 0) { + if (d->addAddBookmark == nullptr) { d->addAddBookmark = m_actionCollection->addAction( KStandardAction::AddBookmark, - m_bIsRoot ? "add_bookmark" : 0, + m_bIsRoot ? "add_bookmark" : nullptr, this, SLOT(slotAddBookmark())); if (!m_bIsRoot) { d->addAddBookmark->setShortcut(QKeySequence()); } } m_parentMenu->addAction(d->addAddBookmark); } void KBookmarkMenu::addEditBookmarks() { if ((m_pOwner && !m_pOwner->enableOption(KBookmarkOwner::ShowEditBookmark)) || !KAuthorized::authorizeKAction(QStringLiteral("bookmarks"))) { return; } QAction *m_paEditBookmarks = m_actionCollection->addAction(KStandardAction::EditBookmarks, QStringLiteral("edit_bookmarks"), m_pManager, SLOT(slotEditBookmarks())); m_parentMenu->addAction(m_paEditBookmarks); m_paEditBookmarks->setToolTip(tr("Edit your bookmark collection in a separate window")); m_paEditBookmarks->setStatusTip(m_paEditBookmarks->toolTip()); } void KBookmarkMenu::addNewFolder() { if (!m_pOwner || !m_pOwner->enableOption(KBookmarkOwner::ShowAddBookmark) || !KAuthorized::authorizeKAction(QStringLiteral("bookmarks"))) { return; } - if (d->newBookmarkFolder == 0) { + if (d->newBookmarkFolder == nullptr) { d->newBookmarkFolder = new QAction(tr("New Bookmark Folder..."), this); d->newBookmarkFolder->setIcon(QIcon::fromTheme(QStringLiteral("folder-new"))); d->newBookmarkFolder->setToolTip(tr("Create a new bookmark folder in this menu")); d->newBookmarkFolder->setStatusTip(d->newBookmarkFolder->toolTip()); connect(d->newBookmarkFolder, &QAction::triggered, this, &KBookmarkMenu::slotNewFolder); } m_parentMenu->addAction(d->newBookmarkFolder); } void KBookmarkMenu::fillBookmarks() { KBookmarkGroup parentBookmark = m_pManager->findByAddress(m_parentAddress).toGroup(); Q_ASSERT(!parentBookmark.isNull()); if (m_bIsRoot && !parentBookmark.first().isNull()) { // at least one bookmark m_parentMenu->addSeparator(); } for (KBookmark bm = parentBookmark.first(); !bm.isNull(); bm = parentBookmark.next(bm)) { m_parentMenu->addAction(actionForBookmark(bm)); } } QAction *KBookmarkMenu::actionForBookmark(const KBookmark &bm) { if (bm.isGroup()) { //qDebug() << "Creating bookmark submenu named " << bm.text(); KActionMenu *actionMenu = new KBookmarkActionMenu(bm, this); m_actions.append(actionMenu); KBookmarkMenu *subMenu = new KBookmarkMenu(m_pManager, m_pOwner, actionMenu->menu(), bm.address()); m_lstSubMenus.append(subMenu); return actionMenu; } else if (bm.isSeparator()) { QAction *sa = new QAction(this); sa->setSeparator(true); m_actions.append(sa); return sa; } else { //qDebug() << "Creating bookmark menu item for " << bm.text(); QAction *action = new KBookmarkAction(bm, m_pOwner, this); m_actions.append(action); return action; } } void KBookmarkMenu::slotAddBookmarksList() { if (!m_pOwner || !m_pOwner->supportsTabs()) { return; } KBookmarkGroup parentBookmark = m_pManager->findByAddress(m_parentAddress).toGroup(); KBookmarkDialog *dlg = m_pOwner->bookmarkDialog(m_pManager, QApplication::activeWindow()); dlg->addBookmarks(m_pOwner->currentBookmarkList(), QLatin1String(""), parentBookmark); delete dlg; } void KBookmarkMenu::slotAddBookmark() { if (!m_pOwner) { return; } if (m_pOwner->currentTitle().isEmpty() && m_pOwner->currentUrl().isEmpty()) { return; } KBookmarkGroup parentBookmark = m_pManager->findByAddress(m_parentAddress).toGroup(); if (KBookmarkSettings::self()->m_advancedaddbookmark) { KBookmarkDialog *dlg = m_pOwner->bookmarkDialog(m_pManager, QApplication::activeWindow()); dlg->addBookmark(m_pOwner->currentTitle(), m_pOwner->currentUrl(), m_pOwner->currentIcon(), parentBookmark); delete dlg; } else { parentBookmark.addBookmark(m_pOwner->currentTitle(), m_pOwner->currentUrl(), m_pOwner->currentIcon()); m_pManager->emitChanged(parentBookmark); } } void KBookmarkMenu::slotOpenFolderInTabs() { m_pOwner->openFolderinTabs(m_pManager->findByAddress(m_parentAddress).toGroup()); } void KBookmarkMenu::slotNewFolder() { if (!m_pOwner) { return; // this view doesn't handle bookmarks... } KBookmarkGroup parentBookmark = m_pManager->findByAddress(m_parentAddress).toGroup(); Q_ASSERT(!parentBookmark.isNull()); KBookmarkDialog *dlg = m_pOwner->bookmarkDialog(m_pManager, QApplication::activeWindow()); dlg->createNewFolder(QLatin1String(""), parentBookmark); delete dlg; } void KImportedBookmarkMenu::slotNSLoad() { // qDebug()<<"**** slotNSLoad ****"<disconnect(SIGNAL(aboutToShow())); // not NSImporter, but kept old name for BC reasons KBookmarkMenuImporter importer(manager(), this); importer.openBookmarks(m_location, m_type); } 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 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('&', 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(); } #include "moc_kbookmarkmenu.cpp" #include "moc_kbookmarkmenu_p.cpp" diff --git a/src/konqbookmarkmenu.cpp b/src/konqbookmarkmenu.cpp index d2a0525..aa58037 100644 --- a/src/konqbookmarkmenu.cpp +++ b/src/konqbookmarkmenu.cpp @@ -1,238 +1,238 @@ /* This file is part of the KDE project Copyright (C) 1998, 1999 Torben Weis Copyright (C) 2006 Daniel Teske 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 #include #include #include #include #include #include #include "kbookmarkimporter.h" #include "kbookmarkimporter_opera.h" #include "kbookmarkimporter_ie.h" #include "kbookmarkmenu_p.h" 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(QIcon::fromTheme(QLatin1String("")), text, this, SLOT(toggleShowInToolbar())); } addFolderActions(); } else { if (owner()) { addAction(QIcon::fromTheme(QStringLiteral("window-new")), tr("Open in New Window"), this, SLOT(openInNewWindow())); addAction(QIcon::fromTheme(QStringLiteral("tab-new")), tr("Open in New Tab"), this, SLOT(openInNewTab())); } addBookmark(); if (filteredToolbar) { QString text = bookmark().showInToolbar() ? tr("Hide in toolbar") : tr("Show in toolbar"); addAction(QIcon::fromTheme(QLatin1String("")), text, this, SLOT(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()) { // qDebug() << "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 { // qDebug() << "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 = Q_NULLPTR; if (!config.hasKey("DynamicMenus")) { if (bookmarkrc.hasGroup("DynamicMenu-" + id)) { KConfigGroup dynGroup(&bookmarkrc, "DynamicMenu-" + id); 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(QString("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(action); if (!act) { - return 0; + return nullptr; } return new KonqBookmarkContextMenu(act->bookmark(), manager(), owner()); } #include "moc_konqbookmarkmenu.cpp"