1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2007 Urs Wolfer <uwolfer @ kde.org> |
4 | ** |
5 | ** This file is part of KDE. |
6 | ** |
7 | ** This program is free software; you can redistribute it and/or modify |
8 | ** it under the terms of the GNU General Public License as published by |
9 | ** the Free Software Foundation; either version 2 of the License, or |
10 | ** (at your option) any later version. |
11 | ** |
12 | ** This program is distributed in the hope that it will be useful, |
13 | ** but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | ** GNU General Public License for more details. |
16 | ** |
17 | ** You should have received a copy of the GNU General Public License |
18 | ** along with this program; see the file COPYING. If not, write to |
19 | ** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
20 | ** Boston, MA 02110-1301, USA. |
21 | ** |
22 | ****************************************************************************/ |
23 | |
24 | #include "bookmarkmanager.h" |
25 | |
26 | #include "mainwindow.h" |
27 | |
28 | #include <KBookmarkMenu> |
29 | #include <KStandardDirs> |
30 | #include <KDebug> |
31 | |
32 | BookmarkManager::BookmarkManager(KActionCollection *collection, KMenu *, MainWindow *parent) |
33 | : QObject(parent), |
34 | KBookmarkOwner(), |
35 | m_mainWindow(parent) |
36 | { |
37 | const QString file = KStandardDirs::locateLocal("data" , "krdc/bookmarks.xml" ); |
38 | |
39 | m_manager = KBookmarkManager::managerForFile(file, "krdc" ); |
40 | m_manager->setUpdate(true); |
41 | m_bookmarkMenu = new KBookmarkMenu(m_manager, this, menu, collection); |
42 | |
43 | KBookmarkGroup root = m_manager->root(); |
44 | KBookmark bm = root.first(); |
45 | while (!bm.isNull()) { |
46 | if (bm.metaDataItem("krdc-history" ) == "historyfolder" ) // get it also when user renamed it |
47 | break; |
48 | bm = root.next(bm); |
49 | } |
50 | |
51 | if (bm.isNull()) { |
52 | kDebug(5010) << "History folder not found. Create it." ; |
53 | bm = m_manager->root().createNewFolder(i18n("History" )); |
54 | bm.setMetaDataItem("krdc-history" , "historyfolder" ); |
55 | m_manager->emitChanged(); |
56 | } |
57 | |
58 | m_historyGroup = bm.toGroup(); |
59 | } |
60 | |
61 | BookmarkManager::~BookmarkManager() |
62 | { |
63 | delete m_bookmarkMenu; |
64 | } |
65 | |
66 | void BookmarkManager::addHistoryBookmark(RemoteView *view) |
67 | { |
68 | KBookmark bm = m_historyGroup.first(); |
69 | const QString urlString = urlForView(view); |
70 | const KUrl url = KUrl(urlString); |
71 | while (!bm.isNull()) { |
72 | if (bm.url() == url) { |
73 | kDebug(5010) << "Found URL. Move it at the history start." ; |
74 | m_historyGroup.moveBookmark(bm, KBookmark()); |
75 | bm.updateAccessMetadata(); |
76 | m_manager->emitChanged(m_historyGroup); |
77 | return; |
78 | } |
79 | bm = m_historyGroup.next(bm); |
80 | } |
81 | |
82 | if (bm.isNull()) { |
83 | kDebug(5010) << "Add new history bookmark." ; |
84 | bm = m_historyGroup.addBookmark(titleForUrl(urlString), urlString); |
85 | bm.updateAccessMetadata(); |
86 | m_historyGroup.moveBookmark(bm, KBookmark()); |
87 | m_manager->emitChanged(m_historyGroup); |
88 | } |
89 | } |
90 | |
91 | void BookmarkManager::openBookmark(const KBookmark &bm, Qt::MouseButtons, Qt::KeyboardModifiers) |
92 | { |
93 | emit openUrl(bm.url()); |
94 | } |
95 | |
96 | void BookmarkManager::openFolderinTabs(const KBookmarkGroup &bookmarkGroup) |
97 | { |
98 | KBookmark bm = bookmarkGroup.first(); |
99 | while (!bm.isNull()) { |
100 | emit openUrl(bm.url()); |
101 | bm = bookmarkGroup.next(bm); |
102 | } |
103 | } |
104 | |
105 | bool BookmarkManager::addBookmarkEntry() const |
106 | { |
107 | return true; |
108 | } |
109 | |
110 | bool BookmarkManager::editBookmarkEntry() const |
111 | { |
112 | return true; |
113 | } |
114 | |
115 | QString BookmarkManager::currentUrl() const |
116 | { |
117 | RemoteView *view = m_mainWindow->currentRemoteView(); |
118 | if (view) |
119 | return urlForView(view); |
120 | else |
121 | return QString(); |
122 | } |
123 | |
124 | QString BookmarkManager::urlForView(RemoteView *view) const |
125 | { |
126 | return view->url().prettyUrl(KUrl::RemoveTrailingSlash); |
127 | |
128 | } |
129 | |
130 | QString BookmarkManager::currentTitle() const |
131 | { |
132 | return titleForUrl(currentUrl()); |
133 | } |
134 | |
135 | QString BookmarkManager::titleForUrl(const QString &url) const |
136 | { |
137 | return QUrl::fromPercentEncoding(url.toUtf8()); |
138 | |
139 | } |
140 | |
141 | bool BookmarkManager::supportsTabs() const |
142 | { |
143 | return true; |
144 | } |
145 | |
146 | QList<QPair<QString, QString> > BookmarkManager::currentBookmarkList() const |
147 | { |
148 | QList<QPair<QString, QString> > list; |
149 | |
150 | QMapIterator<QWidget *, RemoteView *> iter(m_mainWindow->remoteViewList()); |
151 | |
152 | while (iter.hasNext()) { |
153 | RemoteView *next = iter.next().value(); |
154 | const QString url = next->url().prettyUrl(KUrl::RemoveTrailingSlash); |
155 | list << QPair<QString, QString>(url, url); |
156 | } |
157 | |
158 | return list; |
159 | } |
160 | |
161 | void BookmarkManager::addManualBookmark(const QString &url, const QString &text) |
162 | { |
163 | m_manager->root().addBookmark(url, text); |
164 | emit m_manager->emitChanged(); |
165 | } |
166 | |
167 | KBookmarkManager* BookmarkManager::getManager() |
168 | { |
169 | return m_manager; |
170 | } |
171 | |
172 | const QStringList BookmarkManager::findBookmarkAddresses(const KBookmarkGroup &group, const QString &url) |
173 | { |
174 | QStringList bookmarkAddresses = QStringList(); |
175 | KBookmark bm = group.first(); |
176 | while (!bm.isNull()) { |
177 | if (bm.isGroup()) { |
178 | bookmarkAddresses.append(findBookmarkAddresses(bm.toGroup(), url)); |
179 | } |
180 | |
181 | if (bm.url() == url) { |
182 | bookmarkAddresses.append(bm.address()); |
183 | } |
184 | bm = group.next(bm); |
185 | } |
186 | return bookmarkAddresses; |
187 | } |
188 | |
189 | void BookmarkManager::removeByUrl(KBookmarkManager *manager, const QString &url, bool ignoreHistory, const QString updateTitle) |
190 | { |
191 | foreach(const QString &address, findBookmarkAddresses(manager->root(), url)) { |
192 | KBookmark bm = manager->findByAddress(address); |
193 | if (ignoreHistory && bm.parentGroup().metaDataItem("krdc-history" ) == "historyfolder" ) { |
194 | if (!updateTitle.isEmpty()) { |
195 | kDebug(5010) << "Update" << bm.fullText(); |
196 | bm.setFullText(updateTitle); |
197 | } |
198 | } else { |
199 | if (!bm.isGroup()) { // please don't delete groups... happened in testing |
200 | kDebug(5010) << "Delete" << bm.fullText(); |
201 | bm.parentGroup().deleteBookmark(bm); |
202 | } |
203 | } |
204 | } |
205 | |
206 | manager->emitChanged(); |
207 | } |
208 | |
209 | void BookmarkManager::updateTitle(KBookmarkManager *manager, const QString &url, const QString &title) |
210 | { |
211 | foreach(const QString &address, findBookmarkAddresses(manager->root(), url)) { |
212 | KBookmark bm = manager->findByAddress(address); |
213 | bm.setFullText(title); |
214 | kDebug(5010) << "Update" << bm.fullText(); |
215 | } |
216 | manager->emitChanged(); |
217 | } |
218 | |
219 | #include "bookmarkmanager.moc" |
220 | |