[gvfs] mtp: added support for OBJECT_ADDED event



commit 6eb6853bfca43d714f48ca5edd676feaec357326
Author: Rok Mandeljc <rok mandeljc gmail com>
Date:   Mon Jul 28 20:45:03 2014 +0200

    mtp: added support for OBJECT_ADDED event
    
    This patch adds support for OBJECT_ADDED event, which is emitted
    when a file or a folder is created on the device. When such an
    event is received, its path is determined by first looking up the
    parent ID and appending object's name to it; the path and IDs are
    added to cache, and create event is emitted, causing clients (for
    example Nautilus) to display the new file/folder without having to
    refresh the display of parent folder.
    
    Signed-off-by: Rok Mandeljc <rok mandeljc gmail com>

 daemon/gvfsbackendmtp.c |   52 +++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 52 insertions(+), 0 deletions(-)
---
diff --git a/daemon/gvfsbackendmtp.c b/daemon/gvfsbackendmtp.c
index ee50350..9148dd4 100644
--- a/daemon/gvfsbackendmtp.c
+++ b/daemon/gvfsbackendmtp.c
@@ -709,6 +709,58 @@ check_event (gpointer user_data)
       } else {
         return NULL;
       }
+    case LIBMTP_EVENT_OBJECT_ADDED:
+      backend = g_weak_ref_get (event_ref);
+      if (backend && !g_atomic_int_get (&backend->unmount_started)) {
+        g_mutex_lock (&backend->mutex);
+
+        LIBMTP_file_t *object = LIBMTP_Get_Filemetadata(backend->device, param1);
+        if (object) {
+          /* Obtain parent's path by searching cache by object's parent
+             ID; if the latter is zero, the object is located in storage's
+             root, otherwise, it is located in one of subfolders */
+          const char *parent_path = NULL;
+          GHashTableIter iter;
+          gpointer key, value;
+
+          g_hash_table_iter_init (&iter, backend->file_cache);
+          while (g_hash_table_iter_next (&iter, &key, &value)) {
+            const char *path = key;
+            const CacheEntry *entry = value;
+
+            if (object->parent_id != 0) {
+              if (object->parent_id == entry->id && object->storage_id == entry->storage) {
+                parent_path = path;
+                break;
+              }
+            } else {
+              if (entry->id == -1 && object->storage_id == entry->storage) {
+                parent_path = path;
+                break;
+              }
+            }
+          }
+
+          if (parent_path) {
+            /* Create path, add cache entry and emit create event(s) */
+            char *path = g_build_filename (parent_path, object->filename, NULL);
+
+            add_cache_entry (G_VFS_BACKEND_MTP (backend),
+                             path,
+                             object->storage_id,
+                             object->item_id);
+            g_hash_table_foreach (backend->monitors, emit_create_event, path);
+          }
+
+          LIBMTP_destroy_file_t (object);
+        }
+
+        g_mutex_unlock (&backend->mutex);
+        g_object_unref (backend);
+        break;
+      } else {
+        return NULL;
+      }
 #endif
     default:
       break;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]