aboutsummaryrefslogtreecommitdiffstats
path: root/e-util/e-table-subset.c
blob: 741fc8d230376b34ccc73f088a0dc7aa0cf11e3b (plain) (blame)
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
/*
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) version 3.
 *
 * 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>
 *
 *
 * Authors:
 *      Chris Lahey <clahey@ximian.com>
 *      Miguel de Icaza <miguel@ximian.com>
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>

#include "e-table-subset.h"

#define E_TABLE_SUBSET_GET_PRIVATE(obj) \
    (G_TYPE_INSTANCE_GET_PRIVATE \
    ((obj), E_TYPE_TABLE_SUBSET, ETableSubsetPrivate))

#define VALID_ROW(table_subset, row) \
    (row >= -1 && row < table_subset->n_map)
#define MAP_ROW(table_subset, row) \
    (row == -1 ? -1 : table_subset->map_table[row])

#define d(x)

struct _ETableSubsetPrivate {
    ETableModel *source_model;
    gulong table_model_pre_change_handler_id;
    gulong table_model_no_change_handler_id;
    gulong table_model_changed_handler_id;
    gulong table_model_row_changed_handler_id;
    gulong table_model_cell_changed_handler_id;
    gulong table_model_rows_inserted_handler_id;
    gulong table_model_rows_deleted_handler_id;

    gint last_access;
};

/* Forward Declarations */
static void e_table_subset_table_model_init
                    (ETableModelInterface *interface);

G_DEFINE_TYPE_WITH_CODE (
    ETableSubset,
    e_table_subset,
    G_TYPE_OBJECT,
    G_IMPLEMENT_INTERFACE (
        E_TYPE_TABLE_MODEL,
        e_table_subset_table_model_init))

static gint
table_subset_get_view_row (ETableSubset *table_subset,
                           gint row)
{
    const gint n = table_subset->n_map;
    const gint * const map_table = table_subset->map_table;
    gint i;

    gint end = MIN (
        table_subset->n_map,
        table_subset->priv->last_access + 10);
    gint start = MAX (0, table_subset->priv->last_access - 10);
    gint initial = MAX (MIN (table_subset->priv->last_access, end), start);

    for (i = initial; i < end; i++) {
        if (map_table[i] == row) {
            table_subset->priv->last_access = i;
            return i;
        }
    }

    for (i = initial - 1; i >= start; i--) {
        if (map_table[i] == row) {
            table_subset->priv->last_access = i;
            return i;
        }
    }

    for (i = 0; i < n; i++) {
        if (map_table[i] == row) {
            table_subset->priv->last_access = i;
            return i;
        }
    }
    return -1;
}

static void
table_subset_dispose (GObject *object)
{
    ETableSubsetPrivate *priv;

    priv = E_TABLE_SUBSET_GET_PRIVATE (object);

    if (priv->table_model_pre_change_handler_id > 0) {
        g_signal_handler_disconnect (
            priv->source_model,
            priv->table_model_pre_change_handler_id);
        priv->table_model_pre_change_handler_id = 0;
    }

    if (priv->table_model_no_change_handler_id > 0) {
        g_signal_handler_disconnect (
            priv->source_model,
            priv->table_model_no_change_handler_id);
        priv->table_model_no_change_handler_id = 0;
    }

    if (priv->table_model_changed_handler_id > 0) {
        g_signal_handler_disconnect (
            priv->source_model,
            priv->table_model_changed_handler_id);
        priv->table_model_changed_handler_id = 0;
    }

    if (priv->table_model_row_changed_handler_id > 0) {
        g_signal_handler_disconnect (
            priv->source_model,
            priv->table_model_row_changed_handler_id);
        priv->table_model_row_changed_handler_id = 0;
    }

    if (priv->table_model_cell_changed_handler_id > 0) {
        g_signal_handler_disconnect (
            priv->source_model,
            priv->table_model_cell_changed_handler_id);
        priv->table_model_cell_changed_handler_id = 0;
    }

    if (priv->table_model_rows_inserted_handler_id > 0) {
        g_signal_handler_disconnect (
            priv->source_model,
            priv->table_model_rows_inserted_handler_id);
        priv->table_model_rows_inserted_handler_id = 0;
    }

    if (priv->table_model_rows_deleted_handler_id > 0) {
        g_signal_handler_disconnect (
            priv->source_model,
            priv->table_model_rows_deleted_handler_id);
        priv->table_model_rows_deleted_handler_id = 0;
    }

    g_clear_object (&priv->source_model);

    /* Chain up to parent's dispose() method. */
    G_OBJECT_CLASS (e_table_subset_parent_class)->dispose (object);
}

