]> www.fi.muni.cz Git - evince.git/blob - cut-n-paste/recent-files/egg-recent-util.c
6780cd1ee02dcfd1deeacd16baa4720fd2f7c714
[evince.git] / cut-n-paste / recent-files / egg-recent-util.c
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"
14
15 #define EGG_RECENT_UTIL_HOSTNAME_SIZE 512
16
17 /* ripped out of gedit2 */
18 gchar* 
19 egg_recent_util_escape_underlines (const gchar* text)
20 {
21         GString *str;
22         gint length;
23         const gchar *p;
24         const gchar *end;
25
26         g_return_val_if_fail (text != NULL, NULL);
27
28         length = strlen (text);
29
30         str = g_string_new ("");
31
32         p = text;
33         end = text + length;
34
35         while (p != end)
36         {
37                 const gchar *next;
38                 next = g_utf8_next_char (p);
39
40                 switch (*p)
41                 {
42                         case '_':
43                                 g_string_append (str, "__");
44                                 break;
45                         default:
46                                 g_string_append_len (str, p, next - p);
47                         break;
48                 }
49
50                 p = next;
51         }
52
53         return g_string_free (str, FALSE);
54 }
55
56 #ifndef USE_STABLE_LIBGNOMEUI
57 static GdkPixbuf *
58 load_icon_file (char          *filename,
59                 guint          nominal_size)
60 {
61         GdkPixbuf *pixbuf, *scaled_pixbuf;
62         guint width, height;
63
64         pixbuf = gdk_pixbuf_new_from_file_at_size (filename, nominal_size, nominal_size, NULL);
65
66         if (pixbuf == NULL) {
67                 return NULL;
68         }
69         
70         width = gdk_pixbuf_get_width (pixbuf); 
71         height = gdk_pixbuf_get_height (pixbuf);
72         /* if the icon is larger than the nominal size, scale down */
73         if (MAX (width, height) > nominal_size) {
74                 if (width > height) {
75                         height = height * nominal_size / width;
76                         width = nominal_size;
77                 } else {
78                         width = width * nominal_size / height;
79                         height = nominal_size;
80                 }
81                 scaled_pixbuf = gdk_pixbuf_scale_simple
82                         (pixbuf, width, height, GDK_INTERP_BILINEAR);
83                 g_object_unref (pixbuf);
84                 pixbuf = scaled_pixbuf;
85         }
86
87         return pixbuf;
88 }
89
90 GdkPixbuf *
91 egg_recent_util_get_icon (GnomeIconTheme *theme, const gchar *uri,
92                           const gchar *mime_type, int size)
93 {
94         gchar *icon;
95         gchar *filename;
96         const GnomeIconData *icon_data;
97         GdkPixbuf *pixbuf;
98         
99         icon = gnome_icon_lookup (theme, NULL, uri, NULL, NULL,
100                                   mime_type, 0, NULL);
101         
102
103         g_return_val_if_fail (icon != NULL, NULL);
104
105         filename = gnome_icon_theme_lookup_icon (theme, icon,
106                                                  size,
107                                                  &icon_data,
108                                                  NULL);
109         g_free (icon);
110
111         if (filename == NULL) {
112                 return NULL;
113         }
114
115         pixbuf = load_icon_file (filename, size);
116         g_free (filename);
117         
118         
119         return pixbuf;
120 }
121 #endif /* !USE_STABLE_LIBGNOMEUI */
122
123 gchar *
124 egg_recent_util_get_unique_id (void)
125 {
126         char hostname[EGG_RECENT_UTIL_HOSTNAME_SIZE];
127         time_t the_time;
128         guint32 rand;
129         int pid;
130         
131         gethostname (hostname, EGG_RECENT_UTIL_HOSTNAME_SIZE);
132         
133         time (&the_time);
134         rand = g_random_int ();
135         pid = getpid ();
136
137         return g_strdup_printf ("%s-%d-%d-%d", hostname, (int)time, (int)rand, (int)pid);
138 }