]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Make use of KFilePlaces instead of the bookmark system in the URL
authorKevin Ottens <ervin@kde.org>
Mon, 2 Apr 2007 17:32:49 +0000 (17:32 +0000)
committerKevin Ottens <ervin@kde.org>
Mon, 2 Apr 2007 17:32:49 +0000 (17:32 +0000)
navigator. Yay!

svn path=/trunk/KDE/kdebase/apps/; revision=649452

src/bookmarkselector.cpp
src/bookmarkselector.h
src/dolphinview.cpp
src/urlnavigator.cpp
src/urlnavigator.h

index 77fa5b7ac2d4cec364d9f36f8553fc0780da23d8..10ce0f2b3c7cd57076deff5c936db04f80379c90 100644 (file)
@@ -25,7 +25,7 @@
 
 #include <kiconloader.h>
 #include <kglobalsettings.h>
-#include <kbookmarkmanager.h>
+#include <kfileplacesmodel.h>
 #include <kmenu.h>
 #include <kdebug.h>
 
 #include <QPixmap>
 #include <kicon.h>
 
-BookmarkSelector::BookmarkSelector(UrlNavigator* parent, KBookmarkManager* bookmarkManager) :
+BookmarkSelector::BookmarkSelector(UrlNavigator* parent, KFilePlacesModel* placesModel) :
     UrlButton(parent),
-    m_selectedAddress(),
+    m_selectedItem(-1),
     m_urlNavigator(parent),
-    m_bookmarkManager(bookmarkManager)
+    m_placesModel(placesModel)
 {
     setFocusPolicy(Qt::NoFocus);
 
-    m_bookmarksMenu = new KMenu(this);
-
-    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);
-        m_bookmarksMenu->addAction(action);
-        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());
-            setMinimumWidth(pixmap.width() + 2);
-        }
-        bookmark = root.next(bookmark);
-        ++i;
-    }
+    m_placesMenu = new KMenu(this);
 
-    connect(m_bookmarksMenu, SIGNAL(triggered(QAction*)),
-            this, SLOT(activateBookmark(QAction*)));
+    updateMenu();
 
-    setMenu(m_bookmarksMenu);
+    connect(m_placesModel, SIGNAL(rowsInserted(const QModelIndex&, int, int)),
+            this, SLOT(updateMenu()));
+    connect(m_placesModel, SIGNAL(rowsRemoved(const QModelIndex&, int, int)),
+            this, SLOT(updateMenu()));
+    connect(m_placesMenu, SIGNAL(triggered(QAction*)),
+            this, SLOT(activatePlace(QAction*)));
+
+    setMenu(m_placesMenu);
 }
 
 BookmarkSelector::~BookmarkSelector()
 {
 }
 
+void BookmarkSelector::updateMenu()
+{
+    m_placesMenu->clear();
+
+    for (int i=0; i<m_placesModel->rowCount(); ++i) {
+        QModelIndex index = m_placesModel->index(i, 0);
+        QAction* action = new QAction(m_placesModel->icon(index),
+                                      m_placesModel->text(index),
+                                      m_placesMenu);
+        m_placesMenu->addAction(action);
+
+        action->setData(i);
+
+        if (i == m_selectedItem) {
+            //QPixmap pixmap = SmallIcon(bookmark.icon());
+            setIcon(m_placesModel->icon(index));
+            //setIconSize(pixmap.size());
+            //setMinimumWidth(pixmap.width() + 2);
+        }
+    }
+}
+
 void BookmarkSelector::updateSelection(const KUrl& url)
 {
-    KBookmark bookmark = m_bookmarkManager->root().closestBookmark(url);
-    if (!bookmark.isNull()) {
-        m_selectedAddress = bookmark.address();
-        setIcon(KIcon(bookmark.icon()));
+    QModelIndex index = m_placesModel->closestItem(url);
+
+    if (index.isValid()) {
+        m_selectedItem = index.row();
+        setIcon(m_placesModel->icon(index));
     }
     else {
-        m_selectedAddress = QString();
+        m_selectedItem = -1;
         // No bookmark has been found which matches to the given Url. Show
         // a generic folder icon as pixmap for indication:
         setIcon(KIcon("folder"));
     }
 }
 
-KBookmark BookmarkSelector::selectedBookmark() const
+KUrl BookmarkSelector::selectedPlaceUrl() const
+{
+    QModelIndex index = m_placesModel->index(m_selectedItem, 0);
+
+    if (index.isValid())
+        return m_placesModel->url(index);
+    else
+        return KUrl();
+}
+
+QString BookmarkSelector::selectedPlaceText() const
 {
-    return m_bookmarkManager->findByAddress(m_selectedAddress);
+    QModelIndex index = m_placesModel->index(m_selectedItem, 0);
+
+    if (index.isValid())
+        return m_placesModel->text(index);
+    else
+        return QString();
 }
 
 QSize BookmarkSelector::sizeHint() const
@@ -148,14 +171,17 @@ void BookmarkSelector::paintEvent(QPaintEvent* /*event*/)
     painter.drawPixmap(x, y, pixmap);
 }
 
