~ubuntu-branches/ubuntu/hoary/gimp/hoary

« back to all changes in this revision

Viewing changes to app/file/file-save.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2005-04-04 14:51:23 UTC
  • Revision ID: james.westby@ubuntu.com-20050404145123-9py049eeelfymur8
Tags: upstream-2.2.2
ImportĀ upstreamĀ versionĀ 2.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* The GIMP -- an image manipulation program
 
2
 * Copyright (C) 1995, 1996, 1997 Spencer Kimball and Peter Mattis
 
3
 * Copyright (C) 1997 Josh MacDonald
 
4
 *
 
5
 * file-save.c
 
6
 *
 
7
 * This program is free software; you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation; either version 2 of the License, or
 
10
 * (at your option) any later version.
 
11
 *
 
12
 * This program is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with this program; if not, write to the Free Software
 
19
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
20
 */
 
21
 
 
22
#include "config.h"
 
23
 
 
24
#include <errno.h>
 
25
#include <stdio.h>
 
26
#include <stdlib.h>
 
27
#include <string.h>
 
28
#ifdef HAVE_SYS_PARAM_H
 
29
#include <sys/param.h>
 
30
#endif
 
31
#ifdef HAVE_UNISTD_H
 
32
#include <unistd.h>
 
33
#endif
 
34
 
 
35
#include <glib-object.h>
 
36
 
 
37
#ifdef G_OS_WIN32
 
38
#include <io.h>
 
39
#define W_OK 2
 
40
#define access(f,p) _access(f,p)
 
41
#endif
 
42
 
 
43
#include "core/core-types.h"
 
44
 
 
45
#include "config/gimpcoreconfig.h"
 
46
 
 
47
#include "core/gimp.h"
 
48
#include "core/gimpcontext.h"
 
49
#include "core/gimpdocumentlist.h"
 
50
#include "core/gimpdrawable.h"
 
51
#include "core/gimpimage.h"
 
52
#include "core/gimpimagefile.h"
 
53
#include "core/gimpprogress.h"
 
54
 
 
55
#include "pdb/procedural_db.h"
 
56
 
 
57
#include "plug-in/plug-in.h"
 
58
#include "plug-in/plug-in-proc-def.h"
 
59
 
 
60
#include "file-save.h"
 
61
#include "file-utils.h"
 
62
#include "gimprecentlist.h"
 
63
 
 
64
#include "gimp-intl.h"
 
65
 
 
66
 
 
67
/*  public functions  */
 
68
 
 
69
GimpPDBStatusType
 
70
file_save (GimpImage    *gimage,
 
71
           GimpContext  *context,
 
72
           GimpProgress *progress,
 
73
           GimpRunMode   run_mode,
 
74
           GError      **error)
 
75
{
 
76
  const gchar   *uri;
 
77
  PlugInProcDef *file_proc;
 
78
 
 
79
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), GIMP_PDB_CALLING_ERROR);
 
80
  g_return_val_if_fail (GIMP_IS_CONTEXT (context), GIMP_PDB_CALLING_ERROR);
 
81
  g_return_val_if_fail (progress == NULL || GIMP_IS_PROGRESS (progress),
 
82
                        GIMP_PDB_CALLING_ERROR);
 
83
  g_return_val_if_fail (error == NULL || *error == NULL,
 
84
                        GIMP_PDB_CALLING_ERROR);
 
85
 
 
86
  uri = gimp_object_get_name (GIMP_OBJECT (gimage));
 
87
 
 
88
  g_return_val_if_fail (uri != NULL, GIMP_PDB_CALLING_ERROR);
 
89
 
 
90
  file_proc = gimp_image_get_save_proc (gimage);
 
91
 
 
92
  return file_save_as (gimage, context, progress,
 
93
                       uri, uri, file_proc, run_mode, FALSE, error);
 
94
}
 
95
 
 
96
GimpPDBStatusType
 
97
file_save_as (GimpImage      *gimage,
 
98
              GimpContext    *context,
 
99
              GimpProgress   *progress,
 
100
              const gchar    *uri,
 
101
              const gchar    *raw_filename,
 
102
              PlugInProcDef  *file_proc,
 
103
              GimpRunMode     run_mode,
 
104
              gboolean        save_a_copy,
 
105
              GError        **error)
 
106
{
 
107
  const ProcRecord  *proc;
 
108
  Argument          *args;
 
109
  Argument          *return_vals;
 
110
  GimpPDBStatusType  status;
 
111
  gint               i;
 
112
  gchar             *filename;
 
113
 
 
114
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), GIMP_PDB_CALLING_ERROR);
 
115
  g_return_val_if_fail (GIMP_IS_CONTEXT (context), GIMP_PDB_CALLING_ERROR);
 
116
  g_return_val_if_fail (progress == NULL || GIMP_IS_PROGRESS (progress),
 
117
                        GIMP_PDB_CALLING_ERROR);
 
