file-open-dialog.c 20.8 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 32 33 34
/* 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"

#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

35 36 37 38 39
#ifdef __GNUC__
#warning GTK_DISABLE_DEPRECATED
#endif
#undef GTK_DISABLE_DEPRECATED

40 41 42 43 44
#include <gtk/gtk.h>

#include "libgimpmath/gimpmath.h"
#include "libgimpwidgets/gimpwidgets.h"

45
#include "gui-types.h"
46

Michael Natterer's avatar
Michael Natterer committed
47 48
#include "base/temp-buf.h"

49
#include "core/gimp.h"
50
#include "core/gimpcontext.h"
Michael Natterer's avatar
Michael Natterer committed
51
#include "core/gimpcoreconfig.h"
52
#include "core/gimpdocuments.h"
53 54
#include "core/gimpimage.h"

55 56
#include "plug-in/plug-in-types.h"
#include "plug-in/plug-in-proc.h"
Michael Natterer's avatar
Michael Natterer committed
57

Michael Natterer's avatar
Michael Natterer committed
58 59 60
#include "file/file-open.h"
#include "file/file-utils.h"

61 62
#include "widgets/gimpitemfactory.h"

63 64
#include "file-dialog-utils.h"
#include "file-open-dialog.h"
65

66 67 68 69 70
#include "undo.h"

#include "libgimp/gimpintl.h"


71
/*  local function prototypes  */
72

73 74 75 76 77 78 79 80 81
static void     file_open_dialog_create       (Gimp             *gimp);
static void     file_open_genbutton_callback  (GtkWidget        *widget,
					       gpointer          data);
static void     file_open_selchanged_callback (GtkTreeSelection *sel,
                                               gpointer          data);
static void     file_open_ok_callback         (GtkWidget        *widget,
                                               gpointer          data);
static void     file_open_type_callback       (GtkWidget        *widget,
                                               gpointer          data);
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102



static GtkWidget  *fileload                    = NULL;
static GtkWidget  *open_options                = NULL;

static GtkPreview *open_options_preview        = NULL;
static GtkWidget  *open_options_fixed          = NULL;
static GtkWidget  *open_options_label          = NULL;
static GtkWidget  *open_options_frame          = NULL;
static GtkWidget  *open_options_genbuttonlabel = NULL;

/* Some state for the thumbnailer */
static gchar *preview_fullname = NULL;

static PlugInProcDef *load_file_proc = NULL;


/*  public functions  */

void
Michael Natterer's avatar
Michael Natterer committed
103
file_open_dialog_menu_init (Gimp *gimp)
104 105 106 107 108
{
  GimpItemFactoryEntry  entry;
  PlugInProcDef        *file_proc;
  GSList               *list;

Michael Natterer's avatar
Michael Natterer committed
109
  g_return_if_fail (GIMP_IS_GIMP (gimp));
110

Michael Natterer's avatar
Michael Natterer committed
111 112 113
  gimp->load_procs = g_slist_reverse (gimp->load_procs);

  for (list = gimp->load_procs; list; list = g_slist_next (list))
114
    {
115 116
      gchar *basename;
      gchar *lowercase_basename;
117 118 119 120
      gchar *help_page;

      file_proc = (PlugInProcDef *) list->data;

121 122
      basename = g_path_get_basename (file_proc->prog);

123
      lowercase_basename = g_ascii_strdown (basename, -1);
124 125 126

      g_free (basename);

127
      help_page = g_strconcat ("filters/",
128
			       lowercase_basename,
129 130
			       ".html",
			       NULL);
131 132

      g_free (lowercase_basename);
133 134 135 136 137 138

      entry.entry.path            = file_proc->menu_path;
      entry.entry.accelerator     = NULL;
      entry.entry.callback        = file_open_type_callback;
      entry.entry.callback_action = 0;
      entry.entry.item_type       = NULL;
139
      entry.quark_string          = NULL;
140 141 142
      entry.help_page             = help_page;
      entry.description           = NULL;

143
      gimp_menu_item_create (&entry, NULL, file_proc);
144 145 146 147
    }
}

