file-save-dialog.c 15.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995, 1996, 1997 Spencer Kimball and Peter Mattis
 * Copyright (C) 1997 Josh MacDonald
 *
 * 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.
 */

#include "config.h"

#include <string.h>

#include <gtk/gtk.h>

#include "libgimpwidgets/gimpwidgets.h"

28
#include "dialogs-types.h"
29

Michael Natterer's avatar
Michael Natterer committed
30
#include "core/gimp.h"
31
#include "core/gimpimage.h"
32
#include "core/gimpprogress.h"
33

34 35
#include "plug-in/plug-in-proc-def.h"

Michael Natterer's avatar
Michael Natterer committed
36 37 38
#include "file/file-save.h"
#include "file/file-utils.h"

Michael Natterer's avatar
Michael Natterer committed
39
#include "widgets/gimpactiongroup.h"
40
#include "widgets/gimpfiledialog.h"
41
#include "widgets/gimphelp-ids.h"
42
#include "widgets/gimpmessagebox.h"
43
#include "widgets/gimpmessagedialog.h"
44

45 46
#include "file-save-dialog.h"

47
#include "gimp-intl.h"
48 49


50 51
/*  local function prototypes  */

52 53 54 55 56
static void       file_save_dialog_response      (GtkWidget      *save_dialog,
                                                  gint            response_id,
                                                  Gimp           *gimp);
static gboolean   file_save_dialog_check_uri     (GtkWidget      *save_dialog,
                                                  Gimp           *gimp,
57 58 59
                                                  gchar         **ret_uri,
                                                  gchar         **ret_basename,
                                                  PlugInProcDef **ret_save_proc);
60 61 62 63 64 65 66
static gboolean   file_save_dialog_use_extension (GtkWidget      *save_dialog,
                                                  const gchar    *uri);
static gboolean   file_save_dialog_save_image    (GtkWidget      *save_dialog,
                                                  GimpImage      *gimage,
                                                  const gchar    *uri,
                                                  PlugInProcDef  *save_proc,
                                                  gboolean        save_a_copy);
67 68 69 70


/*  public functions  */

71 72
GtkWidget *
file_save_dialog_new (Gimp *gimp)
73
{
74 75
  GtkWidget   *dialog;
  const gchar *uri;
76

77 78
  g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);

79 80 81 82 83
  dialog = gimp_file_dialog_new (gimp,
                                 GTK_FILE_CHOOSER_ACTION_SAVE,
                                 _("Save Image"), "gimp-file-save",
                                 GTK_STOCK_SAVE,
                                 GIMP_HELP_FILE_SAVE);
84

85 86 87 88 89
  gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
                                           GTK_RESPONSE_OK,
                                           GTK_RESPONSE_CANCEL,
                                           -1);

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
  uri = g_object_get_data (G_OBJECT (gimp), "gimp-file-save-last-uri");

  if (uri)
    {
      gchar *folder_uri = g_path_get_dirname (uri);

#ifdef __GNUC__
#warning: FIXME: should use set_uri() but idle stuff in the file chooser seems to override set_current_name() when called immediately after set_uri()
#endif

      if (folder_uri)
        {
          gtk_file_chooser_set_current_folder_uri (GTK_FILE_CHOOSER (dialog),
                                                   folder_uri);
          g_free (folder_uri);
        }

      gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), "");
    }

110
  g_signal_connect (dialog, "response",
111
                    G_CALLBACK (file_save_dialog_response),
112 113
                    gimp);

114
  return dialog;
115 116
}

117 118 119

/*  private functions  */

120
static void
121 122 123
file_save_dialog_response (GtkWidget *save_dialog,
                           gint       response_id,
                           Gimp      *gimp)
