libpurple/protocols/msnp9/object.c

changeset 21481
d52b697eaae7
child 22738
bc6dfe40f496
equal deleted inserted replaced
21480:3fdf3e905e24 21481:d52b697eaae7
1 /**
2 * @file object.c MSNObject API
3 *
4 * purple
5 *
6 * Purple 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., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 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 /* SHA1C is not always sent anymore */
96 if (obj->creator == NULL || obj->size == 0 || obj->type == 0
97 || obj->location == NULL || obj->friendly == NULL
98 || obj->sha1d == NULL /*|| obj->sha1c == NULL*/) {
99 purple_debug_error("msn", "Discarding invalid msnobj: '%s'\n", str);
100 msn_object_destroy(obj);
101 obj = NULL;
102 }
103
104 return obj;
105 }
106
107 void
108 msn_object_destroy(MsnObject *obj)
109 {
110 g_return_if_fail(obj != NULL);
111
112 g_free(obj->creator);
113 g_free(obj->location);
114 g_free(obj->friendly);
115 g_free(obj->sha1d);
116 g_free(obj->sha1c);
117
118 purple_imgstore_unref(obj->img);
119
120 if (obj->local)
121 local_objs = g_list_remove(local_objs, obj);
122
123 g_free(obj);
124 }
125
126 char *
127 msn_object_to_string(const MsnObject *obj)
128 {
129 char *str;
130 const char *sha1c;
131
132 g_return_val_if_fail(obj != NULL, NULL);
133
134 sha1c = msn_object_get_sha1c(obj);
135
136 str = g_strdup_printf("<msnobj Creator=\"%s\" Size=\"%d\" Type=\"%d\" "
137 "Location=\"%s\" Friendly=\"%s\" SHA1D=\"%s\""
138 "%s%s%s/>",
139 msn_object_get_creator(obj),
140 msn_object_get_size(obj),
141 msn_object_get_type(obj),
142 msn_object_get_location(obj),
143 msn_object_get_friendly(obj),
144 msn_object_get_sha1d(obj),
145 sha1c ? " SHA1C=\"" : "",
146 sha1c ? sha1c : "",
147 sha1c ? "\"" : "");
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 const char *
280 msn_object_get_sha1(const MsnObject *obj)
281 {
282 g_return_val_if_fail(obj != NULL, NULL);
283
284 if(obj->sha1c != NULL) {
285 return obj->sha1c;
286 } else {
287 return obj->sha1d;
288 }
289 }
290
291 static MsnObject *
292 msn_object_find_local(const char *sha1)
293 {
294 GList *l;
295
296 g_return_val_if_fail(sha1 != NULL, NULL);
297
298 for (l = local_objs; l != NULL; l = l->next)
299 {
300 MsnObject *local_obj = l->data;
301
302 if (!strcmp(msn_object_get_sha1(local_obj), sha1))
303 return local_obj;
304 }
305
306 return NULL;
307
308 }
309
310 void
311 msn_object_set_local(MsnObject *obj)
312 {
313 g_return_if_fail(obj != NULL);
314
315 obj->local = TRUE;
316
317 local_objs = g_list_append(local_objs, obj);
318 }
319
320 void
321 msn_object_set_image(MsnObject *obj, PurpleStoredImage *img)
322 {
323 g_return_if_fail(obj != NULL);
324 g_return_if_fail(img != NULL);
325
326 /* obj->local = TRUE; */
327
328 purple_imgstore_unref(obj->img);
329 obj->img = purple_imgstore_ref(img);
330 }
331
332 PurpleStoredImage *
333 msn_object_get_image(const MsnObject *obj)
334 {
335 MsnObject *local_obj;
336
337 g_return_val_if_fail(obj != NULL, NULL);
338
339 local_obj = msn_object_find_local(msn_object_get_sha1(obj));
340
341 if (local_obj != NULL)
342 return local_obj->img;
343
344 return NULL;
345 }

mercurial