1 /*
2  * gedit-file-bookmarks-utils.c - Gedit plugin providing easy file access
3  * from the sidepanel
4  *
5  * Copyright (C) 2006 - Jesse van den Kieboom <jesse@icecrew.nl>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include "config.h"
22 
23 #include <glib/gi18n-lib.h>
24 #include <gedit/gedit-utils.h>
25 
26 #include "gedit-file-browser-utils.h"
27 
28 static GdkPixbuf *
process_icon_pixbuf(GdkPixbuf * pixbuf,gchar const * name,gint size,GError * error)29 process_icon_pixbuf (GdkPixbuf   *pixbuf,
30 		     gchar const *name,
31 		     gint         size,
32 		     GError      *error)
33 {
34 	GdkPixbuf *scale;
35 
36 	if (error != NULL)
37 	{
38 		g_warning ("Could not load theme icon %s: %s",
39 			   name,
40 			   error->message);
41 		g_error_free (error);
42 	}
43 
44 	if (pixbuf && gdk_pixbuf_get_width (pixbuf) > size)
45 	{
46 		scale = gdk_pixbuf_scale_simple (pixbuf,
47 		                                 size,
48 		                                 size,
49 		                                 GDK_INTERP_BILINEAR);
50 		g_object_unref (pixbuf);
51 		pixbuf = scale;
52 	}
53 
54 	return pixbuf;
55 }
56 
57 GdkPixbuf *
gedit_file_browser_utils_pixbuf_from_theme(gchar const * name,GtkIconSize size)58 gedit_file_browser_utils_pixbuf_from_theme (gchar const *name,
59                                             GtkIconSize  size)
60 {
61 	gint width;
62 	GError *error = NULL;
63 	GdkPixbuf *pixbuf;
64 
65 	gtk_icon_size_lookup (size, &width, NULL);
66 
67 	pixbuf = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (),
68 					   name,
69 					   width,
70 					   0,
71 					   &error);
72 
73 	pixbuf = process_icon_pixbuf (pixbuf, name, width, error);
74 
75 	return pixbuf;
76 }
77 
78 GdkPixbuf *
gedit_file_browser_utils_pixbuf_from_icon(GIcon * icon,GtkIconSize size)79 gedit_file_browser_utils_pixbuf_from_icon (GIcon       *icon,
80                                            GtkIconSize  size)
81 {
82 	GdkPixbuf *ret = NULL;
83 	GtkIconTheme *theme;
84 	GtkIconInfo *info;
85 	gint width;
86 
87 	if (!icon)
88 		return NULL;
89 
90 	theme = gtk_icon_theme_get_default ();
91 	gtk_icon_size_lookup (size, &width, NULL);
92 
93 	info = gtk_icon_theme_lookup_by_gicon (theme,
94 					       icon,
95 					       width,
96 					       GTK_ICON_LOOKUP_USE_BUILTIN);
97 
98 	if (!info)
99 		return NULL;
100 
101 	ret = gtk_icon_info_load_icon (info, NULL);
102 	g_object_unref (info);
103 
104 	return ret;
105 }
106 
107 GdkPixbuf *
gedit_file_browser_utils_pixbuf_from_file(GFile * file,GtkIconSize size,gboolean use_symbolic)108 gedit_file_browser_utils_pixbuf_from_file (GFile       *file,
109                                            GtkIconSize  size,
110                                            gboolean     use_symbolic)
111 {
112 	GIcon *icon;
113 	GFileInfo *info;
114 	GdkPixbuf *ret = NULL;
115 	const char *attribute;
116 
117 	attribute = use_symbolic ? G_FILE_ATTRIBUTE_STANDARD_SYMBOLIC_ICON :
118 	                           G_FILE_ATTRIBUTE_STANDARD_ICON;
119 
120 	info = g_file_query_info (file,
121 				  attribute,
122 				  G_FILE_QUERY_INFO_NONE,
123 				  NULL,
124 				  NULL);
125 
126 	if (!info)
127 		return NULL;
128 
129 	icon = use_symbolic ? g_file_info_get_symbolic_icon (info) :
130 	                      g_file_info_get_icon (info);
131 	if (icon != NULL)
132 		ret = gedit_file_browser_utils_pixbuf_from_icon (icon, size);
133 
134 	g_object_unref (info);
135 
136 	return ret;
137 }
138 
139 gchar *
gedit_file_browser_utils_symbolic_icon_name_from_file(GFile * file)140 gedit_file_browser_utils_symbolic_icon_name_from_file (GFile *file)
141 {
142 	GFileInfo *info;
143 	GIcon *icon;
144 
145 	info = g_file_query_info (file,
146 				  G_FILE_ATTRIBUTE_STANDARD_SYMBOLIC_ICON,
147 				  G_FILE_QUERY_INFO_NONE,
148 				  NULL,
149 				  NULL);
150 
151 	if (!info)
152 		return NULL;
153 
154 	if ((icon = g_file_info_get_symbolic_icon (info)) && G_IS_THEMED_ICON (icon))
155 	{
156 		const gchar * const *names = g_themed_icon_get_names (G_THEMED_ICON (icon));
157 		return g_strdup (names[0]);
158 	}
159 
160 	g_object_unref (info);
161 	return NULL;
162 }
163 
164 gchar *
gedit_file_browser_utils_name_from_themed_icon(GIcon * icon)165 gedit_file_browser_utils_name_from_themed_icon (GIcon *icon)
166 {
167 	GtkIconTheme *theme;
168 	const gchar * const *names;
169 
170 	if (!G_IS_THEMED_ICON (icon))
171 		return NULL;
172 
173 	theme = gtk_icon_theme_get_default ();
174 	names = g_themed_icon_get_names (G_THEMED_ICON (icon));
175 
176 	if (gtk_icon_theme_has_icon (theme, names[0]))
177 		return g_strdup (names[0]);
178 
179 	return NULL;
180 }
181 
182 gchar *
gedit_file_browser_utils_file_basename(GFile * file)183 gedit_file_browser_utils_file_basename (GFile *file)
184 {
185 	return gedit_utils_basename_for_display (file);
186 }
187 
188 gboolean
gedit_file_browser_utils_confirmation_dialog(GeditWindow * window,GtkMessageType type,gchar const * message,gchar const * secondary,gchar const * button_label)189 gedit_file_browser_utils_confirmation_dialog (GeditWindow    *window,
190                                               GtkMessageType  type,
191                                               gchar const    *message,
192 		                              gchar const    *secondary,
193 		                              gchar const    *button_label)
194 {
195 	GtkWidget *dlg;
196 	gint ret;
197 
198 	dlg = gtk_message_dialog_new (GTK_WINDOW (window),
199 				      GTK_DIALOG_MODAL |
200 				      GTK_DIALOG_DESTROY_WITH_PARENT,
201 				      type,
202 				      GTK_BUTTONS_NONE, "%s", message);
203 
204 	if (secondary)
205 	{
206 		gtk_message_dialog_format_secondary_text
207 		    (GTK_MESSAGE_DIALOG (dlg), "%s", secondary);
208 	}
209 
210 	gtk_dialog_add_buttons (GTK_DIALOG (dlg),
211 	                        _("_Cancel"), GTK_RESPONSE_CANCEL,
212 	                        button_label, GTK_RESPONSE_OK,
213 	                        NULL);
214 
215 	gtk_dialog_set_default_response (GTK_DIALOG (dlg), GTK_RESPONSE_CANCEL);
216 
217 	ret = gtk_dialog_run (GTK_DIALOG (dlg));
218 	gtk_widget_destroy (dlg);
219 
220 	return (ret == GTK_RESPONSE_OK);
221 }
222 
223 /* ex:set ts=8 noet: */
224