>From 89612e96575bb2d81665381c1a55e89d93106b73 Mon Sep 17 00:00:00 2001
From: Sian Cao <sycao@redflag-linux.com>
Date: Mon, 28 Dec 2009 15:39:01 +0800
Subject: [PATCH] Fix for the awareness of accessibility change for dolphin

this patch provide an interface to inform outside the changes
of device accessibilities. so app like dolphin know how to do
the correct thing when this happens, like reloading the view.

Signed-off-by: Sian Cao <sycao@redflag-linux.com>
---
 kfile/kfileplacesitem.cpp  |   11 +++++++++++
 kfile/kfileplacesitem_p.h  |    1 +
 kfile/kfileplacesmodel.cpp |   32 +++++++++++++++++++++++++++++++-
 kfile/kfileplacesmodel.h   |    6 ++++--
 kfile/kfileplacesview.cpp  |    4 +++-
 kfile/kfileplacesview.h    |    6 +++++-
 6 files changed, 55 insertions(+), 5 deletions(-)

diff --git a/kfile/kfileplacesitem.cpp b/kfile/kfileplacesitem.cpp
index 240ac49..298ace0 100644
--- a/kfile/kfileplacesitem.cpp
+++ b/kfile/kfileplacesitem.cpp
@@ -54,6 +54,7 @@ KFilePlacesItem::KFilePlacesItem(KBookmarkManager *manager,
         Solid::Device dev(udi);
         Solid::StorageAccess *access = dev.as<Solid::StorageAccess>();
         if (access!=0) {
+            m_bookmark.setMetaDataItem("lastMountPoint", access->filePath());
             connect(access, SIGNAL(accessibilityChanged(bool, const QString &)),
                     this, SLOT(onAccessibilityChanged()));
         }
@@ -267,7 +268,17 @@ QString KFilePlacesItem::generateNewId()
 
 void KFilePlacesItem::onAccessibilityChanged()
 {
+    if ( isDevice() ) {
+        Solid::Device dev = device();
+        if ( dev.as<Solid::StorageAccess>()->isAccessible() ) {
+            const QString path = m_access->filePath();			
+            if ( m_bookmark.metaDataItem("lastMountPoint") != path )
+                m_bookmark.setMetaDataItem("lastMountPoint", path);
+        }
+    }
+	
     emit itemChanged(id());
+    emit accessibilityChanged(id());
 }
 
 bool KFilePlacesItem::hasFullIcon(const KBookmark &bookmark) const
diff --git a/kfile/kfileplacesitem_p.h b/kfile/kfileplacesitem_p.h
index 49339f2..f16a5cd 100644
--- a/kfile/kfileplacesitem_p.h
+++ b/kfile/kfileplacesitem_p.h
@@ -66,6 +66,7 @@ public:
 
 Q_SIGNALS:
     void itemChanged(const QString &id);
+    void accessibilityChanged(const QString &id);	
 
 private Q_SLOTS:
     void onAccessibilityChanged();
diff --git a/kfile/kfileplacesmodel.cpp b/kfile/kfileplacesmodel.cpp
index 7fe5bcc..51e16e6 100644
--- a/kfile/kfileplacesmodel.cpp
+++ b/kfile/kfileplacesmodel.cpp
@@ -69,7 +69,7 @@ public:
     Solid::Predicate predicate;
     KBookmarkManager *bookmarkManager;
     KFilePlacesSharedBookmarks * sharedBookmarks;
-
+	
     void reloadAndSignal();
     QList<KFilePlacesItem *> loadBookmarkList();
 
@@ -80,6 +80,7 @@ public:
     void _k_reloadBookmarks();
     void _k_storageSetupDone(Solid::ErrorType error, QVariant errorData);
     void _k_storageTeardownDone(Solid::ErrorType error, QVariant errorData);
+	void _k_storageChanged(const QString& id);
 };
 
 KFilePlacesModel::KFilePlacesModel(QObject *parent)
@@ -434,6 +435,8 @@ QList<KFilePlacesItem *> KFilePlacesModel::Private::loadBookmarkList()
             }
             connect(item, SIGNAL(itemChanged(const QString&)),
                     q, SLOT(_k_itemChanged(const QString&)));
+            connect(item, SIGNAL(accessibilityChanged(const QString&)),
+                    q, SLOT(_k_storageChanged(const QString&)));
             items << item;
         }
 
@@ -448,6 +451,8 @@ QList<KFilePlacesItem *> KFilePlacesModel::Private::loadBookmarkList()
                                                         bookmark.address(), udi);
             connect(item, SIGNAL(itemChanged(const QString&)),
                     q, SLOT(_k_itemChanged(const QString&)));