-void BookmarkSelector::activateBookmark(QAction* action)
+void BookmarkSelector::activatePlace(QAction* action)
 {
     assert(action != 0);
-    m_selectedAddress = action->data().toString();
+    m_selectedItem = action->data().toInt();
 
-    const KBookmark bookmark = selectedBookmark();
-    setPixmap(SmallIcon(bookmark.icon()));
-    emit bookmarkActivated(bookmark.url());
+    QModelIndex index = m_placesModel->index(m_selectedItem, 0);
+
+    if (index.isValid()) {
+        setIcon(m_placesModel->icon(index));
+        emit placeActivated(m_placesModel->url(index));
+    }
 }
 
 #include "bookmarkselector.moc"
index a5f0d434ef36268df2c37ca5ad597b984ee79c01..bedfcb233f710610eb500aa224e328abc89ba43e 100644 (file)
 #ifndef BOOKMARKSELECTOR_H
 #define BOOKMARKSELECTOR_H
 
-#include <kbookmark.h>
 #include <urlbutton.h>
+#include <kurl.h>
 
+class KFilePlacesModel;
 class UrlNavigator;
 class KMenu;
-class KUrl;
 
 /**
  * @brief Allows to select a bookmark from a popup menu.
@@ -44,7 +44,7 @@ public:
      * @param parent Parent widget where the bookmark selector
      *               is embedded into.
      */
-    BookmarkSelector(UrlNavigator* parent, KBookmarkManager* bookmarkManager);
+    BookmarkSelector(UrlNavigator* parent, KFilePlacesModel* placesModel);
 
     virtual ~BookmarkSelector();
 
@@ -58,7 +58,9 @@ public:
     void updateSelection(const KUrl& url);
 
     /** Returns the selected bookmark. */
-    KBookmark selectedBookmark() const;
+    KUrl selectedPlaceUrl() const;
+    /** Returns the selected bookmark. */
+    QString selectedPlaceText() const;
 
     /** @see QWidget::sizeHint() */
     virtual QSize sizeHint() const;
@@ -66,9 +68,9 @@ public:
 signals:
     /**
      * Is send when a bookmark has been activated by the user.
-     * @param url URL of the selected bookmark.
+     * @param url URL of the selected place.
      */
-    void bookmarkActivated(const KUrl& url);
+    void placeActivated(const KUrl& url);
 
 protected:
     /**
@@ -82,13 +84,15 @@ private slots:
      * Updates the selected index and the icon to the bookmark
      * which is indicated by the triggered action \a action.
      */
-    void activateBookmark(QAction* action);
+    void activatePlace(QAction* action);
+
+    void updateMenu();
 
 private:
-    QString m_selectedAddress;
+    int m_selectedItem;
     UrlNavigator* m_urlNavigator;
-    KMenu* m_bookmarksMenu;
-    KBookmarkManager* m_bookmarkManager;
+    KMenu* m_placesMenu;
+    KFilePlacesModel* m_placesModel;
 };
 
 #endif
index bb84d32c01fe34c21152439cb6b27d1d93e77bbf..402a673f0486ce81281963e09fa0a67cf6504799 100644 (file)
@@ -31,6 +31,7 @@
 
 #include <kdirmodel.h>
 #include <kfileitemdelegate.h>
+#include <kfileplacesmodel.h>
 #include <klocale.h>
 #include <kiconeffect.h>
 #include <kio/netaccess.h>
