]> www.fi.muni.cz Git - evince.git/blob - cut-n-paste/recent-files/egg-recent-util.c
4597f03c2d7aa280910143036289e6841f73c190
[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 #ifndef USE_STABLE_LIBGNOMEUI
6 #include <libgnomeui/gnome-icon-lookup.h>
7 #endif
8 #include <time.h>
9 #include <unistd.h>
10 #include <sys/types.h>
11 #include <math.h>
12 #include "egg-recent-util.h"
13
14 #ifdef G_OS_WIN32
15 #include <windows.h>
16 #endif
17
18 #define EGG_RECENT_UTIL_HOSTNAME_SIZE 512
19
20 /* ripped out of gedit2 */
21 gchar* 
22 egg_recent_util_escape_underlines (const gchar* text)
23 {
24         GString *str;
25         gint length;
26         const gchar *p;
27         const gchar *end;
28
29         g_return_val_if_fail (text != NULL, NULL);
30
31         length = strlen (text);
32
33         str = g_string_new ("");
34
35         p = text;
36         end = text + length;
37
38         while (p != end)
39         {
40                 const gchar *next;
41                 next = g_utf8_next_char (p);
42
43                 switch (*p)
44                 {
45                         case '_':
46                                 g_string_append (str, "__");
47                                 break;
48                         default:
49                                 g_string_append_len (str, p, next - p);
50                         break;
51                 }
52
53                 p = next;
54         }
55
56         return g_string_free (str, FALSE);
57 }
58
59 GdkPixbuf *
60 egg_recent_util_get_icon (GtkIconTheme *theme, const gchar *uri,
61                           const gchar *mime_type, int size)
62 {
63 #ifndef USE_STABLE_LIBGNOMEUI
64         gchar *icon;
65         GdkPixbuf *pixbuf;
66         
67         icon = gnome_icon_lookup (theme, NULL, uri, NULL, NULL,
68                                   mime_type, 0, NULL);
69
70         g_return_val_if_fail (icon != NULL, NULL);
71
72         pixbuf = gtk_icon_theme_load_icon (theme, icon, size, 0, NULL);
73         g_free (icon);
74
75         return pixbuf;
76 #endif
77         return NULL;
78 }
79
80 gchar *
81 egg_recent_util_get_unique_id (void)
82 {
83         char hostname[EGG_RECENT_UTIL_HOSTNAME_SIZE];
84         time_t the_time;
85         guint32 rand;
86         int pid;
87         
88 #ifndef G_OS_WIN32
89         gethostname (hostname, EGG_RECENT_UTIL_HOSTNAME_SIZE);
90 #else
91         {
92                 DWORD size = EGG_RECENT_UTIL_HOSTNAME_SIZE;
93                 GetComputerName (hostname, &size);
94         }
95 #endif
96         
97         time (&the_time);
98         rand = g_random_int ();
99         pid = getpid ();
100
101         return g_strdup_printf ("%s-%d-%d-%d", hostname, (int)time, (int)rand, (int)pid);
102 }