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);
|
|
79 g_return_val_if_fail(!strncmp(str, "<msnobj ", 8), NULL);
|
|
80
|
|
81 obj = msn_object_new();
|
|
82
|
|
83 GET_STRING_TAG(creator, "Creator");
|
|
84 GET_INT_TAG(size, "Size");
|
|
85 GET_INT_TAG(type, "Type");
|
|
86 GET_STRING_TAG(location, "Location");
|
|
87 GET_STRING_TAG(friendly, "Friendly");
|
|
88 GET_STRING_TAG(sha1d, "SHA1D");
|
|
89 GET_STRING_TAG(sha1c, "SHA1C");
|
|
90
|
|
91 /* If we are missing any of the required elements then discard the object */
|
|
92 if (obj->creator == NULL || obj->size == 0 || obj->type == 0
|
|
93 || obj->location == NULL || obj->friendly == NULL
|
|
94 || obj->sha1d == NULL || obj->sha1c == NULL) {
|
|
95 msn_object_destroy(obj);
|
|
96 obj = NULL;
|
|
97 }
|
|
98
|
|
99 return obj;
|
|
100 }
|
|
101
|
|
102 void
|
|
103 msn_object_destroy(MsnObject *obj)
|
|
104 {
|
|
105 g_return_if_fail(obj != NULL);
|
|
106
|
|
107 if (obj->creator != NULL)
|
|
108 g_free(obj->creator);
|
|
109
|
|
110 if (obj->location != NULL)
|
|
111 g_free(obj->location);
|
|
112
|
|
113 if (obj->real_location != NULL)
|
|
114 g_free(obj->real_location);
|
|
115
|
|
116 if (obj->friendly != NULL)
|
|
117 g_free(obj->friendly);
|
|
118
|
|
119 if (obj->sha1d != NULL)
|
|
120 g_free(obj->sha1d);
|
|
121
|
|
122 if (obj->sha1c != NULL)
|
|
123 g_free(obj->sha1c);
|
|
124
|
|
125 if (obj->local)
|
|
126 local_objs = g_list_remove(local_objs, obj);
|
|
127
|
|
128 g_free(obj);
|
|
129 }
|
|
130
|
|
131 char *
|
|
132 msn_object_to_string(const MsnObject *obj)
|
|
133 {
|
|
134 char *str;
|
|
135
|
|
136 g_return_val_if_fail(obj != NULL, NULL);
|
|
137
|
|
138 str = g_strdup_printf("<msnobj Creator=\"%s\" Size=\"%d\" Type=\"%d\" "
|
|
139 "Location=\"%s\" Friendly=\"%s\" SHA1D=\"%s\" "
|
|
140 "SHA1C=\"%s\"/>",
|
|
141 msn_object_get_creator(obj),
|
|
142 msn_object_get_size(obj),
|
|
143 msn_object_get_type(obj),
|
|
144 msn_object_get_location(obj),
|
|
145 msn_object_get_friendly(obj),
|
|
146 msn_object_get_sha1d(obj),
|
|
147 msn_object_get_sha1c(obj));
|
|
148
|
|
149 return str;
|
|
150 }
|
|
151
|
|
152 void
|
|
153 msn_object_set_creator(MsnObject *obj, const char *creator)
|
|
154 {
|
|
155 g_return_if_fail(obj != NULL);
|
|
156
|
|
157 if (obj->creator != NULL)
|
|
158 g_free(obj->creator);
|
|
159
|
|
160 obj->creator = (creator == NULL ? NULL : g_strdup(creator));
|
|
161 }
|
|
162
|
|
163 void
|
|
164 msn_object_set_size(MsnObject *obj, int size)
|
|
165 {
|
|
166 g_return_if_fail(obj != NULL);
|
|
167
|
|
168 obj->size = size;
|
|
169 }
|
|
170
|
|
171 void
|
|
172 msn_object_set_type(MsnObject *obj, MsnObjectType type)
|
|
173 {
|
|
174 g_return_if_fail(obj != NULL);
|
|
175
|
|
176 obj->type = type;
|
|
177 }
|
|
178
|
|
179 void
|
|
180 msn_object_set_location(MsnObject *obj, const char *location)
|
|
181 {
|
|
182 g_return_if_fail(obj != NULL);
|
|
183
|
|
184 if (obj->location != NULL)
|
|
185 g_free(obj->location);
|
|
186
|
|
187 obj->location = (location == NULL ? NULL : g_strdup(location));
|
|
188 }
|
|
189
|
|
190 void
|
|
191 msn_object_set_friendly(MsnObject *obj, const char *friendly)
|
|
192 {
|
|
193 g_return_if_fail(obj != NULL);
|
|
194
|
|
195 if (obj->friendly != NULL)
|
|
196 g_free(obj->friendly);
|
|
197
|
|
198 obj->friendly = (friendly == NULL ? NULL : g_strdup(friendly));
|
|
199 }
|
|
200
|
|
201 void
|
|
202 msn_object_set_sha1d(MsnObject *obj, const char *sha1d)
|
|
203 {
|
|
204 g_return_if_fail(obj != NULL);
|
|
205
|
|
206 if (obj->sha1d != NULL)
|
|
207 g_free(obj->sha1d);
|
|
208
|
|
209 obj->sha1d = (sha1d == NULL ? NULL : g_strdup(sha1d));
|
|
210 }
|
|
211
|
|
212 void
|
|
213 msn_object_set_sha1c(MsnObject *obj, const char *sha1c)
|
|
214 {
|
|
215 g_return_if_fail(obj != NULL);
|
|
216
|
|
217 if (obj->sha1c != NULL)
|
|
218 g_free(obj->sha1c);
|
|
219
|
|
220 obj->sha1c = (sha1c == NULL ? NULL : g_strdup(sha1c));
|
|
221 }
|
|
222
|
|
223 const char *
|
|
224 msn_object_get_creator(const MsnObject *obj)
|
|
225 {
|
|
226 g_return_val_if_fail(obj != NULL, NULL);
|
|
227
|
|
228 return obj->creator;
|
|
229 }
|
|
230
|
|
231 int
|
|
232 msn_object_get_size(const MsnObject *obj)
|
|
233 {
|
|
234 g_return_val_if_fail(obj != NULL, 0);
|
|
235
|
|
236 return obj->size;
|
|
237 }
|
|
238
|
|
239 MsnObjectType
|
|
240 msn_object_get_type(const MsnObject *obj)
|
|
241 {
|
|
242 g_return_val_if_fail(obj != NULL, MSN_OBJECT_UNKNOWN);
|
|
243
|
|
244 return obj->type;
|
|
245 }
|
|
246
|
|
247 const char *
|
|
248 msn_object_get_location(const MsnObject *obj)
|
|
249 {
|
|
250 g_return_val_if_fail(obj != NULL, NULL);
|
|
251
|
|
252 return obj->location;
|
|
253 }
|
|
254
|
|
255 const char *
|
|
256 msn_object_get_friendly(const MsnObject *obj)
|
|
257 {
|
|
258 g_return_val_if_fail(obj != NULL, NULL);
|
|
259
|
|
260 return obj->friendly;
|
|
261 }
|
|
262
|
|
263 const char *
|
|
264 msn_object_get_sha1d(const MsnObject *obj)
|
|
265 {
|
|
266 g_return_val_if_fail(obj != NULL, NULL);
|
|
267
|
|
268 return obj->sha1d;
|
|
269 }
|
|
270
|
|
271 const char *
|
|
272 msn_object_get_sha1c(const MsnObject *obj)
|
|
273 {
|
|
274 g_return_val_if_fail(obj != NULL, NULL);
|
|
275
|
|
276 return obj->sha1c;
|
|
277 }
|
|
278
|
|
279 static MsnObject *
|
|
280 msn_object_find_local(const char *sha1c)
|
|
281 {
|
|
282 GList *l;
|
|
283
|
|
284 g_return_val_if_fail(sha1c != NULL, NULL);
|
|
285
|
|
286 for (l = local_objs; l != NULL; l = l->next)
|
|
287 {
|
|
288 MsnObject *local_obj = l->data;
|
|
289
|
|
290 if (!strcmp(msn_object_get_sha1c(local_obj), sha1c))
|
|
291 return local_obj;
|
|
292 }
|
|
293
|
|
294 return NULL;
|
|
295
|
|
296 }
|
|
297
|
|
298 void
|
|
299 msn_object_set_local(MsnObject *obj)
|
|
300 {
|
|
301 g_return_if_fail(obj != NULL);
|
|
302
|
|
303 obj->local = TRUE;
|
|
304
|
|
305 local_objs = g_list_append(local_objs, obj);
|
|
306 }
|
|
307
|
|
308 void
|
|
309 msn_object_set_real_location(MsnObject *obj, const char *real_location)
|
|
310 {
|
|
311 g_return_if_fail(obj != NULL);
|
|
312
|
|
313 /* obj->local = TRUE; */
|
|
314
|
|
315 if (obj->real_location != NULL)
|
|
316 g_free(obj->real_location);
|
|
317
|
|
318 obj->real_location =
|
|
319 (real_location == NULL ? NULL : g_strdup(real_location));
|
|
320 }
|
|
321
|
|
322 const char *
|
|
323 msn_object_get_real_location(const MsnObject *obj)
|
|
324 {
|
|
325 MsnObject *local_obj;
|
|
326
|
|
327 g_return_val_if_fail(obj != NULL, NULL);
|
|
328
|
|
329 local_obj = msn_object_find_local(msn_object_get_sha1c(obj));
|
|
330
|
|
331 if (local_obj != NULL)
|
|
332 return local_obj->real_location;
|
|
333
|
|
334 return NULL;
|
|
335 }
|