void
148 149 150 151 152 153
file_open_dialog_menu_reset (void)
{
  load_file_proc = NULL;
}

void
Michael Natterer's avatar
Michael Natterer committed
154
file_open_dialog_show (Gimp *gimp)
155
{
Michael Natterer's avatar
Michael Natterer committed
156 157
  g_return_if_fail (GIMP_IS_GIMP (gimp));

158
  if (! fileload)
Michael Natterer's avatar
Michael Natterer committed
159
    file_open_dialog_create (gimp);
160 161 162 163 164 165 166

  gtk_widget_set_sensitive (GTK_WIDGET (fileload), TRUE);
  if (GTK_WIDGET_VISIBLE (fileload))
    return;

  gtk_file_selection_set_filename (GTK_FILE_SELECTION (fileload),
				   "." G_DIR_SEPARATOR_S);
167
  gtk_window_set_title (GTK_WINDOW (fileload), _("Open Image"));
168 169 170 171

  file_dialog_show (fileload);
}

172 173 174 175


/*  private functions  */

176
static void
Michael Natterer's avatar
Michael Natterer committed
177
file_open_dialog_create (Gimp *gimp)
178
{
179
  GtkFileSelection *file_sel;
180
  GtkTreeSelection *tree_sel;
181

182
  fileload = gtk_file_selection_new (_("Open Image"));
Michael Natterer's avatar
Michael Natterer committed
183 184 185

  g_object_set_data (G_OBJECT (fileload), "gimp", gimp);

186 187 188
  gtk_window_set_position (GTK_WINDOW (fileload), GTK_WIN_POS_MOUSE);
  gtk_window_set_wmclass (GTK_WINDOW (fileload), "load_image", "Gimp");

189 190
  file_sel = GTK_FILE_SELECTION (fileload);

191
  gtk_container_set_border_width (GTK_CONTAINER (fileload), 2);
192 193 194 195 196 197 198 199 200 201 202 203 204
  gtk_container_set_border_width (GTK_CONTAINER (file_sel->button_area), 2);

  g_signal_connect_swapped (G_OBJECT (file_sel->cancel_button), "clicked",
			    G_CALLBACK (file_dialog_hide),
			    fileload);
  g_signal_connect (G_OBJECT (fileload), "delete_event",
		    G_CALLBACK (file_dialog_hide),
		    NULL);

  g_signal_connect (G_OBJECT (file_sel->ok_button), "clicked",
		    G_CALLBACK (file_open_ok_callback),
		    fileload);

205 206
  gtk_quit_add_destroy (1, GTK_OBJECT (fileload));

207
  gtk_file_selection_set_select_multiple (GTK_FILE_SELECTION (fileload), TRUE);
208 209
  tree_sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (GTK_FILE_SELECTION (fileload)->file_list));

210
  /* Catch file-list clicks so we can update the preview thumbnail */
211
  g_signal_connect (G_OBJECT (tree_sel), "changed",
212
		    G_CALLBACK (file_open_selchanged_callback),
213
		    fileload);
214 215

  /*  Connect the "F1" help key  */
216 217 218
  gimp_help_connect (fileload,
		     gimp_standard_help_func,
		     "open/dialogs/file_open.html");
