fb18fb16faf3f3d3f48975995c4f716b45791ccf
[geeqie.git] / src / pan-view / pan-view-filter.c
1 /*
2  * Copyright (C) 2006 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 "pan-view-filter.h"
23
24 #include "image.h"
25 #include "metadata.h"
26 #include "pan-item.h"
27 #include "pan-util.h"
28 #include "pan-view.h"
29 #include "ui_fileops.h"
30 #include "ui_tabcomp.h"
31 #include "ui_misc.h"
32
33 PanViewFilterUi *pan_filter_ui_new(PanWindow *pw)
34 {
35         PanViewFilterUi *ui = g_new0(PanViewFilterUi, 1);
36         GtkWidget *combo;
37         GtkWidget *hbox;
38         gint i;
39
40         /* Since we're using the GHashTable as a HashSet (in which key and value pointers
41          * are always identical), specifying key _and_ value destructor callbacks will
42          * cause a double-free.
43          */
44         {
45                 GtkTreeIter iter;
46                 ui->filter_mode_model = gtk_list_store_new(3, G_TYPE_INT, G_TYPE_STRING, G_TYPE_STRING);
47                 gtk_list_store_append(ui->filter_mode_model, &iter);
48                 gtk_list_store_set(ui->filter_mode_model, &iter,
49                                    0, PAN_VIEW_FILTER_REQUIRE, 1, _("Require"), 2, _("R"), -1);
50                 gtk_list_store_append(ui->filter_mode_model, &iter);
51                 gtk_list_store_set(ui->filter_mode_model, &iter,
52                                    0, PAN_VIEW_FILTER_EXCLUDE, 1, _("Exclude"), 2, _("E"), -1);
53                 gtk_list_store_append(ui->filter_mode_model, &iter);
54                 gtk_list_store_set(ui->filter_mode_model, &iter,
55                                    0, PAN_VIEW_FILTER_INCLUDE, 1, _("Include"), 2, _("I"), -1);
56                 gtk_list_store_append(ui->filter_mode_model, &iter);
57                 gtk_list_store_set(ui->filter_mode_model, &iter,
58                                    0, PAN_VIEW_FILTER_GROUP, 1, _("Group"), 2, _("G"), -1);
59
60                 ui->filter_mode_combo = gtk_combo_box_new_with_model(GTK_TREE_MODEL(ui->filter_mode_model));
61                 gtk_combo_box_set_focus_on_click(GTK_COMBO_BOX(ui->filter_mode_combo), FALSE);
62                 gtk_combo_box_set_active(GTK_COMBO_BOX(ui->filter_mode_combo), 0);
63
64                 GtkCellRenderer *render = gtk_cell_renderer_text_new();
65                 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(ui->filter_mode_combo), render, TRUE);
66                 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(ui->filter_mode_combo), render, "text", 1, NULL);
67         }
68
69         // Build the actual filter UI.
70         ui->filter_box = gtk_hbox_new(FALSE, PREF_PAD_SPACE);
71         pref_spacer(ui->filter_box, 0);
72         pref_label_new(ui->filter_box, _("Keyword Filter:"));
73
74         gtk_box_pack_start(GTK_BOX(ui->filter_box), ui->filter_mode_combo, FALSE, FALSE, 0);
75         gtk_widget_show(ui->filter_mode_combo);
76
77         hbox = gtk_hbox_new(TRUE, PREF_PAD_SPACE);
78         gtk_box_pack_start(GTK_BOX(ui->filter_box), hbox, TRUE, TRUE, 0);
79         gtk_widget_show(hbox);
80
81         combo = tab_completion_new_with_history(&ui->filter_entry, "", "pan_view_filter", -1,
82                                                 pan_filter_activate_cb, pw);
83         gtk_box_pack_start(GTK_BOX(hbox), combo, TRUE, TRUE, 0);
84         gtk_widget_show(combo);
85
86         // TODO(xsdg): Figure out whether it's useful to keep this label around.
87         ui->filter_label = gtk_label_new("");
88         //gtk_box_pack_start(GTK_BOX(hbox), ui->filter_label, FALSE, FALSE, 0);
89         //gtk_widget_show(ui->filter_label);
90
91         ui->filter_kw_hbox = gtk_hbox_new(FALSE, PREF_PAD_SPACE);
92         gtk_box_pack_start(GTK_BOX(hbox), ui->filter_kw_hbox, TRUE, TRUE, 0);
93         gtk_widget_show(ui->filter_kw_hbox);
94
95         // Build the spin-button to show/hide the filter UI.
96         ui->filter_button = gtk_toggle_button_new();
97         gtk_button_set_relief(GTK_BUTTON(ui->filter_button), GTK_RELIEF_NONE);
98         gtk_button_set_focus_on_click(GTK_BUTTON(ui->filter_button), FALSE);
99         hbox = gtk_hbox_new(FALSE, PREF_PAD_GAP);
100         gtk_container_add(GTK_CONTAINER(ui->filter_button), hbox);
101         gtk_widget_show(hbox);
102         ui->filter_button_arrow = gtk_arrow_new(GTK_ARROW_UP, GTK_SHADOW_NONE);
103         gtk_box_pack_start(GTK_BOX(hbox), ui->filter_button_arrow, FALSE, FALSE, 0);
104         gtk_widget_show(ui->filter_button_arrow);
105         pref_label_new(hbox, _("Filter"));
106
107         g_signal_connect(G_OBJECT(ui->filter_button), "clicked",
108                          G_CALLBACK(pan_filter_toggle_cb), pw);
109
110         // Add check buttons for filtering by image class
111         for (i = 0; i < FILE_FORMAT_CLASSES; i++)
112         {
113                 ui->filter_check_buttons[i] = gtk_check_button_new_with_label(_(format_class_list[i]));
114                 gtk_box_pack_start(GTK_BOX(ui->filter_box), ui->filter_check_buttons[i], FALSE, FALSE, 0);
115                 gtk_widget_show(ui->filter_check_buttons[i]);
116         }
117
118         gtk_toggle_button_set_active((GtkToggleButton *)ui->filter_check_buttons[FORMAT_CLASS_IMAGE], TRUE);
119         gtk_toggle_button_set_active((GtkToggleButton *)ui->filter_check_buttons[FORMAT_CLASS_RAWIMAGE], TRUE);
120         gtk_toggle_button_set_active((GtkToggleButton *)ui->filter_check_buttons[FORMAT_CLASS_VIDEO], TRUE);
121         ui->filter_classes = (1 << FORMAT_CLASS_IMAGE) | (1 << FORMAT_CLASS_RAWIMAGE) | (1 << FORMAT_CLASS_VIDEO);
122
123         // Connecting the signal before setting the state causes segfault as pw is not yet prepared
124         for (i = 0; i < FILE_FORMAT_CLASSES; i++)
125                 g_signal_connect((GtkToggleButton *)(ui->filter_check_buttons[i]), "toggled", G_CALLBACK(pan_filter_toggle_button_cb), pw);
126
127         return ui;
128 }
129
130 void pan_filter_ui_destroy(PanViewFilterUi **ui_ptr)
131 {
132         if (ui_ptr == NULL || *ui_ptr == NULL) return;
133
134         // Note that g_clear_pointer handles already-NULL pointers.
135         //g_clear_pointer(&(*ui_ptr)->filter_kw_table, g_hash_table_destroy);
136
137         g_free(*ui_ptr);
138         *ui_ptr = NULL;
139 }
140
141 static void pan_filter_status(PanWindow *pw, const gchar *text)
142 {
143         gtk_label_set_text(GTK_LABEL(pw->filter_ui->filter_label), (text) ? text : "");
144 }
145
146 static void pan_filter_kw_button_cb(GtkButton *widget, gpointer data)
147 {
148         PanFilterCallbackState *cb_state = data;
149         PanWindow *pw = cb_state->pw;
150         PanViewFilterUi *ui = pw->filter_ui;
151
152         // TODO(xsdg): Fix filter element pointed object memory leak.
153         ui->filter_elements = g_list_delete_link(ui->filter_elements, cb_state->filter_element);
154         gtk_widget_destroy(GTK_WIDGET(widget));
155         g_free(cb_state);
156
157         pan_filter_status(pw, _("Removed keyword…"));
158         pan_layout_update(pw);
159 }
160
161 void pan_filter_activate_cb(const gchar *text, gpointer data)
162 {
163         GtkWidget *kw_button;
164         PanWindow *pw = data;
165         PanViewFilterUi *ui = pw->filter_ui;
166         GtkTreeIter iter;
167
168         if (!text) return;
169
170         // Get all relevant state and reset UI.
171         gtk_combo_box_get_active_iter(GTK_COMBO_BOX(ui->filter_mode_combo), &iter);
172         gtk_entry_set_text(GTK_ENTRY(ui->filter_entry), "");
173         tab_completion_append_to_history(ui->filter_entry, text);
174
175         // Add new filter element.
176         PanViewFilterElement *element = g_new0(PanViewFilterElement, 1);
177         gtk_tree_model_get(GTK_TREE_MODEL(ui->filter_mode_model), &iter, 0, &element->mode, -1);
178         element->keyword = g_strdup(text);
179         if (g_strcmp0(text, g_regex_escape_string(text, -1)))
180                 {
181                 // It's an actual regex, so compile
182                 element->kw_regex = g_regex_new(text, G_REGEX_ANCHORED | G_REGEX_OPTIMIZE, G_REGEX_MATCH_ANCHORED, NULL);
183                 }
184         ui->filter_elements = g_list_append(ui->filter_elements, element);
185
186         // Get the short version of the mode value.
187         gchar *short_mode;
188         gtk_tree_model_get(GTK_TREE_MODEL(ui->filter_mode_model), &iter, 2, &short_mode, -1);
189
190         // Create the button.
191         // TODO(xsdg): Use MVC so that the button list is an actual representation of the GList
192         gchar *label = g_strdup_printf("(%s) %s", short_mode, text);
193         kw_button = gtk_button_new_with_label(label);
194         g_clear_pointer(&label, g_free);
195
196         gtk_box_pack_start(GTK_BOX(ui->filter_kw_hbox), kw_button, FALSE, FALSE, 0);
197         gtk_widget_show(kw_button);
198
199         PanFilterCallbackState *cb_state = g_new0(PanFilterCallbackState, 1);
200         cb_state->pw = pw;
201         cb_state->filter_element = g_list_last(ui->filter_elements);
202
203         g_signal_connect(G_OBJECT(kw_button), "clicked",
204                          G_CALLBACK(pan_filter_kw_button_cb), cb_state);
205
206         pan_layout_update(pw);
207 }
208
209 void pan_filter_activate(PanWindow *pw)
210 {
211         gchar *text;
212
213         text = g_strdup(gtk_entry_get_text(GTK_ENTRY(pw->filter_ui->filter_entry)));
214         pan_filter_activate_cb(text, pw);
215         g_free(text);
216 }
217
218 void pan_filter_toggle_cb(GtkWidget *button, gpointer data)
219 {
220         PanWindow *pw = data;
221         PanViewFilterUi *ui = pw->filter_ui;
222         gboolean visible;
223
224         visible = gtk_widget_get_visible(ui->filter_box);
225         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button)) == visible) return;
226
227         if (visible)
228                 {
229                 gtk_widget_hide(ui->filter_box);
230                 gtk_arrow_set(GTK_ARROW(ui->filter_button_arrow), GTK_ARROW_UP, GTK_SHADOW_NONE);
231                 }
232         else
233                 {
234                 gtk_widget_show(ui->filter_box);
235                 gtk_arrow_set(GTK_ARROW(ui->filter_button_arrow), GTK_ARROW_DOWN, GTK_SHADOW_NONE);
236                 gtk_widget_grab_focus(ui->filter_entry);
237                 }
238 }
239
240 void pan_filter_toggle_visible(PanWindow *pw, gboolean enable)
241 {
242         PanViewFilterUi *ui = pw->filter_ui;
243         if (pw->fs) return;
244
245         if (enable)
246                 {
247                 if (gtk_widget_get_visible(ui->filter_box))
248                         {
249                         gtk_widget_grab_focus(ui->filter_entry);
250                         }
251                 else
252                         {
253                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ui->filter_button), TRUE);
254                         }
255                 }
256         else
257                 {
258                 if (gtk_widget_get_visible(ui->filter_entry))
259                         {
260                         if (gtk_widget_has_focus(ui->filter_entry))
261                                 {
262                                 gtk_widget_grab_focus(GTK_WIDGET(pw->imd->widget));
263                                 }
264                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ui->filter_button), FALSE);
265                         }
266                 }
267 }
268
269 void pan_filter_toggle_button_cb(GtkWidget *button, gpointer data)
270 {
271         PanWindow *pw = data;
272         PanViewFilterUi *ui = pw->filter_ui;
273
274         gint old_classes = ui->filter_classes;
275         ui->filter_classes = 0;
276
277         for (gint i = 0; i < FILE_FORMAT_CLASSES; i++)
278         {
279                 ui->filter_classes |= gtk_toggle_button_get_active((GtkToggleButton *)ui->filter_check_buttons[i]) ? 1 << i : 0;
280         }
281
282         if (ui->filter_classes != old_classes) 
283                 pan_layout_update(pw);
284 }
285
286 static gboolean pan_view_list_contains_kw_pattern(GList *haystack, PanViewFilterElement *filter, gchar **found_kw)
287 {
288         if (filter->kw_regex)
289                 {
290                 // regex compile succeeded; attempt regex match.
291                 GList *work = g_list_first(haystack);
292                 while (work)
293                         {
294                         gchar *keyword = work->data;
295                         work = work->next;
296                         if (g_regex_match(filter->kw_regex, keyword, 0x0, NULL))
297                                 {
298                                 if (found_kw) *found_kw = keyword;
299                                 return TRUE;
300                                 }
301                         }
302                 return FALSE;
303                 }
304         else
305                 {
306                 // regex compile failed; fall back to exact string match.
307                 GList *found_elem = g_list_find_custom(haystack, filter->keyword, (GCompareFunc)g_strcmp0);
308                 if (found_elem && found_kw) *found_kw = found_elem->data;
309                 return !!found_elem;
310                 }
311 }
312
313 gboolean pan_filter_fd_list(GList **fd_list, GList *filter_elements, gint filter_classes)
314 {
315         GList *work;
316         gboolean modified = FALSE;
317         GHashTable *seen_kw_table = NULL;
318
319         if (!fd_list || !*fd_list) return modified;
320
321         // seen_kw_table is only valid in this scope, so don't take ownership of any strings.
322         if (filter_elements)
323                 seen_kw_table = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, NULL);
324
325         work = *fd_list;
326         while (work)
327                 {
328                 FileData *fd = work->data;
329                 GList *last_work = work;
330                 work = work->next;
331
332                 gboolean should_reject = FALSE;
333                 gchar *group_kw = NULL;
334
335                 if (!((1 << fd -> format_class) & filter_classes))
336                         {
337                         should_reject = TRUE;
338                         }
339                 else if (filter_elements)
340                         {
341                         // TODO(xsdg): OPTIMIZATION Do the search inside of metadata.c to avoid a
342                         // bunch of string list copies.
343                         GList *img_keywords = metadata_read_list(fd, KEYWORD_KEY, METADATA_PLAIN);
344
345                         // TODO(xsdg): OPTIMIZATION Determine a heuristic for when to linear-search the
346                         // keywords list, and when to build a hash table for the image's keywords.
347                         GList *filter_element = filter_elements;
348
349                         while (filter_element)
350                                 {
351                                 PanViewFilterElement *filter = filter_element->data;
352                                 filter_element = filter_element->next;
353                                 gchar *found_kw = NULL;
354                                 gboolean has_kw = pan_view_list_contains_kw_pattern(img_keywords, filter, &found_kw);
355
356                                 switch (filter->mode)
357                                         {
358                                         case PAN_VIEW_FILTER_REQUIRE:
359                                                 should_reject |= !has_kw;
360                                                 break;
361                                         case PAN_VIEW_FILTER_EXCLUDE:
362                                                 should_reject |= has_kw;
363                                                 break;
364                                         case PAN_VIEW_FILTER_INCLUDE:
365                                                 if (has_kw) should_reject = FALSE;
366                                                 break;
367                                         case PAN_VIEW_FILTER_GROUP:
368                                                 if (has_kw)
369                                                         {
370                                                         if (g_hash_table_contains(seen_kw_table, found_kw))
371                                                                 {
372                                                                 should_reject = TRUE;
373                                                                 }
374                                                         else if (group_kw == NULL)
375                                                                 {
376                                                                 group_kw = found_kw;
377                                                                 }
378                                                         }
379                                                 break;
380                                         }
381                                 }
382                         string_list_free(img_keywords);
383                         if (!should_reject && group_kw != NULL) g_hash_table_add(seen_kw_table, group_kw);
384                         group_kw = NULL;  // group_kw references an item from img_keywords.
385                         }
386
387                 if (should_reject)
388                         {
389                         *fd_list = g_list_delete_link(*fd_list, last_work);
390                         modified = TRUE;
391                         }
392                 }
393
394         if (filter_elements)
395                 g_hash_table_destroy(seen_kw_table);
396
397         return modified;
398 }