Simplify vflist_get_formatted()
[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 static void bar_pane_comment_changed(GtkTextBuffer *buffer, gpointer data);
34
35 /*
36  *-------------------------------------------------------------------
37  * keyword / comment utils
38  *-------------------------------------------------------------------
39  */
40
41
42
43 typedef struct _PaneCommentData PaneCommentData;
44 struct _PaneCommentData
45 {
46         PaneData pane;
47         GtkWidget *widget;
48         GtkWidget *comment_view;
49         FileData *fd;
50         gchar *key;
51         gint height;
52 };
53
54
55 static void bar_pane_comment_write(PaneCommentData *pcd)
56 {
57         gchar *comment;
58
59         if (!pcd->fd) return;
60
61         comment = text_widget_text_pull(pcd->comment_view);
62
63         metadata_write_string(pcd->fd, pcd->key, comment);
64         g_free(comment);
65 }
66
67
68 static void bar_pane_comment_update(PaneCommentData *pcd)
69 {
70         gchar *comment = NULL;
71         gchar *orig_comment = NULL;
72         gchar *comment_not_null;
73         gshort rating;
74         GtkTextBuffer *comment_buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(pcd->comment_view));
75
76         orig_comment = text_widget_text_pull(pcd->comment_view);
77         if (g_strcmp0(pcd->key, "Xmp.xmp.Rating") == 0)
78                 {
79                 rating = metadata_read_int(pcd->fd, pcd->key, 0);
80                 comment = g_strdup_printf("%d", rating);
81                 }
82         else
83                 {
84                 comment = metadata_read_string(pcd->fd, pcd->key, METADATA_PLAIN);
85                 }
86         comment_not_null = (comment) ? comment : "";
87
88         if (strcmp(orig_comment, comment_not_null) != 0)
89                 {
90                 g_signal_handlers_block_by_func(comment_buffer, bar_pane_comment_changed, pcd);
91                 gtk_text_buffer_set_text(comment_buffer, comment_not_null, -1);
92                 g_signal_handlers_unblock_by_func(comment_buffer, bar_pane_comment_changed, pcd);
93                 }
94         g_free(comment);
95         g_free(orig_comment);
96
97         gtk_widget_set_sensitive(pcd->comment_view, (pcd->fd != NULL));
98 }
99
100 static void bar_pane_comment_set_selection(PaneCommentData *pcd, gboolean append)
101 {
102         GList *list = NULL;
103         GList *work;
104         gchar *comment = NULL;
105
106         comment = text_widget_text_pull(pcd->comment_view);
107
108         list = layout_selection_list(pcd->pane.lw);
109         list = file_data_process_groups_in_selection(list, FALSE, NULL);
110
111         work = list;
112         while (work)
113                 {
114                 FileData *fd = work->data;
115                 work = work->next;
116                 if (fd == pcd->fd) continue;
117
118                 if (append)
119                         {
120                         metadata_append_string(fd, pcd->key, comment);
121                         }
122                 else
123                         {
124                         metadata_write_string(fd, pcd->key, comment);
125                         }
126                 }
127
128         filelist_free(list);
129         g_free(comment);
130 }
131
132 static void bar_pane_comment_sel_add_cb(GtkWidget *button, gpointer data)
133 {
134         PaneCommentData *pcd = data;
135
136         bar_pane_comment_set_selection(pcd, TRUE);
137 }
138
139 static void bar_pane_comment_sel_replace_cb(GtkWidget *button, gpointer data)
140 {
141         PaneCommentData *pcd = data;
142
143         bar_pane_comment_set_selection(pcd, FALSE);
144 }
145
146
147 static void bar_pane_comment_set_fd(GtkWidget *bar, FileData *fd)
148 {
149         PaneCommentData *pcd;
150
151         pcd = g_object_get_data(G_OBJECT(bar), "pane_data");
152         if (!pcd) return;
153
154         file_data_unref(pcd->fd);
155         pcd->fd = file_data_ref(fd);
156
157         bar_pane_comment_update(pcd);
158 }
159
160 static gint bar_pane_comment_event(GtkWidget *bar, GdkEvent *event)
161 {
162         PaneCommentData *pcd;
163
164         pcd = g_object_get_data(G_OBJECT(bar), "pane_data");
165         if (!pcd) return FALSE;
166
167         if (gtk_widget_has_focus(pcd->comment_view)) return gtk_widget_event(pcd->comment_view, event);
168
169         return FALSE;
170 }
171
172 static void bar_pane_comment_write_config(GtkWidget *pane, GString *outstr, gint indent)
173 {
174         PaneCommentData *pcd;
175         gint w, h;
176
177         pcd = g_object_get_data(G_OBJECT(pane), "pane_data");
178         if (!pcd) return;
179
180         gtk_widget_get_size_request(GTK_WIDGET(pane), &w, &h);
181
182         if (!g_strcmp0(pcd->pane.id, "title"))
183                 {
184                 pcd->height = h;
185                 }
186         if (!g_strcmp0(pcd->pane.id, "comment"))
187                 {
188                 pcd->height = h;
189                 }
190         if (!g_strcmp0(pcd->pane.id, "rating"))
191                 {
192                 pcd->height = h;
193                 }
194         if (!g_strcmp0(pcd->pane.id, "headline"))
195                 {
196                 pcd->height = h;
197                 }
198
199         WRITE_NL(); WRITE_STRING("<pane_comment ");
200         write_char_option(outstr, indent, "id", pcd->pane.id);
201         write_char_option(outstr, indent, "title", gtk_label_get_text(GTK_LABEL(pcd->pane.title)));
202         WRITE_BOOL(pcd->pane, expanded);
203         WRITE_CHAR(*pcd, key);
204         WRITE_INT(*pcd, height);
205         WRITE_STRING("/>");
206 }
207
208 static void bar_pane_comment_notify_cb(FileData *fd, NotifyType type, gpointer data)
209 {
210         PaneCommentData *pcd = data;
211         if ((type & (NOTIFY_REREAD | NOTIFY_CHANGE | NOTIFY_METADATA)) && fd == pcd->fd)
212                 {
213                 DEBUG_1("Notify pane_comment: %s %04x", fd->path, type);
214
215                 bar_pane_comment_update(pcd);
216                 }
217 }
218
219 static void bar_pane_comment_changed(GtkTextBuffer *buffer, gpointer data)
220 {
221         PaneCommentData *pcd = data;
222
223         bar_pane_comment_write(pcd);
224 }
225
226
227 static void bar_pane_comment_populate_popup(GtkTextView *textview, GtkMenu *menu, gpointer data)
228 {
229         PaneCommentData *pcd = data;
230
231         menu_item_add_divider(GTK_WIDGET(menu));
232         menu_item_add_stock(GTK_WIDGET(menu), _("Add text to selected files"), GTK_STOCK_ADD, G_CALLBACK(bar_pane_comment_sel_add_cb), pcd);
233         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);
234 }
235
236 static void bar_pane_comment_destroy(GtkWidget *widget, gpointer data)
237 {
238         PaneCommentData *pcd = data;
239
240         file_data_unregister_notify_func(bar_pane_comment_notify_cb, pcd);
241
242         file_data_unref(pcd->fd);
243         g_free(pcd->key);
244
245         g_free(pcd->pane.id);
246
247         g_free(pcd);
248 }
249
250
251 static GtkWidget *bar_pane_comment_new(const gchar *id, const gchar *title, const gchar *key, gboolean expanded, gint height)
252 {
253         PaneCommentData *pcd;
254         GtkWidget *scrolled;
255         GtkTextBuffer *buffer;
256
257         pcd = g_new0(PaneCommentData, 1);
258
259         pcd->pane.pane_set_fd = bar_pane_comment_set_fd;
260         pcd->pane.pane_event = bar_pane_comment_event;
261         pcd->pane.pane_write_config = bar_pane_comment_write_config;
262         pcd->pane.title = bar_pane_expander_title(title);
263         pcd->pane.id = g_strdup(id);
264         pcd->pane.type = PANE_COMMENT;
265
266         pcd->pane.expanded = expanded;
267
268         pcd->key = g_strdup(key);
269         pcd->height = height;
270
271         scrolled = gtk_scrolled_window_new(NULL, NULL);
272
273         pcd->widget = scrolled;
274         g_object_set_data(G_OBJECT(pcd->widget), "pane_data", pcd);
275         g_signal_connect(G_OBJECT(pcd->widget), "destroy",
276                          G_CALLBACK(bar_pane_comment_destroy), pcd);
277
278         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_IN);
279         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
280                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
281
282         gtk_widget_set_size_request(pcd->widget, -1, height);
283         gtk_widget_show(scrolled);
284
285         pcd->comment_view = gtk_text_view_new();
286         gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(pcd->comment_view), GTK_WRAP_WORD);
287         gtk_container_add(GTK_CONTAINER(scrolled), pcd->comment_view);
288         g_signal_connect(G_OBJECT(pcd->comment_view), "populate-popup",
289                          G_CALLBACK(bar_pane_comment_populate_popup), pcd);
290         gtk_widget_show(pcd->comment_view);
291
292         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(pcd->comment_view));
293         g_signal_connect(G_OBJECT(buffer), "changed",
294                          G_CALLBACK(bar_pane_comment_changed), pcd);
295
296
297         file_data_register_notify_func(bar_pane_comment_notify_cb, pcd, NOTIFY_PRIORITY_LOW);
298
299         return pcd->widget;
300 }
301
302 GtkWidget *bar_pane_comment_new_from_config(const gchar **attribute_names, const gchar **attribute_values)
303 {
304         gchar *title = NULL;
305         gchar *key = g_strdup(COMMENT_KEY);
306         gboolean expanded = TRUE;
307         gint height = 50;
308         gchar *id = g_strdup("comment");
309         GtkWidget *ret;
310
311         while (*attribute_names)
312                 {
313                 const gchar *option = *attribute_names++;
314                 const gchar *value = *attribute_values++;
315
316                 if (READ_CHAR_FULL("title", title)) continue;
317                 if (READ_CHAR_FULL("key", key)) continue;
318                 if (READ_BOOL_FULL("expanded", expanded)) continue;
319                 if (READ_INT_FULL("height", height)) continue;
320                 if (READ_CHAR_FULL("id", id)) continue;
321
322
323                 log_printf("unknown attribute %s = %s\n", option, value);
324                 }
325
326         if (!g_strcmp0(id, "title"))
327                 {
328                 options->info_title.height = height;
329                 }
330         if (!g_strcmp0(id, "comment"))
331                 {
332                 options->info_comment.height = height;
333                 }
334         if (!g_strcmp0(id, "rating"))
335                 {
336                 options->info_rating.height = height;
337                 }
338         if (!g_strcmp0(id, "headline"))
339                 {
340                 options->info_headline.height = height;
341                 }
342
343         bar_pane_translate_title(PANE_COMMENT, id, &title);
344         ret = bar_pane_comment_new(id, title, key, expanded, height);
345         g_free(title);
346         g_free(key);
347         g_free(id);
348         return ret;
349 }
350
351 void bar_pane_comment_update_from_config(GtkWidget *pane, const gchar **attribute_names, const gchar **attribute_values)
352 {
353         PaneCommentData *pcd;
354
355         pcd = g_object_get_data(G_OBJECT(pane), "pane_data");
356         if (!pcd) return;
357
358         gchar *title = NULL;
359
360         while (*attribute_names)
361                 {
362                 const gchar *option = *attribute_names++;
363                 const gchar *value = *attribute_values++;
364
365                 if (READ_CHAR_FULL("title", title)) continue;
366                 if (READ_CHAR_FULL("key", pcd->key)) continue;
367                 if (READ_BOOL_FULL("expanded", pcd->pane.expanded)) continue;
368                 if (READ_INT_FULL("height", pcd->height)) continue;
369                 if (READ_CHAR_FULL("id", pcd->pane.id)) continue;
370
371
372                 log_printf("unknown attribute %s = %s\n", option, value);
373                 }
374
375         if (title)
376                 {
377                 bar_pane_translate_title(PANE_COMMENT, pcd->pane.id, &title);
378                 gtk_label_set_text(GTK_LABEL(pcd->pane.title), title);
379                 g_free(title);
380                 }
381         gtk_widget_set_size_request(pcd->widget, -1, pcd->height);
382         bar_update_expander(pane);
383         bar_pane_comment_update(pcd);
384 }
385
386 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */