evolution-3.6.4/filter/e-filter-int.c

No issues found

  1 /*
  2  * This program is free software; you can redistribute it and/or
  3  * modify it under the terms of the GNU Lesser General Public
  4  * License as published by the Free Software Foundation; either
  5  * version 2 of the License, or (at your option) version 3.
  6  *
  7  * This program is distributed in the hope that it will be useful,
  8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 10  * Lesser General Public License for more details.
 11  *
 12  * You should have received a copy of the GNU Lesser General Public
 13  * License along with the program; if not, see <http://www.gnu.org/licenses/>
 14  *
 15  *
 16  * Authors:
 17  *		Jeffrey Stedfast <fejj@ximian.com>
 18  *
 19  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 20  *
 21  */
 22 
 23 #ifdef HAVE_CONFIG_H
 24 #include <config.h>
 25 #endif
 26 
 27 #include <stdlib.h>
 28 #include <gtk/gtk.h>
 29 
 30 #include "e-filter-int.h"
 31 
 32 G_DEFINE_TYPE (
 33 	EFilterInt,
 34 	e_filter_int,
 35 	E_TYPE_FILTER_ELEMENT)
 36 
 37 static void
 38 filter_int_spin_changed (GtkSpinButton *spin_button,
 39                          EFilterElement *element)
 40 {
 41 	EFilterInt *filter_int = E_FILTER_INT (element);
 42 
 43 	filter_int->val = gtk_spin_button_get_value_as_int (spin_button);
 44 }
 45 
 46 static void
 47 filter_int_finalize (GObject *object)
 48 {
 49 	EFilterInt *filter_int = E_FILTER_INT (object);
 50 
 51 	g_free (filter_int->type);
 52 
 53 	/* Chain up to parent's finalize() method. */
 54 	G_OBJECT_CLASS (e_filter_int_parent_class)->finalize (object);
 55 }
 56 
 57 static gint
 58 filter_int_eq (EFilterElement *element_a,
 59                EFilterElement *element_b)
 60 {
 61 	EFilterInt *filter_int_a = E_FILTER_INT (element_a);
 62 	EFilterInt *filter_int_b = E_FILTER_INT (element_b);
 63 
 64 	/* Chain up to parent's eq() method. */
 65 	if (!E_FILTER_ELEMENT_CLASS (e_filter_int_parent_class)->
 66 		eq (element_a, element_b))
 67 		return FALSE;
 68 
 69 	return (filter_int_a->val == filter_int_b->val);
 70 }
 71 
 72 static EFilterElement *
 73 filter_int_clone (EFilterElement *element)
 74 {
 75 	EFilterInt *filter_int = E_FILTER_INT (element);
 76 	EFilterInt *clone;
 77 
 78 	clone = (EFilterInt *) e_filter_int_new_type (
 79 		filter_int->type, filter_int->min, filter_int->max);
 80 	clone->val = filter_int->val;
 81 
 82 	E_FILTER_ELEMENT (clone)->name = g_strdup (element->name);
 83 
 84 	return E_FILTER_ELEMENT (clone);
 85 }
 86 
 87 static xmlNodePtr
 88 filter_int_xml_encode (EFilterElement *element)
 89 {
 90 	EFilterInt *filter_int = E_FILTER_INT (element);
 91 	xmlNodePtr value;
 92 	gchar intval[32];
 93 	const gchar *type;
 94 
 95 	type = filter_int->type ? filter_int->type : "integer";
 96 
 97 	value = xmlNewNode (NULL, (xmlChar *)"value");
 98 	xmlSetProp (value, (xmlChar *) "name", (xmlChar *) element->name);
 99 	xmlSetProp (value, (xmlChar *) "type", (xmlChar *) type);
100 
101 	sprintf (intval, "%d", filter_int->val);
102 	xmlSetProp (value, (xmlChar *) type, (xmlChar *) intval);
103 
104 	return value;
105 }
106 
107 static gint
108 filter_int_xml_decode (EFilterElement *element,
109                        xmlNodePtr node)
110 {
111 	EFilterInt *filter_int = E_FILTER_INT (element);
112 	gchar *name, *type;
113 	gchar *intval;
114 
115 	name = (gchar *) xmlGetProp (node, (xmlChar *)"name");
116 	xmlFree (element->name);
117 	element->name = name;
118 
119 	type = (gchar *) xmlGetProp (node, (xmlChar *)"type");
120 	g_free (filter_int->type);
121 	filter_int->type = g_strdup (type);
122 	xmlFree (type);
123 
124 	intval = (gchar *) xmlGetProp (
125 		node, (xmlChar *) (filter_int->type ?
126 		filter_int->type : "integer"));
127 	if (intval) {
128 		filter_int->val = atoi (intval);
129 		xmlFree (intval);
130 	} else {
131 		filter_int->val = 0;
132 	}
133 
134 	return 0;
135 }
136 
137 static GtkWidget *
138 filter_int_get_widget (EFilterElement *element)
139 {
140 	EFilterInt *filter_int = E_FILTER_INT (element);
141 	GtkWidget *widget;
142 	GtkAdjustment *adjustment;
143 
144 	adjustment = GTK_ADJUSTMENT (gtk_adjustment_new (
145 		0.0, (gfloat) filter_int->min,
146 		(gfloat) filter_int->max, 1.0, 1.0, 0));
147 	widget = gtk_spin_button_new (
148 		adjustment,
149 		filter_int->max > filter_int->min + 1000 ? 5.0 : 1.0, 0);
150 	gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (widget), TRUE);
151 
152 	if (filter_int->val)
153 		gtk_spin_button_set_value (
154 			GTK_SPIN_BUTTON (widget), (gfloat) filter_int->val);
155 
156 	g_signal_connect (
157 		widget, "value-changed",
158 		G_CALLBACK (filter_int_spin_changed), element);
159 
160 	return widget;
161 }
162 
163 static void
164 filter_int_format_sexp (EFilterElement *element,
165                         GString *out)
166 {
167 	EFilterInt *filter_int = E_FILTER_INT (element);
168 
169 	if (filter_int->val < 0)
170 		/* See #364731 #457523 C6*/
171 		g_string_append_printf (out, "(- 0 %d)", (filter_int->val * -1));
172 	else
173 		g_string_append_printf (out, "%d", filter_int->val);
174 }
175 
176 static void
177 e_filter_int_class_init (EFilterIntClass *class)
178 {
179 	GObjectClass *object_class;
180 	EFilterElementClass *filter_element_class;
181 
182 	object_class = G_OBJECT_CLASS (class);
183 	object_class->finalize = filter_int_finalize;
184 
185 	filter_element_class = E_FILTER_ELEMENT_CLASS (class);
186 	filter_element_class->eq = filter_int_eq;
187 	filter_element_class->clone = filter_int_clone;
188 	filter_element_class->xml_encode = filter_int_xml_encode;
189 	filter_element_class->xml_decode = filter_int_xml_decode;
190 	filter_element_class->get_widget = filter_int_get_widget;
191 	filter_element_class->format_sexp = filter_int_format_sexp;
192 }
193 
194 static void
195 e_filter_int_init (EFilterInt *filter_int)
196 {
197 	filter_int->min = 0;
198 	filter_int->max = G_MAXINT;
199 }
200 
201 EFilterElement *
202 e_filter_int_new (void)
203 {
204 	return g_object_new (E_TYPE_FILTER_INT, NULL);
205 }
206 
207 EFilterElement *
208 e_filter_int_new_type (const gchar *type,
209                        gint min,
210                        gint max)
211 {
212 	EFilterInt *filter_int;
213 
214 	filter_int = g_object_new (E_TYPE_FILTER_INT, NULL);
215 
216 	filter_int->type = g_strdup (type);
217 	filter_int->min = min;
218 	filter_int->max = max;
219 
220 	return E_FILTER_ELEMENT (filter_int);
221 }
222 
223 void
224 e_filter_int_set_value (EFilterInt *filter_int,
225                         gint value)
226 {
227 	g_return_if_fail (E_IS_FILTER_INT (filter_int));
228 
229 	filter_int->val = value;
230 }