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

« back to all changes in this revision

Viewing changes to app/dialogs/file-save-dialog.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
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
18
 */
 
19
 
 
20
#include "config.h"
 
21
 
 
22
#include <string.h>
 
23
 
 
24
#include <gtk/gtk.h>
 
25
 
 
26
#include "libgimpwidgets/gimpwidgets.h"
 
27
 
 
28
#include "dialogs-types.h"
 
29
 
 
30
#include "core/gimp.h"
 
31
#include "core/gimpimage.h"
 
32
#include "core/gimpprogress.h"
 
33
 
 
34
#include "file/file-save.h"
 
35
#include "file/file-utils.h"
 
36
 
 
37
#include "widgets/gimpfiledialog.h"
 
38
#include "widgets/gimphelp-ids.h"
 
39
#include "widgets/gimpmessagebox.h"
 
40
#include "widgets/gimpmessagedialog.h"
 
41
 
 
42
#include "file-save-dialog.h"
 
43
 
 
44
#include "gimp-intl.h"
 
45
 
 
46
 
 
47
/*  local function prototypes  */
 
48
 
 
49
static void       file_save_dialog_response    (GtkWidget     *save_dialog,
 
50
                                                gint           response_id,
 
51
                                                Gimp          *gimp);
 
52
static void       file_save_overwrite          (GtkWidget     *save_dialog,
 
53
                                                const gchar   *uri,
 
54
                                                const gchar   *raw_filename);
 
55
static void       file_save_overwrite_response (GtkWidget     *dialog,
 
56
                                                gint           response_id,
 
57
                                                gpointer       data);
 
58
static gboolean   file_save_dialog_save_image  (GtkWidget     *save_dialog,
 
59
                                                GimpImage     *gimage,
 
60
                                                const gchar   *uri,
 
61
                                                const gchar   *raw_filename,
 
62
                                                PlugInProcDef *save_proc,
 
63
                                                gboolean       save_a_copy);
 
64
 
 
65
 
 
66
/*  public functions  */
 
67
 
 
68
GtkWidget *
 
69
file_save_dialog_new (Gimp *gimp)
 
70
{
 
71
  GtkWidget *dialog;
 
72
 
 
73
  g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
 
74
 
 
75
  dialog = gimp_file_dialog_new (gimp,
 
76
                                 GTK_FILE_CHOOSER_ACTION_SAVE,
 
77
                                 _("Save Image"), "gimp-file-save",
 
78
                                 GTK_STOCK_SAVE,
 
79
                                 GIMP_HELP_FILE_SAVE);
 
80
 
 
81
  g_signal_connect (dialog, "response",
 
82
                    G_CALLBACK (file_save_dialog_response),
 
83
                    gimp);
 
84
 
 
85
  return dialog;
 
86
}
 
87
 
 
88
 
 
89
/*  private functions  */
 
90
 
 
91
static void
 
92
file_save_dialog_response (GtkWidget *save_dialog,
 
93
                           gint       response_id,
 
94
                           Gimp      *gimp)
 
95
{
 
96
  GimpFileDialog *dialog = GIMP_FILE_DIALOG (save_dialog);
 
97
  gchar          *uri;
 
98
 
 
99
  if (response_id != GTK_RESPONSE_OK)
 
100
    {
 
101
      if (! dialog->busy)
 
102
        gtk_widget_hide (save_dialog);
 
103
 
 
104
      return;
 
105
    }
 
106
 
 
107
  uri = gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (save_dialog));
 
108
 
 
109
  if (uri && strlen (uri))
 
110
    {
 
111
      gchar *filename = g_filename_from_uri (uri, NULL, NULL);
 
112
      g_return_if_fail (filename != NULL);
 
113
 
 
114
      if (g_file_test (filename, G_FILE_TEST_EXISTS))
 
115
        {
 
116
          file_save_overwrite (save_dialog, uri, uri);
 
117
        }
 
118
      else
 
119
        {
 
120
          gimp_file_dialog_set_sensitive (dialog, FALSE);
 
121
 
 
122
          if (file_save_dialog_save_image (save_dialog,
 
123
                                           dialog->gimage,
 
124
                                           uri,
 
125
                                           uri,
 
126
                                           dialog->file_proc,
 
127
                                           dialog->save_a_copy))
 
128
            {
 
129
              gtk_widget_hide (save_dialog);
 
130
            }
 
131
 
 
132
          gimp_file_dialog_set_sensitive (dialog, TRUE);
 
133
        }
 
134
 
 
135
      g_free (filename);
 
136
      g_free (uri);
 
137
    }
 
138
}
 
139
 
 
140
typedef struct _OverwriteData OverwriteData;
 
141
 
 
142
struct _OverwriteData
 
143
{
 
144
  GtkWidget *save_dialog;
 
145
  gchar     *uri;
 
146
  gchar     *raw_filename;
 
147
};
 
148
 
 
149
static void
 
