Mercurial > pidgin.yaz
annotate src/protocols/msn/object.c @ 10116:9f358a718f38
[gaim-migrate @ 11153]
I went through every commit email on oldstatus since we branched, and merged
relevant things to HEAD that had only been applied on oldstatus.
committer: Tailor Script <tailor@pidgin.im>
author | Stu Tomlinson <stu@nosnilmot.com> |
---|---|
date | Mon, 18 Oct 2004 19:02:33 +0000 |
parents | 9d839c9f6c2d |
children | 77ef3f2f0df8 |
rev | line source |
---|---|
9193 | 1 /** |
2 * @file object.c MSNObject API | |
3 * | |
4 * gaim | |
5 * | |
9198
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
6 * Gaim is the legal property of its developers, whose names are too numerous |
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
7 * to list here. Please refer to the COPYRIGHT file distributed with this |
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
8 * source distribution. |
9193 | 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, '"'); \ | |
10116 | 31 if (c != NULL) \ |
32 { \ | |
33 if (obj->field != NULL) \ | |
34 g_free(obj->field); \ | |
35 obj->field = g_strndup(tag, c - tag); \ | |
36 } \ | |
9193 | 37 } |
38 | |
39 #define GET_INT_TAG(field, id) \ | |
40 if ((tag = strstr(str, id "=\"")) != NULL) \ | |
41 { \ | |
42 char buf[16]; \ | |
9823 | 43 size_t offset; \ |
9193 | 44 tag += strlen(id "=\""); \ |
45 c = strchr(tag, '"'); \ | |
9823 | 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 } \ | |
9193 | 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 | |
9776 | 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 | |
9193 | 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->friendly != NULL) | |
114 g_free(obj->friendly); | |
115 | |
116 if (obj->sha1d != NULL) | |
117 g_free(obj->sha1d); | |
118 | |
119 if (obj->sha1c != NULL) | |
120 g_free(obj->sha1c); | |
9198
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
121 |
9193 | 122 if (obj->local) |
123 local_objs = g_list_remove(local_objs, obj); | |
124 | |
125 g_free(obj); | |
126 } | |
127 | |
128 char * | |
129 msn_object_to_string(const MsnObject *obj) | |
130 { | |
131 char *str; | |
132 | |
133 g_return_val_if_fail(obj != NULL, NULL); | |
134 | |
135 str = g_strdup_printf("<msnobj Creator=\"%s\" Size=\"%d\" Type=\"%d\" " | |
136 "Location=\"%s\" Friendly=\"%s\" SHA1D=\"%s\" " | |
137 "SHA1C=\"%s\"/>", | |
138 msn_object_get_creator(obj), | |
139 msn_object_get_size(obj), | |
140 msn_object_get_type(obj), | |
141 msn_object_get_location(obj), | |
142 msn_object_get_friendly(obj), | |
143 msn_object_get_sha1d(obj), | |
144 msn_object_get_sha1c(obj)); | |
145 | |
146 return str; | |
147 } | |
148 | |
149 void | |
150 msn_object_set_creator(MsnObject *obj, const char *creator) | |
151 { | |
152 g_return_if_fail(obj != NULL); | |
153 | |
154 if (obj->creator != NULL) | |
155 g_free(obj->creator); | |
156 | |
157 obj->creator = (creator == NULL ? NULL : g_strdup(creator)); | |
158 } | |
159 | |
160 void | |
161 msn_object_set_size(MsnObject *obj, int size) | |
162 { | |
163 g_return_if_fail(obj != NULL); | |
164 | |
165 obj->size = size; | |
166 } | |
167 | |
168 void | |
169 msn_object_set_type(MsnObject *obj, MsnObjectType type) | |
170 { | |
171 g_return_if_fail(obj != NULL); | |
172 | |
173 obj->type = type; | |
174 } | |
175 | |
176 void | |
177 msn_object_set_location(MsnObject *obj, const char *location) | |
178 { | |
179 g_return_if_fail(obj != NULL); | |
180 | |
181 if (obj->location != NULL) | |
182 g_free(obj->location); | |
183 | |
184 obj->location = (location == NULL ? NULL : g_strdup(location)); | |
185 } | |
186 | |
187 void | |
188 msn_object_set_friendly(MsnObject *obj, const char *friendly) | |
189 { | |
190 g_return_if_fail(obj != NULL); | |
191 | |
192 if (obj->friendly != NULL) | |
193 g_free(obj->friendly); | |
194 | |
195 obj->friendly = (friendly == NULL ? NULL : g_strdup(friendly)); | |
196 } | |
197 | |
198 void | |
199 msn_object_set_sha1d(MsnObject *obj, const char *sha1d) | |
200 { | |
201 g_return_if_fail(obj != NULL); | |
202 | |
203 if (obj->sha1d != NULL) | |
204 g_free(obj->sha1d); | |
205 | |
206 obj->sha1d = (sha1d == NULL ? NULL : g_strdup(sha1d)); | |
207 } | |
208 | |
209 void | |
210 msn_object_set_sha1c(MsnObject *obj, const char *sha1c) | |
211 { | |
212 g_return_if_fail(obj != NULL); | |
213 | |
214 if (obj->sha1c != NULL) | |
215 g_free(obj->sha1c); | |
216 | |
217 obj->sha1c = (sha1c == NULL ? NULL : g_strdup(sha1c)); | |
218 } | |
219 | |
220 const char * | |
221 msn_object_get_creator(const MsnObject *obj) | |
222 { | |
223 g_return_val_if_fail(obj != NULL, NULL); | |
224 | |
225 return obj->creator; | |
226 } | |
227 | |
228 int | |
229 msn_object_get_size(const MsnObject *obj) | |
230 { | |
231 g_return_val_if_fail(obj != NULL, 0); | |
232 | |
233 return obj->size; | |
234 } | |
235 | |
236 MsnObjectType | |
237 msn_object_get_type(const MsnObject *obj) | |
238 { | |
239 g_return_val_if_fail(obj != NULL, MSN_OBJECT_UNKNOWN); | |
240 | |
241 return obj->type; | |
242 } | |
243 | |
244 const char * | |
245 msn_object_get_location(const MsnObject *obj) | |
246 { | |
247 g_return_val_if_fail(obj != NULL, NULL); | |
248 | |
249 return obj->location; | |
250 } | |
251 | |
252 const char * | |
253 msn_object_get_friendly(const MsnObject *obj) | |
254 { | |
255 g_return_val_if_fail(obj != NULL, NULL); | |
256 | |
257 return obj->friendly; | |
258 } | |
259 | |
260 const char * | |
261 msn_object_get_sha1d(const MsnObject *obj) | |
262 { | |
263 g_return_val_if_fail(obj != NULL, NULL); | |
264 | |
265 return obj->sha1d; | |
266 } | |
267 | |
268 const char * | |
269 msn_object_get_sha1c(const MsnObject *obj) | |
270 { | |
271 g_return_val_if_fail(obj != NULL, NULL); | |
272 | |
273 return obj->sha1c; | |
274 } | |
275 | |
276 MsnObject * | |
277 msn_object_find_local(const char *sha1c) | |
278 { | |
279 GList *l; | |
9198
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
280 |
9193 | 281 g_return_val_if_fail(sha1c != NULL, NULL); |
282 | |
283 for (l = local_objs; l != NULL; l = l->next) | |
284 { | |
285 MsnObject *local_obj = l->data; | |
286 | |
287 if (!strcmp(msn_object_get_sha1c(local_obj), sha1c)) | |
288 return local_obj; | |
289 } | |
290 | |
291 return NULL; | |
292 | |
293 } | |
294 | |
295 void | |
296 msn_object_set_local(MsnObject *obj) | |
297 { | |
298 g_return_if_fail(obj != NULL); | |
299 | |
300 obj->local = TRUE; | |
301 | |
302 local_objs = g_list_append(local_objs, obj); | |
303 } | |
304 | |
305 void | |
306 msn_object_set_real_location(MsnObject *obj, const char *real_location) | |
307 { | |
308 g_return_if_fail(obj != NULL); | |
9198
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
309 |
9193 | 310 /* obj->local = TRUE; */ |
311 | |
312 if (obj->real_location != NULL) | |
313 g_free(obj->real_location); | |
314 | |
315 obj->real_location = | |
316 (real_location == NULL ? NULL : g_strdup(real_location)); | |
317 } | |
318 | |
319 const char * | |
320 msn_object_get_real_location(const MsnObject *obj) | |
321 { | |
322 MsnObject *local_obj; | |
9198
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
323 |
9193 | 324 g_return_val_if_fail(obj != NULL, NULL); |
325 | |
326 local_obj = msn_object_find_local(msn_object_get_sha1c(obj)); | |
327 | |
328 if (local_obj != NULL) | |
329 return local_obj->real_location; | |
9198
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
330 |
9193 | 331 return NULL; |
332 } |