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 <libgimp/gimp.h>
21 
22 #include "print-utils.h"
23 
24 
25 GKeyFile *
print_utils_key_file_load_from_rcfile(const gchar * basename)26 print_utils_key_file_load_from_rcfile (const gchar *basename)
27 {
28   GKeyFile *key_file;
29   gchar    *filename;
30 
31   g_return_val_if_fail (basename != NULL, NULL);
32 
33   filename = g_build_filename (gimp_directory (), basename, NULL);
34 
35   key_file = g_key_file_new ();
36 
37   if (! g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL))
38     {
39       g_key_file_free (key_file);
40       key_file = NULL;
41     }
42 
43   g_free (filename);
44 
45   return key_file;
46 }
47 
48 GKeyFile *
print_utils_key_file_load_from_parasite(gint32 image_ID,const gchar * parasite_name)49 print_utils_key_file_load_from_parasite (gint32       image_ID,
50                                          const gchar *parasite_name)
51 {
52   GimpParasite *parasite;
53   GKeyFile     *key_file;
54   GError       *error = NULL;
55 
56   g_return_val_if_fail (parasite_name != NULL, NULL);
57 
58   parasite = gimp_image_get_parasite (image_ID, parasite_name);
59 
60   if (! parasite)
61     return NULL;
62 
63   key_file = g_key_file_new ();
64 
65   if (! g_key_file_load_from_data (key_file,
66                                    gimp_parasite_data (parasite),
67                                    gimp_parasite_data_size (parasite),
68                                    G_KEY_FILE_NONE, &error))
69     {
70       g_key_file_free (key_file);
71       gimp_parasite_free (parasite);
72 
73       g_warning ("Unable to create key file from image parasite '%s': %s",
74                  parasite_name, error->message);
75       g_error_free (error);
76       return NULL;
77     }
78 
79   gimp_parasite_free (parasite);
80 
81   return key_file;
82 }
83 
84 void
print_utils_key_file_save_as_rcfile(GKeyFile * key_file,const gchar * basename)85 print_utils_key_file_save_as_rcfile (GKeyFile    *key_file,
86                                      const gchar *basename)
87 {
88   gchar  *filename;
89   gchar  *contents;
90   gsize   length;
91   GError *error = NULL;
92 
93   g_return_if_fail (basename != NULL);
94 
95   contents = g_key_file_to_data (key_file, &length, &error);
96 
97   if (! contents)
98     {
99       g_warning ("Unable to get contents of key file for '%s': %s",
100                  basename, error->message);
101       g_error_free (error);
102       return;
103     }
104 
105   filename = g_build_filename (gimp_directory (), basename, NULL);
106 
107   if (! g_file_set_contents (filename, contents, length, &error))
108     {
109       g_warning ("Unable to write settings to '%s': %s",
110                  gimp_filename_to_utf8 (filename), error->message);
111       g_error_free (error);
112     }
113 
114   g_free (filename);
115   g_free (contents);
116 }
117 
118 void
print_utils_key_file_save_as_parasite(GKeyFile * key_file,gint32 image_ID,const gchar * parasite_name)119 print_utils_key_file_save_as_parasite (GKeyFile    *key_file,
120                                        gint32       image_ID,
121                                        const gchar *parasite_name)
122 {
123   GimpParasite *parasite;
124   gchar        *contents;
125   gsize         length;
126   GError       *error = NULL;
127 
128   g_return_if_fail (parasite_name != NULL);
129 
130   contents = g_key_file_to_data (key_file, &length, &error);
131 
132   if (! contents)
133     {
134       g_warning ("Unable to get contents of key file for parasite '%s': %s",
135                  parasite_name, error->message);
136       g_error_free (error);
137       return;
138     }
139 
140   parasite = gimp_parasite_new (parasite_name, 0, length, contents);
141   g_free (contents);
142 
143   gimp_image_attach_parasite (image_ID, parasite);
144   gimp_parasite_free (parasite);
145 }
146