1 /* GIMP - The GNU Image Manipulation Program
2  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
3  *
4  * gimpexportdialog.c
5  * Copyright (C) 2015 Jehan <jehan@girinstud.io>
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 <gegl.h>
24 #include <gtk/gtk.h>
25 
26 #include "libgimpbase/gimpbase.h"
27 #include "libgimpwidgets/gimpwidgets.h"
28 
29 #include "widgets-types.h"
30 
31 #include "core/gimp.h"
32 #include "core/gimp-utils.h"
33 #include "core/gimpimage.h"
34 
35 #include "config/gimpcoreconfig.h"
36 
37 #include "file/gimp-file.h"
38 
39 #include "gimpexportdialog.h"
40 #include "gimphelp-ids.h"
41 
42 #include "gimp-intl.h"
43 
44 
G_DEFINE_TYPE(GimpExportDialog,gimp_export_dialog,GIMP_TYPE_FILE_DIALOG)45 G_DEFINE_TYPE (GimpExportDialog, gimp_export_dialog,
46                GIMP_TYPE_FILE_DIALOG)
47 
48 #define parent_class gimp_export_dialog_parent_class
49 
50 
51 static void
52 gimp_export_dialog_class_init (GimpExportDialogClass *klass)
53 {
54 }
55 
56 static void
gimp_export_dialog_init(GimpExportDialog * dialog)57 gimp_export_dialog_init (GimpExportDialog *dialog)
58 {
59 }
60 
61 
62 /*  public functions  */
63 
64 GtkWidget *
gimp_export_dialog_new(Gimp * gimp)65 gimp_export_dialog_new (Gimp *gimp)
66 {
67   g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
68 
69   return g_object_new (GIMP_TYPE_EXPORT_DIALOG,
70                        "gimp",                  gimp,
71                        "title",                 _("Export Image"),
72                        "role",                  "gimp-file-export",
73                        "help-id",               GIMP_HELP_FILE_EXPORT_AS,
74                        "ok-button-label",       _("_Export"),
75 
76                        "automatic-label",       _("By Extension"),
77                        "automatic-help-id",     GIMP_HELP_FILE_SAVE_BY_EXTENSION,
78 
79                        "action",                GTK_FILE_CHOOSER_ACTION_SAVE,
80                        "file-procs",            GIMP_FILE_PROCEDURE_GROUP_EXPORT,
81                        "file-procs-all-images", GIMP_FILE_PROCEDURE_GROUP_SAVE,
82                        "file-filter-label",     _("All export images"),
83                        NULL);
84 }
85 
86 void
gimp_export_dialog_set_image(GimpExportDialog * dialog,GimpImage * image)87 gimp_export_dialog_set_image (GimpExportDialog *dialog,
88                               GimpImage        *image)
89 {
90   GimpFileDialog *file_dialog;
91   GFile          *dir_file  = NULL;
92   GFile          *name_file = NULL;
93   GFile          *ext_file  = NULL;
94   gchar          *basename;
95 
96   g_return_if_fail (GIMP_IS_EXPORT_DIALOG (dialog));
97   g_return_if_fail (GIMP_IS_IMAGE (image));
98 
99   file_dialog = GIMP_FILE_DIALOG (dialog);
100 
101   file_dialog->image = image;
102 
103   gimp_file_dialog_set_file_proc (file_dialog, NULL);
104 
105   /* Priority of default paths for Export:
106    *
107    *   1. Last Export path
108    *   2. Path of import source
109    *   3. Path of XCF source
110    *   4. Last path of any save to XCF
111    *   5. Last Export path of any document
112    *   6. The default path (usually the OS 'Documents' path)
113    */
114 
115   dir_file = gimp_image_get_exported_file (image);
116 
117   if (! dir_file)
118     dir_file = g_object_get_data (G_OBJECT (image),
119                                   "gimp-image-source-file");
120 
121   if (! dir_file)
122     dir_file = gimp_image_get_imported_file (image);
123 
124   if (! dir_file)
125     dir_file = gimp_image_get_file (image);
126 
127   if (! dir_file)
128     dir_file = g_object_get_data (G_OBJECT (file_dialog->gimp),
129                                   GIMP_FILE_SAVE_LAST_FILE_KEY);
130 
131   if (! dir_file)
132     dir_file = g_object_get_data (G_OBJECT (file_dialog->gimp),
133                                   GIMP_FILE_EXPORT_LAST_FILE_KEY);
134 
135   if (! dir_file)
136     dir_file = gimp_file_dialog_get_default_folder (file_dialog);
137 
138   /* Priority of default basenames for Export:
139    *
140    *   1. Last Export name
141    *   3. Save URI
142    *   2. Source file name
143    *   3. 'Untitled'
144    */
145 
146   name_file = gimp_image_get_exported_file (image);
147 
148   if (! name_file)
149     name_file = gimp_image_get_file (image);
150 
151   if (! name_file)
152     name_file = gimp_image_get_imported_file (image);
153 
154   if (! name_file)
155     name_file = gimp_image_get_untitled_file (image);
156 
157 
158   /* Priority of default type/extension for Export:
159    *
160    *   1. Type of last Export
161    *   2. Type of the image Import
162    *   3. Type of latest Export of any document
163    *   4. Default file type set in Preferences
164    */
165 
166   ext_file = gimp_image_get_exported_file (image);
167 
168   if (! ext_file)
169     ext_file = gimp_image_get_imported_file (image);
170 
171   if (! ext_file)
172     ext_file = g_object_get_data (G_OBJECT (file_dialog->gimp),
173                                   GIMP_FILE_EXPORT_LAST_FILE_KEY);
174 
175   if (ext_file)
176     {
177       g_object_ref (ext_file);
178     }
179   else
180     {
181       const gchar *extension;
182       gchar       *uri;
183 
184       gimp_enum_get_value (GIMP_TYPE_EXPORT_FILE_TYPE,
185                            image->gimp->config->export_file_type,
186                            NULL, &extension, NULL, NULL);
187 
188       uri = g_strconcat ("file:///we/only/care/about/extension.",
189                          extension, NULL);
190       ext_file = g_file_new_for_uri (uri);
191       g_free (uri);
192     }
193 
194   if (ext_file)
195     {
196       GFile *tmp_file = gimp_file_with_new_extension (name_file, ext_file);
197       basename = g_path_get_basename (gimp_file_get_utf8_name (tmp_file));
198       g_object_unref (tmp_file);
199       g_object_unref (ext_file);
200     }
201   else
202     {
203       basename = g_path_get_basename (gimp_file_get_utf8_name (name_file));
204     }
205 
206   if (g_file_query_file_type (dir_file, G_FILE_QUERY_INFO_NONE, NULL) ==
207       G_FILE_TYPE_DIRECTORY)
208     {
209       gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (dialog),
210                                                 dir_file, NULL);
211     }
212   else
213     {
214       GFile *parent_file = g_file_get_parent (dir_file);
215       gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (dialog),
216                                                 parent_file, NULL);
217       g_object_unref (parent_file);
218     }
219 
220   gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), basename);
221 }
222