1 /* GIMP - The GNU Image Manipulation Program
2  * Copyright (C) 1995-1999 Spencer Kimball and Peter Mattis
3  *
4  * gimp-contexts.c
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
18  */
19 
20 #include "config.h"
21 
22 #include <gdk-pixbuf/gdk-pixbuf.h>
23 #include <gegl.h>
24 
25 #include "libgimpbase/gimpbase.h"
26 #include "libgimpconfig/gimpconfig.h"
27 
28 #include "core-types.h"
29 
30 #include "gimp.h"
31 #include "gimperror.h"
32 #include "gimp-contexts.h"
33 #include "gimpcontext.h"
34 
35 #include "config/gimpconfig-file.h"
36 
37 #include "gimp-intl.h"
38 
39 
40 void
gimp_contexts_init(Gimp * gimp)41 gimp_contexts_init (Gimp *gimp)
42 {
43   GimpContext *context;
44 
45   g_return_if_fail (GIMP_IS_GIMP (gimp));
46 
47   /*  the default context contains the user's saved preferences
48    *
49    *  TODO: load from disk
50    */
51   context = gimp_context_new (gimp, "Default", NULL);
52   gimp_set_default_context (gimp, context);
53   g_object_unref (context);
54 
55   /*  the initial user_context is a straight copy of the default context
56    */
57   context = gimp_context_new (gimp, "User", context);
58   gimp_set_user_context (gimp, context);
59   g_object_unref (context);
60 }
61 
62 void
gimp_contexts_exit(Gimp * gimp)63 gimp_contexts_exit (Gimp *gimp)
64 {
65   g_return_if_fail (GIMP_IS_GIMP (gimp));
66 
67   gimp_set_user_context (gimp, NULL);
68   gimp_set_default_context (gimp, NULL);
69 }
70 
71 gboolean
gimp_contexts_load(Gimp * gimp,GError ** error)72 gimp_contexts_load (Gimp    *gimp,
73                     GError **error)
74 {
75   GFile    *file;
76   GError   *my_error = NULL;
77   gboolean  success;
78 
79   g_return_val_if_fail (GIMP_IS_GIMP (gimp), FALSE);
80   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
81 
82   file = gimp_directory_file ("contextrc", NULL);
83 
84   if (gimp->be_verbose)
85     g_print ("Parsing '%s'\n", gimp_file_get_utf8_name (file));
86 
87   success = gimp_config_deserialize_gfile (GIMP_CONFIG (gimp_get_user_context (gimp)),
88                                            file,
89                                            NULL, &my_error);
90 
91   g_object_unref (file);
92 
93   if (! success)
94     {
95       if (my_error->code == GIMP_CONFIG_ERROR_OPEN_ENOENT)
96         {
97           g_clear_error (&my_error);
98           success = TRUE;
99         }
100       else
101         {
102           g_propagate_error (error, my_error);
103         }
104     }
105 
106   return success;
107 }
108 
109 gboolean
gimp_contexts_save(Gimp * gimp,GError ** error)110 gimp_contexts_save (Gimp    *gimp,
111                     GError **error)
112 {
113   GFile    *file;
114   gboolean  success;
115 
116   g_return_val_if_fail (GIMP_IS_GIMP (gimp), FALSE);
117   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
118 
119   file = gimp_directory_file ("contextrc", NULL);
120 
121   if (gimp->be_verbose)
122     g_print ("Writing '%s'\n", gimp_file_get_utf8_name (file));
123 
124   success = gimp_config_serialize_to_gfile (GIMP_CONFIG (gimp_get_user_context (gimp)),
125                                             file,
126                                             "GIMP user context",
127                                             "end of user context",
128                                             NULL, error);
129 
130   g_object_unref (file);
131 
132   return success;
133 }
134 
135 gboolean
gimp_contexts_clear(Gimp * gimp,GError ** error)136 gimp_contexts_clear (Gimp    *gimp,
137                      GError **error)
138 {
139   GFile    *file;
140   GError   *my_error = NULL;
141   gboolean  success  = TRUE;
142 
143   g_return_val_if_fail (GIMP_IS_GIMP (gimp), FALSE);
144 
145   file = gimp_directory_file ("contextrc", NULL);
146 
147   if (! g_file_delete (file, NULL, &my_error) &&
148       my_error->code != G_IO_ERROR_NOT_FOUND)
149     {
150       success = FALSE;
151 
152       g_set_error (error, GIMP_ERROR, GIMP_FAILED,
153                    _("Deleting \"%s\" failed: %s"),
154                    gimp_file_get_utf8_name (file), my_error->message);
155     }
156 
157   g_clear_error (&my_error);
158   g_object_unref (file);
159 
160   return success;
161 }
162