No issues found
1 /*
2 *
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2 of the License, or (at your option) version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with the program; if not, see <http://www.gnu.org/licenses/>
15 *
16 *
17 * Authors:
18 * Chris Lahey <clahey@ximian.com>
19 *
20 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
21 *
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include "e-util/e-util.h"
29
30 #include "e-table-one.h"
31
32 G_DEFINE_TYPE (ETableOne, e_table_one, E_TYPE_TABLE_MODEL)
33
34 static gint
35 one_column_count (ETableModel *etm)
36 {
37 ETableOne *one = E_TABLE_ONE (etm);
38
39 if (one->source)
40 return e_table_model_column_count (one->source);
41 else
42 return 0;
43 }
44
45 static gint
46 one_row_count (ETableModel *etm)
47 {
48 return 1;
49 }
50
51 static gpointer
52 one_value_at (ETableModel *etm,
53 gint col,
54 gint row)
55 {
56 ETableOne *one = E_TABLE_ONE (etm);
57
58 if (one->data)
59 return one->data[col];
60 else
61 return NULL;
62 }
63
64 static void
65 one_set_value_at (ETableModel *etm,
66 gint col,
67 gint row,
68 gconstpointer val)
69 {
70 ETableOne *one = E_TABLE_ONE (etm);
71
72 if (one->data && one->source) {
73 e_table_model_free_value (one->source, col, one->data[col]);
74 one->data[col] = e_table_model_duplicate_value (one->source, col, val);
75 }
76 }
77
78 static gboolean
79 one_is_cell_editable (ETableModel *etm,
80 gint col,
81 gint row)
82 {
83 ETableOne *one = E_TABLE_ONE (etm);
84
85 if (one->source)
86 return e_table_model_is_cell_editable (one->source, col, -1);
87 else
88 return FALSE;
89 }
90
91 /* The default for one_duplicate_value is to return the raw value. */
92 static gpointer
93 one_duplicate_value (ETableModel *etm,
94 gint col,
95 gconstpointer value)
96 {
97 ETableOne *one = E_TABLE_ONE (etm);
98
99 if (one->source)
100 return e_table_model_duplicate_value (one->source, col, value);
101 else
102 return (gpointer) value;
103 }
104
105 static void
106 one_free_value (ETableModel *etm,
107 gint col,
108 gpointer value)
109 {
110 ETableOne *one = E_TABLE_ONE (etm);
111
112 if (one->source)
113 e_table_model_free_value (one->source, col, value);
114 }
115
116 static gpointer
117 one_initialize_value (ETableModel *etm,
118 gint col)
119 {
120 ETableOne *one = E_TABLE_ONE (etm);
121
122 if (one->source)
123 return e_table_model_initialize_value (one->source, col);
124 else
125 return NULL;
126 }
127
128 static gboolean
129 one_value_is_empty (ETableModel *etm,
130 gint col,
131 gconstpointer value)
132 {
133 ETableOne *one = E_TABLE_ONE (etm);
134
135 if (one->source)
136 return e_table_model_value_is_empty (one->source, col, value);
137 else
138 return FALSE;
139 }
140
141 static gchar *
142 one_value_to_string (ETableModel *etm,
143 gint col,
144 gconstpointer value)
145 {
146 ETableOne *one = E_TABLE_ONE (etm);
147
148 if (one->source)
149 return e_table_model_value_to_string (one->source, col, value);
150 else
151 return g_strdup ("");
152 }
153
154 static void
155 one_finalize (GObject *object)
156 {
157 G_OBJECT_CLASS (e_table_one_parent_class)->finalize (object);
158 }
159
160 static void
161 one_dispose (GObject *object)
162 {
163 ETableOne *one = E_TABLE_ONE (object);
164
165 if (one->data) {
166 gint i;
167 gint col_count;
168
169 if (one->source) {
170 col_count = e_table_model_column_count (one->source);
171
172 for (i = 0; i < col_count; i++)
173 e_table_model_free_value (one->source, i, one->data[i]);
174 }
175
176 g_free (one->data);
177 }
178 one->data = NULL;
179
180 if (one->source)
181 g_object_unref (one->source);
182 one->source = NULL;
183
184 G_OBJECT_CLASS (e_table_one_parent_class)->dispose (object);
185 }
186
187 static void
188 e_table_one_class_init (ETableOneClass *class)
189 {
190 GObjectClass *object_class = G_OBJECT_CLASS (class);
191 ETableModelClass *model_class = E_TABLE_MODEL_CLASS (class);
192
193 model_class->column_count = one_column_count;
194 model_class->row_count = one_row_count;
195 model_class->value_at = one_value_at;
196 model_class->set_value_at = one_set_value_at;
197 model_class->is_cell_editable = one_is_cell_editable;
198 model_class->duplicate_value = one_duplicate_value;
199 model_class->free_value = one_free_value;
200 model_class->initialize_value = one_initialize_value;
201 model_class->value_is_empty = one_value_is_empty;
202 model_class->value_to_string = one_value_to_string;
203
204 object_class->dispose = one_dispose;
205 object_class->finalize = one_finalize;
206 }
207
208 static void
209 e_table_one_init (ETableOne *one)
210 {
211 one->source = NULL;
212 one->data = NULL;
213 }
214
215 ETableModel *
216 e_table_one_new (ETableModel *source)
217 {
218 ETableOne *eto;
219 gint col_count;
220 gint i;
221
222 eto = g_object_new (E_TYPE_TABLE_ONE, NULL);
223 eto->source = source;
224
225 col_count = e_table_model_column_count (source);
226 eto->data = g_new (gpointer , col_count);
227 for (i = 0; i < col_count; i++) {
228 eto->data[i] = e_table_model_initialize_value (source, i);
229 }
230
231 if (source)
232 g_object_ref (source);
233
234 return (ETableModel *) eto;
235 }
236
237 void
238 e_table_one_commit (ETableOne *one)
239 {
240 if (one->source) {
241 gint empty = TRUE;
242 gint col;
243 gint cols = e_table_model_column_count (one->source);
244 for (col = 0; col < cols; col++) {
245 if (!e_table_model_value_is_empty (one->source, col, one->data[col])) {
246 empty = FALSE;
247 break;
248 }
249 }
250 if (!empty) {
251 e_table_model_append_row (one->source, E_TABLE_MODEL (one), 0);
252 }
253 }
254 }