static void
table_subset_finalize (GObject *object)
{
    ETableSubset *table_subset;

    table_subset = E_TABLE_SUBSET (object);

    g_free (table_subset->map_table);

    /* Chain up to parent's finalize() method. */
    G_OBJECT_CLASS (e_table_subset_parent_class)->finalize (object);
}

static void
table_subset_proxy_model_pre_change_real (ETableSubset *table_subset,
                                          ETableModel *source_model)
{
    e_table_model_pre_change (E_TABLE_MODEL (table_subset));
}

static void
table_subset_proxy_model_no_change_real (ETableSubset *table_subset,
                                         ETableModel *source_model)
{
    e_table_model_no_change (E_TABLE_MODEL (table_subset));
}

static void
table_subset_proxy_model_changed_real (ETableSubset *table_subset,
                                       ETableModel *source_model)
{
    e_table_model_changed (E_TABLE_MODEL (table_subset));
}

static void
table_subset_proxy_model_row_changed_real (ETableSubset *table_subset,
                                           ETableModel *source_model,
                                           gint row)
{
    gint view_row = table_subset_get_view_row (table_subset, row);

    if (view_row != -1)
        e_table_model_row_changed (
            E_TABLE_MODEL (table_subset), view_row);
    else
        e_table_model_no_change (E_TABLE_MODEL (table_subset));
}

static void
table_subset_proxy_model_cell_changed_real (ETableSubset *table_subset,
                                            ETableModel *source_model,
                                            gint col,
                                            gint row)
{
    gint view_row = table_subset_get_view_row (table_subset, row);

    if (view_row != -1)
        e_table_model_cell_changed (
            E_TABLE_MODEL (table_subset), col, view_row);
    else
        e_table_model_no_change (E_TABLE_MODEL (table_subset));
}

static void
table_subset_proxy_model_rows_inserted_real (ETableSubset *table_subset,
                                             ETableModel *source_model,
                                             gint row,
                                             gint count)
{
    e_table_model_no_change (E_TABLE_MODEL (table_subset));
}

static void
table_subset_proxy_model_rows_deleted_real (ETableSubset *table_subset,
                                            ETableModel *source_model,
                                            gint row,
                                            gint count)
{
    e_table_model_no_change (E_TABLE_MODEL (table_subset));
}

static gint
table_subset_column_count (ETableModel *table_model)
{
    ETableSubset *table_subset = (ETableSubset *) table_model;

    return e_table_model_column_count (table_subset->priv->source_model);
}

static gint
table_subset_row_count (ETableModel *table_model)
{
    ETableSubset *table_subset = (ETableSubset *) table_model;

    return table_subset->n_map;
}

static gpointer
table_subset_value_at (ETableModel *table_model,
                       gint col,
                       gint row)
{
    ETableSubset *table_subset = (ETableSubset *) table_model;

    g_return_val_if_fail (VALID_ROW (table_subset, row), NULL);

    table_subset->priv->last_access = row;

    return e_table_model_value_at (
        table_subset->priv->source_model,
        col, MAP_ROW (table_subset, row));
}

