Metadata spelling checks
[geeqie.git] / src / bar_comment.c
1 /*
2  * Copyright (C) 2004 John Ellis
3  * Copyright (C) 2008 - 2016 The Geeqie Team
4  *
5  * Author: John Ellis
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include "main.h"
23 #include "bar_comment.h"
24
25 #include "bar.h"
26 #include "metadata.h"
27 #include "filedata.h"
28 #include "ui_menu.h"
29 #include "ui_misc.h"
30 #include "rcfile.h"
31 #include "layout.h"
32
33 #ifdef HAVE_SPELL
34 #include <gspell/gspell.h>
35 #endif
36
37 static void bar_pane_comment_changed(GtkTextBuffer *buffer, gpointer data);
38
39 /*
40  *-------------------------------------------------------------------
41  * keyword / comment utils
42  *-------------------------------------------------------------------
43  */
44
45
46
47 typedef struct _PaneCommentData PaneCommentData;
48 struct _PaneCommentData
49 {
50         PaneData pane;
51         GtkWidget *widget;
52         GtkWidget *comment_view;
53         FileData *fd;
54         gchar *key;
55         gint height;
56 };
57
58
59 static void bar_pane_comment_write(PaneCommentData *pcd)
60 {
61         gchar *comment;
62
63         if (!pcd->fd) return;
64
65         comment = text_widget_text_pull(pcd->comment_view);
66
67         metadata_write_string(pcd->fd, pcd->key, comment);
68         g_free(comment);
69 }
70
71
72 static void bar_pane_comment_update(PaneCommentData *pcd)
73 {
74         gchar *comment = NULL;
75         gchar *orig_comment = NULL;
76         gchar *comment_not_null;
77         gshort rating;
78         GtkTextBuffer *comment_buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(pcd->comment_view));
79
80         orig_comment = text_widget_text_pull(pcd->comment_view);
81         if (g_strcmp0(pcd->key, "Xmp.xmp.Rating") == 0)
82                 {
83                 rating = metadata_read_int(pcd->fd, pcd->key, 0);
84                 comment = g_strdup_printf("%d", rating);
85                 }
86         else
87                 {
88                 comment = metadata_read_string(pcd->fd, pcd->key, METADATA_PLAIN);
89                 }
90         comment_not_null = (comment) ? comment : "";
91
92         if (strcmp(orig_comment, comment_not_null) != 0)
93                 {
94                 g_signal_handlers_block_by_func(comment_buffer, bar_pane_comment_changed, pcd);
95                 gtk_text_buffer_set_text(comment_buffer, comment_not_null, -1);
96                 g_signal_handlers_unblock_by_func(comment_buffer, bar_pane_comment_changed, pcd);
97                 }
98         g_free(comment);
99         g_free(orig_comment);
100
101         gtk_widget_set_sensitive(pcd->comment_view, (pcd->fd != NULL));
102 }
103
104 static void bar_pane_comment_set_selection(PaneCommentData *pcd, gboolean append)
105 {
106         GList *list = NULL;
107         GList *work;
108         gchar *comment = NULL;
109
110         comment = text_widget_text_pull(pcd->comment_view);
111
112         list = layout_selection_list(pcd->pane.lw);
113         list = file_data_process_groups_in_selection(list, FALSE, NULL);
114
115         work = list;
116         while (work)
117                 {
118                 FileData *fd = work->data;
119                 work = work->next;
120                 if (fd == pcd->fd) continue;
121
122                 if (append)
123                         {
124                         metadata_append_string(fd, pcd->key, comment);
125                         }
126                 else
127                         {
128                         metadata_write_string(fd, pcd->key, comment);
129                         }
130                 }
131
132         filelist_free(list);
133         g_free(comment);
134 }
135
136 static void bar_pane_comment_sel_add_cb(GtkWidget *button, gpointer data)
137 {
138         PaneCommentData *pcd = data;
139
140         bar_pane_comment_set_selection(pcd, TRUE);
141 }
142
143 static void bar_pane_comment_sel_replace_cb(GtkWidget *button, gpointer data)
144 {
145         PaneCommentData *pcd = data;
146
147         bar_pane_comment_set_selection(pcd, FALSE);
148 }
149
150
151 static void bar_pane_comment_set_fd(GtkWidget *bar, FileData *fd)
152 {
153         PaneCommentData *pcd;
154
155         pcd = g_object_get_data(G_OBJECT(bar), "pane_data");
156         if (!pcd) return;
157
158         file_data_unref(pcd->fd);
159         pcd->fd = file_data_ref(fd);
160
161         bar_pane_comment_update(pcd);
162 }
163
164 static gint bar_pane_comment_event(GtkWidget *bar, GdkEvent *event)
165 {
166         PaneCommentData *pcd;
167
168         pcd = g_object_get_data(G_OBJECT(bar), "pane_data");
169         if (!pcd) return FALSE;
170
171         if (gtk_widget_has_focus(pcd->comment_view)) return gtk_widget_event(pcd->comment_view, event);
172
173         return FALSE;
174 }
175
176 static void bar_pane_comment_write_config(GtkWidget *pane, GString *outstr, gint indent)
177 {
178         PaneCommentData *pcd;
179         gint w, h;
180
181         pcd = g_object_get_data(G_OBJECT(pane), "pane_data");
182         if (!pcd) return;
183
184         gtk_widget_get_size_request(GTK_WIDGET(pane), &w, &h);
185
186         if (!g_strcmp0(pcd->pane.id, "title"))
187                 {
188                 pcd->height = h;
189                 }
190         if (!g_strcmp0(pcd->pane.id, "comment"))
191                 {
192                 pcd->height = h;
193                 }
194         if (!g_strcmp0(pcd->pane.id, "rating"))
195                 {
196                 pcd->height = h;
197                 }
198         if (!g_strcmp0(pcd->pane.id, "headline"))
199                 {
200                 pcd->height = h;
201                 }
202
203         WRITE_NL(); WRITE_STRING("<pane_comment ");
204         write_char_option(outstr, indent, "id", pcd->pane.id);
205         write_char_option(outstr, indent, "title", gtk_label_get_text(GTK_LABEL(pcd->pane.title)));
206         WRITE_BOOL(pcd->pane, expanded);
207         WRITE_CHAR(*pcd, key);
208         WRITE_INT(*pcd, height);
209         WRITE_STRING("/>");
210 }
211
212 static void bar_pane_comment_notify_cb(FileData *fd, NotifyType type, gpointer data)
213 {
214         PaneCommentData *pcd = data;
215         if ((type & (NOTIFY_REREAD | NOTIFY_CHANGE | NOTIFY_METADATA)) && fd == pcd->fd)
216                 {
217                 DEBUG_1("Notify pane_comment: %s %04x", fd->path, type);
218
219                 bar_pane_comment_update(pcd);
220                 }
221 }
222
223 static void bar_pane_comment_changed(GtkTextBuffer *buffer, gpointer data)
224 {
225         PaneCommentData *pcd = data;
226
227         bar_pane_comment_write(pcd);
228 }
229
230
231 static void bar_pane_comment_populate_popup(GtkTextView *textview, GtkMenu *menu, gpointer data)
232 {
233         PaneCommentData *pcd = data;
234
235         menu_item_add_divider(GTK_WIDGET(menu));
236         menu_item_add_stock(GTK_WIDGET(menu), _("Add text to selected files"), GTK_STOCK_ADD, G_CALLBACK(bar_pane_comment_sel_add_cb), pcd);
237         menu_item_add_stock(GTK_WIDGET(menu), _("Replace existing text in selected files"), GTK_STOCK_CONVERT, G_CALLBACK(bar_pane_comment_sel_replace_cb), data);
238 }
239
240 static void bar_pane_comment_destroy(GtkWidget *widget, gpointer data)
241 {
242         PaneCommentData *pcd = data;
243
244         file_data_unregister_notify_func(bar_pane_comment_notify_cb, pcd);
245
246         file_data_unref(pcd->fd);
247         g_free(pcd->key);
248
249         g_free(pcd->pane.id);
250
251         g_free(pcd);
252 }
253
254
255 static GtkWidget *bar_pane_comment_new(const gchar *id, const gchar *title, const gchar *key, gboolean expanded, gint height)
256 {
257         PaneCommentData *pcd;
258         GtkWidget *scrolled;
259         GtkTextBuffer *buffer;
260 #ifdef HAVE_SPELL
261         GspellTextView *gspell_view;
262 #endif
263
264         pcd = g_new0(PaneCommentData, 1);
265
266         pcd->pane.pane_set_fd = bar_pane_comment_set_fd;
267         pcd->pane.pane_event = bar_pane_comment_event;
268         pcd->pane.pane_write_config = bar_pane_comment_write_config;
269         pcd->pane.title = bar_pane_expander_title(title);
270         pcd->pane.id = g_strdup(id);
271         pcd->pane.type = PANE_COMMENT;
272
273         pcd->pane.expanded = expanded;
274
275         pcd->key = g_strdup(key);
276         pcd->height = height;
277
278         scrolled = gtk_scrolled_window_new(NULL, NULL);
279
280         pcd->widget = scrolled;
281         g_object_set_data(G_OBJECT(pcd->widget), "pane_data", pcd);
282         g_signal_connect(G_OBJECT(pcd->widget), "destroy",
283                          G_CALLBACK(bar_pane_comment_destroy), pcd);
284
285         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_IN);
286         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
287                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
288
289         gtk_widget_set_size_request(pcd->widget, -1, height);
290         gtk_widget_show(scrolled);
291
292         pcd->comment_view = gtk_text_view_new();
293         gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(pcd->comment_view), GTK_WRAP_WORD);
294         gtk_container_add(GTK_CONTAINER(scrolled), pcd->comment_view);
295         g_signal_connect(G_OBJECT(pcd->comment_view), "populate-popup",
296                          G_CALLBACK(bar_pane_comment_populate_popup), pcd);
297         gtk_widget_show(pcd->comment_view);
298
299 #ifdef HAVE_SPELL
300 #if GTK_CHECK_VERSION(3,20,0)
301         if (g_strcmp0(key, "Xmp.xmp.Rating") != 0)
302                 {
303                 if (options->metadata.check_spelling)
304                         {
305                         gspell_view = gspell_text_view_get_from_gtk_text_view(GTK_TEXT_VIEW(pcd->comment_view));
306                         gspell_text_view_basic_setup(gspell_view);
307                         }
308         }
309 #endif
310 #endif
311
312         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(pcd->comment_view));
313         g_signal_connect(G_OBJECT(buffer), "changed",
314                          G_CALLBACK(bar_pane_comment_changed), pcd);
315
316
317         file_data_register_notify_func(bar_pane_comment_notify_cb, pcd, NOTIFY_PRIORITY_LOW);
318
319         return pcd->widget;
320 }
321
322 GtkWidget *bar_pane_comment_new_from_config(const gchar **attribute_names, const gchar **attribute_values)
323 {
324         gchar *title = NULL;
325         gchar *key = g_strdup(COMMENT_KEY);
326         gboolean expanded = TRUE;
327         gint height = 50;
328         gchar *id = g_strdup("comment");
329         GtkWidget *ret;
330
331         while (*attribute_names)
332                 {
333                 const gchar *option = *attribute_names++;
334                 const gchar *value = *attribute_values++;
335
336                 if (READ_CHAR_FULL("title", title)) continue;
337                 if (READ_CHAR_FULL("key", key)) continue;
338                 if (READ_BOOL_FULL("expanded", expanded)) continue;
339                 if (READ_INT_FULL("height", height)) continue;
340                 if (READ_CHAR_FULL("id", id)) continue;
341
342
343                 log_printf("unknown attribute %s = %s\n", option, value);
344                 }
345
346         if (!g_strcmp0(id, "title"))
347                 {
348                 options->info_title.height = height;
349                 }
350         if (!g_strcmp0(id, "comment"))
351                 {
352                 options->info_comment.height = height;
353                 }
354         if (!g_strcmp0(id, "rating"))
355                 {
356                 options->info_rating.height = height;
357                 }
358         if (!g_strcmp0(id, "headline"))
359                 {
360                 options->info_headline.height = height;
361                 }
362
363         bar_pane_translate_title(PANE_COMMENT, id, &title);
364         ret = bar_pane_comment_new(id, title, key, expanded, height);
365         g_free(title);
366         g_free(key);
367         g_free(id);
368         return ret;
369 }
370
371 void bar_pane_comment_update_from_config(GtkWidget *pane, const gchar **attribute_names, const gchar **attribute_values)
372 {
373         PaneCommentData *pcd;
374
375         pcd = g_object_get_data(G_OBJECT(pane), "pane_data");
376         if (!pcd) return;
377
378         gchar *title = NULL;
379
380         while (*attribute_names)
381                 {
382                 const gchar *option = *attribute_names++;
383                 const gchar *value = *attribute_values++;
384
385                 if (READ_CHAR_FULL("title", title)) continue;
386                 if (READ_CHAR_FULL("key", pcd->key)) continue;
387                 if (READ_BOOL_FULL("expanded", pcd->pane.expanded)) continue;
388                 if (READ_INT_FULL("height", pcd->height)) continue;
389                 if (READ_CHAR_FULL("id", pcd->pane.id)) continue;
390
391
392                 log_printf("unknown attribute %s = %s\n", option, value);
393                 }
394
395         if (title)
396                 {
397                 bar_pane_translate_title(PANE_COMMENT, pcd->pane.id, &title);
398                 gtk_label_set_text(GTK_LABEL(pcd->pane.title), title);
399                 g_free(title);
400                 }
401         gtk_widget_set_size_request(pcd->widget, -1, pcd->height);
402         bar_update_expander(pane);
403         bar_pane_comment_update(pcd);
404 }
405
406 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */