Mercurial > pidgin
annotate src/protocols/msn/object.c @ 9995:792a7409f6b0
[gaim-migrate @ 10908]
Baby steps to the kitchen...
Look man, just because I have a goldfish around my neck doesn't mean
I'm crazy!
committer: Tailor Script <tailor@pidgin.im>
author | Mark Doliner <mark@kingant.net> |
---|---|
date | Thu, 09 Sep 2004 04:08:50 +0000 |
parents | 54377b120d19 |
children | 9d839c9f6c2d |
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, '"'); \ | |
31 obj->field = g_strndup(tag, c - tag); \ | |
32 } | |
33 | |
34 #define GET_INT_TAG(field, id) \ | |
35 if ((tag = strstr(str, id "=\"")) != NULL) \ | |
36 { \ | |
37 char buf[16]; \ | |
9823 | 38 size_t offset; \ |
9193 | 39 tag += strlen(id "=\""); \ |
40 c = strchr(tag, '"'); \ | |
9823 | 41 if (c != NULL) \ |
42 { \ | |
43 memset(buf, 0, sizeof(buf)); \ | |
44 offset = c - tag; \ | |
45 if (offset >= sizeof(buf)) \ | |
46 offset = sizeof(buf) - 1; \ | |
47 strncpy(buf, tag, offset); \ | |
48 obj->field = atoi(buf); \ | |
49 } \ | |
9193 | 50 } |
51 | |
52 static GList *local_objs; | |
53 | |
54 MsnObject * | |
55 msn_object_new(void) | |
56 { | |
57 MsnObject *obj; | |
58 | |
59 obj = g_new0(MsnObject, 1); | |
60 | |
61 msn_object_set_type(obj, MSN_OBJECT_UNKNOWN); | |
62 msn_object_set_friendly(obj, "AAA="); | |
63 | |
64 return obj; | |
65 } | |
66 | |
67 MsnObject * | |
68 msn_object_new_from_string(const char *str) | |
69 { | |
70 MsnObject *obj; | |
71 char *tag, *c; | |
72 | |
73 g_return_val_if_fail(str != NULL, NULL); | |
74 g_return_val_if_fail(!strncmp(str, "<msnobj ", 8), NULL); | |
75 | |
76 obj = msn_object_new(); | |
77 | |
78 GET_STRING_TAG(creator, "Creator"); | |
79 GET_INT_TAG(size, "Size"); | |
80 GET_INT_TAG(type, "Type"); | |
81 GET_STRING_TAG(location, "Location"); | |
82 GET_STRING_TAG(friendly, "Friendly"); | |
83 GET_STRING_TAG(sha1d, "SHA1D"); | |
84 GET_STRING_TAG(sha1c, "SHA1C"); | |
85 | |
9776 | 86 /* If we are missing any of the required elements then discard the object */ |
87 if (obj->creator == NULL || obj->size == 0 || obj->type == 0 | |
88 || obj->location == NULL || obj->friendly == NULL | |
89 || obj->sha1d == NULL || obj->sha1c == NULL) { | |
90 msn_object_destroy(obj); | |
91 obj = NULL; | |
92 } | |
93 | |
9193 | 94 return obj; |
95 } | |
96 | |
97 void | |
98 msn_object_destroy(MsnObject *obj) | |
99 { | |
100 g_return_if_fail(obj != NULL); | |
101 | |
102 if (obj->creator != NULL) | |
103 g_free(obj->creator); | |
104 | |
105 if (obj->location != NULL) | |
106 g_free(obj->location); | |
107 | |
108 if (obj->friendly != NULL) | |
109 g_free(obj->friendly); | |
110 | |
111 if (obj->sha1d != NULL) | |
112 g_free(obj->sha1d); | |
113 | |
114 if (obj->sha1c != NULL) | |
115 g_free(obj->sha1c); | |
9198
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
116 |
9193 | 117 if (obj->local) |
118 local_objs = g_list_remove(local_objs, obj); | |
119 | |
120 g_free(obj); | |
121 } | |
122 | |
123 char * | |
124 msn_object_to_string(const MsnObject *obj) | |
125 { | |
126 char *str; | |
127 | |
128 g_return_val_if_fail(obj != NULL, NULL); | |
129 | |
130 str = g_strdup_printf("<msnobj Creator=\"%s\" Size=\"%d\" Type=\"%d\" " | |
131 "Location=\"%s\" Friendly=\"%s\" SHA1D=\"%s\" " | |
132 "SHA1C=\"%s\"/>", | |
133 msn_object_get_creator(obj), | |
134 msn_object_get_size(obj), | |
135 msn_object_get_type(obj), | |
136 msn_object_get_location(obj), | |
137 msn_object_get_friendly(obj), | |
138 msn_object_get_sha1d(obj), | |
139 msn_object_get_sha1c(obj)); | |
140 | |
141 return str; | |
142 } | |
143 | |
144 void | |
145 msn_object_set_creator(MsnObject *obj, const char *creator) | |
146 { | |
147 g_return_if_fail(obj != NULL); | |
148 | |
149 if (obj->creator != NULL) | |
150 g_free(obj->creator); | |
151 | |
152 obj->creator = (creator == NULL ? NULL : g_strdup(creator)); | |
153 } | |
154 | |
155 void | |
156 msn_object_set_size(MsnObject *obj, int size) | |
157 { | |
158 g_return_if_fail(obj != NULL); | |
159 | |
160 obj->size = size; | |
161 } | |
162 | |
163 void | |
164 msn_object_set_type(MsnObject *obj, MsnObjectType type) | |
165 { | |
166 g_return_if_fail(obj != NULL); | |
167 | |
168 obj->type = type; | |
169 } | |
170 | |
171 void | |
172 msn_object_set_location(MsnObject *obj, const char *location) | |
173 { | |
174 g_return_if_fail(obj != NULL); | |
175 | |
176 if (obj->location != NULL) | |
177 g_free(obj->location); | |
178 | |
179 obj->location = (location == NULL ? NULL : g_strdup(location)); | |
180 } | |
181 | |
182 void | |
183 msn_object_set_friendly(MsnObject *obj, const char *friendly) | |
184 { | |
185 g_return_if_fail(obj != NULL); | |
186 | |
187 if (obj->friendly != NULL) | |
188 g_free(obj->friendly); | |
189 | |
190 obj->friendly = (friendly == NULL ? NULL : g_strdup(friendly)); | |
191 } | |
192 | |
193 void | |
194 msn_object_set_sha1d(MsnObject *obj, const char *sha1d) | |
195 { | |
196 g_return_if_fail(obj != NULL); | |
197 | |
198 if (obj->sha1d != NULL) | |
199 g_free(obj->sha1d); | |
200 | |
201 obj->sha1d = (sha1d == NULL ? NULL : g_strdup(sha1d)); | |
202 } | |
203 | |
204 void | |
205 msn_object_set_sha1c(MsnObject *obj, const char *sha1c) | |
206 { | |
207 g_return_if_fail(obj != NULL); | |
208 | |
209 if (obj->sha1c != NULL) | |
210 g_free(obj->sha1c); | |
211 | |
212 obj->sha1c = (sha1c == NULL ? NULL : g_strdup(sha1c)); | |
213 } | |
214 | |
215 const char * | |
216 msn_object_get_creator(const MsnObject *obj) | |
217 { | |
218 g_return_val_if_fail(obj != NULL, NULL); | |
219 | |
220 return obj->creator; | |
221 } | |
222 | |
223 int | |
224 msn_object_get_size(const MsnObject *obj) | |
225 { | |
226 g_return_val_if_fail(obj != NULL, 0); | |
227 | |
228 return obj->size; | |
229 } | |
230 | |
231 MsnObjectType | |
232 msn_object_get_type(const MsnObject *obj) | |
233 { | |
234 g_return_val_if_fail(obj != NULL, MSN_OBJECT_UNKNOWN); | |
235 | |
236 return obj->type; | |
237 } | |
238 | |
239 const char * | |
240 msn_object_get_location(const MsnObject *obj) | |
241 { | |
242 g_return_val_if_fail(obj != NULL, NULL); | |
243 | |
244 return obj->location; | |
245 } | |
246 | |
247 const char * | |
248 msn_object_get_friendly(const MsnObject *obj) | |
249 { | |
250 g_return_val_if_fail(obj != NULL, NULL); | |
251 | |
252 return obj->friendly; | |
253 } | |
254 | |
255 const char * | |
256 msn_object_get_sha1d(const MsnObject *obj) | |
257 { | |
258 g_return_val_if_fail(obj != NULL, NULL); | |
259 | |
260 return obj->sha1d; | |
261 } | |
262 | |
263 const char * | |
264 msn_object_get_sha1c(const MsnObject *obj) | |
265 { | |
266 g_return_val_if_fail(obj != NULL, NULL); | |
267 | |
268 return obj->sha1c; | |
269 } | |
270 | |
271 MsnObject * | |
272 msn_object_find_local(const char *sha1c) | |
273 { | |
274 GList *l; | |
9198
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
275 |
9193 | 276 g_return_val_if_fail(sha1c != NULL, NULL); |
277 | |
278 for (l = local_objs; l != NULL; l = l->next) | |
279 { | |
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); | |
9198
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
304 |
9193 | 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; | |
9198
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
318 |
9193 | 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; | |
9198
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
325 |
9193 | 326 return NULL; |
327 } |