124
{
125
  GimpFileDialog *dialog = GIMP_FILE_DIALOG (save_dialog);
126
  gchar          *uri;
127
  gchar          *basename;
128
  PlugInProcDef  *save_proc;
129

130 131
  if (response_id != GTK_RESPONSE_OK)
    {
132
      if (! dialog->busy)
133
        gtk_widget_hide (save_dialog);
134

135 136 137
      return;
    }

138 139
  gimp_file_dialog_set_sensitive (dialog, FALSE);

140 141
  if (file_save_dialog_check_uri (save_dialog, gimp,
                                  &uri, &basename, &save_proc))
142 143 144 145 146 147 148 149 150 151 152
    {
      if (file_save_dialog_save_image (save_dialog,
                                       dialog->gimage,
                                       uri,
                                       save_proc,
                                       dialog->save_a_copy))
        {
          gtk_widget_hide (save_dialog);
        }

      g_free (uri);
153
      g_free (basename);
154 155 156 157 158 159 160 161 162
    }

  gimp_file_dialog_set_sensitive (dialog, TRUE);
}

static gboolean
file_save_dialog_check_uri (GtkWidget      *save_dialog,
                            Gimp           *gimp,
                            gchar         **ret_uri,
163
                            gchar         **ret_basename,
164 165 166 167 168 169 170 171 172
                            PlugInProcDef **ret_save_proc)
{
  GimpFileDialog *dialog = GIMP_FILE_DIALOG (save_dialog);
  gchar          *uri;
  gchar          *basename;
  PlugInProcDef  *save_proc;
  PlugInProcDef  *uri_proc;
  PlugInProcDef  *basename_proc;

173
  uri = gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (save_dialog));
174

175 176 177 178 179 180 181 182 183
  if (! (uri && strlen (uri)))
    return FALSE;

  basename = g_path_get_basename (uri);

  save_proc     = dialog->file_proc;
  uri_proc      = file_utils_find_proc (gimp->save_procs, uri);
  basename_proc = file_utils_find_proc (gimp->save_procs, basename);

184 185 186 187 188
  g_print ("\n\n%s: URI = %s\n",
           G_STRFUNC, uri);
  g_print ("%s: basename = %s\n",
           G_STRFUNC, basename);
  g_print ("%s: selected save_proc: %s\n",
Hans Breuer's avatar
updated  
Hans Breuer committed
189
           G_STRFUNC, save_proc && save_proc->menu_label ? save_proc->menu_label : "NULL");
190 191 192
  g_print ("%s: URI save_proc: %s\n",
           G_STRFUNC, uri_proc ? uri_proc->menu_label : "NULL");
  g_print ("%s: basename save_proc: %s\n\n",
Hans Breuer's avatar
updated  
Hans Breuer committed
193
           G_STRFUNC, basename_proc && basename_proc->menu_label ? basename_proc->menu_label : "NULL");
194 195


196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
  /*  first check if the user entered an extension at all  */
  if (! basename_proc)
    {
      g_print ("%s: basename has no valid extension\n",
               G_STRFUNC);

      if (! strchr (basename, '.'))
        {
          const gchar *ext = NULL;

          g_print ("%s: basename has no '.', trying to add extension\n",
                   G_STRFUNC);

          if (! save_proc)
            {
              ext = "xcf";
            }
          else if (save_proc->extensions_list)
            {
              ext = save_proc->extensions_list->data;
            }

          if (ext)
            {
              gchar *ext_uri      = g_strconcat (uri,      ".", ext, NULL);
              gchar *ext_basename = g_strconcat (basename, ".", ext, NULL);
222
              gchar *utf8;
223

224
              g_print ("%s: appending .%s to basename\n",
225 226 227 228 229 230 231 232 233 234
                       G_STRFUNC, ext);

              g_free (uri);
              g_free (basename);

              uri      = ext_uri;
              basename = ext_basename;

              uri_proc      = file_utils_find_proc (gimp->save_procs, uri);
              basename_proc = file_utils_find_proc (gimp->save_procs, basename);
235 236 237 238 239

              utf8 = g_filename_to_utf8 (basename, -1, NULL, NULL, NULL);
              gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (save_dialog),
                                                 utf8);
              g_free (utf8);
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
            }
          else
            {
              g_print ("%s: save_proc has no extensions, continuing without\n",
                       G_STRFUNC);

              /*  there may be file formats with no extension at all, use
               *  the selected proc in this case.
               */
              basename_proc = save_proc;

              if (! uri_proc)
                uri_proc = basename_proc;
            }

          if (! basename_proc)
            {
              g_print ("%s: unable to figure save_proc, bailing out\n",
                       G_STRFUNC);

              g_message (_("The given filename does not have any known "
                           "file extension. Please enter a known file "
                           "extension or select a file format from the "
                           "file format list."));
              g_free (uri);
              g_free (basename);
              return FALSE;
            }
        }
      else if (save_proc && ! save_proc->extensions_list)
        {
          g_print ("%s: basename has '.', but save_proc has no extensions, "
                   "accepting random extension\n",
                   G_STRFUNC);

          /*  accept any random extension if the file format has
           *  no extensions at all
           */
          basename_proc = save_proc;

          if (! uri_proc)
            uri_proc = basename_proc;
        }
    }

  /*  then check if the selected format matches the entered extension  */
  if (! save_proc)
