File: meta.c

package info (click to toggle)
gwyddion 2.47-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 38,180 kB
  • ctags: 37,276
  • sloc: ansic: 317,531; sh: 5,122; xml: 3,583; python: 3,067; makefile: 2,694; cpp: 2,550; pascal: 418; perl: 154; ruby: 130; sed: 17
file content (754 lines) | stat: -rw-r--r-- 25,657 bytes parent folder | download
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
231
232
233
234
235
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
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
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
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
/*
 *  @(#) $Id: meta.c 19043 2016-10-08 18:58:43Z yeti-dn $
 *  Copyright (C) 2003-2013 David Necas (Yeti), Petr Klapetek.
 *  E-mail: yeti@gwyddion.net, klapetek@gwyddion.net.
 *
 *  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., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 */

#include "config.h"
#include <string.h>
#include <gtk/gtk.h>
#include <libgwyddion/gwymacros.h>
#include <libgwyddion/gwyutils.h>
#include <libgwyddion/gwycontainer.h>
#include <libprocess/datafield.h>
#include <libgwydgets/gwydatawindow.h>
#include <libgwydgets/gwydgetutils.h>
#include <app/app.h>
#include <app/help.h>
#include <app/data-browser.h>
#include <app/gwymoduleutils.h>

enum {
    META_KEY,
    META_VALUE
};

typedef enum {
    BROWSER_DATA_CHANNEL,
    BROWSER_DATA_VOLUME,
    BROWSER_DATA_XYZ,
} BrowserDataType;

typedef struct {
    GQuark quark;
    gchar *value;
    gboolean isok;
} FixupData;

typedef struct {
    GwyContainer *container;
    GwyContainer *meta;
    gulong changed_id;
    gulong destroy_id;
    GtkWidget *window;
    GtkWidget *treeview;
    GtkWidget *new;
    GtkWidget *delete;
    GtkWidget *close;
    GtkWidget *save;
} MetadataBrowser;

static GtkWidget*       get_metadata_browser          (GwyContainer *data,
                                                       BrowserDataType type,
                                                       gint id);
static MetadataBrowser* metadata_browser_new          (void);
static void             gwy_meta_browser_construct    (MetadataBrowser *browser);
static MetadataBrowser* gwy_meta_switch_data          (MetadataBrowser *browser,
                                                       GwyContainer *data,
                                                       BrowserDataType type,
                                                       gint id);
static void             gwy_meta_cell_edited          (GtkCellRendererText *renderer,
                                                       const gchar *strpath,
                                                       const gchar *text,
                                                       MetadataBrowser *browser);
static void             gwy_meta_browser_cell_renderer(GtkTreeViewColumn *column,
                                                       GtkCellRenderer *cell,
                                                       GtkTreeModel *model,
                                                       GtkTreeIter *piter,
                                                       gpointer data);
static void             gwy_meta_browser_add_line     (gpointer hkey,
                                                       GValue *value,
                                                       GSList **slist);
static void             gwy_meta_item_changed         (GwyContainer *container,
                                                       GQuark quark,
                                                       MetadataBrowser *browser);
static void             gwy_meta_new_item             (MetadataBrowser *browser);
static void             gwy_meta_delete_item          (MetadataBrowser *browser);
static void             gwy_meta_destroy              (MetadataBrowser *browser);
static void             gwy_meta_data_finalized       (MetadataBrowser *browser);
static void             gwy_meta_focus_iter           (MetadataBrowser *browser,
                                                       GtkTreeIter *iter);
static gboolean         gwy_meta_find_key             (MetadataBrowser *browser,
                                                       GQuark quark,
                                                       GtkTreeIter *iter);
static void             gwy_meta_save_items           (MetadataBrowser *browser);

/**
 * gwy_app_metadata_browser_for_channel:
 * @data: A data container.
 * @id: Id of a channel in @data to show metadata for.
 *
 * Shows a simple metadata browser for a channel.
 *
 * If the metadata browser is already shown for this channel it is just raised
 * and given focus.  Otherwise, a new window is created.
 *
 * Returns: The metadata browser (owned by the library).  Usually, you can
 *          ignore the return value.
 *
 * Since: 2.32
 **/
GtkWidget*
gwy_app_metadata_browser_for_channel(GwyContainer *data,
                                     gint id)
{
    return get_metadata_browser(data, BROWSER_DATA_CHANNEL, id);
}

