aboutsummaryrefslogtreecommitdiffstats
path: root/e-util/e-table-sorted-variable.c
blob: 065fe540ea6fda27839361e558ebad54cbeac159 (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
/*
 * 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.
 *
 * 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 Lesser General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
 *
 * Authors:
 *      Chris Lahey <clahey@ximian.com>
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

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

#include <stdlib.h>
#include <string.h>

#include "e-table-sorted-variable.h"
#include "e-table-sorting-utils.h"

#define d(x)

#define INCREMENT_AMOUNT 100

/* maximum insertions between an idle event that we will do without scheduling an idle sort */
#define ETSV_INSERT_MAX (4)

G_DEFINE_TYPE (
    ETableSortedVariable,
    e_table_sorted_variable,
    E_TYPE_TABLE_SUBSET_VARIABLE)

static void etsv_sort_info_changed        (ETableSortInfo *info, ETableSortedVariable *etsv);
static void etsv_sort                     (ETableSortedVariable *etsv);
static void etsv_add                      (ETableSubsetVariable *etssv, gint                  row);
static void etsv_add_all                  (ETableSubsetVariable *etssv);

static void
etsv_dispose (GObject *object)
{
    ETableSortedVariable *etsv = E_TABLE_SORTED_VARIABLE (object);

    if (etsv->sort_info_changed_id)
        g_signal_handler_disconnect (
            etsv->sort_info,
            etsv->sort_info_changed_id);
    etsv->sort_info_changed_id = 0;

    if (etsv->sort_idle_id) {
        g_source_remove (etsv->sort_idle_id);
        etsv->sort_idle_id = 0;
    }
    if (etsv->insert_idle_id) {
        g_source_remove (etsv->insert_idle_id);
        etsv->insert_idle_id = 0;
    }

    if (etsv->sort_info)
        g_object_unref (etsv->sort_info);
    etsv->sort_info = NULL;

    if (etsv->full_header)
        g_object_unref (etsv->full_header);
    etsv->full_header = NULL;

    G_OBJECT_CLASS (e_table_sorted_variable_parent_class)->dispose (object);
}

static void
e_table_sorted_variable_class_init (ETableSortedVariableClass *class)
{
    ETableSubsetVariableClass *etssv_class = E_TABLE_SUBSET_VARIABLE_CLASS (class);
    GObjectClass *object_class = G_OBJECT_CLASS (class);

    object_class->dispose = etsv_dispose;

    etssv_class->add = etsv_add;
    etssv_class->add_all = etsv_add_all;
}

static void
e_table_sorted_variable_init (ETableSortedVariable *etsv)
{
    etsv->full_header = NULL;
    etsv->sort_info = NULL;

    etsv->sort_info_changed_id = 0;

    etsv->sort_idle_id = 0;
    etsv->insert_count = 0;
}

static gboolean
etsv_sort_idle (ETableSortedVariable *etsv)
{
    g_object_ref (etsv);
    etsv_sort (etsv);
    etsv->sort_idle_id = 0;
    etsv->insert_count = 0;
    g_object_unref (etsv);
    return FALSE;
}

static gboolean
etsv_insert_idle (ETableSortedVariable *etsv)
{
    etsv->insert_count = 0;
    etsv->insert_idle_id = 0;
    return FALSE;
}

static void
etsv_add (ETableSubsetVariable *etssv,
          gint row)
{
    ETableModel *etm = E_TABLE_MODEL (etssv);
    ETableSubset *etss = E_TABLE_SUBSET (etssv);
    ETableSortedVariable *etsv = E_TABLE_SORTED_VARIABLE (etssv);
    ETableModel *source_model;
    gint i;

    source_model = e_table_subset_get_source_model (etss);

    e_table_model_pre_change (etm);

    if (etss->n_map + 1 > etssv->n_vals_allocated) {
        etssv->n_vals_allocated += INCREMENT_AMOUNT;
        etss->map_table = g_realloc (etss->map_table, (etssv->n_vals_allocated) * sizeof (gint));
    }
    i = etss->n_map;
    if (etsv->sort_idle_id == 0) {
        /* this is to see if we're inserting a lot of things between idle loops.
         * If we are, we're busy, its faster to just append and perform a full sort later */
        etsv->insert_count++;
        if (etsv->insert_count > ETSV_INSERT_MAX) {
            /* schedule a sort, and append instead */
            etsv->sort_idle_id = g_idle_add_full (50, (GSourceFunc) etsv_sort_idle, etsv, NULL);
        } else {
            /* make sure we have an idle handler to reset the count every now and then */
            if (etsv->insert_idle_id == 0) {
                etsv->insert_idle_id = g_idle_add_full (40, (GSourceFunc) etsv_insert_idle, etsv, NULL);
            }
            i = e_table_sorting_utils_insert (source_model, etsv->sort_info, etsv->full_header, etss->map_table, etss->n_map, row);
            memmove (etss->map_table + i + 1, etss->map_table + i, (etss->n_map - i) * sizeof (gint));
        }
    }
    etss->map_table[i] = row;
    etss->n_map++;

    e_table_model_row_inserted (etm, i);
}

static void
etsv_add_all (ETableSubsetVariable *etssv)
{
    ETableModel *etm = E_TABLE_MODEL (etssv);
    ETableSubset *etss = E_TABLE_SUBSET (etssv);
    ETableSortedVariable *etsv = E_TABLE_SORTED_VARIABLE (etssv);
    ETableModel *source_model;
    gint rows;
    gint i;

    e_table_model_pre_change (etm);

    source_model = e_table_subset_get_source_model (etss);
    rows = e_table_model_row_count (source_model);

    if (etss->n_map + rows > etssv->n_vals_allocated) {
        etssv->n_vals_allocated += MAX (INCREMENT_AMOUNT, rows);
        etss->map_table = g_realloc (etss->map_table, etssv->n_vals_allocated * sizeof (gint));
    }
    for (i = 0; i < rows; i++)
        etss->map_table[etss->n_map++] = i;

    if (etsv->sort_idle_id == 0) {
        etsv->sort_idle_id = g_idle_add_full (50, (GSourceFunc) etsv_sort_idle, etsv, NULL);
    }

    e_table_model_changed (etm);
}

ETableModel *
e_table_sorted_variable_new (ETableModel *source,
                             ETableHeader *full_header,
                             ETableSortInfo *sort_info)
{
    ETableSortedVariable *etsv = g_object_new (E_TYPE_TABLE_SORTED_VARIABLE, NULL);
    ETableSubsetVariable *etssv = E_TABLE_SUBSET_VARIABLE (etsv);

    if (e_table_subset_variable_construct (etssv, source) == NULL) {
        g_object_unref (etsv);
        return NULL;
    }

    etsv->sort_info = sort_info;
    g_object_ref (etsv->sort_info);
    etsv->full_header = full_header;
    g_object_ref (etsv->full_header);

    etsv->sort_info_changed_id = g_signal_connect (
        sort_info, "sort_info_changed",
        G_CALLBACK (etsv_sort_info_changed), etsv);

    return E_TABLE_MODEL (etsv);
}

static void
etsv_sort_info_changed (ETableSortInfo *info,
                        ETableSortedVariable *etsv)
{
    etsv_sort (etsv);
}

static void
etsv_sort (ETableSortedVariable *etsv)
{
    ETableSubset *etss = E_TABLE_SUBSET (etsv);
    ETableModel *source_model;
    static gint reentering = 0;

    if (reentering)
        return;
    reentering = 1;

    e_table_model_pre_change (E_TABLE_MODEL (etsv));

    source_model = e_table_subset_get_source_model (etss);
    e_table_sorting_utils_sort (source_model, etsv->sort_info, etsv->full_header, etss->map_table, etss->n_map);

    e_table_model_changed (E_TABLE_MODEL (etsv));
    reentering = 0;
}