2021-05-24 07:25:56 +00:00
|
|
|
/*
|
|
|
|
SPDX-FileCopyrightText: 2006 Pino Toscano <pino@kde.org>
|
|
|
|
|
|
|
|
SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
2006-12-27 16:04:49 +00:00
|
|
|
|
2007-04-19 18:30:20 +00:00
|
|
|
#include "bookmarkmanager.h"
|
|
|
|
|
2006-12-27 16:04:49 +00:00
|
|
|
// qt/kde includes
|
2020-07-08 11:54:37 +00:00
|
|
|
#include <KBookmarkAction>
|
|
|
|
#include <KBookmarkManager>
|
|
|
|
#include <KBookmarkMenu>
|
2022-10-15 20:52:36 +00:00
|
|
|
#include <KBookmarkOwner>
|
2015-01-29 19:55:57 +00:00
|
|
|
#include <QDebug>
|
2020-07-22 23:18:59 +00:00
|
|
|
#include <QFileInfo>
|
2014-10-08 11:48:01 +00:00
|
|
|
#include <QGuiApplication>
|
2015-01-29 19:55:57 +00:00
|
|
|
#include <QHash>
|
2020-07-08 11:54:37 +00:00
|
|
|
#include <QSet>
|
2014-10-07 11:21:58 +00:00
|
|
|
#include <QStandardPaths>
|
|
|
|
#include <QUrl>
|
2006-12-27 16:04:49 +00:00
|
|
|
|
|
|
|
// local includes
|
2007-10-28 18:31:33 +00:00
|
|
|
#include "document_p.h"
|
|
|
|
#include "observer.h"
|
2006-12-27 16:04:49 +00:00
|
|
|
|
|
|
|
using namespace Okular;
|
|
|
|
|
2007-10-28 18:31:33 +00:00
|
|
|
#define foreachObserver(cmd) \
|
|
|
|
{ \
|
2013-02-24 21:58:53 +00:00
|
|
|
QSet<DocumentObserver *>::const_iterator it = d->document->m_observers.constBegin(), end = d->document->m_observers.constEnd(); \
|
2007-10-28 18:31:33 +00:00
|
|
|
for (; it != end; ++it) { \
|
|
|
|
(*it)->cmd; \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2009-12-26 16:57:09 +00:00
|
|
|
#define foreachObserverD(cmd) \
|
|
|
|
{ \
|
2013-02-24 21:58:53 +00:00
|
|
|
QSet<DocumentObserver *>::const_iterator it = document->m_observers.constBegin(), end = document->m_observers.constEnd(); \
|
2009-12-26 16:57:09 +00:00
|
|
|
for (; it != end; ++it) { \
|
|
|
|
(*it)->cmd; \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2007-01-18 21:20:08 +00:00
|
|
|
class OkularBookmarkAction : public KBookmarkAction
|
|
|
|
{
|
2020-02-21 15:25:29 +00:00
|
|
|
Q_OBJECT
|
2007-01-18 21:20:08 +00:00
|
|
|
public:
|
|
|
|
OkularBookmarkAction(const Okular::DocumentViewport &vp, const KBookmark &bk, KBookmarkOwner *owner, QObject *parent)
|
|
|
|
: KBookmarkAction(bk, owner, parent)
|
|
|
|
{
|
|
|
|
if (vp.isValid()) {
|
2016-07-11 20:07:57 +00:00
|
|
|
setText(QString::number(vp.pageNumber + 1) + QStringLiteral(" - ") + text());
|
2022-03-08 10:10:43 +00:00
|
|
|
}
|
2011-10-25 22:10:42 +00:00
|
|
|
setProperty("pageNumber", vp.pageNumber + 1);
|
2014-08-08 00:00:17 +00:00
|
|
|
setProperty("htmlRef", bk.url().fragment(QUrl::FullyDecoded));
|
2010-05-22 17:44:26 +00:00
|
|
|
}
|
2012-03-26 18:01:01 +00:00
|
|
|
|
2010-05-22 17:44:26 +00:00
|
|
|
inline int pageNumber() const
|
2020-07-10 22:15:05 +00:00
|
|
|
{
|
2011-10-25 22:10:42 +00:00
|
|
|
return property("pageNumber").toInt();
|
2020-07-10 22:15:05 +00:00
|
|
|
}
|
|
|
|
|
2012-03-26 18:01:01 +00:00
|
|
|
inline QString htmlRef() const
|
|
|
|
{
|
|
|
|
return property("htmlRef").toString();
|
|
|
|
}
|
2007-01-18 21:20:08 +00:00
|
|
|
};
|
|
|
|
|
2012-03-26 18:01:01 +00:00
|
|
|
static inline bool documentViewportFuzzyCompare(const DocumentViewport &vp1, const DocumentViewport &vp2)
|
2010-05-22 17:44:26 +00:00
|
|
|
{
|
2012-03-26 18:01:01 +00:00
|
|
|
bool equal = vp1.isValid() && vp2.isValid() && (vp1.pageNumber == vp2.pageNumber) && (vp1.rePos.pos == vp2.rePos.pos);
|
|
|
|
|
|
|
|
if (!equal) {
|
|
|
|
return false;
|
2022-03-08 10:10:43 +00:00
|
|
|
}
|
2012-03-26 18:01:01 +00:00
|
|
|
|
|
|
|
if (qAbs(vp1.rePos.normalizedX - vp2.rePos.normalizedX) >= 0.000001) {
|
|
|
|
return false;
|
2022-03-08 10:10:43 +00:00
|
|
|
}
|
2012-03-26 18:01:01 +00:00
|
|
|
|
|
|
|
if (qAbs(vp1.rePos.normalizedY - vp2.rePos.normalizedY) >= 0.000001) {
|
|
|
|
return false;
|
2022-03-08 10:10:43 +00:00
|
|
|
}
|
2012-03-26 18:01:01 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool bookmarkLessThan(const KBookmark &b1, const KBookmark &b2)
|
|
|
|
{
|
2014-08-08 00:00:17 +00:00
|
|
|
DocumentViewport vp1(b1.url().fragment(QUrl::FullyDecoded));
|
|
|
|
DocumentViewport vp2(b2.url().fragment(QUrl::FullyDecoded));
|
2012-03-26 18:01:01 +00:00
|
|
|
|
2012-06-04 14:10:33 +00:00
|
|
|
return vp1 < vp2;
|
2012-03-26 18:01:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool okularBookmarkActionLessThan(QAction *a1, QAction *a2)
|
|
|
|
{
|
|
|
|
DocumentViewport vp1(static_cast<OkularBookmarkAction *>(a1)->htmlRef());
|
|
|
|
DocumentViewport vp2(static_cast<OkularBookmarkAction *>(a2)->htmlRef());
|
|
|
|
|
2012-06-04 14:10:33 +00:00
|
|
|
return vp1 < vp2;
|
2010-05-22 17:44:26 +00:00
|
|
|
}
|
|
|
|
|
2020-07-22 23:18:59 +00:00
|
|
|
static QUrl mostCanonicalUrl(const QUrl &url)
|
|
|
|
{
|
|
|
|
if (!url.isLocalFile()) {
|
|
|
|
return url;
|
2022-03-08 10:10:43 +00:00
|
|
|
}
|
2020-07-22 23:18:59 +00:00
|
|
|
|
|
|
|
const QFileInfo fi(url.toLocalFile());
|
|
|
|
return QUrl::fromLocalFile(fi.canonicalFilePath());
|
|
|
|
}
|
|
|
|
|
2007-01-13 15:26:05 +00:00
|
|
|
class BookmarkManager::Private : public KBookmarkOwner
|
2006-12-27 16:04:49 +00:00
|
|
|
{
|
|
|
|
public:
|
2021-08-21 23:14:34 +00:00
|
|
|
explicit Private(BookmarkManager *qq)
|
2017-09-05 21:27:18 +00:00
|
|
|
: KBookmarkOwner()
|
|
|
|
, q(qq)
|
|
|
|
, document(nullptr)
|
2023-12-05 15:48:45 +00:00
|
|
|
, file(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/okular/bookmarks.xml"))
|
|
|
|
, manager(KBookmarkManager(file))
|
2006-12-27 16:04:49 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-10-29 14:38:19 +00:00
|
|
|
~Private() override
|
2007-01-13 15:26:05 +00:00
|
|
|
{
|
|
|
|
knownFiles.clear();
|
2007-07-14 12:44:08 +00:00
|
|
|
// no need to delete the manager, it's automatically done by KBookmarkManager
|
|
|
|
// delete manager;
|
2007-01-13 15:26:05 +00:00
|
|
|
}
|
|
|
|
|
2020-02-21 15:11:42 +00:00
|
|
|
Private(const Private &) = delete;
|
|
|
|
Private &operator=(const Private &) = delete;
|
|
|
|
|
2016-10-29 14:38:19 +00:00
|
|
|
QUrl currentUrl() const override;
|
|
|
|
QString currentTitle() const override;
|
|
|
|
bool enableOption(BookmarkOption option) const override;
|
|
|
|
void openBookmark(const KBookmark &bm, Qt::MouseButtons, Qt::KeyboardModifiers) override;
|
2007-01-13 15:26:05 +00:00
|
|
|
|
2017-09-05 21:27:18 +00:00
|
|
|
QHash<QUrl, QString>::iterator bookmarkFind(const QUrl &url, bool doCreate, KBookmarkGroup *result = nullptr);
|
2009-12-26 16:57:09 +00:00
|
|
|
|
|
|
|
// slots
|
2024-01-03 20:02:38 +00:00
|
|
|
void _o_changed(const QString &groupAddress);
|
2008-02-26 21:27:19 +00:00
|
|
|
|
2007-01-13 15:26:05 +00:00
|
|
|
BookmarkManager *q;
|
2015-01-29 19:55:57 +00:00
|
|
|
QUrl url;
|
2012-03-26 18:01:01 +00:00
|
|
|
QHash<int, int> urlBookmarks;
|
2007-10-28 18:31:33 +00:00
|
|
|
DocumentPrivate *document;
|
2006-12-27 16:04:49 +00:00
|
|
|
QString file;
|
2023-12-05 15:48:45 +00:00
|
|
|
KBookmarkManager manager;
|
2015-01-29 19:55:57 +00:00
|
|
|
QHash<QUrl, QString> knownFiles;
|
2006-12-27 16:04:49 +00:00
|
|
|
};
|
|
|
|
|
2015-01-29 19:55:57 +00:00
|
|
|
static inline QUrl urlForGroup(const KBookmark &group)
|
2010-03-27 18:00:48 +00:00
|
|
|
{
|
2015-01-29 19:55:57 +00:00
|
|
|
if (group.url().isValid()) {
|
|
|
|
return group.url();
|
|
|
|
} else {
|
|
|
|
return QUrl::fromUserInput(group.fullText());
|
2022-03-08 10:10:43 +00:00
|
|
|
}
|
2010-03-27 18:00:48 +00:00
|
|
|
}
|
|
|
|
|
2007-10-28 18:31:33 +00:00
|
|
|
BookmarkManager::BookmarkManager(DocumentPrivate *document)
|
|
|
|
: QObject(document->m_parent)
|
|
|
|
, d(new Private(this))
|
2006-12-27 16:04:49 +00:00
|
|
|
{
|
2015-10-29 12:37:11 +00:00
|
|
|
setObjectName(QStringLiteral("Okular::BookmarkManager"));
|
2006-12-27 16:04:49 +00:00
|
|
|
|
|
|
|
d->document = document;
|
|
|
|
|
2024-01-03 20:02:38 +00:00
|
|
|
connect(&d->manager, &KBookmarkManager::changed, this, [this](const QString &groupAddress) { d->_o_changed(groupAddress); });
|
2006-12-27 16:04:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BookmarkManager::~BookmarkManager()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
// BEGIN Reimplementations from KBookmarkOwner
|
2014-08-08 00:00:17 +00:00
|
|
|
QUrl BookmarkManager::Private::currentUrl() const
|
2006-12-27 16:04:49 +00:00
|
|
|
{
|
2014-08-08 00:00:17 +00:00
|
|
|
return url;
|
2006-12-27 16:04:49 +00:00
|
|
|
}
|
|
|
|
|
2007-01-13 15:26:05 +00:00
|
|
|
QString BookmarkManager::Private::currentTitle() const
|
2006-12-27 16:04:49 +00:00
|
|
|
{
|
2015-01-29 19:55:57 +00:00
|
|
|
return url.toDisplayString();
|
2006-12-27 16:04:49 +00:00
|
|
|
}
|
|
|
|
|
2007-08-13 10:51:17 +00:00
|
|
|
bool BookmarkManager::Private::enableOption(BookmarkOption option) const
|
2006-12-27 16:04:49 +00:00
|
|
|
{
|
2007-08-14 12:26:08 +00:00
|
|
|
Q_UNUSED(option)
|
2007-01-13 15:26:05 +00:00
|
|
|
return false;
|
2006-12-27 16:04:49 +00:00
|
|
|
}
|
|
|
|
|
2007-01-13 15:26:05 +00:00
|
|
|
void BookmarkManager::Private::openBookmark(const KBookmark &bm, Qt::MouseButtons, Qt::KeyboardModifiers)
|
2006-12-27 16:04:49 +00:00
|
|
|
{
|
2022-03-18 21:35:45 +00:00
|
|
|
Q_EMIT q->openUrl(bm.url());
|
2006-12-27 16:04:49 +00:00
|
|
|
}
|
|
|
|
// END Reimplementations from KBookmarkOwner
|
|
|
|
|
2024-01-03 20:02:38 +00:00
|
|
|
void BookmarkManager::Private::_o_changed(const QString &groupAddress)
|
2009-12-26 16:57:09 +00:00
|
|
|
{
|
|
|
|
if (groupAddress.isEmpty()) {
|
|
|
|
return;
|
2022-03-08 10:10:43 +00:00
|
|
|
}
|
2009-12-26 16:57:09 +00:00
|
|
|
|
2015-01-29 19:55:57 +00:00
|
|
|
QUrl referurl;
|
2009-12-26 16:57:09 +00:00
|
|
|
// first, try to find the bookmark group whom change notification was just received
|
2015-01-29 19:55:57 +00:00
|
|
|
QHash<QUrl, QString>::iterator it = knownFiles.begin(), itEnd = knownFiles.end();
|
2009-12-26 16:57:09 +00:00
|
|
|
for (; it != itEnd; ++it) {
|
|
|
|
if (it.value() == groupAddress) {
|
|
|
|
referurl = it.key();
|
|
|
|
knownFiles.erase(it);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!referurl.isValid()) {
|
2023-12-05 15:48:45 +00:00
|
|
|
const KBookmark bm = manager.findByAddress(groupAddress);
|
2009-12-26 16:57:09 +00:00
|
|
|
// better be safe than sorry
|
|
|
|
if (bm.isNull()) {
|
|
|
|
return;
|
2022-03-08 10:10:43 +00:00
|
|
|
}
|
2009-12-26 16:57:09 +00:00
|
|
|
Q_ASSERT(bm.isGroup());
|
2010-03-27 18:00:48 +00:00
|
|
|
referurl = urlForGroup(bm);
|
2009-12-26 16:57:09 +00:00
|
|
|
}
|
|
|
|
Q_ASSERT(referurl.isValid());
|
2022-03-18 21:35:45 +00:00
|
|
|
Q_EMIT q->bookmarksChanged(referurl);
|
2009-12-26 16:57:09 +00:00
|
|
|
// case for the url representing the current document
|
|
|
|
// (this might happen if the same document is open in another place;
|
|
|
|
// in such case, make really sure to be in sync)
|
|
|
|
if (referurl == url) {
|
|
|
|
// save the old bookmarks for the current url
|
2012-03-26 18:01:01 +00:00
|
|
|
const QHash<int, int> oldUrlBookmarks = urlBookmarks;
|
2009-12-26 16:57:09 +00:00
|
|
|
// set the same url again, so we reload the information we have about it
|
|
|
|
q->setUrl(referurl);
|
|
|
|
// then notify the observers about the changes in the bookmarks
|
2012-03-26 18:01:01 +00:00
|
|
|
for (int i = 0; i < qMax(oldUrlBookmarks.size(), urlBookmarks.size()); i++) {
|
|
|
|
bool oldContains = oldUrlBookmarks.contains(i) && oldUrlBookmarks[i] > 0;
|
|
|
|
bool curContains = urlBookmarks.contains(i) && urlBookmarks[i] > 0;
|
|
|
|
|
|
|
|
if (oldContains != curContains) {
|
|
|
|
foreachObserverD(notifyPageChanged(i, DocumentObserver::Bookmark));
|
|
|
|
} else if (oldContains && oldUrlBookmarks[i] != urlBookmarks[i]) {
|
|
|
|
foreachObserverD(notifyPageChanged(i, DocumentObserver::Bookmark));
|
|
|
|
}
|
2009-12-26 16:57:09 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-18 21:35:45 +00:00
|
|
|
Q_EMIT q->saved();
|
2009-12-26 16:57:09 +00:00
|
|
|
}
|
|
|
|
|
2014-08-13 11:07:44 +00:00
|
|
|
QList<QUrl> BookmarkManager::files() const
|
2006-12-27 16:04:49 +00:00
|
|
|
{
|
2014-08-13 11:07:44 +00:00
|
|
|
QList<QUrl> ret;
|
2023-12-05 15:48:45 +00:00
|
|
|
KBookmarkGroup group = d->manager.root();
|
2006-12-27 16:04:49 +00:00
|
|
|
for (KBookmark bm = group.first(); !bm.isNull(); bm = group.next(bm)) {
|
|
|
|
if (bm.isSeparator() || !bm.isGroup()) {
|
|
|
|
continue;
|
2022-03-08 10:10:43 +00:00
|
|
|
}
|
2006-12-27 16:04:49 +00:00
|
|
|
|
2010-03-27 18:00:48 +00:00
|
|
|
ret.append(urlForGroup(bm));
|
2006-12-27 16:04:49 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-07-22 23:18:59 +00:00
|
|
|
KBookmark::List BookmarkManager::bookmarks(const QUrl &documentUrl) const
|
2006-12-27 16:04:49 +00:00
|
|
|
{
|
2020-07-22 23:18:59 +00:00
|
|
|
const QUrl url = mostCanonicalUrl(documentUrl);
|
2006-12-27 16:04:49 +00:00
|
|
|
KBookmark::List ret;
|
2023-12-05 15:48:45 +00:00
|
|
|
KBookmarkGroup group = d->manager.root();
|
2006-12-27 16:04:49 +00:00
|
|
|
for (KBookmark bm = group.first(); !bm.isNull(); bm = group.next(bm)) {
|
2010-03-27 18:00:48 +00:00
|
|
|
if (!bm.isGroup() || urlForGroup(bm) != url) {
|
2006-12-27 16:04:49 +00:00
|
|
|
continue;
|
2022-03-08 10:10:43 +00:00
|
|
|
}
|
2006-12-27 16:04:49 +00:00
|
|
|
|
|
|
|
KBookmarkGroup group = bm.toGroup();
|
|
|
|
for (KBookmark b = group.first(); !b.isNull(); b = group.next(b)) {
|
|
|
|
if (b.isSeparator() || b.isGroup()) {
|
|
|
|
continue;
|
2022-03-08 10:10:43 +00:00
|
|
|
}
|
2006-12-27 16:04:49 +00:00
|
|
|
|
|
|
|
ret.append(b);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2012-03-26 18:01:01 +00:00
|
|
|
|
2006-12-27 16:04:49 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-10-25 20:21:22 +00:00
|
|
|
KBookmark::List BookmarkManager::bookmarks() const
|
|
|
|
{
|
|
|
|
return bookmarks(d->url);
|
|
|
|
}
|
2012-03-26 18:01:01 +00:00
|
|
|
|
|
|
|
KBookmark::List BookmarkManager::bookmarks(int page) const
|
|
|
|
{
|
|
|
|
const KBookmark::List bmarks = bookmarks();
|
|
|
|
KBookmark::List ret;
|
2019-12-09 13:16:55 +00:00
|
|
|
for (const KBookmark &bm : bmarks) {
|
2014-08-08 00:00:17 +00:00
|
|
|
DocumentViewport vp(bm.url().fragment(QUrl::FullyDecoded));
|
2012-03-26 18:01:01 +00:00
|
|
|
if (vp.isValid() && vp.pageNumber == page) {
|
|
|
|
ret.append(bm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-10-25 20:21:22 +00:00
|
|
|
KBookmark BookmarkManager::bookmark(int page) const
|
|
|
|
{
|
|
|
|
const KBookmark::List bmarks = bookmarks();
|
2019-12-09 13:16:55 +00:00
|
|
|
for (const KBookmark &bm : bmarks) {
|
2014-08-08 00:00:17 +00:00
|
|
|
DocumentViewport vp(bm.url().fragment(QUrl::FullyDecoded));
|
2011-10-25 20:21:22 +00:00
|
|
|
if (vp.isValid() && vp.pageNumber == page) {
|
|
|
|
return bm;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return KBookmark();
|
|
|
|
}
|
|
|
|
|
2012-03-26 18:01:01 +00:00
|
|
|
KBookmark BookmarkManager::bookmark(const DocumentViewport &viewport) const
|
|
|
|
{
|
|
|
|
if (!viewport.isValid() || !isBookmarked(viewport.pageNumber)) {
|
|
|
|
return KBookmark();
|
2022-03-08 10:10:43 +00:00
|
|
|
}
|
2012-03-26 18:01:01 +00:00
|
|
|
|
|
|
|
KBookmarkGroup thebg;
|
2015-01-29 19:55:57 +00:00
|
|
|
QHash<QUrl, QString>::iterator it = d->bookmarkFind(d->url, false, &thebg);
|
2012-03-26 18:01:01 +00:00
|
|
|
if (it == d->knownFiles.end()) {
|
|
|
|
return KBookmark();
|
2022-03-08 10:10:43 +00:00
|
|
|
}
|
2012-03-26 18:01:01 +00:00
|
|
|
|
|
|
|
for (KBookmark bm = thebg.first(); !bm.isNull(); bm = thebg.next(bm)) {
|
|
|
|
if (bm.isSeparator() || bm.isGroup()) {
|
|
|
|
continue;
|
2022-03-08 10:10:43 +00:00
|
|
|
}
|
2012-03-26 18:01:01 +00:00
|
|
|
|
2014-08-08 00:00:17 +00:00
|
|
|
DocumentViewport vp(bm.url().fragment(QUrl::FullyDecoded));
|
2012-03-26 18:01:01 +00:00
|
|
|
if (documentViewportFuzzyCompare(vp, viewport)) {
|
|
|
|
return bm;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return KBookmark();
|
|
|
|
}
|
|
|
|
|
2006-12-27 16:04:49 +00:00
|
|
|
void BookmarkManager::save() const
|
|
|
|
{
|
2023-12-05 15:48:45 +00:00
|
|
|
d->manager.emitChanged();
|
2022-03-18 21:35:45 +00:00
|
|
|
Q_EMIT const_cast<BookmarkManager *>(this)->saved();
|
2006-12-27 16:04:49 +00:00
|
|
|
}
|
|
|
|
|
2015-01-29 19:55:57 +00:00
|
|
|
QHash<QUrl, QString>::iterator BookmarkManager::Private::bookmarkFind(const QUrl &url, bool doCreate, KBookmarkGroup *result)
|
2006-12-27 16:04:49 +00:00
|
|
|
{
|
2015-01-29 19:55:57 +00:00
|
|
|
QHash<QUrl, QString>::iterator it = knownFiles.find(url);
|
2008-02-26 21:27:19 +00:00
|
|
|
if (it == knownFiles.end()) {
|
2006-12-27 16:04:49 +00:00
|
|
|
// if the url we want to add a new entry for is not in the hash of the
|
|
|
|
// known files, then first try to find the file among the top-level
|
|
|
|
// "folder" names
|
|
|
|
bool found = false;
|
2023-12-05 15:48:45 +00:00
|
|
|
KBookmarkGroup root = manager.root();
|
2006-12-27 16:04:49 +00:00
|
|
|
for (KBookmark bm = root.first(); !found && !bm.isNull(); bm = root.next(bm)) {
|
|
|
|
if (bm.isSeparator() || !bm.isGroup()) {
|
|
|
|
continue;
|
2022-03-08 10:10:43 +00:00
|
|
|
}
|
2006-12-27 16:04:49 +00:00
|
|
|
|
2015-01-29 19:55:57 +00:00
|
|
|
QUrl tmpurl(urlForGroup(bm));
|
2006-12-27 16:04:49 +00:00
|
|
|
if (tmpurl == url) {
|
|
|
|
// got it! place it the hash of known files
|
2009-12-26 16:57:09 +00:00
|
|
|
KBookmarkGroup bg = bm.toGroup();
|
|
|
|
it = knownFiles.insert(url, bg.address());
|
2006-12-27 16:04:49 +00:00
|
|
|
found = true;
|
2009-12-26 16:57:09 +00:00
|
|
|
if (result) {
|
|
|
|
*result = bg;
|
2022-03-08 10:10:43 +00:00
|
|
|
}
|
2006-12-27 16:04:49 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found && doCreate) {
|
|
|
|
// folder not found :(
|
|
|
|
// then, in a single step create a new folder and add it in our cache :)
|
2015-01-29 19:55:57 +00:00
|
|
|
QString purl = url.isLocalFile() ? url.toLocalFile() : url.toDisplayString();
|
2009-12-26 16:57:09 +00:00
|
|
|
KBookmarkGroup newbg = root.createNewFolder(purl);
|
2010-03-27 18:00:48 +00:00
|
|
|
newbg.setUrl(url);
|
2009-12-26 16:57:09 +00:00
|
|
|
it = knownFiles.insert(url, newbg.address());
|
|
|
|
if (result) {
|
|
|
|
*result = newbg;
|
2022-03-08 10:10:43 +00:00
|
|
|
}
|
2006-12-27 16:04:49 +00:00
|
|
|
}
|
2009-12-26 16:57:09 +00:00
|
|
|
} else if (result) {
|
2023-12-05 15:48:45 +00:00
|
|
|
const KBookmark bm = manager.findByAddress(it.value());
|
2009-12-26 16:57:09 +00:00
|
|
|
Q_ASSERT(bm.isGroup());
|
|
|
|
*result = bm.toGroup();
|
|
|
|
}
|
2006-12-27 16:04:49 +00:00
|
|
|
return it;
|
|
|
|
}
|
|
|
|
|
2019-12-20 15:40:59 +00:00
|
|
|
void BookmarkManager::addBookmark(int page)
|
2007-10-28 18:31:33 +00:00
|
|
|
{
|
2019-12-20 15:40:59 +00:00
|
|
|
if (page >= 0 && page < (int)d->document->m_pagesVector.count()) {
|
|
|
|
if (setPageBookmark(page))
|
|
|
|
foreachObserver(notifyPageChanged(page, DocumentObserver::Bookmark));
|
2007-10-28 18:31:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-26 18:01:01 +00:00
|
|
|
void BookmarkManager::addBookmark(const DocumentViewport &vp)
|
|
|
|
{
|
|
|
|
addBookmark(d->url, vp);
|
|
|
|
}
|
|
|
|
|
2020-07-22 23:18:59 +00:00
|
|
|
bool BookmarkManager::addBookmark(const QUrl &documentUrl, const Okular::DocumentViewport &vp, const QString &title)
|
2006-12-27 16:04:49 +00:00
|
|
|
{
|
2020-07-22 23:18:59 +00:00
|
|
|
if (!documentUrl.isValid() || !vp.isValid()) {
|
2006-12-27 16:04:49 +00:00
|
|
|
return false;
|
2022-03-08 10:10:43 +00:00
|
|
|
}
|
2006-12-27 16:04:49 +00:00
|
|
|
|
2012-03-26 18:01:01 +00:00
|
|
|
if (vp.pageNumber < 0 || vp.pageNumber >= d->document->m_pagesVector.count()) {
|
|
|
|
return false;
|
2022-03-08 10:10:43 +00:00
|
|
|
}
|
2012-03-26 18:01:01 +00:00
|
|
|
|
2020-07-22 23:18:59 +00:00
|
|
|
const QUrl referurl = mostCanonicalUrl(documentUrl);
|
|
|
|
|
2009-12-26 16:57:09 +00:00
|
|
|
KBookmarkGroup thebg;
|
2015-01-29 19:55:57 +00:00
|
|
|
QHash<QUrl, QString>::iterator it = d->bookmarkFind(referurl, true, &thebg);
|
2006-12-27 16:04:49 +00:00
|
|
|
Q_ASSERT(it != d->knownFiles.end());
|
|
|
|
|
2012-03-26 18:01:01 +00:00
|
|
|
int count = 0; // Number of bookmarks in the current page
|
|
|
|
bool found = false;
|
|
|
|
// Check if the bookmark already exists
|
|
|
|
for (KBookmark bm = thebg.first(); !found && !bm.isNull(); bm = thebg.next(bm)) {
|
|
|
|
if (bm.isSeparator() || bm.isGroup()) {
|
|
|
|
continue;
|
2022-03-08 10:10:43 +00:00
|
|
|
}
|
2012-03-26 18:01:01 +00:00
|
|
|
|
2014-08-08 00:00:17 +00:00
|
|
|
DocumentViewport bmViewport(bm.url().fragment(QUrl::FullyDecoded));
|
2012-03-26 18:01:01 +00:00
|
|
|
if (bmViewport.isValid() && bmViewport.pageNumber == vp.pageNumber) {
|
|
|
|
++count;
|
|
|
|
|
|
|
|
if (documentViewportFuzzyCompare(bmViewport, vp)) {
|
|
|
|
found = true;
|
2022-03-08 10:10:43 +00:00
|
|
|
}
|
2012-03-26 18:01:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found) {
|
|
|
|
return false;
|
2022-03-08 10:10:43 +00:00
|
|
|
}
|
2012-03-26 18:01:01 +00:00
|
|
|
|
2006-12-27 16:04:49 +00:00
|
|
|
QString newtitle;
|
|
|
|
if (title.isEmpty()) {
|
|
|
|
// if we have no title specified for the new bookmark, then give it the
|
2012-03-26 18:01:01 +00:00
|
|
|
// name '#p' where p is the page number where the bookmark is located.
|
|
|
|
// if there's more than one bookmark per page, give the name '#p-n'
|
|
|
|
// where n is the index of this bookmark among the ones of its page.
|
|
|
|
if (count > 0) {
|
2015-10-29 12:37:11 +00:00
|
|
|
newtitle = QStringLiteral("#%1-%2").arg(vp.pageNumber + 1).arg(count);
|
2012-03-26 18:01:01 +00:00
|
|
|
} else {
|
2015-10-29 12:37:11 +00:00
|
|
|
newtitle = QStringLiteral("#%1").arg(vp.pageNumber + 1);
|
2022-03-08 10:10:43 +00:00
|
|
|
}
|
2006-12-27 16:04:49 +00:00
|
|
|
} else {
|
|
|
|
newtitle = title;
|
2022-03-08 10:10:43 +00:00
|
|
|
}
|
2012-03-26 18:01:01 +00:00
|
|
|
|
2015-01-29 19:55:57 +00:00
|
|
|
QUrl newurl = referurl;
|
|
|
|
newurl.setFragment(vp.toString(), QUrl::DecodedMode);
|
2009-12-26 16:57:09 +00:00
|
|
|
thebg.addBookmark(newtitle, newurl, QString());
|
2008-04-04 09:50:41 +00:00
|
|
|
if (referurl == d->document->m_url) {
|
2012-03-26 18:01:01 +00:00
|
|
|
d->urlBookmarks[vp.pageNumber]++;
|
2008-04-04 09:50:41 +00:00
|
|
|
foreachObserver(notifyPageChanged(vp.pageNumber, DocumentObserver::Bookmark));
|
|
|
|
}
|
2023-12-05 15:48:45 +00:00
|
|
|
d->manager.emitChanged(thebg);
|
2006-12-27 16:04:49 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-12-20 15:40:59 +00:00
|
|
|
void BookmarkManager::removeBookmark(int page)
|
2007-10-28 18:31:33 +00:00
|
|
|
{
|
2019-12-20 15:40:59 +00:00
|
|
|
if (page >= 0 && page < (int)d->document->m_pagesVector.count()) {
|
|
|
|
if (removePageBookmark(page))
|
|
|
|
foreachObserver(notifyPageChanged(page, DocumentObserver::Bookmark));
|
2007-10-28 18:31:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-26 18:01:01 +00:00
|
|
|
void BookmarkManager::removeBookmark(const DocumentViewport &vp)
|
|
|
|
{
|
|
|
|
int page = vp.pageNumber;
|
|
|
|
if (page >= 0 && page < d->document->m_pagesVector.count()) {
|
|
|
|
removeBookmark(d->url, bookmark(vp));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-25 20:21:22 +00:00
|
|
|
void BookmarkManager::renameBookmark(KBookmark *bm, const QString &newName)
|
|
|
|
{
|
|
|
|
KBookmarkGroup thebg;
|
2015-01-29 19:55:57 +00:00
|
|
|
QHash<QUrl, QString>::iterator it = d->bookmarkFind(d->url, false, &thebg);
|
2011-10-25 20:21:22 +00:00
|
|
|
Q_ASSERT(it != d->knownFiles.end());
|
|
|
|
if (it == d->knownFiles.end()) {
|
|
|
|
return;
|
2022-03-08 10:10:43 +00:00
|
|
|
}
|
2011-10-25 20:21:22 +00:00
|
|
|
|
|
|
|
bm->setFullText(newName);
|
2023-12-05 15:48:45 +00:00
|
|
|
d->manager.emitChanged(thebg);
|
2011-10-25 20:21:22 +00:00
|
|
|
}
|
|
|
|
|
2020-07-22 23:18:59 +00:00
|
|
|
void BookmarkManager::renameBookmark(const QUrl &documentUrl, const QString &newName)
|
2012-03-08 22:26:44 +00:00
|
|
|
{
|
2020-07-22 23:18:59 +00:00
|
|
|
if (!documentUrl.isValid()) {
|
2012-03-08 22:26:44 +00:00
|
|
|
return;
|
2022-03-08 10:10:43 +00:00
|
|
|
}
|
2012-03-08 22:26:44 +00:00
|
|
|
|
2020-07-22 23:18:59 +00:00
|
|
|
const QUrl referurl = mostCanonicalUrl(documentUrl);
|
|
|
|
|
2012-03-08 22:26:44 +00:00
|
|
|
KBookmarkGroup thebg;
|
2015-01-29 19:55:57 +00:00
|
|
|
QHash<QUrl, QString>::iterator it = d->bookmarkFind(referurl, false, &thebg);
|
2012-03-08 22:26:44 +00:00
|
|
|
Q_ASSERT(it != d->knownFiles.end());
|
|
|
|
if (it == d->knownFiles.end()) {
|
|
|
|
return;
|
2022-03-08 10:10:43 +00:00
|
|
|
}
|
2012-03-08 22:26:44 +00:00
|
|
|
|
|
|
|
thebg.setFullText(newName);
|
2023-12-05 15:48:45 +00:00
|
|
|
d->manager.emitChanged(thebg);
|
2012-03-08 22:26:44 +00:00
|
|
|
}
|
|
|
|
|
2020-07-22 23:18:59 +00:00
|
|
|
QString BookmarkManager::titleForUrl(const QUrl &documentUrl) const
|
2012-03-08 22:26:44 +00:00
|
|
|
{
|
|
|
|
KBookmarkGroup thebg;
|
2020-07-22 23:18:59 +00:00
|
|
|
QHash<QUrl, QString>::iterator it = d->bookmarkFind(mostCanonicalUrl(documentUrl), false, &thebg);
|
2012-03-08 22:26:44 +00:00
|
|
|
Q_ASSERT(it != d->knownFiles.end());
|
|
|
|
|
|
|
|
return thebg.fullText();
|
|
|
|
}
|
|
|
|
|
2020-07-22 23:18:59 +00:00
|
|
|
int BookmarkManager::removeBookmark(const QUrl &documentUrl, const KBookmark &bm)
|
2006-12-27 16:04:49 +00:00
|
|
|
{
|
2020-07-22 23:18:59 +00:00
|
|
|
if (!documentUrl.isValid() || bm.isNull() || bm.isGroup() || bm.isSeparator()) {
|
2006-12-27 16:04:49 +00:00
|
|
|
return -1;
|
2022-03-08 10:10:43 +00:00
|
|
|
}
|
2006-12-27 16:04:49 +00:00
|
|
|
|
2014-08-08 00:00:17 +00:00
|
|
|
DocumentViewport vp(bm.url().fragment(QUrl::FullyDecoded));
|
2006-12-27 16:04:49 +00:00
|
|
|
if (!vp.isValid()) {
|
|
|
|
return -1;
|
2022-03-08 10:10:43 +00:00
|
|
|
}
|
2006-12-27 16:04:49 +00:00
|
|
|
|
2020-07-22 23:18:59 +00:00
|
|
|
const QUrl referurl = mostCanonicalUrl(documentUrl);
|
|
|
|
|
2009-12-26 16:57:09 +00:00
|
|
|
KBookmarkGroup thebg;
|
2015-01-29 19:55:57 +00:00
|
|
|
QHash<QUrl, QString>::iterator it = d->bookmarkFind(referurl, false, &thebg);
|
2006-12-27 16:04:49 +00:00
|
|
|
if (it == d->knownFiles.end()) {
|
|
|
|
return -1;
|
2022-03-08 10:10:43 +00:00
|
|
|
}
|
2006-12-27 16:04:49 +00:00
|
|
|
|
2009-12-26 16:57:09 +00:00
|
|
|
thebg.deleteBookmark(bm);
|
2012-03-26 18:01:01 +00:00
|
|
|
|
2008-04-04 09:50:41 +00:00
|
|
|
if (referurl == d->document->m_url) {
|
2012-03-26 18:01:01 +00:00
|
|
|
d->urlBookmarks[vp.pageNumber]--;
|
2008-04-04 09:50:41 +00:00
|
|
|
foreachObserver(notifyPageChanged(vp.pageNumber, DocumentObserver::Bookmark));
|
|
|
|
}
|
2023-12-05 15:48:45 +00:00
|
|
|
d->manager.emitChanged(thebg);
|
2007-10-28 18:31:33 +00:00
|
|
|
|
2006-12-27 16:04:49 +00:00
|
|
|
return vp.pageNumber;
|
|
|
|
}
|
|
|
|
|
2020-07-22 23:18:59 +00:00
|
|
|
void BookmarkManager::removeBookmarks(const QUrl &documentUrl, const KBookmark::List &list)
|
2010-01-09 13:13:43 +00:00
|
|
|
{
|
2020-07-22 23:18:59 +00:00
|
|
|
if (!documentUrl.isValid() || list.isEmpty()) {
|
2010-01-09 13:13:43 +00:00
|
|
|
return;
|
2022-03-08 10:10:43 +00:00
|
|
|
}
|
2010-01-09 13:13:43 +00:00
|
|
|
|
2020-07-22 23:18:59 +00:00
|
|
|
const QUrl referurl = mostCanonicalUrl(documentUrl);
|
|
|
|
|
2010-01-09 13:13:43 +00:00
|
|
|
KBookmarkGroup thebg;
|
2015-01-29 19:55:57 +00:00
|
|
|
QHash<QUrl, QString>::iterator it = d->bookmarkFind(referurl, false, &thebg);
|
2010-01-09 13:13:43 +00:00
|
|
|
if (it == d->knownFiles.end()) {
|
|
|
|
return;
|
2022-03-08 10:10:43 +00:00
|
|
|
}
|
2010-01-09 13:13:43 +00:00
|
|
|
|
2012-03-26 18:01:01 +00:00
|
|
|
const QHash<int, int> oldUrlBookmarks = d->urlBookmarks;
|
2010-01-09 13:13:43 +00:00
|
|
|
bool deletedAny = false;
|
2019-12-09 13:16:55 +00:00
|
|
|
for (const KBookmark &bm : list) {
|
2010-01-09 13:13:43 +00:00
|
|
|
if (bm.parentGroup() == thebg) {
|
|
|
|
thebg.deleteBookmark(bm);
|
|
|
|
deletedAny = true;
|
2012-03-26 18:01:01 +00:00
|
|
|
|
2014-08-08 00:00:17 +00:00
|
|
|
DocumentViewport vp(bm.url().fragment(QUrl::FullyDecoded));
|
2010-01-09 13:13:43 +00:00
|
|
|
if (referurl == d->document->m_url) {
|
2012-03-26 18:01:01 +00:00
|
|
|
d->urlBookmarks[vp.pageNumber]--;
|
2010-01-09 13:13:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (referurl == d->document->m_url) {
|
2012-03-26 18:01:01 +00:00
|
|
|
for (int i = 0; i < qMax(oldUrlBookmarks.size(), d->urlBookmarks.size()); i++) {
|
|
|
|
bool oldContains = oldUrlBookmarks.contains(i) && oldUrlBookmarks[i] > 0;
|
|
|
|
bool curContains = d->urlBookmarks.contains(i) && d->urlBookmarks[i] > 0;
|
|
|
|
|
|
|
|
if (oldContains != curContains) {
|
|
|
|
foreachObserver(notifyPageChanged(i, DocumentObserver::Bookmark));
|
|
|
|
} else if (oldContains && oldUrlBookmarks[i] != d->urlBookmarks[i]) {
|
|
|
|
foreachObserver(notifyPageChanged(i, DocumentObserver::Bookmark));
|
|
|
|
}
|
2010-01-09 13:13:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (deletedAny) {
|
2023-12-05 15:48:45 +00:00
|
|
|
d->manager.emitChanged(thebg);
|
2022-03-08 10:10:43 +00:00
|
|
|
}
|
2010-01-09 13:13:43 +00:00
|
|
|
}
|
|
|
|
|
2020-07-22 23:18:59 +00:00
|
|
|
QList<QAction *> BookmarkManager::actionsForUrl(const QUrl &documentUrl) const
|
2007-01-13 17:28:54 +00:00
|
|
|
{
|
2020-07-22 23:18:59 +00:00
|
|
|
const QUrl url = mostCanonicalUrl(documentUrl);
|
2007-01-13 17:28:54 +00:00
|
|
|
QList<QAction *> ret;
|
2023-12-05 15:48:45 +00:00
|
|
|
KBookmarkGroup group = d->manager.root();
|
2007-01-13 17:28:54 +00:00
|
|
|
for (KBookmark bm = group.first(); !bm.isNull(); bm = group.next(bm)) {
|
2010-03-27 18:00:48 +00:00
|
|
|
if (!bm.isGroup() || urlForGroup(bm) != url) {
|
2007-01-13 17:28:54 +00:00
|
|
|
continue;
|
2022-03-08 10:10:43 +00:00
|
|
|
}
|
2007-01-13 17:28:54 +00:00
|
|
|
|
|
|
|
KBookmarkGroup group = bm.toGroup();
|
|
|
|
for (KBookmark b = group.first(); !b.isNull(); b = group.next(b)) {
|
|
|
|
if (b.isSeparator() || b.isGroup()) {
|
|
|
|
continue;
|
2022-03-08 10:10:43 +00:00
|
|
|
}
|
2007-01-13 17:28:54 +00:00
|
|
|
|
2017-09-05 21:27:18 +00:00
|
|
|
ret.append(new OkularBookmarkAction(DocumentViewport(b.url().fragment(QUrl::FullyDecoded)), b, d, nullptr));
|
2007-01-13 17:28:54 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2019-03-12 12:13:53 +00:00
|
|
|
std::sort(ret.begin(), ret.end(), okularBookmarkActionLessThan);
|
2007-01-13 17:28:54 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-01-29 19:55:57 +00:00
|
|
|
void BookmarkManager::setUrl(const QUrl &url)
|
2006-12-27 16:04:49 +00:00
|
|
|
{
|
2020-07-22 23:18:59 +00:00
|
|
|
d->url = mostCanonicalUrl(url);
|
2006-12-27 16:04:49 +00:00
|
|
|
d->urlBookmarks.clear();
|
2009-12-26 16:57:09 +00:00
|
|
|
KBookmarkGroup thebg;
|
2020-07-22 23:18:59 +00:00
|
|
|
QHash<QUrl, QString>::iterator it = d->bookmarkFind(d->url, false, &thebg);
|
2006-12-27 16:04:49 +00:00
|
|
|
if (it != d->knownFiles.end()) {
|
2009-12-26 16:57:09 +00:00
|
|
|
for (KBookmark bm = thebg.first(); !bm.isNull(); bm = thebg.next(bm)) {
|
2006-12-27 16:04:49 +00:00
|
|
|
if (bm.isSeparator() || bm.isGroup()) {
|
|
|
|
continue;
|
2022-03-08 10:10:43 +00:00
|
|
|
}
|
2006-12-27 16:04:49 +00:00
|
|
|
|
2014-08-08 00:00:17 +00:00
|
|
|
DocumentViewport vp(bm.url().fragment(QUrl::FullyDecoded));
|
2006-12-27 16:04:49 +00:00
|
|
|
if (!vp.isValid()) {
|
|
|
|
continue;
|
2022-03-08 10:10:43 +00:00
|
|
|
}
|
2006-12-27 16:04:49 +00:00
|
|
|
|
2012-03-26 18:01:01 +00:00
|
|
|
d->urlBookmarks[vp.pageNumber]++;
|
2006-12-27 16:04:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BookmarkManager::setPageBookmark(int page)
|
|
|
|
{
|
2009-12-26 16:57:09 +00:00
|
|
|
KBookmarkGroup thebg;
|
2015-01-29 19:55:57 +00:00
|
|
|
QHash<QUrl, QString>::iterator it = d->bookmarkFind(d->url, true, &thebg);
|
2006-12-27 16:04:49 +00:00
|
|
|
Q_ASSERT(it != d->knownFiles.end());
|
|
|
|
|
|
|
|
bool found = false;
|
|
|
|
bool added = false;
|
2009-12-26 16:57:09 +00:00
|
|
|
for (KBookmark bm = thebg.first(); !found && !bm.isNull(); bm = thebg.next(bm)) {
|
2006-12-27 16:04:49 +00:00
|
|
|
if (bm.isSeparator() || bm.isGroup()) {
|
|
|
|
continue;
|
2022-03-08 10:10:43 +00:00
|
|
|
}
|
2006-12-27 16:04:49 +00:00
|
|
|
|
2014-08-08 00:00:17 +00:00
|
|
|
DocumentViewport vp(bm.url().fragment(QUrl::FullyDecoded));
|
2006-12-27 16:04:49 +00:00
|
|
|
if (vp.isValid() && vp.pageNumber == page) {
|
|
|
|
found = true;
|
2022-03-08 10:10:43 +00:00
|
|
|
}
|
2006-12-27 16:04:49 +00:00
|
|
|
}
|
|
|
|
if (!found) {
|
2012-03-26 18:01:01 +00:00
|
|
|
d->urlBookmarks[page]++;
|
2006-12-27 16:04:49 +00:00
|
|
|
DocumentViewport vp;
|
|
|
|
vp.pageNumber = page;
|
2015-01-29 19:55:57 +00:00
|
|
|
QUrl newurl = d->url;
|
|
|
|
newurl.setFragment(vp.toString(), QUrl::DecodedMode);
|
2015-10-29 12:37:11 +00:00
|
|
|
thebg.addBookmark(QLatin1String("#") + QString::number(vp.pageNumber + 1), newurl, QString());
|
2006-12-27 16:04:49 +00:00
|
|
|
added = true;
|
2023-12-05 15:48:45 +00:00
|
|
|
d->manager.emitChanged(thebg);
|
2006-12-27 16:04:49 +00:00
|
|
|
}
|
|
|
|
return added;
|
|
|
|
}
|
|
|
|
|
2007-01-13 14:45:51 +00:00
|
|
|
bool BookmarkManager::removePageBookmark(int page)
|
|
|
|
{
|
2009-12-26 16:57:09 +00:00
|
|
|
KBookmarkGroup thebg;
|
2015-01-29 19:55:57 +00:00
|
|
|
QHash<QUrl, QString>::iterator it = d->bookmarkFind(d->url, false, &thebg);
|
2007-01-13 14:45:51 +00:00
|
|
|
if (it == d->knownFiles.end()) {
|
|
|
|
return false;
|
2022-03-08 10:10:43 +00:00
|
|
|
}
|
2007-01-13 14:45:51 +00:00
|
|
|
|
|
|
|
bool found = false;
|
2009-12-26 16:57:09 +00:00
|
|
|
for (KBookmark bm = thebg.first(); !found && !bm.isNull(); bm = thebg.next(bm)) {
|
2007-01-13 14:45:51 +00:00
|
|
|
if (bm.isSeparator() || bm.isGroup()) {
|
|
|
|
continue;
|
2022-03-08 10:10:43 +00:00
|
|
|
}
|
2007-01-13 14:45:51 +00:00
|
|
|
|
2014-08-08 00:00:17 +00:00
|
|
|
DocumentViewport vp(bm.url().fragment(QUrl::FullyDecoded));
|
2007-01-13 14:45:51 +00:00
|
|
|
if (vp.isValid() && vp.pageNumber == page) {
|
|
|
|
found = true;
|
2009-12-26 16:57:09 +00:00
|
|
|
thebg.deleteBookmark(bm);
|
2012-03-26 18:01:01 +00:00
|
|
|
d->urlBookmarks[page]--;
|
2023-12-05 15:48:45 +00:00
|
|
|
d->manager.emitChanged(thebg);
|
2007-01-13 14:45:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2007-10-28 18:31:33 +00:00
|
|
|
bool BookmarkManager::isBookmarked(int page) const
|
2006-12-27 16:04:49 +00:00
|
|
|
{
|
2012-03-26 18:01:01 +00:00
|
|
|
return d->urlBookmarks.contains(page) && d->urlBookmarks[page] > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BookmarkManager::isBookmarked(const DocumentViewport &viewport) const
|
|
|
|
{
|
|
|
|
KBookmark bm = bookmark(viewport);
|
|
|
|
|
|
|
|
return !bm.isNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
KBookmark BookmarkManager::nextBookmark(const DocumentViewport &viewport) const
|
|
|
|
{
|
|
|
|
KBookmark::List bmarks = bookmarks();
|
2019-03-12 12:13:53 +00:00
|
|
|
std::sort(bmarks.begin(), bmarks.end(), bookmarkLessThan);
|
2012-03-26 18:01:01 +00:00
|
|
|
|
|
|
|
KBookmark bookmark;
|
2023-12-19 08:58:22 +00:00
|
|
|
for (const KBookmark &bm : std::as_const(bmarks)) {
|
2014-08-08 00:00:17 +00:00
|
|
|
DocumentViewport vp(bm.url().fragment(QUrl::FullyDecoded));
|
2012-06-04 14:10:33 +00:00
|
|
|
if (viewport < vp) {
|
2012-03-26 18:01:01 +00:00
|
|
|
bookmark = bm;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bookmark;
|
|
|
|
}
|
|
|
|
|
|
|
|
KBookmark BookmarkManager::previousBookmark(const DocumentViewport &viewport) const
|
|
|
|
{
|
|
|
|
KBookmark::List bmarks = bookmarks();
|
2019-03-12 12:13:53 +00:00
|
|
|
std::sort(bmarks.begin(), bmarks.end(), bookmarkLessThan);
|
2012-03-26 18:01:01 +00:00
|
|
|
|
|
|
|
KBookmark bookmark;
|
2012-03-27 11:25:12 +00:00
|
|
|
for (KBookmark::List::const_iterator it = bmarks.constEnd(); it != bmarks.constBegin(); --it) {
|
2012-03-26 18:01:01 +00:00
|
|
|
KBookmark bm = *(it - 1);
|
2014-08-08 00:00:17 +00:00
|
|
|
DocumentViewport vp(bm.url().fragment(QUrl::FullyDecoded));
|
2012-06-04 14:10:33 +00:00
|
|
|
if (vp < viewport) {
|
2012-03-26 18:01:01 +00:00
|
|
|
bookmark = bm;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bookmark;
|
2006-12-27 16:04:49 +00:00
|
|
|
}
|
|
|
|
|
2009-01-10 11:32:54 +00:00
|
|
|
#undef foreachObserver
|
2009-12-26 16:57:09 +00:00
|
|
|
#undef foreachObserverD
|
2009-01-10 11:32:54 +00:00
|
|
|
|
2020-02-21 15:25:29 +00:00
|
|
|
#include "bookmarkmanager.moc"
|
|
|
|
|
2010-03-27 18:00:48 +00:00
|
|
|
/* kate: replace-tabs on; indent-width 4; */
|