aboutsummaryrefslogtreecommitdiffstats
path: root/widgets/menus/gal-define-views-model.c
blob: 40e54a53b60ef5a8d5e4870cf99e0b89b4679a05 (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * gal-define-views-model.c
 * Copyright 2000, 2001, Ximian, Inc.
 *
 * Authors:
 *   Chris Lahey <clahey@ximian.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License, version 2, as published by the Free Software Foundation.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 */

#include <config.h>
#include <libxml/tree.h>
#include <libxml/parser.h>
#include <libxml/xmlmemory.h>
#include "gal-define-views-model.h"
#include <gal/util/e-i18n.h>
#include <gal/util/e-util.h>

#define PARENT_TYPE E_TABLE_MODEL_TYPE
static ETableModelClass *parent_class;

/*
 * GalDefineViewsModel callbacks
 * These are the callbacks that define the behavior of our custom model.
 */
static void gal_define_views_model_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
static void gal_define_views_model_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);

enum {
    PROP_0,
    PROP_EDITABLE,
    PROP_COLLECTION
};

static void
gdvm_dispose(GObject *object)
{
    GalDefineViewsModel *model = GAL_DEFINE_VIEWS_MODEL(object);

    if (model->collection)
        g_object_unref(model->collection);
    model->collection = NULL;

    if (G_OBJECT_CLASS (parent_class)->dispose)
        (* G_OBJECT_CLASS (parent_class)->dispose) (object);
}

/* This function returns the number of columns in our ETableModel. */
static int
gdvm_col_count (ETableModel *etc)
{
    return 1;
}

/* This function returns the number of rows in our ETableModel. */
static int
gdvm_row_count (ETableModel *etc)
{
    GalDefineViewsModel *views = GAL_DEFINE_VIEWS_MODEL(etc);
    if (views->collection)
        return gal_view_collection_get_count(views->collection);
    else
        return 0;
}

/* This function returns the value at a particular point in our ETableModel. */
static void *
gdvm_value_at (ETableModel *etc, int col, int row)
{
    GalDefineViewsModel *views = GAL_DEFINE_VIEWS_MODEL(etc);
    const char *value;

    value = gal_view_get_title (gal_view_collection_get_view(views->collection, row));

    return (void *)(value ? value : "");
}

/* This function sets the value at a particular point in our ETableModel. */
static void
gdvm_set_value_at (ETableModel *etc, int col, int row, const void *val)
{
    GalDefineViewsModel *views = GAL_DEFINE_VIEWS_MODEL(etc);
    if (views->editable) {
        e_table_model_pre_change(etc);
        gal_view_set_title(gal_view_collection_get_view(views->collection, row), val);
        e_table_model_cell_changed(etc, col, row);
    }
}

/* This function returns whether a particular cell is editable. */
static gboolean
gdvm_is_cell_editable (ETableModel *etc, int col, int row)
{
    return GAL_DEFINE_VIEWS_MODEL(etc)->editable;
}

static void
gdvm_append_row (ETableModel *etm, ETableModel *source, gint row)
{
}

/* This function duplicates the value passed to it. */
static void *
gdvm_duplicate_value (ETableModel *etc, int col, const void *value)
{
    return g_strdup(value);
}

/* This function frees the value passed to it. */
static void
gdvm_free_value (ETableModel *etc, int col, void *value)
{
    g_free(value);
}

static void *
gdvm_initialize_value (ETableModel *etc, int col)
{
    return g_strdup("");
}

static gboolean
gdvm_value_is_empty (ETableModel *etc, int col, const void *value)
{
    return !(value && *(char *)value);
}

static char *
gdvm_value_to_string (ETableModel *etc, int col, const void *value)
{
    return g_strdup(value);
}

/**
 * gal_define_views_model_append
 * @model: The model to add to.
 * @view: The view to add.
 *
 * Adds the given view to the gal define views model.
 */
void
gal_define_views_model_append (GalDefineViewsModel *model,
                   GalView             *view)
{
    ETableModel *etm = E_TABLE_MODEL(model);

    e_table_model_pre_change(etm);
    gal_view_collection_append(model->collection, view);
    e_table_model_row_inserted(etm, gal_view_collection_get_count(model->collection) - 1);
}

