Code cleanups.
[gnumeric.git] / src / go-data-slicer.c
blobb13d1992de16f318c778517e500ad4c5b1c2b7d9
1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * go-data-slicer.h : The definition of a content for a data slicer
5 * Copyright (C) 2008 Jody Goldberg (jody@gnome.org)
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) version 3.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
20 * USA
23 #include <gnumeric-config.h>
24 #include "go-data-slicer-impl.h"
25 #include "go-data-slicer-field-impl.h"
26 #include "go-data-cache.h"
28 #include <gsf/gsf-impl-utils.h>
29 #include <glib/gi18n-lib.h>
30 #include <string.h>
32 #define GO_DATA_SLICER_CLASS(k) (G_TYPE_CHECK_CLASS_CAST ((k), GO_DATA_SLICER_TYPE, GODataSlicerClass))
33 #define IS_GO_DATA_SLICER_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), GO_DATA_SLICER_TYPE))
34 #define GO_DATA_SLICER_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), GO_DATA_SLICER_TYPE, GODataSlicerClass))
36 enum {
37 PROP_0,
38 PROP_CACHE, /* GODataCache * */
39 PROP_NAME, /* GOString */
41 static void
42 go_data_slicer_init (GODataSlicer *ds)
44 int i;
46 ds->cache = NULL;
47 ds->name = NULL;
48 ds->all_fields = g_ptr_array_new ();
49 for (i = GDS_FIELD_TYPE_MAX ; --i > GDS_FIELD_TYPE_UNSET ; )
50 ds->fields[i] = g_array_new (FALSE, FALSE, sizeof (int));
53 static GObjectClass *parent_klass;
54 static void
55 go_data_slicer_finalize (GObject *obj)
57 GODataSlicer *ds = (GODataSlicer *)obj;
58 int i;
60 for (i = GDS_FIELD_TYPE_MAX ; --i > GDS_FIELD_TYPE_UNSET ; ) {
61 g_array_free (ds->fields[i], TRUE);
62 ds->fields[i] = NULL;
65 for (i = (int)ds->all_fields->len ; i-- > 0 ; )
66 g_object_unref (g_ptr_array_index (ds->all_fields, i));
67 g_ptr_array_free (ds->all_fields, TRUE);
68 ds->all_fields = NULL;
70 go_data_slicer_set_cache (ds, NULL);
71 go_string_unref (ds->name); ds->name = NULL;
73 (parent_klass->finalize) (obj);
76 static void
77 go_data_slicer_set_property (GObject *obj, guint property_id,
78 GValue const *value, GParamSpec *pspec)
80 GODataSlicer *ds = (GODataSlicer *)obj;
82 switch (property_id) {
83 case PROP_CACHE : go_data_slicer_set_cache (ds, g_value_get_object (value)); break;
84 case PROP_NAME : go_string_unref (ds->name); ds->name = g_value_dup_boxed (value); break;
85 default:
86 G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, property_id, pspec);
90 static void
91 go_data_slicer_get_property (GObject *obj, guint property_id,
92 GValue *value, GParamSpec *pspec)
94 GODataSlicer const *ds = (GODataSlicer const *)obj;
95 switch (property_id) {
96 case PROP_CACHE : g_value_set_object (value, ds->cache); break;
97 case PROP_NAME : g_value_set_boxed (value, ds->name); break;
98 default:
99 G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, property_id, pspec);
103 static void
104 go_data_slicer_class_init (GODataSlicerClass *klass)
106 GObjectClass *gobject_class = (GObjectClass *)klass;
107 gobject_class->set_property = go_data_slicer_set_property;
108 gobject_class->get_property = go_data_slicer_get_property;
109 gobject_class->finalize = go_data_slicer_finalize;
111 g_object_class_install_property (gobject_class, PROP_CACHE,
112 g_param_spec_object ("cache", NULL, NULL,
113 GO_DATA_CACHE_TYPE, GSF_PARAM_STATIC | G_PARAM_READWRITE));
114 g_object_class_install_property (gobject_class, PROP_NAME,
115 g_param_spec_boxed ("name", NULL, NULL, go_string_get_type (),
116 GSF_PARAM_STATIC | G_PARAM_READWRITE));
118 parent_klass = g_type_class_peek_parent (klass);
121 GSF_CLASS (GODataSlicer, go_data_slicer,
122 go_data_slicer_class_init, go_data_slicer_init,
123 G_TYPE_OBJECT)
126 * go_data_slicer_get_cache:
127 * @ds: #GODataSlicer
129 * Does not add a reference.
131 * Returns : the #GODataCache associated with @ds
133 GODataCache *
134 go_data_slicer_get_cache (GODataSlicer const *ds)
136 g_return_val_if_fail (IS_GO_DATA_SLICER (ds), NULL);
137 return ds->cache;
141 * go_data_slicer_set_cache:
142 * @ds: #GODataSlicer
143 * @cache: #GODataCache
145 * Assign @cache to @ds, and adds a reference to @cache
147 void
148 go_data_slicer_set_cache (GODataSlicer *ds, GODataCache *cache)
150 g_return_if_fail (IS_GO_DATA_SLICER (ds));
152 if (NULL != cache)
153 g_object_ref (cache);
154 if (NULL != ds->cache)
155 g_object_unref (ds->cache);
156 ds->cache = cache;
159 void
160 go_data_slicer_add_field (GODataSlicer *ds, GODataSlicerField *field)
162 g_return_if_fail (IS_GO_DATA_SLICER (ds));
163 g_return_if_fail (IS_GO_DATA_SLICER_FIELD (field));
164 g_return_if_fail (field->indx < 0);
165 g_return_if_fail (field->ds == NULL);
167 field->indx = ds->all_fields->len;
168 field->ds = ds;
169 g_ptr_array_add (ds->all_fields, field);
172 unsigned int
173 go_data_slicer_num_fields (GODataSlicer const *ds)
175 g_return_val_if_fail (IS_GO_DATA_SLICER (ds), 0);
176 return ds->all_fields->len;
179 GODataSlicerField *
180 go_data_slicer_get_field (GODataSlicer const *ds, unsigned int field_index)
182 g_return_val_if_fail (IS_GO_DATA_SLICER (ds), NULL);
183 g_return_val_if_fail (field_index < ds->all_fields->len, NULL);
184 return g_ptr_array_index (ds->all_fields, field_index);