219 220

  {
221 222 223 224 225 226
    GimpItemFactory *item_factory;
    GtkWidget       *frame;
    GtkWidget       *vbox;
    GtkWidget       *hbox;
    GtkWidget       *option_menu;
    GtkWidget       *open_options_genbutton;
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246

    open_options = gtk_hbox_new (TRUE, 1);

    /* format-chooser frame */
    frame = gtk_frame_new (_("Determine File Type"));
    gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_ETCHED_IN);
    gtk_box_pack_start (GTK_BOX (open_options), frame, TRUE, TRUE, 4);

    vbox = gtk_vbox_new (FALSE, 2);
    gtk_container_set_border_width (GTK_CONTAINER (vbox), 4);
    gtk_container_add (GTK_CONTAINER (frame), vbox);

    hbox = gtk_hbox_new (FALSE, 0);
    gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
    gtk_widget_show (hbox);

    option_menu = gtk_option_menu_new ();
    gtk_box_pack_start (GTK_BOX (hbox), option_menu, FALSE, FALSE, 0);
    gtk_widget_show (option_menu);

247
    item_factory = gimp_item_factory_from_path ("<Load>");
248
    gtk_option_menu_set_menu (GTK_OPTION_MENU (option_menu),
249
                              GTK_ITEM_FACTORY (item_factory)->widget);
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271

    gtk_widget_show (vbox);
    gtk_widget_show (frame);

    /* Preview frame */
    open_options_frame = frame = gtk_frame_new ("");
    gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_ETCHED_IN);
    gtk_box_pack_end (GTK_BOX (open_options), frame, FALSE, TRUE, 4);

    vbox = gtk_vbox_new (FALSE, 2);
    gtk_container_set_border_width (GTK_CONTAINER (vbox), 4);
    gtk_container_add (GTK_CONTAINER (frame), vbox);

    hbox = gtk_hbox_new (TRUE, 0);
    gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
    gtk_widget_show (hbox);

    open_options_genbutton = gtk_button_new ();
    gtk_box_pack_start (GTK_BOX (hbox), open_options_genbutton,
			TRUE, FALSE, 0);
    gtk_widget_show (open_options_genbutton);	

272 273 274 275
    g_signal_connect (G_OBJECT (open_options_genbutton), "clicked",
		      G_CALLBACK (file_open_genbutton_callback),
		      fileload);

276
    open_options_fixed = gtk_fixed_new ();
277
    gtk_widget_set_size_request (open_options_fixed, 80, 60);
278 279 280 281 282 283 284 285 286 287 288 289 290 291
    gtk_container_add (GTK_CONTAINER (GTK_BIN (open_options_genbutton)),
		       open_options_fixed);
    gtk_widget_show (open_options_fixed);

    {
      GtkWidget* abox;
      GtkWidget* sbox;
      GtkWidget* align;

      sbox = gtk_vbox_new (TRUE, 0);
      gtk_container_add (GTK_CONTAINER (open_options_fixed), sbox);
      gtk_widget_show (sbox);

      align = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
292
      gtk_widget_set_size_request (align, 80, 60);
293 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 320 321 322 323 324
      gtk_box_pack_start (GTK_BOX (sbox), align, FALSE, TRUE, 0);
      gtk_widget_show (align);

      abox = gtk_hbox_new (FALSE, 0);
      gtk_container_add (GTK_CONTAINER (align), abox);
      gtk_widget_show (abox);

      open_options_preview =
	GTK_PREVIEW (gtk_preview_new (GTK_PREVIEW_COLOR));
      gtk_box_pack_start (GTK_BOX (abox), GTK_WIDGET (open_options_preview),
			  FALSE, TRUE, 0);
      gtk_widget_show (GTK_WIDGET (open_options_preview));

      open_options_genbuttonlabel = gtk_label_new (_("Generate\nPreview"));
      gtk_box_pack_start (GTK_BOX (abox), open_options_genbuttonlabel,
			  FALSE, TRUE, 0);
      gtk_widget_show (open_options_genbuttonlabel);
    }

    open_options_label = gtk_label_new ("");
    gtk_box_pack_start (GTK_BOX (vbox), open_options_label, FALSE, FALSE, 0);
    gtk_widget_show (open_options_label); 

    gtk_widget_show (vbox);
    gtk_widget_show (frame);

    /* pack the containing open_options hbox into the open-dialog */
    gtk_box_pack_end (GTK_BOX (GTK_FILE_SELECTION (fileload)->main_vbox),
		      open_options, FALSE, FALSE, 0);
  }

  gtk_frame_set_label (GTK_FRAME (open_options_frame), _("Preview"));