@@ -96,7 +97,7 @@ DolphinView::DolphinView(DolphinMainWindow* mainWindow,
     connect(clipboard, SIGNAL(dataChanged()),
             this, SLOT(updateCutItems()));
 
-    m_urlNavigator = new UrlNavigator(DolphinSettings::instance().bookmarkManager(), url, this);
+    m_urlNavigator = new UrlNavigator(new KFilePlacesModel(this), url, this);
     m_urlNavigator->setUrlEditable(DolphinSettings::instance().generalSettings()->editableUrl());
     m_urlNavigator->setHomeUrl(DolphinSettings::instance().generalSettings()->homeUrl());
     m_urlNavigator->setShowHiddenFiles(showHiddenFiles);
index 514e4b22b12806928ee76c4115d111c52addf5cc..1e8f06b6e3acc7665a93febad0c3e53f2dc58cc0 100644 (file)
@@ -98,7 +98,7 @@ HistoryElem::~HistoryElem()
 class UrlNavigator::Private
 {
 public:
-    Private(UrlNavigator* q, KBookmarkManager* bookmarkManager);
+    Private(UrlNavigator* q, KFilePlacesModel* placesModel);
 
     void slotReturnPressed(const QString&);
     void slotRemoteHostActivated();
@@ -162,7 +162,7 @@ public:
 };
 
 
-UrlNavigator::Private::Private(UrlNavigator* q, KBookmarkManager* bookmarkManager)
+UrlNavigator::Private::Private(UrlNavigator* q, KFilePlacesModel* placesModel)
     :
     m_active(true),
     m_showHiddenFiles(false),
@@ -189,8 +189,8 @@ UrlNavigator::Private::Private(UrlNavigator* q, KBookmarkManager* bookmarkManage
             q, SLOT(switchView()));
 
     // initialize the bookmark selector
-    m_bookmarkSelector = new BookmarkSelector(q, bookmarkManager);
-    connect(m_bookmarkSelector, SIGNAL(bookmarkActivated(const KUrl&)),
+    m_bookmarkSelector = new BookmarkSelector(q, placesModel);
+    connect(m_bookmarkSelector, SIGNAL(placeActivated(const KUrl&)),
             q, SLOT(setUrl(const KUrl&)));
 
     // initialize the path box of the traditional view
@@ -403,39 +403,39 @@ void UrlNavigator::Private::updateContent()
         m_filler->show();
 
         // get the data from the currently selected bookmark
-        KBookmark bookmark = m_bookmarkSelector->selectedBookmark();
+        KUrl placeUrl = m_bookmarkSelector->selectedPlaceUrl();
 
-        QString bookmarkPath;
-        if (bookmark.isNull()) {
-            // No bookmark is a part of the current Url.
-            // The following code tries to guess the bookmark
+        QString placePath;
+        if (!placeUrl.isValid()) {
+            // No place is a part of the current Url.
+            // The following code tries to guess the place
             // path. E. g. "fish://root@192.168.0.2/var/lib" writes
-            // "fish://root@192.168.0.2" to 'bookmarkPath', which leads to the
+            // "fish://root@192.168.0.2" to 'placePath', which leads to the
             // navigation indication 'Custom Path > var > lib".
             int idx = path.indexOf(QString("//"));
             idx = path.indexOf("/", (idx < 0) ? 0 : idx + 2);
-            bookmarkPath = (idx < 0) ? path : path.left(idx);
+            placePath = (idx < 0) ? path : path.left(idx);
         }
         else {
-            bookmarkPath = bookmark.url().pathOrUrl();
+            placePath = placeUrl.pathOrUrl();
         }
-        const uint len = bookmarkPath.length();
+        const uint len = placePath.length();
 
         // calculate the start point for the URL navigator buttons by counting
-        // the slashs inside the bookmark URL
+        // the slashs inside the place URL
         int slashCount = 0;
         for (uint i = 0; i < len; ++i) {
-            if (bookmarkPath.at(i) == QChar('/')) {
+            if (placePath.at(i) == QChar('/')) {
                 ++slashCount;
             }
         }
-        if ((len > 0) && bookmarkPath.at(len - 1) == QChar('/')) {
+        if ((len > 0) && placePath.at(len - 1) == QChar('/')) {
             assert(slashCount > 0);
             --slashCount;
         }
 
         const KUrl currentUrl = q->url();
-        if (!currentUrl.isLocalFile() && bookmark.isNull()) {
+        if (!currentUrl.isLocalFile() && !placeUrl.isValid()) {
             QString protocol = currentUrl.protocol();
             if (!m_protocols) {
                 deleteButtons();
@@ -511,9 +511,9 @@ void UrlNavigator::Private::updateButtons(const QString& path, int startIndex)
             QString text;
             if (isFirstButton) {
                 // the first URL navigator button should get the name of the
-                // bookmark instead of the directory name
-                const KBookmark bookmark = m_bookmarkSelector->selectedBookmark();
-                text = bookmark.text();
+                // place instead of the directory name
+                const KUrl placeUrl = m_bookmarkSelector->selectedPlaceUrl();
+                text = m_bookmarkSelector->selectedPlaceText();
                 if (text.isEmpty()) {
                     if (currentUrl.isLocalFile()) {
                         text = i18n("Custom Path");
@@ -576,11 +576,11 @@ void UrlNavigator::Private::deleteButtons()
 ////
 
 
-UrlNavigator::UrlNavigator(KBookmarkManager* bookmarkManager,
+UrlNavigator::UrlNavigator(KFilePlacesModel* placesModel,
                            const KUrl& url,
                            QWidget* parent) :
     QWidget(parent),
-    d( new Private(this, bookmarkManager) )
+    d( new Private(this, placesModel) )
 {
     d->m_history.prepend(HistoryElem(url));
 
index cec3a0a9d80cc661c0c70c3518bf80c819e1b823..af77fa8e39f9c13103d7084e756021671e2daffc 100644 (file)
@@ -25,7 +25,7 @@
 #include <kurl.h>
 #include <QWidget>
 
-class KBookmarkManager;
+class KFilePlacesModel;
 class QMouseEvent;
 
 /**
@@ -50,7 +50,7 @@ class UrlNavigator : public QWidget
     Q_OBJECT
 
 public:
-    UrlNavigator(KBookmarkManager* bookmarkManager, const KUrl& url, QWidget* parent);
+    UrlNavigator(KFilePlacesModel* placesModel, const KUrl& url, QWidget* parent);
     virtual ~UrlNavigator();
 
     /** Returns the current active URL. */