static void
table_subset_set_value_at (ETableModel *table_model,
                           gint col,
                           gint row,
                           gconstpointer val)
{
    ETableSubset *table_subset = (ETableSubset *) table_model;

    g_return_if_fail (VALID_ROW (table_subset, row));

    table_subset->priv->last_access = row;

    e_table_model_set_value_at (
        table_subset->priv->source_model,
        col, MAP_ROW (table_subset, row), val);
}

static gboolean
table_subset_is_cell_editable (ETableModel *table_model,
                               gint col,
                               gint row)
{
    ETableSubset *table_subset = (ETableSubset *) table_model;

    g_return_val_if_fail (VALID_ROW (table_subset, row), FALSE);

    return e_table_model_is_cell_editable (
        table_subset->priv->source_model,
        col, MAP_ROW (table_subset, row));
}

static gboolean
table_subset_has_save_id (ETableModel *table_model)
{
    return TRUE;
}

static gchar *
table_subset_get_save_id (ETableModel *table_model,
                          gint row)
{
    ETableSubset *table_subset = (ETableSubset *) table_model;

    g_return_val_if_fail (VALID_ROW (table_subset, row), NULL);

    if (e_table_model_has_save_id (table_subset->priv->source_model))
        return e_table_model_get_save_id (
            table_subset->priv->source_model,
            MAP_ROW (table_subset, row));
    else
        return g_strdup_printf ("%d", MAP_ROW (table_subset, row));
}

static void
table_subset_append_row (ETableModel *table_model,
                         ETableModel *source,
                         gint row)
{
    ETableSubset *table_subset = (ETableSubset *) table_model;

    e_table_model_append_row (
        table_subset->priv->source_model, source, row);
}

static gpointer
table_subset_duplicate_value (ETableModel *table_model,
                              gint col,
                              gconstpointer value)
{
    ETableSubset *table_subset = (ETableSubset *) table_model;

    return e_table_model_duplicate_value (
        table_subset->priv->source_model, col, value);
}

static void
table_subset_free_value (ETableModel *table_model,
                         gint col,
                         gpointer value)
{
    ETableSubset *table_subset = (ETableSubset *) table_model;

    e_table_model_free_value (
        table_subset->priv->source_model, col, value);
}

static gpointer
table_subset_initialize_value (ETableModel *table_model,
                               gint col)
{
    ETableSubset *table_subset = (ETableSubset *) table_model;

    return e_table_model_initialize_value (
        table_subset->priv->source_model, col);
}

static gboolean
table_subset_value_is_empty (ETableModel *table_model,
                             gint col,
                             gconstpointer value)
{
    ETableSubset *table_subset = (ETableSubset *) table_model;

    return e_table_model_value_is_empty (
        table_subset->priv->source_model, col, value);
}

static gchar *
table_subset_value_to_string (ETableModel *table_model,
                              gint col,
                              gconstpointer value)
{
    ETableSubset *table_subset = (ETableSubset *) table_model;

    return e_table_model_value_to_string (
        table_subset->priv->source_model, col, value);
}

static void
e_table_subset_class_init (ETableSubsetClass *class)
{
    GObjectClass *object_class;

    g_type_class_add_private (class, sizeof (ETableSubsetPrivate));

    object_class = G_OBJECT_CLASS (class);
    object_class->dispose = table_subset_dispose;
    object_class->finalize = table_subset_finalize;

    class->proxy_model_pre_change = table_subset_proxy_model_pre_change_real;
    class->proxy_model_no_change = table_subset_proxy_model_no_change_real;
    class->proxy_model_changed = table_subset_proxy_model_changed_real;
    class->proxy_model_row_changed = table_subset_proxy_model_row_changed_real;
    class->proxy_model_cell_changed = table_subset_proxy_model_cell_changed_real;
    class->proxy_model_rows_inserted = table_subset_proxy_model_rows_inserted_real;
    class->proxy_model_rows_deleted = table_subset_proxy_model_rows_deleted_real;
}

