gimp-parasites.c 4.18 KB
Newer Older
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 7 8 9 10 11 12 13
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * 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 18
#include "config.h"

19
#include <gio/gio.h>
Sven Neumann's avatar
Sven Neumann committed
20

21
#include "libgimpbase/gimpbase.h"
22
#include "libgimpconfig/gimpconfig.h"
23

24
#include "core-types.h"
Sven Neumann's avatar
Sven Neumann committed
25

26
#include "gimp.h"
27
#include "gimp-parasites.h"
28 29
#include "gimpparasitelist.h"

30

31 32 33 34 35 36 37 38 39 40 41 42
gboolean
gimp_parasite_validate (Gimp                *gimp,
                        const GimpParasite  *parasite,
                        GError             **error)
{
  g_return_val_if_fail (GIMP_IS_GIMP (gimp), FALSE);
  g_return_val_if_fail (parasite != NULL, FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  return TRUE;
}

43
void
44
gimp_parasite_attach (Gimp               *gimp,
45
                      const GimpParasite *parasite)
46
{
47 48 49
  g_return_if_fail (GIMP_IS_GIMP (gimp));
  g_return_if_fail (parasite != NULL);

50
  gimp_parasite_list_add (gimp->parasites, parasite);
51 52 53
}

void
54
gimp_parasite_detach (Gimp        *gimp,
55
                      const gchar *name)
56
{
57 58 59
  g_return_if_fail (GIMP_IS_GIMP (gimp));
  g_return_if_fail (name != NULL);

60
  gimp_parasite_list_remove (gimp->parasites, name);
61 62
}

63
const GimpParasite *
64
gimp_parasite_find (Gimp        *gimp,
65
                    const gchar *name)
66
{
67 68 69
  g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
  g_return_val_if_fail (name != NULL, NULL);

70
  return gimp_parasite_list_find (gimp->parasites, name);
71 72
}

73
static void
Sven Neumann's avatar
Sven Neumann committed
74
list_func (const gchar    *key,
75 76
           GimpParasite   *parasite,
           gchar        ***current)
Marc Lehmann's avatar
Marc Lehmann committed
77
{
Sven Neumann's avatar
Sven Neumann committed
78
  *(*current)++ = g_strdup (key);
Marc Lehmann's avatar
Marc Lehmann committed
79 80
}

81
gchar **
82
gimp_parasite_list (Gimp *gimp,
83
                    gint *count)
Marc Lehmann's avatar
Marc Lehmann committed
84
{
85
  gchar **list;
Sven Neumann's avatar
Sven Neumann committed
86
  gchar **current;
Marc Lehmann's avatar
Marc Lehmann committed
87

88 89 90
  g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
  g_return_val_if_fail (count != NULL, NULL);

91
  *count = gimp_parasite_list_length (gimp->parasites);
Marc Lehmann's avatar
Marc Lehmann committed
92

Sven Neumann's avatar
Sven Neumann committed
93 94 95 96
  list = current = g_new (gchar *, *count);

  gimp_parasite_list_foreach (gimp->parasites, (GHFunc) list_func, &current);

Marc Lehmann's avatar
Marc Lehmann committed
97 98 99
  return list;
}

100

101 102 103 104 105
/*  FIXME: this doesn't belong here  */

void
gimp_parasite_shift_parent (GimpParasite *parasite)
{
106
  g_return_if_fail (parasite != NULL);
107 108 109 110 111 112

  parasite->flags = (parasite->flags >> 8);
}


/*  parasiterc functions  */
113

114
void
115 116
gimp_parasiterc_load (Gimp *gimp)
{
117
  GFile  *file;
Sven Neumann's avatar
Sven Neumann committed
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 128
  if (! gimp_config_deserialize_gfile (GIMP_CONFIG (gimp->parasites),
                                       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

Sven Neumann's avatar
Sven Neumann committed
133
      g_error_free (error);
134 135
    }

136
  g_object_unref (file);
137 138 139 140
}

void
gimp_parasiterc_save (Gimp *gimp)
141
{
142
  const gchar *header =
143 144
    "GIMP parasiterc\n"
    "\n"
145
    "This file will be entirely rewritten each time you exit.";
146
  const gchar *footer =
147
    "end of parasiterc";
148

149
  GFile  *file;
Sven Neumann's avatar
Sven Neumann committed
150
  GError *error = NULL;
151

Sven Neumann's avatar
Sven Neumann committed
152
  g_return_if_fail (GIMP_IS_GIMP (gimp));
153
  g_return_if_fail (GIMP_IS_PARASITE_LIST (gimp->parasites));
Marc Lehmann's avatar
Marc Lehmann committed
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 161 162 163
  if (! gimp_config_serialize_to_gfile (GIMP_CONFIG (gimp->parasites),
                                        file,
                                        header, footer, NULL,
                                        &error))
164
    {
165
      gimp_message_literal (gimp, NULL, GIMP_MESSAGE_ERROR, error->message);
Sven Neumann's avatar
Sven Neumann committed
166
      g_error_free (error);
167
    }
168

169
  g_object_unref (file);
170
}