interface.c 19.7 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
/* interface.c - user interface for the metadata editor
 *
 * Copyright (C) 2004-2005, Raphaël Quinet <raphael@gimp.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "config.h"

#include <errno.h>
#include <string.h>
#include <stdlib.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
30

31
32
33
34
35
36
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include <glib/gstdio.h>

37
38
#include <glib.h>

39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#ifdef G_OS_WIN32
#include <io.h>
#endif

#ifndef _O_BINARY
#define _O_BINARY 0
#endif

#include <libgimp/gimp.h>
#include <libgimp/gimpui.h>

#include "libgimp/stdplugins-intl.h"

#include "interface.h"
#include "xmp-model.h"
54
#include "xmp-encode.h"
55

56

57
58
59
#define RESPONSE_IMPORT   1
#define RESPONSE_EXPORT   2

60

61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
typedef struct
{
  GtkWidget    *dlg;
  XMPModel     *xmp_model;
  GdkPixbuf    *edit_icon;
  GdkPixbuf    *auto_icon;
  gboolean      run_ok;
} MetadataGui;

static void
value_edited (GtkCellRendererText *cell,
	      const gchar         *path_string,
	      const gchar         *new_text,
	      gpointer             data)
{
76
77
  GtkTreeModel *model = data;
  GtkTreePath  *path  = gtk_tree_path_new_from_string (path_string);
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
  GtkTreeIter   iter;
  gchar        *old_text;

  gtk_tree_model_get_iter (model, &iter, path);
  gtk_tree_model_get (model, &iter, COL_XMP_VALUE, &old_text, -1);
  g_free (old_text);

  /* FIXME: update value[] array */
  /* FIXME: check widget xref and update other widget if not NULL */
  gtk_tree_store_set (GTK_TREE_STORE (model), &iter,
		      COL_XMP_VALUE, new_text,
		      -1);

  gtk_tree_path_free (path);
}

static void
add_view_columns (GtkTreeView *treeview)
{
  gint               col_offset;
  GtkCellRenderer   *renderer;
  GtkTreeViewColumn *column;
  GtkTreeModel      *model = gtk_tree_view_get_model (treeview);

  /* Property Name */
  renderer = gtk_cell_renderer_text_new ();
  g_object_set (renderer, "xalign", 0.0, NULL);
  col_offset =
    gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview),
                                                 -1, _("Property"),
                                                 renderer,
                                                 "text",
                                                 COL_XMP_NAME,
                                                 "weight",
                                                 COL_XMP_WEIGHT,
                                                 "weight-set",
                                                 COL_XMP_WEIGHT_SET,
                                                 NULL);
  column = gtk_tree_view_get_column (GTK_TREE_VIEW (treeview), col_offset - 1);
  gtk_tree_view_column_set_clickable (GTK_TREE_VIEW_COLUMN (column), TRUE);

  /* Icon */
  renderer = gtk_cell_renderer_pixbuf_new ();
  col_offset =
    gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview),
                                                 -1, "",
                                                 renderer,
                                                 "pixbuf",
                                                 COL_XMP_EDIT_ICON,
                                                 "visible",
                                                 COL_XMP_VISIBLE,
                                                 NULL);
  column = gtk_tree_view_get_column (GTK_TREE_VIEW (treeview), col_offset - 1);
  gtk_tree_view_column_set_clickable (GTK_TREE_VIEW_COLUMN (column), TRUE);

  /* Value */
  renderer = gtk_cell_renderer_text_new ();
  g_object_set (renderer, "xalign", 0.0, NULL);

  g_signal_connect (renderer, "edited",
		    G_CALLBACK (value_edited), model);
  col_offset =
    gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview),
                                                 -1, _("Value"),
                                                 renderer,
                                                 "text",
                                                 COL_XMP_VALUE,
                                                 "editable",
                                                 COL_XMP_EDITABLE,
                                                 "visible",
                                                 COL_XMP_VISIBLE,
                                                 NULL);
  column = gtk_tree_view_get_column (GTK_TREE_VIEW (treeview), col_offset - 1);
  gtk_tree_view_column_set_clickable (GTK_TREE_VIEW_COLUMN (column), TRUE);
}

