* plugins/symbol-browser/an_symbol_prefs.c: (refresh_tags_list),
[anjuta-git-plugin.git] / libegg / egg-recent-util.c
blobcb30e053fb1554450576cea155b38afbb9635f9e
1 #include <config.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <gtk/gtk.h>
5 #include <time.h>
6 #include <unistd.h>
7 #include <sys/types.h>
8 #ifndef USE_STABLE_LIBGNOMEUI
9 #include <libgnomeui/gnome-icon-theme.h>
10 #include <libgnomeui/gnome-icon-lookup.h>
11 #endif
12 #include <math.h>
13 #include "egg-recent-util.h"
15 #define EGG_RECENT_UTIL_HOSTNAME_SIZE 512
17 /* ripped out of gedit2 */
18 gchar*
19 egg_recent_util_escape_underlines (const gchar* text)
21 GString *str;
22 gint length;
23 const gchar *p;
24 const gchar *end;
26 g_return_val_if_fail (text != NULL, NULL);
28 length = strlen (text);
30 str = g_string_new ("");
32 p = text;
33 end = text + length;
35 while (p != end)
37 const gchar *next;
38 next = g_utf8_next_char (p);
40 switch (*p)
42 case '_':
43 g_string_append (str, "__");
44 break;
45 default:
46 g_string_append_len (str, p, next - p);
47 break;
50 p = next;
53 return g_string_free (str, FALSE);
56 #ifndef USE_STABLE_LIBGNOMEUI
57 static GdkPixbuf *
58 scale_icon (GdkPixbuf *pixbuf,
59 double *scale)
61 guint width, height;
63 width = gdk_pixbuf_get_width (pixbuf);
64 height = gdk_pixbuf_get_height (pixbuf);
66 width = floor (width * *scale + 0.5);
67 height = floor (height * *scale + 0.5);
69 return gdk_pixbuf_scale_simple (pixbuf, width, height, GDK_INTERP_BILINEAR);
72 static GdkPixbuf *
73 load_icon_file (char *filename,
74 guint base_size,
75 guint nominal_size)
77 GdkPixbuf *pixbuf, *scaled_pixbuf;
78 guint width, height, size;
79 double scale;
81 pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
83 if (pixbuf == NULL) {
84 return NULL;
87 if (base_size == 0) {
88 width = gdk_pixbuf_get_width (pixbuf);
89 height = gdk_pixbuf_get_height (pixbuf);
90 size = MAX (width, height);
91 if (size > nominal_size) {
92 base_size = size;
93 } else {
94 /* Don't scale up small icons */
95 base_size = nominal_size;
99 if (base_size != nominal_size) {
100 scale = (double)nominal_size/base_size;
101 scaled_pixbuf = scale_icon (pixbuf, &scale);
102 g_object_unref (pixbuf);
103 pixbuf = scaled_pixbuf;
106 return pixbuf;
109 GdkPixbuf *
110 egg_recent_util_get_icon (GnomeIconTheme *theme, const gchar *uri,
111 const gchar *mime_type, int size)
113 gchar *icon;
114 gchar *filename;
115 const GnomeIconData *icon_data;
116 int base_size;
117 GdkPixbuf *pixbuf;
119 icon = gnome_icon_lookup (theme, NULL, uri, NULL, NULL,
120 mime_type, 0, NULL);
123 g_return_val_if_fail (icon != NULL, NULL);
125 filename = gnome_icon_theme_lookup_icon (theme, icon,
126 size,
127 &icon_data,
128 &base_size);
129 g_free (icon);
131 if (filename == NULL) {
132 return NULL;
135 pixbuf = load_icon_file (filename, base_size, size);
136 g_free (filename);
139 return pixbuf;
141 #endif /* !USE_STABLE_LIBGNOMEUI */
143 gchar *
144 egg_recent_util_get_unique_id (void)
146 char hostname[EGG_RECENT_UTIL_HOSTNAME_SIZE];
147 time_t the_time;
148 guint32 rand;
149 int pid;
151 gethostname (hostname, EGG_RECENT_UTIL_HOSTNAME_SIZE);
153 time (&the_time);
154 rand = g_random_int ();
155 pid = getpid ();
157 return g_strdup_printf ("%s-%d-%d-%d", hostname, (int)time, (int)rand, (int)pid);