1 /* GIMP - The GNU Image Manipulation Program
2 * Copyright (C) 1995, 1996, 1997 Spencer Kimball and Peter Mattis
3 * Copyright (C) 1997 Josh MacDonald
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
19 #include "config.h"
20
21 #include <string.h>
22
23 #include <gegl.h>
24 #include <gtk/gtk.h>
25
26 #include "libgimpbase/gimpbase.h"
27 #include "libgimpwidgets/gimpwidgets.h"
28
29 #include "dialogs-types.h"
30
31 #include "core/gimp.h"
32 #include "core/gimpcontext.h"
33 #include "core/gimpprogress.h"
34
35 #include "file/file-open.h"
36 #include "file/file-utils.h"
37
38 #include "widgets/gimpcontainerentry.h"
39 #include "widgets/gimphelp-ids.h"
40 #include "widgets/gimpprogressbox.h"
41 #include "widgets/gimpwidgets-utils.h"
42
43 #include "file-open-location-dialog.h"
44
45 #include "gimp-intl.h"
46
47
48 static void file_open_location_response (GtkDialog *dialog,
49 gint response_id,
50 Gimp *gimp);
51
52 static gboolean file_open_location_completion (GtkEntryCompletion *completion,
53 const gchar *key,
54 GtkTreeIter *iter,
55 gpointer data);
56
57
58 /* public functions */
59
60 GtkWidget *
file_open_location_dialog_new(Gimp * gimp)61 file_open_location_dialog_new (Gimp *gimp)
62 {
63 GimpContext *context;
64 GtkWidget *dialog;
65 GtkWidget *hbox;
66 GtkWidget *vbox;
67 GtkWidget *image;
68 GtkWidget *label;
69 GtkWidget *entry;
70 GtkEntryCompletion *completion;
71
72 g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
73
74 dialog = gimp_dialog_new (_("Open Location"),
75 "gimp-file-open-location",
76 NULL, 0,
77 gimp_standard_help_func,
78 GIMP_HELP_FILE_OPEN_LOCATION,
79
80 _("_Cancel"), GTK_RESPONSE_CANCEL,
81 _("_Open"), GTK_RESPONSE_OK,
82
83 NULL);
84
85 gtk_dialog_set_alternative_button_order (GTK_DIALOG(dialog),
86 GTK_RESPONSE_OK,
87 GTK_RESPONSE_CANCEL,
88 -1);
89
90 g_signal_connect (dialog, "response",
91 G_CALLBACK (file_open_location_response),
92 gimp);
93
94 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
95 gtk_container_set_border_width (GTK_CONTAINER (hbox), 12);
96 gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
97 hbox, FALSE, FALSE, 0);
98 gtk_widget_show (hbox);
99
100 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
101 gtk_box_pack_start (GTK_BOX (hbox), vbox, FALSE, FALSE, 0);
102 gtk_widget_show (vbox);
103
104 image = gtk_image_new_from_icon_name (GIMP_ICON_WEB, GTK_ICON_SIZE_BUTTON);
105 gtk_box_pack_start (GTK_BOX (vbox), image, FALSE, FALSE, 0);
106 gtk_widget_show (image);
107
108 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
109 gtk_box_pack_start (GTK_BOX (hbox), vbox, TRUE, TRUE, 0);
110 gtk_widget_show (vbox);
111
112 label = gtk_label_new (_("Enter location (URI):"));
113 gtk_label_set_xalign (GTK_LABEL (label), 0.0);
114 gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
115 gtk_widget_show (label);
116
117 /* we don't want the context to affect the entry, so create
118 * a scratch one instead of using e.g. the user context
119 */
120 context = gimp_context_new (gimp, "file-open-location-dialog", NULL);
121 entry = gimp_container_entry_new (gimp->documents, context,
122 GIMP_VIEW_SIZE_SMALL, 0);
123 g_object_unref (context);
124
125 completion = gtk_entry_get_completion (GTK_ENTRY (entry));
126 gtk_entry_completion_set_match_func (completion,
127 file_open_location_completion,
128 NULL, NULL);
129
130 gtk_entry_set_activates_default (GTK_ENTRY (entry), TRUE);
131 gtk_widget_set_size_request (entry, 400, -1);
132 gtk_box_pack_start (GTK_BOX (vbox), entry, FALSE, FALSE, 0);
133 gtk_widget_show (entry);
134
135 g_object_set_data (G_OBJECT (dialog), "location-entry", entry);
136
137 return dialog;
138 }
139
140
141 /* private functions */
142
143 static void
file_open_location_response(GtkDialog * dialog,gint response_id,Gimp * gimp)144 file_open_location_response (GtkDialog *dialog,
145 gint response_id,
146 Gimp *gimp)
147 {
148 GtkWidget *entry;
149 GtkWidget *box;
150 const gchar *text = NULL;
151
152 box = g_object_get_data (G_OBJECT (dialog), "progress-box");
153
154 if (response_id != GTK_RESPONSE_OK)
155 {
156 if (box && GIMP_PROGRESS_BOX (box)->active)
157 gimp_progress_cancel (GIMP_PROGRESS (box));
158 else
159 gtk_widget_destroy (GTK_WIDGET (dialog));
160
161 return;
162 }
163
164 entry = g_object_get_data (G_OBJECT (dialog), "location-entry");
165 text = gtk_entry_get_text (GTK_ENTRY (entry));
166
167 if (text && strlen (text))
168 {
169 GimpImage *image;
170 gchar *filename;
171 GFile *file;
172 GimpPDBStatusType status;
173 GError *error = NULL;
174
175 filename = g_filename_from_uri (text, NULL, NULL);
176
177 if (filename)
178 {
179 file = g_file_new_for_uri (text);
180 g_free (filename);
181 }
182 else
183 {
184 file = file_utils_filename_to_file (gimp, text, &error);
185 }
186
187 if (!box)
188 {
189 box = gimp_progress_box_new ();
190 gtk_container_set_border_width (GTK_CONTAINER (box), 12);
191 gtk_box_pack_end (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
192 box, FALSE, FALSE, 0);
193
194 g_object_set_data (G_OBJECT (dialog), "progress-box", box);
195 }
196
197 if (file)
198 {
199 GFile *entered_file = g_file_new_for_uri (text);
200
201 /* should not fail but does, see issue #3093 */
202 if (! entered_file)
203 entered_file = g_object_ref (file);
204
205 gtk_widget_show (box);
206
207 gtk_editable_set_editable (GTK_EDITABLE (entry), FALSE);
208 gtk_dialog_set_response_sensitive (dialog, GTK_RESPONSE_OK, FALSE);
209
210 image = file_open_with_proc_and_display (gimp,
211 gimp_get_user_context (gimp),
212 GIMP_PROGRESS (box),
213 file, entered_file,
214 FALSE, NULL,
215 G_OBJECT (gtk_widget_get_screen (entry)),
216 gimp_widget_get_monitor (entry),
217 &status, &error);
218
219 gtk_dialog_set_response_sensitive (dialog, GTK_RESPONSE_OK, TRUE);
220 gtk_editable_set_editable (GTK_EDITABLE (entry), TRUE);
221
222 g_object_unref (entered_file);
223
224 if (image == NULL && status != GIMP_PDB_CANCEL)
225 {
226 gimp_message (gimp, G_OBJECT (box), GIMP_MESSAGE_ERROR,
227 _("Opening '%s' failed:\n\n%s"),
228 gimp_file_get_utf8_name (file), error->message);
229 g_clear_error (&error);
230 }
231
232 g_object_unref (file);
233
234 if (image != NULL)
235 {
236 gtk_widget_destroy (GTK_WIDGET (dialog));
237 return;
238 }
239 }
240 else
241 {
242 gimp_message (gimp, G_OBJECT (box), GIMP_MESSAGE_ERROR,
243 _("Opening '%s' failed:\n\n%s"),
244 text,
245 /* error should never be NULL, also issue #3093 */
246 error ? error->message : _("Invalid URI"));
247 g_clear_error (&error);
248 }
249 }
250 }
251
252 static gboolean
file_open_location_completion(GtkEntryCompletion * completion,const gchar * key,GtkTreeIter * iter,gpointer data)253 file_open_location_completion (GtkEntryCompletion *completion,
254 const gchar *key,
255 GtkTreeIter *iter,
256 gpointer data)
257 {
258 GtkTreeModel *model = gtk_entry_completion_get_model (completion);
259 gchar *name;
260 gchar *normalized;
261 gchar *case_normalized;
262 gboolean match;
263
264 gtk_tree_model_get (model, iter,
265 1, &name,
266 -1);
267
268 if (! name)
269 return FALSE;
270
271 normalized = g_utf8_normalize (name, -1, G_NORMALIZE_ALL);
272 case_normalized = g_utf8_casefold (normalized, -1);
273
274 match = (strncmp (key, case_normalized, strlen (key)) == 0);
275
276 if (! match)
277 {
278 const gchar *colon = strchr (case_normalized, ':');
279
280 if (colon && strlen (colon) > 2 && colon[1] == '/' && colon[2] == '/')
281 match = (strncmp (key, colon + 3, strlen (key)) == 0);
282 }
283
284 g_free (normalized);
285 g_free (case_normalized);
286 g_free (name);
287
288 return match;
289 }
290