]> cloud.milkyroute.net Git - dolphin.git/blob - src/bookmarkssidebarpage.cpp
adaptions to the cleaned up interface of KUrlNavigator
[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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 <kmenu.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 "editbookmarkdialog.h"
40
41 BookmarksSidebarPage::BookmarksSidebarPage(QWidget* parent) :
42 SidebarPage(parent)
43 {
44 Q3VBoxLayout* layout = new Q3VBoxLayout(this);
45 m_bookmarksList = new BookmarksListBox(this);
46 m_bookmarksList->setPaletteBackgroundColor(palette().brush(QPalette::Background).color());
47
48 layout->addWidget(m_bookmarksList);
49 connect(m_bookmarksList, SIGNAL(mouseButtonClicked(int, Q3ListBoxItem*, const QPoint&)),
50 this, SLOT(slotMouseButtonClicked(int, Q3ListBoxItem*)));
51 connect(m_bookmarksList, SIGNAL(contextMenuRequested(Q3ListBoxItem*, const QPoint&)),
52 this, SLOT(slotContextMenuRequested(Q3ListBoxItem*, const QPoint&)));
53
54 KBookmarkManager* manager = DolphinSettings::instance().bookmarkManager();
55 connect(manager, SIGNAL(changed(const QString&, const QString&)),
56 this, SLOT(updateBookmarks()));
57
58 updateBookmarks();
59 }
60
61 BookmarksSidebarPage::~BookmarksSidebarPage()
62 {
63 }
64
65 void BookmarksSidebarPage::setUrl(const KUrl& url)
66 {
67 if (!m_url.equals(url, KUrl::CompareWithoutTrailingSlash)) {
68 m_url = url;
69 adjustSelection(m_url);
70 }
71 }
72
73 void BookmarksSidebarPage::updateBookmarks()
74 {
75 m_bookmarksList->clear();
76
77 KIconLoader iconLoader;
78
79 KBookmarkGroup root = DolphinSettings::instance().bookmarkManager()->root();
80 KBookmark bookmark = root.first();
81 while (!bookmark.isNull()) {
82 QPixmap icon(iconLoader.loadIcon(bookmark.icon(),
83 K3Icon::NoGroup,
84 K3Icon::SizeMedium));
85 BookmarkItem* item = new BookmarkItem(icon, bookmark.text());
86 m_bookmarksList->insertItem(item);
87
88 bookmark = root.next(bookmark);
89 }
90 }
91
92 void BookmarksSidebarPage::slotMouseButtonClicked(int button, Q3ListBoxItem* item)
93 {
94 if ((button != Qt::LeftButton) || (item == 0)) {
95 return;
96 }
97
98 const int index = m_bookmarksList->index(item);
99 KBookmark bookmark = DolphinSettings::instance().bookmark(index);
100 emit changeUrl(bookmark.url());
101 }
102
103 void BookmarksSidebarPage::slotContextMenuRequested(Q3ListBoxItem* item,
104 const QPoint& pos)
105 {
106 const int insertID = 1;
107 const int editID = 2;
108 const int deleteID = 3;
109 const int addID = 4;
110
111 KMenu* popup = new KMenu();
112 if (item == 0) {
113 QAction *action = popup->addAction(KIcon("document-new"), i18n("Add Bookmark..."));
114 action->setData(addID);
115 }
116 else {
117 QAction *action = popup->addAction(KIcon("document-new"), i18n("Insert Bookmark..."));
118 action->setData(insertID);
119 action = popup->addAction(KIcon("edit"), i18n("Edit..."));
120 action->setData(editID);
121 action = popup->addAction(KIcon("edit-delete"), i18n("Delete"));
122 action->setData(deleteID);
123
124 }
125
126 KBookmarkManager* manager = DolphinSettings::instance().bookmarkManager();
127 KBookmarkGroup root = manager->root();
128 const int index = m_bookmarksList->index(m_bookmarksList->selectedItem());
129 QAction *result = popup->exec(pos);
130 if( result)
131 {
132 switch(result->data().toInt()) {
133 case insertID: {
134 KBookmark newBookmark = EditBookmarkDialog::getBookmark(i18n("Insert Bookmark"),
135 "New bookmark",
136 KUrl(),
137 "bookmark");
138 if (!newBookmark.isNull()) {
139 root.addBookmark(manager, newBookmark);
140 if (index > 0) {
141 KBookmark prevBookmark = DolphinSettings::instance().bookmark(index - 1);
142 root.moveItem(newBookmark, prevBookmark);
143 }
144 else {
145 // insert bookmark at first position (is a little bit tricky as KBookmarkGroup
146 // only allows to move items after existing items)
147 KBookmark firstBookmark = root.first();
148 root.moveItem(newBookmark, firstBookmark);
149 root.moveItem(firstBookmark, newBookmark);
150 }
151 manager->emitChanged(root);
152 }
153 break;
154 }
155
156 case editID: {
157 KBookmark oldBookmark = DolphinSettings::instance().bookmark(index);
158 KBookmark newBookmark = EditBookmarkDialog::getBookmark(i18n("Edit Bookmark"),
159 oldBookmark.text(),
160 oldBookmark.url(),
161 oldBookmark.icon());
162 if (!newBookmark.isNull()) {
163 root.addBookmark(manager, newBookmark);
164 root.moveItem(newBookmark, oldBookmark);
165 root.deleteBookmark(oldBookmark);
166 manager->emitChanged(root);
167 }
168 break;
169 }
170
171 case deleteID: {
172 KBookmark bookmark = DolphinSettings::instance().bookmark(index);
173 root.deleteBookmark(bookmark);
174 manager->emitChanged(root);
175 break;
176 }
177
178 case addID: {
179 KBookmark bookmark = EditBookmarkDialog::getBookmark(i18n("Add Bookmark"),
180 "New bookmark",
181 KUrl(),
182 "bookmark");
183 if (!bookmark.isNull()) {
184 root.addBookmark(manager, bookmark);
185 manager->emitChanged(root);
186 }
187 }
188
189 default: break;
190 }
191 }
192 delete popup;
193 popup = 0;
194 }
195
196
197 void BookmarksSidebarPage::adjustSelection(const KUrl& url)
198 {
199 KBookmarkGroup root = DolphinSettings::instance().bookmarkManager()->root();
200 KBookmark bookmark = root.closestBookmark(url);
201
202 const bool block = m_bookmarksList->signalsBlocked();
203 m_bookmarksList->blockSignals(true);
204 if (bookmark.isNull()) {
205 // no bookmark matches, hence deactivate any selection
206 const int currentIndex = m_bookmarksList->index(m_bookmarksList->selectedItem());
207 m_bookmarksList->setSelected(currentIndex, false);
208 }
209 else {
210 // select the bookmark which is part of the current Url
211 // TODO when porting to QListWidget, use the address as item data?
212 int selectedIndex = bookmark.address().mid(1).toInt(); // convert "/5" to 5.
213 m_bookmarksList->setSelected(selectedIndex, true);
214 }
215 m_bookmarksList->blockSignals(block);
216 }
217
218 BookmarksListBox::BookmarksListBox(QWidget* parent) :
219 Q3ListBox(parent)
220 {
221 }
222 BookmarksListBox::~BookmarksListBox()
223 {
224 }
225
226 void BookmarksListBox::paintEvent(QPaintEvent* /* event */)
227 {
228 // don't invoke QListBox::paintEvent(event) to prevent
229 // that any kind of frame is drawn
230 }
231
232 BookmarkItem::BookmarkItem(const QPixmap& pixmap, const QString& text) :
233 Q3ListBoxPixmap(pixmap, text)
234 {
235 }
236
237 BookmarkItem::~BookmarkItem()
238 {
239 }
240
241 int BookmarkItem::height(const Q3ListBox* listBox) const
242 {
243 return Q3ListBoxPixmap::height(listBox) + 8;
244 }
245
246 #include "bookmarkssidebarpage.moc"