]> cloud.milkyroute.net Git - dolphin.git/commitdiff
PlacesItemModel: Automatically save bookmarks
authorPeter Penz <peter.penz19@gmail.com>
Mon, 14 May 2012 12:57:08 +0000 (14:57 +0200)
committerPeter Penz <peter.penz19@gmail.com>
Mon, 14 May 2012 12:58:34 +0000 (14:58 +0200)
Prevent that a manual call to save changed bookmarks is
necessary.

src/kitemviews/kstandarditemmodel.cpp
src/panels/places/placesitem.cpp
src/panels/places/placesitemmodel.cpp
src/panels/places/placesitemmodel.h
src/panels/places/placespanel.cpp

index 545a06b5da90081055895546d478b90ada576d1e..fbcda837042e90be24edfb4992a89811c7868c65 100644 (file)
@@ -199,6 +199,7 @@ void KStandardItemModel::onItemInserted(int index)
 void KStandardItemModel::onItemChanged(int index, const QSet<QByteArray>& changedRoles)
 {
     Q_UNUSED(index);
+    Q_UNUSED(changedRoles);
 }
 
 void KStandardItemModel::onItemRemoved(int index)
index 292312d0a07243c57c1cf9d3d91db7bc1f2f9a8f..19b1d9e9a573021c38891b60a4c9d5e3e8d21612 100644 (file)
@@ -44,13 +44,16 @@ PlacesItem::PlacesItem(const KBookmark& bookmark, PlacesItem* parent) :
 
 PlacesItem::PlacesItem(const PlacesItem& item) :
     KStandardItem(item),
-    m_device(),
-    m_access(),
-    m_volume(),
-    m_disc(),
+    m_device(item.m_device),
+    m_access(item.m_access),
+    m_volume(item.m_volume),
+    m_disc(item.m_disc),
     m_accessListener(0),
-    m_bookmark()
+    m_bookmark(item.m_bookmark)
 {
+    if (item.m_accessListener) {
+        m_accessListener = new PlacesItemStorageAccessListener(this);
+    }
 }
 
 PlacesItem::~PlacesItem()
index 358652d4d7fd5cf323589ff610add4baff08af5e..0b1daf4b9e3080f31ed201f3f718efef1d9702c1 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,10 +70,16 @@ 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();
 }
@@ -124,7 +131,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,13 +274,6 @@ 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)
 {
     if (index == count() - 1) {
@@ -297,6 +297,8 @@ void PlacesItemModel::onItemInserted(int index)
     }
     m_hiddenItems.insert(hiddenIndex, 0);
 
+    m_saveBookmarksTimer->start();
+
 #ifdef PLACESITEMMODEL_DEBUG
     kDebug() << "Inserted item" << index;
     showModelState();
@@ -308,6 +310,9 @@ void PlacesItemModel::onItemRemoved(int index)
     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();
@@ -319,13 +324,12 @@ void PlacesItemModel::onItemChanged(int index, const QSet<QByteArray>& changedRo
     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 +380,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();
index e9604ad22199c278d081488c4f7ef5c752c4d599..bfc845e41d56b0103b7840f75ac9ab93a9d62807 100644 (file)
@@ -34,6 +34,7 @@
 class KBookmarkManager;
 class PlacesItem;
 class QAction;
+class QTimer;
 
 // #define PLACESITEMMODEL_DEBUG
 
@@ -79,8 +80,6 @@ public:
     void requestEject(int index);
     void requestTeardown(int index);
 
-    void save();
-
 signals:
     void errorMessage(const QString& message);
 
@@ -94,9 +93,10 @@ private slots:
     void slotDeviceRemoved(const QString& udi);
     void slotStorageTeardownDone(Solid::ErrorType error, const QVariant& errorData);
     void removeHiddenItem();
+    void saveBookmarks();
 
 private:
-    void loadBookmarks();
+    void loadBookmarks();   
 
     /**
      * Helper method for loadBookmarks(): Adds the items
@@ -159,6 +159,8 @@ private:
     // asynchronously as in the scope of onItemChanged()
     // removing an item is not allowed.
     int m_hiddenItemToRemove;
+
+    QTimer* m_saveBookmarksTimer;
 };
 
 #endif
index bc639c0fb6fbee9434088d850d263657096c52c7..75f76581276a41b26d0e61c7f9ab3f700bd8cdca 100644 (file)
@@ -217,10 +217,8 @@ void PlacesPanel::slotItemContextMenuRequested(int index, const QPointF& pos)
             editEntry(index);
         } else if (action == removeAction) {
             m_model->removeItem(index);
-            m_model->save();
         } else if (action == hideAction) {
             item->setHidden(hideAction->isChecked());
-            m_model->save();
         } else if (action == openInNewTabAction) {
             const KUrl url = m_model->item(index)->dataValue("url").value<KUrl>();
             emit placeMiddleClicked(url);
@@ -337,8 +335,6 @@ void PlacesPanel::addEntry()
     }
 
     delete dialog;
-
-    m_model->save();
 }
 
 void PlacesPanel::editEntry(int index)
@@ -364,8 +360,6 @@ void PlacesPanel::editEntry(int index)
     }
 
     delete dialog;
-
-    m_model->save();
 }
 
 void PlacesPanel::selectClosestItem()