1
/* GIMP - The GNU Image Manipulation Program
2
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; either version 2 of the License, or
7
* (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21
#include <libgnomevfs/gnome-vfs.h>
24
#include <libgnomeui/gnome-authentication-manager.h>
26
#ifdef HAVE_GNOME_KEYRING
27
#include <gnome-keyring.h>
30
#include <libgimp/gimp.h>
31
#include <libgimp/gimpui.h>
33
#include "uri-backend.h"
35
#include "libgimp/stdplugins-intl.h"
41
/* local function prototypes */
43
static gchar * get_protocols (void);
44
static gboolean copy_uri (const gchar *src_uri,
45
const gchar *dest_uri,
46
const gchar *copying_format_str,
47
const gchar *copied_format_str,
50
#ifdef HAVE_GNOME_KEYRING
51
static void vfs_async_fill_authentication_callback (gconstpointer in,
56
GnomeVFSModuleCallbackResponse response,
57
gpointer response_data);
58
static void vfs_fill_authentication_callback (gconstpointer in,
63
#endif /* HAVE_GNOME_KEYRING */
66
/* private variables */
68
static gchar *supported_protocols = NULL;
71
/* public functions */
74
uri_backend_init (const gchar *plugin_name,
79
if (! gnome_vfs_init ())
81
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
82
"%s", "Could not initialize GnomeVFS");
89
if (run_mode == GIMP_RUN_INTERACTIVE)
91
gimp_ui_init (plugin_name, FALSE);
92
gnome_authentication_manager_init ();
96
#ifdef HAVE_GNOME_KEYRING
97
gnome_vfs_async_module_callback_set_default
98
(GNOME_VFS_MODULE_CALLBACK_FILL_AUTHENTICATION,
99
vfs_async_fill_authentication_callback,
103
gnome_vfs_module_callback_set_default
104
(GNOME_VFS_MODULE_CALLBACK_FILL_AUTHENTICATION,
105
vfs_fill_authentication_callback,
108
#endif /* HAVE_GNOME_KEYRING */
111
#endif /* HAVE_GNOMEUI */
117
uri_backend_shutdown (void)
119
gnome_vfs_shutdown ();
123
uri_backend_get_load_help (void)
125
return "Loads a file using the GnomeVFS library";
129
uri_backend_get_save_help (void)
131
return "Saves a file using the GnomeVFS library";
135
uri_backend_get_load_protocols (void)
137
if (! supported_protocols)
138
supported_protocols = get_protocols ();
140
return supported_protocols;
144
uri_backend_get_save_protocols (void)
146
if (! supported_protocols)
147
supported_protocols = get_protocols ();
149
return supported_protocols;
153
uri_backend_load_image (const gchar *uri,
154
const gchar *tmpname,
155
GimpRunMode run_mode,
161
dest_uri = g_filename_to_uri (tmpname, NULL, NULL);
162
success = copy_uri (uri, dest_uri,
163
_("Downloading %s of image data"),
164
_("Downloaded %s of image data"),
172
uri_backend_save_image (const gchar *uri,
173
const gchar *tmpname,
174
GimpRunMode run_mode,
180
src_uri = g_filename_to_uri (tmpname, NULL, NULL);
181
success = copy_uri (src_uri, uri,
182
_("Uploading %s of image data"),
183
_("Uploaded %s of image data"),
191
/* private functions */
196
static const gchar *protocols[] =
208
GString *string = g_string_new (NULL);
211
for (i = 0; i < G_N_ELEMENTS (protocols); i++)
214
GnomeVFSURI *vfs_uri;
216
uri = g_strdup_printf ("%s//foo/bar.xcf", protocols[i]);
218
vfs_uri = gnome_vfs_uri_new (uri);
223
g_string_append_c (string, ',');
225
g_string_append (string, protocols[i]);
227
gnome_vfs_uri_unref (vfs_uri);
233
return g_string_free (string, FALSE);
237
copy_uri (const gchar *src_uri,
238
const gchar *dest_uri,
239
const gchar *copying_format_str,
240
const gchar *copied_format_str,
243
GnomeVFSHandle *read_handle;
244
GnomeVFSHandle *write_handle;
245
GnomeVFSFileInfo *src_info;
246
GnomeVFSFileSize file_size = 0;
247
GnomeVFSFileSize bytes_read = 0;
248
guchar buffer[BUFSIZE];
249
GnomeVFSResult result;
251
GTimeVal last_time = { 0, 0 };
253
gimp_progress_init (_("Connecting to server"));
255
src_info = gnome_vfs_file_info_new ();
256
result = gnome_vfs_get_file_info (src_uri, src_info, 0);
258
/* ignore errors here, they will be noticed below */
259
if (result == GNOME_VFS_OK &&
260
(src_info->valid_fields & GNOME_VFS_FILE_INFO_FIELDS_SIZE))
262
file_size = src_info->size;
265
gnome_vfs_file_info_unref (src_info);
267
result = gnome_vfs_open (&read_handle, src_uri, GNOME_VFS_OPEN_READ);
269
if (result != GNOME_VFS_OK)
271
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
272
_("Could not open '%s' for reading: %s"),
273
src_uri, gnome_vfs_result_to_string (result));
277
result = gnome_vfs_create (&write_handle, dest_uri,
278
GNOME_VFS_OPEN_WRITE, FALSE, 0644);
280
if (result != GNOME_VFS_OK)
282
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
283
_("Could not open '%s' for writing: %s"),
284
dest_uri, gnome_vfs_result_to_string (result));
285
gnome_vfs_close (read_handle);
289
memsize = g_format_size_for_display (file_size);
291
gimp_progress_init_printf (file_size > 0 ?
292
copying_format_str : copied_format_str,
299
GnomeVFSFileSize chunk_read;
300
GnomeVFSFileSize chunk_written;
303
result = gnome_vfs_read (read_handle, buffer, sizeof (buffer),
308
if (result != GNOME_VFS_ERROR_EOF)
310
memsize = g_format_size_for_display (sizeof (buffer));
311
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
312
_("Failed to read %s from '%s': %s"),
314
gnome_vfs_result_to_string (result));
317
gnome_vfs_close (read_handle);
318
gnome_vfs_close (write_handle);
323
gimp_progress_update (1.0);
328
bytes_read += chunk_read;
330
/* update the progress only up to 10 times a second */
332
g_get_current_time (&now);
334
if (((now.tv_sec - last_time.tv_sec) * 1000 +
335
(now.tv_usec - last_time.tv_usec) / 1000) > 100)
339
gimp_progress_update ((gdouble) bytes_read / (gdouble) file_size);
343
memsize = g_format_size_for_display (bytes_read);
345
gimp_progress_set_text_printf (copied_format_str, memsize);
346
gimp_progress_pulse ();
354
result = gnome_vfs_write (write_handle, buffer, chunk_read,
357
if (chunk_written < chunk_read)
359
memsize = g_format_size_for_display (chunk_read);
360
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
361
_("Failed to write %s to '%s': %s"),
363
gnome_vfs_result_to_string (result));
366
gnome_vfs_close (read_handle);
367
gnome_vfs_close (write_handle);
372
gnome_vfs_close (read_handle);
373
gnome_vfs_close (write_handle);
378
#ifdef HAVE_GNOME_KEYRING
380
/* gnome-keyring code copied from
381
* libgnomeui/libgnomeui/gnome-authentication-manager.c CVS version 1.13
386
const GnomeVFSModuleCallbackFillAuthenticationIn *in_args;
387
GnomeVFSModuleCallbackFillAuthenticationOut *out_args;
389
GnomeVFSModuleCallbackResponse response;
390
gpointer response_data;
394
fill_auth_callback (GnomeKeyringResult result,
398
FillCallbackInfo *info = data;
399
GnomeKeyringNetworkPasswordData *pwd_data;;
401
if (result != GNOME_KEYRING_RESULT_OK || list == NULL)
403
info->out_args->valid = FALSE;
407
/* We use the first result, which is the least specific match */
408
pwd_data = list->data;
410
info->out_args->valid = TRUE;
411
info->out_args->username = g_strdup (pwd_data->user);
412
info->out_args->domain = g_strdup (pwd_data->domain);
413
info->out_args->password = g_strdup (pwd_data->password);
416
info->response (info->response_data);
419
static void /* GnomeVFSAsyncModuleCallback */
420
vfs_async_fill_authentication_callback (gconstpointer in,
425
GnomeVFSModuleCallbackResponse response,
426
gpointer response_data)
428
GnomeVFSModuleCallbackFillAuthenticationIn *in_real;
429
GnomeVFSModuleCallbackFillAuthenticationOut *out_real;
431
FillCallbackInfo *info;
434
(sizeof (GnomeVFSModuleCallbackFillAuthenticationIn) == in_size &&
435
sizeof (GnomeVFSModuleCallbackFillAuthenticationOut) == out_size);
437
g_return_if_fail (in != NULL);
438
g_return_if_fail (out != NULL);
440
in_real = (GnomeVFSModuleCallbackFillAuthenticationIn *)in;
441
out_real = (GnomeVFSModuleCallbackFillAuthenticationOut *)out;
443
info = g_new (FillCallbackInfo, 1);
445
info->in_args = in_real;
446
info->out_args = out_real;
447
info->response = response;
448
info->response_data = response_data;
450
request = gnome_keyring_find_network_password (in_real->username,
461
static void /* GnomeVFSModuleCallback */
462
vfs_fill_authentication_callback (gconstpointer in,
468
GnomeVFSModuleCallbackFillAuthenticationIn *in_real;
469
GnomeVFSModuleCallbackFillAuthenticationOut *out_real;
470
GnomeKeyringNetworkPasswordData *pwd_data;
472
GnomeKeyringResult result;
475
(sizeof (GnomeVFSModuleCallbackFillAuthenticationIn) == in_size &&
476
sizeof (GnomeVFSModuleCallbackFillAuthenticationOut) == out_size);
478
g_return_if_fail (in != NULL);
479
g_return_if_fail (out != NULL);
481
in_real = (GnomeVFSModuleCallbackFillAuthenticationIn *)in;
482
out_real = (GnomeVFSModuleCallbackFillAuthenticationOut *)out;
484
result = gnome_keyring_find_network_password_sync (in_real->username,
493
if (result != GNOME_KEYRING_RESULT_OK || list == NULL)
495
out_real->valid = FALSE;
499
/* We use the first result, which is the least specific match */
500
pwd_data = list->data;
502
out_real->valid = TRUE;
503
out_real->username = g_strdup (pwd_data->user);
504
out_real->domain = g_strdup (pwd_data->domain);
505
out_real->password = g_strdup (pwd_data->password);
507
gnome_keyring_network_password_list_free (list);
511
#endif /* HAVE_GNOME_KEYRING */