325
  gtk_label_set_text (GTK_LABEL (open_options_label), _("No Selection"));
326 327 328 329 330 331 332 333 334 335 336 337 338 339

  gtk_widget_show (GTK_WIDGET (open_options_genbuttonlabel));
  gtk_widget_hide (GTK_WIDGET (open_options_preview));
  gtk_widget_set_sensitive (GTK_WIDGET (open_options_frame), FALSE);

  gtk_widget_show (open_options);
}

static void
file_open_type_callback (GtkWidget *widget,
			 gpointer   data)
{
  PlugInProcDef *proc = (PlugInProcDef *) data;

340
  file_dialog_update_name (proc, GTK_FILE_SELECTION (fileload));
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 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 401 402 403 404 405 406 407 408 409

  load_file_proc = proc;
}

static guchar *
make_RGBbuf_from_tempbuf (TempBuf *tempbuf,
			  gint    *width_rtn,
			  gint    *height_rtn)
{
  gint    i, j, w, h;
  guchar *tbd;
  guchar *ptr;
  guchar *rtn = NULL;
  guchar  alpha, r, g, b;

  w = (*width_rtn) = tempbuf->width;
  h = (*height_rtn) = tempbuf->height;
  tbd = temp_buf_data (tempbuf);

  switch (tempbuf->bytes)
    {
    case 4:
      rtn = ptr = g_malloc (3 * w * h);
      for (i=0; i<h; i++)
	{
	  for (j=0; j<w; j++)
	    {
	      r = *(tbd++);
	      g = *(tbd++);
	      b = *(tbd++);
	      alpha = *(tbd++);

	      if (alpha & 128)
		{
		  *(ptr++) = r;
		  *(ptr++) = g;
		  *(ptr++) = b;
		}
	      else
		{
		  r = (( (i^j) & 4 ) << 5) | 64;
		  *(ptr++) = r;
		  *(ptr++) = r;
		  *(ptr++) = r;
		}
	    }
	}
      break;

    case 2:
      rtn = ptr = g_malloc (3 * w * h);
      for (i=0; i<h; i++)
	{
	  for (j=0; j<w; j++)
	    {
	      r = *(tbd++);
	      alpha = *(tbd++);

	      if (!(alpha & 128))
		r = (( (i^j) & 4 ) << 5) | 64;

	      *(ptr++) = r;
	      *(ptr++) = r;
	      *(ptr++) = r;
	    }
	}
      break;

    default:
410
      g_warning ("%s: Unknown TempBuf width.", G_STRLOC);
411 412 413 414 415 416 417
    }

  return (rtn);
}

