]>
cloud.milkyroute.net Git - dolphin.git/blob - src/bookmarkssidebarpage.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at> *
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. *
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. *
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 ***************************************************************************/
20 #include "bookmarkssidebarpage.h"
22 #include <q3listbox.h>
27 #include <Q3VBoxLayout>
28 #include <QPaintEvent>
30 #include <q3popupmenu.h>
32 #include <kbookmark.h>
33 #include <kbookmarkmanager.h>
34 #include <kmessagebox.h>
35 #include <kiconloader.h>
38 #include "dolphinsettings.h"
40 #include "dolphinview.h"
41 #include "editbookmarkdialog.h"
43 BookmarksSidebarPage::BookmarksSidebarPage(QWidget
* parent
) :
46 Q3VBoxLayout
* layout
= new Q3VBoxLayout(this);
47 m_bookmarksList
= new BookmarksListBox(this);
48 m_bookmarksList
->setPaletteBackgroundColor(colorGroup().background());
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
&)));
56 KBookmarkManager
* manager
= DolphinSettings::instance().bookmarkManager();
57 connect(manager
, SIGNAL(changed(const QString
&, const QString
&)),
58 this, SLOT(updateBookmarks()));
63 BookmarksSidebarPage::~BookmarksSidebarPage()
67 void BookmarksSidebarPage::activeViewChanged()
69 connectToActiveView();
72 void BookmarksSidebarPage::updateBookmarks()
74 m_bookmarksList
->clear();
76 KIconLoader iconLoader
;
78 KBookmarkGroup root
= DolphinSettings::instance().bookmarkManager()->root();
79 KBookmark bookmark
= root
.first();
80 while (!bookmark
.isNull()) {
81 QPixmap
icon(iconLoader
.loadIcon(bookmark
.icon(),
84 BookmarkItem
* item
= new BookmarkItem(icon
, bookmark
.text());
85 m_bookmarksList
->insertItem(item
);
87 bookmark
= root
.next(bookmark
);
90 connectToActiveView();
93 void BookmarksSidebarPage::slotMouseButtonClicked(int button
, Q3ListBoxItem
* item
)
95 if ((button
!= Qt::LeftButton
) || (item
== 0)) {
99 const int index
= m_bookmarksList
->index(item
);
100 KBookmark bookmark
= DolphinSettings::instance().bookmark(index
);
101 Dolphin::mainWin().activeView()->setURL(bookmark
.url());
104 void BookmarksSidebarPage::slotContextMenuRequested(Q3ListBoxItem
* item
,
107 const int insertID
= 1;
108 const int editID
= 2;
109 const int deleteID
= 3;
112 Q3PopupMenu
* popup
= new Q3PopupMenu();
114 popup
->insertItem(SmallIcon("filenew"), i18n("Add Bookmark..."), addID
);
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
);
122 KBookmarkManager
* manager
= DolphinSettings::instance().bookmarkManager();
123 KBookmarkGroup root
= manager
->root();
124 const int index
= m_bookmarksList
->index(m_bookmarksList
->selectedItem());
126 const int result
= popup
->exec(pos
);
129 KBookmark newBookmark
= EditBookmarkDialog::getBookmark(i18n("Insert Bookmark"),
133 if (!newBookmark
.isNull()) {
134 root
.addBookmark(manager
, newBookmark
);
136 KBookmark prevBookmark
= DolphinSettings::instance().bookmark(index
- 1);
137 root
.moveItem(newBookmark
, prevBookmark
);
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
);
146 manager
->emitChanged(root
);
152 KBookmark oldBookmark
= DolphinSettings::instance().bookmark(index
);
153 KBookmark newBookmark
= EditBookmarkDialog::getBookmark(i18n("Edit Bookmark"),
157 if (!newBookmark
.isNull()) {
158 root
.addBookmark(manager
, newBookmark
);
159 root
.moveItem(newBookmark
, oldBookmark
);
160 root
.deleteBookmark(oldBookmark
);
161 manager
->emitChanged(root
);
167 KBookmark bookmark
= DolphinSettings::instance().bookmark(index
);
168 root
.deleteBookmark(bookmark
);
169 manager
->emitChanged(root
);
174 KBookmark bookmark
= EditBookmarkDialog::getBookmark(i18n("Add Bookmark"),
178 if (!bookmark
.isNull()) {
179 root
.addBookmark(manager
, bookmark
);
180 manager
->emitChanged(root
);
190 DolphinView
* view
= Dolphin::mainWin().activeView();
191 adjustSelection(view
->url());
195 void BookmarksSidebarPage::adjustSelection(const KURL
& url
)
197 // TODO (remarked in dolphin/TODO): the following code is quite equal
198 // to BookmarkSelector::updateSelection().
200 KBookmarkGroup root
= DolphinSettings::instance().bookmarkManager()->root();
201 KBookmark bookmark
= root
.first();
204 int selectedIndex
= -1;
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.
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
) {
219 bookmark
= root
.next(bookmark
);
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);
231 // select the bookmark which is part of the current URL
232 m_bookmarksList
->setSelected(selectedIndex
, true);
234 m_bookmarksList
->blockSignals(block
);
237 void BookmarksSidebarPage::slotURLChanged(const KURL
& url
)
239 adjustSelection(url
);
242 void BookmarksSidebarPage::connectToActiveView()
244 DolphinView
* view
= Dolphin::mainWin().activeView();
245 adjustSelection(view
->url());
246 connect(view
, SIGNAL(signalURLChanged(const KURL
&)),
247 this, SLOT(slotURLChanged(const KURL
&)));
250 BookmarksListBox::BookmarksListBox(QWidget
* parent
) :
254 BookmarksListBox::~BookmarksListBox()
258 void BookmarksListBox::paintEvent(QPaintEvent
* /* event */)
260 // don't invoke QListBox::paintEvent(event) to prevent
261 // that any kind of frame is drawn
264 BookmarkItem::BookmarkItem(const QPixmap
& pixmap
, const QString
& text
) :
265 Q3ListBoxPixmap(pixmap
, text
)
269 BookmarkItem::~BookmarkItem()
273 int BookmarkItem::height(const Q3ListBox
* listBox
) const
275 return Q3ListBoxPixmap::height(listBox
) + 8;