add support for Ayatana indicator to Notification plugin
[claws.git] / src / common / stringtable.c
blob5a4a3251b8370edfcab37771e6aee636f0eb5188
1 /*
2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2012 Hiroyuki Yamamoto and the Claws Mail team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "config.h"
22 #include <glib.h>
23 #include <string.h>
25 #include "stringtable.h"
26 #include "utils.h"
28 /* alfons - hashed string table (I wasn't content with GStringChunk;
29 * can't recall why :-) */
31 #if 0
32 #define XXX_DEBUG \
33 debug_print
34 #else
35 #define XXX_DEBUG \
36 if (0) debug_print
37 #endif
39 typedef struct StringEntry_ {
40 gint ref_count;
41 gchar *string;
42 } StringEntry;
44 static StringEntry *string_entry_new(const gchar *str)
46 StringEntry *entry;
48 entry = g_new0(StringEntry, 1);
49 entry->ref_count = 1;
50 entry->string = g_strdup(str);
51 return entry;
54 static void string_entry_free(StringEntry *entry)
56 cm_return_if_fail(entry != NULL);
58 g_free(entry->string);
59 g_free(entry);
62 StringTable *string_table_new(void)
64 StringTable *strtable;
66 strtable = g_new0(StringTable, 1);
67 cm_return_val_if_fail(strtable != NULL, NULL);
68 strtable->hash_table = g_hash_table_new(g_str_hash, g_str_equal);
69 if (strtable->hash_table == NULL) {
70 g_free(strtable);
71 return NULL;
73 return strtable;
76 gchar *string_table_insert_string(StringTable *table, const gchar *str)
78 StringEntry *entry;
80 entry = g_hash_table_lookup(table->hash_table, str);
82 if (entry) {
83 entry->ref_count++;
84 XXX_DEBUG ("ref++ for %s (%d)\n", entry->string,
85 entry->ref_count);
86 } else {
87 entry = string_entry_new(str);
88 XXX_DEBUG ("inserting %s\n", str);
89 /* insert entry->string instead of str, since it can be
90 * invalid pointer after this. */
91 g_hash_table_insert(table->hash_table, entry->string, entry);
94 return entry->string;
97 void string_table_free_string(StringTable *table, const gchar *str)
99 StringEntry *entry;
101 entry = g_hash_table_lookup(table->hash_table, str);
103 if (entry) {
104 entry->ref_count--;
105 if (entry->ref_count <= 0) {
106 XXX_DEBUG ("refcount of string %s dropped to zero\n",
107 entry->string);
108 g_hash_table_remove(table->hash_table, str);
109 string_entry_free(entry);
110 } else {
111 XXX_DEBUG ("ref-- for %s (%d)\n", entry->string,
112 entry->ref_count);
117 static gboolean string_table_remove_for_each_fn(gchar *key, StringEntry *entry,
118 gpointer user_data)
120 cm_return_val_if_fail(key != NULL, TRUE);
121 cm_return_val_if_fail(entry != NULL, TRUE);
123 string_entry_free(entry);
125 return TRUE;
128 void string_table_free(StringTable *table)
130 cm_return_if_fail(table != NULL);
131 cm_return_if_fail(table->hash_table != NULL);
133 g_hash_table_foreach_remove(table->hash_table,
134 (GHRFunc)string_table_remove_for_each_fn,
135 NULL);
136 g_hash_table_destroy(table->hash_table);
137 g_free(table);
140 static void string_table_stats_for_each_fn(gchar *key, StringEntry *entry,
141 guint *totals)
143 if (entry->ref_count > 1) {
144 *totals += strlen(key) * (entry->ref_count - 1);
148 void string_table_get_stats(StringTable *table)
150 guint totals = 0;
152 g_hash_table_foreach(table->hash_table,
153 (GHFunc)string_table_stats_for_each_fn, &totals);
154 XXX_DEBUG ("TOTAL UNSPILLED %d (%dK)\n", totals, totals / 1024);