* plugins/debug-manager/command.c,
[anjuta-git-plugin.git] / libegg / egg-recent-item.c
blob1ec9ddcc44aeeda17b2ed30dd77516856f2c249f
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /**
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.
17 * Authors:
18 * James Willcox <jwillcox@cs.indiana.edu>
22 #include <stdio.h>
23 #include <string.h>
24 #include <glib.h>
25 #include <libgnomevfs/gnome-vfs.h>
26 #include <libgnomevfs/gnome-vfs-mime-utils.h>
27 #include "egg-recent-item.h"
31 EggRecentItem *
32 egg_recent_item_new (void)
34 EggRecentItem *item;
36 item = g_new (EggRecentItem, 1);
38 item->groups = NULL;
39 item->private_data = FALSE;
40 item->uri = NULL;
41 item->mime_type = NULL;
43 item->refcount = 1;
45 return item;
48 static void
49 egg_recent_item_free (EggRecentItem *item)
51 if (item->uri)
52 g_free (item->uri);
54 if (item->mime_type)
55 g_free (item->mime_type);
57 if (item->groups) {
58 g_list_foreach (item->groups, (GFunc)g_free, NULL);
59 g_list_free (item->groups);
60 item->groups = NULL;
63 g_free (item);
66 EggRecentItem *
67 egg_recent_item_ref (EggRecentItem *item)
69 item->refcount++;
70 return item;
73 EggRecentItem *
74 egg_recent_item_unref (EggRecentItem *item)
76 item->refcount--;
78 if (item->refcount == 0) {
79 egg_recent_item_free (item);
82 return item;
86 EggRecentItem *
87 egg_recent_item_new_from_uri (const gchar *uri)
89 EggRecentItem *item;
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);
97 return NULL;
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);
105 return item;
109 static GList *
110 egg_recent_item_copy_groups (const GList *list)
112 GList *newlist = NULL;
114 while (list) {
115 gchar *group = (gchar *)list->data;
117 newlist = g_list_prepend (newlist, g_strdup (group));
119 list = list->next;
122 return newlist;
126 EggRecentItem *
127 egg_recent_item_copy (const EggRecentItem *item)
129 EggRecentItem *newitem;
131 newitem = egg_recent_item_new ();
132 newitem->uri = g_strdup (item->uri);
133 if (item->mime_type)
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);
139 return newitem;
144 EggRecentItem *
145 egg_recent_item_new_valist (const gchar *uri, va_list args)
147 EggRecentItem *item;
148 EggRecentArg arg;
149 gchar *str1;
150 gchar *str2;
151 gboolean priv;
153 item = egg_recent_item_new ();
155 arg = va_arg (args, EggRecentArg);
157 while (arg != EGG_RECENT_ARG_NONE) {
158 switch (arg) {
159 case EGG_RECENT_ARG_MIME_TYPE:
160 str1 = va_arg (args, gchar*);
162 egg_recent_item_set_mime_type (item, str1);
163 break;
164 case EGG_RECENT_ARG_GROUP:
165 str1 = va_arg (args, gchar*);
167 egg_recent_item_add_group (item, str1);
168 break;
169 case EGG_RECENT_ARG_PRIVATE:
170 priv = va_arg (args, gboolean);
172 egg_recent_item_set_private (item, priv);
173 break;
174 default:
175 break;
178 arg = va_arg (args, EggRecentArg);
181 return item;
185 gboolean
186 egg_recent_item_set_uri (EggRecentItem *item, const gchar *uri)
188 gchar *utf8_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);
193 } else {
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");
198 return FALSE;
201 if (g_utf8_validate (utf8_uri, -1, NULL)) {
202 item->uri = gnome_vfs_make_uri_from_input (utf8_uri);
203 } else {
204 g_free (utf8_uri);
205 return FALSE;
208 g_free (utf8_uri);
211 return TRUE;
214 gchar *
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)
223 return item->uri;
226 gchar *
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
230 * once in set_uri()
232 return g_filename_to_utf8 (item->uri, -1, NULL, NULL, NULL);
235 gchar *
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() */
242 static char *
243 make_valid_utf8 (const char *name)
245 GString *string;
246 const char *remainder, *invalid;
247 int remaining_bytes, valid_bytes;
249 string = NULL;
250 remainder = name;
251 remaining_bytes = strlen (name);
253 while (remaining_bytes != 0) {
254 if (g_utf8_validate (remainder, remaining_bytes, &invalid))
255 break;
257 valid_bytes = invalid - remainder;
259 if (string == NULL)
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;
269 if (string == NULL)
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
288 * g_free().
290 gchar *
291 egg_recent_item_get_short_name (const EggRecentItem *item)
293 GnomeVFSURI *uri;
294 char *short_name;
295 gboolean valid;
297 g_return_val_if_fail (item != NULL, NULL);
299 if (item->uri == NULL)
300 return NULL;
302 uri = gnome_vfs_uri_new (item->uri);
303 if (uri == NULL)
304 return NULL;
306 short_name = gnome_vfs_uri_extract_short_name (uri);
307 valid = FALSE;
309 if (strcmp (gnome_vfs_uri_get_scheme (uri), "file") == 0) {
310 char *tmp;
312 tmp = g_filename_to_utf8 (short_name, -1, NULL, NULL, NULL);
313 if (tmp) {
314 g_free (short_name);
315 short_name = tmp;
316 valid = TRUE;
320 if (!valid) {
321 char *tmp;
323 tmp = make_valid_utf8 (short_name);
324 g_assert (tmp != NULL);
325 g_free (short_name);
326 short_name = tmp;
329 gnome_vfs_uri_unref (uri);
331 return short_name;
334 void
335 egg_recent_item_set_mime_type (EggRecentItem *item, const gchar *mime)
337 item->mime_type = g_strdup (mime);
340 gchar *
341 egg_recent_item_get_mime_type (const EggRecentItem *item)
343 return g_strdup (item->mime_type);
346 void
347 egg_recent_item_set_timestamp (EggRecentItem *item, time_t timestamp)
349 if (timestamp == (time_t) -1)
350 time (&timestamp);
352 item->timestamp = timestamp;
355 time_t
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)
364 return item->groups;
367 gboolean
368 egg_recent_item_in_group (const EggRecentItem *item, const gchar *group_name)
370 GList *tmp;
372 tmp = item->groups;
373 while (tmp != NULL) {
374 gchar *val = (gchar *)tmp->data;
376 if (strcmp (group_name, val) == 0)
377 return TRUE;
379 tmp = tmp->next;
382 return FALSE;
385 void
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));
394 void
395 egg_recent_item_remove_group (EggRecentItem *item, const gchar *group_name)
397 GList *tmp;
399 g_return_if_fail (group_name != NULL);
401 tmp = item->groups;
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,
407 val);
408 g_free (val);
409 break;
412 tmp = tmp->next;
416 void
417 egg_recent_item_set_private (EggRecentItem *item, gboolean priv)
419 item->private_data = priv;
422 gboolean
423 egg_recent_item_get_private (const EggRecentItem *item)
425 return item->private_data;
428 GType
429 egg_recent_item_get_type (void)
431 static GType boxed_type = 0;
433 if (!boxed_type) {
434 boxed_type = g_boxed_type_register_static ("EggRecentItem",
435 (GBoxedCopyFunc)egg_recent_item_ref,
436 (GBoxedFreeFunc)egg_recent_item_unref);
439 return boxed_type;