diff options
author | Rok Mandeljc <rok.mandeljc@gmail.com> | 2014-07-23 23:15:17 +0200 |
---|---|---|
committer | Philip Langdale <philipl@overt.org> | 2014-07-23 15:48:16 -0700 |
commit | 64e51eb07cd6628991842c6be8eb276802b71154 (patch) | |
tree | 86b1c0b11f1be589b513cf57a90fddaa1cacae79 | |
parent | 8f97c9f56e7ad868393c4b344fa7b18ab6a65782 (diff) | |
download | gvfs-64e51eb07cd6628991842c6be8eb276802b71154.tar.gz |
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>
-rw-r--r-- | daemon/gvfsbackendmtp.c | 35 |
1 files changed, 34 insertions, 1 deletions
diff --git a/daemon/gvfsbackendmtp.c b/daemon/gvfsbackendmtp.c index 3af15214..8c04a0b5 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); + } } |