/**
 * gwy_app_metadata_browser_for_volume:
 * @data: A data container.
 * @id: Id of volume data in @data to show metadata for.
 *
 * Shows a simple metadata browser for volume data.
 *
 * If the metadata browser is already shown for this volume data it is just
 * raised and given focus.  Otherwise, a new window is created.
 *
 * Returns: The metadata browser (owned by the library).  Usually, you can
 *          ignore the return value.
 *
 * Since: 2.32
 **/
GtkWidget*
gwy_app_metadata_browser_for_volume(GwyContainer *data,
                                    gint id)
{
    return get_metadata_browser(data, BROWSER_DATA_VOLUME, id);
}

/**
 * gwy_app_metadata_browser_for_xyz:
 * @data: A data container.
 * @id: Id of XYZ data in @data to show metadata for.
 *
 * Shows a simple metadata browser for XYZ data.
 *
 * If the metadata browser is already shown for this XYZ data it is just
 * raised and given focus.  Otherwise, a new window is created.
 *
 * Returns: The metadata browser (owned by the library).  Usually, you can
 *          ignore the return value.
 *
 * Since: 2.45
 **/
GtkWidget*
gwy_app_metadata_browser_for_xyz(GwyContainer *data,
                                 gint id)
{
    return get_metadata_browser(data, BROWSER_DATA_XYZ, id);
}

static GtkWidget*
get_metadata_browser(GwyContainer *data,
                     BrowserDataType type,
                     gint id)
{
    MetadataBrowser *browser;

    g_return_val_if_fail(GWY_IS_CONTAINER(data), NULL);
    g_return_val_if_fail(id >= 0, NULL);
    if ((browser = gwy_meta_switch_data(NULL, data, type, id))) {
        gtk_window_present(GTK_WINDOW(browser->window));
        return browser->window;
    }
    browser = metadata_browser_new();
    gwy_meta_switch_data(browser, data, type, id);
    gtk_widget_show_all(browser->window);
    return browser->window;
}

static MetadataBrowser*
metadata_browser_new(void)
{
    MetadataBrowser *browser;
    GtkWidget *scroll, *vbox, *hbox;
    GtkRequisition request;

    browser = g_new0(MetadataBrowser, 1);
    gwy_meta_browser_construct(browser);
    browser->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gwy_help_add_to_window(GTK_WINDOW(browser->window), "metadata", NULL,
                           GWY_HELP_DEFAULT);

    gtk_widget_size_request(browser->treeview, &request);
    request.width = MAX(request.width, 320);
    request.height = MAX(request.height, 400);
    gtk_window_set_default_size(GTK_WINDOW(browser->window),
                                MIN(request.width + 24, 2*gdk_screen_width()/3),
                                MIN(request.height + 32,
                                    2*gdk_screen_height()/3));
    gwy_app_add_main_accel_group(GTK_WINDOW(browser->window));

    vbox = gtk_vbox_new(FALSE, 0);
    gtk_container_add(GTK_CONTAINER(browser->window), vbox);

    scroll = gtk_scrolled_window_new(NULL, NULL);
    gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),
                                   GTK_POLICY_AUTOMATIC,
                                   GTK_POLICY_AUTOMATIC);
    gtk_box_pack_start(GTK_BOX(vbox), scroll, TRUE, TRUE, 0);
    gtk_container_add(GTK_CONTAINER(scroll), browser->treeview);
    browser->destroy_id
        = g_signal_connect_swapped(browser->window, "destroy",
                                   G_CALLBACK(gwy_meta_destroy), browser);

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

    browser->save = gwy_stock_like_button_new(_("_Export"), GTK_STOCK_SAVE);
    gtk_box_pack_start(GTK_BOX(hbox), browser->save, TRUE, TRUE, 0);
    g_signal_connect_swapped(browser->save, "clicked",
                             G_CALLBACK(gwy_meta_save_items), browser);
    gtk_widget_set_sensitive(browser->save, FALSE);

    browser->new = gwy_stock_like_button_new(_("_New"), GTK_STOCK_NEW);
    gtk_box_pack_start(GTK_BOX(hbox), browser->new, TRUE, TRUE, 0);
    g_signal_connect_swapped(browser->new, "clicked",
                             G_CALLBACK(gwy_meta_new_item), browser);

    browser->delete = gwy_stock_like_button_new(_("_Delete"), GTK_STOCK_DELETE);
    gtk_box_pack_start(GTK_BOX(hbox), browser->delete, TRUE, TRUE, 0);
    g_signal_connect_swapped(browser->delete, "clicked",
                             G_CALLBACK(gwy_meta_delete_item), browser);

    browser->close = gwy_stock_like_button_new(_("_Close"), GTK_STOCK_CLOSE);
    gtk_box_pack_start(GTK_BOX(hbox), browser->close, TRUE, TRUE, 0);
    g_signal_connect_swapped(browser->close, "clicked",
                             G_CALLBACK(gtk_widget_destroy), browser->window);

    return browser;
}