287
    {
288 289 290 291
      g_print ("%s: no save_proc was selected from the list\n",
               G_STRFUNC);

      if (! basename_proc)
292
        {
293 294 295 296 297 298 299 300 301 302
          g_print ("%s: basename had no useful extension, bailing out\n",
                   G_STRFUNC);

          g_message (_("The given filename does not have any known "
                       "file extension. Please enter a known file "
                       "extension or select a file format from the "
                       "file format list."));
          g_free (uri);
          g_free (basename);
          return FALSE;
303
        }
304 305

      g_print ("%s: use URI's proc '%s' so indirect saving works\n",
Hans Breuer's avatar
updated  
Hans Breuer committed
306
               G_STRFUNC, uri_proc->menu_label ? uri_proc->menu_label : "?");
307 308 309 310 311 312 313 314 315 316

      /*  use the URI's proc if no save proc was selected  */
      save_proc = uri_proc;
    }
  else
    {
      g_print ("%s: save_proc '%s' was selected from the list\n",
               G_STRFUNC, save_proc->menu_label);

      if (save_proc != basename_proc)
317
        {
318 319 320 321 322
          g_print ("%s: however the basename's proc is '%s'\n",
                   G_STRFUNC,
                   basename_proc ? basename_proc->menu_label : "NULL");

          if (uri_proc != basename_proc)
323
            {
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
              g_print ("%s: that's impossible for remote URIs, bailing out\n",
                       G_STRFUNC);

              /*  remote URI  */

              g_message (_("Saving remote files needs to determine the "
                           "file format from the file extension. Please "
                           "enter a file extension that matches the selected "
                           "file format or enter no file extension at all."));
              g_free (uri);
              g_free (basename);
              return FALSE;
            }
          else
            {
              g_print ("%s: ask the user if she really wants that filename\n",
                       G_STRFUNC);

              /*  local URI  */

              if (! file_save_dialog_use_extension (save_dialog, uri))
                {
                  g_free (uri);
                  g_free (basename);
                  return FALSE;
                }
350
            }
351 352 353 354 355
        }
      else if (save_proc != uri_proc)
        {
          g_print ("%s: use URI's proc '%s' so indirect saving works\n",
                   G_STRFUNC, uri_proc->menu_label);
356

357 358 359 360
          /*  need to use the URI's proc for saving because e.g.
           *  the GIF plug-in can't save a GIF to sftp://
           */
          save_proc = uri_proc;
361
        }
362
    }
363

364 365 366
  if (! save_proc)
    {
      g_print ("%s: EEEEEEK\n", G_STRFUNC);
367

368 369 370
      g_message ("Yay! You found a bug. Please report this at "
                 "http://bugzilla.gnome.org/ and paste the console "
                 "output to the 'Additional Comments' field");
371

372 373
      return FALSE;
    }
374

375
  *ret_uri       = uri;