static void
gal_define_views_model_class_init (GObjectClass *object_class)
{
    ETableModelClass *model_class = (ETableModelClass *) object_class;

    parent_class = g_type_class_ref (PARENT_TYPE);

    object_class->dispose        = gdvm_dispose;
    object_class->set_property   = gal_define_views_model_set_property;
    object_class->get_property   = gal_define_views_model_get_property;

    g_object_class_install_property (object_class, PROP_EDITABLE, 
                     g_param_spec_boolean ("editable",
                                   _("Editable"),
                                   /*_( */"XXX blurb" /*)*/,
                                   FALSE,
                                   G_PARAM_READWRITE));

    g_object_class_install_property (object_class, PROP_COLLECTION, 
                     g_param_spec_object ("collection",
                                  _("Collection"),
                                  /*_( */"XXX blurb" /*)*/,
                                  GAL_VIEW_COLLECTION_TYPE,
                                  G_PARAM_READWRITE));

    model_class->column_count     = gdvm_col_count;
    model_class->row_count        = gdvm_row_count;
    model_class->value_at         = gdvm_value_at;
    model_class->set_value_at     = gdvm_set_value_at;
    model_class->is_cell_editable = gdvm_is_cell_editable;
    model_class->append_row       = gdvm_append_row;
    model_class->duplicate_value  = gdvm_duplicate_value;
    model_class->free_value       = gdvm_free_value;
    model_class->initialize_value = gdvm_initialize_value;
    model_class->value_is_empty   = gdvm_value_is_empty;
    model_class->value_to_string  = gdvm_value_to_string;
}

static void
gal_define_views_model_init (GObject *object)
{
    GalDefineViewsModel *model = GAL_DEFINE_VIEWS_MODEL(object);

    model->collection = NULL;
}

static void
gal_define_views_model_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
{
    GalDefineViewsModel *model;

    model = GAL_DEFINE_VIEWS_MODEL (object);

    switch (prop_id){
    case PROP_EDITABLE:
        model->editable = g_value_get_boolean (value);
        break;

    case PROP_COLLECTION:
        e_table_model_pre_change(E_TABLE_MODEL(object));
        if (g_value_get_object (value))
            model->collection = GAL_VIEW_COLLECTION(g_value_get_object (value));
        else
            model->collection = NULL;
        e_table_model_changed(E_TABLE_MODEL(object));
        break;
    }
}

static void
gal_define_views_model_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
{
    GalDefineViewsModel *model;

    model = GAL_DEFINE_VIEWS_MODEL (object);

    switch (prop_id) {
    case PROP_EDITABLE:
        g_value_set_boolean (value, model->editable);
        break;

    case PROP_COLLECTION:
        g_value_set_object (value, model->collection);

    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
}

E_MAKE_TYPE(gal_define_views_model, "GalDefineViewsModel", GalDefineViewsModel, gal_define_views_model_class_init, gal_define_views_model_init, PARENT_TYPE)

/**
 * gal_define_views_model_new
 *
 * Returns a new define views model.  This is a list of views as an
 * ETable for use in the GalDefineViewsDialog.
 *
 * Returns: The new GalDefineViewsModel.
 */
ETableModel *
gal_define_views_model_new (void)
{
    GalDefineViewsModel *et;

    et = g_object_new (GAL_DEFINE_VIEWS_MODEL_TYPE, NULL);

    return E_TABLE_MODEL(et);
}

/**
 * gal_define_views_model_get_view:
 * @model: The GalDefineViewsModel.
 * @n: Which view to get.
 *
 * Gets the nth view.
 *
 * Returns: The view.
 */
GalView *
gal_define_views_model_get_view (GalDefineViewsModel *model,
                 int n)
{
    return gal_view_collection_get_view(model->collection, n);
}

/**
 * gal_define_views_model_delete_view:
 * @model: The GalDefineViewsModel.
 * @n: Which view to delete.
 *
 * Deletes the nth view.
 */
void
gal_define_views_model_delete_view (GalDefineViewsModel *model,
                    int n)
{
    e_table_model_pre_change(E_TABLE_MODEL(model));
    gal_view_collection_delete_view(model->collection, n);
    e_table_model_row_deleted(E_TABLE_MODEL(model), n);
}

/**
 * gal_define_views_model_copy_view:
 * @model: The GalDefineViewsModel.
 * @n: Which view to copy.
 *
 * Copys the nth view.
 */
void
gal_define_views_model_copy_view (GalDefineViewsModel *model,
                  int n)
{
    ETableModel *etm = E_TABLE_MODEL(model);
    e_table_model_pre_change(etm);
    gal_view_collection_copy_view(model->collection, n);
    e_table_model_row_inserted(etm, gal_view_collection_get_count(model->collection) - 1);
}