2 * beagle-hits-added-response.c
4 * Copyright (C) 2005 Novell, Inc.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 * DEALINGS IN THE SOFTWARE.
29 #include <sys/types.h>
30 #include <sys/socket.h>
33 #include "beagle-hits-added-response.h"
34 #include "beagle-private.h"
35 #include "beagle-property.h"
38 BeagleHit
*hit
; /* Current hit */
39 BeagleProperty
*prop
; /* Current property; */
41 GSList
*hits
; /* of BeagleHit */
42 } BeagleHitsAddedResponsePrivate
;
44 #define BEAGLE_HITS_ADDED_RESPONSE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), BEAGLE_TYPE_HITS_ADDED_RESPONSE, BeagleHitsAddedResponsePrivate))
46 static BeagleResponseClass
*parent_class
= NULL
;
48 G_DEFINE_TYPE (BeagleHitsAddedResponse
, beagle_hits_added_response
, BEAGLE_TYPE_RESPONSE
)
51 beagle_hits_added_response_finalize (GObject
*obj
)
53 BeagleHitsAddedResponsePrivate
*priv
= BEAGLE_HITS_ADDED_RESPONSE_GET_PRIVATE (obj
);
55 _beagle_hit_list_free (priv
->hits
);
57 if (G_OBJECT_CLASS (parent_class
)->finalize
)
58 G_OBJECT_CLASS (parent_class
)->finalize (obj
);
62 start_hit (BeagleParserContext
*ctx
, const char **attrs
)
64 BeagleHitsAddedResponse
*response
= BEAGLE_HITS_ADDED_RESPONSE (_beagle_parser_context_get_response (ctx
));
65 BeagleHitsAddedResponsePrivate
*priv
= BEAGLE_HITS_ADDED_RESPONSE_GET_PRIVATE (response
);
69 priv
->hit
= _beagle_hit_new ();
71 for (i
= 0; attrs
[i
] != NULL
; i
+= 2) {
72 if (strcmp (attrs
[i
], "Uri") == 0)
73 priv
->hit
->uri
= g_strdup (attrs
[i
+ 1]);
74 else if (strcmp (attrs
[i
], "ParentUri") == 0)
75 priv
->hit
->parent_uri
= g_strdup (attrs
[i
+ 1]);
76 else if (strcmp (attrs
[i
], "Timestamp") == 0)
77 priv
->hit
->timestamp
= beagle_timestamp_new_from_string (attrs
[i
+ 1]);
78 else if (strcmp (attrs
[i
], "Type") == 0)
79 priv
->hit
->type
= g_strdup (attrs
[i
+ 1]);
80 else if (strcmp (attrs
[i
], "MimeType") == 0)
81 priv
->hit
->mime_type
= g_strdup (attrs
[i
+ 1]);
82 else if (strcmp (attrs
[i
], "Source") == 0)
83 priv
->hit
->source
= g_strdup (attrs
[i
+ 1]);
84 else if (strcmp (attrs
[i
], "SourceObjectName") == 0)
85 priv
->hit
->source_object_name
= g_strdup (attrs
[i
+ 1]);
86 else if (strcmp (attrs
[i
], "Score") == 0)
87 priv
->hit
->score
= g_ascii_strtod (attrs
[i
+ 1], NULL
);
89 g_warning ("unknown attribute \"%s\" with value \"%s\"", attrs
[i
], attrs
[i
+ 1]);
94 end_hit (BeagleParserContext
*ctx
)
96 BeagleHitsAddedResponse
*response
= BEAGLE_HITS_ADDED_RESPONSE (_beagle_parser_context_get_response (ctx
));
97 BeagleHitsAddedResponsePrivate
*priv
= BEAGLE_HITS_ADDED_RESPONSE_GET_PRIVATE (response
);
99 priv
->hits
= g_slist_prepend (priv
->hits
, priv
->hit
);
104 start_property (BeagleParserContext
*ctx
, const char **attrs
)
106 BeagleHitsAddedResponse
*response
= BEAGLE_HITS_ADDED_RESPONSE (_beagle_parser_context_get_response (ctx
));
107 BeagleHitsAddedResponsePrivate
*priv
= BEAGLE_HITS_ADDED_RESPONSE_GET_PRIVATE (response
);
108 BeaglePropertyType type
= BEAGLE_PROPERTY_TYPE_UNKNOWN
;
109 const char *key
= NULL
, *value
= NULL
;
110 gboolean is_mutable
= FALSE
, is_searched
= FALSE
;
113 for (i
= 0; attrs
[i
] != NULL
; i
+= 2) {
114 if (strcmp (attrs
[i
], "Key") == 0)
116 else if (strcmp (attrs
[i
], "Value") == 0)
117 value
= attrs
[i
+ 1];
118 else if (strcmp (attrs
[i
], "IsMutable") == 0)
119 is_mutable
= strcmp (attrs
[i
+ 1], "true") == 0;
120 else if (strcmp (attrs
[i
], "IsSearched") == 0)
121 is_searched
= strcmp (attrs
[i
+ 1], "true") == 0;
122 else if (strcmp (attrs
[i
], "Type") == 0)
123 if (strcmp (attrs
[i
+ 1], "Text") == 0)
124 type
= BEAGLE_PROPERTY_TYPE_TEXT
;
125 else if (strcmp (attrs
[i
+ 1], "Keyword") == 0)
126 type
= BEAGLE_PROPERTY_TYPE_KEYWORD
;
127 else if (strcmp (attrs
[i
+ 1], "Date") == 0)
128 type
= BEAGLE_PROPERTY_TYPE_DATE
;
130 g_warning ("could not handle %s", attrs
[i
]);
133 if (!key
|| !value
) {
134 g_warning ("key or value was null");
138 priv
->prop
= beagle_property_new (type
, key
, value
);
139 priv
->prop
->is_mutable
= is_mutable
;
140 priv
->prop
->is_searched
= is_searched
;
144 end_property (BeagleParserContext
*ctx
)
146 BeagleHitsAddedResponse
*response
= BEAGLE_HITS_ADDED_RESPONSE (_beagle_parser_context_get_response (ctx
));
147 BeagleHitsAddedResponsePrivate
*priv
= BEAGLE_HITS_ADDED_RESPONSE_GET_PRIVATE (response
);
149 _beagle_hit_add_property (priv
->hit
, priv
->prop
);
156 PARSER_STATE_PROPERTIES
,
157 PARSER_STATE_PROPERTY
160 static BeagleParserHandler parser_handlers
[] = {
175 PARSER_STATE_PROPERTIES
,
179 PARSER_STATE_PROPERTIES
,
180 PARSER_STATE_PROPERTY
,
188 beagle_hits_added_response_class_init (BeagleHitsAddedResponseClass
*klass
)
190 GObjectClass
*obj_class
= G_OBJECT_CLASS (klass
);
192 parent_class
= g_type_class_peek_parent (klass
);
194 obj_class
->finalize
= beagle_hits_added_response_finalize
;
196 _beagle_response_class_set_parser_handlers (BEAGLE_RESPONSE_CLASS (klass
),
199 g_type_class_add_private (klass
, sizeof (BeagleHitsAddedResponsePrivate
));
203 beagle_hits_added_response_init (BeagleHitsAddedResponse
*response
)
208 * beagle_hits_added_response_get_hits:
209 * @response: a #BeagleHitsAddedResponse
211 * Fetches the hits from the given #BeagleHitsAddedResponse. The list should not be modified or freed.
213 * Return value: A list of #BeagleHit.
216 beagle_hits_added_response_get_hits (BeagleHitsAddedResponse
*response
)
218 BeagleHitsAddedResponsePrivate
*priv
;
220 g_return_val_if_fail (BEAGLE_IS_HITS_ADDED_RESPONSE (response
), NULL
);
222 priv
= BEAGLE_HITS_ADDED_RESPONSE_GET_PRIVATE (response
);