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  * file-open.c
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 3 of the License, or
10  * (at your option) 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 <https://www.gnu.org/licenses/>.
19  */
20 
21 #include "config.h"
22 
23 #include <gdk-pixbuf/gdk-pixbuf.h>
24 #include <gegl.h>
25 
26 #include "libgimpbase/gimpbase.h"
27 
28 #include "core/core-types.h"
29 
30 #include "gegl/gimp-babl.h"
31 
32 #include "core/gimp.h"
33 #include "core/gimpcontext.h"
34 #include "core/gimpdocumentlist.h"
35 #include "core/gimpimage.h"
36 #include "core/gimpimage-merge.h"
37 #include "core/gimpimage-undo.h"
38 #include "core/gimpimagefile.h"
39 #include "core/gimplayer.h"
40 #include "core/gimpparamspecs.h"
41 #include "core/gimpprogress.h"
42 
43 #include "pdb/gimppdb.h"
44 
45 #include "plug-in/gimppluginmanager-file.h"
46 #include "plug-in/gimppluginprocedure.h"
47 
48 #include "file-import.h"
49 #include "file-open.h"
50 #include "file-remote.h"
51 #include "gimp-file.h"
52 
53 #include "gimp-intl.h"
54 
55 
56 static void     file_open_sanitize_image       (GimpImage           *image,
57                                                 gboolean             as_new);
58 static void     file_open_convert_items        (GimpImage           *dest_image,
59                                                 const gchar         *basename,
60                                                 GList               *items);
61 static GList *  file_open_get_layers           (GimpImage           *image,
62                                                 gboolean             merge_visible,
63                                                 gint                *n_visible);
64 static gboolean file_open_file_proc_is_import  (GimpPlugInProcedure *file_proc);
65 
66 
67 /*  public functions  */
68 
69 GimpImage *
file_open_image(Gimp * gimp,GimpContext * context,GimpProgress * progress,GFile * file,GFile * entered_file,gboolean as_new,GimpPlugInProcedure * file_proc,GimpRunMode run_mode,GimpPDBStatusType * status,const gchar ** mime_type,GError ** error)70 file_open_image (Gimp                *gimp,
71                  GimpContext         *context,
72                  GimpProgress        *progress,
73                  GFile               *file,
74                  GFile               *entered_file,
75                  gboolean             as_new,
76                  GimpPlugInProcedure *file_proc,
77                  GimpRunMode          run_mode,
78                  GimpPDBStatusType   *status,
79                  const gchar        **mime_type,
80                  GError             **error)
81 {
82   GimpValueArray *return_vals;
83   GimpImage      *image       = NULL;
84   GFile          *local_file  = NULL;
85   gchar          *path        = NULL;
86   gchar          *entered_uri = NULL;
87   gboolean        mounted     = TRUE;
88   GError         *my_error    = NULL;
89 
90   g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
91   g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL);
92   g_return_val_if_fail (progress == NULL || GIMP_IS_PROGRESS (progress), NULL);
93   g_return_val_if_fail (G_IS_FILE (file), NULL);
94   g_return_val_if_fail (G_IS_FILE (entered_file), NULL);
95   g_return_val_if_fail (status != NULL, NULL);
96   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
97 
98   *status = GIMP_PDB_EXECUTION_ERROR;
99 
100   if (! g_file_is_native (file) &&
101       ! file_remote_mount_file (gimp, file, progress, &my_error))
102     {
103       if (my_error)
104         {
105           g_printerr ("%s: mounting remote volume failed, trying to download"
106                       "the file: %s\n",
107                       G_STRFUNC, my_error->message);
108           g_clear_error (&my_error);
109 
110           mounted = FALSE;
111         }
112       else
113         {
114           *status = GIMP_PDB_CANCEL;
115 
116           return NULL;
117         }
118     }
119 
120   /* FIXME enable these tests for remote files again, needs testing */
121   if (g_file_is_native (file) &&
122       g_file_query_exists (file, NULL))
123     {
124       GFileInfo *info;
125 
126       info = g_file_query_info (file,
127                                 G_FILE_ATTRIBUTE_STANDARD_TYPE ","
128                                 G_FILE_ATTRIBUTE_ACCESS_CAN_READ,
129                                 G_FILE_QUERY_INFO_NONE,
130                                 NULL, error);
131       if (! info)
132         return NULL;
133 
134       if (g_file_info_get_file_type (info) != G_FILE_TYPE_REGULAR)
135         {
136           g_set_error_literal (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
137                                _("Not a regular file"));
138           g_object_unref (info);
139           return NULL;
140         }
141 
142       if (! g_file_info_get_attribute_boolean (info,
143                                                G_FILE_ATTRIBUTE_ACCESS_CAN_READ))
144         {
145           g_set_error_literal (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
146                                _("Permission denied"));
147           g_object_unref (info);
148           return NULL;
149         }
150 
151       g_object_unref (info);
152     }
153 
154   if (! file_proc)
155     file_proc = gimp_plug_in_manager_file_procedure_find (gimp->plug_in_manager,
156                                                           GIMP_FILE_PROCEDURE_GROUP_OPEN,
157                                                           file, error);
158 
159   if (! file_proc || ! file_proc->handles_uri || ! mounted)
160     {
161       gchar *my_path = g_file_get_path (file);
162 
163       if (! my_path)
164         {
165           g_clear_error (error);
166 
167           local_file = file_remote_download_image (gimp, file, progress,
168                                                    &my_error);
169 
170           if (! local_file)
171             {
172               if (my_error)
173                 g_propagate_error (error, my_error);
174               else
175                 *status = GIMP_PDB_CANCEL;
176 
177               return NULL;
178             }
179 
180           /*  if we don't have a file proc yet, try again on the local
181            *  file
182            */
183           if (! file_proc)
184             file_proc = gimp_plug_in_manager_file_procedure_find (gimp->plug_in_manager,
185                                                                   GIMP_FILE_PROCEDURE_GROUP_OPEN,
186                                                                   local_file, error);
187         }
188 
189       g_free (my_path);
190     }
191 
192   if (! file_proc)
193     {
194       if (local_file)
195         {
196           g_file_delete (local_file, NULL, NULL);
197           g_object_unref (local_file);
198         }
199 
200       return NULL;
201     }
202 
203   if (file_proc->handles_uri)
204     path = g_file_get_uri (local_file ? local_file : file);
205   else
206     path = g_file_get_path (local_file ? local_file : file);
207 
208   entered_uri = g_file_get_uri (entered_file);
209 
210   if (! entered_uri)
211     entered_uri = g_strdup (path);
212 
213   if (progress)
214     g_object_add_weak_pointer (G_OBJECT (progress), (gpointer) &progress);
215 
216   return_vals =
217     gimp_pdb_execute_procedure_by_name (gimp->pdb,
218                                         context, progress, error,
219                                         gimp_object_get_name (file_proc),
220                                         GIMP_TYPE_INT32, run_mode,
221                                         G_TYPE_STRING,   path,
222                                         G_TYPE_STRING,   entered_uri,
223                                         G_TYPE_NONE);
224 
225   if (progress)
226     g_object_remove_weak_pointer (G_OBJECT (progress), (gpointer) &progress);
227 
228   g_free (path);
229   g_free (entered_uri);
230 
231   *status = g_value_get_enum (gimp_value_array_index (return_vals, 0));
232 
233   if (*status == GIMP_PDB_SUCCESS)
234     image = gimp_value_get_image (gimp_value_array_index (return_vals, 1),
235                                   gimp);
236 
237   if (local_file)
238     {
239       if (image)
240         gimp_image_set_file (image, file);
241 
242       g_file_delete (local_file, NULL, NULL);
243       g_object_unref (local_file);
244     }
245 
246   if (*status == GIMP_PDB_SUCCESS)
247     {
248       if (image)
249         {
250           /* Only set the load procedure if it hasn't already been set. */
251           if (! gimp_image_get_load_proc (image))
252             gimp_image_set_load_proc (image, file_proc);
253 
254           file_proc = gimp_image_get_load_proc (image);
255 
256           if (mime_type)
257             *mime_type = g_slist_nth_data (file_proc->mime_types_list, 0);
258         }
259       else
260         {
261           if (error && ! *error)
262             g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
263                          _("%s plug-in returned SUCCESS but did not "
264                            "return an image"),
265                          gimp_procedure_get_label (GIMP_PROCEDURE (file_proc)));
266 
267           *status = GIMP_PDB_EXECUTION_ERROR;
268         }
269     }
270   else if (*status != GIMP_PDB_CANCEL)
271     {
272       if (error && ! *error)
273         g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
274                      _("%s plug-in could not open image"),
275                      gimp_procedure_get_label (GIMP_PROCEDURE (file_proc)));
276     }
277 
278   gimp_value_array_unref (return_vals);
279 
280   if (image)
281     {
282       gimp_image_undo_disable (image);
283 
284       if (file_open_file_proc_is_import (file_proc))
285         {
286           file_import_image (image, context, file,
287                              run_mode == GIMP_RUN_INTERACTIVE,
288                              progress);
289         }
290 
291       /* Enables undo again */
292       file_open_sanitize_image (image, as_new);
293     }
294 
295   return image;
296 }
297 
298 /**
299  * file_open_thumbnail:
300  * @gimp:
301  * @context:
302  * @progress:
303  * @file:         an image file
304  * @size:         requested size of the thumbnail
305  * @mime_type:    return location for image MIME type
306  * @image_width:  return location for image width
307  * @image_height: return location for image height
308  * @format:       return location for image format (set to NULL if unknown)
309  * @num_layers:   return location for number of layers
310  *                (set to -1 if the number of layers is not known)
311  * @error:
312  *
313  * Attempts to load a thumbnail by using a registered thumbnail loader.
314  *
315  * Return value: the thumbnail image
316  */
317 GimpImage *
file_open_thumbnail(Gimp * gimp,GimpContext * context,GimpProgress * progress,GFile * file,gint size,const gchar ** mime_type,gint * image_width,gint * image_height,const Babl ** format,gint * num_layers,GError ** error)318 file_open_thumbnail (Gimp           *gimp,
319                      GimpContext    *context,
320                      GimpProgress   *progress,
321                      GFile          *file,
322                      gint            size,
323                      const gchar   **mime_type,
324                      gint           *image_width,
325                      gint           *image_height,
326                      const Babl    **format,
327                      gint           *num_layers,
328                      GError        **error)
329 {
330   GimpPlugInProcedure *file_proc;
331   GimpProcedure       *procedure;
332 
333   g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
334   g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL);
335   g_return_val_if_fail (progress == NULL || GIMP_IS_PROGRESS (progress), NULL);
336   g_return_val_if_fail (G_IS_FILE (file), NULL);
337   g_return_val_if_fail (mime_type != NULL, NULL);
338   g_return_val_if_fail (image_width != NULL, NULL);
339   g_return_val_if_fail (image_height != NULL, NULL);
340   g_return_val_if_fail (format != NULL, NULL);
341   g_return_val_if_fail (num_layers != NULL, NULL);
342   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
343 
344   *image_width  = 0;
345   *image_height = 0;
346   *format       = NULL;
347   *num_layers   = -1;
348 
349   file_proc = gimp_plug_in_manager_file_procedure_find (gimp->plug_in_manager,
350                                                         GIMP_FILE_PROCEDURE_GROUP_OPEN,
351                                                         file, NULL);
352 
353   if (! file_proc || ! file_proc->thumb_loader)
354     return NULL;
355 
356   procedure = gimp_pdb_lookup_procedure (gimp->pdb, file_proc->thumb_loader);
357 
358   if (procedure && procedure->num_args >= 2 && procedure->num_values >= 1)
359     {
360       GimpPDBStatusType  status;
361       GimpValueArray    *return_vals;
362       GimpImage         *image = NULL;
363       gchar             *path  = NULL;
364 
365       if (! file_proc->handles_uri)
366         path = g_file_get_path (file);
367 
368       if (! path)
369         path = g_file_get_uri (file);
370 
371       return_vals =
372         gimp_pdb_execute_procedure_by_name (gimp->pdb,
373                                             context, progress, error,
374                                             gimp_object_get_name (procedure),
375                                             G_TYPE_STRING,   path,
376                                             GIMP_TYPE_INT32, size,
377                                             G_TYPE_NONE);
378 
379       g_free (path);
380 
381       status = g_value_get_enum (gimp_value_array_index (return_vals, 0));
382 
383       if (status == GIMP_PDB_SUCCESS &&
384           GIMP_VALUE_HOLDS_IMAGE_ID (gimp_value_array_index (return_vals, 1)))
385         {
386           image = gimp_value_get_image (gimp_value_array_index (return_vals, 1),
387                                         gimp);
388 
389           if (gimp_value_array_length (return_vals) >= 3 &&
390               G_VALUE_HOLDS_INT (gimp_value_array_index (return_vals, 2)) &&
391               G_VALUE_HOLDS_INT (gimp_value_array_index (return_vals, 3)))
392             {
393               *image_width =
394                 MAX (0, g_value_get_int (gimp_value_array_index (return_vals, 2)));
395 
396               *image_height =
397                 MAX (0, g_value_get_int (gimp_value_array_index (return_vals, 3)));
398 
399               if (gimp_value_array_length (return_vals) >= 5 &&
400                   G_VALUE_HOLDS_INT (gimp_value_array_index (return_vals, 4)))
401                 {
402                   gint value = g_value_get_int (gimp_value_array_index (return_vals, 4));
403 
404                   switch (value)
405                     {
406                     case GIMP_RGB_IMAGE:
407                       *format = gimp_babl_format (GIMP_RGB,
408                                                   GIMP_PRECISION_U8_GAMMA,
409                                                   FALSE);
410                       break;
411 
412                     case GIMP_RGBA_IMAGE:
413                       *format = gimp_babl_format (GIMP_RGB,
414                                                   GIMP_PRECISION_U8_GAMMA,
415                                                   TRUE);
416                       break;
417 
418                     case GIMP_GRAY_IMAGE:
419                       *format = gimp_babl_format (GIMP_GRAY,
420                                                   GIMP_PRECISION_U8_GAMMA,
421                                                   FALSE);
422                       break;
423 
424                     case GIMP_GRAYA_IMAGE:
425                       *format = gimp_babl_format (GIMP_GRAY,
426                                                   GIMP_PRECISION_U8_GAMMA,
427                                                   TRUE);
428                       break;
429 
430                     case GIMP_INDEXED_IMAGE:
431                     case GIMP_INDEXEDA_IMAGE:
432                       {
433                         const Babl *rgb;
434                         const Babl *rgba;
435 
436                         babl_new_palette ("-gimp-indexed-format-dummy",
437                                           &rgb, &rgba);
438 
439                         if (value == GIMP_INDEXED_IMAGE)
440                           *format = rgb;
441                         else
442                           *format = rgba;
443                       }
444                       break;
445 
446                     default:
447                       break;
448                     }
449                 }
450 
451               if (gimp_value_array_length (return_vals) >= 6 &&
452                   G_VALUE_HOLDS_INT (gimp_value_array_index (return_vals, 5)))
453                 {
454                   *num_layers =
455                     MAX (0, g_value_get_int (gimp_value_array_index (return_vals, 5)));
456                 }
457             }
458 
459           if (image)
460             {
461               file_open_sanitize_image (image, FALSE);
462 
463               *mime_type = g_slist_nth_data (file_proc->mime_types_list, 0);
464 
465 #ifdef GIMP_UNSTABLE
466               g_printerr ("opened thumbnail at %d x %d\n",
467                           gimp_image_get_width  (image),
468                           gimp_image_get_height (image));
469 #endif
470             }
471         }
472 
473       gimp_value_array_unref (return_vals);
474 
475       return image;
476     }
477 
478   return NULL;
479 }
480 
481 GimpImage *
file_open_with_display(Gimp * gimp,GimpContext * context,GimpProgress * progress,GFile * file,gboolean as_new,GObject * screen,gint monitor,GimpPDBStatusType * status,GError ** error)482 file_open_with_display (Gimp               *gimp,
483                         GimpContext        *context,
484                         GimpProgress       *progress,
485                         GFile              *file,
486                         gboolean            as_new,
487                         GObject            *screen,
488                         gint                monitor,
489                         GimpPDBStatusType  *status,
490                         GError            **error)
491 {
492   return file_open_with_proc_and_display (gimp, context, progress,
493                                           file, file, as_new, NULL,
494                                           screen, monitor,
495                                           status, error);
496 }
497 
498 GimpImage *
file_open_with_proc_and_display(Gimp * gimp,GimpContext * context,GimpProgress * progress,GFile * file,GFile * entered_file,gboolean as_new,GimpPlugInProcedure * file_proc,GObject * screen,gint monitor,GimpPDBStatusType * status,GError ** error)499 file_open_with_proc_and_display (Gimp                *gimp,
500                                  GimpContext         *context,
501                                  GimpProgress        *progress,
502                                  GFile               *file,
503                                  GFile               *entered_file,
504                                  gboolean             as_new,
505                                  GimpPlugInProcedure *file_proc,
506                                  GObject             *screen,
507                                  gint                 monitor,
508                                  GimpPDBStatusType   *status,
509                                  GError             **error)
510 {
511   GimpImage   *image;
512   const gchar *mime_type = NULL;
513 
514   g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
515   g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL);
516   g_return_val_if_fail (progress == NULL || GIMP_IS_PROGRESS (progress), NULL);
517   g_return_val_if_fail (G_IS_FILE (file), NULL);
518   g_return_val_if_fail (G_IS_FILE (entered_file), NULL);
519   g_return_val_if_fail (screen == NULL || G_IS_OBJECT (screen), NULL);
520   g_return_val_if_fail (status != NULL, NULL);
521 
522   image = file_open_image (gimp, context, progress,
523                            file,
524                            entered_file,
525                            as_new,
526                            file_proc,
527                            GIMP_RUN_INTERACTIVE,
528                            status,
529                            &mime_type,
530                            error);
531 
532   if (image)
533     {
534       /* If the file was imported we want to set the layer name to the
535        * file name. For now, assume that multi-layered imported images
536        * have named the layers already, so only rename the layer of
537        * single-layered imported files. Note that this will also
538        * rename already named layers from e.g. single-layered PSD
539        * files. To solve this properly, we would need new file plug-in
540        * API.
541        */
542       if (! file_proc)
543         file_proc = gimp_image_get_load_proc (image);
544 
545       if (file_open_file_proc_is_import (file_proc) &&
546           gimp_image_get_n_layers (image) == 1)
547         {
548           GimpObject *layer = gimp_image_get_layer_iter (image)->data;
549           gchar      *basename;
550 
551           basename = g_path_get_basename (gimp_file_get_utf8_name (file));
552 
553           gimp_item_rename (GIMP_ITEM (layer), basename, NULL);
554           gimp_image_undo_free (image);
555           gimp_image_clean_all (image);
556 
557           g_free (basename);
558         }
559 
560       if (gimp_create_display (image->gimp, image, GIMP_UNIT_PIXEL, 1.0,
561                                screen, monitor))
562         {
563           /*  the display owns the image now  */
564           g_object_unref (image);
565         }
566 
567       if (! as_new)
568         {
569           GimpDocumentList *documents = GIMP_DOCUMENT_LIST (gimp->documents);
570           GimpImagefile    *imagefile;
571           GFile            *any_file;
572 
573           imagefile = gimp_document_list_add_file (documents, file, mime_type);
574 
575           /*  can only create a thumbnail if the passed file and the
576            *  resulting image's file match. Use any_file() here so we
577            *  create thumbnails for both XCF and imported images.
578            */
579           any_file = gimp_image_get_any_file (image);
580 
581           if (any_file && g_file_equal (file, any_file))
582             {
583               /*  no need to save a thumbnail if there's a good one already  */
584               if (! gimp_imagefile_check_thumbnail (imagefile))
585                 {
586                   gimp_imagefile_save_thumbnail (imagefile, mime_type, image,
587                                                  NULL);
588                 }
589             }
590         }
591 
592       /*  announce that we opened this image  */
593       gimp_image_opened (image->gimp, file);
594     }
595 
596   return image;
597 }
598 
599 GList *
file_open_layers(Gimp * gimp,GimpContext * context,GimpProgress * progress,GimpImage * dest_image,gboolean merge_visible,GFile * file,GimpRunMode run_mode,GimpPlugInProcedure * file_proc,GimpPDBStatusType * status,GError ** error)600 file_open_layers (Gimp                *gimp,
601                   GimpContext         *context,
602                   GimpProgress        *progress,
603                   GimpImage           *dest_image,
604                   gboolean             merge_visible,
605                   GFile               *file,
606                   GimpRunMode          run_mode,
607                   GimpPlugInProcedure *file_proc,
608                   GimpPDBStatusType   *status,
609                   GError             **error)
610 {
611   GimpImage   *new_image;
612   GList       *layers    = NULL;
613   const gchar *mime_type = NULL;
614 
615   g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
616   g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL);
617   g_return_val_if_fail (progress == NULL || GIMP_IS_PROGRESS (progress), NULL);
618   g_return_val_if_fail (GIMP_IS_IMAGE (dest_image), NULL);
619   g_return_val_if_fail (G_IS_FILE (file), NULL);
620   g_return_val_if_fail (status != NULL, NULL);
621   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
622 
623   new_image = file_open_image (gimp, context, progress,
624                                file, file, FALSE,
625                                file_proc,
626                                run_mode,
627                                status, &mime_type, error);
628 
629   if (new_image)
630     {
631       gint n_visible = 0;
632 
633       gimp_image_undo_disable (new_image);
634 
635       layers = file_open_get_layers (new_image, merge_visible, &n_visible);
636 
637       if (merge_visible && n_visible > 1)
638         {
639           GimpLayer *layer;
640 
641           g_list_free (layers);
642 
643           layer = gimp_image_merge_visible_layers (new_image, context,
644                                                    GIMP_CLIP_TO_IMAGE,
645                                                    FALSE, FALSE,
646                                                    NULL);
647 
648           layers = g_list_prepend (NULL, layer);
649         }
650 
651       if (layers)
652         {
653           gchar *basename;
654 
655           basename = g_path_get_basename (gimp_file_get_utf8_name (file));
656           file_open_convert_items (dest_image, basename, layers);
657           g_free (basename);
658 
659           gimp_document_list_add_file (GIMP_DOCUMENT_LIST (gimp->documents),
660                                        file, mime_type);
661         }
662       else
663         {
664           g_set_error_literal (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
665                                _("Image doesn't contain any layers"));
666           *status = GIMP_PDB_EXECUTION_ERROR;
667         }
668 
669       g_object_unref (new_image);
670     }
671 
672   return g_list_reverse (layers);
673 }
674 
675 
676 /*  This function is called for filenames passed on the command-line
677  *  or from the D-Bus service.
678  */
679 gboolean
file_open_from_command_line(Gimp * gimp,GFile * file,gboolean as_new,GObject * screen,gint monitor)680 file_open_from_command_line (Gimp     *gimp,
681                              GFile    *file,
682                              gboolean  as_new,
683                              GObject  *screen,
684                              gint      monitor)
685 
686 {
687   GimpImage         *image;
688   GimpObject        *display;
689   GimpPDBStatusType  status;
690   gboolean           success = FALSE;
691   GError            *error   = NULL;
692 
693   g_return_val_if_fail (GIMP_IS_GIMP (gimp), FALSE);
694   g_return_val_if_fail (G_IS_FILE (file), FALSE);
695   g_return_val_if_fail (screen == NULL || G_IS_OBJECT (screen), FALSE);
696 
697   display = gimp_get_empty_display (gimp);
698 
699   /* show the progress in the last opened display, see bug #704896 */
700   if (! display)
701     display = gimp_context_get_display (gimp_get_user_context (gimp));
702 
703   if (display)
704     g_object_add_weak_pointer (G_OBJECT (display), (gpointer) &display);
705 
706   image = file_open_with_display (gimp,
707                                   gimp_get_user_context (gimp),
708                                   GIMP_PROGRESS (display),
709                                   file, as_new,
710                                   screen, monitor,
711                                   &status, &error);
712 
713   if (image)
714     {
715       success = TRUE;
716 
717       g_object_set_data_full (G_OBJECT (gimp), GIMP_FILE_OPEN_LAST_FILE_KEY,
718                               g_object_ref (file),
719                               (GDestroyNotify) g_object_unref);
720     }
721   else if (status != GIMP_PDB_CANCEL && display)
722     {
723       gimp_message (gimp, G_OBJECT (display), GIMP_MESSAGE_ERROR,
724                     _("Opening '%s' failed: %s"),
725                     gimp_file_get_utf8_name (file), error->message);
726       g_clear_error (&error);
727     }
728 
729   if (display)
730     g_object_remove_weak_pointer (G_OBJECT (display), (gpointer) &display);
731 
732   return success;
733 }
734 
735 
736 /*  private functions  */
737 
738 static void
file_open_sanitize_image(GimpImage * image,gboolean as_new)739 file_open_sanitize_image (GimpImage *image,
740                           gboolean   as_new)
741 {
742   if (as_new)
743     gimp_image_set_file (image, NULL);
744 
745   /* clear all undo steps */
746   gimp_image_undo_free (image);
747 
748   /* make sure that undo is enabled */
749   while (! gimp_image_undo_is_enabled (image))
750     gimp_image_undo_thaw (image);
751 
752   /* Set the image to clean. Note that export dirtiness is not set to
753    * clean here; we can only consider export clean after the first
754    * export
755    */
756   gimp_image_clean_all (image);
757 
758   /* Make sure the projection is completely constructed from valid
759    * layers, this is needed in case something triggers projection or
760    * image preview creation before all layers are loaded, see bug #767663.
761    */
762   gimp_image_invalidate_all (image);
763 
764   /* Make sure all image states are up-to-date */
765   gimp_image_flush (image);
766 }
767 
768 /* Converts items from one image to another */
769 static void
file_open_convert_items(GimpImage * dest_image,const gchar * basename,GList * items)770 file_open_convert_items (GimpImage   *dest_image,
771                          const gchar *basename,
772                          GList       *items)
773 {
774   GList *list;
775 
776   for (list = items; list; list = g_list_next (list))
777     {
778       GimpItem *src = list->data;
779       GimpItem *item;
780 
781       item = gimp_item_convert (src, dest_image, G_TYPE_FROM_INSTANCE (src));
782 
783       if (g_list_length (items) == 1)
784         {
785           gimp_object_set_name (GIMP_OBJECT (item), basename);
786         }
787       else
788         {
789           gimp_object_set_name (GIMP_OBJECT (item),
790                                 gimp_object_get_name (src));
791         }
792 
793       list->data = item;
794     }
795 }
796 
797 static GList *
file_open_get_layers(GimpImage * image,gboolean merge_visible,gint * n_visible)798 file_open_get_layers (GimpImage *image,
799                       gboolean   merge_visible,
800                       gint      *n_visible)
801 {
802   GList *iter   = NULL;
803   GList *layers = NULL;
804 
805   for (iter = gimp_image_get_layer_iter (image);
806        iter;
807        iter = g_list_next (iter))
808     {
809       GimpItem *item = iter->data;
810 
811       if (! merge_visible)
812         layers = g_list_prepend (layers, item);
813 
814       if (gimp_item_get_visible (item))
815         {
816           if (n_visible)
817             (*n_visible)++;
818 
819           if (! layers)
820             layers = g_list_prepend (layers, item);
821         }
822     }
823 
824   return layers;
825 }
826 
827 static gboolean
file_open_file_proc_is_import(GimpPlugInProcedure * file_proc)828 file_open_file_proc_is_import (GimpPlugInProcedure *file_proc)
829 {
830   return !(file_proc &&
831            file_proc->mime_types &&
832            strcmp (file_proc->mime_types, "image/x-xcf") == 0);
833 }
834