1 /*
2  * This program is free software; you can redistribute it and/or modify it
3  * under the terms of the GNU Lesser General Public License as published by
4  * the Free Software Foundation.
5  *
6  * This program is distributed in the hope that it will be useful, but
7  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
8  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9  * for more details.
10  *
11  * You should have received a copy of the GNU Lesser General Public License
12  * along with this program; if not, see <http://www.gnu.org/licenses/>.
13  *
14  *
15  * Authors:
16  *		Chris Lahey <clahey@ximian.com>
17  *
18  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
19  *
20  */
21 
22 #include "evolution-config.h"
23 
24 #include <string.h>
25 
26 #include <gdk/gdkkeysyms.h>
27 
28 #include <glib/gi18n.h>
29 
30 #include "e-table-selection-model.h"
31 
32 G_DEFINE_TYPE (
33 	ETableSelectionModel,
34 	e_table_selection_model,
35 	E_TYPE_SELECTION_MODEL_ARRAY)
36 
37 static gint etsm_get_row_count (ESelectionModelArray *esm);
38 
39 enum {
40 	PROP_0,
41 	PROP_MODEL,
42 	PROP_HEADER
43 };
44 
45 static void
save_to_hash(gint model_row,gpointer closure)46 save_to_hash (gint model_row,
47               gpointer closure)
48 {
49 	ETableSelectionModel *etsm = closure;
50 	const gchar *key = e_table_model_get_save_id (etsm->model, model_row);
51 
52 	g_hash_table_insert (etsm->hash, (gpointer) key, (gpointer) key);
53 }
54 
55 static void
free_hash(ETableSelectionModel * etsm)56 free_hash (ETableSelectionModel *etsm)
57 {
58 	g_clear_pointer (&etsm->hash, g_hash_table_destroy);
59 	g_clear_pointer (&etsm->cursor_id, g_free);
60 }
61 
62 static void
model_pre_change(ETableModel * etm,ETableSelectionModel * etsm)63 model_pre_change (ETableModel *etm,
64                   ETableSelectionModel *etsm)
65 {
66 	free_hash (etsm);
67 
68 	if (etsm->model && e_table_model_has_save_id (etsm->model)) {
69 		gint cursor_row;
70 
71 		etsm->hash = g_hash_table_new_full (
72 			g_str_hash, g_str_equal,
73 			(GDestroyNotify) g_free,
74 			(GDestroyNotify) NULL);
75 		e_selection_model_foreach (E_SELECTION_MODEL (etsm), save_to_hash, etsm);
76 
77 		g_object_get (
78 			etsm,
79 			"cursor_row", &cursor_row,
80 			NULL);
81 		g_free (etsm->cursor_id);
82 		if (cursor_row != -1)
83 			etsm->cursor_id = e_table_model_get_save_id (etm, cursor_row);
84 		else
85 			etsm->cursor_id = NULL;
86 	}
87 }
88 
89 static gint
model_changed_idle(ETableSelectionModel * etsm)90 model_changed_idle (ETableSelectionModel *etsm)
91 {
92 	ETableModel *etm = etsm->model;
93 
94 	e_selection_model_clear (E_SELECTION_MODEL (etsm));
95 
96 	if (etsm->cursor_id && etm && e_table_model_has_save_id (etm)) {
97 		gint row_count = e_table_model_row_count (etm);
98 		gint cursor_row = -1;
99 		gint cursor_col = -1;
100 		gint i;
101 		e_selection_model_array_confirm_row_count (E_SELECTION_MODEL_ARRAY (etsm));
102 		for (i = 0; i < row_count; i++) {
103 			gchar *save_id = e_table_model_get_save_id (etm, i);
104 			if (g_hash_table_lookup (etsm->hash, save_id))
105 				e_selection_model_change_one_row (E_SELECTION_MODEL (etsm), i, TRUE);
106 
107 			if (etsm->cursor_id && !strcmp (etsm->cursor_id, save_id)) {
108 				cursor_row = i;
109 				cursor_col = e_selection_model_cursor_col (E_SELECTION_MODEL (etsm));
110 				if (cursor_col == -1) {
111 					if (etsm->eth) {
112 						cursor_col = e_table_header_prioritized_column (etsm->eth);
113 					} else
114 						cursor_col = 0;
115 				}
116 				e_selection_model_change_cursor (E_SELECTION_MODEL (etsm), cursor_row, cursor_col);
117 				g_free (etsm->cursor_id);
118 				etsm->cursor_id = NULL;
119 			}
120 			g_free (save_id);
121 		}
122 		free_hash (etsm);
123 		e_selection_model_selection_changed (E_SELECTION_MODEL (etsm));
124 		e_selection_model_cursor_changed (E_SELECTION_MODEL (etsm), cursor_row, cursor_col);
125 	}
126 	etsm->model_changed_idle_id = 0;
127 	return FALSE;
128 }
129 
130 static void
model_changed(ETableModel * etm,ETableSelectionModel * etsm)131 model_changed (ETableModel *etm,
132                ETableSelectionModel *etsm)
133 {
134 	e_selection_model_clear (E_SELECTION_MODEL (etsm));
135 	if (!etsm->model_changed_idle_id && etm && e_table_model_has_save_id (etm)) {
136 		etsm->model_changed_idle_id = g_idle_add_full (G_PRIORITY_HIGH, (GSourceFunc) model_changed_idle, etsm, NULL);
137 	}
138 }
139 
140 static void
model_row_changed(ETableModel * etm,gint row,ETableSelectionModel * etsm)141 model_row_changed (ETableModel *etm,
142                    gint row,
143                    ETableSelectionModel *etsm)
144 {
145 	free_hash (etsm);
146 }
147 
148 static void
model_cell_changed(ETableModel * etm,gint col,gint row,ETableSelectionModel * etsm)149 model_cell_changed (ETableModel *etm,
150                     gint col,
151                     gint row,
152                     ETableSelectionModel *etsm)
153 {
154 	free_hash (etsm);
155 }
156 
157 #if 1
158 static void
model_rows_inserted(ETableModel * etm,gint row,gint count,ETableSelectionModel * etsm)159 model_rows_inserted (ETableModel *etm,
160                      gint row,
161                      gint count,
162                      ETableSelectionModel *etsm)
163 {
164 	e_selection_model_array_insert_rows (E_SELECTION_MODEL_ARRAY (etsm), row, count);
165 	free_hash (etsm);
166 }
167 
168 static void
model_rows_deleted(ETableModel * etm,gint row,gint count,ETableSelectionModel * etsm)169 model_rows_deleted (ETableModel *etm,
170                     gint row,
171                     gint count,
172                     ETableSelectionModel *etsm)
173 {
174 	e_selection_model_array_delete_rows (E_SELECTION_MODEL_ARRAY (etsm), row, count);
175 	free_hash (etsm);
176 }
177 
178 #else
179 
180 static void
model_rows_inserted(ETableModel * etm,gint row,gint count,ETableSelectionModel * etsm)181 model_rows_inserted (ETableModel *etm,
182                      gint row,
183                      gint count,
184                      ETableSelectionModel *etsm)
185 {
186 	model_changed (etm, etsm);
187 }
188 
189 static void
model_rows_deleted(ETableModel * etm,gint row,gint count,ETableSelectionModel * etsm)190 model_rows_deleted (ETableModel *etm,
191                     gint row,
192                     gint count,
193                     ETableSelectionModel *etsm)
194 {
195 	model_changed (etm, etsm);
196 }
197 #endif
198 
199 inline static void
add_model(ETableSelectionModel * etsm,ETableModel * model)200 add_model (ETableSelectionModel *etsm,
201            ETableModel *model)
202 {
203 	etsm->model = model;
204 	if (model) {
205 		g_object_ref (model);
206 		etsm->model_pre_change_id = g_signal_connect (
207 			model, "model_pre_change",
208 			G_CALLBACK (model_pre_change), etsm);
209 		etsm->model_changed_id = g_signal_connect (
210 			model, "model_changed",
211 			G_CALLBACK (model_changed), etsm);
212 		etsm->model_row_changed_id = g_signal_connect (
213 			model, "model_row_changed",
214 			G_CALLBACK (model_row_changed), etsm);
215 		etsm->model_cell_changed_id = g_signal_connect (
216 			model, "model_cell_changed",
217 			G_CALLBACK (model_cell_changed), etsm);
218 		etsm->model_rows_inserted_id = g_signal_connect (
219 			model, "model_rows_inserted",
220 			G_CALLBACK (model_rows_inserted), etsm);
221 		etsm->model_rows_deleted_id = g_signal_connect (
222 			model, "model_rows_deleted",
223 			G_CALLBACK (model_rows_deleted), etsm);
224 	}
225 	e_selection_model_array_confirm_row_count (E_SELECTION_MODEL_ARRAY (etsm));
226 }
227 
228 inline static void
drop_model(ETableSelectionModel * etsm)229 drop_model (ETableSelectionModel *etsm)
230 {
231 	if (etsm->model) {
232 		g_signal_handler_disconnect (
233 			etsm->model,
234 			etsm->model_pre_change_id);
235 		g_signal_handler_disconnect (
236 			etsm->model,
237 			etsm->model_changed_id);
238 		g_signal_handler_disconnect (
239 			etsm->model,
240 			etsm->model_row_changed_id);
241 		g_signal_handler_disconnect (
242 			etsm->model,
243 			etsm->model_cell_changed_id);
244 		g_signal_handler_disconnect (
245 			etsm->model,
246 			etsm->model_rows_inserted_id);
247 		g_signal_handler_disconnect (
248 			etsm->model,
249 			etsm->model_rows_deleted_id);
250 
251 		g_object_unref (etsm->model);
252 	}
253 	etsm->model = NULL;
254 }
255 
256 static void
etsm_dispose(GObject * object)257 etsm_dispose (GObject *object)
258 {
259 	ETableSelectionModel *etsm;
260 
261 	etsm = E_TABLE_SELECTION_MODEL (object);
262 
263 	if (etsm->model_changed_idle_id)
264 		g_source_remove (etsm->model_changed_idle_id);
265 	etsm->model_changed_idle_id = 0;
266 
267 	drop_model (etsm);
268 	free_hash (etsm);
269 
270 	/* Chain up to parent's dispose() method. */
271 	G_OBJECT_CLASS (e_table_selection_model_parent_class)->dispose (object);
272 }
273 
274 static void
etsm_get_property(GObject * object,guint property_id,GValue * value,GParamSpec * pspec)275 etsm_get_property (GObject *object,
276                    guint property_id,
277                    GValue *value,
278                    GParamSpec *pspec)
279 {
280 	ETableSelectionModel *etsm = E_TABLE_SELECTION_MODEL (object);
281 
282 	switch (property_id) {
283 	case PROP_MODEL:
284 		g_value_set_object (value, etsm->model);
285 		break;
286 	case PROP_HEADER:
287 		g_value_set_object (value, etsm->eth);
288 		break;
289 	}
290 }
291 
292 static void
etsm_set_property(GObject * object,guint property_id,const GValue * value,GParamSpec * pspec)293 etsm_set_property (GObject *object,
294                    guint property_id,
295                    const GValue *value,
296                    GParamSpec *pspec)
297 {
298 	ETableSelectionModel *etsm = E_TABLE_SELECTION_MODEL (object);
299 
300 	switch (property_id) {
301 	case PROP_MODEL:
302 		drop_model (etsm);
303 		add_model (etsm, g_value_get_object (value) ? E_TABLE_MODEL (g_value_get_object (value)) : NULL);
304 		break;
305 	case PROP_HEADER:
306 		etsm->eth = E_TABLE_HEADER (g_value_get_object (value));
307 		break;
308 	}
309 }
310 
311 static void
e_table_selection_model_init(ETableSelectionModel * selection)312 e_table_selection_model_init (ETableSelectionModel *selection)
313 {
314 	selection->model = NULL;
315 	selection->hash = NULL;
316 	selection->cursor_id = NULL;
317 
318 	selection->model_changed_idle_id = 0;
319 }
320 
321 static void
e_table_selection_model_class_init(ETableSelectionModelClass * class)322 e_table_selection_model_class_init (ETableSelectionModelClass *class)
323 {
324 	GObjectClass *object_class;
325 	ESelectionModelArrayClass *esma_class;
326 
327 	object_class = G_OBJECT_CLASS (class);
328 	esma_class = E_SELECTION_MODEL_ARRAY_CLASS (class);
329 
330 	object_class->dispose = etsm_dispose;
331 	object_class->get_property = etsm_get_property;
332 	object_class->set_property = etsm_set_property;
333 
334 	esma_class->get_row_count = etsm_get_row_count;
335 
336 	g_object_class_install_property (
337 		object_class,
338 		PROP_MODEL,
339 		g_param_spec_object (
340 			"model",
341 			"Model",
342 			NULL,
343 			E_TYPE_TABLE_MODEL,
344 			G_PARAM_READWRITE));
345 
346 	g_object_class_install_property (
347 		object_class,
348 		PROP_HEADER,
349 		g_param_spec_object (
350 			"header",
351 			"Header",
352 			NULL,
353 			E_TYPE_TABLE_HEADER,
354 			G_PARAM_READWRITE));
355 }
356 
357 /**
358  * e_table_selection_model_new
359  *
360  * This routine creates a new #ETableSelectionModel.
361  *
362  * Returns: The new #ETableSelectionModel.
363  */
364 ETableSelectionModel *
e_table_selection_model_new(void)365 e_table_selection_model_new (void)
366 {
367 	return g_object_new (E_TYPE_TABLE_SELECTION_MODEL, NULL);
368 }
369 
370 static gint
etsm_get_row_count(ESelectionModelArray * esma)371 etsm_get_row_count (ESelectionModelArray *esma)
372 {
373 	ETableSelectionModel *etsm = E_TABLE_SELECTION_MODEL (esma);
374 
375 	if (etsm->model)
376 		return e_table_model_row_count (etsm->model);
377 	else
378 		return 0;
379 }
380