/* don't call with preview_fullname as parameter!  will be clobbered! */
static void
Michael Natterer's avatar
Michael Natterer committed
418 419
set_preview (Gimp        *gimp,
             const gchar *fullfname,
420 421 422 423 424 425 426
	     guchar      *RGB_source,
	     gint         RGB_w,
	     gint         RGB_h)
{
  guchar      *thumb_rgb;
  guchar      *raw_thumb;
  gint         tnw,tnh, i;
427 428
  gchar       *dirname;
  gchar       *basename;
429 430 431 432 433 434 435
  gchar       *tname;
  gchar       *imginfo = NULL;
  struct stat  file_stat;
  struct stat  thumb_stat;
  gboolean     thumb_may_be_outdated = FALSE;
  gboolean     show_generate_label   = TRUE;

436
  dirname  = g_path_get_dirname (fullfname);
437
  basename = g_path_get_basename (fullfname);
438

439
  tname = g_build_filename (dirname, ".xvpics", basename, NULL);
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456

  /*  If the file is newer than its thumbnail, the thumbnail may
   *  be out of date.
   */
  if ((stat (tname,     &thumb_stat) == 0) &&
      (stat (fullfname, &file_stat ) == 0))
    {
      if ((thumb_stat.st_mtime) < (file_stat.st_mtime))
	{
	  thumb_may_be_outdated = TRUE;
	}
    }

  raw_thumb = readXVThumb (tname, &tnw, &tnh, &imginfo);

  g_free (tname);

457 458 459 460
  gtk_frame_set_label (GTK_FRAME (open_options_frame), basename);

  g_free (dirname);
  g_free (basename);
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507

  g_free (preview_fullname);
  preview_fullname = g_strdup (fullfname);

  if (RGB_source)
    {
      gtk_preview_size (open_options_preview, RGB_w, RGB_h);
      
      for (i = 0; i < RGB_h; i++)
	{
	  gtk_preview_draw_row (open_options_preview, &RGB_source[3*i*RGB_w],
				0, i,
				RGB_w);
	}
    }
  else
    {
      if (raw_thumb)
	{
	  thumb_rgb = g_malloc (3 * tnw * tnh);

	  for (i = 0; i < tnw * tnh; i++)
	    {
	      thumb_rgb[i*3  ] = ((raw_thumb[i]>>5)*255)/7;
	      thumb_rgb[i*3+1] = (((raw_thumb[i]>>2)&7)*255)/7;
	      thumb_rgb[i*3+2] = (((raw_thumb[i])&3)*255)/3;
	    }

	  gtk_preview_size (open_options_preview, tnw, tnh);

	  for (i = 0; i < tnh; i++)
	    {
	      gtk_preview_draw_row (open_options_preview, &thumb_rgb[3*i*tnw],
				    0, i,
				    tnw);
	    }

	  g_free (thumb_rgb);
	}
    }

  if (raw_thumb || RGB_source)  /* We can show *some* kind of preview. */
    {
      if (raw_thumb) /* Managed to commit thumbnail file to disk */
	{
	  gtk_label_set_text (GTK_LABEL (open_options_label),
			      thumb_may_be_outdated ?
508 509
			      _("This thumbnail may be out of date") :
			      (imginfo ? imginfo : _("No Information")));
510 511 512 513 514
	  if (imginfo)
	    g_free (imginfo);
	}
      else
	{
515 516
          if (gimp->config->write_thumbnails)
            {
517
	      gtk_label_set_text (GTK_LABEL(open_options_label),
518
				  _("Could not write thumbnail file."));
519 520 521
            }
          else
            {
522
	      gtk_label_set_text (GTK_LABEL(open_options_label),
523
				  _("Thumbnail saving is disabled."));
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
	    }
	}

      gtk_widget_show (GTK_WIDGET (open_options_preview));
      gtk_widget_queue_draw (GTK_WIDGET(open_options_preview));

      show_generate_label = FALSE;
      
      g_free (raw_thumb);
    }
  else
    {
      if (imginfo)
	g_free (imginfo);

      gtk_widget_hide (GTK_WIDGET (open_options_preview));
      gtk_label_set_text (GTK_LABEL (open_options_label),
			  _("No preview available"));
    }

  if (show_generate_label)
    {
      gtk_widget_hide (GTK_WIDGET (open_options_preview));
      gtk_widget_show (GTK_WIDGET (open_options_genbuttonlabel));
    }
  else
    {
      gtk_widget_hide (GTK_WIDGET (open_options_genbuttonlabel));
      gtk_widget_show (GTK_WIDGET (open_options_preview));
    }
}

556 557 558 559 560 561 562 563 564 565 566
static void
selchanged_foreach (GtkTreeModel *model,
		    GtkTreePath  *path,
		    GtkTreeIter  *iter,
		    gpointer      data)
{
  gboolean *selected = data;

  *selected = TRUE;
}

567
static void
568 569
file_open_selchanged_callback (GtkTreeSelection *sel,
			       gpointer          data)
