1 /* gimpparasite.c: Copyright 1998 Jay Cox <jaycox@gimp.org>
2  *
3  * This program is free software: you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 3 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
15  */
16 
17 #include "config.h"
18 
19 #include <gio/gio.h>
20 
21 #include "libgimpbase/gimpbase.h"
22 #include "libgimpconfig/gimpconfig.h"
23 
24 #include "core-types.h"
25 
26 #include "gimp.h"
27 #include "gimp-parasites.h"
28 #include "gimpparasitelist.h"
29 
30 
31 gboolean
gimp_parasite_validate(Gimp * gimp,const GimpParasite * parasite,GError ** error)32 gimp_parasite_validate (Gimp                *gimp,
33                         const GimpParasite  *parasite,
34                         GError             **error)
35 {
36   g_return_val_if_fail (GIMP_IS_GIMP (gimp), FALSE);
37   g_return_val_if_fail (parasite != NULL, FALSE);
38   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
39 
40   return TRUE;
41 }
42 
43 void
gimp_parasite_attach(Gimp * gimp,const GimpParasite * parasite)44 gimp_parasite_attach (Gimp               *gimp,
45                       const GimpParasite *parasite)
46 {
47   g_return_if_fail (GIMP_IS_GIMP (gimp));
48   g_return_if_fail (parasite != NULL);
49 
50   gimp_parasite_list_add (gimp->parasites, parasite);
51 }
52 
53 void
gimp_parasite_detach(Gimp * gimp,const gchar * name)54 gimp_parasite_detach (Gimp        *gimp,
55                       const gchar *name)
56 {
57   g_return_if_fail (GIMP_IS_GIMP (gimp));
58   g_return_if_fail (name != NULL);
59 
60   gimp_parasite_list_remove (gimp->parasites, name);
61 }
62 
63 const GimpParasite *
gimp_parasite_find(Gimp * gimp,const gchar * name)64 gimp_parasite_find (Gimp        *gimp,
65                     const gchar *name)
66 {
67   g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
68   g_return_val_if_fail (name != NULL, NULL);
69 
70   return gimp_parasite_list_find (gimp->parasites, name);
71 }
72 
73 static void
list_func(const gchar * key,GimpParasite * parasite,gchar *** current)74 list_func (const gchar    *key,
75            GimpParasite   *parasite,
76            gchar        ***current)
77 {
78   *(*current)++ = g_strdup (key);
79 }
80 
81 gchar **
gimp_parasite_list(Gimp * gimp,gint * count)82 gimp_parasite_list (Gimp *gimp,
83                     gint *count)
84 {
85   gchar **list;
86   gchar **current;
87 
88   g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
89   g_return_val_if_fail (count != NULL, NULL);
90 
91   *count = gimp_parasite_list_length (gimp->parasites);
92 
93   list = current = g_new (gchar *, *count);
94 
95   gimp_parasite_list_foreach (gimp->parasites, (GHFunc) list_func, &current);
96 
97   return list;
98 }
99 
100 
101 /*  FIXME: this doesn't belong here  */
102 
103 void
gimp_parasite_shift_parent(GimpParasite * parasite)104 gimp_parasite_shift_parent (GimpParasite *parasite)
105 {
106   g_return_if_fail (parasite != NULL);
107 
108   parasite->flags = (parasite->flags >> 8);
109 }
110 
111 
112 /*  parasiterc functions  */
113 
114 void
gimp_parasiterc_load(Gimp * gimp)115 gimp_parasiterc_load (Gimp *gimp)
116 {
117   GFile  *file;
118   GError *error = NULL;
119 
120   g_return_if_fail (GIMP_IS_GIMP (gimp));
121 
122   file = gimp_directory_file ("parasiterc", NULL);
123 
124   if (gimp->be_verbose)
125     g_print ("Parsing '%s'\n", gimp_file_get_utf8_name (file));
126 
127   if (! gimp_config_deserialize_gfile (GIMP_CONFIG (gimp->parasites),
128                                        file, NULL, &error))
129     {
130       if (error->code != GIMP_CONFIG_ERROR_OPEN_ENOENT)
131         gimp_message_literal (gimp, NULL, GIMP_MESSAGE_ERROR, error->message);
132 
133       g_error_free (error);
134     }
135 
136   g_object_unref (file);
137 }
138 
139 void
gimp_parasiterc_save(Gimp * gimp)140 gimp_parasiterc_save (Gimp *gimp)
141 {
142   const gchar *header =
143     "GIMP parasiterc\n"
144     "\n"
145     "This file will be entirely rewritten each time you exit.";
146   const gchar *footer =
147     "end of parasiterc";
148 
149   GFile  *file;
150   GError *error = NULL;
151 
152   g_return_if_fail (GIMP_IS_GIMP (gimp));
153   g_return_if_fail (GIMP_IS_PARASITE_LIST (gimp->parasites));
154 
155   file = gimp_directory_file ("parasiterc", NULL);
156 
157   if (gimp->be_verbose)
158     g_print ("Writing '%s'\n", gimp_file_get_utf8_name (file));
159 
160   if (! gimp_config_serialize_to_gfile (GIMP_CONFIG (gimp->parasites),
161                                         file,
162                                         header, footer, NULL,
163                                         &error))
164     {
165       gimp_message_literal (gimp, NULL, GIMP_MESSAGE_ERROR, error->message);
166       g_error_free (error);
167     }
168 
169   g_object_unref (file);
170 }
171