comparison libgaim/protocols/msn/object.c @ 20389:e354528c4163

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