static void
e_table_subset_table_model_init (ETableModelInterface *interface)
{
    interface->column_count = table_subset_column_count;
    interface->row_count = table_subset_row_count;
    interface->append_row = table_subset_append_row;

    interface->value_at = table_subset_value_at;
    interface->set_value_at = table_subset_set_value_at;
    interface->is_cell_editable = table_subset_is_cell_editable;

    interface->has_save_id = table_subset_has_save_id;
    interface->get_save_id = table_subset_get_save_id;

    interface->duplicate_value = table_subset_duplicate_value;
    interface->free_value = table_subset_free_value;
    interface->initialize_value = table_subset_initialize_value;
    interface->value_is_empty = table_subset_value_is_empty;
    interface->value_to_string = table_subset_value_to_string;
}

static void
e_table_subset_init (ETableSubset *table_subset)
{
    table_subset->priv = E_TABLE_SUBSET_GET_PRIVATE (table_subset);
}

static void
table_subset_proxy_model_pre_change (ETableModel *source_model,
                                     ETableSubset *table_subset)
{
    ETableSubsetClass *class;

    class = E_TABLE_SUBSET_GET_CLASS (table_subset);

    if (class->proxy_model_pre_change != NULL)
        class->proxy_model_pre_change (table_subset, source_model);
}

static void
table_subset_proxy_model_no_change (ETableModel *source_model,
                                    ETableSubset *table_subset)
{
    ETableSubsetClass *class;

    class = E_TABLE_SUBSET_GET_CLASS (table_subset);

    if (class->proxy_model_no_change != NULL)
        class->proxy_model_no_change (table_subset, source_model);
}

static void
table_subset_proxy_model_changed (ETableModel *source_model,
                                  ETableSubset *table_subset)
{
    ETableSubsetClass *class;

    class = E_TABLE_SUBSET_GET_CLASS (table_subset);

    if (class->proxy_model_changed != NULL)
        class->proxy_model_changed (table_subset, source_model);
}

static void
table_subset_proxy_model_row_changed (ETableModel *source_model,
                                      gint row,
                                      ETableSubset *table_subset)
{
    ETableSubsetClass *class;

    class = E_TABLE_SUBSET_GET_CLASS (table_subset);

    if (class->proxy_model_row_changed != NULL)
        class->proxy_model_row_changed (
            table_subset, source_model, row);
}

static void
table_subset_proxy_model_cell_changed (ETableModel *source_model,
                                       gint col,
                                       gint row,
                                       ETableSubset *table_subset)
{
    ETableSubsetClass *class;

    class = E_TABLE_SUBSET_GET_CLASS (table_subset);

    if (class->proxy_model_cell_changed != NULL)
        class->proxy_model_cell_changed (
            table_subset, source_model, col, row);
}

static void
table_subset_proxy_model_rows_inserted (ETableModel *source_model,
                                        gint row,
                                        gint col,
                                        ETableSubset *table_subset)
{
    ETableSubsetClass *class;

    class = E_TABLE_SUBSET_GET_CLASS (table_subset);

    if (class->proxy_model_rows_inserted != NULL)
        class->proxy_model_rows_inserted (
            table_subset, source_model, row, col);
}

static void
table_subset_proxy_model_rows_deleted (ETableModel *source_model,
                                       gint row,
                                       gint col,
                                       ETableSubset *table_subset)
{
    ETableSubsetClass *class;

    class = E_TABLE_SUBSET_GET_CLASS (table_subset);

    if (class->proxy_model_rows_deleted != NULL)
        class->proxy_model_rows_deleted (
            table_subset, source_model, row, col);
}

