1 /* Fo
2  * fo-property-orphans.c: 'orphans' property
3  *
4  * Copyright (C) 2001 Sun Microsystems
5  * Copyright (C) 2007 Menteith Consulting Ltd
6  *
7  * See COPYING for the status of this software.
8  */
9 
10 #include <string.h>
11 #include "fo-utils.h"
12 #include "fo-context.h"
13 #include "datatype/fo-datatype.h"
14 #include "property/fo-property-private.h"
15 #include "property/fo-property-font-size.h"
16 #include "property/fo-property-orphans.h"
17 
18 
19 #define FO_PROPERTY_ORPHANS_INITIAL	2
20 /* Inherited: TRUE */
21 /* Shorthand: FALSE */
22 /* orphans */
23 /* <integer> | inherit */
24 /* NO ENUMERATED VALUE */
25 
26 
27 struct _FoPropertyOrphans
28 {
29   FoProperty parent_instance;
30 };
31 
32 struct _FoPropertyOrphansClass
33 {
34   FoPropertyClass parent_class;
35 };
36 
37 static void fo_property_orphans_init         (FoPropertyOrphans      *property_orphans);
38 static void fo_property_orphans_class_init   (FoPropertyOrphansClass *klass);
39 static void fo_property_orphans_finalize     (GObject       *object);
40 
41 static FoDatatype* fo_property_orphans_resolve_enum (const gchar *token,
42                                                      FoContext   *context,
43                                                      GError     **error);
44 static FoDatatype* fo_property_orphans_validate (FoDatatype *datatype,
45                                                  FoContext  *context,
46                                                  GError    **error);
47 
48 static const gchar class_name[] = "orphans";
49 static gpointer parent_class;
50 
51 /**
52  * fo_property_orphans_get_type:
53  *
54  * Register the #FoPropertyOrphans type if not already registered and
55  * return its #GType value.
56  *
57  * Return value: #GType of #FoPropertyOrphans.
58  **/
59 GType
fo_property_orphans_get_type(void)60 fo_property_orphans_get_type (void)
61 {
62   static GType object_type = 0;
63 
64   if (!object_type)
65     {
66       static const GTypeInfo object_info =
67       {
68         sizeof (FoPropertyOrphansClass),
69         NULL,           /* base_init */
70         NULL,           /* base_finalize */
71         (GClassInitFunc) fo_property_orphans_class_init,
72         NULL,           /* class_finalize */
73         NULL,           /* class_data */
74         sizeof (FoPropertyOrphans),
75         0,              /* n_preallocs */
76         (GInstanceInitFunc) fo_property_orphans_init,
77 	NULL		/* value_table */
78       };
79 
80       object_type = g_type_register_static (FO_TYPE_PROPERTY,
81                                             class_name,
82                                             &object_info, 0);
83     }
84 
85   return object_type;
86 }
87 
88 /**
89  * fo_property_orphans_init:
90  * @orphans: #FoPropertyOrphans object to initialise.
91  *
92  * Implements #GInstanceInitFunc for #FoPropertyOrphans.
93  **/
94 void
fo_property_orphans_init(FoPropertyOrphans * orphans)95 fo_property_orphans_init (FoPropertyOrphans *orphans)
96 {
97   FO_PROPERTY (orphans)->value =
98     g_object_ref (g_object_new (FO_TYPE_INTEGER,
99 				"value",
100 				FO_PROPERTY_ORPHANS_INITIAL,
101 				NULL));
102 }
103 
104 /**
105  * fo_property_orphans_class_init:
106  * @klass: #FoPropertyOrphansClass object to initialise.
107  *
108  * Implements #GClassInitFunc for #FoPropertyOrphansClass.
109  **/
110 void
fo_property_orphans_class_init(FoPropertyOrphansClass * klass)111 fo_property_orphans_class_init (FoPropertyOrphansClass *klass)
112 {
113   GObjectClass *object_class = G_OBJECT_CLASS (klass);
114   FoPropertyClass *property_class = FO_PROPERTY_CLASS (klass);
115 
116   parent_class = g_type_class_peek_parent (klass);
117 
118   object_class->finalize = fo_property_orphans_finalize;
119 
120   property_class->is_inherited = TRUE;
121   property_class->is_shorthand = FALSE;
122   property_class->resolve_enum =
123     fo_property_orphans_resolve_enum;
124   property_class->validate =
125     fo_property_orphans_validate;
126   property_class->get_initial =
127     fo_property_orphans_get_initial;
128 }
129 
130 /**
131  * fo_property_orphans_finalize:
132  * @object: #FoPropertyOrphans object to finalize.
133  *
134  * Implements #GObjectFinalizeFunc for #FoPropertyOrphans.
135  **/
136 void
fo_property_orphans_finalize(GObject * object)137 fo_property_orphans_finalize (GObject *object)
138 {
139   FoPropertyOrphans *orphans;
140 
141   orphans = FO_PROPERTY_ORPHANS (object);
142 
143   G_OBJECT_CLASS (parent_class)->finalize (object);
144 }
145 
146 
147 /**
148  * fo_property_orphans_new:
149  *
150  * Creates a new #FoPropertyOrphans initialized to default value.
151  *
152  * Return value: the new #FoPropertyOrphans.
153  **/
154 FoProperty*
fo_property_orphans_new(void)155 fo_property_orphans_new (void)
156 {
157   FoProperty* orphans;
158 
159   orphans =
160     FO_PROPERTY (g_object_new (fo_property_orphans_get_type (),
161                                NULL));
162 
163   return orphans;
164 }
165 
166 /**
167  * fo_property_orphans_resolve_enum:
168  * @token:   Token from the XML attribute value to be evaluated as an
169  *           enumeration token.
170  * @context: #FoContext object from which to possibly inherit values.
171  * @error:   Information about any error that has occurred.
172  *
173  * Compare @token against the enumeration tokens that are valid for the
174  * current FO property.  If @token is valid, returns either an #FoEnum datatype
175  * representing the enumeration token or a different datatype representing
176  * the enumeration token's resolved value.  If @token is not valid,
177  * sets @error and returns NULL.
178  *
179  * Return value: Resolved enumeration value or NULL.
180  **/
181 FoDatatype*
fo_property_orphans_resolve_enum(const gchar * token,FoContext * context,GError ** error)182 fo_property_orphans_resolve_enum (const gchar *token,
183                                   FoContext   *context,
184                                   GError     **error)
185 {
186   g_return_val_if_fail (token != NULL, NULL);
187   g_return_val_if_fail (FO_IS_CONTEXT (context), NULL);
188   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
189 
190       g_set_error (error,
191 		   FO_FO_ERROR,
192 		   FO_FO_ERROR_ENUMERATION_TOKEN,
193 		   _(fo_fo_error_messages[FO_FO_ERROR_ENUMERATION_TOKEN]),
194 		   class_name,
195 		   token);
196       return NULL;
197 }
198 
199 /**
200  * fo_property_orphans_validate:
201  * @datatype: #FoDatatype to be validated against allowed datatypes and
202  *            values for current property.
203  * @context:  #FoContext object from which to possibly inherit values.
204  * @error:    Information about any error that has occurred.
205  *
206  * Validates @datatype against allowed values.  Returns @datatype, a
207  * replacement datatype value, or NULL if validation failed.
208  *
209  * Return value: Valid datatype value or NULL.
210  **/
211 FoDatatype*
fo_property_orphans_validate(FoDatatype * datatype,FoContext * context,GError ** error)212 fo_property_orphans_validate (FoDatatype *datatype,
213                               FoContext  *context,
214                               GError    **error)
215 {
216   FoDatatype *new_datatype;
217 
218   g_return_val_if_fail (datatype != NULL, NULL);
219   g_return_val_if_fail (FO_IS_DATATYPE (datatype), NULL);
220   g_return_val_if_fail (context != NULL, NULL);
221   g_return_val_if_fail (FO_IS_CONTEXT (context), NULL);
222   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
223 
224   if (FO_IS_INTEGER (datatype))
225     {
226       return datatype;
227     }
228   else if (FO_IS_NUMBER (datatype))
229     {
230       new_datatype =
231 	fo_integer_new_with_value ((gint) fo_number_get_value (datatype));
232 
233       g_object_unref (datatype);
234 
235       return new_datatype;
236     }
237   else
238     {
239       gchar *datatype_sprintf = fo_object_sprintf (datatype);
240 
241       g_set_error (error,
242 		   FO_FO_ERROR,
243 		   FO_FO_ERROR_DATATYPE,
244 		   _(fo_fo_error_messages[FO_FO_ERROR_DATATYPE]),
245 		   class_name,
246 		   datatype_sprintf,
247 		   g_type_name (G_TYPE_FROM_INSTANCE (datatype)));
248 
249       g_object_unref (datatype);
250 
251       g_free (datatype_sprintf);
252 
253       return NULL;
254     }
255 }
256 
257 /**
258  * fo_property_orphans_get_initial:
259  *
260  * Get an instance of the property with the correct initial value.
261  *
262  * Return value: An instance of the property.
263  **/
264 FoProperty*
fo_property_orphans_get_initial(void)265 fo_property_orphans_get_initial (void)
266 {
267   static FoProperty *orphans = NULL;
268 
269   if (orphans == NULL)
270     {
271       orphans = fo_property_orphans_new ();
272     }
273 
274   return orphans;
275 }
276