1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/*
* Copyright (C) 2000 Eazel, Inc
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* Author: Maciej Stachowiak <mjs@eazel.com>
*/
/* nautilus-tree-expansion-state.c - class to track expansion state for the tree view */
#include <config.h>
#include "nautilus-tree-expansion-state.h"
#include <eel/eel-glib-extensions.h>
#include <eel/eel-gtk-macros.h>
#include <eel/eel-string.h>
#include <libnautilus-private/nautilus-global-preferences.h>
struct NautilusTreeExpansionStateDetails {
GHashTable *table;
GHashTable *ever_expanded_table;
};
static void nautilus_tree_expansion_state_destroy (GtkObject *object);
static void nautilus_tree_expansion_state_initialize (gpointer object,
gpointer klass);
static void nautilus_tree_expansion_state_initialize_class (gpointer klass);
EEL_DEFINE_CLASS_BOILERPLATE (NautilusTreeExpansionState, nautilus_tree_expansion_state, GTK_TYPE_OBJECT)
static gboolean expansion_table_hash_remove_func (gpointer key,
gpointer value,
gpointer user_data);
static void nautilus_tree_expansion_state_expand_node_internal (NautilusTreeExpansionState *expansion_state,
const char *uri);
/* infrastructure stuff */
static void
nautilus_tree_expansion_state_initialize_class (gpointer klass)
{
GtkObjectClass *object_class;
object_class = GTK_OBJECT_CLASS (klass);
object_class->destroy = nautilus_tree_expansion_state_destroy;
}
/* Preference name for the list of expanded uris */
#define NAUTILUS_PREFERENCES_TREE_VIEW_EXPANSION_STATE "tree-sidebar-panel/expansion_state"
static void
expansion_state_load_callback (const char *uri,
gpointer callback_data)
{
g_return_if_fail (eel_strlen (uri) > 0);
g_return_if_fail (NAUTILUS_IS_TREE_EXPANSION_STATE (callback_data));
nautilus_tree_expansion_state_expand_node_internal
(callback_data, uri);
}
static void
nautilus_tree_expansion_state_load_from_preferences (NautilusTreeExpansionState *expansion_state)
{
EelStringList *uris;
uris = eel_preferences_get_string_list (NAUTILUS_PREFERENCES_TREE_VIEW_EXPANSION_STATE);
eel_string_list_for_each (uris, expansion_state_load_callback, expansion_state);
eel_string_list_free (uris);
}
static void
hash_table_get_keys_callback (gpointer key,
gpointer value,
gpointer user_data)
{
GList **keys;
keys = (GList **) user_data;
*keys = g_list_prepend (*keys, key);
}
static GList *
hash_table_get_keys (GHashTable *hash_table)
{
GList *keys;
keys = NULL;
g_hash_table_foreach (hash_table,
hash_table_get_keys_callback,
&keys);
return keys;
}
static void
nautilus_tree_expansion_state_save_to_preferences (NautilusTreeExpansionState *expansion_state)
{
GList *glist;
EelStringList *uris;
glist = hash_table_get_keys (expansion_state->details->table);
glist = eel_g_str_list_alphabetize (glist);
uris = eel_string_list_new_from_g_list (glist, TRUE);
eel_preferences_set_string_list (NAUTILUS_PREFERENCES_TREE_VIEW_EXPANSION_STATE, uris);
g_list_free (glist);
eel_string_list_free (uris);
}
static void
nautilus_tree_expansion_state_initialize (gpointer object, gpointer klass)
{
NautilusTreeExpansionState *expansion_state;
expansion_state = NAUTILUS_TREE_EXPANSION_STATE (object);
expansion_state->details = g_new0 (NautilusTreeExpansionStateDetails, 1);
expansion_state->details->table = g_hash_table_new (g_str_hash, g_str_equal);
expansion_state->details->ever_expanded_table = g_hash_table_new (g_str_hash, g_str_equal);
nautilus_tree_expansion_state_load_from_preferences (expansion_state);
}
static void
nautilus_tree_expansion_state_destroy (GtkObject *object)
{
NautilusTreeExpansionState *expansion_state;
expansion_state = (NautilusTreeExpansionState *) object;
g_hash_table_foreach_remove (expansion_state->details->table,
expansion_table_hash_remove_func,
NULL);
g_hash_table_destroy (expansion_state->details->table);
g_hash_table_foreach_remove (expansion_state->details->ever_expanded_table,
expansion_table_hash_remove_func,
NULL);
g_hash_table_destroy (expansion_state->details->ever_expanded_table);
g_free (expansion_state->details);
EEL_CALL_PARENT (GTK_OBJECT_CLASS, destroy, (object));
}
NautilusTreeExpansionState *
nautilus_tree_expansion_state_new (void)
{
NautilusTreeExpansionState *state;
state = NAUTILUS_TREE_EXPANSION_STATE (gtk_object_new (NAUTILUS_TYPE_TREE_EXPANSION_STATE, NULL));
gtk_object_ref (GTK_OBJECT (state));
gtk_object_sink (GTK_OBJECT (state));
return state;
}
gboolean
nautilus_tree_expansion_state_is_node_expanded (NautilusTreeExpansionState *expansion_state,
const char *uri)
{
return (g_hash_table_lookup (expansion_state->details->table,
uri) != NULL);
}
gboolean
nautilus_tree_expansion_state_was_ever_expanded (NautilusTreeExpansionState *expansion_state,
const char *uri)
{
return (g_hash_table_lookup (expansion_state->details->ever_expanded_table,
uri) != NULL);
}
static void
nautilus_tree_expansion_state_expand_node_internal (NautilusTreeExpansionState *expansion_state,
const char *uri)
{
gpointer orig_key;
gpointer value;
if (!g_hash_table_lookup_extended (expansion_state->details->table,
uri,
&orig_key,
&value)) {
g_hash_table_insert (expansion_state->details->table,
g_strdup (uri),
GINT_TO_POINTER (1));
}
}
void
nautilus_tree_expansion_state_expand_node (NautilusTreeExpansionState *expansion_state,
const char *uri)
{
gpointer orig_key;
gpointer value;
nautilus_tree_expansion_state_expand_node_internal (expansion_state,
uri);
if (!g_hash_table_lookup_extended (expansion_state->details->ever_expanded_table,
uri,
&orig_key,
&value)) {
g_hash_table_insert (expansion_state->details->ever_expanded_table,
g_strdup (uri),
GINT_TO_POINTER (1));
}
}
/* We only want to remember expanded nodes. */
void
nautilus_tree_expansion_state_collapse_node (NautilusTreeExpansionState *expansion_state,
const char *uri)
{
nautilus_tree_expansion_state_remove_node (expansion_state, uri);
}
void
nautilus_tree_expansion_state_remove_node (NautilusTreeExpansionState *expansion_state,
const char *uri)
{
gpointer orig_key;
gpointer value;
if (g_hash_table_lookup_extended (expansion_state->details->table,
uri,
&orig_key,
&value)) {
g_hash_table_remove (expansion_state->details->table,
uri);
g_free (orig_key);
}
}
void
nautilus_tree_expansion_state_save (NautilusTreeExpansionState *expansion_state)
{
nautilus_tree_expansion_state_save_to_preferences (expansion_state);
}
static gboolean
expansion_table_hash_remove_func (gpointer key,
gpointer value,
gpointer user_data)
{
g_free (key);
return TRUE;
}
|