static gint
gwy_meta_sort_func(GtkTreeModel *model,
                   GtkTreeIter *a,
                   GtkTreeIter *b,
                   G_GNUC_UNUSED gpointer userdata)
{
    GQuark qa, qb;

    gtk_tree_model_get(model, a, META_KEY, &qa, -1);
    gtk_tree_model_get(model, b, META_KEY, &qb, -1);
    return g_utf8_collate(g_quark_to_string(qa), g_quark_to_string(qb));
}

static gboolean
gwy_meta_equal_func(GtkTreeModel *model,
                    G_GNUC_UNUSED gint column,
                    const gchar *key,
                    GtkTreeIter *iter,
                    G_GNUC_UNUSED gpointer search_data)
{
    GQuark quark;

    gtk_tree_model_get(model, iter, META_KEY, &quark, -1);
    return !strstr(g_quark_to_string(quark), key);
}

static void
gwy_meta_update_title(MetadataBrowser *browser,
                      GwyContainer *data,
                      BrowserDataType type,
                      gint id)
{
    gchar *title, *dataname;
    GtkTreeModel *model;
    GtkTreeIter iter;

    if (type == BROWSER_DATA_CHANNEL)
        dataname = gwy_app_get_data_field_title(data, id);
    else if (type == BROWSER_DATA_VOLUME)
        dataname = gwy_app_get_brick_title(data, id);
    else if (type == BROWSER_DATA_XYZ)
        dataname = gwy_app_get_surface_title(data, id);
    else {
        g_return_if_reached();
    }

    title = g_strdup_printf(_("Metadata of %s (%s)"),
                            dataname, g_get_application_name());
    gtk_window_set_title(GTK_WINDOW(browser->window), title);
    g_free(title);
    g_free(dataname);

    model = gtk_tree_view_get_model(GTK_TREE_VIEW(browser->treeview));
    if (gtk_tree_model_get_iter_first(model, &iter))
         gtk_widget_set_sensitive(browser->save, TRUE);
    else
         gtk_widget_set_sensitive(browser->save, FALSE);
}

static MetadataBrowser*
gwy_meta_switch_data(MetadataBrowser *browser,
                     GwyContainer *data,
                     BrowserDataType type,
                     gint id)
{
    GwyContainer *meta;
    GtkListStore *store;
    GtkTreeView *treeview;
    GtkTreeIter iter;
    GSList *fixlist, *l;
    GQuark quark;

    if (type == BROWSER_DATA_CHANNEL)
        quark = gwy_app_get_data_meta_key_for_id(id);
    else if (type == BROWSER_DATA_VOLUME)
        quark = gwy_app_get_brick_meta_key_for_id(id);
    else if (type == BROWSER_DATA_XYZ)
        quark = gwy_app_get_surface_meta_key_for_id(id);
    else {
        g_return_val_if_reached(NULL);
    }

    if (gwy_container_gis_object(data, quark, &meta)) {
        if (!browser)
            browser = g_object_get_data(G_OBJECT(meta), "metadata-browser");
        if (!browser || browser->meta == meta)
            return browser;

        fixlist = NULL;
        store = gtk_list_store_new(1, G_TYPE_UINT);
        gwy_container_foreach(meta, NULL,
                              (GHFunc)(gwy_meta_browser_add_line), &fixlist);
        /* Commit UTF-8 fixes found by gwy_meta_browser_add_line() */
        for (l = fixlist; l; l = g_slist_next(l)) {
            FixupData *fd = (FixupData*)l->data;

            if (fd->isok || fd->value) {
                if (!fd->isok)
                    gwy_container_set_string(meta, fd->quark, fd->value);
                gtk_list_store_insert_with_values(store, &iter, G_MAXINT,
                                                  META_KEY, fd->quark,
                                                  -1);
            }
            else
                gwy_container_remove(meta, fd->quark);
            g_free(fd);
        }
        g_slist_free(fixlist);
    }
    else {
        if (!browser)
            return NULL;

        meta = gwy_container_new();
        gwy_container_set_object(data, quark, meta);
        g_object_unref(meta);
        store = gtk_list_store_new(1, G_TYPE_UINT);
    }

    GWY_SIGNAL_HANDLER_DISCONNECT(browser->meta, browser->changed_id);
    if (browser->meta) {
        g_object_set_data(G_OBJECT(browser->meta), "metadata-browser", NULL);
        g_object_weak_unref(G_OBJECT(browser->meta),
                            (GWeakNotify)&gwy_meta_data_finalized,
                            browser);
    }
    browser->meta = meta;
    g_object_set_data(G_OBJECT(browser->meta), "metadata-browser", browser);
    g_object_weak_ref(G_OBJECT(browser->meta),
                      (GWeakNotify)&gwy_meta_data_finalized,
                      browser);

    browser->changed_id = g_signal_connect(meta, "item-changed",
                                           G_CALLBACK(gwy_meta_item_changed),
                                           browser);

    treeview = GTK_TREE_VIEW(browser->treeview);
    gtk_tree_view_set_model(treeview, GTK_TREE_MODEL(store));
    g_object_unref(store);

    /* Search column must set whenver the model changes */
    gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(store), 0,
                                    gwy_meta_sort_func, NULL, NULL);
    gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store), META_KEY,
                                         GTK_SORT_ASCENDING);
    gtk_tree_view_set_search_column(treeview, META_KEY);
    gtk_tree_view_set_search_equal_func(treeview, gwy_meta_equal_func,
                                        browser, NULL);

    gwy_meta_update_title(browser, data, type, id);

    return browser;
}

