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 /*
19  * Desktop Entry Specification
20  * http://standards.freedesktop.org/desktop-entry-spec/latest/
21  */
22 
23 #include "config.h"
24 
25 #include <string.h>
26 
27 #include <glib/gstdio.h>
28 
29 #include <libgimp/gimp.h>
30 
31 #include "libgimp/stdplugins-intl.h"
32 
33 
34 #define LOAD_PROC      "file-desktop-link-load"
35 #define PLUG_IN_BINARY "file-desktop-link"
36 #define PLUG_IN_ROLE   "gimp-file-desktop-link"
37 
38 
39 static void    query      (void);
40 static void    run        (const gchar      *name,
41                            gint              nparams,
42                            const GimpParam  *param,
43                            gint             *nreturn_vals,
44                            GimpParam       **return_vals);
45 
46 static gint32  load_image (const gchar      *filename,
47                            GimpRunMode       run_mode,
48                            GError          **error);
49 
50 
51 const GimpPlugInInfo PLUG_IN_INFO =
52 {
53   NULL,  /* init_proc  */
54   NULL,  /* quit_proc  */
55   query, /* query_proc */
56   run,   /* run_proc   */
57 };
58 
MAIN()59 MAIN ()
60 
61 static void
62 query (void)
63 {
64   static const GimpParamDef load_args[] =
65   {
66     { GIMP_PDB_INT32,  "run-mode",     "The run mode { RUN-INTERACTIVE (0), RUN-NONINTERACTIVE (1) }" },
67     { GIMP_PDB_STRING, "filename",     "The name of the file to load" },
68     { GIMP_PDB_STRING, "raw-filename", "The name entered"             }
69   };
70 
71   static const GimpParamDef load_return_vals[] =
72   {
73     { GIMP_PDB_IMAGE,  "image",        "Output image"                 }
74   };
75 
76   gimp_install_procedure (LOAD_PROC,
77                           "Follows a link to an image in a .desktop file",
78                           "Opens a .desktop file and if it is a link, it "
79                           "asks GIMP to open the file the link points to.",
80                           "Sven Neumann",
81                           "Sven Neumann",
82                           "2006",
83                           N_("Desktop Link"),
84                           NULL,
85                           GIMP_PLUGIN,
86                           G_N_ELEMENTS (load_args),
87                           G_N_ELEMENTS (load_return_vals),
88                           load_args, load_return_vals);
89 
90   gimp_register_load_handler (LOAD_PROC, "desktop", "");
91 }
92 
93 static void
run(const gchar * name,gint nparams,const GimpParam * param,gint * nreturn_vals,GimpParam ** return_vals)94 run (const gchar      *name,
95      gint              nparams,
96      const GimpParam  *param,
97      gint             *nreturn_vals,
98      GimpParam       **return_vals)
99 {
100   static GimpParam   values[2];
101   GimpRunMode        run_mode;
102   GimpPDBStatusType  status = GIMP_PDB_EXECUTION_ERROR;
103   GError            *error  = NULL;
104   gint32             image_ID;
105 
106   run_mode = param[0].data.d_int32;
107 
108   *nreturn_vals = 1;
109   *return_vals  = values;
110 
111   values[0].type          = GIMP_PDB_STATUS;
112   values[0].data.d_status = status;
113 
114   if (strcmp (name, LOAD_PROC) == 0)
115     {
116       image_ID = load_image (param[1].data.d_string, run_mode, &error);
117 
118       if (image_ID != -1)
119         {
120           status = GIMP_PDB_SUCCESS;
121 
122           *nreturn_vals = 2;
123           values[1].type         = GIMP_PDB_IMAGE;
124           values[1].data.d_image = image_ID;
125         }
126       else if (error)
127         {
128           *nreturn_vals = 2;
129           values[1].type          = GIMP_PDB_STRING;
130           values[1].data.d_string = error->message;
131         }
132     }
133   else
134     {
135       status = GIMP_PDB_CALLING_ERROR;
136     }
137 
138   values[0].data.d_status = status;
139 }
140 
141 static gint32
load_image(const gchar * filename,GimpRunMode run_mode,GError ** load_error)142 load_image (const gchar  *filename,
143             GimpRunMode   run_mode,
144             GError      **load_error)
145 {
146   GKeyFile *file     = g_key_file_new ();
147   gchar    *group    = NULL;
148   gchar    *value    = NULL;
149   gint32    image_ID = -1;
150   GError   *error    = NULL;
151 
152   if (! g_key_file_load_from_file (file, filename, G_KEY_FILE_NONE, &error))
153     goto out;
154 
155   group = g_key_file_get_start_group (file);
156   if (! group || strcmp (group, G_KEY_FILE_DESKTOP_GROUP) != 0)
157     goto out;
158 
159   value = g_key_file_get_value (file,
160                                 group, G_KEY_FILE_DESKTOP_KEY_TYPE, &error);
161   if (! value || strcmp (value, G_KEY_FILE_DESKTOP_TYPE_LINK) != 0)
162     goto out;
163 
164   g_free (value);
165 
166   value = g_key_file_get_value (file,
167                                 group, G_KEY_FILE_DESKTOP_KEY_URL, &error);
168   if (value)
169     image_ID = gimp_file_load (run_mode, value, value);
170 
171  out:
172   if (error)
173     {
174       g_set_error (load_error, error->domain, error->code,
175                    _("Error loading desktop file '%s': %s"),
176                    gimp_filename_to_utf8 (filename), error->message);
177       g_error_free (error);
178     }
179 
180   g_free (value);
181   g_free (group);
182   g_key_file_free (file);
183 
184   return image_ID;
185 }
186