+            connect(item, SIGNAL(accessibilityChanged(const QString&)),
+                    q, SLOT(_k_storageChanged(const QString&)));
             // TODO: Update bookmark internal element
             items << item;
         }
@@ -822,4 +827,29 @@ void KFilePlacesModel::Private::_k_storageTeardownDone(Solid::ErrorType error, Q
     }
 }
 
+void KFilePlacesModel::Private::_k_storageChanged(const QString& id)
+{
+    bool removed = false;
+    QModelIndex index;
+    KUrl url("");
+    for ( int row = 0; row < items.size(); ++row ) {
+        if ( items.at(row)->id() == id ) {
+            QModelIndex index = q->index(row, 0);
+            Solid::Device dev = q->deviceForIndex(index);
+            if (dev.is<Solid::StorageAccess>() && dev.as<Solid::StorageAccess>()->isAccessible()) {
+                url = q->url(index);
+                removed = false;
+            } else {
+                removed = true;
+                const KBookmark bookmark = items.at(row)->bookmark();
+                url = bookmark.metaDataItem("lastMountPoint");
+            }
+            break;
+        }
+    }
+
+    kDebug() << "id " << id << ", url " << url;
+    emit q->accessibilityChanged(url, removed);
+}
+
 #include "kfileplacesmodel.moc"
diff --git a/kfile/kfileplacesmodel.h b/kfile/kfileplacesmodel.h
index 6637267..30e6fb8 100644
--- a/kfile/kfileplacesmodel.h
+++ b/kfile/kfileplacesmodel.h
@@ -130,7 +130,8 @@ public:
 Q_SIGNALS:
     void errorMessage(const QString &message);
     void setupDone(const QModelIndex &index, bool success);
-
+	void accessibilityChanged(const KUrl& url, bool removed);
+	
 private:
     Q_PRIVATE_SLOT(d, void _k_initDeviceList())
     Q_PRIVATE_SLOT(d, void _k_deviceAdded(const QString&))
@@ -139,7 +140,8 @@ private:
     Q_PRIVATE_SLOT(d, void _k_reloadBookmarks())
     Q_PRIVATE_SLOT(d, void _k_storageSetupDone(Solid::ErrorType, QVariant))
     Q_PRIVATE_SLOT(d, void _k_storageTeardownDone(Solid::ErrorType, QVariant))
-
+    Q_PRIVATE_SLOT(d, void _k_storageChanged(const QString& id))
+		
     class Private;
     Private * const d;
     friend class Private;
diff --git a/kfile/kfileplacesview.cpp b/kfile/kfileplacesview.cpp
index 07baca8..4b8e47d 100644
--- a/kfile/kfileplacesview.cpp
+++ b/kfile/kfileplacesview.cpp
@@ -835,6 +835,8 @@ void KFilePlacesView::setModel(QAbstractItemModel *model)
             this, SLOT(adaptItemSize()));
     connect(selectionModel(), SIGNAL(currentChanged(const QModelIndex&,const QModelIndex&)),
             d->watcher, SLOT(currentIndexChanged(const QModelIndex&)));
+	connect(model, SIGNAL(accessibilityChanged(const KUrl&, bool)),
+			this, SIGNAL(placeAccessabilityChange(const KUrl&, bool)));
 }
 
 void KFilePlacesView::rowsInserted(const QModelIndex &parent, int start, int end)
@@ -890,7 +892,7 @@ void KFilePlacesView::Private::setCurrentIndex(const QModelIndex &index)
 
     KUrl url = placesModel->url(index);
 
-    if (url.isValid()) {
+    if (url.isValid() && currentUrl != url ) {
         currentUrl = url;
         updateHiddenRows();
         emit q->urlChanged(url);
diff --git a/kfile/kfileplacesview.h b/kfile/kfileplacesview.h
index 88ba8dd..8e1bb93 100644
--- a/kfile/kfileplacesview.h
+++ b/kfile/kfileplacesview.h
@@ -90,7 +90,11 @@ Q_SIGNALS:
      * corresponding action like copying or moving.
      */
     void urlsDropped(const KUrl &dest, QDropEvent *event, QWidget *parent);
-
+	/**
+	 * Is emitted if device(removable) accessability changed
+	 */
+	void placeAccessabilityChange(const KUrl &url, bool removed);
+	
 private:
     Q_PRIVATE_SLOT(d, void adaptItemSize())
     Q_PRIVATE_SLOT(d, void _k_placeClicked(const QModelIndex &))
-- 
1.6.5.2