150
file_save_overwrite (GtkWidget   *save_dialog,
 
151
                     const gchar *uri,
 
152
                     const gchar *raw_filename)
 
153
{
 
154
  OverwriteData *overwrite_data = g_new0 (OverwriteData, 1);
 
155
  GtkWidget     *dialog;
 
156
  gchar         *filename;
 
157
 
 
158
  overwrite_data->save_dialog  = save_dialog;
 
159
  overwrite_data->uri          = g_strdup (uri);
 
160
  overwrite_data->raw_filename = g_strdup (raw_filename);
 
161
 
 
162
  dialog =
 
163
    gimp_message_dialog_new (_("File exists"), GIMP_STOCK_WARNING,
 
164
                             save_dialog, 0,
 
165
                             gimp_standard_help_func, NULL,
 
166
 
 
167
                             GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
 
168
                             _("_Replace"),    GTK_RESPONSE_OK,
 
169
 
 
170
                             NULL);
 
171
 
 
172
  g_signal_connect (dialog, "response",
 
173
                    G_CALLBACK (file_save_overwrite_response),
 
174
                    overwrite_data);
 
175
 
 
176
  filename = file_utils_uri_to_utf8_filename (uri);
 
177
  gimp_message_box_set_primary_text (GIMP_MESSAGE_DIALOG (dialog)->box,
 
178
                                     _("A file named '%s' already exists."),
 
179
                                     filename);
 
180
  g_free (filename);
 
181
 
 
182
  gimp_message_box_set_text (GIMP_MESSAGE_DIALOG (dialog)->box,
 
183
                             _("Do you want to replace it with the image "
 
184
                               "you are saving?"));
 
185
 
 
186
  gimp_file_dialog_set_sensitive (GIMP_FILE_DIALOG (save_dialog), FALSE);
 
187
  gtk_dialog_set_response_sensitive (GTK_DIALOG (save_dialog),
 
188
                                     GTK_RESPONSE_CANCEL, FALSE);
 
189
 
 
190
  gtk_widget_show (dialog);
 
191
}
 
192
 
 
193
static void
 
194
file_save_overwrite_response (GtkWidget *dialog,
 
195
                              gint       response_id,
 
196
                              gpointer   data)
 
197
{
 
198
  OverwriteData  *overwrite_data = data;
 
199
  GimpFileDialog *save_dialog = GIMP_FILE_DIALOG (overwrite_data->save_dialog);
 
200
 
 
201
  gtk_dialog_set_response_sensitive (GTK_DIALOG (save_dialog),
 
202
                                     GTK_RESPONSE_CANCEL, TRUE);
 
203
 
 
204
  gtk_widget_destroy (dialog);
 
205
 
 
206
  if (response_id == GTK_RESPONSE_OK)
 
207
    {
 
208
      if (file_save_dialog_save_image (overwrite_data->save_dialog,
 
209
                                       save_dialog->gimage,
 
210
                                       overwrite_data->uri,
 
211
                                       overwrite_data->raw_filename,
 
212
                                       save_dialog->file_proc,
 
213
                                       save_dialog->save_a_copy))
 
214
        {
 
215
          gtk_widget_hide (overwrite_data->save_dialog);
 
216
        }
 
217
    }
 
218
 
 
219
  gimp_file_dialog_set_sensitive (save_dialog, TRUE);
 
220
 
 
221
  g_free (overwrite_data->uri);
 
222
  g_free (overwrite_data->raw_filename);
 
223
  g_free (overwrite_data);
 
224
}
 
225
 
 
226
static gboolean
 
227
file_save_dialog_save_image (GtkWidget     *save_dialog,
 
228
                             GimpImage     *gimage,
 
229
                             const gchar   *uri,
 
230
                             const gchar   *raw_filename,
 
231
                             PlugInProcDef *save_proc,
 
232
                             gboolean       save_a_copy)
 
233
{
 
234
  GimpPDBStatusType  status;
 
235
  GError            *error = NULL;
 
236
 
 
237
  status = file_save_as (gimage,
 
238
                         gimp_get_user_context (gimage->gimp),
 
239
                         GIMP_PROGRESS (save_dialog),
 
240
                         uri,
 
241
                         raw_filename,
 
242
                         save_proc,
 
243
                         GIMP_RUN_INTERACTIVE,
 
244
                         save_a_copy,
 
245
                         &error);
 
246
 
 
247
  if (status != GIMP_PDB_SUCCESS &&
 
248
      status != GIMP_PDB_CANCEL)
 
249
    {
 
250
      gchar *filename = file_utils_uri_to_utf8_filename (uri);
 
251
 
 
252
      g_message (_("Saving '%s' failed:\n\n%s"),
 
253
                 filename, error->message);
 
254
      g_clear_error (&error);
 
255
 
 
256
      g_free (filename);
 
257
 
 
258
      return FALSE;
 
259
    }
 
260
 
 
261
  return TRUE;
 
262
}