570
{
Michael Natterer's avatar
Michael Natterer committed
571 572 573
  GtkFileSelection *fileload;
  Gimp             *gimp;
  const gchar      *fullfname;
574
  gboolean          selected = FALSE;
Michael Natterer's avatar
Michael Natterer committed
575

576 577 578 579
  gtk_tree_selection_selected_foreach (sel,
				       selchanged_foreach,
				       &selected);
  if (selected)
580 581
    {
      fileload = GTK_FILE_SELECTION (data);
Michael Natterer's avatar
Michael Natterer committed
582

583
      gimp = GIMP (g_object_get_data (G_OBJECT (fileload), "gimp"));
584

585
      fullfname = gtk_file_selection_get_filename (fileload);
586

587 588 589
      gtk_widget_set_sensitive (GTK_WIDGET (open_options_frame), TRUE);
      set_preview (gimp, fullfname, NULL, 0, 0);
    }
590 591 592 593 594 595 596 597 598 599 600
}

static void
file_open_genbutton_callback (GtkWidget *widget,
			      gpointer   data)
{
  GimpImage *gimage_to_be_thumbed;
  guchar    *RGBbuf;
  TempBuf   *tempbuf;
  gint       RGBbuf_w;
  gint       RGBbuf_h;
Michael Natterer's avatar
Michael Natterer committed
601
  Gimp      *gimp;
602 603

  /* added for multi-file preview generation... */
604 605 606 607 608 609 610
  GtkFileSelection  *fs;
  gchar             *full_filename = NULL;
  gchar             *raw_filename = NULL;
  struct stat        buf;
  gint               err;
  gchar            **selections;
  gint               i;
611 612 613

  fs = GTK_FILE_SELECTION (data);

Michael Natterer's avatar
Michael Natterer committed
614 615
  gimp = GIMP (g_object_get_data (G_OBJECT (fs), "gimp"));

616 617
  if (! preview_fullname)
    {
618
      g_warning ("%s: Tried to generate thumbnail for NULL file name.", G_STRLOC);
619 620 621
      return;
    }

Michael Natterer's avatar
Michael Natterer committed
622
  gimp_set_busy (gimp);
623 624 625 626
  gtk_widget_set_sensitive (GTK_WIDGET (fileload), FALSE);

  /* new mult-file preview make: */  

627
  selections = gtk_file_selection_get_selections (fs);
628

629
  for (i = 0; selections[i] != NULL; i++)
630
    {
631
      full_filename = selections[i];
632 633 634 635 636 637 638 639
      
      err = stat (full_filename, &buf);
      
      if (! (err == 0 && (buf.st_mode & S_IFDIR)))
        {
          /* Is not directory. */
          GimpPDBStatusType dummy;
          
640
	  raw_filename = g_path_get_basename (full_filename);
641 642
          gimage_to_be_thumbed = file_open_image (gimp,
                                                  full_filename,
643
                                                  raw_filename,
644 645
                                                  NULL,
                                                  NULL,
646
                                                  GIMP_RUN_NONINTERACTIVE,
647
                                                  &dummy);
648
	  g_free (raw_filename);
649 650 651 652 653 654 655
          
          if (gimage_to_be_thumbed)
            {			
              tempbuf = make_thumb_tempbuf (gimage_to_be_thumbed);
              RGBbuf  = make_RGBbuf_from_tempbuf (tempbuf,
                                                  &RGBbuf_w,
                                                  &RGBbuf_h);
656
              if (gimp->config->write_thumbnails)
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
                {
                  file_save_thumbnail (gimage_to_be_thumbed,
                                       full_filename, tempbuf);
                }
              
              set_preview (gimp, full_filename,
                           RGBbuf, RGBbuf_w, RGBbuf_h);
              
              g_object_unref (G_OBJECT (gimage_to_be_thumbed));
              
              if (RGBbuf)
                g_free (RGBbuf);
            }
          else
            {
              gtk_label_set_text (GTK_LABEL (open_options_label),
673
                                  _("Failed to generate preview."));
674 675 676 677
            }
        }      
     }

678
  g_strfreev (selections);
679 680

  gtk_widget_set_sensitive (GTK_WIDGET (fileload), TRUE);
Michael Natterer's avatar
Michael Natterer committed
681
  gimp_unset_busy (gimp);
682 683 684 685 686 687
}

