9da97a986f7150922b7f20f9808afd40b4ce46c3
[geeqie.git] / src / bar_info.c
1 /*
2  * Geeqie
3  * (C) 2004 John Ellis
4  * Copyright (C) 2008 The Geeqie Team
5  *
6  * Author: John Ellis
7  *
8  * This software is released under the GNU General Public License (GNU GPL).
9  * Please read the included file COPYING for more information.
10  * This software comes with no warranty of any kind, use at your own risk!
11  */
12
13
14 #include "main.h"
15 #include "exif.h"
16
17 #include "bar_info.h"
18
19 #include "cache.h"
20 #include "filedata.h"
21 #include "history_list.h"
22 #include "info.h"
23 #include "secure_save.h"
24 #include "utilops.h"
25 #include "ui_fileops.h"
26 #include "ui_misc.h"
27 #include "ui_utildlg.h"
28
29
30 #define BAR_KEYWORD_AUTOSAVE_TIME 10000
31
32
33 static const gchar *keyword_favorite_defaults[] = {
34         N_("Favorite"),
35         N_("Todo"),
36         N_("People"),
37         N_("Places"),
38         N_("Art"),
39         N_("Nature"),
40         N_("Possessions"),
41         NULL
42 };
43
44
45 static void bar_info_keyword_update_all(void);
46
47
48 /*
49  *-------------------------------------------------------------------
50  * keyword / comment utils
51  *-------------------------------------------------------------------
52  */
53
54 static gint comment_file_write(gchar *path, GList *keywords, const gchar *comment)
55 {
56         SecureSaveInfo *ssi;
57
58         ssi = secure_open(path);
59         if (!ssi) return FALSE;
60
61         secure_fprintf(ssi, "#%s comment (%s)\n\n", GQ_APPNAME, VERSION);
62
63         secure_fprintf(ssi, "[keywords]\n");
64         while (keywords && secsave_errno == SS_ERR_NONE)
65                 {
66                 const gchar *word = keywords->data;
67                 keywords = keywords->next;
68
69                 secure_fprintf(ssi, "%s\n", word);
70                 }
71         secure_fputc(ssi, '\n');
72
73         secure_fprintf(ssi, "[comment]\n");
74         secure_fprintf(ssi, "%s\n", (comment) ? comment : "");
75
76         secure_fprintf(ssi, "#end\n");
77
78         return (secure_close(ssi) == 0);
79 }
80
81 static gint comment_legacy_write(FileData *fd, GList *keywords, const gchar *comment)
82 {
83         gchar *comment_path;
84         gint success = FALSE;
85
86         /* If an existing metadata file exists, we will try writing to
87          * it's location regardless of the user's preference.
88          */
89         comment_path = cache_find_location(CACHE_TYPE_METADATA, fd->path);
90         if (comment_path && !access_file(comment_path, W_OK))
91                 {
92                 g_free(comment_path);
93                 comment_path = NULL;
94                 }
95
96         if (!comment_path)
97                 {
98                 gchar *comment_dir;
99                 mode_t mode = 0755;
100
101                 comment_dir = cache_get_location(CACHE_TYPE_METADATA, fd->path, FALSE, &mode);
102                 if (cache_ensure_dir_exists(comment_dir, mode))
103                         {
104                         gchar *filename = g_strconcat(fd->name, GQ_CACHE_EXT_METADATA, NULL);
105                         
106                         comment_path = g_build_filename(comment_dir, filename, NULL);
107                         g_free(filename);
108                         }
109                 g_free(comment_dir);
110                 }
111
112         if (comment_path)
113                 {
114                 gchar *comment_pathl;
115
116                 DEBUG_1("Saving comment: %s", comment_path);
117
118                 comment_pathl = path_from_utf8(comment_path);
119
120                 success = comment_file_write(comment_pathl, keywords, comment);
121
122                 g_free(comment_pathl);
123                 g_free(comment_path);
124                 }
125
126         return success;
127 }
128
129 typedef enum {
130         MK_NONE,
131         MK_KEYWORDS,
132         MK_COMMENT
133 } MetadataKey;
134
135 static gint comment_file_read(gchar *path, GList **keywords, gchar **comment)
136 {
137         FILE *f;
138         gchar s_buf[1024];
139         MetadataKey key = MK_NONE;
140         GList *list = NULL;
141         GString *comment_build = NULL;
142
143         f = fopen(path, "r");
144         if (!f) return FALSE;
145
146         while (fgets(s_buf, sizeof(s_buf), f))
147                 {
148                 gchar *ptr = s_buf;
149
150                 if (*ptr == '#') continue;
151                 if (*ptr == '[')
152                         {
153                         gchar *keystr = ++ptr;
154                         
155                         key = MK_NONE;
156                         while (*ptr != ']' && *ptr != '\n' && *ptr != '\0') ptr++;
157                         
158                         if (*ptr == ']')
159                                 {
160                                 *ptr = '\0';
161                                 if (g_ascii_strcasecmp(keystr, "keywords") == 0)
162                                         key = MK_KEYWORDS;
163                                 else if (g_ascii_strcasecmp(keystr, "comment") == 0)
164                                         key = MK_COMMENT;
165                                 }
166                         continue;
167                         }
168                 
169                 switch(key)
170                         {
171                         case MK_NONE:
172                                 break;
173                         case MK_KEYWORDS:
174                                 {
175                                 while (*ptr != '\n' && *ptr != '\0') ptr++;
176                                 *ptr = '\0';
177                                 if (strlen(s_buf) > 0)
178                                         {
179                                         gchar *kw = utf8_validate_or_convert(s_buf);
180
181                                         list = g_list_prepend(list, kw);
182                                         }
183                                 }
184                                 break;
185                         case MK_COMMENT:
186                                 if (!comment_build) comment_build = g_string_new("");
187                                 g_string_append(comment_build, s_buf);
188                                 break;
189                         }
190                 }
191         
192         fclose(f);
193
194         *keywords = g_list_reverse(list);
195         if (comment_build)
196                 {
197                 if (comment)
198                         {
199                         gint len;
200                         gchar *ptr = comment_build->str;
201
202                         /* strip leading and trailing newlines */
203                         while (*ptr == '\n') ptr++;
204                         len = strlen(ptr);
205                         while (len > 0 && ptr[len - 1] == '\n') len--;
206                         if (ptr[len] == '\n') len++; /* keep the last one */
207                         if (len > 0)
208                                 {
209                                 gchar *text = g_strndup(ptr, len);
210
211                                 *comment = utf8_validate_or_convert(text);
212                                 g_free(text);
213                                 }
214                         }
215                 g_string_free(comment_build, TRUE);
216                 }
217
218         return TRUE;
219 }
220
221 static gint comment_delete_legacy(FileData *fd)
222 {
223         gchar *comment_path;
224         gchar *comment_pathl;
225         gint success = FALSE;
226         if (!fd) return FALSE;
227
228         comment_path = cache_find_location(CACHE_TYPE_METADATA, fd->path);
229         if (!comment_path) return FALSE;
230
231         comment_pathl = path_from_utf8(comment_path);
232
233         success = !unlink(comment_pathl);
234
235         g_free(comment_pathl);
236         g_free(comment_path);
237
238         return success;
239 }
240
241 static gint comment_legacy_read(FileData *fd, GList **keywords, gchar **comment)
242 {
243         gchar *comment_path;
244         gchar *comment_pathl;
245         gint success = FALSE;
246         if (!fd) return FALSE;
247
248         comment_path = cache_find_location(CACHE_TYPE_METADATA, fd->path);
249         if (!comment_path) return FALSE;
250
251         comment_pathl = path_from_utf8(comment_path);
252
253         success = comment_file_read(comment_pathl, keywords, comment);
254
255         g_free(comment_pathl);
256         g_free(comment_path);
257
258         return success;
259 }
260
261 static GList *remove_duplicate_strings_from_list(GList *list)
262 {
263         GList *work = list;
264         GHashTable *hashtable = g_hash_table_new(g_str_hash, g_str_equal);
265         GList *newlist = NULL;
266
267         while (work)
268                 {
269                 gchar *key = work->data;
270
271                 if (g_hash_table_lookup(hashtable, key) == NULL)
272                         {
273                         g_hash_table_insert(hashtable, (gpointer) key, GINT_TO_POINTER(1));
274                         newlist = g_list_prepend(newlist, key);
275                         }
276                 work = work->next;
277                 }
278
279         g_hash_table_destroy(hashtable);
280         g_list_free(list);
281
282         return g_list_reverse(newlist);
283 }
284
285 #define COMMENT_KEY "Xmp.dc.description"
286 #define KEYWORD_KEY "Xmp.dc.subject"
287
288 static gint comment_xmp_read(FileData *fd, GList **keywords, gchar **comment)
289 {
290         ExifData *exif;
291
292         exif = exif_read_fd(fd);
293         if (!exif) return FALSE;
294
295         if (comment)
296                 {
297                 gchar *text;
298                 ExifItem *item = exif_get_item(exif, COMMENT_KEY);
299
300                 text = exif_item_get_string(item, 0);
301                 *comment = utf8_validate_or_convert(text);
302                 g_free(text);
303                 }
304
305         if (keywords)
306                 {
307                 ExifItem *item;
308                 guint i;
309                 
310                 *keywords = NULL;
311                 item = exif_get_item(exif, KEYWORD_KEY);
312                 for (i = 0; i < exif_item_get_elements(item); i++)
313                         {
314                         gchar *kw = exif_item_get_string(item, i);
315                         gchar *utf8_kw;
316
317                         if (!kw) break;
318
319                         utf8_kw = utf8_validate_or_convert(kw);
320                         *keywords = g_list_append(*keywords, (gpointer) utf8_kw);
321                         g_free(kw);
322                         }
323
324                 /* FIXME:
325                  * Exiv2 handles Iptc keywords as multiple entries with the
326                  * same key, thus exif_get_item returns only the first keyword
327                  * and the only way to get all keywords is to iterate through
328                  * the item list.
329                  */
330                 for (item = exif_get_first_item(exif);
331                      item;
332                      item = exif_get_next_item(exif))
333                         {
334                         guint tag;
335                 
336                         tag = exif_item_get_tag_id(item);
337                         if (tag == 0x0019)
338                                 {
339                                 gchar *tag_name = exif_item_get_tag_name(item);
340
341                                 if (strcmp(tag_name, "Iptc.Application2.Keywords") == 0)
342                                         {
343                                         gchar *kw;
344                                         gchar *utf8_kw;
345
346                                         kw = exif_item_get_data_as_text(item);
347                                         if (!kw) continue;
348
349                                         utf8_kw = utf8_validate_or_convert(kw);
350                                         *keywords = g_list_append(*keywords, (gpointer) utf8_kw);
351                                         g_free(kw);
352                                         }
353                                 g_free(tag_name);
354                                 }
355                         }
356                 }
357
358         exif_free_fd(fd, exif);
359
360         return (comment && *comment) || (keywords && *keywords);
361 }
362
363 static gint comment_xmp_write(FileData *fd, GList *keywords, const gchar *comment)
364 {
365         gint success;
366         gint write_comment = (comment && comment[0]);
367         ExifData *exif;
368         ExifItem *item;
369
370         exif = exif_read_fd(fd);
371         if (!exif) return FALSE;
372
373         item = exif_get_item(exif, COMMENT_KEY);
374         if (item && !write_comment)
375                 {
376                 exif_item_delete(exif, item);
377                 item = NULL;
378                 }
379
380         if (!item && write_comment) item = exif_add_item(exif, COMMENT_KEY);
381         if (item) exif_item_set_string(item, comment);
382
383         while ((item = exif_get_item(exif, KEYWORD_KEY)))
384                 {
385                 exif_item_delete(exif, item);
386                 }
387
388         if (keywords)
389                 {
390                 GList *work;
391
392                 item = exif_add_item(exif, KEYWORD_KEY);
393
394                 work = keywords;
395                 while (work)
396                         {
397                         exif_item_set_string(item, (gchar *) work->data);
398                         work = work->next;
399                         }
400                 }
401
402         success = exif_write(exif);
403
404         exif_free_fd(fd, exif);
405
406         return success;
407 }
408
409 gint comment_write(FileData *fd, GList *keywords, const gchar *comment)
410 {
411         if (!fd) return FALSE;
412
413         if (options->save_metadata_in_image_file &&
414             comment_xmp_write(fd, keywords, comment))
415                 {
416                 comment_delete_legacy(fd);
417                 return TRUE;
418                 }
419
420         return comment_legacy_write(fd, keywords, comment);
421 }
422
423 gint comment_read(FileData *fd, GList **keywords, gchar **comment)
424 {
425         GList *keywords1 = NULL;
426         GList *keywords2 = NULL;
427         gchar *comment1 = NULL;
428         gchar *comment2 = NULL;
429         gint res1, res2;
430
431         if (!fd) return FALSE;
432
433         res1 = comment_xmp_read(fd, &keywords1, &comment1);
434         res2 = comment_legacy_read(fd, &keywords2, &comment2);
435
436         if (!res1 && !res2)
437                 {
438                 return FALSE;
439                 }
440
441         if (keywords)
442                 {
443                 if (res1 && res2)
444                         *keywords = g_list_concat(keywords1, keywords2);
445                 else
446                         *keywords = res1 ? keywords1 : keywords2;
447
448                 *keywords = remove_duplicate_strings_from_list(*keywords);
449                 }
450         else
451                 {
452                 if (res1) string_list_free(keywords1);
453                 if (res2) string_list_free(keywords2);
454                 }
455
456
457         if (comment)
458                 {
459                 if (res1 && res2 && comment1 && comment2 && comment1[0] && comment2[0])
460                         *comment = g_strdup_printf("%s\n%s", comment1, comment2);
461                 else
462                         *comment = res1 ? comment1 : comment2;
463                 }
464         if (res1 && (!comment || *comment != comment1)) g_free(comment1);
465         if (res2 && (!comment || *comment != comment2)) g_free(comment2);
466
467         return TRUE;
468 }
469
470
471 static gchar *comment_pull(GtkWidget *textview)
472 {
473         GtkTextBuffer *buffer;
474         GtkTextIter start, end;
475
476         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview));
477         gtk_text_buffer_get_bounds(buffer, &start, &end);
478
479         return gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
480 }
481
482 static gint keyword_list_find(GList *list, const gchar *keyword)
483 {
484         while (list)
485                 {
486                 gchar *haystack = list->data;
487
488                 if (haystack && keyword && strcmp(haystack, keyword) == 0) return TRUE;
489
490                 list = list->next;
491                 }
492
493         return FALSE;
494 }
495
496 GList *keyword_list_pull(GtkWidget *text_widget)
497 {
498         GList *list = NULL;
499         gchar *text;
500         gchar *ptr;
501
502         if (GTK_IS_TEXT_VIEW(text_widget))
503                 {
504                 text = comment_pull(text_widget);
505                 }
506         else if (GTK_IS_ENTRY(text_widget))
507                 {
508                 text = g_strdup(gtk_entry_get_text(GTK_ENTRY(text_widget)));
509                 }
510         else
511                 {
512                 return NULL;
513                 }
514
515         ptr = text;
516         while (*ptr != '\0')
517                 {
518                 gchar *begin;
519                 gint l = 0;
520
521 #define KEYWORDS_SEPARATOR(c) ((c) == ',' || (c) == ';' || (c) == '\n' || (c) == '\r' || (c) == '\b')
522                 while (KEYWORDS_SEPARATOR(*ptr)) ptr++;
523                 begin = ptr;
524                 while (*ptr != '\0' && !KEYWORDS_SEPARATOR(*ptr))
525                         {
526                         ptr++;
527                         l++;
528                         }
529
530                 /* trim starting and ending whitespaces */
531                 while (l > 0 && g_ascii_isspace(*begin)) begin++, l--;
532                 while (l > 0 && g_ascii_isspace(begin[l-1])) l--;
533
534                 if (l > 0)
535                         {
536                         gchar *keyword = g_strndup(begin, l);
537
538                         /* only add if not already in the list */
539                         if (keyword_list_find(list, keyword) == FALSE)
540                                 list = g_list_append(list, keyword);
541                         else
542                                 g_free(keyword);
543                         }
544                 }
545
546         g_free(text);
547
548         return list;
549 }
550
551 void keyword_list_push(GtkWidget *textview, GList *list)
552 {
553         GtkTextBuffer *buffer;
554         GtkTextIter start, end;
555
556         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview));
557         gtk_text_buffer_get_bounds(buffer, &start, &end);
558         gtk_text_buffer_delete(buffer, &start, &end);
559
560         while (list)
561                 {
562                 const gchar *word = list->data;
563                 GtkTextIter iter;
564
565                 gtk_text_buffer_get_end_iter(buffer, &iter);
566                 if (word) gtk_text_buffer_insert(buffer, &iter, word, -1);
567                 gtk_text_buffer_get_end_iter(buffer, &iter);
568                 gtk_text_buffer_insert(buffer, &iter, "\n", -1);
569
570                 list = list->next;
571                 }
572 }
573
574 static void metadata_set_keywords(FileData *fd, GList *keywords_to_use, gchar *comment_to_use, gint add)
575 {
576         gchar *comment = NULL;
577         GList *keywords = NULL;
578         GList *save_list = NULL;
579
580         comment_read(fd, &keywords, &comment);
581         
582         if (comment_to_use)
583                 {
584                 if (add && comment && *comment)
585                         {
586                         gchar *tmp = comment;
587                                 
588                         comment = g_strconcat(tmp, comment_to_use, NULL);
589                         g_free(tmp);
590                         }
591                 else
592                         {
593                         g_free(comment);
594                         comment = g_strdup(comment_to_use);
595                         }
596                 }
597         
598         if (keywords_to_use)
599                 {
600                 if (add && keywords && g_list_length(keywords) > 0)
601                         {
602                         GList *work;
603
604                         work = keywords_to_use;
605                         while (work)
606                                 {
607                                 gchar *key;
608                                 GList *p;
609
610                                 key = work->data;
611                                 work = work->next;
612
613                                 p = keywords;
614                                 while (p && key)
615                                         {
616                                         gchar *needle = p->data;
617                                         p = p->next;
618
619                                         if (strcmp(needle, key) == 0) key = NULL;
620                                         }
621
622                                 if (key) keywords = g_list_append(keywords, g_strdup(key));
623                                 }
624                         save_list = keywords;
625                         }
626                 else
627                         {
628                         save_list = keywords_to_use;
629                         }
630                 }
631         
632         comment_write(fd, save_list, comment);
633
634         string_list_free(keywords);
635         g_free(comment);
636 }
637
638 /*
639  *-------------------------------------------------------------------
640  * keyword list dialog
641  *-------------------------------------------------------------------
642  */
643
644 #define KEYWORD_DIALOG_WIDTH  200
645 #define KEYWORD_DIALOG_HEIGHT 250
646
647 typedef struct _KeywordDlg KeywordDlg;
648 struct _KeywordDlg
649 {
650         GenericDialog *gd;
651         GtkWidget *treeview;
652 };
653
654 static KeywordDlg *keyword_dialog = NULL;
655
656
657 static void keyword_dialog_cancel_cb(GenericDialog *gd, gpointer data)
658 {
659         g_free(keyword_dialog);
660         keyword_dialog = NULL;
661 }
662
663 static void keyword_dialog_ok_cb(GenericDialog *gd, gpointer data)
664 {
665         KeywordDlg *kd = data;
666         GtkTreeModel *store;
667         GtkTreeIter iter;
668         gint valid;
669
670         history_list_free_key("keywords");
671
672         store = gtk_tree_view_get_model(GTK_TREE_VIEW(kd->treeview));
673         valid = gtk_tree_model_get_iter_first(store, &iter);
674         while (valid)
675                 {
676                 gchar *key;
677
678                 gtk_tree_model_get(store, &iter, 0, &key, -1);
679                 valid = gtk_tree_model_iter_next(store, &iter);
680
681                 history_list_add_to_key("keywords", key, 0);
682                 }
683
684         keyword_dialog_cancel_cb(gd, data);
685
686         bar_info_keyword_update_all();
687 }
688
689 static void keyword_dialog_add_cb(GtkWidget *button, gpointer data)
690 {
691         KeywordDlg *kd = data;
692         GtkTreeSelection *selection;
693         GtkTreeModel *store;
694         GtkTreeIter sibling;
695         GtkTreeIter iter;
696         GtkTreePath *tpath;
697
698         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(kd->treeview));
699         if (gtk_tree_selection_get_selected(selection, &store, &sibling))
700                 {
701                 gtk_list_store_insert_before(GTK_LIST_STORE(store), &iter, &sibling);
702                 }
703         else
704                 {
705                 store = gtk_tree_view_get_model(GTK_TREE_VIEW(kd->treeview));
706                 gtk_list_store_append(GTK_LIST_STORE(store), &iter);
707                 }
708
709         gtk_list_store_set(GTK_LIST_STORE(store), &iter, 1, TRUE, -1);
710
711         tpath = gtk_tree_model_get_path(store, &iter);
712         gtk_tree_view_set_cursor(GTK_TREE_VIEW(kd->treeview), tpath,
713                                  gtk_tree_view_get_column(GTK_TREE_VIEW(kd->treeview), 0), TRUE);
714         gtk_tree_path_free(tpath);
715 }
716
717 static void keyword_dialog_remove_cb(GtkWidget *button, gpointer data)
718 {
719         KeywordDlg *kd = data;
720         GtkTreeSelection *selection;
721         GtkTreeModel *store;
722         GtkTreeIter iter;
723         GtkTreeIter next;
724         GtkTreePath *tpath;
725
726         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(kd->treeview));
727         if (!gtk_tree_selection_get_selected(selection, &store, &iter)) return;
728
729         tpath = NULL;
730         next = iter;
731         if (gtk_tree_model_iter_next(store, &next))
732                 {
733                 tpath = gtk_tree_model_get_path(store, &next);
734                 }
735         else
736                 {
737                 tpath = gtk_tree_model_get_path(store, &iter);
738                 if (!gtk_tree_path_prev(tpath))
739                         {
740                         gtk_tree_path_free(tpath);
741                         tpath = NULL;
742                         }
743                 }
744         if (tpath)
745                 {
746                 gtk_tree_view_set_cursor(GTK_TREE_VIEW(kd->treeview), tpath,
747                                          gtk_tree_view_get_column(GTK_TREE_VIEW(kd->treeview), 0), FALSE);
748                 gtk_tree_path_free(tpath);
749                 }
750
751         gtk_list_store_remove(GTK_LIST_STORE(store), &iter);
752 }
753
754 static void keyword_dialog_edit_cb(GtkCellRendererText *renderer, const gchar *path,
755                                    const gchar *new_text, gpointer data)
756 {
757         KeywordDlg *kd = data;
758         GtkTreeModel *store;
759         GtkTreeIter iter;
760         GtkTreePath *tpath;
761
762         if (!new_text || strlen(new_text) == 0) return;
763
764         store = gtk_tree_view_get_model(GTK_TREE_VIEW(kd->treeview));
765
766         tpath = gtk_tree_path_new_from_string(path);
767         gtk_tree_model_get_iter(store, &iter, tpath);
768         gtk_tree_path_free(tpath);
769
770         gtk_list_store_set(GTK_LIST_STORE(store), &iter, 0, new_text, -1);
771 }
772
773 static void keyword_dialog_populate(KeywordDlg *kd)
774 {
775         GtkListStore *store;
776         GList *list;
777
778         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(kd->treeview)));
779         gtk_list_store_clear(store);
780
781         list = history_list_get_by_key("keywords");
782         list = g_list_last(list);
783         while (list)
784                 {
785                 GtkTreeIter iter;
786
787                 gtk_list_store_append(store, &iter);
788                 gtk_list_store_set(store, &iter, 0, list->data,
789                                                  1, TRUE, -1);
790
791                 list = list->prev;
792                 }
793 }
794
795 static void keyword_dialog_show(void)
796 {
797         GtkWidget *scrolled;
798         GtkListStore *store;
799         GtkTreeViewColumn *column;
800         GtkCellRenderer *renderer;
801         GtkWidget *hbox;
802         GtkWidget *button;
803
804         if (keyword_dialog)
805                 {
806                 gtk_window_present(GTK_WINDOW(keyword_dialog->gd->dialog));
807                 return;
808                 }
809
810         keyword_dialog = g_new0(KeywordDlg, 1);
811
812         keyword_dialog->gd = generic_dialog_new(_("Keyword Presets"),
813                                                 GQ_WMCLASS, "keyword_presets", NULL, TRUE,
814                                                 keyword_dialog_cancel_cb, keyword_dialog);
815         generic_dialog_add_message(keyword_dialog->gd, NULL, _("Favorite keywords list"), NULL);
816
817         generic_dialog_add_button(keyword_dialog->gd, GTK_STOCK_OK, NULL,
818                                  keyword_dialog_ok_cb, TRUE);
819
820         scrolled = gtk_scrolled_window_new(NULL, NULL);
821         gtk_widget_set_size_request(scrolled, KEYWORD_DIALOG_WIDTH, KEYWORD_DIALOG_HEIGHT);
822         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_IN);
823         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
824                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
825         gtk_box_pack_start(GTK_BOX(keyword_dialog->gd->vbox), scrolled, TRUE, TRUE, 5);
826         gtk_widget_show(scrolled);
827
828         store = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_BOOLEAN);
829         keyword_dialog->treeview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
830         g_object_unref(store);
831
832         gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(keyword_dialog->treeview), FALSE);
833         gtk_tree_view_set_search_column(GTK_TREE_VIEW(keyword_dialog->treeview), 0);
834         gtk_tree_view_set_reorderable(GTK_TREE_VIEW(keyword_dialog->treeview), TRUE);
835
836         column = gtk_tree_view_column_new();
837         gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
838         renderer = gtk_cell_renderer_text_new();
839         g_signal_connect(G_OBJECT(renderer), "edited",
840                          G_CALLBACK(keyword_dialog_edit_cb), keyword_dialog);
841         gtk_tree_view_column_pack_start(column, renderer, TRUE);
842         gtk_tree_view_column_add_attribute(column, renderer, "text", 0);
843         gtk_tree_view_column_add_attribute(column, renderer, "editable", 1);
844         gtk_tree_view_append_column(GTK_TREE_VIEW(keyword_dialog->treeview), column);
845
846         gtk_container_add(GTK_CONTAINER(scrolled), keyword_dialog->treeview);
847         gtk_widget_show(keyword_dialog->treeview);
848
849         hbox = gtk_hbox_new(FALSE, 5);
850         gtk_box_pack_start(GTK_BOX(keyword_dialog->gd->vbox), hbox, FALSE, FALSE, 0);
851         gtk_widget_show(hbox);
852
853         button = gtk_button_new_from_stock(GTK_STOCK_ADD);
854         g_signal_connect(G_OBJECT(button), "clicked",
855                          G_CALLBACK(keyword_dialog_add_cb), keyword_dialog);
856         gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
857         gtk_widget_show(button);
858
859         button = gtk_button_new_from_stock(GTK_STOCK_REMOVE);
860         g_signal_connect(G_OBJECT(button), "clicked",
861                          G_CALLBACK(keyword_dialog_remove_cb), keyword_dialog);
862         gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
863         gtk_widget_show(button);
864
865         keyword_dialog_populate(keyword_dialog);
866
867         gtk_widget_show(keyword_dialog->gd->dialog);
868 }
869
870
871 static void bar_keyword_edit_cb(GtkWidget *button, gpointer data)
872 {
873         keyword_dialog_show();
874 }
875
876
877 /*
878  *-------------------------------------------------------------------
879  * info bar
880  *-------------------------------------------------------------------
881  */
882
883 typedef enum {
884         BAR_SORT_COPY,
885         BAR_SORT_MOVE,
886         BAR_SORT_LINK
887 } SortActionType;
888
889 enum {
890         KEYWORD_COLUMN_TOGGLE = 0,
891         KEYWORD_COLUMN_TEXT
892 };
893
894 typedef struct _BarInfoData BarInfoData;
895 struct _BarInfoData
896 {
897         GtkWidget *vbox;
898         GtkWidget *group_box;
899         GtkWidget *label_file_name;
900         GtkWidget *label_file_time;
901
902         GtkWidget *keyword_view;
903         GtkWidget *keyword_treeview;
904
905         GtkWidget *comment_view;
906
907         GtkWidget *button_save;
908         GtkWidget *button_set_keywords_add;
909         GtkWidget *button_set_keywords_replace;
910         GtkWidget *button_set_comment_add;
911         GtkWidget *button_set_comment_replace;
912
913         FileData *fd;
914
915         gint changed;
916         gint save_timeout_id;
917
918         GList *(*list_func)(gpointer);
919         gpointer list_data;
920 };
921
922
923 static GList *bar_list = NULL;
924
925
926 static void bar_info_write(BarInfoData *bd)
927 {
928         GList *list;
929         gchar *comment;
930
931         if (!bd->fd) return;
932
933         list = keyword_list_pull(bd->keyword_view);
934         comment = comment_pull(bd->comment_view);
935
936         comment_write(bd->fd, list, comment);
937
938         string_list_free(list);
939         g_free(comment);
940
941         bd->changed = FALSE;
942         gtk_widget_set_sensitive(bd->button_save, FALSE);
943 }
944
945 static gint bar_info_autosave(gpointer data)
946 {
947         BarInfoData *bd = data;
948
949         bar_info_write(bd);
950
951         bd->save_timeout_id = -1;
952
953         return FALSE;
954 }
955
956 static void bar_info_save_update(BarInfoData *bd, gint enable)
957 {
958         if (bd->save_timeout_id != -1)
959                 {
960                 g_source_remove(bd->save_timeout_id);
961                 bd->save_timeout_id = -1;
962                 }
963         if (enable)
964                 {
965                 bd->save_timeout_id = g_timeout_add(BAR_KEYWORD_AUTOSAVE_TIME, bar_info_autosave, bd);
966                 }
967 }
968
969 static void bar_keyword_list_sync(BarInfoData *bd, GList *keywords)
970 {
971         GList *list;
972         GtkListStore *store;
973         GtkTreeIter iter;
974
975         list = history_list_get_by_key("keywords");
976         if (!list)
977                 {
978                 /* blank? set up a few example defaults */
979
980                 gint i = 0;
981
982                 while (keyword_favorite_defaults[i] != NULL)
983                         {
984                         history_list_add_to_key("keywords", _(keyword_favorite_defaults[i]), 0);
985                         i++;
986                         }
987
988                 list = history_list_get_by_key("keywords");
989                 }
990
991         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(bd->keyword_treeview)));
992
993         gtk_list_store_clear(store);
994
995         list = g_list_last(list);
996         while (list)
997                 {
998                 gchar *key = list->data;
999
1000                 gtk_list_store_append(store, &iter);
1001                 gtk_list_store_set(store, &iter, KEYWORD_COLUMN_TOGGLE, keyword_list_find(keywords, key),
1002                                                  KEYWORD_COLUMN_TEXT, key, -1);
1003
1004                 list = list->prev;
1005                 }
1006 }
1007
1008 static void bar_info_keyword_update_all(void)
1009 {
1010         GList *work;
1011
1012         work = bar_list;
1013         while (work)
1014                 {
1015                 BarInfoData *bd;
1016                 GList *keywords;
1017
1018                 bd = work->data;
1019                 work = work->next;
1020
1021                 keywords = keyword_list_pull(bd->keyword_view);
1022                 bar_keyword_list_sync(bd, keywords);
1023                 string_list_free(keywords);
1024                 }
1025 }
1026
1027 static void bar_info_update(BarInfoData *bd)
1028 {
1029         GList *keywords = NULL;
1030         gchar *comment = NULL;
1031
1032         if (bd->label_file_name)
1033                 {
1034                 gtk_label_set_text(GTK_LABEL(bd->label_file_name), (bd->fd) ? bd->fd->name : "");
1035                 }
1036         if (bd->label_file_time)
1037                 {
1038                 gtk_label_set_text(GTK_LABEL(bd->label_file_time), (bd->fd) ? text_from_time(bd->fd->date) : "");
1039                 }
1040
1041         if (comment_read(bd->fd, &keywords, &comment))
1042                 {
1043                 keyword_list_push(bd->keyword_view, keywords);
1044                 gtk_text_buffer_set_text(gtk_text_view_get_buffer(GTK_TEXT_VIEW(bd->comment_view)),
1045                                          (comment) ? comment : "", -1);
1046
1047                 bar_keyword_list_sync(bd, keywords);
1048
1049                 string_list_free(keywords);
1050                 g_free(comment);
1051                 }
1052         else
1053                 {
1054                 gtk_text_buffer_set_text(gtk_text_view_get_buffer(GTK_TEXT_VIEW(bd->keyword_view)), "", -1);
1055                 gtk_text_buffer_set_text(gtk_text_view_get_buffer(GTK_TEXT_VIEW(bd->comment_view)), "", -1);
1056
1057                 bar_keyword_list_sync(bd, NULL);
1058                 }
1059
1060         bar_info_save_update(bd, FALSE);
1061         bd->changed = FALSE;
1062         gtk_widget_set_sensitive(bd->button_save, FALSE);
1063
1064         gtk_widget_set_sensitive(bd->group_box, (bd->fd != NULL));
1065 }
1066
1067 void bar_info_set(GtkWidget *bar, FileData *fd)
1068 {
1069         BarInfoData *bd;
1070
1071         bd = g_object_get_data(G_OBJECT(bar), "bar_info_data");
1072         if (!bd) return;
1073
1074         if (bd->changed) bar_info_write(bd);
1075
1076         file_data_unref(bd->fd);
1077         bd->fd = file_data_ref(fd);
1078
1079         bar_info_update(bd);
1080 }
1081
1082 void bar_info_maint_renamed(GtkWidget *bar, FileData *fd)
1083 {
1084         BarInfoData *bd;
1085
1086         bd = g_object_get_data(G_OBJECT(bar), "bar_info_data");
1087         if (!bd) return;
1088
1089         file_data_unref(bd->fd);
1090         bd->fd = file_data_ref(fd);
1091
1092         if (bd->label_file_name)
1093                 {
1094                 gtk_label_set_text(GTK_LABEL(bd->label_file_name), (bd->fd) ? bd->fd->name : "");
1095                 }
1096 }
1097
1098 gint bar_info_event(GtkWidget *bar, GdkEvent *event)
1099 {
1100         BarInfoData *bd;
1101
1102         bd = g_object_get_data(G_OBJECT(bar), "bar_info_data");
1103         if (!bd) return FALSE;
1104
1105         if (GTK_WIDGET_HAS_FOCUS(bd->keyword_view)) return gtk_widget_event(bd->keyword_view, event);
1106         if (GTK_WIDGET_HAS_FOCUS(bd->comment_view)) return gtk_widget_event(bd->comment_view, event);
1107
1108         return FALSE;
1109 }
1110
1111 static void bar_info_keyword_set(BarInfoData *bd, const gchar *keyword, gint active)
1112 {
1113         GList *list;
1114         gint found;
1115
1116         if (!keyword) return;
1117
1118         list = keyword_list_pull(bd->keyword_view);
1119         found = keyword_list_find(list, keyword);
1120
1121         if (active != found)
1122                 {
1123                 if (found)
1124                         {
1125                         GList *work = list;
1126
1127                         while (work)
1128                                 {
1129                                 gchar *key = work->data;
1130                                 work = work->next;
1131
1132                                 if (key && keyword && strcmp(key, keyword) == 0)
1133                                         {
1134                                         list = g_list_remove(list, key);
1135                                         g_free(key);
1136                                         }
1137                                 }
1138                         }
1139                 else
1140                         {
1141                         list = g_list_append(list, g_strdup(keyword));
1142                         }
1143
1144                 keyword_list_push(bd->keyword_view, list);
1145                 }
1146
1147         string_list_free(list);
1148 }
1149
1150 static void bar_info_keyword_toggle(GtkCellRendererToggle *toggle, const gchar *path, gpointer data)
1151 {
1152         BarInfoData *bd = data;
1153         GtkTreeModel *store;
1154         GtkTreeIter iter;
1155         GtkTreePath *tpath;
1156         gchar *key = NULL;
1157         gboolean active;
1158
1159         store = gtk_tree_view_get_model(GTK_TREE_VIEW(bd->keyword_treeview));
1160
1161         tpath = gtk_tree_path_new_from_string(path);
1162         gtk_tree_model_get_iter(store, &iter, tpath);
1163         gtk_tree_path_free(tpath);
1164
1165         gtk_tree_model_get(store, &iter, KEYWORD_COLUMN_TOGGLE, &active,
1166                                          KEYWORD_COLUMN_TEXT, &key, -1);
1167         active = (!active);
1168         gtk_list_store_set(GTK_LIST_STORE(store), &iter, KEYWORD_COLUMN_TOGGLE, active, -1);
1169
1170         bar_info_keyword_set(bd, key, active);
1171         g_free(key);
1172 }
1173
1174 static void bar_info_save(GtkWidget *button, gpointer data)
1175 {
1176         BarInfoData *bd = data;
1177
1178         bar_info_save_update(bd, FALSE);
1179         bar_info_write(bd);
1180 }
1181
1182 static void bar_info_set_selection(BarInfoData *bd, gint set_keywords, gint set_comment, gint add)
1183 {
1184         GList *keywords = NULL;
1185         GList *list = NULL;
1186         GList *work;
1187         gchar *comment = NULL;
1188
1189         if (!bd->list_func) return;
1190
1191         if (set_keywords)
1192                 {
1193                 keywords = keyword_list_pull(bd->keyword_view);
1194                 }
1195
1196         if (set_comment)
1197                 {
1198                 comment = comment_pull(bd->comment_view);
1199                 }
1200
1201         if (add && !keywords && !comment) return;
1202
1203         list = bd->list_func(bd->list_data);
1204         work = list;
1205         while (work)
1206                 {
1207                 FileData *fd = work->data;
1208                 work = work->next;
1209
1210                 metadata_set_keywords(fd, keywords, comment, add);
1211                 }
1212
1213         filelist_free(list);
1214         string_list_free(keywords);
1215         g_free(comment);
1216 }
1217
1218 static void bar_info_set_keywords_add(GtkWidget *button, gpointer data)
1219 {
1220         BarInfoData *bd = data;
1221
1222         bar_info_set_selection(bd, TRUE, FALSE, TRUE);
1223 }
1224
1225 static void bar_info_set_keywords_replace(GtkWidget *button, gpointer data)
1226 {
1227         BarInfoData *bd = data;
1228
1229         bar_info_set_selection(bd, TRUE, FALSE, FALSE);
1230 }
1231
1232 static void bar_info_set_comment_add(GtkWidget *button, gpointer data)
1233 {
1234         BarInfoData *bd = data;
1235
1236         bar_info_set_selection(bd, FALSE, TRUE, TRUE);
1237 }
1238
1239 static void bar_info_set_comment_replace(GtkWidget *button, gpointer data)
1240 {
1241         BarInfoData *bd = data;
1242
1243         bar_info_set_selection(bd, FALSE, TRUE, FALSE);
1244 }
1245
1246 static void bar_info_changed(GtkTextBuffer *buffer, gpointer data)
1247 {
1248         BarInfoData *bd = data;
1249
1250         bd->changed = TRUE;
1251         gtk_widget_set_sensitive(bd->button_save, TRUE);
1252
1253         bar_info_save_update(bd, TRUE);
1254 }
1255
1256 void bar_info_close(GtkWidget *bar)
1257 {
1258         BarInfoData *bd;
1259
1260         bd = g_object_get_data(G_OBJECT(bar), "bar_info_data");
1261         if (!bd) return;
1262
1263         gtk_widget_destroy(bd->vbox);
1264 }
1265
1266 static void bar_info_destroy(GtkWidget *widget, gpointer data)
1267 {
1268         BarInfoData *bd = data;
1269
1270         if (bd->changed) bar_info_write(bd);
1271         bar_info_save_update(bd, FALSE);
1272
1273         bar_list = g_list_remove(bar_list, bd);
1274
1275         file_data_unref(bd->fd);
1276
1277         g_free(bd);
1278 }
1279
1280 GtkWidget *bar_info_new(FileData *fd, gint metadata_only, GtkWidget *bounding_widget)
1281 {
1282         BarInfoData *bd;
1283         GtkWidget *box;
1284         GtkWidget *hbox;
1285         GtkWidget *table;
1286         GtkWidget *scrolled;
1287         GtkTextBuffer *buffer;
1288         GtkWidget *label;
1289         GtkWidget *tbar;
1290         GtkListStore *store;
1291         GtkTreeViewColumn *column;
1292         GtkCellRenderer *renderer;
1293
1294         bd = g_new0(BarInfoData, 1);
1295
1296         bd->list_func = NULL;
1297         bd->list_data = NULL;
1298
1299         bd->vbox = gtk_vbox_new(FALSE, PREF_PAD_GAP);
1300         g_object_set_data(G_OBJECT(bd->vbox), "bar_info_data", bd);
1301         g_signal_connect(G_OBJECT(bd->vbox), "destroy",
1302                          G_CALLBACK(bar_info_destroy), bd);
1303
1304         if (!metadata_only)
1305                 {
1306                 hbox = pref_box_new(bd->vbox, FALSE, GTK_ORIENTATION_HORIZONTAL, PREF_PAD_GAP);
1307
1308                 label = sizer_new(bd->vbox, bounding_widget, SIZER_POS_LEFT);
1309                 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
1310                 gtk_widget_show(label);
1311
1312                 label = gtk_label_new(_("Keywords"));
1313                 pref_label_bold(label, TRUE, FALSE);
1314                 gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 0);
1315                 gtk_widget_show(label);
1316                 }
1317
1318         bd->group_box = pref_box_new(bd->vbox, TRUE, GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
1319
1320         if (!metadata_only)
1321                 {
1322                 GtkWidget *table;
1323
1324                 table = pref_table_new(bd->group_box, 2, 2, FALSE, FALSE);
1325
1326                 bd->label_file_name = table_add_line(table, 0, 0, _("Filename:"), NULL);
1327                 bd->label_file_time = table_add_line(table, 0, 1, _("File date:"), NULL);
1328                 }
1329         else
1330                 {
1331                 bd->label_file_name = NULL;
1332                 bd->label_file_time = NULL;
1333                 }
1334
1335         table = gtk_table_new(3, 1, TRUE);
1336         gtk_table_set_row_spacings(GTK_TABLE(table), PREF_PAD_GAP);
1337         gtk_box_pack_start(GTK_BOX(bd->group_box), table, TRUE, TRUE, 0);
1338         gtk_widget_show(table);
1339
1340         /* keyword entry */
1341
1342         box = gtk_vbox_new(FALSE, 0);
1343         gtk_table_attach(GTK_TABLE(table), box, 0, 1, 0, 2,
1344                          GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
1345         gtk_widget_show(box);
1346
1347         label = pref_label_new(box, _("Keywords:"));
1348         gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
1349         pref_label_bold(label, TRUE, FALSE);
1350
1351         hbox = pref_box_new(box, TRUE, GTK_ORIENTATION_HORIZONTAL, PREF_PAD_GAP);
1352
1353         scrolled = gtk_scrolled_window_new(NULL, NULL);
1354         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_IN);
1355         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
1356                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1357         gtk_box_pack_start(GTK_BOX(hbox), scrolled, TRUE, TRUE, 0);
1358         gtk_widget_show(scrolled);
1359
1360         bd->keyword_view = gtk_text_view_new();
1361         gtk_container_add(GTK_CONTAINER(scrolled), bd->keyword_view);
1362         gtk_widget_show(bd->keyword_view);
1363
1364         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(bd->keyword_view));
1365         g_signal_connect(G_OBJECT(buffer), "changed",
1366                          G_CALLBACK(bar_info_changed), bd);
1367
1368         scrolled = gtk_scrolled_window_new(NULL, NULL);
1369         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_IN);
1370         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
1371                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1372         gtk_box_pack_start(GTK_BOX(hbox), scrolled, TRUE, TRUE, 0);
1373         gtk_widget_show(scrolled);
1374
1375         store = gtk_list_store_new(2, G_TYPE_BOOLEAN, G_TYPE_STRING);
1376         bd->keyword_treeview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
1377         g_object_unref(store);
1378
1379         gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(bd->keyword_treeview), FALSE);
1380
1381         if (metadata_only)
1382                 {
1383                 gtk_tree_view_set_search_column(GTK_TREE_VIEW(bd->keyword_treeview), KEYWORD_COLUMN_TEXT);
1384                 }
1385         else
1386                 {
1387                 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(bd->keyword_treeview), FALSE);
1388                 }
1389
1390         column = gtk_tree_view_column_new();
1391         gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
1392
1393         renderer = gtk_cell_renderer_toggle_new();
1394         gtk_tree_view_column_pack_start(column, renderer, FALSE);
1395         gtk_tree_view_column_add_attribute(column, renderer, "active", KEYWORD_COLUMN_TOGGLE);
1396         g_signal_connect(G_OBJECT(renderer), "toggled",
1397                          G_CALLBACK(bar_info_keyword_toggle), bd);
1398
1399         renderer = gtk_cell_renderer_text_new();
1400         gtk_tree_view_column_pack_start(column, renderer, TRUE);
1401         gtk_tree_view_column_add_attribute(column, renderer, "text", KEYWORD_COLUMN_TEXT);
1402
1403         gtk_tree_view_append_column(GTK_TREE_VIEW(bd->keyword_treeview), column);
1404
1405         gtk_container_add(GTK_CONTAINER(scrolled), bd->keyword_treeview);
1406         gtk_widget_show(bd->keyword_treeview);
1407
1408         /* comment entry */
1409
1410         box = gtk_vbox_new(FALSE, 0);
1411         gtk_table_attach(GTK_TABLE(table), box, 0, 1, 2, 3,
1412                          GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
1413         gtk_widget_show(box);
1414
1415         label = pref_label_new(box, _("Comment:"));
1416         gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
1417         pref_label_bold(label, TRUE, FALSE);
1418
1419         scrolled = gtk_scrolled_window_new(NULL, NULL);
1420         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_IN);
1421         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
1422                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1423         gtk_box_pack_start(GTK_BOX(box), scrolled, TRUE, TRUE, 0);
1424         gtk_widget_show(scrolled);
1425
1426         bd->comment_view = gtk_text_view_new();
1427         gtk_container_add(GTK_CONTAINER(scrolled), bd->comment_view);
1428         gtk_widget_show(bd->comment_view);
1429
1430         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(bd->comment_view));
1431         g_signal_connect(G_OBJECT(buffer), "changed",
1432                          G_CALLBACK(bar_info_changed), bd);
1433
1434         /* toolbar */
1435
1436         tbar = pref_toolbar_new(bd->group_box, GTK_TOOLBAR_ICONS);
1437
1438         pref_toolbar_button(tbar, GTK_STOCK_INDEX, NULL, FALSE,
1439                         _("Edit favorite keywords list."),
1440                         G_CALLBACK(bar_keyword_edit_cb), bd);
1441         pref_toolbar_spacer(tbar);
1442         bd->button_set_keywords_add = pref_toolbar_button(tbar, GTK_STOCK_ADD, NULL, FALSE,
1443                         _("Add keywords to selected files"),
1444                         G_CALLBACK(bar_info_set_keywords_add), bd);
1445         bd->button_set_keywords_replace = pref_toolbar_button(tbar, GTK_STOCK_CONVERT, NULL, FALSE,
1446                         _("Add keywords to selected files, replacing existing ones"),
1447                         G_CALLBACK(bar_info_set_keywords_replace), bd);
1448         bd->button_set_comment_add = pref_toolbar_button(tbar, GTK_STOCK_DND_MULTIPLE, NULL, FALSE,
1449                         _("Add comment to selected files"),
1450                         G_CALLBACK(bar_info_set_comment_add), bd);
1451         bd->button_set_comment_replace = pref_toolbar_button(tbar, GTK_STOCK_DND, NULL, FALSE,
1452                         _("Add comment to selected files, replacing existing one"),
1453                         G_CALLBACK(bar_info_set_comment_replace), bd);
1454
1455         pref_toolbar_spacer(tbar);
1456         bd->button_save = pref_toolbar_button(tbar, GTK_STOCK_SAVE, NULL, FALSE,
1457                         _("Save comment now"),
1458                         G_CALLBACK(bar_info_save), bd);
1459
1460         bd->save_timeout_id = -1;
1461
1462         bd->fd = file_data_ref(fd);
1463         bar_info_update(bd);
1464
1465         bar_info_selection(bd->vbox, 0);
1466
1467         bar_list = g_list_append(bar_list, bd);
1468
1469         return bd->vbox;
1470 }
1471
1472 void bar_info_set_selection_func(GtkWidget *bar, GList *(*list_func)(gpointer data), gpointer data)
1473 {
1474         BarInfoData *bd;
1475
1476         bd = g_object_get_data(G_OBJECT(bar), "bar_info_data");
1477         if (!bd) return;
1478
1479         bd->list_func = list_func;
1480         bd->list_data = data;
1481 }
1482
1483 void bar_info_selection(GtkWidget *bar, gint count)
1484 {
1485         BarInfoData *bd;
1486         gint enable;
1487
1488         bd = g_object_get_data(G_OBJECT(bar), "bar_info_data");
1489         if (!bd) return;
1490
1491         enable = (count > 0 && bd->list_func != NULL);
1492
1493         gtk_widget_set_sensitive(bd->button_set_keywords_add, enable);
1494         gtk_widget_set_sensitive(bd->button_set_keywords_replace, enable);
1495         gtk_widget_set_sensitive(bd->button_set_comment_add, enable);
1496         gtk_widget_set_sensitive(bd->button_set_comment_replace, enable);
1497
1498 }