static void
gwy_meta_browser_construct(MetadataBrowser *browser)
{
    static const struct {
        const gchar *title;
        const guint id;
    }
    columns[] = {
        { N_("Name"),  META_KEY,   },
        { N_("Value"), META_VALUE, },
    };

    GtkTreeView *treeview;
    GtkTreeSelection *selection;
    GtkCellRenderer *renderer;
    GtkTreeViewColumn *column;
    gsize i;

    browser->treeview = gtk_tree_view_new();
    treeview = GTK_TREE_VIEW(browser->treeview);
    gtk_tree_view_set_rules_hint(treeview, TRUE);
    gtk_tree_view_set_enable_search(treeview, TRUE);

    for (i = 0; i < G_N_ELEMENTS(columns); i++) {
        renderer = gtk_cell_renderer_text_new();
        g_object_set(renderer, "editable", TRUE, "editable-set", TRUE, NULL);
        column = gtk_tree_view_column_new_with_attributes(_(columns[i].title),
                                                          renderer, NULL);
        gtk_tree_view_column_set_cell_data_func(column, renderer,
                                                gwy_meta_browser_cell_renderer,
                                                browser, NULL);
        gtk_tree_view_append_column(treeview, column);
        g_object_set_data(G_OBJECT(renderer), "column",
                          GUINT_TO_POINTER(columns[i].id));
        g_signal_connect(renderer, "edited",
                         G_CALLBACK(gwy_meta_cell_edited), browser);
    }

    selection = gtk_tree_view_get_selection(treeview);
    gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE);
}

static gboolean
gwy_meta_validate_key(const gchar *key)
{
    if (!key || !*key)
        return FALSE;

    while (*key) {
        gchar c = *key;

        if (c < ' ' || c == '/' || c == '<' || c == '>' || c == '&' || c == 127)
            return FALSE;
        key++;
    }
    return TRUE;
}

static void
gwy_meta_cell_edited(GtkCellRendererText *renderer,
                     const gchar *strpath,
                     const gchar *text,
                     MetadataBrowser *browser)
{
    GtkTreeModel *model;
    GtkTreePath *path;
    GtkTreeIter iter;
    GQuark oldkey, quark;
    guint col;

    col = GPOINTER_TO_UINT(g_object_get_data(G_OBJECT(renderer), "column"));
    gwy_debug("Column %d edited to <%s> (path %s)", col, text, strpath);

    model = gtk_tree_view_get_model(GTK_TREE_VIEW(browser->treeview));
    path = gtk_tree_path_new_from_string(strpath);
    gtk_tree_model_get_iter(model, &iter, path);
    gtk_tree_path_free(path);

    gtk_tree_model_get(model, &iter, META_KEY, &oldkey, -1);
    switch (col) {
        case META_KEY:
        if (gwy_meta_validate_key(text)) {
            quark = g_quark_from_string(text);
            gwy_container_rename(browser->meta, oldkey, quark, FALSE);
        }
        break;

        case META_VALUE:
        if (pango_parse_markup(text, -1, 0, NULL, NULL, NULL, NULL))
            gwy_container_set_string(browser->meta, oldkey,
                                     g_strdup(text));
        break;

        default:
        g_return_if_reached();
        break;
    }
}

