2 * e-table-extras.c - Set of hash table sort of thingies.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 * Chris Lahey <clahey@ximian.com>
20 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
33 #include "e-cell-checkbox.h"
34 #include "e-cell-date.h"
35 #include "e-cell-number.h"
36 #include "e-cell-pixbuf.h"
37 #include "e-cell-size.h"
38 #include "e-cell-text.h"
39 #include "e-cell-tree.h"
40 #include "e-table-extras.h"
41 #include "e-table-sorting-utils.h"
43 #define E_TABLE_EXTRAS_GET_PRIVATE(obj) \
44 (G_TYPE_INSTANCE_GET_PRIVATE \
45 ((obj), E_TYPE_TABLE_EXTRAS, ETableExtrasPrivate))
47 struct _ETableExtrasPrivate
{
50 GHashTable
*icon_names
;
60 ete_finalize (GObject
*object
)
62 ETableExtrasPrivate
*priv
;
64 priv
= E_TABLE_EXTRAS_GET_PRIVATE (object
);
67 g_hash_table_destroy (priv
->cells
);
72 g_hash_table_destroy (priv
->compares
);
73 priv
->compares
= NULL
;
77 g_hash_table_destroy (priv
->searches
);
78 priv
->searches
= NULL
;
81 if (priv
->icon_names
) {
82 g_hash_table_destroy (priv
->icon_names
);
83 priv
->icon_names
= NULL
;
86 G_OBJECT_CLASS (e_table_extras_parent_class
)->finalize (object
);
90 e_table_extras_class_init (ETableExtrasClass
*class)
92 GObjectClass
*object_class
;
94 g_type_class_add_private (class, sizeof (ETableExtrasPrivate
));
96 object_class
= G_OBJECT_CLASS (class);
97 object_class
->finalize
= ete_finalize
;
101 e_strint_compare (gconstpointer data1
,
104 gint int1
= atoi (data1
);
105 gint int2
= atoi (data2
);
107 return e_int_compare (GINT_TO_POINTER (int1
), GINT_TO_POINTER (int2
));
111 e_int64ptr_compare (gconstpointer data1
,
114 const gint64
*pa
= data1
, *pb
= data2
;
117 return (*pa
== *pb
) ? 0 : (*pa
< *pb
) ? -1 : 1;
119 /* sort unset values before set */
120 return (!pa
&& !pb
) ? 0 : (pa
? 1 : -1);
123 /* UTF-8 strncasecmp - not optimized */
126 g_utf8_strncasecmp (const gchar
*s1
,
132 g_return_val_if_fail (s1
!= NULL
&& g_utf8_validate (s1
, -1, NULL
), 0);
133 g_return_val_if_fail (s2
!= NULL
&& g_utf8_validate (s2
, -1, NULL
), 0);
135 while (n
&& *s1
&& *s2
)
140 c1
= g_unichar_tolower (g_utf8_get_char (s1
));
141 c2
= g_unichar_tolower (g_utf8_get_char (s2
));
143 /* Collation is locale-dependent, so this
144 * totally fails to do the right thing. */
146 return c1
< c2
? -1 : 1;
148 s1
= g_utf8_next_char (s1
);
149 s2
= g_utf8_next_char (s2
);
152 if (n
== 0 || (*s1
== '\0' && *s2
== '\0'))
159 e_string_search (gconstpointer haystack
,
163 if (haystack
== NULL
)
166 length
= g_utf8_strlen (needle
, -1);
167 if (g_utf8_strncasecmp (haystack
, needle
, length
) == 0)
174 e_table_str_case_compare (gconstpointer x
,
178 const gchar
*cx
= NULL
, *cy
= NULL
;
181 return e_str_case_compare (x
, y
);
183 if (x
== NULL
|| y
== NULL
) {
190 #define prepare_value(_z, _cz) \
191 _cz = e_table_sorting_utils_lookup_cmp_cache (cmp_cache, _z); \
193 gchar *tmp = g_utf8_casefold (_z, -1); \
194 _cz = g_utf8_collate_key (tmp, -1); \
197 e_table_sorting_utils_add_to_cmp_cache ( \
198 cmp_cache, _z, (gchar *) _cz); \
201 prepare_value (x
, cx
);
202 prepare_value (y
, cy
);
206 return strcmp (cx
, cy
);
210 e_table_collate_compare (gconstpointer x
,
214 const gchar
*cx
= NULL
, *cy
= NULL
;
217 return e_collate_compare (x
, y
);
219 if (x
== NULL
|| y
== NULL
) {
226 #define prepare_value(_z, _cz) \
227 _cz = e_table_sorting_utils_lookup_cmp_cache (cmp_cache, _z); \
229 _cz = g_utf8_collate_key (_z, -1); \
231 e_table_sorting_utils_add_to_cmp_cache ( \
232 cmp_cache, _z, (gchar *) _cz); \
235 prepare_value (x
, cx
);
236 prepare_value (y
, cy
);
240 return strcmp (cx
, cy
);
244 safe_unref (gpointer object
)
247 g_object_unref (object
);
251 e_table_extras_init (ETableExtras
*extras
)
253 ECell
*cell
, *sub_cell
;
255 extras
->priv
= E_TABLE_EXTRAS_GET_PRIVATE (extras
);
257 extras
->priv
->cells
= g_hash_table_new_full (
258 g_str_hash
, g_str_equal
,
259 (GDestroyNotify
) g_free
,
260 (GDestroyNotify
) safe_unref
);
262 extras
->priv
->compares
= g_hash_table_new_full (
263 g_str_hash
, g_str_equal
,
264 (GDestroyNotify
) g_free
,
265 (GDestroyNotify
) NULL
);
267 extras
->priv
->icon_names
= g_hash_table_new_full (
268 g_str_hash
, g_str_equal
,
269 (GDestroyNotify
) g_free
,
270 (GDestroyNotify
) g_free
);
272 extras
->priv
->searches
= g_hash_table_new_full (
273 g_str_hash
, g_str_equal
,
274 (GDestroyNotify
) g_free
,
275 (GDestroyNotify
) NULL
);
277 e_table_extras_add_compare (
279 (GCompareDataFunc
) e_str_compare
);
280 e_table_extras_add_compare (
281 extras
, "stringcase",
282 (GCompareDataFunc
) e_table_str_case_compare
);
283 e_table_extras_add_compare (
285 (GCompareDataFunc
) e_table_collate_compare
);
286 e_table_extras_add_compare (
288 (GCompareDataFunc
) e_int_compare
);
289 e_table_extras_add_compare (
290 extras
, "string-integer",
291 (GCompareDataFunc
) e_strint_compare
);
292 e_table_extras_add_compare (
293 extras
, "pointer-integer64",
294 (GCompareDataFunc
) e_int64ptr_compare
);
296 e_table_extras_add_search (extras
, "string", e_string_search
);
298 cell
= e_cell_checkbox_new ();
299 e_table_extras_add_cell (extras
, "checkbox", cell
);
300 g_object_unref (cell
);
302 cell
= e_cell_date_new (NULL
, GTK_JUSTIFY_LEFT
);
303 e_table_extras_add_cell (extras
, "date", cell
);
304 g_object_unref (cell
);
306 cell
= e_cell_number_new (NULL
, GTK_JUSTIFY_RIGHT
);
307 e_table_extras_add_cell (extras
, "number", cell
);
308 g_object_unref (cell
);
310 cell
= e_cell_pixbuf_new ();
311 e_table_extras_add_cell (extras
, "pixbuf", cell
);
312 g_object_unref (cell
);
314 cell
= e_cell_size_new (NULL
, GTK_JUSTIFY_RIGHT
);
315 e_table_extras_add_cell (extras
, "size", cell
);
316 g_object_unref (cell
);
318 cell
= e_cell_text_new (NULL
, GTK_JUSTIFY_LEFT
);
319 e_table_extras_add_cell (extras
, "string", cell
);
320 g_object_unref (cell
);
322 sub_cell
= e_cell_text_new (NULL
, GTK_JUSTIFY_LEFT
);
323 cell
= e_cell_tree_new (TRUE
, sub_cell
);
324 e_table_extras_add_cell (extras
, "tree-string", cell
);
325 g_object_unref (sub_cell
);
326 g_object_unref (cell
);
330 e_table_extras_new (void)
332 return g_object_new (E_TYPE_TABLE_EXTRAS
, NULL
);
336 e_table_extras_add_cell (ETableExtras
*extras
,
340 g_return_if_fail (E_IS_TABLE_EXTRAS (extras
));
341 g_return_if_fail (id
!= NULL
);
344 g_object_ref_sink (cell
);
346 g_hash_table_insert (extras
->priv
->cells
, g_strdup (id
), cell
);
350 e_table_extras_get_cell (ETableExtras
*extras
,
353 g_return_val_if_fail (E_IS_TABLE_EXTRAS (extras
), NULL
);
354 g_return_val_if_fail (id
!= NULL
, NULL
);
356 return g_hash_table_lookup (extras
->priv
->cells
, id
);
360 e_table_extras_add_compare (ETableExtras
*extras
,
362 GCompareDataFunc compare
)
364 g_return_if_fail (E_IS_TABLE_EXTRAS (extras
));
365 g_return_if_fail (id
!= NULL
);
367 g_hash_table_insert (
368 extras
->priv
->compares
,
369 g_strdup (id
), (gpointer
) compare
);
373 e_table_extras_get_compare (ETableExtras
*extras
,
376 g_return_val_if_fail (E_IS_TABLE_EXTRAS (extras
), NULL
);
377 g_return_val_if_fail (id
!= NULL
, NULL
);
379 return g_hash_table_lookup (extras
->priv
->compares
, id
);
383 e_table_extras_add_search (ETableExtras
*extras
,
385 ETableSearchFunc search
)
387 g_return_if_fail (E_IS_TABLE_EXTRAS (extras
));
388 g_return_if_fail (id
!= NULL
);
390 g_hash_table_insert (
391 extras
->priv
->searches
,
392 g_strdup (id
), (gpointer
) search
);
396 e_table_extras_get_search (ETableExtras
*extras
,
399 g_return_val_if_fail (E_IS_TABLE_EXTRAS (extras
), NULL
);
400 g_return_val_if_fail (id
!= NULL
, NULL
);
402 return g_hash_table_lookup (extras
->priv
->searches
, id
);
406 e_table_extras_add_icon_name (ETableExtras
*extras
,
408 const gchar
*icon_name
)
410 g_return_if_fail (E_IS_TABLE_EXTRAS (extras
));
411 g_return_if_fail (id
!= NULL
);
413 g_hash_table_insert (
414 extras
->priv
->icon_names
,
415 g_strdup (id
), g_strdup (icon_name
));
419 e_table_extras_get_icon_name (ETableExtras
*extras
,
422 g_return_val_if_fail (E_IS_TABLE_EXTRAS (extras
), NULL
);
423 g_return_val_if_fail (id
!= NULL
, NULL
);
425 return g_hash_table_lookup (extras
->priv
->icon_names
, id
);