1 /* MManager - a Desktop wide manager for multimedia applications.
3 * Copyright (C) 2008 Cosimo Cecchi <cosimoc@gnome.org>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 #include <libxml/tree.h>
23 #include <libxml/xmlwriter.h>
24 #include "mm-string-utils.h"
27 MMComparisionOperator op
;
31 static OperatorGrid operator_grid
[] =
33 { MM_COMP_EQUAL
, "EQ" },
34 { MM_COMP_GREATER
, "GR" },
35 { MM_COMP_GREATER_EQUAL
, "GRQ" },
36 { MM_COMP_LESS
, "LS" },
37 { MM_COMP_LESS_EQUAL
, "LSQ" },
42 value_to_string (GValue
*v
)
46 GType type
= G_VALUE_TYPE (v
);
48 /* guess the most used cases and handle those first */
49 if (type
== G_TYPE_STRING
) {
50 ret
= g_strdup (g_value_get_string (v
));
51 } else if (type
== G_TYPE_BOOLEAN
) {
52 ret
= g_strdup_printf ("%d", g_value_get_boolean (v
));
53 } else if (type
== G_TYPE_INT
) {
54 ret
= g_strdup_printf ("%d", g_value_get_int (v
));
55 } else if (type
== G_TYPE_FLOAT
) {
56 ret
= g_strdup_printf ("%f", g_value_get_float (v
));
57 } else if (type
== G_TYPE_DOUBLE
) {
58 char double_buff
[G_ASCII_DTOSTR_BUF_SIZE
];
59 ret
= g_ascii_dtostr (double_buff
, sizeof (double_buff
),
60 g_value_get_double (v
));
61 } else if (type
== G_TYPE_LONG
) {
62 ret
= g_strdup_printf ("%ld", g_value_get_long (v
));
63 } else if (type
== G_TYPE_INT64
) {
64 ret
= g_strdup_printf ("%lld", g_value_get_int64 (v
));
65 } else if (type
== G_TYPE_UINT
) {
66 ret
= g_strdup_printf ("%u", g_value_get_uint (v
));
67 } else if (type
== G_TYPE_ULONG
) {
68 ret
= g_strdup_printf ("%lu", g_value_get_ulong (v
));
69 } else if (type
== G_TYPE_UINT64
) {
70 ret
= g_strdup_printf ("%llu", g_value_get_uint64 (v
));
71 } else if (G_VALUE_HOLDS_CHAR (v
)) {
72 ret
= g_strdup_printf ("%c", g_value_get_char (v
));
73 } else if (G_VALUE_HOLDS_UCHAR (v
)) {
74 ret
= g_strdup_printf ("%c", g_value_get_uchar (v
));
76 g_warning ("Can't convert the value to string: unhandled type");
80 safe
= xmlCharStrdup (ret
);
86 op_to_string (MMComparisionOperator op
)
90 for (idx
= 0; idx
< G_N_ELEMENTS (operator_grid
); idx
++) {
91 if (op
== operator_grid
[idx
].op
) {
92 return xmlCharStrdup (operator_grid
[idx
].string
);
100 add_filter_param_to_xml (MMFilterParam
*fp
,
101 xmlTextWriterPtr writer
)
103 MMAttribute
*attribute
;
106 attribute
= mm_filter_param_get_attribute (fp
);
108 xmlTextWriterStartElement (writer
, BAD_CAST ("filter-param"));
110 xmlTextWriterStartElement (writer
, BAD_CAST ("attribute"));
111 safe_str
= xmlCharStrdup (mm_attribute_get_id (attribute
));
112 xmlTextWriterWriteAttribute (writer
, BAD_CAST ("id"), safe_str
);
114 safe_str
= xmlCharStrdup (mm_attribute_get_name (attribute
));
115 xmlTextWriterWriteAttribute (writer
, BAD_CAST ("name"), safe_str
);
117 safe_str
= xmlCharStrdup (mm_attribute_get_description (attribute
));
118 xmlTextWriterWriteAttribute (writer
, BAD_CAST ("description"), safe_str
);
120 xmlTextWriterWriteAttribute (writer
, BAD_CAST ("type"), BAD_CAST (g_type_name (mm_attribute_get_value_type (attribute
))));
121 /* close "attribute" */
122 xmlTextWriterEndElement (writer
);
124 safe_str
= value_to_string (mm_filter_param_get_value (fp
));
125 xmlTextWriterWriteElement (writer
, BAD_CAST ("value"), safe_str
);
128 safe_str
= op_to_string (mm_filter_param_get_operator (fp
));
129 xmlTextWriterWriteElement (writer
, BAD_CAST ("op"), safe_str
);
132 /* close "filter-param" */
133 xmlTextWriterEndElement (writer
);
136 /* public functions */
138 mm_filter_serialize (MMFilter
*filter
)
142 xmlTextWriterPtr writer
;
143 GList
*filter_params
;
145 buffer
= xmlBufferCreate ();
146 writer
= xmlNewTextWriterMemory (buffer
, 0);
148 xmlTextWriterStartDocument (writer
, NULL
, NULL
, NULL
);
150 xmlTextWriterStartElement (writer
, BAD_CAST ("filter"));
151 filter_params
= mm_filter_get_filtering_params (filter
);
152 g_list_foreach (filter_params
, (GFunc
) add_filter_param_to_xml
, writer
);
154 xmlTextWriterEndElement (writer
);
156 xmlTextWriterEndDocument (writer
);
158 xmlFreeTextWriter (writer
);
159 serialized
= g_strdup ((char *) xmlBufferContent (buffer
));
160 xmlBufferFree (buffer
);
166 mm_filter_unserialize (const char *s
)