static gboolean
icon_foreach_func (GtkTreeModel *model,
                   GtkTreePath  *path,
                   GtkTreeIter  *iter,
                   gpointer      user_data)
{
  gboolean     editable;
  MetadataGui *mgui = user_data;

  gtk_tree_model_get (model, iter,
                      COL_XMP_EDITABLE, &editable,
                      -1);
  if (editable == XMP_AUTO_UPDATE)
    gtk_tree_store_set (GTK_TREE_STORE (model), iter,
                        COL_XMP_EDIT_ICON, mgui->auto_icon,
                        -1);
  else if (editable == TRUE)
    gtk_tree_store_set (GTK_TREE_STORE (model), iter,
                        COL_XMP_EDIT_ICON, mgui->edit_icon,
                        -1);
  else
    gtk_tree_store_set (GTK_TREE_STORE (model), iter,
                        COL_XMP_EDIT_ICON, NULL,
                        -1);
  return FALSE;
}

static void
update_icons (MetadataGui *mgui)
{
  GtkTreeModel *model;

  /* add the edit icon to the rows that are editable */
  model = xmp_model_get_tree_model (mgui->xmp_model);
  gtk_tree_model_foreach (model, icon_foreach_func, mgui);
}

static void
add_description_tab (GtkWidget    *notebook)
{
  GtkWidget     *frame;
  GtkWidget     *table;
  GtkWidget     *entry;
  GtkWidget     *scrolled_window;
  GtkWidget     *text_view;
  GtkTextBuffer *text_buffer;

  frame = gimp_frame_new (_("Description"));
  gtk_notebook_append_page (GTK_NOTEBOOK (notebook), frame,
			    gtk_label_new (_("Description")));
  gtk_container_set_border_width (GTK_CONTAINER (frame), 10);
  /* gtk_widget_show (frame); */

  table = gtk_table_new (5, 2, FALSE);
  gtk_table_set_col_spacings (GTK_TABLE (table), 6);
  gtk_table_set_row_spacings (GTK_TABLE (table), 6);
  gtk_container_add (GTK_CONTAINER (frame), table);
  /* gtk_widget_show (table); */

  entry = gtk_entry_new ();
  gimp_table_attach_aligned (GTK_TABLE (table), 0, 0,
			     _("Image _Title:"), 0.0, 0.5,
			     entry, 1, FALSE);

  entry = gtk_entry_new ();
  gimp_table_attach_aligned (GTK_TABLE (table), 0, 1,
			     _("_Author:"), 0.0, 0.5,
			     entry, 1, FALSE);

  scrolled_window = gtk_scrolled_window_new (NULL, NULL);
  gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_window),
                                       GTK_SHADOW_IN);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
				  GTK_POLICY_AUTOMATIC,
				  GTK_POLICY_AUTOMATIC);
  text_view = gtk_text_view_new ();
  text_buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view));
231
232
233
234
235
  gtk_text_buffer_set_text (text_buffer,
                            "FIXME:\n"
                            "These widgets are currently disconnected from the XMP model.\n"
                            "Please use the Advanced tab.",
                            -1); /*FIXME*/
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
  gtk_container_add (GTK_CONTAINER (scrolled_window), text_view);
  gimp_table_attach_aligned (GTK_TABLE (table), 0, 2,
			     _("_Description:"), 0.0, 0.5,
			     scrolled_window, 1, FALSE);

  entry = gtk_entry_new ();
  gimp_table_attach_aligned (GTK_TABLE (table), 0, 3,
			     _("Description _Writer:"), 0.0, 0.5,
			     entry, 1, FALSE);

  scrolled_window = gtk_scrolled_window_new (NULL, NULL);
  gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_window),
                                       GTK_SHADOW_IN);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
				  GTK_POLICY_AUTOMATIC,
				  GTK_POLICY_AUTOMATIC);
  text_view = gtk_text_view_new ();
  text_buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view));
  gtk_container_add (GTK_CONTAINER (scrolled_window), text_view);
  gimp_table_attach_aligned (GTK_TABLE (table), 0, 4,
			     _("_Keywords:"), 0.0, 0.5,
			     scrolled_window, 1, FALSE);

  gtk_widget_show_all (frame);
}

