File: bookmarkmanager.cpp

package info (click to toggle)
krdc 4%3A25.04.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,124 kB
  • sloc: cpp: 8,746; xml: 304; makefile: 6; sh: 5
file content (203 lines) | stat: -rw-r--r-- 6,062 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
    SPDX-FileCopyrightText: 2007 Urs Wolfer <uwolfer@kde.org>

    SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "bookmarkmanager.h"
#include "krdc_debug.h"
#include "mainwindow.h"

#include <KLocalizedString>

#include <QStandardPaths>

BookmarkManager::BookmarkManager(KActionCollection *collection, QMenu *menu, MainWindow *parent)
    : QObject(parent)
    , KBookmarkOwner()
    , m_mainWindow(parent)
{
    const QString dir = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QString(), QStandardPaths::LocateOption::LocateDirectory);
    const QString file = dir + QLatin1String("krdc/bookmarks.xml");

    m_manager = new KBookmarkManager(file, this);
    m_bookmarkMenu = new KBookmarkMenu(m_manager, this, menu);
    collection->addActions(menu->actions());

    KBookmarkGroup root = m_manager->root();
    KBookmark bm = root.first();
    while (!bm.isNull()) {
        if (bm.metaDataItem(QLatin1String("krdc-history")) == QLatin1String("historyfolder")) // get it also when user renamed it
            break;
        bm = root.next(bm);
    }

    if (bm.isNull()) {
        qCDebug(KRDC) << "History folder not found. Create it.";
        bm = m_manager->root().createNewFolder(i18n("History"));
        bm.setMetaDataItem(QStringLiteral("krdc-history"), QStringLiteral("historyfolder"));
        m_manager->emitChanged();
    }

    m_historyGroup = bm.toGroup();
}

BookmarkManager::~BookmarkManager()
{
    delete m_bookmarkMenu;
}

void BookmarkManager::addHistoryBookmark(RemoteView *view)
{
    KBookmark bm = m_historyGroup.first();
    const QString urlString = urlForView(view);
    const QUrl url = QUrl::fromUserInput(urlString);
    while (!bm.isNull()) {
        if (bm.url() == url) {
            qCDebug(KRDC) << "Found URL. Move it at the history start.";
            m_historyGroup.moveBookmark(bm, KBookmark());
            bm.updateAccessMetadata();
            m_manager->emitChanged(m_historyGroup);
            return;
        }
        bm = m_historyGroup.next(bm);
    }

    if (bm.isNull()) {
        qCDebug(KRDC) << "Add new history bookmark.";
        bm = m_historyGroup.addBookmark(titleForUrl(url), url, QString());
        bm.updateAccessMetadata();
        m_historyGroup.moveBookmark(bm, KBookmark());
        m_manager->emitChanged(m_historyGroup);
    }
}

void BookmarkManager::openBookmark(const KBookmark &bm, Qt::MouseButtons, Qt::KeyboardModifiers)
{
    Q_EMIT openUrl(bm.url());
}

void BookmarkManager::openFolderinTabs(const KBookmarkGroup &bookmarkGroup)
{
    KBookmark bm = bookmarkGroup.first();
    while (!bm.isNull()) {
        Q_EMIT openUrl(bm.url());
        bm = bookmarkGroup.next(bm);
    }
}

bool BookmarkManager::addBookmarkEntry() const
{
    return true;
}

bool BookmarkManager::editBookmarkEntry() const
{
    return true;
}

QUrl BookmarkManager::currentUrl() const
{
    RemoteView *view = m_mainWindow->currentRemoteView();
    if (view)
        return QUrl(urlForView(view));
    else
        return QUrl();
}

QString BookmarkManager::urlForView(RemoteView *view) const
{
    return view->url().toDisplayString(QUrl::UrlFormattingOption::StripTrailingSlash);
}

QString BookmarkManager::currentTitle() const
{
    return titleForUrl(currentUrl());
}

QString BookmarkManager::titleForUrl(const QUrl &url) const
{
    return url.toDisplayString(QUrl::UrlFormattingOption::StripTrailingSlash);
}

bool BookmarkManager::supportsTabs() const
{
    return true;
}

QList<KBookmarkOwner::FutureBookmark> BookmarkManager::currentBookmarkList() const
{
    QList<KBookmarkOwner::FutureBookmark> list;

    QMapIterator<QWidget *, RemoteView *> iter(m_mainWindow->remoteViewList());

    while (iter.hasNext()) {
        RemoteView *next = iter.next().value();
        const QUrl url = next->url();
        const QString title = titleForUrl(url);
        KBookmarkOwner::FutureBookmark bookmark = KBookmarkOwner::FutureBookmark(title, url, QString());
        list.append(bookmark);
    }

    return list;
}

void BookmarkManager::addManualBookmark(const QUrl &url, const QString &text)
{
    m_manager->root().addBookmark(text, url, QString());
    m_manager->emitChanged();
}

KBookmarkManager *BookmarkManager::getManager()
{
    return m_manager;
}

const QStringList BookmarkManager::findBookmarkAddresses(const KBookmarkGroup &group, const QString &url)
{
    QStringList bookmarkAddresses = QStringList();
    KBookmark bm = group.first();
    while (!bm.isNull()) {
        if (bm.isGroup()) {
            bookmarkAddresses.append(findBookmarkAddresses(bm.toGroup(), url));
        }

        if (bm.url() == QUrl::fromUserInput(url)) {
            bookmarkAddresses.append(bm.address());
        }
        bm = group.next(bm);
    }
    return bookmarkAddresses;
}

void BookmarkManager::removeByUrl(KBookmarkManager *manager, const QString &url, bool ignoreHistory, const QString &updateTitle)
{
    const QStringList addresses = findBookmarkAddresses(manager->root(), url);
    for (const QString &address : addresses) {
        KBookmark bm = manager->findByAddress(address);
        if (ignoreHistory && bm.parentGroup().metaDataItem(QStringLiteral("krdc-history")) == QLatin1String("historyfolder")) {
            if (!updateTitle.isEmpty()) {
                qCDebug(KRDC) << "Update" << bm.fullText();
                bm.setFullText(updateTitle);
            }
        } else {
            if (!bm.isGroup()) { // please don't delete groups... happened in testing
                qCDebug(KRDC) << "Delete" << bm.fullText();
                bm.parentGroup().deleteBookmark(bm);
            }
        }
    }

    manager->emitChanged();
}

void BookmarkManager::updateTitle(KBookmarkManager *manager, const QString &url, const QString &title)
{
    const QStringList addresses = findBookmarkAddresses(manager->root(), url);
    for (const QString &address : addresses) {
        KBookmark bm = manager->findByAddress(address);
        bm.setFullText(title);
        qCDebug(KRDC) << "Update" << bm.fullText();
    }
    manager->emitChanged();
}