ETableModel *
e_table_subset_construct (ETableSubset *table_subset,
                          ETableModel *source_model,
                          gint nvals)
{
    gulong handler_id;
    guint *buffer = NULL;
    gint i;

    if (nvals > 0)
        buffer = (guint *) g_malloc (sizeof (guint) * nvals);
    table_subset->map_table = (gint *) buffer;
    table_subset->n_map = nvals;
    table_subset->priv->source_model = g_object_ref (source_model);

    /* Init */
    for (i = 0; i < nvals; i++)
        table_subset->map_table[i] = i;

    handler_id = g_signal_connect (
        source_model, "model_pre_change",
        G_CALLBACK (table_subset_proxy_model_pre_change),
        table_subset);
    table_subset->priv->table_model_pre_change_handler_id = handler_id;

    handler_id = g_signal_connect (
        source_model, "model_no_change",
        G_CALLBACK (table_subset_proxy_model_no_change),
        table_subset);
    table_subset->priv->table_model_no_change_handler_id = handler_id;

    handler_id = g_signal_connect (
        source_model, "model_changed",
        G_CALLBACK (table_subset_proxy_model_changed),
        table_subset);
    table_subset->priv->table_model_changed_handler_id = handler_id;

    handler_id = g_signal_connect (
        source_model, "model_row_changed",
        G_CALLBACK (table_subset_proxy_model_row_changed),
        table_subset);
    table_subset->priv->table_model_row_changed_handler_id = handler_id;

    handler_id = g_signal_connect (
        source_model, "model_cell_changed",
        G_CALLBACK (table_subset_proxy_model_cell_changed),
        table_subset);
    table_subset->priv->table_model_cell_changed_handler_id = handler_id;

    handler_id = g_signal_connect (
        source_model, "model_rows_inserted",
        G_CALLBACK (table_subset_proxy_model_rows_inserted),
        table_subset);
    table_subset->priv->table_model_rows_inserted_handler_id = handler_id;

    handler_id = g_signal_connect (
        source_model, "model_rows_deleted",
        G_CALLBACK (table_subset_proxy_model_rows_deleted),
        table_subset);
    table_subset->priv->table_model_rows_deleted_handler_id = handler_id;

    return E_TABLE_MODEL (table_subset);
}

ETableModel *
e_table_subset_new (ETableModel *source_model,
                    const gint nvals)
{
    ETableSubset *table_subset;

    g_return_val_if_fail (E_IS_TABLE_MODEL (source_model), NULL);

    table_subset = g_object_new (E_TYPE_TABLE_SUBSET, NULL);

    if (e_table_subset_construct (table_subset, source_model, nvals) == NULL) {
        g_object_unref (table_subset);
        return NULL;
    }

    return (ETableModel *) table_subset;
}

ETableModel *
e_table_subset_get_source_model (ETableSubset *table_subset)
{
    g_return_val_if_fail (E_IS_TABLE_SUBSET (table_subset), NULL);

    return table_subset->priv->source_model;
}

gint
e_table_subset_model_to_view_row (ETableSubset *table_subset,
                                  gint model_row)
{
    gint i;

    g_return_val_if_fail (E_IS_TABLE_SUBSET (table_subset), -1);

    for (i = 0; i < table_subset->n_map; i++) {
        if (table_subset->map_table[i] == model_row)
            return i;
    }
    return -1;
}

gint
e_table_subset_view_to_model_row (ETableSubset *table_subset,
                                  gint view_row)
{
    g_return_val_if_fail (E_IS_TABLE_SUBSET (table_subset), -1);

    if (view_row >= 0 && view_row < table_subset->n_map)
        return table_subset->map_table[view_row];
    else
        return -1;
}

ETableModel *
e_table_subset_get_toplevel (ETableSubset *table_subset)
{
    g_return_val_if_fail (E_IS_TABLE_SUBSET (table_subset), NULL);

    if (E_IS_TABLE_SUBSET (table_subset->priv->source_model))
        return e_table_subset_get_toplevel (
            E_TABLE_SUBSET (table_subset->priv->source_model));
    else
        return table_subset->priv->source_model;
}

void
e_table_subset_print_debugging (ETableSubset *table_subset)
{
    gint i;

    g_return_if_fail (E_IS_TABLE_SUBSET (table_subset));

    for (i = 0; i < table_subset->n_map; i++) {
        g_print ("%8d\n", table_subset->map_table[i]);
    }
}