376
  *ret_basename  = basename;
377 378 379 380 381 382 383 384
  *ret_save_proc = save_proc;

  return TRUE;
}

static gboolean
file_save_dialog_use_extension (GtkWidget   *save_dialog,
                                const gchar *uri)
385
{
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
  GtkWidget *dialog;
  gboolean   use_name = FALSE;

  dialog = gimp_message_dialog_new (_("Extension Mismatch"),
                                    GIMP_STOCK_QUESTION,
                                    save_dialog, GTK_DIALOG_DESTROY_WITH_PARENT,
                                    gimp_standard_help_func, NULL,

                                    GTK_STOCK_NO,  GTK_RESPONSE_CANCEL,
                                    GTK_STOCK_YES, GTK_RESPONSE_OK,

                                    NULL);

  gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
                                           GTK_RESPONSE_OK,
                                           GTK_RESPONSE_CANCEL,
                                           -1);

  gimp_message_box_set_primary_text (GIMP_MESSAGE_DIALOG (dialog)->box,
                                     _("The given file extension does "
                                       "not match the chosen file type."));

  gimp_message_box_set_text (GIMP_MESSAGE_DIALOG (dialog)->box,
                             _("Do you want to use this name anyway?"));

  gtk_dialog_set_response_sensitive (GTK_DIALOG (save_dialog),
                                     GTK_RESPONSE_CANCEL, FALSE);

  g_object_ref (dialog);

  use_name = (gimp_dialog_run (GIMP_DIALOG (dialog)) == GTK_RESPONSE_OK);

  gtk_widget_destroy (dialog);
  g_object_unref (dialog);
Michael Natterer's avatar
Michael Natterer committed
420

421 422 423 424 425 426
  gtk_dialog_set_response_sensitive (GTK_DIALOG (save_dialog),
                                     GTK_RESPONSE_CANCEL, TRUE);

  return use_name;
}

427
static gboolean
428 429 430 431
file_save_dialog_save_image (GtkWidget     *save_dialog,
                             GimpImage     *gimage,
                             const gchar   *uri,
                             PlugInProcDef *save_proc,
432
                             gboolean       save_a_copy)
433
{
434
  GimpPDBStatusType  status;
Michael Natterer's avatar
Michael Natterer committed
435 436 437 438 439 440 441 442 443 444
  GError            *error   = NULL;
  GList             *list;
  gboolean           success = TRUE;

  for (list = gimp_action_groups_from_name ("file");
       list;
       list = g_list_next (list))
    {
      gimp_action_group_set_action_sensitive (list->data, "file-quit", FALSE);
    }
445

446 447
  g_object_ref (gimage);

448 449 450 451
  status = file_save (gimage, gimp_get_user_context (gimage->gimp),
                      GIMP_PROGRESS (save_dialog),
                      uri, save_proc,
                      GIMP_RUN_INTERACTIVE, save_a_copy, &error);
452

453 454 455 456 457 458
  if (status == GIMP_PDB_SUCCESS)
    g_object_set_data_full (G_OBJECT (gimage->gimp), "gimp-file-save-last-uri",
                            g_strdup (uri), (GDestroyNotify) g_free);

  g_object_unref (gimage);

459 460 461
  if (status != GIMP_PDB_SUCCESS &&
      status != GIMP_PDB_CANCEL)
    {
462
      gchar *filename = file_utils_uri_display_name (uri);
463 464 465 466 467 468

      g_message (_("Saving '%s' failed:\n\n%s"),
                 filename, error->message);
      g_clear_error (&error);

      g_free (filename);
469

Michael Natterer's avatar
Michael Natterer committed
470
      success = FALSE;
471
    }
472

Michael Natterer's avatar
Michael Natterer committed
473 474 475 476 477 478 479 480
  for (list = gimp_action_groups_from_name ("file");
       list;
       list = g_list_next (list))
    {
      gimp_action_group_set_action_sensitive (list->data, "file-quit", TRUE);
    }

  return success;
481
}