static void
263
add_copyright_tab (GtkWidget *notebook)
264
{
265
  GtkWidget *label;
266
267
268
269
270
271
272
273

  label = gtk_label_new (_("Empty"));
  gtk_notebook_append_page (GTK_NOTEBOOK (notebook), label,
			    gtk_label_new (_("Copyright")));
  gtk_widget_show (label);
}

static void
274
add_origin_tab (GtkWidget *notebook)
275
{
276
  GtkWidget *label;
277
278
279
280
281
282
283
284

  label = gtk_label_new (_("Empty"));
  gtk_notebook_append_page (GTK_NOTEBOOK (notebook), label,
			    gtk_label_new (_("Origin")));
  gtk_widget_show (label);
}

static void
285
add_camera1_tab (GtkWidget *notebook)
286
{
287
  GtkWidget *label;
288
289
290
291
292
293
294
295

  label = gtk_label_new (_("Empty"));
  gtk_notebook_append_page (GTK_NOTEBOOK (notebook), label,
			    gtk_label_new (_("Camera 1")));
  gtk_widget_show (label);
}

static void
296
add_camera2_tab (GtkWidget *notebook)
297
{
298
  GtkWidget *label;
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
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367

  label = gtk_label_new (_("Empty"));
  gtk_notebook_append_page (GTK_NOTEBOOK (notebook), label,
			    gtk_label_new (_("Camera 2")));
  gtk_widget_show (label);
}

static void
add_advanced_tab (GtkWidget    *notebook,
		  GtkTreeModel *model)
{
  GtkWidget *sw;
  GtkWidget *treeview;

  /* Advanced tab */
  sw = gtk_scrolled_window_new (NULL, NULL);
  gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw),
				       GTK_SHADOW_ETCHED_IN);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
				  GTK_POLICY_AUTOMATIC,
				  GTK_POLICY_AUTOMATIC);
  gtk_notebook_append_page (GTK_NOTEBOOK (notebook), sw,
			    gtk_label_new (_("Advanced")));

  /* create tree view - model will be unref'ed in xmp_model_free() */
  treeview = gtk_tree_view_new_with_model (model);
  gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (treeview), TRUE);

  add_view_columns (GTK_TREE_VIEW (treeview));

  gtk_container_add (GTK_CONTAINER (sw), treeview);

  /* expand all rows after the treeview widget has been realized */
  g_signal_connect (treeview, "realize",
		    G_CALLBACK (gtk_tree_view_expand_all), NULL);
  gtk_widget_show (treeview);
  gtk_widget_show (sw);
}

/* show a transient message dialog */
static void
metadata_message_dialog (GtkMessageType  type,
                         GtkWindow      *parent,
                         const gchar    *title,
                         const gchar    *message)
{
  GtkWidget *dlg;

  dlg = gtk_message_dialog_new (parent, 0, type, GTK_BUTTONS_OK, message);

  if (title)
    gtk_window_set_title (GTK_WINDOW (dlg), title);

  gtk_window_set_role (GTK_WINDOW (dlg), "metadata-message");
  gtk_dialog_run (GTK_DIALOG (dlg));
  gtk_widget_destroy (dlg);
}

/* load XMP metadata from a file (the file may contain other data) */
static void
import_dialog_response (GtkWidget *dlg,
                        gint       response_id,
                        gpointer   data)
{
  MetadataGui *mgui = data;

  g_return_if_fail (mgui != NULL);
  if (response_id == GTK_RESPONSE_OK)
    {
368
369
      gchar  *filename;
      gchar  *buffer;
370
      gssize   buffer_length;
371
      GError *error = NULL;
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
410
411

      filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dlg));

      if (! g_file_get_contents (filename, &buffer, &buffer_length, &error))
        {
          metadata_message_dialog (GTK_MESSAGE_ERROR, GTK_WINDOW (dlg),
                                   _("Open failed"), error->message);
          g_error_free (error);
          g_free (filename);
          return;
        }

      if (! xmp_model_parse_buffer (mgui->xmp_model, buffer, buffer_length,
                                    TRUE, &error))
        {
          metadata_message_dialog (GTK_MESSAGE_ERROR, GTK_WINDOW (dlg),
                                   _("Open failed"), error->message);
          g_error_free (error);
          g_free (filename);
          return;
        }

      update_icons (mgui);

      g_free (buffer);
      g_free (filename);
    }

  gtk_widget_destroy (dlg); /* FIXME: destroy or unmap? */
}

