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