[gvfs] mtp: use "storage-description (hex-storage-id)" storage name format only when necessary



commit 64e51eb07cd6628991842c6be8eb276802b71154
Author: Rok Mandeljc <rok mandeljc gmail com>
Date:   Wed Jul 23 23:15:17 2014 +0200

    mtp: use "storage-description (hex-storage-id)" storage name format only when necessary
    
    This patch implements on-demand post-fixing of storage description with ID for storage name, i.e., only 
in cases, when description itself is not unique.
    
    Signed-off-by: Rok Mandeljc <rok mandeljc gmail com>

 daemon/gvfsbackendmtp.c |   35 ++++++++++++++++++++++++++++++++++-
 1 files changed, 34 insertions(+), 1 deletions(-)
---
diff --git a/daemon/gvfsbackendmtp.c b/daemon/gvfsbackendmtp.c
index 3af1521..8c04a0b 100644
--- a/daemon/gvfsbackendmtp.c
+++ b/daemon/gvfsbackendmtp.c
@@ -141,7 +141,40 @@ emit_delete_event (gpointer key,
 
 static char *create_storage_name (const LIBMTP_devicestorage_t *storage)
 {
-  return g_strdup_printf("%s (%X)", storage->StorageDescription, storage->id);
+  /* The optional post-fixing of storage's name with ID requires us to
+     know in advance whether the storage's description string is unique
+     or not. Since this function is called in several places, it is
+     safest to perform this check here, each time that storage name needs
+     to be created. */
+  gboolean is_unique = TRUE;
+  const LIBMTP_devicestorage_t *tmp_storage;
+
+  /* Forward search for duplicates */
+  for (tmp_storage = storage->next; tmp_storage != 0; tmp_storage = tmp_storage->next) {
+    if (!g_strcmp0 (storage->StorageDescription, tmp_storage->StorageDescription)) {
+      is_unique = FALSE;
+      break;
+    }
+  }
+
+  /* Backward search, if necessary */
+  if (is_unique) {
+    for (tmp_storage = storage->prev; tmp_storage != 0; tmp_storage = tmp_storage->prev) {
+      /* Compare descriptions */
+      if (!g_strcmp0 (storage->StorageDescription, tmp_storage->StorageDescription)) {
+        is_unique = FALSE;
+        break;
+      }
+    }
+  }
+
+  /* If description is unique, we can use it as storage name; otherwise,
+     we add storage ID to it */
+  if (is_unique) {
+    return g_strdup (storage->StorageDescription);
+  } else {
+    return g_strdup_printf ("%s (%X)", storage->StorageDescription, storage->id);
+  }
 }
 
 


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