14192
|
1 /**
|
|
2 * @file object.c MSNObject API
|
|
3 *
|
|
4 * gaim
|
|
5 *
|
|
6 * Gaim is the legal property of its developers, whose names are too numerous
|
|
7 * to list here. Please refer to the COPYRIGHT file distributed with this
|
|
8 * source distribution.
|
|
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, write to the Free Software
|
|
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
23 */
|
|
24 #include "object.h"
|
|
25
|
|
26 #define GET_STRING_TAG(field, id) \
|
|
27 if ((tag = strstr(str, id "=\"")) != NULL) \
|
|
28 { \
|
|
29 tag += strlen(id "=\""); \
|
|
30 c = strchr(tag, '"'); \
|
|
31 if (c != NULL) \
|
|
32 { \
|
|
33 if (obj->field != NULL) \
|
|
34 g_free(obj->field); \
|
|
35 obj->field = g_strndup(tag, c - tag); \
|
|
36 } \
|
|
37 }
|
|
38
|
|
39 #define GET_INT_TAG(field, id) \
|
|
40 if ((tag = strstr(str, id "=\"")) != NULL) \
|
|
41 { \
|
|
42 char buf[16]; \
|
|
43 size_t offset; \
|
|
44 tag += strlen(id "=\""); \
|
|
45 c = strchr(tag, '"'); \
|
|
46 if (c != NULL) \
|
|
47 { \
|
|
48 memset(buf, 0, sizeof(buf)); \
|
|
49 offset = c - tag; \
|
|
50 if (offset >= sizeof(buf)) \
|
|
51 offset = sizeof(buf) - 1; \
|
|
52 strncpy(buf, tag, offset); \
|
|
53 obj->field = atoi(buf); \
|
|
54 } \
|
|
55 }
|
|
56
|
|
57 static GList *local_objs;
|
|
58
|
|
59 MsnObject *
|
|
60 msn_object_new(void)
|
|
61 {
|
|
62 MsnObject *obj;
|
|
63
|
|
64 obj = g_new0(MsnObject, 1);
|
|
65
|
|
66 msn_object_set_type(obj, MSN_OBJECT_UNKNOWN);
|
|
67 msn_object_set_friendly(obj, "AAA=");
|
|
68
|
|
69 return obj;
|
|
70 }
|
|
71
|
|
72 MsnObject *
|
|
73 msn_object_new_from_string(const char *str)
|
|
74 {
|
|
75 MsnObject *obj;
|
|
76 char *tag, *c;
|
|
77
|
|
78 g_return_val_if_fail(str != NULL, NULL);
|
14357
|
79
|
|
80 if (strncmp(str, "<msnobj ", 8))
|
|
81 return NULL;
|
14192
|
82
|
|
83 obj = msn_object_new();
|
|
84
|
|
85 GET_STRING_TAG(creator, "Creator");
|
|
86 GET_INT_TAG(size, "Size");
|
|
87 GET_INT_TAG(type, "Type");
|
|
88 GET_STRING_TAG(location, "Location");
|
|
89 GET_STRING_TAG(friendly, "Friendly");
|
|
90 GET_STRING_TAG(sha1d, "SHA1D");
|
|
91 GET_STRING_TAG(sha1c, "SHA1C");
|
|
92
|
|
93 /* If we are missing any of the required elements then discard the object */
|
|
94 if (obj->creator == NULL || obj->size == 0 || obj->type == 0
|
|
95 || obj->location == NULL || obj->friendly == NULL
|
|
96 || obj->sha1d == NULL || obj->sha1c == NULL) {
|
|
97 msn_object_destroy(obj);
|
|
98 obj = NULL;
|
|
99 }
|
|
100
|
|
101 return obj;
|
|
102 }
|
|
103
|
|
104 void
|
|
105 msn_object_destroy(MsnObject *obj)
|
|
106 {
|
|
107 g_return_if_fail(obj != NULL);
|
|
108
|
|
109 if (obj->creator != NULL)
|
|
110 g_free(obj->creator);
|
|
111
|
|
112 if (obj->location != NULL)
|
|
113 g_free(obj->location);
|
|
114
|
|
115 if (obj->real_location != NULL)
|
|
116 g_free(obj->real_location);
|
|
117
|
|
118 if (obj->friendly != NULL)
|
|
119 g_free(obj->friendly);
|
|
120
|
|
121 if (obj->sha1d != NULL)
|
|
122 g_free(obj->sha1d);
|
|
123
|
|
124 if (obj->sha1c != NULL)
|
|
125 g_free(obj->sha1c);
|
|
126
|
|
127 if (obj->local)
|
|
128 local_objs = g_list_remove(local_objs, obj);
|
|
129
|
|
130 g_free(obj);
|
|
131 }
|
|
132
|
|
133 char *
|
|
134 msn_object_to_string(const MsnObject *obj)
|
|
135 {
|
|
136 char *str;
|
|
137
|
|
138 g_return_val_if_fail(obj != NULL, NULL);
|
|
139
|
|
140 str = g_strdup_printf("<msnobj Creator=\"%s\" Size=\"%d\" Type=\"%d\" "
|
|
141 "Location=\"%s\" Friendly=\"%s\" SHA1D=\"%s\" "
|
|
142 "SHA1C=\"%s\"/>",
|
|
143 msn_object_get_creator(obj),
|
|
144 msn_object_get_size(obj),
|
|
145 msn_object_get_type(obj),
|
|
146 msn_object_get_location(obj),
|
|
147 msn_object_get_friendly(obj),
|
|
148 msn_object_get_sha1d(obj),
|
|
149 msn_object_get_sha1c(obj));
|
|
150
|
|
151 return str;
|
|
152 }
|
|
153
|
|
154 void
|
|
155 msn_object_set_creator(MsnObject *obj, const char *creator)
|
|
156 {
|
|
157 g_return_if_fail(obj != NULL);
|
|
158
|
|
159 if (obj->creator != NULL)
|
|
160 g_free(obj->creator);
|
|
161
|
|
162 obj->creator = (creator == NULL ? NULL : g_strdup(creator));
|
|
163 }
|
|
164
|
|
165 void
|
|
166 msn_object_set_size(MsnObject *obj, int size)
|
|
167 {
|
|
168 g_return_if_fail(obj != NULL);
|
|
169
|
|
170 obj->size = size;
|
|
171 }
|
|
172
|
|
173 void
|
|
174 msn_object_set_type(MsnObject *obj, MsnObjectType type)
|
|
175 {
|
|
176 g_return_if_fail(obj != NULL);
|
|
177
|
|
178 obj->type = type;
|
|
179 }
|
|
180
|
|
181 void
|
|
182 msn_object_set_location(MsnObject *obj, const char *location)
|
|
183 {
|
|
184 g_return_if_fail(obj != NULL);
|
|
185
|
|
186 if (obj->location != NULL)
|
|
187 g_free(obj->location);
|
|
188
|
|
189 obj->location = (location == NULL ? NULL : g_strdup(location));
|
|
190 }
|
|
191
|
|
192 void
|
|
193 msn_object_set_friendly(MsnObject *obj, const char *friendly)
|
|
194 {
|
|
195 g_return_if_fail(obj != NULL);
|
|
196
|
|
197 if (obj->friendly != NULL)
|
|
198 g_free(obj->friendly);
|
|
199
|
|
200 obj->friendly = (friendly == NULL ? NULL : g_strdup(friendly));
|
|
201 }
|
|
202
|
|
203 void
|
|
204 msn_object_set_sha1d(MsnObject *obj, const char *sha1d)
|
|
205 {
|
|
206 g_return_if_fail(obj != NULL);
|
|
207
|
|
208 if (obj->sha1d != NULL)
|
|
209 g_free(obj->sha1d);
|
|
210
|
|
211 obj->sha1d = (sha1d == NULL ? NULL : g_strdup(sha1d));
|
|
212 }
|
|
213
|
|
214 void
|
|
215 msn_object_set_sha1c(MsnObject *obj, const char *sha1c)
|
|
216 {
|
|
217 g_return_if_fail(obj != NULL);
|
|
218
|
|
219 if (obj->sha1c != NULL)
|
|
220 g_free(obj->sha1c);
|
|
221
|
|
222 obj->sha1c = (sha1c == NULL ? NULL : g_strdup(sha1c));
|
|
223 }
|
|
224
|
|
225 const char *
|
|
226 msn_object_get_creator(const MsnObject *obj)
|
|
227 {
|
|
228 g_return_val_if_fail(obj != NULL, NULL);
|
|
229
|
|
230 return obj->creator;
|
|
231 }
|
|
232
|
|
233 int
|
|
234 msn_object_get_size(const MsnObject *obj)
|
|
235 {
|
|
236 g_return_val_if_fail(obj != NULL, 0);
|
|
237
|
|
238 return obj->size;
|
|
239 }
|
|
240
|
|
241 MsnObjectType
|
|
242 msn_object_get_type(const MsnObject *obj)
|
|
243 {
|
|
244 g_return_val_if_fail(obj != NULL, MSN_OBJECT_UNKNOWN);
|
|
245
|
|
246 return obj->type;
|
|
247 }
|
|
248
|
|
249 const char *
|
|
250 msn_object_get_location(const MsnObject *obj)
|
|
251 {
|
|
252 g_return_val_if_fail(obj != NULL, NULL);
|
|
253
|
|
254 return obj->location;
|
|
255 }
|
|
256
|
|
257 const char *
|
|
258 msn_object_get_friendly(const MsnObject *obj)
|
|
259 {
|
|
260 g_return_val_if_fail(obj != NULL, NULL);
|
|
261
|
|
262 return obj->friendly;
|
|
263 }
|
|
264
|
|
265 const char *
|
|
266 msn_object_get_sha1d(const MsnObject *obj)
|
|
267 {
|
|
268 g_return_val_if_fail(obj != NULL, NULL);
|
|
269
|
|
270 return obj->sha1d;
|
|
271 }
|
|
272
|
|
273 const char *
|
|
274 msn_object_get_sha1c(const MsnObject *obj)
|
|
275 {
|
|
276 g_return_val_if_fail(obj != NULL, NULL);
|
|
277
|
|
278 return obj->sha1c;
|
|
279 }
|
|
280
|
|
281 static MsnObject *
|
|
282 msn_object_find_local(const char *sha1c)
|
|
283 {
|
|
284 GList *l;
|
|
285
|
|
286 g_return_val_if_fail(sha1c != NULL, NULL);
|
|
287
|
|
288 for (l = local_objs; l != NULL; l = l->next)
|
|
289 {
|
|
290 MsnObject *local_obj = l->data;
|
|
291
|
|
292 if (!strcmp(msn_object_get_sha1c(local_obj), sha1c))
|
|
293 return local_obj;
|
|
294 }
|
|
295
|
|
296 return NULL;
|
|
297
|
|
298 }
|
|
299
|
|
300 void
|
|
301 msn_object_set_local(MsnObject *obj)
|
|
302 {
|
|
303 g_return_if_fail(obj != NULL);
|
|
304
|
|
305 obj->local = TRUE;
|
|
306
|
|
307 local_objs = g_list_append(local_objs, obj);
|
|
308 }
|
|
309
|
|
310 void
|
|
311 msn_object_set_real_location(MsnObject *obj, const char *real_location)
|
|
312 {
|
|
313 g_return_if_fail(obj != NULL);
|
|
314
|
|
315 /* obj->local = TRUE; */
|
|
316
|
|
317 if (obj->real_location != NULL)
|
|
318 g_free(obj->real_location);
|
|
319
|
|
320 obj->real_location =
|
|
321 (real_location == NULL ? NULL : g_strdup(real_location));
|
|
322 }
|
|
323
|
|
324 const char *
|
|
325 msn_object_get_real_location(const MsnObject *obj)
|
|
326 {
|
|
327 MsnObject *local_obj;
|
|
328
|
|
329 g_return_val_if_fail(obj != NULL, NULL);
|
|
330
|
|
331 local_obj = msn_object_find_local(msn_object_get_sha1c(obj));
|
|
332
|
|
333 if (local_obj != NULL)
|
|
334 return local_obj->real_location;
|
|
335
|
|
336 return NULL;
|
|
337 }
|