1 /*
2  * e-table-model.c
3  *
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.
7  *
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
11  * for more details.
12  *
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/>.
15  *
16  */
17 
18 #include "e-table-model.h"
19 
20 #define d(x)
21 
22 d (static gint depth = 0;)
23 
24 G_DEFINE_INTERFACE (ETableModel, e_table_model, G_TYPE_OBJECT)
25 
26 enum {
27 	MODEL_NO_CHANGE,
28 	MODEL_CHANGED,
29 	MODEL_PRE_CHANGE,
30 	MODEL_ROW_CHANGED,
31 	MODEL_CELL_CHANGED,
32 	MODEL_ROWS_INSERTED,
33 	MODEL_ROWS_DELETED,
34 	ROW_SELECTION,
35 	LAST_SIGNAL
36 };
37 
38 static guint signals[LAST_SIGNAL] = { 0, };
39 
40 static gint
table_model_is_frozen(ETableModel * table_model)41 table_model_is_frozen (ETableModel *table_model)
42 {
43 	gpointer data;
44 
45 	data = g_object_get_data (G_OBJECT (table_model), "frozen");
46 
47 	return (GPOINTER_TO_INT (data) != 0);
48 }
49 
50 static void
e_table_model_default_init(ETableModelInterface * iface)51 e_table_model_default_init (ETableModelInterface *iface)
52 {
53 	signals[MODEL_NO_CHANGE] = g_signal_new (
54 		"model_no_change",
55 		G_TYPE_FROM_INTERFACE (iface),
56 		G_SIGNAL_RUN_LAST,
57 		G_STRUCT_OFFSET (ETableModelInterface, model_no_change),
58 		NULL, NULL, NULL,
59 		G_TYPE_NONE, 0);
60 
61 	signals[MODEL_CHANGED] = g_signal_new (
62 		"model_changed",
63 		G_TYPE_FROM_INTERFACE (iface),
64 		G_SIGNAL_RUN_LAST,
65 		G_STRUCT_OFFSET (ETableModelInterface, model_changed),
66 		NULL, NULL, NULL,
67 		G_TYPE_NONE, 0);
68 
69 	signals[MODEL_PRE_CHANGE] = g_signal_new (
70 		"model_pre_change",
71 		G_TYPE_FROM_INTERFACE (iface),
72 		G_SIGNAL_RUN_LAST,
73 		G_STRUCT_OFFSET (ETableModelInterface, model_pre_change),
74 		NULL, NULL, NULL,
75 		G_TYPE_NONE, 0);
76 
77 	signals[MODEL_ROW_CHANGED] = g_signal_new (
78 		"model_row_changed",
79 		G_TYPE_FROM_INTERFACE (iface),
80 		G_SIGNAL_RUN_LAST,
81 		G_STRUCT_OFFSET (ETableModelInterface, model_row_changed),
82 		NULL, NULL, NULL,
83 		G_TYPE_NONE, 1,
84 		G_TYPE_INT);
85 
86 	signals[MODEL_CELL_CHANGED] = g_signal_new (
87 		"model_cell_changed",
88 		G_TYPE_FROM_INTERFACE (iface),
89 		G_SIGNAL_RUN_LAST,
90 		G_STRUCT_OFFSET (ETableModelInterface, model_cell_changed),
91 		NULL, NULL, NULL,
92 		G_TYPE_NONE, 2,
93 		G_TYPE_INT,
94 		G_TYPE_INT);
95 
96 	signals[MODEL_ROWS_INSERTED] = g_signal_new (
97 		"model_rows_inserted",
98 		G_TYPE_FROM_INTERFACE (iface),
99 		G_SIGNAL_RUN_LAST,
100 		G_STRUCT_OFFSET (ETableModelInterface, model_rows_inserted),
101 		NULL, NULL, NULL,
102 		G_TYPE_NONE, 2,
103 		G_TYPE_INT,
104 		G_TYPE_INT);
105 
106 	signals[MODEL_ROWS_DELETED] = g_signal_new (
107 		"model_rows_deleted",
108 		G_TYPE_FROM_INTERFACE (iface),
109 		G_SIGNAL_RUN_LAST,
110 		G_STRUCT_OFFSET (ETableModelInterface, model_rows_deleted),
111 		NULL, NULL, NULL,
112 		G_TYPE_NONE, 2,
113 		G_TYPE_INT,
114 		G_TYPE_INT);
115 }
116 
117 /**
118  * e_table_model_column_count:
119  * @table_model: The e-table-model to operate on
120  *
121  * Returns: the number of columns in the table model.
122  */
123 gint
e_table_model_column_count(ETableModel * table_model)124 e_table_model_column_count (ETableModel *table_model)
125 {
126 	ETableModelInterface *iface;
127 
128 	g_return_val_if_fail (E_IS_TABLE_MODEL (table_model), 0);
129 
130 	iface = E_TABLE_MODEL_GET_INTERFACE (table_model);
131 	g_return_val_if_fail (iface->column_count != NULL, 0);
132 
133 	return iface->column_count (table_model);
134 }
135 
136 /**
137  * e_table_model_row_count:
138  * @table_model: the e-table-model to operate on
139  *
140  * Returns: the number of rows in the Table model.
141  */
142 gint
e_table_model_row_count(ETableModel * table_model)143 e_table_model_row_count (ETableModel *table_model)
144 {
145 	ETableModelInterface *iface;
146 
147 	g_return_val_if_fail (E_IS_TABLE_MODEL (table_model), 0);
148 
149 	iface = E_TABLE_MODEL_GET_INTERFACE (table_model);
150 	g_return_val_if_fail (iface->row_count != NULL, 0);
151 
152 	return iface->row_count (table_model);
153 }
154 
155 /**
156  * e_table_model_append_row:
157  * @table_model: the table model to append the a row to.
158  * @source:
159  * @row:
160  *
161  */
162 void
e_table_model_append_row(ETableModel * table_model,ETableModel * source,gint row)163 e_table_model_append_row (ETableModel *table_model,
164                           ETableModel *source,
165                           gint row)
166 {
167 	ETableModelInterface *iface;
168 
169 	g_return_if_fail (E_IS_TABLE_MODEL (table_model));
170 
171 	iface = E_TABLE_MODEL_GET_INTERFACE (table_model);
172 
173 	if (iface->append_row != NULL)
174 		iface->append_row (table_model, source, row);
175 }
176 
177 /**
178  * e_table_value_at:
179  * @table_model: the e-table-model to operate on
180  * @col: column in the model to pull data from.
181  * @row: row in the model to pull data from.
182  *
183  * Return value: This function returns the value that is stored
184  * by the @table_model in column @col and row @row.  The data
185  * returned can be a pointer or any data value that can be stored
186  * inside a pointer.
187  *
188  * The data returned is typically used by an ECell renderer.
189  *
190  * The data returned must be valid until the model sends a signal that
191  * affect that piece of data.  model_changed affects all data.
192  * row_changed affects the data in that row.  cell_changed affects the
193  * data in that cell.  rows_deleted affects all data in those rows.
194  * rows_inserted and no_change don't affect any data in this way.
195  **/
196 gpointer
e_table_model_value_at(ETableModel * table_model,gint col,gint row)197 e_table_model_value_at (ETableModel *table_model,
198                         gint col,
199                         gint row)
200 {
201 	ETableModelInterface *iface;
202 
203 	g_return_val_if_fail (E_IS_TABLE_MODEL (table_model), NULL);
204 
205 	iface = E_TABLE_MODEL_GET_INTERFACE (table_model);
206 	g_return_val_if_fail (iface->value_at != NULL, NULL);
207 
208 	return iface->value_at (table_model, col, row);
209 }
210 
211 /**
212  * e_table_model_set_value_at:
213  * @table_model: the table model to operate on.
214  * @col: the column where the data will be stored in the model.
215  * @row: the row where the data will be stored in the model.
216  * @value: the data to be stored.
217  *
218  * This function instructs the model to store the value in @data in the
219  * the @table_model at column @col and row @row.  The @data typically
220  * comes from one of the ECell rendering objects.
221  *
222  * There should be an agreement between the Table Model and the user
223  * of this function about the data being stored.  Typically it will
224  * be a pointer to a set of data, or a datum that fits inside a gpointer .
225  */
226 void
e_table_model_set_value_at(ETableModel * table_model,gint col,gint row,gconstpointer value)227 e_table_model_set_value_at (ETableModel *table_model,
228                             gint col,
229                             gint row,
230                             gconstpointer value)
231 {
232 	ETableModelInterface *iface;
233 
234 	g_return_if_fail (E_IS_TABLE_MODEL (table_model));
235 
236 	iface = E_TABLE_MODEL_GET_INTERFACE (table_model);
237 	g_return_if_fail (iface->set_value_at != NULL);
238 
239 	iface->set_value_at (table_model, col, row, value);
240 }
241 
242 /**
243  * e_table_model_is_cell_editable:
244  * @table_model: the table model to query.
245  * @col: column to query.
246  * @row: row to query.
247  *
248  * Returns: %TRUE if the cell in @table_model at @col,@row can be
249  * edited, %FALSE otherwise
250  */
251 gboolean
e_table_model_is_cell_editable(ETableModel * table_model,gint col,gint row)252 e_table_model_is_cell_editable (ETableModel *table_model,
253                                 gint col,
254                                 gint row)
255 {
256 	ETableModelInterface *iface;
257 
258 	g_return_val_if_fail (E_IS_TABLE_MODEL (table_model), FALSE);
259 
260 	iface = E_TABLE_MODEL_GET_INTERFACE (table_model);
261 	g_return_val_if_fail (iface->is_cell_editable != NULL, FALSE);
262 
263 	return iface->is_cell_editable (table_model, col, row);
264 }
265 
266 gpointer
e_table_model_duplicate_value(ETableModel * table_model,gint col,gconstpointer value)267 e_table_model_duplicate_value (ETableModel *table_model,
268                                gint col,
269                                gconstpointer value)
270 {
271 	ETableModelInterface *iface;
272 
273 	g_return_val_if_fail (E_IS_TABLE_MODEL (table_model), NULL);
274 
275 	iface = E_TABLE_MODEL_GET_INTERFACE (table_model);
276 
277 	if (iface->duplicate_value == NULL)
278 		return NULL;
279 
280 	return iface->duplicate_value (table_model, col, value);
281 }
282 
283 void
e_table_model_free_value(ETableModel * table_model,gint col,gpointer value)284 e_table_model_free_value (ETableModel *table_model,
285                           gint col,
286                           gpointer value)
287 {
288 	ETableModelInterface *iface;
289 
290 	g_return_if_fail (E_IS_TABLE_MODEL (table_model));
291 
292 	iface = E_TABLE_MODEL_GET_INTERFACE (table_model);
293 
294 	if (iface->free_value != NULL)
295 		iface->free_value (table_model, col, value);
296 }
297 
298 gboolean
e_table_model_has_save_id(ETableModel * table_model)299 e_table_model_has_save_id (ETableModel *table_model)
300 {
301 	ETableModelInterface *iface;
302 
303 	g_return_val_if_fail (E_IS_TABLE_MODEL (table_model), FALSE);
304 
305 	iface = E_TABLE_MODEL_GET_INTERFACE (table_model);
306 
307 	if (iface->has_save_id == NULL)
308 		return FALSE;
309 
310 	return iface->has_save_id (table_model);
311 }
312 
313 gchar *
e_table_model_get_save_id(ETableModel * table_model,gint row)314 e_table_model_get_save_id (ETableModel *table_model,
315                            gint row)
316 {
317 	ETableModelInterface *iface;
318 
319 	g_return_val_if_fail (E_IS_TABLE_MODEL (table_model), NULL);
320 
321 	iface = E_TABLE_MODEL_GET_INTERFACE (table_model);
322 
323 	if (iface->get_save_id == NULL)
324 		return NULL;
325 
326 	return iface->get_save_id (table_model, row);
327 }
328 
329 gboolean
e_table_model_has_change_pending(ETableModel * table_model)330 e_table_model_has_change_pending (ETableModel *table_model)
331 {
332 	ETableModelInterface *iface;
333 
334 	g_return_val_if_fail (E_IS_TABLE_MODEL (table_model), FALSE);
335 
336 	iface = E_TABLE_MODEL_GET_INTERFACE (table_model);
337 
338 	if (iface->has_change_pending == NULL)
339 		return FALSE;
340 
341 	return iface->has_change_pending (table_model);
342 }
343 
344 gpointer
e_table_model_initialize_value(ETableModel * table_model,gint col)345 e_table_model_initialize_value (ETableModel *table_model,
346                                 gint col)
347 {
348 	ETableModelInterface *iface;
349 
350 	g_return_val_if_fail (E_IS_TABLE_MODEL (table_model), NULL);
351 
352 	iface = E_TABLE_MODEL_GET_INTERFACE (table_model);
353 
354 	if (iface->initialize_value == NULL)
355 		return NULL;
356 
357 	return iface->initialize_value (table_model, col);
358 }
359 
360 gboolean
e_table_model_value_is_empty(ETableModel * table_model,gint col,gconstpointer value)361 e_table_model_value_is_empty (ETableModel *table_model,
362                               gint col,
363                               gconstpointer value)
364 {
365 	ETableModelInterface *iface;
366 
367 	g_return_val_if_fail (E_IS_TABLE_MODEL (table_model), FALSE);
368 
369 	iface = E_TABLE_MODEL_GET_INTERFACE (table_model);
370 
371 	if (iface->value_is_empty == NULL)
372 		return FALSE;
373 
374 	return iface->value_is_empty (table_model, col, value);
375 }
376 
377 gchar *
e_table_model_value_to_string(ETableModel * table_model,gint col,gconstpointer value)378 e_table_model_value_to_string (ETableModel *table_model,
379                                gint col,
380                                gconstpointer value)
381 {
382 	ETableModelInterface *iface;
383 
384 	g_return_val_if_fail (E_IS_TABLE_MODEL (table_model), NULL);
385 
386 	iface = E_TABLE_MODEL_GET_INTERFACE (table_model);
387 
388 	if (iface->value_to_string == NULL)
389 		return g_strdup ("");
390 
391 	return iface->value_to_string (table_model, col, value);
392 }
393 
394 #if d(!)0
395 static void
print_tabs(void)396 print_tabs (void)
397 {
398 	gint i;
399 	for (i = 0; i < depth; i++)
400 		g_print ("\t");
401 }
402 #endif
403 
404 void
e_table_model_pre_change(ETableModel * table_model)405 e_table_model_pre_change (ETableModel *table_model)
406 {
407 	g_return_if_fail (E_IS_TABLE_MODEL (table_model));
408 
409 	if (table_model_is_frozen (table_model))
410 		return;
411 
412 	d (print_tabs ());
413 	d (depth++);
414 	g_signal_emit (table_model, signals[MODEL_PRE_CHANGE], 0);
415 	d (depth--);
416 }
417 
418 /**
419  * e_table_model_no_change:
420  * @table_model: the table model to notify of the lack of a change
421  *
422  * Use this function to notify any views of this table model that
423  * the contents of the table model have changed.  This will emit
424  * the signal "model_no_change" on the @table_model object.
425  *
426  * It is preferable to use the e_table_model_row_changed() and
427  * the e_table_model_cell_changed() to notify of smaller changes
428  * than to invalidate the entire model, as the views might have
429  * ways of caching the information they render from the model.
430  */
431 void
e_table_model_no_change(ETableModel * table_model)432 e_table_model_no_change (ETableModel *table_model)
433 {
434 	g_return_if_fail (E_IS_TABLE_MODEL (table_model));
435 
436 	if (table_model_is_frozen (table_model))
437 		return;
438 
439 	d (print_tabs ());
440 	d (depth++);
441 	g_signal_emit (table_model, signals[MODEL_NO_CHANGE], 0);
442 	d (depth--);
443 }
444 
445 /**
446  * e_table_model_changed:
447  * @table_model: the table model to notify of the change
448  *
449  * Use this function to notify any views of this table model that
450  * the contents of the table model have changed.  This will emit
451  * the signal "model_changed" on the @table_model object.
452  *
453  * It is preferable to use the e_table_model_row_changed() and
454  * the e_table_model_cell_changed() to notify of smaller changes
455  * than to invalidate the entire model, as the views might have
456  * ways of caching the information they render from the model.
457  */
458 void
e_table_model_changed(ETableModel * table_model)459 e_table_model_changed (ETableModel *table_model)
460 {
461 	g_return_if_fail (E_IS_TABLE_MODEL (table_model));
462 
463 	if (table_model_is_frozen (table_model))
464 		return;
465 
466 	d (print_tabs ());
467 	d (depth++);
468 	g_signal_emit (table_model, signals[MODEL_CHANGED], 0);
469 	d (depth--);
470 }
471 
472 /**
473  * e_table_model_row_changed:
474  * @table_model: the table model to notify of the change
475  * @row: the row that was changed in the model.
476  *
477  * Use this function to notify any views of the table model that
478  * the contents of row @row have changed in model.  This function
479  * will emit the "model_row_changed" signal on the @table_model
480  * object
481  */
482 void
e_table_model_row_changed(ETableModel * table_model,gint row)483 e_table_model_row_changed (ETableModel *table_model,
484                            gint row)
485 {
486 	g_return_if_fail (E_IS_TABLE_MODEL (table_model));
487 
488 	if (table_model_is_frozen (table_model))
489 		return;
490 
491 	d (print_tabs ());
492 	d (depth++);
493 	g_signal_emit (table_model, signals[MODEL_ROW_CHANGED], 0, row);
494 	d (depth--);
495 }
496 
497 /**
498  * e_table_model_cell_changed:
499  * @table_model: the table model to notify of the change
500  * @col: the column.
501  * @row: the row
502  *
503  * Use this function to notify any views of the table model that
504  * contents of the cell at @col,@row has changed. This will emit
505  * the "model_cell_changed" signal on the @table_model
506  * object
507  */
508 void
e_table_model_cell_changed(ETableModel * table_model,gint col,gint row)509 e_table_model_cell_changed (ETableModel *table_model,
510                             gint col,
511                             gint row)
512 {
513 	g_return_if_fail (E_IS_TABLE_MODEL (table_model));
514 
515 	if (table_model_is_frozen (table_model))
516 		return;
517 
518 	d (print_tabs ());
519 	d (depth++);
520 	g_signal_emit (
521 		table_model, signals[MODEL_CELL_CHANGED], 0, col, row);
522 	d (depth--);
523 }
524 
525 /**
526  * e_table_model_rows_inserted:
527  * @table_model: the table model to notify of the change
528  * @row: the row that was inserted into the model.
529  * @count: The number of rows that were inserted.
530  *
531  * Use this function to notify any views of the table model that
532  * @count rows at row @row have been inserted into the model.  This
533  * function will emit the "model_rows_inserted" signal on the
534  * @table_model object
535  */
536 void
e_table_model_rows_inserted(ETableModel * table_model,gint row,gint count)537 e_table_model_rows_inserted (ETableModel *table_model,
538                              gint row,
539                              gint count)
540 {
541 	g_return_if_fail (E_IS_TABLE_MODEL (table_model));
542 
543 	if (table_model_is_frozen (table_model))
544 		return;
545 
546 	d (print_tabs ());
547 	d (depth++);
548 	g_signal_emit (
549 		table_model, signals[MODEL_ROWS_INSERTED], 0, row, count);
550 	d (depth--);
551 }
552 
553 /**
554  * e_table_model_row_inserted:
555  * @table_model: the table model to notify of the change
556  * @row: the row that was inserted into the model.
557  *
558  * Use this function to notify any views of the table model that the
559  * row @row has been inserted into the model.  This function will emit
560  * the "model_rows_inserted" signal on the @table_model object
561  */
562 void
e_table_model_row_inserted(ETableModel * table_model,gint row)563 e_table_model_row_inserted (ETableModel *table_model,
564                             gint row)
565 {
566 	g_return_if_fail (E_IS_TABLE_MODEL (table_model));
567 
568 	e_table_model_rows_inserted (table_model, row, 1);
569 }
570 
571 /**
572  * e_table_model_row_deleted:
573  * @table_model: the table model to notify of the change
574  * @row: the row that was deleted
575  * @count: The number of rows deleted
576  *
577  * Use this function to notify any views of the table model that
578  * @count rows at row @row have been deleted from the model.  This
579  * function will emit the "model_rows_deleted" signal on the
580  * @table_model object
581  */
582 void
e_table_model_rows_deleted(ETableModel * table_model,gint row,gint count)583 e_table_model_rows_deleted (ETableModel *table_model,
584                             gint row,
585                             gint count)
586 {
587 	g_return_if_fail (E_IS_TABLE_MODEL (table_model));
588 
589 	if (table_model_is_frozen (table_model))
590 		return;
591 
592 	d (print_tabs ());
593 	d (depth++);
594 	g_signal_emit (
595 		table_model, signals[MODEL_ROWS_DELETED], 0, row, count);
596 	d (depth--);
597 }
598 
599 /**
600  * e_table_model_row_deleted:
601  * @table_model: the table model to notify of the change
602  * @row: the row that was deleted
603  *
604  * Use this function to notify any views of the table model that the
605  * row @row has been deleted from the model.  This function will emit
606  * the "model_rows_deleted" signal on the @table_model object
607  */
608 void
e_table_model_row_deleted(ETableModel * table_model,gint row)609 e_table_model_row_deleted (ETableModel *table_model,
610                            gint row)
611 {
612 	g_return_if_fail (E_IS_TABLE_MODEL (table_model));
613 
614 	e_table_model_rows_deleted (table_model, row, 1);
615 }
616 
617 void
e_table_model_freeze(ETableModel * table_model)618 e_table_model_freeze (ETableModel *table_model)
619 {
620 	gpointer data;
621 
622 	g_return_if_fail (E_IS_TABLE_MODEL (table_model));
623 
624 	e_table_model_pre_change (table_model);
625 
626 	data = g_object_get_data (G_OBJECT (table_model), "frozen");
627 	data = GINT_TO_POINTER (GPOINTER_TO_INT (data) + 1);
628 	g_object_set_data (G_OBJECT (table_model), "frozen", data);
629 }
630 
631 void
e_table_model_thaw(ETableModel * table_model)632 e_table_model_thaw (ETableModel *table_model)
633 {
634 	gpointer data;
635 
636 	g_return_if_fail (E_IS_TABLE_MODEL (table_model));
637 
638 	data = g_object_get_data (G_OBJECT (table_model), "frozen");
639 	data = GINT_TO_POINTER (GPOINTER_TO_INT (data) - 1);
640 	g_object_set_data (G_OBJECT (table_model), "frozen", data);
641 
642 	e_table_model_changed (table_model);
643 }
644 
645