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