gimp-parasites.c 4.73 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/* gimpparasite.c: Copyright 1998 Jay Cox <jaycox@earthlink.net> 
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (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
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

Tor Lillqvist's avatar
Tor Lillqvist committed
18
19
#include "config.h"

Sven Neumann's avatar
Sven Neumann committed
20
21
#include <gtk/gtk.h>

22
#include <stdio.h>
Tor Lillqvist's avatar
Tor Lillqvist committed
23
#ifdef HAVE_UNISTD_H
24
#include <unistd.h>
Tor Lillqvist's avatar
Tor Lillqvist committed
25
26
#endif

27
28
#include "libgimpbase/gimpbase.h"

29
#include "core-types.h"
Sven Neumann's avatar
Sven Neumann committed
30

31
#include "gimp.h"
32
#include "gimpparasite.h"
33
34
#include "gimpparasitelist.h"

Marc Lehmann's avatar
Marc Lehmann committed
35
#include "gimprc.h"
36

37

38
void
39
40
gimp_parasites_init (Gimp *gimp)
{
41
42
  g_return_if_fail (gimp != NULL);
  g_return_if_fail (GIMP_IS_GIMP (gimp));
43
44
45
46
  g_return_if_fail (gimp->parasites == NULL);

  gimp->parasites = gimp_parasite_list_new ();
}
47
48

void 
49
gimp_parasites_exit (Gimp *gimp)
50
{
51
52
53
  g_return_if_fail (gimp != NULL);
  g_return_if_fail (GIMP_IS_GIMP (gimp));

54
55
  if (gimp->parasites)
    {
56
      g_object_unref (G_OBJECT (gimp->parasites));
57
58
      gimp->parasites = NULL;
    }
59
60
61
}

void
62
63
gimp_parasite_attach (Gimp         *gimp,
		      GimpParasite *parasite)
64
{
65
  gimp_parasite_list_add (gimp->parasites, parasite);
66
67
68
}

void
69
70
gimp_parasite_detach (Gimp        *gimp,
		      const gchar *name)
71
{
72
  gimp_parasite_list_remove (gimp->parasites, name);
73
74
}

75
GimpParasite *
76
77
gimp_parasite_find (Gimp        *gimp,
		    const gchar *name)
78
{
79
  return gimp_parasite_list_find (gimp->parasites, name);
80
81
}

82
static void 
83
84
85
list_func (gchar          *key,
	   GimpParasite   *p,
	   gchar        ***cur)
Marc Lehmann's avatar
Marc Lehmann committed
86
87
88
89
{
  *(*cur)++ = (char *) g_strdup (key);
}

90
gchar **
91
92
gimp_parasite_list (Gimp *gimp,
		    gint *count)
Marc Lehmann's avatar
Marc Lehmann committed
93
{
94
95
  gchar **list;
  gchar **cur;
Marc Lehmann's avatar
Marc Lehmann committed
96

97
  *count = gimp_parasite_list_length (gimp->parasites);
98
  cur = list = g_new (gchar *, *count);
Marc Lehmann's avatar
Marc Lehmann committed
99

100
  gimp_parasite_list_foreach (gimp->parasites, (GHFunc) list_func, &cur);
Marc Lehmann's avatar
Marc Lehmann committed
101
102
103
104
  
  return list;
}

105
static void 
106
save_func (gchar        *key,
107
	   GimpParasite *parasite,
108
	   FILE         *fp)
Marc Lehmann's avatar
Marc Lehmann committed
109
{
110
  if (gimp_parasite_is_persistent (parasite))
Marc Lehmann's avatar
Marc Lehmann committed
111
    {
112
113
      gchar   *s;
      guint32  l;
Marc Lehmann's avatar
Marc Lehmann committed
114

115
      fprintf (fp, "(parasite \"%s\" %lu \"",
116
117
	       gimp_parasite_name (parasite),
	       gimp_parasite_flags (parasite));
Marc Lehmann's avatar
Marc Lehmann committed
118

Marc Lehmann's avatar
Marc Lehmann committed
119
120
121
122
123
124
125
      /*
       * the current methodology is: never move the parasiterc from one
       * system to another. If you want to do this you should probably
       * write out parasites which contain any non-alphanumeric(+some)
       * characters as \xHH sequences altogether.
       */

126
127
      for (s = (gchar *) gimp_parasite_data (parasite),
	     l = gimp_parasite_data_size (parasite);
Marc Lehmann's avatar
Marc Lehmann committed
128
129
130
131
132
           l;
           l--, s++)
        {
          switch (*s)
            {
Marc Lehmann's avatar
Marc Lehmann committed
133
              case '\\': fputs ("\\\\", fp); break;
Marc Lehmann's avatar
Marc Lehmann committed
134
135
136
137
138
139
140
141
142
              case '\0': fputs ("\\0", fp); break;
              case '"' : fputs ("\\\"", fp); break;
              /* disabled, not portable!  */
/*              case '\n': fputs ("\\n", fp); break;*/
/*              case '\r': fputs ("\\r", fp); break;*/
              case 26  : fputs ("\\z", fp); break;
              default  : fputc (*s, fp); break;
            }
        }
143

Marc Lehmann's avatar
Marc Lehmann committed
144
145
146
147
      fputs ("\")\n\n", fp);
    }
}

148
void
149
150
151
152
153
gimp_parasiterc_load (Gimp *gimp)
{
  gchar *filename;

  filename = gimp_personal_rc_file ("parasiterc");
154
  gimprc_parse_file (filename);
155
156
157
158
159
  g_free (filename);
}

void
gimp_parasiterc_save (Gimp *gimp)
160
{
161
162
  gchar *tmp_filename = NULL;
  gchar *bak_filename = NULL;
163
  gchar *rc_filename  = NULL;
164
  FILE  *fp;
Marc Lehmann's avatar
Marc Lehmann committed
165

166
167
168
169
170
  tmp_filename = gimp_personal_rc_file ("#parasiterc.tmp~");
  bak_filename = gimp_personal_rc_file ("parasiterc.bak");
  rc_filename  = gimp_personal_rc_file ("parasiterc");

  fp = fopen (tmp_filename, "w");
Marc Lehmann's avatar
Marc Lehmann committed
171
172

  if (!fp)
173
    goto cleanup;
174

175
176
177
178
  fprintf (fp,
	   "# GIMP parasiterc\n"
	   "# This file will be entirely rewritten every time you "
	   "quit the gimp.\n\n");
179

180
  gimp_parasite_list_foreach (gimp->parasites, (GHFunc) save_func, fp);
181

Marc Lehmann's avatar
Marc Lehmann committed
182
183
  fclose (fp);

184
185
#if defined(G_OS_WIN32) || defined(__EMX__)
  /* First rename the old parasiterc out of the way */
186
187
  unlink (bak_filename);
  rename (rc_filename, bak_filename);
188
189
#endif

190
  if (rename (tmp_filename, rc_filename) != 0)
191
192
193
    {
#if defined(G_OS_WIN32) || defined(__EMX__)
      /* Rename the old parasiterc back */
194
      rename (bak_filename, rc_filename);
195
#endif
196
      unlink (tmp_filename);
197
    }
198
199
200
201
202

 cleanup:
  g_free (tmp_filename);
  g_free (bak_filename);
  g_free (rc_filename);
203
}