evolution-3.6.4/e-util/e-icon-factory.c

No issues found

  1 /*
  2  * This program is free software; you can redistribute it and/or
  3  * modify it under the terms of the GNU Lesser General Public
  4  * License as published by the Free Software Foundation; either
  5  * version 2 of the License, or (at your option) version 3.
  6  *
  7  * This program is distributed in the hope that it will be useful,
  8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 10  * Lesser General Public License for more details.
 11  *
 12  * You should have received a copy of the GNU Lesser General Public
 13  * License along with the program; if not, see <http://www.gnu.org/licenses/>
 14  *
 15  *
 16  * Authors:
 17  *		Jeffrey Stedfast <fejj@novell.com>
 18  *
 19  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 20  *
 21  */
 22 
 23 #ifdef HAVE_CONFIG_H
 24 #include <config.h>
 25 #endif
 26 
 27 #include <stdio.h>
 28 #include <stdlib.h>
 29 #include <string.h>
 30 #include <sys/types.h>
 31 #include <sys/stat.h>
 32 
 33 #define GNOME_DESKTOP_USE_UNSTABLE_API
 34 #include <libgnome-desktop/gnome-desktop-thumbnail.h>
 35 #undef GNOME_DESKTOP_USE_UNSTABLE_API
 36 
 37 #include <glib/gstdio.h>
 38 #include <gtk/gtk.h>
 39 
 40 #include "e-icon-factory.h"
 41 #include "e-util-private.h"
 42 
 43 #define d(x)
 44 
 45 /**
 46  * e_icon_factory_get_icon_filename:
 47  * @icon_name: name of the icon
 48  * @icon_size: size of the icon
 49  *
 50  * Returns the filename of the requested icon in the default icon theme.
 51  *
 52  * Returns: the filename of the requested icon
 53  **/
 54 gchar *
 55 e_icon_factory_get_icon_filename (const gchar *icon_name,
 56                                   GtkIconSize icon_size)
 57 {
 58 	GtkIconTheme *icon_theme;
 59 	GtkIconInfo *icon_info;
 60 	gchar *filename = NULL;
 61 	gint width, height;
 62 
 63 	g_return_val_if_fail (icon_name != NULL, NULL);
 64 
 65 	icon_theme = gtk_icon_theme_get_default ();
 66 
 67 	if (!gtk_icon_size_lookup (icon_size, &width, &height))
 68 		return NULL;
 69 
 70 	icon_info = gtk_icon_theme_lookup_icon (
 71 		icon_theme, icon_name, height, 0);
 72 	if (icon_info != NULL) {
 73 		filename = g_strdup (
 74 			gtk_icon_info_get_filename (icon_info));
 75 		gtk_icon_info_free (icon_info);
 76 	}
 77 
 78 	return filename;
 79 }
 80 
 81 /**
 82  * e_icon_factory_get_icon:
 83  * @icon_name: name of the icon
 84  * @icon_size: size of the icon
 85  *
 86  * Loads the requested icon from the default icon theme and renders it
 87  * to a pixbuf.
 88  *
 89  * Returns: the rendered icon
 90  **/
 91 GdkPixbuf *
 92 e_icon_factory_get_icon (const gchar *icon_name,
 93                          GtkIconSize icon_size)
 94 {
 95 	GtkIconTheme *icon_theme;
 96 	GdkPixbuf *pixbuf;
 97 	gint width, height;
 98 	GError *error = NULL;
 99 
100 	g_return_val_if_fail (icon_name != NULL, NULL);
101 
102 	icon_theme = gtk_icon_theme_get_default ();
103 
104 	if (!gtk_icon_size_lookup (icon_size, &width, &height))
105 		width = height = 16;
106 
107 	pixbuf = gtk_icon_theme_load_icon (
108 		icon_theme, icon_name, height, 0, &error);
109 
110 	if (error != NULL) {
111 		g_warning ("%s", error->message);
112 		g_clear_error (&error);
113 
114 		/* Fallback to missing image */
115 		pixbuf = gtk_icon_theme_load_icon (
116 			icon_theme, GTK_STOCK_MISSING_IMAGE,
117 			height, 0, &error);
118 
119 		if (error != NULL) {
120 			g_error ("%s", error->message);
121 			g_clear_error (&error);
122 		}
123 	}
124 
125 	return pixbuf;
126 }
127 
128 /**
129  * e_icon_factory_pixbuf_scale
130  * @pixbuf: a #GdkPixbuf
131  * @width: desired width, if less or equal to 0, then changed to 1
132  * @height: desired height, if less or equal to 0, then changed to 1
133  *
134  * Scales @pixbuf to desired size.
135  *
136  * Returns: a scaled #GdkPixbuf
137  **/
138 GdkPixbuf *
139 e_icon_factory_pixbuf_scale (GdkPixbuf *pixbuf,
140                              gint width,
141                              gint height)
142 {
143 	g_return_val_if_fail (pixbuf != NULL, NULL);
144 
145 	if (width <= 0)
146 		width = 1;
147 
148 	if (height <= 0)
149 		height = 1;
150 
151 	/* because this can only scale down, not up */
152 	if (gdk_pixbuf_get_width (pixbuf) > width && gdk_pixbuf_get_height (pixbuf) > height)
153 		return gnome_desktop_thumbnail_scale_down_pixbuf (pixbuf, width, height);
154 
155 	return gdk_pixbuf_scale_simple (pixbuf, width, height, GDK_INTERP_BILINEAR);
156 }
157 
158 /**
159  * e_icon_factory_create_thumbnail
160  * @filename: the file name to create the thumbnail for
161  *
162  * Creates system thumbnail for @filename.
163  *
164  * Returns: Path to system thumbnail of the file; %NULL if couldn't
165  *          create it. Free it with g_free().
166  **/
167 gchar *
168 e_icon_factory_create_thumbnail (const gchar *filename)
169 {
170 	static GnomeDesktopThumbnailFactory *thumbnail_factory = NULL;
171 	struct stat file_stat;
172 	gchar *thumbnail = NULL;
173 
174 	g_return_val_if_fail (filename != NULL, NULL);
175 
176 	if (thumbnail_factory == NULL) {
177 		thumbnail_factory = gnome_desktop_thumbnail_factory_new (GNOME_DESKTOP_THUMBNAIL_SIZE_NORMAL);
178 	}
179 
180 	if (g_stat (filename, &file_stat) != -1 && S_ISREG (file_stat.st_mode)) {
181 		gchar *content_type, *mime = NULL;
182 		gboolean uncertain = FALSE;
183 
184 		content_type = g_content_type_guess (filename, NULL, 0, &uncertain);
185 		if (content_type)
186 			mime = g_content_type_get_mime_type (content_type);
187 
188 		if (mime) {
189 			gchar *uri = g_filename_to_uri (filename, NULL, NULL);
190 
191 			g_return_val_if_fail (uri != NULL, NULL);
192 
193 			thumbnail = gnome_desktop_thumbnail_factory_lookup (thumbnail_factory, uri, file_stat.st_mtime);
194 			if (!thumbnail && gnome_desktop_thumbnail_factory_can_thumbnail (thumbnail_factory, uri, mime, file_stat.st_mtime)) {
195 				GdkPixbuf *pixbuf;
196 
197 				pixbuf = gnome_desktop_thumbnail_factory_generate_thumbnail (thumbnail_factory, uri, mime);
198 
199 				if (pixbuf) {
200 					gnome_desktop_thumbnail_factory_save_thumbnail (thumbnail_factory, pixbuf, uri, file_stat.st_mtime);
201 					g_object_unref (pixbuf);
202 
203 					thumbnail = gnome_desktop_thumbnail_factory_lookup (thumbnail_factory, uri, file_stat.st_mtime);
204 				}
205 			}
206 
207 			g_free (uri);
208 		}
209 
210 		g_free (content_type);
211 		g_free (mime);
212 	}
213 
214 	return thumbnail;
215 }