1 /* $Id$
2  *
3  * Lasso - A free implementation of the Liberty Alliance specifications.
4  *
5  * Copyright (C) 2004-2007 Entr'ouvert
6  * http://lasso.entrouvert.org
7  *
8  * Authors: See AUTHORS file in top-level directory.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 #include "private.h"
25 #include "dst_query.h"
26 #include "idwsf_strings.h"
27 
28 /**
29  * SECTION:dst_query
30  * @short_description: &lt;dst:Query&gt;
31  *
32  * <figure><title>Schema fragment for dst:Query</title>
33  * <programlisting><![CDATA[
34  *
35  * <xs:element name="Query" type="QueryType"/>
36  * <xs:complexType name="QueryType">
37  *     <xs:sequence>
38  *         <xs:group ref="ResourceIDGroup" minOccurs="0"/>
39  *	   <xs:element name="QueryItem" maxOccurs="unbounded"/>
40  *	   <xs:element ref="Extension" minOccurs="0" maxOccurs="unbounded"/>
41  *     </xs:sequence>
42  *     <xs:attribute name="id" type="xs:ID"/>
43  *     <xs:attribute name="itemID" type="IDType"/>
44  * </xs:complexType>
45  *
46  * <xs:simpleType name="IDReferenceType">
47  *   <xs:annotation>
48  *     <xs:documentation> This type can be used when referring to elements that are
49  *       identified using an IDType </xs:documentation>
50  *     </xs:annotation>
51  *   <xs:restriction base="xs:string"/>
52  * </xs:simpleType>
53  * ]]></programlisting>
54  * </figure>
55  */
56 
57 /*****************************************************************************/
58 /* private methods                                                           */
59 /*****************************************************************************/
60 
61 static struct XmlSnippet schema_snippets[] = {
62 	{ "ResourceID", SNIPPET_NODE, G_STRUCT_OFFSET(LassoDstQuery, ResourceID), NULL, NULL, NULL},
63 	{ "EncryptedResourceID", SNIPPET_NODE, G_STRUCT_OFFSET(LassoDstQuery,
64 			EncryptedResourceID), NULL, NULL, NULL },
65 	{ "QueryItem", SNIPPET_LIST_NODES, G_STRUCT_OFFSET(LassoDstQuery, QueryItem), NULL, NULL, NULL},
66 	{ "Extension", SNIPPET_EXTENSION, G_STRUCT_OFFSET(LassoDstQuery, Extension), NULL, NULL, NULL},
67 	{ "id", SNIPPET_ATTRIBUTE, G_STRUCT_OFFSET(LassoDstQuery, id), NULL, NULL, NULL},
68 	{ "itemID", SNIPPET_ATTRIBUTE, G_STRUCT_OFFSET(LassoDstQuery, itemID), NULL, NULL, NULL},
69 	{NULL, 0, 0, NULL, NULL, NULL}
70 };
71 
72 static LassoNodeClass *parent_class = NULL;
73 
74 static void
insure_namespace(xmlNode * xmlnode,xmlNs * ns)75 insure_namespace(xmlNode *xmlnode, xmlNs *ns)
76 {
77 	xmlNode *t = xmlnode->children;
78 
79 	xmlSetNs(xmlnode, ns);
80 	while (t) {
81 		if (t->type == XML_ELEMENT_NODE && t->ns == NULL) {
82 			insure_namespace(t, ns);
83 		}
84 		t = t->next;
85 	}
86 }
87 
88 static xmlNode*
get_xmlNode(LassoNode * node,gboolean lasso_dump)89 get_xmlNode(LassoNode *node, gboolean lasso_dump)
90 {
91 	xmlNode *xmlnode;
92 	xmlNs *ns;
93 
94 	xmlnode = parent_class->get_xmlNode(node, lasso_dump);
95 	ns = xmlNewNs(xmlnode, (xmlChar*)LASSO_DST_QUERY(node)->hrefServiceType,
96 			(xmlChar*)LASSO_DST_QUERY(node)->prefixServiceType);
97 	insure_namespace(xmlnode, ns);
98 
99 	return xmlnode;
100 }
101 
102 static int
init_from_xml(LassoNode * node,xmlNode * xmlnode)103 init_from_xml(LassoNode *node, xmlNode *xmlnode)
104 {
105 	LassoDstQuery *query = LASSO_DST_QUERY(node);
106 	int rc = 0;
107 
108 	rc = parent_class->init_from_xml(node, xmlnode);
109 	if (rc) {
110 		return rc;
111 	}
112 
113 	query->hrefServiceType = g_strdup((char*)xmlnode->ns->href);
114 	query->prefixServiceType = lasso_get_prefix_for_dst_service_href(
115 			query->hrefServiceType);
116 	if (query->prefixServiceType == NULL) {
117 		/* XXX: what to do here ? */
118 	}
119 
120 	return 0;
121 }
122 
123 /*****************************************************************************/
124 /* instance and class init functions                                         */
125 /*****************************************************************************/
126 
127 
128 static void
class_init(LassoDstQueryClass * klass,void * unused G_GNUC_UNUSED)129 class_init(LassoDstQueryClass *klass, void *unused G_GNUC_UNUSED)
130 {
131 	LassoNodeClass *nclass = LASSO_NODE_CLASS(klass);
132 
133 	parent_class = g_type_class_peek_parent(klass);
134 	nclass->get_xmlNode = get_xmlNode;
135 	nclass->init_from_xml = init_from_xml;
136 	nclass->node_data = g_new0(LassoNodeClassData, 1);
137 	lasso_node_class_set_nodename(nclass, "Query");
138 	lasso_node_class_add_snippets(nclass, schema_snippets);
139 }
140 
141 GType
lasso_dst_query_get_type()142 lasso_dst_query_get_type()
143 {
144 	static GType this_type = 0;
145 
146 	if (!this_type) {
147 		static const GTypeInfo this_info = {
148 			sizeof (LassoDstQueryClass),
149 			NULL,
150 			NULL,
151 			(GClassInitFunc) class_init,
152 			NULL,
153 			NULL,
154 			sizeof(LassoDstQuery),
155 			0,
156 			NULL,
157 			NULL
158 		};
159 
160 		this_type = g_type_register_static(LASSO_TYPE_NODE,
161 				"LassoDstQuery", &this_info, 0);
162 	}
163 	return this_type;
164 }
165 
166 
167 /**
168  * lasso_dst_query_new:
169  * @query_item: query item to embed in request (optional)
170  *
171  * Creates a new #LassoDstQuery object.  If @query_item is set it is added to
172  * the requested query items.
173  *
174  * Return value: a newly created #LassoDstQuery object.
175  **/
176 LassoDstQuery*
lasso_dst_query_new(LassoDstQueryItem * queryItem)177 lasso_dst_query_new(LassoDstQueryItem *queryItem)
178 {
179 	LassoDstQuery *query;
180 
181 	query = g_object_new(LASSO_TYPE_DST_QUERY, NULL);
182 
183 	if (queryItem) {
184 		query->QueryItem = g_list_append(query->QueryItem, queryItem);
185 	}
186 
187 	return query;
188 }
189 
190