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