Colorify cluster elements
[geeqie.git] / src / filecluster.c
1 /*
2  * Copyright (C) 2008 - 2016 The Geeqie Team
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 #include "filecluster.h"
20
21 #include "filedata.h"
22
23 static gboolean check_list_contains_sublist(GList *haystack, GList *needle)
24 {
25         // TODO(xsdg): Optimize this!  Sort, then scan?
26         GList *h_work, *n_work;
27         for (n_work = needle; n_work; n_work = n_work->next)
28         {
29                 gboolean found = FALSE;
30                 for (h_work = haystack; h_work; h_work = h_work->next)
31                 {
32                         if (n_work == h_work)
33                                 {
34                                 found = TRUE;
35                                 break;
36                                 }
37                 }
38
39                 if (!found) return FALSE;
40         }
41
42         return TRUE;
43 }
44
45 static gboolean filecluster_fd_equal(gconstpointer ptr_a, gconstpointer ptr_b)
46 {
47         // TODO(xsdg): Is there anything we can/should do to validate inputs?
48         FileData *fd_a = (FileData *)ptr_a;
49         FileData *fd_b = (FileData *)ptr_b;
50         return !filelist_sort_compare_filedata(fd_a, fd_b);
51 }
52
53 // TODO(xsdg): Move this into filedata.h
54 static guint filecluster_fd_hash(gconstpointer ptr)
55 {
56         if (!ptr) return 1;
57         FileData *fd = (FileData *)ptr;
58         return 7 * g_str_hash(fd->original_path);
59 }
60
61 FileClusterList *fileclusterlist_new()
62 {
63         FileClusterList *fcl = g_new0(FileClusterList, 1);
64         fcl->clusters = g_hash_table_new(&filecluster_fd_hash, &filecluster_fd_equal);
65 }
66
67 FileCluster *filecluster_new()
68 {
69         FileCluster *fc = g_new0(FileCluster, 1);
70         fc->show_children = FALSE;
71 }
72
73 void fileclusterlist_free(FileClusterList *fcl)
74 {
75         // TODO(xsdg): don't leak stuff
76         // if (fcl->fd_list) g_list_free_full(fcl->fd_list, (GDestroyNotify)&filecluster_free);
77         g_hash_table_destroy(fcl->clusters);
78         g_free(fcl);
79 }
80
81 void filecluster_free(FileCluster *fc)
82 {
83         filelist_free(fc->items);
84         g_free(fc);
85 }
86
87 gboolean filecluster_toggle_show_children(FileCluster *fc)
88 {
89         fc->show_children = !fc->show_children;
90         return fc->show_children;
91 }
92
93 FileCluster *fileclusterlist_create_cluster(FileClusterList *fcl, GList *fd_items)
94 {
95         GList *work;
96
97         // Check preconditions.
98         if (!fd_items) return NULL;
99         for (work = fd_items; work; work = work->next)
100                 {
101                 FileData *fd = work->data;
102                 if (g_hash_table_contains(fcl->clusters, fd))
103                         {
104                         // TODO(xsdg): Show this warning in the UI.
105                         g_warning("Tried to create a cluster with a file that is already clustered.");
106                         return NULL;
107                         }
108                 }
109
110         FileCluster *new_fc = filecluster_new();
111         new_fc->items = filelist_copy(fd_items);
112         new_fc->head = new_fc->items;
113
114         for (GList *item = new_fc->items; item; item = item->next)
115                 {
116                 FileData *fd = item->data;
117                 g_hash_table_insert(fcl->clusters, fd, new_fc);
118                 }
119
120         return new_fc;
121 }
122
123 gboolean filecluster_has_head(FileCluster *fc, FileData *fd)
124 {
125         if (!fd) return FALSE;
126         return filecluster_fd_equal(fc->head->data, fd);
127 }
128
129 gboolean filecluster_has_child(FileCluster *fc, FileData *fd)
130 {
131         if (!fd) return FALSE;
132         return !filecluster_fd_equal(fc->head->data, fd);
133 }
134
135 gboolean fileclusterlist_has_head(FileClusterList *fcl, FileData *fd)
136 {
137         FileCluster *fc = g_hash_table_lookup(fcl->clusters, fd);
138         if (!fc) return FALSE;
139         return filecluster_has_head(fc, fd);
140 }
141
142 gboolean fileclusterlist_has_child(FileClusterList *fcl, FileData *fd)
143 {
144         FileCluster *fc = g_hash_table_lookup(fcl->clusters, fd);
145         if (!fc) return FALSE;
146         return filecluster_has_child(fc, fd);
147 }
148
149 static gboolean fileclusterlist_should_hide(FileClusterList *fcl, FileData *fd)
150 {
151         FileCluster *fc = g_hash_table_lookup(fcl->clusters, fd);
152         if (!fc) return FALSE;
153         // Only difference vs. fileclusterlist_has_child.  Basically, if the node is a child, but
154         // we're showing children, then don't hide.
155         if (fc->show_children) return FALSE;
156         return filecluster_has_child(fc, fd);
157 }
158
159 // TODO(xsdg): pick a better name for this function
160 GList *fileclusterlist_next_non_child(FileClusterList *fcl, GList *list)
161 {
162         // Check for no-ops
163         if (!list || !g_hash_table_size(fcl->clusters)) return list;
164
165         // Clusters are being used, so we have to actually check things.
166         for (; list; list = list->next)
167         {
168                 FileData *fd = list->data;
169                 if (!fileclusterlist_has_child(fcl, fd)) return list;
170         }
171
172         return NULL;
173 }
174
175 GList *fileclusterlist_remove_children_from_list(FileClusterList *fcl, GList *list)
176 {
177         GList *work = list;
178
179         while (work)
180         {
181                 FileData *fd = work->data;
182                 GList *link = work;
183                 // Advance early in case link needs to be removed/freed.
184                 work = work->next;
185
186                 if (fileclusterlist_should_hide(fcl, fd))
187                 {
188                         list = g_list_remove_link(list, link);
189                         file_data_unref(fd);
190                         g_list_free(link);
191                 }
192         }
193
194         return list;
195 }