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);
106 egg_recent_item_copy_groups (const GList *list)
108 GList *newlist = NULL;
111 gchar *group = (gchar *)list->data;
113 newlist = g_list_prepend (newlist, g_strdup (group));
123 egg_recent_item_copy (const EggRecentItem *item)
125 EggRecentItem *newitem;
127 newitem = egg_recent_item_new ();
128 newitem->uri = g_strdup (item->uri);
130 newitem->mime_type = g_strdup (item->mime_type);
131 newitem->mime_type_is_explicit = item->mime_type_is_explicit
132 newitem->timestamp = item->timestamp;
133 newitem->private_data = item->private_data;
134 newitem->groups = egg_recent_item_copy_groups (item->groups);
142 egg_recent_item_new_valist (const gchar *uri, va_list args)
150 item = egg_recent_item_new ();
152 arg = va_arg (args, EggRecentArg);
154 while (arg != EGG_RECENT_ARG_NONE) {
156 case EGG_RECENT_ARG_MIME_TYPE:
157 str1 = va_arg (args, gchar*);
159 egg_recent_item_set_mime_type (item, str1);
161 case EGG_RECENT_ARG_GROUP:
162 str1 = va_arg (args, gchar*);
164 egg_recent_item_add_group (item, str1);
166 case EGG_RECENT_ARG_PRIVATE:
167 priv = va_arg (args, gboolean);
169 egg_recent_item_set_private (item, priv);
175 arg = va_arg (args, EggRecentArg);
183 egg_recent_item_update_mime_type (EggRecentItem *item)
185 if (!item->mime_type_is_explicit) {
186 g_free (item->mime_type);
187 item->mime_type = NULL;
190 item->mime_type = gnome_vfs_get_mime_type (item->uri);
192 if (!item->mime_type)
193 item->mime_type = g_strdup (GNOME_VFS_MIME_TYPE_UNKNOWN);
198 egg_recent_item_set_uri (EggRecentItem *item, const gchar *uri)
202 /* if G_BROKEN_FILENAMES is not set, this should succede */
203 if (g_utf8_validate (uri, -1, NULL)) {
204 item->uri = gnome_vfs_make_uri_from_input (uri);
206 utf8_uri = g_filename_to_utf8 (uri, -1, NULL, NULL, NULL);
208 if (utf8_uri == NULL) {
209 g_warning ("Couldn't convert URI to UTF-8");
213 if (g_utf8_validate (utf8_uri, -1, NULL)) {
214 item->uri = gnome_vfs_make_uri_from_input (utf8_uri);
227 egg_recent_item_get_uri (const EggRecentItem *item)
229 return g_strdup (item->uri);
232 G_CONST_RETURN gchar *
233 egg_recent_item_peek_uri (const EggRecentItem *item)
239 egg_recent_item_get_uri_utf8 (const EggRecentItem *item)
241 /* this could fail, but it's not likely, since we've already done it
244 return g_filename_to_utf8 (item->uri, -1, NULL, NULL, NULL);
248 egg_recent_item_get_uri_for_display (const EggRecentItem *item)
250 return gnome_vfs_format_uri_for_display (item->uri);
253 /* Stolen from gnome_vfs_make_valid_utf8() */
255 make_valid_utf8 (const char *name)
258 const char *remainder, *invalid;
259 int remaining_bytes, valid_bytes;
263 remaining_bytes = name ? strlen (name) : 0;
265 while (remaining_bytes != 0) {
266 if (g_utf8_validate (remainder, remaining_bytes, &invalid))
269 valid_bytes = invalid - remainder;
272 string = g_string_sized_new (remaining_bytes);
274 g_string_append_len (string, remainder, valid_bytes);
275 g_string_append_c (string, '?');
277 remaining_bytes -= valid_bytes + 1;
278 remainder = invalid + 1;
282 return g_strdup (name);
284 g_string_append (string, remainder);
285 /* g_string_append (string, _(" (invalid file name)")); */
286 g_assert (g_utf8_validate (string->str, -1, NULL));
288 return g_string_free (string, FALSE);
292 * egg_recent_item_get_short_name:
293 * @item: an #EggRecentItem
295 * Computes a valid UTF-8 string that can be used as the name of the item in a
296 * menu or list. For example, calling this function on an item that refers to
297 * "file:///foo/bar.txt" will yield "bar.txt".
299 * Return value: A newly-allocated string in UTF-8 encoding; free it with
303 egg_recent_item_get_short_name (const EggRecentItem *item)
309 g_return_val_if_fail (item != NULL, NULL);
311 if (item->uri == NULL)
314 uri = gnome_vfs_uri_new (item->uri);
318 short_name = gnome_vfs_uri_extract_short_name (uri);
319 if (short_name == NULL) {
320 gnome_vfs_uri_unref (uri);
326 if (strcmp (gnome_vfs_uri_get_scheme (uri), "file") == 0) {
329 tmp = g_filename_to_utf8 (short_name, -1, NULL, NULL, NULL);
340 tmp = make_valid_utf8 (short_name);
341 g_assert (tmp != NULL);
346 gnome_vfs_uri_unref (uri);
352 egg_recent_item_set_mime_type (EggRecentItem *item, const gchar *mime)
354 g_free (item->mime_type);
355 item->mime_type = NULL;
357 if (mime && mime[0]) {
358 item->mime_type_is_explicit = TRUE;
359 item->mime_type = g_strdup (mime);
361 item->mime_type_is_explicit = FALSE;
366 egg_recent_item_get_mime_type (EggRecentItem *item)
368 egg_recent_item_update_mime_type (item);
370 return g_strdup (item->mime_type);
374 egg_recent_item_set_timestamp (EggRecentItem *item, time_t timestamp)
376 if (timestamp == (time_t) -1)
379 item->timestamp = timestamp;
383 egg_recent_item_get_timestamp (const EggRecentItem *item)
385 return item->timestamp;
388 G_CONST_RETURN GList *
389 egg_recent_item_get_groups (const EggRecentItem *item)
395 egg_recent_item_in_group (const EggRecentItem *item, const gchar *group_name)
400 while (tmp != NULL) {
401 gchar *val = (gchar *)tmp->data;
403 if (strcmp (group_name, val) == 0)
413 egg_recent_item_add_group (EggRecentItem *item, const gchar *group_name)
415 g_return_if_fail (group_name != NULL);
417 if (!egg_recent_item_in_group (item, group_name))
418 item->groups = g_list_append (item->groups, g_strdup (group_name));
422 egg_recent_item_remove_group (EggRecentItem *item, const gchar *group_name)
426 g_return_if_fail (group_name != NULL);
429 while (tmp != NULL) {
430 gchar *val = (gchar *)tmp->data;
432 if (strcmp (group_name, val) == 0) {
433 item->groups = g_list_remove (item->groups,
444 egg_recent_item_set_private (EggRecentItem *item, gboolean priv)
446 item->private_data = priv;
450 egg_recent_item_get_private (const EggRecentItem *item)
452 return item->private_data;
456 egg_recent_item_get_type (void)
458 static GType boxed_type = 0;
461 boxed_type = g_boxed_type_register_static ("EggRecentItem",
462 (GBoxedCopyFunc)egg_recent_item_ref,
463 (GBoxedFreeFunc)egg_recent_item_unref);