gimpsavedialog.c 12.4 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 28 29 30 31
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * gimpsavedialog.c
 * Copyright (C) 2015 Jehan <jehan@girinstud.io>
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <gegl.h>
#include <gtk/gtk.h>

#include "libgimpbase/gimpbase.h"
#include "libgimpwidgets/gimpwidgets.h"

#include "widgets-types.h"

#include "core/gimp.h"
32
#include "core/gimp-utils.h"
33
#include "core/gimpimage.h"
34
#include "core/gimpimage-metadata.h"
35 36 37 38 39 40 41 42 43

#include "file/gimp-file.h"

#include "gimphelp-ids.h"
#include "gimpsavedialog.h"

#include "gimp-intl.h"


44
typedef struct _GimpSaveDialogState GimpSaveDialogState;
45

46 47 48
struct _GimpSaveDialogState
{
  gchar    *filter_name;
49
  gboolean  compression;
50 51 52
};


53
static void     gimp_save_dialog_constructed       (GObject             *object);
54

55 56 57 58
static void     gimp_save_dialog_save_state        (GimpFileDialog      *dialog,
                                                    const gchar         *state_name);
static void     gimp_save_dialog_load_state        (GimpFileDialog      *dialog,
                                                    const gchar         *state_name);
59

60 61 62 63
static void     gimp_save_dialog_add_compression_toggle
                                                   (GimpSaveDialog      *dialog);
static void     gimp_save_dialog_compression_toggled
                                                   (GtkToggleButton     *button,
64
                                                    GimpSaveDialog      *dialog);
65 66

static GimpSaveDialogState
67 68 69 70
              * gimp_save_dialog_get_state         (GimpSaveDialog      *dialog);
static void     gimp_save_dialog_set_state         (GimpSaveDialog      *dialog,
                                                    GimpSaveDialogState *state);
static void     gimp_save_dialog_state_destroy     (GimpSaveDialogState *state);
71

72 73 74 75 76 77

G_DEFINE_TYPE (GimpSaveDialog, gimp_save_dialog,
               GIMP_TYPE_FILE_DIALOG)

#define parent_class gimp_save_dialog_parent_class

78

79 80 81
static void
gimp_save_dialog_class_init (GimpSaveDialogClass *klass)
{
82 83
  GObjectClass        *object_class = G_OBJECT_CLASS (klass);
  GimpFileDialogClass *fd_class     = GIMP_FILE_DIALOG_CLASS (klass);
84

85
  object_class->constructed = gimp_save_dialog_constructed;
86

87 88
  fd_class->save_state      = gimp_save_dialog_save_state;
  fd_class->load_state      = gimp_save_dialog_load_state;
89 90 91 92 93 94 95 96 97 98 99 100 101
}

static void
gimp_save_dialog_init (GimpSaveDialog *dialog)
{
}

static void
gimp_save_dialog_constructed (GObject *object)
{
  GimpSaveDialog *dialog = GIMP_SAVE_DIALOG (object);

  /* GimpFileDialog's constructed() is doing a few initialization
102 103
   * common to all file dialogs.
   */
104 105
  G_OBJECT_CLASS (parent_class)->constructed (object);

106
  gimp_save_dialog_add_compression_toggle (dialog);
107 108
}

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
static void
gimp_save_dialog_save_state (GimpFileDialog *dialog,
                             const gchar    *state_name)
{
  g_object_set_data_full (G_OBJECT (dialog->gimp), state_name,
                          gimp_save_dialog_get_state (GIMP_SAVE_DIALOG (dialog)),
                          (GDestroyNotify) gimp_save_dialog_state_destroy);
}

static void
gimp_save_dialog_load_state (GimpFileDialog *dialog,
                             const gchar    *state_name)
{
  GimpSaveDialogState *state;

  state = g_object_get_data (G_OBJECT (dialog->gimp), state_name);

  if (state)
    gimp_save_dialog_set_state (GIMP_SAVE_DIALOG (dialog), state);
}


131 132 133 134 135 136 137
/*  public functions  */

GtkWidget *
gimp_save_dialog_new (Gimp *gimp)
{
  g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);