/* select file to import */
static void
file_import_dialog (GtkWidget   *parent,
                    MetadataGui *mgui)
{
  static GtkWidget *dlg = NULL;

  if (! dlg)
    {
412
413
414
      dlg = gtk_file_chooser_dialog_new (_("Import XMP from file"),
                                         GTK_WINDOW (parent),
                                         GTK_FILE_CHOOSER_ACTION_OPEN,
415

416
417
                                         GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                                         GTK_STOCK_OPEN,   GTK_RESPONSE_OK,
418

419
420
                                         NULL);

421
      gtk_dialog_set_alternative_button_order (GTK_DIALOG (dlg),
422
423
424
                                               GTK_RESPONSE_OK,
                                               GTK_RESPONSE_CANCEL,
                                               -1);
425
426
427
428
429
430
431
432
433
434
435

      /* FIXME: gimp_help_connect? */
      gtk_dialog_set_default_response (GTK_DIALOG (dlg), GTK_RESPONSE_OK);

      g_signal_connect (dlg, "destroy",
                        G_CALLBACK (gtk_widget_destroyed),
                        &dlg);
      g_signal_connect (dlg, "response",
                        G_CALLBACK (import_dialog_response),
                        mgui);
    }
436

437
438
439
440
441
442
443
444
445
446
447
448
  gtk_window_present (GTK_WINDOW (dlg));
}

/* save XMP metadata to a file (only XMP, nothing else) */
static void
export_dialog_response (GtkWidget *dlg,
                        gint       response_id,
                        gpointer   data)
{
  MetadataGui *mgui = data;

  g_return_if_fail (mgui != NULL);
449

450
451
  if (response_id == GTK_RESPONSE_OK)
    {
452
453
454
      gchar  *filename;
      gchar  *buffer;
      gssize  buffer_length;
455
      int    fd;
456
457
458
459
460
461
462

      filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dlg));

      /* FIXME: improve this code and rewrite the error handling */
      buffer_length = xmp_estimate_size (mgui->xmp_model);
      buffer = g_new (gchar, buffer_length);
      xmp_generate_block (mgui->xmp_model, buffer, buffer_length);
463

464
465
466
467
468
469
470
471
472
473
      fd = g_open (filename, O_CREAT | O_TRUNC | O_WRONLY | _O_BINARY, 0666);
      if (fd < 0)
        {
          metadata_message_dialog (GTK_MESSAGE_ERROR, GTK_WINDOW (dlg),
                                   _("Open failed"),
                                   _("Cannot create file"));
          g_free (buffer);
          g_free (filename);
          return;
        }
474

475
      /*
476
477
      fprintf (stderr, "\nwriting %d bytes to %s...\n",
               strlen (buffer), filename);
478
      */
479

480
481
482
483
484
485
486
487
488
      if (write (fd, buffer, strlen (buffer)) < 0)
        {
          metadata_message_dialog (GTK_MESSAGE_ERROR, GTK_WINDOW (dlg),
                                   _("Save failed"),
                                   _("Some error occured while saving"));
          g_free (buffer);
          g_free (filename);
          return;
        }
489

490
491
492
493
494
495
496
497
498
      if (close (fd) < 0)
        {
          metadata_message_dialog (GTK_MESSAGE_ERROR, GTK_WINDOW (dlg),
                                   _("Save failed"),
                                   _("Could not close the file"));
          g_free (buffer);
          g_free (filename);
          return;
        }
499

500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
      g_free (buffer);
      g_free (filename);
    }

  gtk_widget_destroy (dlg); /* FIXME: destroy or unmap? */
}

