#include "bookmarkselector.h"
-#include "dolphinsettings.h"
#include "urlnavigator.h"
#include <assert.h>
#include <QPainter>
#include <QPixmap>
+#include <kicon.h>
-BookmarkSelector::BookmarkSelector(UrlNavigator* parent) :
+BookmarkSelector::BookmarkSelector(UrlNavigator* parent, KBookmarkManager* bookmarkManager) :
UrlButton(parent),
- m_selectedIndex(0),
- m_urlNavigator(parent)
+ m_selectedAddress(),
+ m_urlNavigator(parent),
+ m_bookmarkManager(bookmarkManager)
{
setFocusPolicy(Qt::NoFocus);
m_bookmarksMenu = new KMenu(this);
- KBookmarkGroup root = DolphinSettings::instance().bookmarkManager()->root();
+ KBookmarkGroup root = m_bookmarkManager->root();
KBookmark bookmark = root.first();
int i = 0;
while (!bookmark.isNull()) {
QAction* action = new QAction(MainBarIcon(bookmark.icon()),
bookmark.text(),
this);
- action->setData(i);
m_bookmarksMenu->addAction(action);
- if (i == m_selectedIndex) {
+ QString address = QChar('/');
+ address += QString::number(i);
+ action->setData(address);
+ if (address == m_selectedAddress) {
QPixmap pixmap = SmallIcon(bookmark.icon());
setIcon(QIcon(pixmap));
setIconSize(pixmap.size());
void BookmarkSelector::updateSelection(const KUrl& url)
{
- m_selectedIndex = baseBookmarkIndex(url);
- if (m_selectedIndex >= 0) {
- KBookmark bookmark = DolphinSettings::instance().bookmark(m_selectedIndex);
- setIcon(SmallIcon(bookmark.icon()));
+ KBookmark bookmark = m_bookmarkManager->root().closestBookmark(url);
+ if (!bookmark.isNull()) {
+ m_selectedAddress = bookmark.address();
+ setIcon(KIcon(bookmark.icon()));
}
else {
+ m_selectedAddress = QString();
// No bookmark has been found which matches to the given Url. Show
// a generic folder icon as pixmap for indication:
- setIcon(SmallIcon("folder"));
+ setIcon(KIcon("folder"));
}
}
KBookmark BookmarkSelector::selectedBookmark() const
{
- return DolphinSettings::instance().bookmark(m_selectedIndex);
+ return m_bookmarkManager->findByAddress(m_selectedAddress);
}
QSize BookmarkSelector::sizeHint() const
return QSize(height, height);
}
-KBookmark BookmarkSelector::baseBookmark(const KUrl& url)
-{
- const int index = baseBookmarkIndex(url);
- return DolphinSettings::instance().bookmark(index);
-}
-
void BookmarkSelector::paintEvent(QPaintEvent* /*event*/)
{
QPainter painter(this);
void BookmarkSelector::activateBookmark(QAction* action)
{
assert(action != 0);
- m_selectedIndex = action->data().toInt();
+ m_selectedAddress = action->data().toString();
const KBookmark bookmark = selectedBookmark();
setPixmap(SmallIcon(bookmark.icon()));
emit bookmarkActivated(bookmark.url());
}
-int BookmarkSelector::baseBookmarkIndex(const KUrl& url)
-{
- int index = -1; // return value
-
- KBookmarkGroup root = DolphinSettings::instance().bookmarkManager()->root();
- KBookmark bookmark = root.first();
-
- int maxLength = 0;
-
- // Search the bookmark which is equal to the Url or at least is a parent Url.
- // If there are more than one possible parent Url candidates, choose the bookmark
- // which covers the bigger range of the Url.
- int i = 0;
- while (!bookmark.isNull()) {
- const KUrl bookmarkUrl = bookmark.url();
- if (bookmarkUrl.isParentOf(url)) {
- const int length = bookmarkUrl.prettyUrl().length();
- if (length > maxLength) {
- index = i;
- maxLength = length;
- }
- }
- bookmark = root.next(bookmark);
- ++i;
- }
-
- return index;
-}
-
#include "bookmarkselector.moc"