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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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
;
49 egg_recent_item_free (EggRecentItem
*item
)
55 g_free (item
->mime_type
);
58 g_list_foreach (item
->groups
, (GFunc
)g_free
, NULL
);
59 g_list_free (item
->groups
);
67 egg_recent_item_ref (EggRecentItem
*item
)
74 egg_recent_item_unref (EggRecentItem
*item
)
78 if (item
->refcount
== 0) {
79 egg_recent_item_free (item
);
87 egg_recent_item_new_from_uri (const gchar
*uri
)
91 g_return_val_if_fail (uri
!= NULL
, NULL
);
93 item
= egg_recent_item_new ();
95 if (!egg_recent_item_set_uri (item
,uri
)) {
96 egg_recent_item_free (item
);
100 item
->mime_type
= gnome_vfs_get_mime_type (item
->uri
);
102 if (!item
->mime_type
)
103 item
->mime_type
= g_strdup (GNOME_VFS_MIME_TYPE_UNKNOWN
);
110 egg_recent_item_copy_groups (const GList *list)
112 GList *newlist = NULL;
115 gchar *group = (gchar *)list->data;
117 newlist = g_list_prepend (newlist, g_strdup (group));
127 egg_recent_item_copy (const EggRecentItem *item)
129 EggRecentItem *newitem;
131 newitem = egg_recent_item_new ();
132 newitem->uri = g_strdup (item->uri);
134 newitem->mime_type = g_strdup (item->mime_type);
135 newitem->timestamp = item->timestamp;
136 newitem->private_data = item->private_data;
137 newitem->groups = egg_recent_item_copy_groups (item->groups);
145 egg_recent_item_new_valist (const gchar *uri, va_list args)
153 item = egg_recent_item_new ();
155 arg = va_arg (args, EggRecentArg);
157 while (arg != EGG_RECENT_ARG_NONE) {
159 case EGG_RECENT_ARG_MIME_TYPE:
160 str1 = va_arg (args, gchar*);
162 egg_recent_item_set_mime_type (item, str1);
164 case EGG_RECENT_ARG_GROUP:
165 str1 = va_arg (args, gchar*);
167 egg_recent_item_add_group (item, str1);
169 case EGG_RECENT_ARG_PRIVATE:
170 priv = va_arg (args, gboolean);
172 egg_recent_item_set_private (item, priv);
178 arg = va_arg (args, EggRecentArg);
186 egg_recent_item_set_uri (EggRecentItem
*item
, const gchar
*uri
)
190 /* if G_BROKEN_FILENAMES is not set, this should succede */
191 if (g_utf8_validate (uri
, -1, NULL
)) {
192 item
->uri
= gnome_vfs_make_uri_from_input (uri
);
194 utf8_uri
= g_filename_to_utf8 (uri
, -1, NULL
, NULL
, NULL
);
196 if (utf8_uri
== NULL
) {
197 g_warning ("Couldn't convert URI to UTF-8");
201 if (g_utf8_validate (utf8_uri
, -1, NULL
)) {
202 item
->uri
= gnome_vfs_make_uri_from_input (utf8_uri
);
215 egg_recent_item_get_uri (const EggRecentItem
*item
)
217 return g_strdup (item
->uri
);
220 G_CONST_RETURN gchar
*
221 egg_recent_item_peek_uri (const EggRecentItem
*item
)
227 egg_recent_item_get_uri_utf8 (const EggRecentItem
*item
)
229 /* this could fail, but it's not likely, since we've already done it
232 return g_filename_to_utf8 (item
->uri
, -1, NULL
, NULL
, NULL
);
236 egg_recent_item_get_uri_for_display (const EggRecentItem
*item
)
238 return gnome_vfs_format_uri_for_display (item
->uri
);
241 /* Stolen from gnome_vfs_make_valid_utf8() */
243 make_valid_utf8 (const char *name
)
246 const char *remainder
, *invalid
;
247 int remaining_bytes
, valid_bytes
;
251 remaining_bytes
= strlen (name
);
253 while (remaining_bytes
!= 0) {
254 if (g_utf8_validate (remainder
, remaining_bytes
, &invalid
))
257 valid_bytes
= invalid
- remainder
;
260 string
= g_string_sized_new (remaining_bytes
);
262 g_string_append_len (string
, remainder
, valid_bytes
);
263 g_string_append_c (string
, '?');
265 remaining_bytes
-= valid_bytes
+ 1;
266 remainder
= invalid
+ 1;
270 return g_strdup (name
);
272 g_string_append (string
, remainder
);
273 /* g_string_append (string, _(" (invalid file name)")); */
274 g_assert (g_utf8_validate (string
->str
, -1, NULL
));
276 return g_string_free (string
, FALSE
);
280 * egg_recent_item_get_short_name:
281 * @item: an #EggRecentItem
283 * Computes a valid UTF-8 string that can be used as the name of the item in a
284 * menu or list. For example, calling this function on an item that refers to
285 * "file:///foo/bar.txt" will yield "bar.txt".
287 * Return value: A newly-allocated string in UTF-8 encoding; free it with
291 egg_recent_item_get_short_name (const EggRecentItem
*item
)
297 g_return_val_if_fail (item
!= NULL
, NULL
);
299 if (item
->uri
== NULL
)
302 uri
= gnome_vfs_uri_new (item
->uri
);
306 short_name
= gnome_vfs_uri_extract_short_name (uri
);
309 if (strcmp (gnome_vfs_uri_get_scheme (uri
), "file") == 0) {
312 tmp
= g_filename_to_utf8 (short_name
, -1, NULL
, NULL
, NULL
);
323 tmp
= make_valid_utf8 (short_name
);
324 g_assert (tmp
!= NULL
);
329 gnome_vfs_uri_unref (uri
);
335 egg_recent_item_set_mime_type (EggRecentItem
*item
, const gchar
*mime
)
337 item
->mime_type
= g_strdup (mime
);
341 egg_recent_item_get_mime_type (const EggRecentItem
*item
)
343 return g_strdup (item
->mime_type
);
347 egg_recent_item_set_timestamp (EggRecentItem
*item
, time_t timestamp
)
349 if (timestamp
== (time_t) -1)
352 item
->timestamp
= timestamp
;
356 egg_recent_item_get_timestamp (const EggRecentItem
*item
)
358 return item
->timestamp
;
361 G_CONST_RETURN GList
*
362 egg_recent_item_get_groups (const EggRecentItem
*item
)
368 egg_recent_item_in_group (const EggRecentItem
*item
, const gchar
*group_name
)
373 while (tmp
!= NULL
) {
374 gchar
*val
= (gchar
*)tmp
->data
;
376 if (strcmp (group_name
, val
) == 0)
386 egg_recent_item_add_group (EggRecentItem
*item
, const gchar
*group_name
)
388 g_return_if_fail (group_name
!= NULL
);
390 if (!egg_recent_item_in_group (item
, group_name
))
391 item
->groups
= g_list_append (item
->groups
, g_strdup (group_name
));
395 egg_recent_item_remove_group (EggRecentItem
*item
, const gchar
*group_name
)
399 g_return_if_fail (group_name
!= NULL
);
402 while (tmp
!= NULL
) {
403 gchar
*val
= (gchar
*)tmp
->data
;
405 if (strcmp (group_name
, val
) == 0) {
406 item
->groups
= g_list_remove (item
->groups
,
417 egg_recent_item_set_private (EggRecentItem
*item
, gboolean priv
)
419 item
->private_data
= priv
;
423 egg_recent_item_get_private (const EggRecentItem
*item
)
425 return item
->private_data
;
429 egg_recent_item_get_type (void)
431 static GType boxed_type
= 0;
434 boxed_type
= g_boxed_type_register_static ("EggRecentItem",
435 (GBoxedCopyFunc
)egg_recent_item_ref
,
436 (GBoxedFreeFunc
)egg_recent_item_unref
);