/* select file to export */
static void
file_export_dialog (GtkWidget   *parent,
                    MetadataGui *mgui)
{
  static GtkWidget *dlg = NULL;

  if (! dlg)
    {
516
517
518
      dlg = gtk_file_chooser_dialog_new (_("Export XMP to file"),
                                         GTK_WINDOW (parent),
                                         GTK_FILE_CHOOSER_ACTION_SAVE,
519

520
521
                                         GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                                         GTK_STOCK_OPEN,   GTK_RESPONSE_OK,
522

523
524
                                         NULL);

525
      gtk_dialog_set_alternative_button_order (GTK_DIALOG (dlg),
526
527
528
                                               GTK_RESPONSE_OK,
                                               GTK_RESPONSE_CANCEL,
                                               -1);
529
530
531
532
533
534
535
536
537
538
539

      /* FIXME: gimp_help_connect? */
      gtk_dialog_set_default_response (GTK_DIALOG (dlg), GTK_RESPONSE_OK);

      g_signal_connect (dlg, "destroy",
                        G_CALLBACK (gtk_widget_destroyed),
                        &dlg);
      g_signal_connect (dlg, "response",
                        G_CALLBACK (export_dialog_response),
                        mgui);
    }
540

541
542
543
544
545
546
547
548
549
550
551
  gtk_window_present (GTK_WINDOW (dlg));
}

static void
metadata_dialog_response (GtkWidget *widget,
                          gint       response_id,
                          gpointer   data)
{
  MetadataGui *mgui = data;

  g_return_if_fail (mgui != NULL);
552

553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
  switch (response_id)
    {
    case RESPONSE_IMPORT:
      file_import_dialog (widget, mgui);
      break;

    case RESPONSE_EXPORT:
      file_export_dialog (widget, mgui);
      break;

    case GTK_RESPONSE_OK:
      mgui->run_ok = TRUE;
      /*fallthrough*/

    default:
      gtk_widget_destroy (widget);
      break;
    }
}

gboolean
metadata_dialog (gint32    image_ID,
                 XMPModel *xmp_model)
{
577
578
  MetadataGui  mgui;
  GtkWidget   *notebook;
579
580

  gimp_ui_init ("metadata", FALSE);
581

582
583
584
585
586
587
588
589
590
591
592
  mgui.dlg = gimp_dialog_new (_("Image Properties"), "metadata",
                              NULL, 0,
                              gimp_standard_help_func, "plug-in-metadata",

                              _("_Import XMP"), RESPONSE_IMPORT,
                              _("_Export XMP"), RESPONSE_EXPORT,
                              GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                              GTK_STOCK_OK,     GTK_RESPONSE_OK,

                              NULL);

593
  gtk_dialog_set_alternative_button_order (GTK_DIALOG (mgui.dlg),
594
595
596
597
598
599
                                           RESPONSE_IMPORT,
                                           RESPONSE_EXPORT,
                                           GTK_RESPONSE_OK,
                                           GTK_RESPONSE_CANCEL,
                                           -1);

600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
  g_signal_connect (mgui.dlg, "response",
                    G_CALLBACK (metadata_dialog_response),
                    &mgui);
  g_signal_connect (mgui.dlg, "destroy",
                    G_CALLBACK (gtk_main_quit),
                    NULL);

  notebook = gtk_notebook_new ();
  gtk_notebook_set_tab_pos (GTK_NOTEBOOK (notebook), GTK_POS_TOP);
  gtk_box_pack_start (GTK_BOX (GTK_DIALOG (mgui.dlg)->vbox), notebook,
		      TRUE, TRUE, 0);
  gtk_widget_show (notebook);

  mgui.xmp_model = xmp_model;
  mgui.edit_icon = gtk_widget_render_icon (mgui.dlg, GIMP_STOCK_EDIT,
                                           GTK_ICON_SIZE_MENU, NULL);
  mgui.auto_icon = gtk_widget_render_icon (mgui.dlg, GIMP_STOCK_WILBER,
                                           GTK_ICON_SIZE_MENU, NULL);
  update_icons (&mgui);
619

620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
  mgui.run_ok = FALSE;

  /* add the tabs to the notebook */
  add_description_tab (notebook);
  add_copyright_tab (notebook);
  add_origin_tab (notebook);
  add_camera1_tab (notebook);
  add_camera2_tab (notebook);
  add_advanced_tab (notebook, xmp_model_get_tree_model (mgui.xmp_model));

  gtk_window_set_default_size (GTK_WINDOW (mgui.dlg), 400, 500);
  gtk_widget_show (mgui.dlg);

  /* run, baby, run! */
  gtk_main ();

  /* clean up and return */
  g_object_unref (mgui.auto_icon);
  g_object_unref (mgui.edit_icon);
639

640
641
  return mgui.run_ok;
}