118
  g_return_val_if_fail (uri != NULL, GIMP_PDB_CALLING_ERROR);
 
119
  g_return_val_if_fail (raw_filename != NULL, GIMP_PDB_CALLING_ERROR);
 
120
  g_return_val_if_fail (error == NULL || *error == NULL,
 
121
                        GIMP_PDB_CALLING_ERROR);
 
122
 
 
123
  if (gimp_image_active_drawable (gimage) == NULL)
 
124
    return GIMP_PDB_EXECUTION_ERROR;
 
125
 
 
126
  if (! file_proc)
 
127
    file_proc = file_utils_find_proc (gimage->gimp->save_procs, raw_filename);
 
128
 
 
129
  if (! file_proc)
 
130
    {
 
131
      g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
 
132
                   _("Unknown file type"));
 
133
      return GIMP_PDB_CALLING_ERROR;
 
134
    }
 
135
 
 
136
  filename = g_filename_from_uri (uri, NULL, NULL);
 
137
 
 
138
  if (filename)
 
139
    {
 
140
      /* check if we are saving to a file */
 
141
      if (g_file_test (filename, G_FILE_TEST_EXISTS))
 
142
        {
 
143
          if (! g_file_test (filename, G_FILE_TEST_IS_REGULAR))
 
144
            {
 
145
              g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
 
146
                           _("Not a regular file"));
 
147
              g_free (filename);
 
148
              return GIMP_PDB_EXECUTION_ERROR;
 
149
            }
 
150
 
 
151
          if (access (filename, W_OK) != 0)
 
152
            {
 
153
              g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_ACCES,
 
154
                           g_strerror (errno));
 
155
              g_free (filename);
 
156
              return GIMP_PDB_EXECUTION_ERROR;
 
157
            }
 
158
        }
 
159
    }
 
160
 
 
161
  /* ref the image, so it can't get deleted during save */
 
162
  g_object_ref (gimage);
 
163
 
 
164
  proc = plug_in_proc_def_get_proc (file_proc);
 
165
 
 
166
  args = g_new0 (Argument, proc->num_args);
 
167
 
 
168
  for (i = 0; i < proc->num_args; i++)
 
169
    args[i].arg_type = proc->args[i].arg_type;
 
170
 
 
171
  args[0].value.pdb_int     = run_mode;
 
172
  args[1].value.pdb_int     = gimp_image_get_ID (gimage);
 
173
  args[2].value.pdb_int     = gimp_item_get_ID (GIMP_ITEM (gimp_image_active_drawable (gimage)));
 
174
  args[3].value.pdb_pointer = (gpointer) (filename ? filename : uri);
 
175
  args[4].value.pdb_pointer = (gpointer) raw_filename;
 
176
 
 
177
  return_vals = procedural_db_execute (gimage->gimp, context, progress,
 
178
                                       proc->name, args);
 
179
 
 
180
  if (filename)
 
181
    g_free (filename);
 
182
 
 
183
  status = return_vals[0].value.pdb_int;
 
184
 
 
185
  if (status == GIMP_PDB_SUCCESS)
 
186
    {
 
187
      GimpDocumentList *documents;
 
188
      GimpImagefile    *imagefile;
 
189
 
 
190
      if (save_a_copy)
 
191
        {
 
192
          /*  remember the "save-a-copy" filename for the next invocation  */
 
193
          g_object_set_data_full (G_OBJECT (gimage),
 
194
                                  "gimp-image-save-a-copy", g_strdup (uri),
 
195
                                  (GDestroyNotify) g_free);
 
196
        }
 
197
      else
 
198
        {
 
199
          /*  reset the "save-a-copy" filename when the image URI changes  */
 
200
          if (uri && strcmp (uri, gimp_image_get_uri (gimage)))
 
201
            g_object_set_data (G_OBJECT (gimage),
 
202
                               "gimp-image-save-a-copy", NULL);
 
203
 
 
204
          gimp_image_set_uri (gimage, uri);
 
205
          gimp_image_set_save_proc (gimage, file_proc);
 
206
          gimp_image_clean_all (gimage);
 
207
        }
 
208
 
 
209
      documents = GIMP_DOCUMENT_LIST (gimage->gimp->documents);
 
210
      imagefile = gimp_document_list_add_uri (documents,
 
211
                                              uri, file_proc->mime_type);
 
212
 
 
213
      gimp_imagefile_save_thumbnail (imagefile, file_proc->mime_type, gimage);
 
214
 
 
215
      gimp_recent_list_add_uri (uri, file_proc->mime_type);
 
216
    }
 
217
  else if (status != GIMP_PDB_CANCEL)
 
218
    {
 
219
      g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
 
220
                   _("Plug-In could not save image"));
 
221
    }
 
222
 
 
223
  g_free (return_vals);
 
224
  g_free (args);
 
225
 
 
226
  g_object_unref (gimage);
 
227
 
 
228
  return status;
 
229
}