~ubuntu-branches/ubuntu/quantal/gbonds/quantal

« back to all changes in this revision

Viewing changes to src/recent-files/egg-recent-util.c

  • Committer: Bazaar Package Importer
  • Author(s): Richard Laager
  • Date: 2007-03-14 23:50:34 UTC
  • Revision ID: james.westby@ubuntu.com-20070314235034-997qegw33jx0wb9r
Tags: upstream-2.0.2
ImportĀ upstreamĀ versionĀ 2.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
scale_icon (GdkPixbuf *pixbuf,
 
59
            double *scale)
 
60
{
 
61
        guint width, height;
 
62
 
 
63
        width = gdk_pixbuf_get_width (pixbuf);
 
64
        height = gdk_pixbuf_get_height (pixbuf);
 
65
 
 
66
        width = floor (width * *scale + 0.5);
 
67
        height = floor (height * *scale + 0.5);
 
68
        
 
69
        return gdk_pixbuf_scale_simple (pixbuf, width, height, GDK_INTERP_BILINEAR);
 
70
}
 
71
 
 
72
static GdkPixbuf *
 
73
load_icon_file (char          *filename,
 
74
                guint          base_size,
 
75
                guint          nominal_size)
 
76
{
 
77
        GdkPixbuf *pixbuf, *scaled_pixbuf;
 
78
        guint width, height, size;
 
79
        double scale;
 
80
 
 
81
        pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
 
82
 
 
83
        if (pixbuf == NULL) {
 
84
                return NULL;
 
85
        }
 
86
        
 
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;
 
96
                }
 
97
        }
 
98
        
 
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;
 
104
        }
 
105
 
 
106
        return pixbuf;
 
107
}
 
108
 
 
109
GdkPixbuf *
 
110
egg_recent_util_get_icon (GnomeIconTheme *theme, const gchar *uri,
 
111
                          const gchar *mime_type, int size)
 
112
{
 
113
        gchar *icon;
 
114
        gchar *filename;
 
115
        const GnomeIconData *icon_data;
 
116
        int base_size;
 
117
        GdkPixbuf *pixbuf;
 
118
        
 
119
        icon = gnome_icon_lookup (theme, NULL, uri, NULL, NULL,
 
120
                                  mime_type, 0, NULL);
 
121
        
 
122
 
 
123
        g_return_val_if_fail (icon != NULL, NULL);
 
124
 
 
125
        filename = gnome_icon_theme_lookup_icon (theme, icon,
 
126
                                                 size,
 
127
                                                 &icon_data,
 
128
                                                 &base_size);
 
129
        g_free (icon);
 
130
 
 
131
        if (filename == NULL) {
 
132
                return NULL;
 
133
        }
 
134
 
 
135
        pixbuf = load_icon_file (filename, base_size, size);
 
136
        g_free (filename);
 
137
        
 
138
        
 
139
        return pixbuf;
 
140
}
 
141
#endif /* !USE_STABLE_LIBGNOMEUI */
 
142
 
 
143
gchar *
 
144
egg_recent_util_get_unique_id (void)
 
145
{
 
146
        char hostname[EGG_RECENT_UTIL_HOSTNAME_SIZE];
 
147
        time_t the_time;
 
148
        guint32 rand;
 
149
        int pid;
 
150
        
 
151
        gethostname (hostname, EGG_RECENT_UTIL_HOSTNAME_SIZE);
 
152
        
 
153
        time (&the_time);
 
154
        rand = g_random_int ();
 
155
        pid = getpid ();
 
156
 
 
157
        return g_strdup_printf ("%s-%d-%d-%d", hostname, (int)time, (int)rand, (int)pid);
 
158
}