diff options
author | Alexander Larsson <alexl@redhat.com> | 2004-08-11 13:56:14 +0000 |
---|---|---|
committer | Alexander Larsson <alexl@src.gnome.org> | 2004-08-11 13:56:14 +0000 |
commit | 32e5bad0ddb9953d7e1a5104c886bd887666490d (patch) | |
tree | 6b2b8f2f24f47cd50895e816228846c7372d539c /cut-n-paste-code | |
parent | 7c9ea47fa0855adc8686b59efc6ebfe352999727 (diff) | |
download | nautilus-32e5bad0ddb9953d7e1a5104c886bd887666490d.tar.gz |
Update to latest libegg code.
2004-08-11 Alexander Larsson <alexl@redhat.com>
* cut-n-paste-code/libegg/egg-recent-item.c: (egg_recent_item_new),
(make_valid_utf8), (egg_recent_item_get_short_name),
(egg_recent_item_set_private), (egg_recent_item_get_private):
* cut-n-paste-code/libegg/egg-recent-item.h:
* cut-n-paste-code/libegg/egg-recent-model.c:
(egg_recent_model_open_file), (egg_recent_model_lock_file),
(egg_recent_model_unlock_file):
Update to latest libegg code.
Diffstat (limited to 'cut-n-paste-code')
-rw-r--r-- | cut-n-paste-code/libegg/egg-recent-item.c | 100 | ||||
-rw-r--r-- | cut-n-paste-code/libegg/egg-recent-item.h | 3 | ||||
-rw-r--r-- | cut-n-paste-code/libegg/egg-recent-model.c | 32 |
3 files changed, 119 insertions, 16 deletions
diff --git a/cut-n-paste-code/libegg/egg-recent-item.c b/cut-n-paste-code/libegg/egg-recent-item.c index 139b637e1..e61c1f816 100644 --- a/cut-n-paste-code/libegg/egg-recent-item.c +++ b/cut-n-paste-code/libegg/egg-recent-item.c @@ -36,7 +36,7 @@ egg_recent_item_new (void) item = g_new (EggRecentItem, 1); item->groups = NULL; - item->private = FALSE; + item->private_data = FALSE; item->uri = NULL; item->mime_type = NULL; @@ -133,7 +133,7 @@ egg_recent_item_copy (const EggRecentItem *item) if (item->mime_type) newitem->mime_type = g_strdup (item->mime_type); newitem->timestamp = item->timestamp; - newitem->private = item->private; + newitem->private_data = item->private_data; newitem->groups = egg_recent_item_copy_groups (item->groups); return newitem; @@ -238,6 +238,98 @@ egg_recent_item_get_uri_for_display (const EggRecentItem *item) return gnome_vfs_format_uri_for_display (item->uri); } +/* Stolen from gnome_vfs_make_valid_utf8() */ +static char * +make_valid_utf8 (const char *name) +{ + GString *string; + const char *remainder, *invalid; + int remaining_bytes, valid_bytes; + + string = NULL; + remainder = name; + remaining_bytes = strlen (name); + + while (remaining_bytes != 0) { + if (g_utf8_validate (remainder, remaining_bytes, &invalid)) + break; + + valid_bytes = invalid - remainder; + + if (string == NULL) + string = g_string_sized_new (remaining_bytes); + + g_string_append_len (string, remainder, valid_bytes); + g_string_append_c (string, '?'); + + remaining_bytes -= valid_bytes + 1; + remainder = invalid + 1; + } + + if (string == NULL) + return g_strdup (name); + + g_string_append (string, remainder); +/* g_string_append (string, _(" (invalid file name)")); */ + g_assert (g_utf8_validate (string->str, -1, NULL)); + + return g_string_free (string, FALSE); +} + +/** + * egg_recent_item_get_short_name: + * @item: an #EggRecentItem + * + * Computes a valid UTF-8 string that can be used as the name of the item in a + * menu or list. For example, calling this function on an item that refers to + * "file:///foo/bar.txt" will yield "bar.txt". + * + * Return value: A newly-allocated string in UTF-8 encoding; free it with + * g_free(). + **/ +gchar * +egg_recent_item_get_short_name (const EggRecentItem *item) +{ + GnomeVFSURI *uri; + char *short_name; + gboolean valid; + + g_return_val_if_fail (item != NULL, NULL); + + if (item->uri == NULL) + return NULL; + + uri = gnome_vfs_uri_new (item->uri); + g_assert (uri != NULL); /* We already checked this in egg_recent_item_set_uri() */ + + short_name = gnome_vfs_uri_extract_short_name (uri); + valid = FALSE; + + if (strcmp (gnome_vfs_uri_get_scheme (uri), "file") == 0) { + char *tmp; + + tmp = g_filename_to_utf8 (short_name, -1, NULL, NULL, NULL); + if (tmp) { + g_free (short_name); + short_name = tmp; + valid = TRUE; + } + } + + if (!valid) { + char *tmp; + + tmp = make_valid_utf8 (short_name); + g_assert (tmp != NULL); + g_free (short_name); + short_name = tmp; + } + + g_free (uri); + + return short_name; +} + void egg_recent_item_set_mime_type (EggRecentItem *item, const gchar *mime) { @@ -323,13 +415,13 @@ egg_recent_item_remove_group (EggRecentItem *item, const gchar *group_name) void egg_recent_item_set_private (EggRecentItem *item, gboolean priv) { - item->private = priv; + item->private_data = priv; } gboolean egg_recent_item_get_private (const EggRecentItem *item) { - return item->private; + return item->private_data; } GType diff --git a/cut-n-paste-code/libegg/egg-recent-item.h b/cut-n-paste-code/libegg/egg-recent-item.h index 556c31e2b..5b3b405c9 100644 --- a/cut-n-paste-code/libegg/egg-recent-item.h +++ b/cut-n-paste-code/libegg/egg-recent-item.h @@ -22,7 +22,7 @@ struct _EggRecentItem { gchar *mime_type; time_t timestamp; - gboolean private; + gboolean private_data; GList *groups; @@ -44,6 +44,7 @@ gboolean egg_recent_item_set_uri (EggRecentItem *item, const gchar *uri); gchar * egg_recent_item_get_uri (const EggRecentItem *item); gchar * egg_recent_item_get_uri_utf8 (const EggRecentItem *item); gchar * egg_recent_item_get_uri_for_display (const EggRecentItem *item); +gchar * egg_recent_item_get_short_name (const EggRecentItem *item); void egg_recent_item_set_mime_type (EggRecentItem *item, const gchar *mime); gchar * egg_recent_item_get_mime_type (const EggRecentItem *item); diff --git a/cut-n-paste-code/libegg/egg-recent-model.c b/cut-n-paste-code/libegg/egg-recent-model.c index fcc2f9581..f36053bc2 100644 --- a/cut-n-paste-code/libegg/egg-recent-model.c +++ b/cut-n-paste-code/libegg/egg-recent-model.c @@ -857,14 +857,17 @@ static FILE * egg_recent_model_open_file (EggRecentModel *model) { FILE *file; + mode_t prev_umask; file = fopen (model->priv->path, "r+"); if (file == NULL) { /* be paranoid */ - umask (077); + prev_umask = umask (077); file = fopen (model->priv->path, "w+"); + umask (prev_umask); + g_return_val_if_fail (file != NULL, NULL); } @@ -875,27 +878,34 @@ static gboolean egg_recent_model_lock_file (FILE *file) { int fd; - gboolean locked = FALSE; - gint try = 4; + gint try = 5; rewind (file); fd = fileno (file); - /* Attempt to lock the file 4 times, - * waiting 1 second in between attempts. + /* Attempt to lock the file 5 times, + * waiting a random interval (< 1 second) + * in between attempts. * We should really be doing asynchronous * locking, but requires substantially larger * changes. */ - while (try-- > 0) + while (try > 0) { - if ((locked = lockf (fd, F_TLOCK, 0) < 0 ? FALSE : TRUE)); - break; - sleep (1); + int rand_interval; + + if (lockf (fd, F_TLOCK, 0) == 0) + return TRUE; + + rand_interval = 1 + (int) (10.0 * rand()/(RAND_MAX + 1.0)); + + g_usleep (100000 * rand_interval); + + --try; } - return locked; + return FALSE; } static gboolean @@ -906,7 +916,7 @@ egg_recent_model_unlock_file (FILE *file) rewind (file); fd = fileno (file); - return lockf (fd, F_ULOCK, 0) < 0 ? FALSE : TRUE; + return (lockf (fd, F_ULOCK, 0) == 0) ? TRUE : FALSE; } static void |