evolution-3.6.4/widgets/table/e-table-selection-model.c

No issues found

Incomplete coverage

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