1 /* GIMP - The GNU Image Manipulation Program
2  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
16  */
17 
18 #include "config.h"
19 
20 #include <gegl.h>
21 #include <gtk/gtk.h>
22 
23 #include "libgimpbase/gimpbase.h"
24 #include "libgimpwidgets/gimpwidgets.h"
25 
26 #include "actions-types.h"
27 
28 #include "core/gimp.h"
29 #include "core/gimpcontainer.h"
30 #include "core/gimpcontext.h"
31 #include "core/gimpdata.h"
32 #include "core/gimpdatafactory.h"
33 
34 #include "file/file-open.h"
35 
36 #include "widgets/gimpclipboard.h"
37 #include "widgets/gimpcontainerview.h"
38 #include "widgets/gimpdataeditor.h"
39 #include "widgets/gimpdatafactoryview.h"
40 #include "widgets/gimpdialogfactory.h"
41 #include "widgets/gimpmessagebox.h"
42 #include "widgets/gimpmessagedialog.h"
43 #include "widgets/gimpwidgets-utils.h"
44 #include "widgets/gimpwindowstrategy.h"
45 #include "widgets/gimpwidgets-utils.h"
46 
47 #include "dialogs/data-delete-dialog.h"
48 
49 #include "actions.h"
50 #include "data-commands.h"
51 
52 #include "gimp-intl.h"
53 
54 
55 /*  public functions  */
56 
57 void
data_open_as_image_cmd_callback(GimpAction * action,GVariant * value,gpointer user_data)58 data_open_as_image_cmd_callback (GimpAction *action,
59                                  GVariant   *value,
60                                  gpointer    user_data)
61 {
62   GimpDataFactoryView *view = GIMP_DATA_FACTORY_VIEW (user_data);
63   GimpContext         *context;
64   GimpData            *data;
65 
66   context =
67     gimp_container_view_get_context (GIMP_CONTAINER_EDITOR (view)->view);
68 
69   data = (GimpData *)
70     gimp_context_get_by_type (context,
71                               gimp_data_factory_view_get_children_type (view));
72 
73   if (data && gimp_data_get_file (data))
74     {
75       GFile             *file   = gimp_data_get_file (data);
76       GtkWidget         *widget = GTK_WIDGET (view);
77       GimpImage         *image;
78       GimpPDBStatusType  status;
79       GError            *error = NULL;
80 
81       image = file_open_with_display (context->gimp, context, NULL,
82                                       file, FALSE,
83                                       G_OBJECT (gtk_widget_get_screen (widget)),
84                                       gimp_widget_get_monitor (widget),
85                                       &status, &error);
86 
87       if (! image && status != GIMP_PDB_CANCEL)
88         {
89           gimp_message (context->gimp, G_OBJECT (view),
90                         GIMP_MESSAGE_ERROR,
91                         _("Opening '%s' failed:\n\n%s"),
92                         gimp_file_get_utf8_name (file), error->message);
93           g_clear_error (&error);
94         }
95     }
96 }
97 
98 void
data_new_cmd_callback(GimpAction * action,GVariant * value,gpointer user_data)99 data_new_cmd_callback (GimpAction *action,
100                        GVariant   *value,
101                        gpointer    user_data)
102 {
103   GimpDataFactoryView *view = GIMP_DATA_FACTORY_VIEW (user_data);
104 
105   if (gimp_data_factory_view_has_data_new_func (view))
106     {
107       GimpDataFactory *factory;
108       GimpContext     *context;
109       GimpData        *data;
110 
111       factory = gimp_data_factory_view_get_data_factory (view);
112 
113       context =
114         gimp_container_view_get_context (GIMP_CONTAINER_EDITOR (view)->view);
115 
116       data = gimp_data_factory_data_new (factory, context, _("Untitled"));
117 
118       if (data)
119         {
120           gimp_context_set_by_type (context,
121                                     gimp_data_factory_view_get_children_type (view),
122                                     GIMP_OBJECT (data));
123 
124           gtk_button_clicked (GTK_BUTTON (gimp_data_factory_view_get_edit_button (view)));
125         }
126     }
127 }
128 
129 void
data_duplicate_cmd_callback(GimpAction * action,GVariant * value,gpointer user_data)130 data_duplicate_cmd_callback (GimpAction *action,
131                              GVariant   *value,
132                              gpointer    user_data)
133 {
134   GimpDataFactoryView *view = GIMP_DATA_FACTORY_VIEW (user_data);
135   GimpContext         *context;
136   GimpData            *data;
137 
138   context = gimp_container_view_get_context (GIMP_CONTAINER_EDITOR (view)->view);
139 
140   data = (GimpData *)
141     gimp_context_get_by_type (context,
142                               gimp_data_factory_view_get_children_type (view));
143 
144   if (data && gimp_data_factory_view_have (view, GIMP_OBJECT (data)))
145     {
146       GimpData *new_data;
147 
148       new_data = gimp_data_factory_data_duplicate (gimp_data_factory_view_get_data_factory (view), data);
149 
150       if (new_data)
151         {
152           gimp_context_set_by_type (context,
153                                     gimp_data_factory_view_get_children_type (view),
154                                     GIMP_OBJECT (new_data));
155 
156           gtk_button_clicked (GTK_BUTTON (gimp_data_factory_view_get_edit_button (view)));
157         }
158     }
159 }
160 
161 void
data_copy_location_cmd_callback(GimpAction * action,GVariant * value,gpointer user_data)162 data_copy_location_cmd_callback (GimpAction *action,
163                                  GVariant   *value,
164                                  gpointer    user_data)
165 {
166   GimpDataFactoryView *view = GIMP_DATA_FACTORY_VIEW (user_data);
167   GimpContext         *context;
168   GimpData            *data;
169 
170   context = gimp_container_view_get_context (GIMP_CONTAINER_EDITOR (view)->view);
171 
172   data = (GimpData *)
173     gimp_context_get_by_type (context,
174                               gimp_data_factory_view_get_children_type (view));
175 
176   if (data)
177     {
178       GFile *file = gimp_data_get_file (data);
179 
180       if (file)
181         {
182           gchar *uri = g_file_get_uri (file);
183 
184           gimp_clipboard_set_text (context->gimp, uri);
185           g_free (uri);
186         }
187     }
188 }
189 
190 void
data_show_in_file_manager_cmd_callback(GimpAction * action,GVariant * value,gpointer user_data)191 data_show_in_file_manager_cmd_callback (GimpAction *action,
192                                         GVariant   *value,
193                                         gpointer    user_data)
194 {
195   GimpDataFactoryView *view = GIMP_DATA_FACTORY_VIEW (user_data);
196   GimpContext         *context;
197   GimpData            *data;
198 
199   context = gimp_container_view_get_context (GIMP_CONTAINER_EDITOR (view)->view);
200 
201   data = (GimpData *)
202     gimp_context_get_by_type (context,
203                               gimp_data_factory_view_get_children_type (view));
204 
205   if (data)
206     {
207       GFile *file = gimp_data_get_file (data);
208 
209       if (file)
210         {
211           GError *error = NULL;
212 
213           if (! gimp_file_show_in_file_manager (file, &error))
214             {
215               gimp_message (context->gimp, G_OBJECT (view),
216                             GIMP_MESSAGE_ERROR,
217                             _("Can't show file in file manager: %s"),
218                             error->message);
219               g_clear_error (&error);
220             }
221         }
222     }
223 }
224 
225 void
data_delete_cmd_callback(GimpAction * action,GVariant * value,gpointer user_data)226 data_delete_cmd_callback (GimpAction *action,
227                           GVariant   *value,
228                           gpointer    user_data)
229 {
230   GimpDataFactoryView *view = GIMP_DATA_FACTORY_VIEW (user_data);
231   GimpContext         *context;
232   GimpData            *data;
233 
234   context =
235     gimp_container_view_get_context (GIMP_CONTAINER_EDITOR (view)->view);
236 
237   data = (GimpData *)
238     gimp_context_get_by_type (context,
239                               gimp_data_factory_view_get_children_type (view));
240 
241   if (data                          &&
242       gimp_data_is_deletable (data) &&
243       gimp_data_factory_view_have (view, GIMP_OBJECT (data)))
244     {
245       GimpDataFactory *factory;
246       GtkWidget       *dialog;
247 
248       factory = gimp_data_factory_view_get_data_factory (view);
249 
250       dialog = data_delete_dialog_new (factory, data, context,
251                                        GTK_WIDGET (view));
252       gtk_widget_show (dialog);
253     }
254 }
255 
256 void
data_refresh_cmd_callback(GimpAction * action,GVariant * value,gpointer user_data)257 data_refresh_cmd_callback (GimpAction *action,
258                            GVariant   *value,
259                            gpointer    user_data)
260 {
261   GimpDataFactoryView *view = GIMP_DATA_FACTORY_VIEW (user_data);
262   Gimp                *gimp;
263   return_if_no_gimp (gimp, user_data);
264 
265   gimp_set_busy (gimp);
266   gimp_data_factory_data_refresh (gimp_data_factory_view_get_data_factory (view),
267                                   action_data_get_context (user_data));
268   gimp_unset_busy (gimp);
269 }
270 
271 void
data_edit_cmd_callback(GimpAction * action,GVariant * value,gpointer user_data)272 data_edit_cmd_callback (GimpAction *action,
273                         GVariant   *value,
274                         gpointer    user_data)
275 {
276   GimpDataFactoryView *view = GIMP_DATA_FACTORY_VIEW (user_data);
277   GimpContext         *context;
278   GimpData            *data;
279 
280   context = gimp_container_view_get_context (GIMP_CONTAINER_EDITOR (view)->view);
281 
282   data = (GimpData *)
283     gimp_context_get_by_type (context,
284                               gimp_data_factory_view_get_children_type (view));
285 
286   if (data && gimp_data_factory_view_have (view, GIMP_OBJECT (data)))
287     {
288       GdkScreen *screen  = gtk_widget_get_screen (GTK_WIDGET (view));
289       gint       monitor = gimp_widget_get_monitor (GTK_WIDGET (view));
290       GtkWidget *dockable;
291 
292       dockable =
293         gimp_window_strategy_show_dockable_dialog (GIMP_WINDOW_STRATEGY (gimp_get_window_strategy (context->gimp)),
294                                                    context->gimp,
295                                                    gimp_dialog_factory_get_singleton (),
296                                                    screen,
297                                                    monitor,
298                                                    g_variant_get_string (value,
299                                                                          NULL));
300 
301       gimp_data_editor_set_data (GIMP_DATA_EDITOR (gtk_bin_get_child (GTK_BIN (dockable))),
302                                  data);
303     }
304 }
305