static void
gwy_meta_browser_cell_renderer(G_GNUC_UNUSED GtkTreeViewColumn *column,
                               GtkCellRenderer *renderer,
                               GtkTreeModel *model,
                               GtkTreeIter *iter,
                               gpointer userdata)
{
    MetadataBrowser *browser = (MetadataBrowser*)userdata;
    const gchar *s;
    GQuark quark;
    gulong id;

    id = GPOINTER_TO_UINT(g_object_get_data(G_OBJECT(renderer), "column"));
    gtk_tree_model_get(model, iter, META_KEY, &quark, -1);
    switch (id) {
        case META_KEY:
        g_object_set(renderer, "text", g_quark_to_string(quark), NULL);
        break;

        case META_VALUE:
        s = gwy_container_get_string(browser->meta, quark);
        g_object_set(renderer, "text", s, NULL);
        break;

        default:
        g_return_if_reached();
        break;
    }
}

static void
gwy_meta_browser_add_line(gpointer hkey,
                          GValue *value,
                          GSList **slist)
{
    FixupData *fd;
    GQuark quark;
    const gchar *val;
    gchar *s;

    g_return_if_fail(G_VALUE_HOLDS_STRING(value));
    quark = GPOINTER_TO_UINT(hkey);
    val = g_value_get_string(value);

    /* Theoretically, modules should assure metadata are in UTF-8 when it's
     * stored to container.  But in practice we cannot rely on it. */
    fd = g_new0(FixupData, 1);
    fd->quark = quark;
    if (g_utf8_validate(val, -1, NULL)) {
        fd->value = (gchar*)val;
        fd->isok = TRUE;
    }
    else if ((s = g_convert(val, -1, "UTF-8", "ISO-8859-1", NULL, NULL, NULL)))
        fd->value = s;
    else {
        g_warning("Bogus metadata <%s> at key <%s>",
                  val, g_quark_to_string(quark));
        g_free(fd);
        return;
    }

    /* The same applies to markup validity.  Fix invalid markup by taking it
     * literally. */
    if (!pango_parse_markup(fd->value, -1, 0, NULL, NULL, NULL, NULL)) {
        s = g_markup_escape_text(fd->value, -1);
        if (!fd->isok)
            g_free(fd->value);
        fd->value = s;
        fd->isok = FALSE;
    }

    *slist = g_slist_prepend(*slist, fd);
}

static void
gwy_meta_item_changed(GwyContainer *container,
                      GQuark quark,
                      MetadataBrowser *browser)
{
    GtkListStore *store;
    GtkTreeView *treeview;
    GtkTreeIter iter;
    G_GNUC_UNUSED const gchar *key;

    key = g_quark_to_string(quark);
    gwy_debug("Meta item <%s> changed", key);
    g_return_if_fail(quark);
    treeview = GTK_TREE_VIEW(browser->treeview);
    store = GTK_LIST_STORE(gtk_tree_view_get_model(treeview));

    if (gwy_meta_find_key(browser, quark, &iter)) {
        if (gwy_container_contains(container, quark))
            gwy_list_store_row_changed(store, &iter, NULL, -1);
        else
            gtk_list_store_remove(store, &iter);
        return;
    }

    gtk_list_store_insert_with_values(store, &iter, G_MAXINT, META_KEY, quark,
                                      -1);
    gwy_meta_focus_iter(browser, &iter);
}

static void
gwy_meta_new_item(MetadataBrowser *browser)
{
    static const gchar *whatever[] = {
        "angary", "bistere", "couchant", "dolerite", "envoy", "figwort",
        "gudgeon", "hidalgo", "ictus", "jibbah", "kenosis", "logie",
        "maser", "nephology", "ozalid", "parallax", "reduit", "savate",
        "thyristor", "urate", "versicle", "wapentake", "xystus",
        "yogh", "zeugma",
    };
    GtkTreeIter iter;
    static GQuark quark = 0;
    gchar *s;

    if (!quark) {
        s = g_strdup(_("New item"));
        quark = g_quark_from_string(s);
        g_free(s);
    }

    if (gwy_container_contains(browser->meta, quark)) {
        if (gwy_meta_find_key(browser, quark, &iter))
            gwy_meta_focus_iter(browser, &iter);
    }
    else {
        if (g_random_int() % 4 == 0)
            s = g_strdup(whatever[g_random_int() % G_N_ELEMENTS(whatever)]);
        else
            s = g_strdup("");
        gwy_container_set_string(browser->meta, quark, s);
    }

    gtk_widget_set_sensitive(browser->save, TRUE);
}

