From: Peter Penz Date: Mon, 14 May 2012 12:57:08 +0000 (+0200) Subject: PlacesItemModel: Automatically save bookmarks X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/commitdiff_plain/77ad7c431ae8629c6c0539378c3d864a87bcd4c8 PlacesItemModel: Automatically save bookmarks Prevent that a manual call to save changed bookmarks is necessary. --- diff --git a/src/kitemviews/kstandarditemmodel.cpp b/src/kitemviews/kstandarditemmodel.cpp index 545a06b5d..fbcda8370 100644 --- a/src/kitemviews/kstandarditemmodel.cpp +++ b/src/kitemviews/kstandarditemmodel.cpp @@ -199,6 +199,7 @@ void KStandardItemModel::onItemInserted(int index) void KStandardItemModel::onItemChanged(int index, const QSet& changedRoles) { Q_UNUSED(index); + Q_UNUSED(changedRoles); } void KStandardItemModel::onItemRemoved(int index) diff --git a/src/panels/places/placesitem.cpp b/src/panels/places/placesitem.cpp index 292312d0a..19b1d9e9a 100644 --- a/src/panels/places/placesitem.cpp +++ b/src/panels/places/placesitem.cpp @@ -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() diff --git a/src/panels/places/placesitemmodel.cpp b/src/panels/places/placesitemmodel.cpp index 358652d4d..0b1daf4b9 100644 --- a/src/panels/places/placesitemmodel.cpp +++ b/src/panels/places/placesitemmodel.cpp @@ -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& 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(); diff --git a/src/panels/places/placesitemmodel.h b/src/panels/places/placesitemmodel.h index e9604ad22..bfc845e41 100644 --- a/src/panels/places/placesitemmodel.h +++ b/src/panels/places/placesitemmodel.h @@ -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 diff --git a/src/panels/places/placespanel.cpp b/src/panels/places/placespanel.cpp index bc639c0fb..75f765812 100644 --- a/src/panels/places/placespanel.cpp +++ b/src/panels/places/placespanel.cpp @@ -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(); 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()