diff options
author | Alexander Larsson <alexl@redhat.com> | 2005-01-14 10:40:12 +0000 |
---|---|---|
committer | Alexander Larsson <alexl@src.gnome.org> | 2005-01-14 10:40:12 +0000 |
commit | 4553e1c81e759416c72e7f2b48af5eece4eb907f (patch) | |
tree | 7233c812f1db4c6fe2be57a8f306aca2e2b52546 /libnautilus-private | |
parent | ea2138d46b2bf8d90a167218ca4e113535f1a21a (diff) | |
download | nautilus-4553e1c81e759416c72e7f2b48af5eece4eb907f.tar.gz |
New function nautilus_ensure_unique_file_name
2005-01-14 Alexander Larsson <alexl@redhat.com>
* libnautilus-private/nautilus-file-utilities.[ch]:
New function nautilus_ensure_unique_file_name
* libnautilus-private/nautilus-link.[ch]:
* libnautilus-private/nautilus-link-desktop-file.[ch]:
Uniquify name for nautilus_link_local_create if requested.
* src/file-manager/fm-directory-view.c:
(fm_directory_view_handle_url_drop):
Uniquify desktop link name
Patch from Jorn Baayen <jbaayen@gnome.org>
Diffstat (limited to 'libnautilus-private')
-rw-r--r-- | libnautilus-private/nautilus-file-utilities.c | 49 | ||||
-rw-r--r-- | libnautilus-private/nautilus-file-utilities.h | 8 | ||||
-rw-r--r-- | libnautilus-private/nautilus-link-desktop-file.c | 18 | ||||
-rw-r--r-- | libnautilus-private/nautilus-link-desktop-file.h | 5 | ||||
-rw-r--r-- | libnautilus-private/nautilus-link.c | 6 | ||||
-rw-r--r-- | libnautilus-private/nautilus-link.h | 3 |
6 files changed, 77 insertions, 12 deletions
diff --git a/libnautilus-private/nautilus-file-utilities.c b/libnautilus-private/nautilus-file-utilities.c index ae21d95e4..4fbd2a440 100644 --- a/libnautilus-private/nautilus-file-utilities.c +++ b/libnautilus-private/nautilus-file-utilities.c @@ -366,6 +366,55 @@ nautilus_get_data_file_path (const char *partial_path) return NULL; } +static gboolean +test_uri_exists (const char *path) +{ + GnomeVFSURI *uri; + gboolean exists; + + uri = gnome_vfs_uri_new (path); + exists = gnome_vfs_uri_exists (uri); + gnome_vfs_uri_unref (uri); + + return exists; +} + +char * +nautilus_ensure_unique_file_name (const char *directory_uri, + const char *base_name, + const char *extension) +{ + char *path, *escaped_name; + gboolean exists; + int copy; + + escaped_name = gnome_vfs_escape_string (base_name); + + path = g_strdup_printf ("%s/%s%s", + directory_uri, + escaped_name, + extension); + exists = test_uri_exists (path); + + copy = 1; + while (exists) { + g_free (path); + path = g_strdup_printf ("%s/%s-%d%s", + directory_uri, + escaped_name, + copy, + extension); + + exists = test_uri_exists (path); + + copy++; + } + + g_free (escaped_name); + + return path; +} + char * nautilus_unique_temporary_file_name (void) { diff --git a/libnautilus-private/nautilus-file-utilities.h b/libnautilus-private/nautilus-file-utilities.h index 5b71e461d..7e3a6ed82 100644 --- a/libnautilus-private/nautilus-file-utilities.h +++ b/libnautilus-private/nautilus-file-utilities.h @@ -66,7 +66,13 @@ char * nautilus_pixmap_file (const char *partial_path); /* Locate a file in either the uers directory or the datadir. */ char * nautilus_get_data_file_path (const char *partial_path); -/* Return an allocated file name that is guranteed to be unique. */ +/* Return an allocated file name that is guranteed to be unique, but + * tries to make the name readable to users. + * This isn't race-free, so don't use for security-related things + */ +char * nautilus_ensure_unique_file_name (const char *directory_uri, + const char *base_name, + const char *extension); char * nautilus_unique_temporary_file_name (void); char * nautilus_find_file_in_gnome_path (char *file); GList * nautilus_find_all_files_in_gnome_path (char *file); diff --git a/libnautilus-private/nautilus-link-desktop-file.c b/libnautilus-private/nautilus-link-desktop-file.c index aa2bec0ff..a5d49e9f2 100644 --- a/libnautilus-private/nautilus-link-desktop-file.c +++ b/libnautilus-private/nautilus-link-desktop-file.c @@ -79,12 +79,13 @@ slurp_key_string (const char *uri, gboolean nautilus_link_desktop_file_local_create (const char *directory_uri, - const char *file_name, + const char *base_name, const char *display_name, const char *image, const char *target_uri, const GdkPoint *point, - int screen) + int screen, + gboolean unique_filename) { char *uri, *contents, *escaped_name; GnomeDesktopItem *desktop_item; @@ -92,13 +93,18 @@ nautilus_link_desktop_file_local_create (const char *directory_uri, NautilusFileChangesQueuePosition item; g_return_val_if_fail (directory_uri != NULL, FALSE); - g_return_val_if_fail (file_name != NULL, FALSE); + g_return_val_if_fail (base_name != NULL, FALSE); g_return_val_if_fail (display_name != NULL, FALSE); g_return_val_if_fail (target_uri != NULL, FALSE); - escaped_name = gnome_vfs_escape_string (file_name); - uri = g_strdup_printf ("%s/%s", directory_uri, escaped_name); - g_free (escaped_name); + if (unique_filename) { + uri = nautilus_ensure_unique_file_name (directory_uri, + base_name, ".desktop"); + } else { + escaped_name = gnome_vfs_escape_string (base_name); + uri = g_strdup_printf ("%s/%s.desktop", directory_uri, escaped_name); + g_free (escaped_name); + } contents = g_strdup_printf ("[Desktop Entry]\n" "Encoding=UTF-8\n" diff --git a/libnautilus-private/nautilus-link-desktop-file.h b/libnautilus-private/nautilus-link-desktop-file.h index 9e189414a..a0ab05700 100644 --- a/libnautilus-private/nautilus-link-desktop-file.h +++ b/libnautilus-private/nautilus-link-desktop-file.h @@ -28,12 +28,13 @@ #include <libnautilus-private/nautilus-link.h> gboolean nautilus_link_desktop_file_local_create (const char *directory_uri, - const char *file_name, + const char *base_name, const char *display_name, const char *image, const char *target_uri, const GdkPoint *point, - int screen); + int screen, + gboolean unique_filename); gboolean nautilus_link_desktop_file_local_set_text (const char *uri, const char *text); char * nautilus_link_desktop_file_local_get_text (const char *uri); diff --git a/libnautilus-private/nautilus-link.c b/libnautilus-private/nautilus-link.c index 199536b4e..521c40499 100644 --- a/libnautilus-private/nautilus-link.c +++ b/libnautilus-private/nautilus-link.c @@ -110,13 +110,15 @@ nautilus_link_local_create (const char *directory_uri, const char *image, const char *target_uri, const GdkPoint *point, - int screen) + int screen, + gboolean unique_filename) { return nautilus_link_desktop_file_local_create (directory_uri, file_name, display_name, image, target_uri, - point, screen); + point, screen, + unique_filename); } /* returns additional text to display under the name, NULL if none */ diff --git a/libnautilus-private/nautilus-link.h b/libnautilus-private/nautilus-link.h index 71741e1bb..c825ce3b9 100644 --- a/libnautilus-private/nautilus-link.h +++ b/libnautilus-private/nautilus-link.h @@ -39,7 +39,8 @@ gboolean nautilus_link_local_create (const char const char *image, const char *target_uri, const GdkPoint *point, - int screen); + int screen, + gboolean unique_filename); /* Returns additional text to display under the name, NULL if * none. Despite the fact that it takes a URI parameter, works only if |