static void
gwy_meta_delete_item(MetadataBrowser *browser)
{
    GtkTreeSelection *selection;
    GtkTreeModel *model;
    GtkTreeIter iter;
    GQuark quark;

    selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(browser->treeview));
    if (!gtk_tree_selection_get_selected(selection, &model, &iter))
        return;

    gtk_tree_model_get(model, &iter, META_KEY, &quark, -1);
    gwy_container_remove(browser->meta, quark);

    if (!gtk_tree_model_get_iter_first(model, &iter))
         gtk_widget_set_sensitive(browser->save, FALSE);
}

static void
gwy_meta_destroy(MetadataBrowser *browser)
{
    GWY_SIGNAL_HANDLER_DISCONNECT(browser->meta, browser->changed_id);
    g_object_set_data(G_OBJECT(browser->meta), "metadata-browser", NULL);
    g_object_weak_unref(G_OBJECT(browser->meta),
                        (GWeakNotify)&gwy_meta_data_finalized,
                        browser);
    g_free(browser);
}

static void
gwy_meta_data_finalized(MetadataBrowser *browser)
{
    browser->changed_id = 0;
    g_signal_handler_disconnect(browser->window, browser->destroy_id);
    gtk_widget_destroy(browser->window);
    g_free(browser);
}

static void
gwy_meta_focus_iter(MetadataBrowser *browser,
                    GtkTreeIter *iter)
{
    GtkTreeView *treeview;
    GtkTreeViewColumn *column;
    GtkTreePath *path;

    treeview = GTK_TREE_VIEW(browser->treeview);
    path = gtk_tree_model_get_path(gtk_tree_view_get_model(treeview), iter);
    gtk_tree_view_scroll_to_cell(treeview, path, NULL, FALSE, 0.0, 0.0);
    column = gtk_tree_view_get_column(treeview, META_KEY);
    gtk_tree_view_set_cursor(treeview, path, column, FALSE);
    gtk_widget_grab_focus(browser->treeview);
    gtk_tree_path_free(path);
}

static gboolean
gwy_meta_find_key(MetadataBrowser *browser,
                  GQuark quark,
                  GtkTreeIter *iter)
{
    GtkTreeModel *model;
    GQuark q;

    model = gtk_tree_view_get_model(GTK_TREE_VIEW(browser->treeview));
    if (gtk_tree_model_get_iter_first(model, iter)) {
        do {
            gtk_tree_model_get(model, iter, META_KEY, &q, -1);
            if (q == quark)
                return TRUE;
        } while (gtk_tree_model_iter_next(model, iter));
    }

    return FALSE;
}

static void
gwy_meta_save_items(MetadataBrowser *browser)
{
    GtkTreeModel *model;
    GtkTreeIter iter;
    GString *str_to_save;
    GQuark name;
    const gchar *value;

    str_to_save = g_string_new(NULL);

    model = gtk_tree_view_get_model(GTK_TREE_VIEW(browser->treeview));
    if (gtk_tree_model_get_iter_first(model, &iter)) {
        do {
            gtk_tree_model_get(model, &iter, META_KEY, &name, -1);
            value = gwy_container_get_string(browser->meta, name);
            g_string_append_printf(str_to_save, "%s %s\n",
                                   g_quark_to_string(name),
                                   value);
        } while (gtk_tree_model_iter_next(model, &iter));
    }

    gwy_save_auxiliary_data(_("Save Metadata"),
                            GTK_WINDOW(browser->window),
                            -1,
                            str_to_save->str);

    g_string_free(str_to_save, TRUE);

}

/************************** Documentation ****************************/

/**
 * SECTION:meta
 * @title: meta
 * @short_description: Metadata browser
 **/

/* vim: set cin et ts=4 sw=4 cino=>1s,e0,n0,f0,{0,}0,^0,\:1s,=0,g1s,h0,t0,+1s,c3,(0,u0 : */