138 139 140 141 142
  return g_object_new (GIMP_TYPE_SAVE_DIALOG,
                       "gimp",                  gimp,
                       "title",                 _("Save Image"),
                       "role",                  "gimp-file-save",
                       "help-id",               GIMP_HELP_FILE_SAVE,
143
                       "ok-button-label",       _("_Save"),
144 145 146 147 148 149 150 151 152

                       "automatic-label",       _("By Extension"),
                       "automatic-help-id",     GIMP_HELP_FILE_SAVE_BY_EXTENSION,

                       "action",                GTK_FILE_CHOOSER_ACTION_SAVE,
                       "file-procs",            GIMP_FILE_PROCEDURE_GROUP_SAVE,
                       "file-procs-all-images", GIMP_FILE_PROCEDURE_GROUP_EXPORT,
                       "file-filter-label",     _("All XCF images"),
                       NULL);
153 154 155 156 157 158 159 160 161
}

void
gimp_save_dialog_set_image (GimpSaveDialog *dialog,
                            GimpImage      *image,
                            gboolean        save_a_copy,
                            gboolean        close_after_saving,
                            GimpObject     *display)
{
162 163 164 165 166 167 168 169 170
  GimpFileDialog *file_dialog;
  GFile          *dir_file  = NULL;
  GFile          *name_file = NULL;
  GFile          *ext_file  = NULL;
  gchar          *basename;
  const gchar    *version_string;
  gint            rle_version;
  gint            zlib_version;
  gchar          *tooltip;
171 172 173 174

  g_return_if_fail (GIMP_IS_SAVE_DIALOG (dialog));
  g_return_if_fail (GIMP_IS_IMAGE (image));

175 176
  file_dialog = GIMP_FILE_DIALOG (dialog);

177 178 179 180
  file_dialog->image         = image;
  dialog->save_a_copy        = save_a_copy;
  dialog->close_after_saving = close_after_saving;
  dialog->display_to_close   = display;
181

182
  gimp_file_dialog_set_file_proc (file_dialog, NULL);
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208

  /*
   * Priority of default paths for Save:
   *
   *   1. Last Save a copy-path (applies only to Save a copy)
   *   2. Last Save path
   *   3. Path of source XCF
   *   4. Path of Import source
   *   5. Last Save path of any GIMP document
   *   6. The default path (usually the OS 'Documents' path)
   */

  if (save_a_copy)
    dir_file = gimp_image_get_save_a_copy_file (image);

  if (! dir_file)
    dir_file = gimp_image_get_file (image);

  if (! dir_file)
    dir_file = g_object_get_data (G_OBJECT (image),
                                  "gimp-image-source-file");

  if (! dir_file)
    dir_file = gimp_image_get_imported_file (image);

  if (! dir_file)
209
    dir_file = g_object_get_data (G_OBJECT (file_dialog->gimp),
210 211 212
                                  GIMP_FILE_SAVE_LAST_FILE_KEY);

  if (! dir_file)
213
    dir_file = gimp_file_dialog_get_default_folder (file_dialog);
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245


  /* Priority of default basenames for Save:
   *
   *   1. Last Save a copy-name (applies only to Save a copy)
   *   2. Last Save name
   *   3. Last Export name
   *   3. The source image path
   *   3. 'Untitled'
   */

  if (save_a_copy)
    name_file = gimp_image_get_save_a_copy_file (image);

  if (! name_file)
    name_file = gimp_image_get_file (image);

  if (! name_file)
    name_file = gimp_image_get_exported_file (image);

  if (! name_file)
    name_file = gimp_image_get_imported_file (image);

  if (! name_file)
    name_file = gimp_image_get_untitled_file (image);


  /* Priority of default type/extension for Save:
   *
   *   1. Type of last Save
   *   2. .xcf (which we don't explicitly append)
   */
246

247 248 249 250 251 252 253 254 255 256 257 258
  ext_file = gimp_image_get_file (image);

  if (ext_file)
    g_object_ref (ext_file);
  else
    ext_file = g_file_new_for_uri ("file:///we/only/care/about/extension.xcf");

  gimp_image_get_xcf_version (image, FALSE, &rle_version,  &version_string);
  gimp_image_get_xcf_version (image, TRUE,  &zlib_version, NULL);

  if (rle_version == zlib_version)
    {
259 260 261
      tooltip = g_strdup_printf (_("The image uses features from %s, disabling "
                                   "compression won't make the XCF file "
                                   "readable by older GIMP versions."),
262 263 264 265
                                 version_string);
    }
  else
    {
266
      tooltip = g_strdup_printf (_("Keep compression disabled to make the XCF "
267 268
                                   "file readable by %s and later."),
                                 version_string);
269
    }
270

271 272 273 274 275 276 277 278 279
  if (gimp_image_get_metadata (image))
    {
      gchar *temp_tooltip;

      temp_tooltip = g_strconcat (tooltip, "\n",
                                  _("Metadata won't be visible in GIMP "
                                    "older than version 2.10."), NULL);
      g_free (tooltip);
      tooltip = temp_tooltip;
280 281
    }

282
  gtk_label_set_text (GTK_LABEL (dialog->compat_info), tooltip);
283 284
  g_free (tooltip);

285
  gtk_widget_show (dialog->compression_toggle);
286
  gtk_widget_show (dialog->compat_info);
287

288 289
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->compression_toggle),
                                gimp_image_get_xcf_compression (image));
