]> cloud.milkyroute.net Git - dolphin.git/blob - src/bookmarkssidebarpage.cpp
commited initial version of Dolphin
[dolphin.git] / src / bookmarkssidebarpage.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
18 ***************************************************************************/
19
20 #include "bookmarkssidebarpage.h"
21
22 #include <q3listbox.h>
23 #include <qlayout.h>
24 #include <qpainter.h>
25 //Added by qt3to4:
26 #include <QPixmap>
27 #include <Q3VBoxLayout>
28 #include <QPaintEvent>
29 #include <assert.h>
30 #include <q3popupmenu.h>
31
32 #include <kbookmark.h>
33 #include <kbookmarkmanager.h>
34 #include <kmessagebox.h>
35 #include <kiconloader.h>
36 #include <klocale.h>
37
38 #include "dolphinsettings.h"
39 #include "dolphin.h"
40 #include "dolphinview.h"
41 #include "editbookmarkdialog.h"
42
43 BookmarksSidebarPage::BookmarksSidebarPage(QWidget* parent) :
44 SidebarPage(parent)
45 {
46 Q3VBoxLayout* layout = new Q3VBoxLayout(this);
47 m_bookmarksList = new BookmarksListBox(this);
48 m_bookmarksList->setPaletteBackgroundColor(colorGroup().background());
49
50 layout->addWidget(m_bookmarksList);
51 connect(m_bookmarksList, SIGNAL(mouseButtonClicked(int, Q3ListBoxItem*, const QPoint&)),
52 this, SLOT(slotMouseButtonClicked(int, Q3ListBoxItem*)));
53 connect(m_bookmarksList, SIGNAL(contextMenuRequested(Q3ListBoxItem*, const QPoint&)),
54 this, SLOT(slotContextMenuRequested(Q3ListBoxItem*, const QPoint&)));
55
56 KBookmarkManager* manager = DolphinSettings::instance().bookmarkManager();
57 connect(manager, SIGNAL(changed(const QString&, const QString&)),
58 this, SLOT(updateBookmarks()));
59
60 updateBookmarks();
61 }
62
63 BookmarksSidebarPage::~BookmarksSidebarPage()
64 {
65 }
66
67 void BookmarksSidebarPage::activeViewChanged()
68 {
69 connectToActiveView();
70 }
71
72 void BookmarksSidebarPage::updateBookmarks()
73 {
74 m_bookmarksList->clear();
75
76 KIconLoader iconLoader;
77
78 KBookmarkGroup root = DolphinSettings::instance().bookmarkManager()->root();
79 KBookmark bookmark = root.first();
80 while (!bookmark.isNull()) {
81 QPixmap icon(iconLoader.loadIcon(bookmark.icon(),
82 KIcon::NoGroup,
83 KIcon::SizeMedium));
84 BookmarkItem* item = new BookmarkItem(icon, bookmark.text());
85 m_bookmarksList->insertItem(item);
86
87 bookmark = root.next(bookmark);
88 }
89
90 connectToActiveView();
91 }
92
93 void BookmarksSidebarPage::slotMouseButtonClicked(int button, Q3ListBoxItem* item)
94 {
95 if ((button != Qt::LeftButton) || (item == 0)) {
96 return;
97 }
98
99 const int index = m_bookmarksList->index(item);
100 KBookmark bookmark = DolphinSettings::instance().bookmark(index);
101 Dolphin::mainWin().activeView()->setURL(bookmark.url());
102 }
103
104 void BookmarksSidebarPage::slotContextMenuRequested(Q3ListBoxItem* item,
105 const QPoint& pos)
106 {
107 const int insertID = 1;
108 const int editID = 2;
109 const int deleteID = 3;
110 const int addID = 4;
111
112 Q3PopupMenu* popup = new Q3PopupMenu();
113 if (item == 0) {
114 popup->insertItem(SmallIcon("filenew"), i18n("Add Bookmark..."), addID);
115 }
116 else {
117 popup->insertItem(SmallIcon("filenew"), i18n("Insert Bookmark..."), insertID);
118 popup->insertItem(SmallIcon("edit"), i18n("Edit..."), editID);
119 popup->insertItem(SmallIcon("editdelete"), i18n("Delete"), deleteID);
120 }
121
122 KBookmarkManager* manager = DolphinSettings::instance().bookmarkManager();
123 KBookmarkGroup root = manager->root();
124 const int index = m_bookmarksList->index(m_bookmarksList->selectedItem());
125
126 const int result = popup->exec(pos);
127 switch (result) {
128 case insertID: {
129 KBookmark newBookmark = EditBookmarkDialog::getBookmark(i18n("Insert Bookmark"),
130 "New bookmark",
131 KURL(),
132 "bookmark");
133 if (!newBookmark.isNull()) {
134 root.addBookmark(manager, newBookmark);
135 if (index > 0) {
136 KBookmark prevBookmark = DolphinSettings::instance().bookmark(index - 1);
137 root.moveItem(newBookmark, prevBookmark);
138 }
139 else {
140 // insert bookmark at first position (is a little bit tricky as KBookmarkGroup
141 // only allows to move items after existing items)
142 KBookmark firstBookmark = root.first();
143 root.moveItem(newBookmark, firstBookmark);
144 root.moveItem(firstBookmark, newBookmark);
145 }
146 manager->emitChanged(root);
147 }
148 break;
149 }
150
151 case editID: {
152 KBookmark oldBookmark = DolphinSettings::instance().bookmark(index);
153 KBookmark newBookmark = EditBookmarkDialog::getBookmark(i18n("Edit Bookmark"),
154 oldBookmark.text(),
155 oldBookmark.url(),
156 oldBookmark.icon());
157 if (!newBookmark.isNull()) {
158 root.addBookmark(manager, newBookmark);
159 root.moveItem(newBookmark, oldBookmark);
160 root.deleteBookmark(oldBookmark);
161 manager->emitChanged(root);
162 }
163 break;
164 }
165
166 case deleteID: {
167 KBookmark bookmark = DolphinSettings::instance().bookmark(index);
168 root.deleteBookmark(bookmark);
169 manager->emitChanged(root);
170 break;
171 }
172
173 case addID: {
174 KBookmark bookmark = EditBookmarkDialog::getBookmark(i18n("Add Bookmark"),
175 "New bookmark",
176 KURL(),
177 "bookmark");
178 if (!bookmark.isNull()) {
179 root.addBookmark(manager, bookmark);
180 manager->emitChanged(root);
181 }
182 }
183
184 default: break;
185 }
186
187 delete popup;
188 popup = 0;
189
190 DolphinView* view = Dolphin::mainWin().activeView();
191 adjustSelection(view->url());
192 }
193
194
195 void BookmarksSidebarPage::adjustSelection(const KURL& url)
196 {
197 // TODO (remarked in dolphin/TODO): the following code is quite equal
198 // to BookmarkSelector::updateSelection().
199
200 KBookmarkGroup root = DolphinSettings::instance().bookmarkManager()->root();
201 KBookmark bookmark = root.first();
202
203 int maxLength = 0;
204 int selectedIndex = -1;
205
206 // Search the bookmark which is equal to the URL or at least is a parent URL.
207 // If there are more than one possible parent URL candidates, choose the bookmark
208 // which covers the bigger range of the URL.
209 int i = 0;
210 while (!bookmark.isNull()) {
211 const KURL bookmarkURL = bookmark.url();
212 if (bookmarkURL.isParentOf(url)) {
213 const int length = bookmarkURL.prettyURL().length();
214 if (length > maxLength) {
215 selectedIndex = i;
216 maxLength = length;
217 }
218 }
219 bookmark = root.next(bookmark);
220 ++i;
221 }
222
223 const bool block = m_bookmarksList->signalsBlocked();
224 m_bookmarksList->blockSignals(true);
225 if (selectedIndex < 0) {
226 // no bookmark matches, hence deactivate any selection
227 const int currentIndex = m_bookmarksList->index(m_bookmarksList->selectedItem());
228 m_bookmarksList->setSelected(currentIndex, false);
229 }
230 else {
231 // select the bookmark which is part of the current URL
232 m_bookmarksList->setSelected(selectedIndex, true);
233 }
234 m_bookmarksList->blockSignals(block);
235 }
236
237 void BookmarksSidebarPage::slotURLChanged(const KURL& url)
238 {
239 adjustSelection(url);
240 }
241
242 void BookmarksSidebarPage::connectToActiveView()
243 {
244 DolphinView* view = Dolphin::mainWin().activeView();
245 adjustSelection(view->url());
246 connect(view, SIGNAL(signalURLChanged(const KURL&)),
247 this, SLOT(slotURLChanged(const KURL&)));
248 }
249
250 BookmarksListBox::BookmarksListBox(QWidget* parent) :
251 Q3ListBox(parent)
252 {
253 }
254 BookmarksListBox::~BookmarksListBox()
255 {
256 }
257
258 void BookmarksListBox::paintEvent(QPaintEvent* /* event */)
259 {
260 // don't invoke QListBox::paintEvent(event) to prevent
261 // that any kind of frame is drawn
262 }
263
264 BookmarkItem::BookmarkItem(const QPixmap& pixmap, const QString& text) :
265 Q3ListBoxPixmap(pixmap, text)
266 {
267 }
268
269 BookmarkItem::~BookmarkItem()
270 {
271 }
272
273 int BookmarkItem::height(const Q3ListBox* listBox) const
274 {
275 return Q3ListBoxPixmap::height(listBox) + 8;
276 }
277