]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/panels/places/placesitemmodel.cpp
Fix several bookmark synchronization issues
[dolphin.git] / src / panels / places / placesitemmodel.cpp
index 358652d4d7fd5cf323589ff610add4baff08af5e..681d9d17ef7572c465e44f02e66865481a46310f 100644 (file)
@@ -58,7 +58,8 @@ PlacesItemModel::PlacesItemModel(QObject* parent) :
     m_systemBookmarks(),
     m_systemBookmarksIndexes(),
     m_hiddenItems(),
-    m_hiddenItemToRemove(-1)
+    m_hiddenItemToRemove(-1),
+    m_saveBookmarksTimer(0)
 {
 #ifdef HAVE_NEPOMUK
     m_nepomukRunning = (Nepomuk::ResourceManager::instance()->initialized());
@@ -69,14 +70,30 @@ PlacesItemModel::PlacesItemModel(QObject* parent) :
     createSystemBookmarks();
     initializeAvailableDevices();
     loadBookmarks();
+
+    m_saveBookmarksTimer = new QTimer(this);
+    m_saveBookmarksTimer->setInterval(100);
+    m_saveBookmarksTimer->setSingleShot(true);
+    connect(m_saveBookmarksTimer, SIGNAL(timeout()), this, SLOT(saveBookmarks()));
 }
 
 PlacesItemModel::~PlacesItemModel()
 {
+    saveBookmarks();
     qDeleteAll(m_hiddenItems);
     m_hiddenItems.clear();
 }
 
+PlacesItem* PlacesItemModel::createPlacesItem(const QString& text,
+                                              const KUrl& url,
+                                              const QString& iconName)
+{
+    const KBookmark bookmark = PlacesItem::createBookmark(m_bookmarkManager, text, url, iconName);
+    PlacesItem* item = new PlacesItem(bookmark);
+    item->setGroup(groupName(url));
+    return item;
+}
+
 PlacesItem* PlacesItemModel::placesItem(int index) const
 {
     return dynamic_cast<PlacesItem*>(item(index));
@@ -124,7 +141,7 @@ void PlacesItemModel::setHiddenItemsShown(bool show)
     } else {
         // Move all items of the model, where the "isHidden" property is true, to
         // m_hiddenItems.
-        //Q_ASSERT(m_hiddenItems.count() == count());
+        Q_ASSERT(m_hiddenItems.count() == count());
         for (int i = count() - 1; i >= 0; --i) {
             PlacesItem* visibleItem = placesItem(i);
             if (visibleItem->isHidden()) {
@@ -267,15 +284,23 @@ void PlacesItemModel::requestTeardown(int index)
     }
 }
 
-
-void PlacesItemModel::save()
-{
-    // TODO: Temporary deactivated until 100 % backward compatibility is provided
-    // m_bookmarkManager->emitChanged(m_bookmarkManager->root());
-}
-
 void PlacesItemModel::onItemInserted(int index)
 {
+    const PlacesItem* insertedItem = placesItem(index);
+    if (insertedItem) {
+        // Take care to apply the PlacesItemModel-order of the inserted item
+        // also to the bookmark-manager.
+        const KBookmark insertedBookmark = insertedItem->bookmark();
+
+        const PlacesItem* previousItem = placesItem(index - 1);
+        KBookmark previousBookmark;
+        if (previousItem) {
+            previousBookmark = previousItem->bookmark();
+        }
+
+        m_bookmarkManager->root().moveBookmark(insertedBookmark, previousBookmark);
+    }
+
     if (index == count() - 1) {
         // The item has been appended as last item to the list. In this
         // case assure that it is also appended after the hidden items and
@@ -297,17 +322,28 @@ void PlacesItemModel::onItemInserted(int index)
     }
     m_hiddenItems.insert(hiddenIndex, 0);
 
+    m_saveBookmarksTimer->start();
+
 #ifdef PLACESITEMMODEL_DEBUG
     kDebug() << "Inserted item" << index;
     showModelState();
 #endif
 }
 
-void PlacesItemModel::onItemRemoved(int index)
+void PlacesItemModel::onItemRemoved(int index, KStandardItem* removedItem)
 {
+    PlacesItem* placesItem = dynamic_cast<PlacesItem*>(removedItem);
+    if (placesItem) {
+        const KBookmark bookmark = placesItem->bookmark();
+        m_bookmarkManager->root().deleteBookmark(bookmark);
+    }
+
     const int removeIndex = hiddenIndex(index);
     Q_ASSERT(!m_hiddenItems[removeIndex]);
     m_hiddenItems.removeAt(removeIndex);
+
+    m_saveBookmarksTimer->start();
+
 #ifdef PLACESITEMMODEL_DEBUG
     kDebug() << "Removed item" << index;
     showModelState();
@@ -316,16 +352,30 @@ void PlacesItemModel::onItemRemoved(int index)
 
 void PlacesItemModel::onItemChanged(int index, const QSet<QByteArray>& changedRoles)
 {
+    const PlacesItem* changedItem = placesItem(index);
+    if (changedItem) {
+        // Take care to apply the PlacesItemModel-order of the inserted item
+        // also to the bookmark-manager.
+        const KBookmark insertedBookmark = changedItem->bookmark();
+
+        const PlacesItem* previousItem = placesItem(index - 1);
+        KBookmark previousBookmark;
+        if (previousItem) {
+            previousBookmark = previousItem->bookmark();
+        }
+
+        m_bookmarkManager->root().moveBookmark(insertedBookmark, previousBookmark);
+    }
+
     if (changedRoles.contains("isHidden")) {
         const PlacesItem* shownItem = placesItem(index);
         Q_ASSERT(shownItem);
-        const bool hide = shownItem->isHidden();
-
-        if (!m_hiddenItemsShown && hide) {
+        if (!m_hiddenItemsShown && shownItem->isHidden()) {
             m_hiddenItemToRemove = index;
             QTimer::singleShot(0, this, SLOT(removeHiddenItem()));
         }
     }
+    m_saveBookmarksTimer->start();
 }
 
 void PlacesItemModel::slotDeviceAdded(const QString& udi)
@@ -376,10 +426,18 @@ void PlacesItemModel::removeHiddenItem()
         PlacesItem* hiddenItem = new PlacesItem(*shownItem);
         removeItem(m_hiddenItemToRemove);
         m_hiddenItems.insert(newIndex, hiddenItem);
+        m_saveBookmarksTimer->start();
     }
     m_hiddenItemToRemove = -1;
 }
 
+
+void PlacesItemModel::saveBookmarks()
+{
+    // TODO: Temporary deactivated until 100 % backward compatibility is provided
+    // m_bookmarkManager->emitChanged(m_bookmarkManager->root());
+}
+
 void PlacesItemModel::loadBookmarks()
 {
     KBookmarkGroup root = m_bookmarkManager->root();
@@ -537,7 +595,7 @@ void PlacesItemModel::createSystemBookmarks()
                                                     searchForGroup));
         m_systemBookmarks.append(SystemBookmarkData(KUrl("search:/audio"),
                                                     "folder-sound",
-                                                    i18nc("@item Commonly Accessed", "Audio"),
+                                                    i18nc("@item Commonly Accessed", "Audio Files"),
                                                     searchForGroup));
         m_systemBookmarks.append(SystemBookmarkData(KUrl("search:/videos"),
                                                     "folder-video",