gimpdnd-xds.c 7.33 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
2 3
 * Copyright (C) 1995-1997 Spencer Kimball and Peter Mattis
 *
Sven Neumann's avatar
Sven Neumann committed
4 5 6 7 8 9 10 11 12
 * gimpdnd-xds.c
 * Copyright (C) 2005  Sven Neumann <sven@gimp.org>
 *
 * Saving Files via Drag-and-Drop:
 * The Direct Save Protocol for the X Window System
 *
 *   http://www.newplanetsoftware.com/xds/
 *   http://rox.sourceforge.net/xds.html
 *
13
 * This program is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU General Public License as published by
15
 * the Free Software Foundation; either version 3 of the License, or
16 17 18 19 20 21 22 23
 * (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
24
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25 26 27 28 29 30
 */

#include "config.h"

#include <string.h>

31
#include <gegl.h>
32 33
#include <gtk/gtk.h>

34 35
#include "libgimpwidgets/gimpwidgets.h"

36 37 38 39 40
#include "widgets-types.h"

#include "core/gimp.h"
#include "core/gimpimage.h"

41 42
#include "plug-in/gimppluginmanager.h"

43
#include "file/file-procedure.h"
44 45 46 47
#include "file/file-save.h"
#include "file/file-utils.h"

#include "gimpdnd-xds.h"
48
#include "gimpfiledialog.h"
49 50
#include "gimpmessagebox.h"
#include "gimpmessagedialog.h"
51

52
#include "gimp-log.h"
53 54 55 56 57 58
#include "gimp-intl.h"


#define MAX_URI_LEN 4096


59 60 61 62 63 64 65 66
/*  local function prototypes  */

static gboolean   gimp_file_overwrite_dialog (GtkWidget   *parent,
                                              const gchar *uri);


/*  public functions  */

67 68 69 70 71 72 73 74 75
void
gimp_dnd_xds_source_set (GdkDragContext *context,
                         GimpImage      *image)
{
  GdkAtom  property;

  g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
  g_return_if_fail (image == NULL || GIMP_IS_IMAGE (image));

76
  GIMP_LOG (DND, NULL);
77

78
  property = gdk_atom_intern_static_string ("XdndDirectSave0");
79

80
  if (image)
81
    {
82
      GdkAtom  type     = gdk_atom_intern_static_string ("text/plain");
83 84 85 86 87 88 89 90 91 92 93 94 95 96
      gchar   *filename = gimp_image_get_filename (image);
      gchar   *basename;

      if (filename)
        {
          basename = g_path_get_basename (filename);
        }
      else
        {
          gchar *tmp = g_strconcat (_("Untitled"), ".xcf", NULL);
          basename = g_filename_from_utf8 (tmp, -1, NULL, NULL, NULL);
          g_free (tmp);
        }

97 98 99

      gdk_property_change (context->source_window,
                           property, type, 8, GDK_PROP_MODE_REPLACE,
Sven Neumann's avatar
Sven Neumann committed
100 101
                           (const guchar *) basename,
                           basename ? strlen (basename) : 0);
102 103 104 105 106 107 108 109 110 111 112 113 114

      g_free (basename);
      g_free (filename);
    }
  else
    {
      gdk_property_delete (context->source_window, property);
    }
}

void
gimp_dnd_xds_save_image (GdkDragContext   *context,
                         GimpImage        *image,
115
                         GtkSelectionData *selection)
116
{
117
  GimpPlugInProcedure *proc;
118 119
  GdkAtom              property;
  GdkAtom              type;
120 121 122 123
  gint                 length;
  guchar              *data;
  gchar               *uri;
  GError              *error = NULL;
124 125 126 127

  g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
  g_return_if_fail (GIMP_IS_IMAGE (image));

128
  GIMP_LOG (DND, NULL);
129

130 131 132
  property = gdk_atom_intern_static_string ("XdndDirectSave0");
  type     = gdk_atom_intern_static_string ("text/plain");

133 134 135 136 137 138 139 140 141
  if (! gdk_property_get (context->source_window, property, type,
                          0, MAX_URI_LEN, FALSE,
                          NULL, NULL, &length, &data))
    return;


  uri = g_strndup ((const gchar *) data, length);
  g_free (data);

142 143
  proc = file_procedure_find (image->gimp->plug_in_manager->save_procs, uri,
                              NULL);
144 145 146

  if (proc)
    {
147 148 149
      gchar *filename = file_utils_filename_from_uri (uri);

      /*  FIXME: shouldn't overwrite non-local files w/o confirmation  */
150

151 152 153 154
      if (! filename ||
          ! g_file_test (filename, G_FILE_TEST_EXISTS) ||
          gimp_file_overwrite_dialog (NULL, uri))
        {
155
          if (file_save (image,
156
                         gimp_get_user_context (image->gimp), NULL,
157 158
                         uri, proc, GIMP_RUN_INTERACTIVE, FALSE,
                         &error) == GIMP_PDB_SUCCESS)
159
            {
160 161 162
              gtk_selection_data_set (selection,
                                      gtk_selection_data_get_target (selection),
                                      8, (const guchar *) "S", 1);
163 164 165
            }
          else
            {
166 167 168
              gtk_selection_data_set (selection,
                                      gtk_selection_data_get_target (selection),
                                      8, (const guchar *) "E", 1);
169 170 171

              if (error)
                {
172
                  gchar *filename = file_utils_uri_display_name (uri);
173

174 175 176
                  gimp_message (image->gimp, NULL, GIMP_MESSAGE_ERROR,
                                _("Saving '%s' failed:\n\n%s"),
                                filename, error->message);
177

178 179 180
                  g_free (filename);
                  g_error_free (error);
                }
181 182
            }
        }
183 184

      g_free (filename);
185 186 187
    }
  else
    {
188 189 190
      gtk_selection_data_set (selection,
                              gtk_selection_data_get_target (selection),
                              8, (const guchar *) "E", 1);
191

192 193 194
      gimp_message_literal (image->gimp, NULL, GIMP_MESSAGE_ERROR,
			    _("The given filename does not have any known "
			      "file extension."));
195 196 197 198
    }

  g_free (uri);
}
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 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 246 247 248 249 250 251


/*  private functions  */

static gboolean
gimp_file_overwrite_dialog (GtkWidget   *parent,
                            const gchar *uri)
{
  GtkWidget *dialog;
  gchar     *filename;
  gboolean   overwrite = FALSE;

  dialog = gimp_message_dialog_new (_("File Exists"), GIMP_STOCK_WARNING,
                                    parent, GTK_DIALOG_DESTROY_WITH_PARENT,
                                    gimp_standard_help_func, NULL,

                                    GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                                    _("_Replace"),    GTK_RESPONSE_OK,

                                    NULL);

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

  filename = file_utils_uri_display_name (uri);
  gimp_message_box_set_primary_text (GIMP_MESSAGE_DIALOG (dialog)->box,
                                     _("A file named '%s' already exists."),
                                     filename);
  g_free (filename);

  gimp_message_box_set_text (GIMP_MESSAGE_DIALOG (dialog)->box,
                             _("Do you want to replace it with the image "
                               "you are saving?"));

  if (GTK_IS_DIALOG (parent))
    gtk_dialog_set_response_sensitive (GTK_DIALOG (parent),
                                       GTK_RESPONSE_CANCEL, FALSE);

  g_object_ref (dialog);

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

  gtk_widget_destroy (dialog);
  g_object_unref (dialog);

  if (GTK_IS_DIALOG (parent))
    gtk_dialog_set_response_sensitive (GTK_DIALOG (parent),
                                       GTK_RESPONSE_CANCEL, TRUE);

  return overwrite;
}