1 /*
2  * pluma-file-bookmarks-store.c - Pluma plugin providing easy file access
3  * from the sidepanel
4  *
5  * Copyright (C) 2006 - Jesse van den Kieboom <jesse@icecrew.nl>
6  * Copyright (C) 2012-2021 MATE Developers
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2, or (at your option)
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
21  */
22 
23 #include "pluma-file-browser-utils.h"
24 #include <pluma/pluma-utils.h>
25 
26 #include <glib/gi18n.h>
27 
28 
29 static GdkPixbuf *
process_icon_pixbuf(GdkPixbuf * pixbuf,gchar const * name,gint size,GError * error)30 process_icon_pixbuf (GdkPixbuf * pixbuf,
31 		     gchar const * name,
32 		     gint size,
33 		     GError * error)
34 {
35 	GdkPixbuf * scale;
36 
37 	if (error != NULL) {
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 		scale = gdk_pixbuf_scale_simple (pixbuf,
46 		                                 size,
47 		                                 size,
48 		                                 GDK_INTERP_BILINEAR);
49 		g_object_unref (pixbuf);
50 		pixbuf = scale;
51 	}
52 
53 	return pixbuf;
54 }
55 
56 GdkPixbuf *
pluma_file_browser_utils_pixbuf_from_theme(gchar const * name,GtkIconSize size)57 pluma_file_browser_utils_pixbuf_from_theme (gchar const * name,
58                                             GtkIconSize size)
59 {
60 	gint width;
61 	GError *error = NULL;
62 	GdkPixbuf *pixbuf;
63 
64 	gtk_icon_size_lookup (size, &width, NULL);
65 
66 	pixbuf = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (),
67 					   name,
68 					   width,
69 					   0,
70 					   &error);
71 
72 	pixbuf = process_icon_pixbuf (pixbuf, name, width, error);
73 
74 	return pixbuf;
75 }
76 
77 GdkPixbuf *
pluma_file_browser_utils_pixbuf_from_icon(GIcon * icon,GtkIconSize size)78 pluma_file_browser_utils_pixbuf_from_icon (GIcon * icon,
79                                            GtkIconSize size)
80 {
81 	GdkPixbuf * ret = NULL;
82 	GtkIconTheme *theme;
83 	GtkIconInfo *info;
84 	gint width;
85 
86 	if (!icon)
87 		return NULL;
88 
89 	theme = gtk_icon_theme_get_default ();
90 	gtk_icon_size_lookup (size, &width, NULL);
91 
92 	info = gtk_icon_theme_lookup_by_gicon (theme,
93 					       icon,
94 					       width,
95 					       GTK_ICON_LOOKUP_USE_BUILTIN);
96 
97 	if (!info)
98 		return NULL;
99 
100 	ret = gtk_icon_info_load_icon (info, NULL);
101 	g_object_unref (info);
102 
103 	return ret;
104 }
105 
106 GdkPixbuf *
pluma_file_browser_utils_pixbuf_from_file(GFile * file,GtkIconSize size)107 pluma_file_browser_utils_pixbuf_from_file (GFile * file,
108                                            GtkIconSize size)
109 {
110 	GIcon * icon;
111 	GFileInfo * info;
112 	GdkPixbuf * ret = NULL;
113 
114 	info = g_file_query_info (file,
115 				  G_FILE_ATTRIBUTE_STANDARD_ICON,
116 				  G_FILE_QUERY_INFO_NONE,
117 				  NULL,
118 				  NULL);
119 
120 	if (!info)
121 		return NULL;
122 
123 	icon = g_file_info_get_icon (info);
124 	if (icon != NULL)
125 		ret = pluma_file_browser_utils_pixbuf_from_icon (icon, size);
126 
127 	g_object_unref (info);
128 
129 	return ret;
130 }
131 
132 gchar *
pluma_file_browser_utils_file_basename(GFile * file)133 pluma_file_browser_utils_file_basename (GFile * file)
134 {
135 	gchar *uri;
136 	gchar *ret;
137 
138 	uri = g_file_get_uri (file);
139 	ret = pluma_file_browser_utils_uri_basename (uri);
140 	g_free (uri);
141 
142 	return ret;
143 }
144 
145 gchar *
pluma_file_browser_utils_uri_basename(gchar const * uri)146 pluma_file_browser_utils_uri_basename (gchar const * uri)
147 {
148 	return pluma_utils_basename_for_display (uri);
149 }
150 
151 gboolean
pluma_file_browser_utils_confirmation_dialog(PlumaWindow * window,GtkMessageType type,gchar const * message,gchar const * secondary)152 pluma_file_browser_utils_confirmation_dialog (PlumaWindow * window,
153                                               GtkMessageType type,
154                                               gchar const *message,
155                                               gchar const *secondary)
156 {
157 	GtkWidget *dlg;
158 	gint ret;
159 	GtkWidget *button;
160 
161 	dlg = gtk_message_dialog_new (GTK_WINDOW (window),
162 				      GTK_DIALOG_MODAL |
163 				      GTK_DIALOG_DESTROY_WITH_PARENT,
164 				      type,
165 				      GTK_BUTTONS_NONE, "%s", message);
166 
167 	if (secondary)
168 		gtk_message_dialog_format_secondary_text
169 		    (GTK_MESSAGE_DIALOG (dlg), "%s", secondary);
170 
171 	/* Add a cancel button */
172 	button = gtk_button_new_with_mnemonic (_("_Cancel"));
173 	gtk_button_set_image (GTK_BUTTON (button), gtk_image_new_from_icon_name ("process-stop", GTK_ICON_SIZE_BUTTON));
174 
175 	gtk_widget_show (button);
176 
177 	gtk_widget_set_can_default (button, TRUE);
178 	gtk_dialog_add_action_widget (GTK_DIALOG (dlg),
179                                       button,
180                                       GTK_RESPONSE_CANCEL);
181 
182 	/* Add delete button */
183 	button = gtk_button_new_with_mnemonic (_("_Delete"));
184 	gtk_button_set_image (GTK_BUTTON (button), gtk_image_new_from_icon_name ("edit-delete", GTK_ICON_SIZE_BUTTON));
185 
186 	gtk_widget_show (button);
187 	gtk_widget_set_can_default (button, TRUE);
188 	gtk_dialog_add_action_widget (GTK_DIALOG (dlg),
189                                       button,
190                                       GTK_RESPONSE_OK);
191 
192 	ret = gtk_dialog_run (GTK_DIALOG (dlg));
193 	gtk_widget_destroy (dlg);
194 
195 	return (ret == GTK_RESPONSE_OK);
196 }
197 
198 // ex:ts=8:noet:
199