290 291 292

  if (ext_file)
    {
293
      GFile *tmp_file = gimp_file_with_new_extension (name_file, ext_file);
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
      basename = g_path_get_basename (gimp_file_get_utf8_name (tmp_file));
      g_object_unref (tmp_file);
      g_object_unref (ext_file);
    }
  else
    {
      basename = g_path_get_basename (gimp_file_get_utf8_name (name_file));
    }

  if (g_file_query_file_type (dir_file, G_FILE_QUERY_INFO_NONE, NULL) ==
      G_FILE_TYPE_DIRECTORY)
    {
      gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (dialog),
                                                dir_file, NULL);
    }
  else
    {
      GFile *parent_file = g_file_get_parent (dir_file);
      gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (dialog),
                                                parent_file, NULL);
      g_object_unref (parent_file);
    }

  gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), basename);
}

320

321 322 323
/*  private functions  */

static void
324
gimp_save_dialog_add_compression_toggle (GimpSaveDialog *dialog)
325
{
326
  GtkWidget *frame;
327

328 329
  dialog->compression_toggle =
    gtk_check_button_new_with_label (_("Save this XCF file with better but slower compression"));
330

331 332 333 334 335
  frame = gimp_frame_new (NULL);
  gtk_frame_set_label_widget (GTK_FRAME (frame), dialog->compression_toggle);
  gimp_file_dialog_add_extra_widget (GIMP_FILE_DIALOG (dialog), frame,
                                     FALSE, FALSE, 0);
  gtk_widget_show (frame);
336

337
  /* Additional information explaining file compatibility things */
338
  dialog->compat_info = gtk_label_new ("");
339
  gtk_label_set_xalign (GTK_LABEL (dialog->compat_info), 0.0);
340 341 342
  gimp_label_set_attributes (GTK_LABEL (dialog->compat_info),
                             PANGO_ATTR_STYLE, PANGO_STYLE_ITALIC,
                             -1);
343
  gtk_container_add (GTK_CONTAINER (frame), dialog->compat_info);
344

345 346
  g_signal_connect (dialog->compression_toggle, "toggled",
                    G_CALLBACK (gimp_save_dialog_compression_toggled),
347 348 349 350
                    dialog);
}

static void
351 352
gimp_save_dialog_compression_toggled (GtkToggleButton *button,
                                      GimpSaveDialog  *dialog)
353
{
354
  dialog->compression = gtk_toggle_button_get_active (button);
355
}
356 357 358 359 360 361 362 363 364 365 366 367 368 369

static GimpSaveDialogState *
gimp_save_dialog_get_state (GimpSaveDialog *dialog)
{
  GimpSaveDialogState *state;
  GtkFileFilter       *filter;

  state = g_slice_new0 (GimpSaveDialogState);

  filter = gtk_file_chooser_get_filter (GTK_FILE_CHOOSER (dialog));

  if (filter)
    state->filter_name = g_strdup (gtk_file_filter_get_name (filter));

370
  state->compression = dialog->compression;
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400

  return state;
}

static void
gimp_save_dialog_set_state (GimpSaveDialog      *dialog,
                            GimpSaveDialogState *state)
{
  if (state->filter_name)
    {
      GSList *filters;
      GSList *list;

      filters = gtk_file_chooser_list_filters (GTK_FILE_CHOOSER (dialog));

      for (list = filters; list; list = list->next)
        {
          GtkFileFilter *filter = GTK_FILE_FILTER (list->data);
          const gchar   *name   = gtk_file_filter_get_name (filter);

          if (name && strcmp (state->filter_name, name) == 0)
            {
              gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (dialog), filter);
              break;
            }
        }

      g_slist_free (filters);
    }

401
  dialog->compression = state->compression;
402 403 404 405 406 407 408 409
}

static void
gimp_save_dialog_state_destroy (GimpSaveDialogState *state)
{
  g_free (state->filter_name);
  g_slice_free (GimpSaveDialogState, state);
}