+void PlacesItemModel::updateBookmarks()
+{
+ // Verify whether new bookmarks have been added or existing
+ // bookmarks have been changed.
+ KBookmarkGroup root = m_bookmarkManager->root();
+ KBookmark newBookmark = root.first();
+ while (!newBookmark.isNull()) {
+ if (acceptBookmark(newBookmark)) {
+ bool found = false;
+ int modelIndex = 0;
+ for (int i = 0; i < m_bookmarkedItems.count(); ++i) {
+ PlacesItem* item = m_bookmarkedItems[i];
+ if (!item) {
+ item = placesItem(modelIndex);
+ ++modelIndex;
+ }
+
+ const KBookmark oldBookmark = item->bookmark();
+ if (equalBookmarkIdentifiers(newBookmark, oldBookmark)) {
+ // The bookmark has been found in the model or as
+ // a hidden item. The content of the bookmark might
+ // have been changed, so an update is done.
+ found = true;
+ if (newBookmark.metaDataItem("UDI").isEmpty()) {
+ item->setBookmark(newBookmark);
+ }
+ break;
+ }
+ }
+
+ if (!found) {
+ PlacesItem* item = new PlacesItem(newBookmark);
+ if (item->isHidden() && !m_hiddenItemsShown) {
+ m_bookmarkedItems.append(item);
+ } else {
+ appendItem(item);
+ }
+ }
+ }
+
+ newBookmark = root.next(newBookmark);
+ }
+
+ // Remove items that are not part of the bookmark-manager anymore
+ int modelIndex = 0;
+ for (int i = m_bookmarkedItems.count() - 1; i >= 0; --i) {
+ PlacesItem* item = m_bookmarkedItems[i];
+ const bool itemIsPartOfModel = (item == 0);
+ if (itemIsPartOfModel) {
+ item = placesItem(modelIndex);
+ }
+
+ bool hasBeenRemoved = true;
+ const KBookmark oldBookmark = item->bookmark();
+ KBookmark newBookmark = root.first();
+ while (!newBookmark.isNull()) {
+ if (equalBookmarkIdentifiers(newBookmark, oldBookmark)) {
+ hasBeenRemoved = false;
+ break;
+ }
+ newBookmark = root.next(newBookmark);
+ }
+
+ if (hasBeenRemoved) {
+ if (m_bookmarkedItems[i]) {
+ delete m_bookmarkedItems[i];
+ m_bookmarkedItems.removeAt(i);
+ } else {
+ removeItem(modelIndex);
+ --modelIndex;
+ }
+ }
+
+ if (itemIsPartOfModel) {
+ ++modelIndex;
+ }
+ }
+}