static void
file_open_ok_callback (GtkWidget *widget,
		       gpointer   data)
{
688 689 690 691 692 693 694 695 696
  GtkFileSelection   *fs;
  Gimp               *gimp;
  gchar              *full_filename;
  gchar              *raw_filename;
  struct stat         buf;
  gint                err;
  GimpPDBStatusType   status;
  gchar             **selections;
  gint                i;
697 698

  fs = GTK_FILE_SELECTION (data);
699

Michael Natterer's avatar
Michael Natterer committed
700 701
  gimp = GIMP (g_object_get_data (G_OBJECT (fs), "gimp"));

702
  selections = gtk_file_selection_get_selections (fs);
703

704
  if (selections == NULL)
705 706
    return;

707 708 709
  full_filename = selections[0];
  raw_filename = g_strdup (gtk_entry_get_text (GTK_ENTRY(fs->selection_entry)));

710 711 712 713 714 715 716 717 718 719 720
  err = stat (full_filename, &buf);

  if (err == 0 && (buf.st_mode & S_IFDIR))
    {
      if (full_filename[strlen (full_filename) - 1] != G_DIR_SEPARATOR)
	{
	  gchar *s = g_strconcat (full_filename, G_DIR_SEPARATOR_S, NULL);
	  gtk_file_selection_set_filename (fs, s);
	  g_free (s);
	}
      else
721 722 723
        {
          gtk_file_selection_set_filename (fs, full_filename);
        }
724

725 726 727
      g_free (raw_filename);
      g_strfreev (selections);

728 729 730 731 732 733 734 735
      return;
    }

  gtk_widget_set_sensitive (GTK_WIDGET (fs), FALSE);

  if (err) /* e.g. http://server/filename.jpg */
    full_filename = raw_filename;

Michael Natterer's avatar
Michael Natterer committed
736 737
  status = file_open_with_proc_and_display (gimp,
                                            full_filename, 
738 739
                                            raw_filename, 
                                            load_file_proc);
740

741
  if (status == GIMP_PDB_SUCCESS)
742 743 744
    {
      file_dialog_hide (data);
    }
745
  else if (status != GIMP_PDB_CANCEL)
746
    {
747 748
      /* Hackery required. Please add error message. --bex */
      g_message (_("Opening '%s' failed."), full_filename);
749 750 751
    }

  /*
752
   * Now deal with multiple selections from the filesel list
753 754
   */

755
  for (i = 1; selections[i] != NULL; i++)
756
    {
757 758 759 760
      full_filename = selections[i];

      g_free (raw_filename);
      raw_filename = g_path_get_basename (full_filename);
761
      
762
      err = stat (full_filename, &buf);
763
          
764 765
      if (! (err == 0 && (buf.st_mode & S_IFDIR)))
	{ /* Is not directory. */
766
              
767 768 769 770
	  status = file_open_with_proc_and_display (gimp,
						    full_filename,
						    raw_filename,
						    load_file_proc);
771
              
772 773 774 775 776 777 778 779 780 781
	  if (status == GIMP_PDB_SUCCESS)
	    {
	      file_dialog_hide (data);
	    }
	  else if (status != GIMP_PDB_CANCEL)
	    {
	      /* same as previous. --bex */
	      g_message (_("Opening '%s' failed."), full_filename);
	    }
	}
782
    }
783

784
  g_free (raw_filename);
785
  g_strfreev (selections);
786 787
    
  gtk_widget_set_sensitive (GTK_WIDGET (fs), TRUE);
788
}