1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as
5 * published by the Free Software Foundation; either version 2 of the
6 * License, or (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18 * James Willcox <jwillcox@cs.indiana.edu>
25 #include <libgnomevfs/gnome-vfs.h>
26 #include <libgnomevfs/gnome-vfs-mime-utils.h>
27 #include "egg-recent-item.h"
32 egg_recent_item_new (void)
36 item = g_new (EggRecentItem, 1);
39 item->private_data = FALSE;
41 item->mime_type = NULL;
42 item->mime_type_is_explicit = FALSE;
50 egg_recent_item_free (EggRecentItem *item)
56 g_free (item->mime_type);
59 g_list_foreach (item->groups, (GFunc)g_free, NULL);
60 g_list_free (item->groups);
68 egg_recent_item_ref (EggRecentItem *item)
75 egg_recent_item_unref (EggRecentItem *item)
79 if (item->refcount == 0) {
80 egg_recent_item_free (item);
88 egg_recent_item_new_from_uri (const gchar *uri)
92 g_return_val_if_fail (uri != NULL, NULL);
94 item = egg_recent_item_new ();
96 if (!egg_recent_item_set_uri (item ,uri)) {
97 egg_recent_item_free (item);
105 egg_recent_item_update_mime_type (EggRecentItem *item)
107 if (!item->mime_type_is_explicit) {
108 g_free (item->mime_type);
109 item->mime_type = NULL;
112 item->mime_type = gnome_vfs_get_mime_type (item->uri);
114 if (!item->mime_type)
115 item->mime_type = g_strdup (GNOME_VFS_MIME_TYPE_UNKNOWN);
120 egg_recent_item_set_uri (EggRecentItem *item, const gchar *uri)
124 /* if G_BROKEN_FILENAMES is not set, this should succede */
125 if (g_utf8_validate (uri, -1, NULL)) {
126 item->uri = gnome_vfs_make_uri_from_input (uri);
128 utf8_uri = g_filename_to_utf8 (uri, -1, NULL, NULL, NULL);
130 if (utf8_uri == NULL) {
131 g_warning ("Couldn't convert URI to UTF-8");
135 if (g_utf8_validate (utf8_uri, -1, NULL)) {
136 item->uri = gnome_vfs_make_uri_from_input (utf8_uri);
149 egg_recent_item_get_uri (const EggRecentItem *item)
151 return g_strdup (item->uri);
154 G_CONST_RETURN gchar *
155 egg_recent_item_peek_uri (const EggRecentItem *item)
161 egg_recent_item_get_uri_utf8 (const EggRecentItem *item)
163 /* this could fail, but it's not likely, since we've already done it
166 return g_filename_to_utf8 (item->uri, -1, NULL, NULL, NULL);
170 egg_recent_item_get_uri_for_display (const EggRecentItem *item)
172 return gnome_vfs_format_uri_for_display (item->uri);
175 /* Stolen from gnome_vfs_make_valid_utf8() */
177 make_valid_utf8 (const char *name)
180 const char *remainder, *invalid;
181 int remaining_bytes, valid_bytes;
185 remaining_bytes = name ? strlen (name) : 0;
187 while (remaining_bytes != 0) {
188 if (g_utf8_validate (remainder, remaining_bytes, &invalid))
191 valid_bytes = invalid - remainder;
194 string = g_string_sized_new (remaining_bytes);
196 g_string_append_len (string, remainder, valid_bytes);
197 g_string_append_c (string, '?');
199 remaining_bytes -= valid_bytes + 1;
200 remainder = invalid + 1;
204 return g_strdup (name);
206 g_string_append (string, remainder);
207 /* g_string_append (string, _(" (invalid file name)")); */
208 g_assert (g_utf8_validate (string->str, -1, NULL));
210 return g_string_free (string, FALSE);
214 get_uri_shortname_for_display (GnomeVFSURI *uri)
220 name = gnome_vfs_uri_extract_short_name (uri);
224 name = gnome_vfs_uri_to_string (uri, GNOME_VFS_URI_HIDE_PASSWORD);
226 else if (g_ascii_strcasecmp (uri->method_string, "file") == 0)
230 text_uri = gnome_vfs_uri_to_string (uri, GNOME_VFS_URI_HIDE_PASSWORD);
231 local_file = gnome_vfs_get_local_path_from_uri (text_uri);
233 if (local_file != NULL)
236 name = g_filename_display_basename (local_file);
243 else if (!gnome_vfs_uri_has_parent (uri))
247 method = uri->method_string;
250 strcmp (name, GNOME_VFS_URI_PATH_STR) == 0)
253 name = g_strdup (method);
260 name = g_strdup_printf ("%s: %s", method, name);
265 if (!validated && !g_utf8_validate (name, -1, NULL))
269 utf8_name = make_valid_utf8 (name);
278 * egg_recent_item_get_short_name:
279 * @item: an #EggRecentItem
281 * Computes a valid UTF-8 string that can be used as the name of the item in a
282 * menu or list. For example, calling this function on an item that refers to
283 * "file:///foo/bar.txt" will yield "bar.txt".
285 * Return value: A newly-allocated string in UTF-8 encoding; free it with
289 egg_recent_item_get_short_name (const EggRecentItem *item)
294 g_return_val_if_fail (item != NULL, NULL);
296 if (item->uri == NULL)
299 uri = gnome_vfs_uri_new (item->uri);
303 short_name = get_uri_shortname_for_display (uri);
305 gnome_vfs_uri_unref (uri);
311 egg_recent_item_set_mime_type (EggRecentItem *item, const gchar *mime)
313 g_free (item->mime_type);
314 item->mime_type = NULL;
316 if (mime && mime[0]) {
317 item->mime_type_is_explicit = TRUE;
318 item->mime_type = g_strdup (mime);
320 item->mime_type_is_explicit = FALSE;
325 egg_recent_item_get_mime_type (EggRecentItem *item)
327 egg_recent_item_update_mime_type (item);
329 return g_strdup (item->mime_type);
333 egg_recent_item_set_timestamp (EggRecentItem *item, time_t timestamp)
335 if (timestamp == (time_t) -1)
338 item->timestamp = timestamp;
342 egg_recent_item_get_timestamp (const EggRecentItem *item)
344 return item->timestamp;
347 G_CONST_RETURN GList *
348 egg_recent_item_get_groups (const EggRecentItem *item)
354 egg_recent_item_in_group (const EggRecentItem *item, const gchar *group_name)
359 while (tmp != NULL) {
360 gchar *val = (gchar *)tmp->data;
362 if (strcmp (group_name, val) == 0)
372 egg_recent_item_add_group (EggRecentItem *item, const gchar *group_name)
374 g_return_if_fail (group_name != NULL);
376 if (!egg_recent_item_in_group (item, group_name))
377 item->groups = g_list_append (item->groups, g_strdup (group_name));
381 egg_recent_item_remove_group (EggRecentItem *item, const gchar *group_name)
385 g_return_if_fail (group_name != NULL);
388 while (tmp != NULL) {
389 gchar *val = (gchar *)tmp->data;
391 if (strcmp (group_name, val) == 0) {
392 item->groups = g_list_remove (item->groups,
403 egg_recent_item_set_private (EggRecentItem *item, gboolean priv)
405 item->private_data = priv;
409 egg_recent_item_get_private (const EggRecentItem *item)
411 return item->private_data;
415 egg_recent_item_get_type (void)
417 static GType boxed_type = 0;
420 boxed_type = g_boxed_type_register_static ("EggRecentItem",
421 (GBoxedCopyFunc)egg_recent_item_ref,
422 (GBoxedFreeFunc)egg_recent_item_unref);