comparison src/protocols/msn/object.c @ 9193:502707ca1836

[gaim-migrate @ 9988] Patch by Felipe Contreras to add MSN file transfer and buddy icons. Please test and report any bugs! committer: Tailor Script <tailor@pidgin.im>
author Christian Hammond <chipx86@chipx86.com>
date Sun, 06 Jun 2004 02:39:08 +0000
parents
children ab6636c5a136
comparison
equal deleted inserted replaced
9192:5655dcd94d0f 9193:502707ca1836
1 /**
2 * @file object.c MSNObject API
3 *
4 * gaim
5 *
6 * Copyright (C) 2003 Christian Hammond <chipx86@gnupdate.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22 #include "object.h"
23
24 #define GET_STRING_TAG(field, id) \
25 if ((tag = strstr(str, id "=\"")) != NULL) \
26 { \
27 tag += strlen(id "=\""); \
28 c = strchr(tag, '"'); \
29 obj->field = g_strndup(tag, c - tag); \
30 }
31
32 #define GET_INT_TAG(field, id) \
33 if ((tag = strstr(str, id "=\"")) != NULL) \
34 { \
35 char buf[16]; \
36 tag += strlen(id "=\""); \
37 c = strchr(tag, '"'); \
38 strncpy(buf, tag, c - tag); \
39 buf[c - tag] = '\0'; \
40 obj->field = atoi(buf); \
41 }
42
43 static GList *local_objs;
44
45 MsnObject *
46 msn_object_new(void)
47 {
48 MsnObject *obj;
49
50 obj = g_new0(MsnObject, 1);
51
52 msn_object_set_type(obj, MSN_OBJECT_UNKNOWN);
53 msn_object_set_friendly(obj, "AAA=");
54
55 return obj;
56 }
57
58 MsnObject *
59 msn_object_new_from_string(const char *str)
60 {
61 MsnObject *obj;
62 char *tag, *c;
63
64 g_return_val_if_fail(str != NULL, NULL);
65 g_return_val_if_fail(!strncmp(str, "<msnobj ", 8), NULL);
66
67 obj = msn_object_new();
68
69 GET_STRING_TAG(creator, "Creator");
70 GET_INT_TAG(size, "Size");
71 GET_INT_TAG(type, "Type");
72 GET_STRING_TAG(location, "Location");
73 GET_STRING_TAG(friendly, "Friendly");
74 GET_STRING_TAG(sha1d, "SHA1D");
75 GET_STRING_TAG(sha1c, "SHA1C");
76
77 return obj;
78 }
79
80 void
81 msn_object_destroy(MsnObject *obj)
82 {
83 g_return_if_fail(obj != NULL);
84
85 if (obj->creator != NULL)
86 g_free(obj->creator);
87
88 if (obj->location != NULL)
89 g_free(obj->location);
90
91 if (obj->friendly != NULL)
92 g_free(obj->friendly);
93
94 if (obj->sha1d != NULL)
95 g_free(obj->sha1d);
96
97 if (obj->sha1c != NULL)
98 g_free(obj->sha1c);
99
100 if (obj->local)
101 local_objs = g_list_remove(local_objs, obj);
102
103 g_free(obj);
104 }
105
106 char *
107 msn_object_to_string(const MsnObject *obj)
108 {
109 char *str;
110
111 g_return_val_if_fail(obj != NULL, NULL);
112
113 str = g_strdup_printf("<msnobj Creator=\"%s\" Size=\"%d\" Type=\"%d\" "
114 "Location=\"%s\" Friendly=\"%s\" SHA1D=\"%s\" "
115 "SHA1C=\"%s\"/>",
116 msn_object_get_creator(obj),
117 msn_object_get_size(obj),
118 msn_object_get_type(obj),
119 msn_object_get_location(obj),
120 msn_object_get_friendly(obj),
121 msn_object_get_sha1d(obj),
122 msn_object_get_sha1c(obj));
123
124 return str;
125 }
126
127 void
128 msn_object_set_creator(MsnObject *obj, const char *creator)
129 {
130 g_return_if_fail(obj != NULL);
131
132 if (obj->creator != NULL)
133 g_free(obj->creator);
134
135 obj->creator = (creator == NULL ? NULL : g_strdup(creator));
136 }
137
138 void
139 msn_object_set_size(MsnObject *obj, int size)
140 {
141 g_return_if_fail(obj != NULL);
142
143 obj->size = size;
144 }
145
146 void
147 msn_object_set_type(MsnObject *obj, MsnObjectType type)
148 {
149 g_return_if_fail(obj != NULL);
150
151 obj->type = type;
152 }
153
154 void
155 msn_object_set_location(MsnObject *obj, const char *location)
156 {
157 g_return_if_fail(obj != NULL);
158
159 if (obj->location != NULL)
160 g_free(obj->location);
161
162 obj->location = (location == NULL ? NULL : g_strdup(location));
163 }
164
165 void
166 msn_object_set_friendly(MsnObject *obj, const char *friendly)
167 {
168 g_return_if_fail(obj != NULL);
169
170 if (obj->friendly != NULL)
171 g_free(obj->friendly);
172
173 obj->friendly = (friendly == NULL ? NULL : g_strdup(friendly));
174 }
175
176 void
177 msn_object_set_sha1d(MsnObject *obj, const char *sha1d)
178 {
179 g_return_if_fail(obj != NULL);
180
181 if (obj->sha1d != NULL)
182 g_free(obj->sha1d);
183
184 obj->sha1d = (sha1d == NULL ? NULL : g_strdup(sha1d));
185 }
186
187 void
188 msn_object_set_sha1c(MsnObject *obj, const char *sha1c)
189 {
190 g_return_if_fail(obj != NULL);
191
192 if (obj->sha1c != NULL)
193 g_free(obj->sha1c);
194
195 obj->sha1c = (sha1c == NULL ? NULL : g_strdup(sha1c));
196 }
197
198 const char *
199 msn_object_get_creator(const MsnObject *obj)
200 {
201 g_return_val_if_fail(obj != NULL, NULL);
202
203 return obj->creator;
204 }
205
206 int
207 msn_object_get_size(const MsnObject *obj)
208 {
209 g_return_val_if_fail(obj != NULL, 0);
210
211 return obj->size;
212 }
213
214 MsnObjectType
215 msn_object_get_type(const MsnObject *obj)
216 {
217 g_return_val_if_fail(obj != NULL, MSN_OBJECT_UNKNOWN);
218
219 return obj->type;
220 }
221
222 const char *
223 msn_object_get_location(const MsnObject *obj)
224 {
225 g_return_val_if_fail(obj != NULL, NULL);
226
227 return obj->location;
228 }
229
230 const char *
231 msn_object_get_friendly(const MsnObject *obj)
232 {
233 g_return_val_if_fail(obj != NULL, NULL);
234
235 return obj->friendly;
236 }
237
238 const char *
239 msn_object_get_sha1d(const MsnObject *obj)
240 {
241 g_return_val_if_fail(obj != NULL, NULL);
242
243 return obj->sha1d;
244 }
245
246 const char *
247 msn_object_get_sha1c(const MsnObject *obj)
248 {
249 g_return_val_if_fail(obj != NULL, NULL);
250
251 return obj->sha1c;
252 }
253
254 MsnObject *
255 msn_object_find_local(const char *sha1c)
256 {
257 GList *l;
258
259 g_return_val_if_fail(sha1c != NULL, NULL);
260
261 for (l = local_objs; l != NULL; l = l->next)
262 {
263 MsnObject *local_obj = l->data;
264
265 if (!strcmp(msn_object_get_sha1c(local_obj), sha1c))
266 return local_obj;
267 }
268
269 return NULL;
270
271 }
272
273 void
274 msn_object_set_local(MsnObject *obj)
275 {
276 g_return_if_fail(obj != NULL);
277
278 obj->local = TRUE;
279
280 local_objs = g_list_append(local_objs, obj);
281 }
282
283 void
284 msn_object_set_real_location(MsnObject *obj, const char *real_location)
285 {
286 g_return_if_fail(obj != NULL);
287
288 /* obj->local = TRUE; */
289
290 if (obj->real_location != NULL)
291 g_free(obj->real_location);
292
293 obj->real_location =
294 (real_location == NULL ? NULL : g_strdup(real_location));
295 }
296
297 const char *
298 msn_object_get_real_location(const MsnObject *obj)
299 {
300 MsnObject *local_obj;
301
302 g_return_val_if_fail(obj != NULL, NULL);
303
304 local_obj = msn_object_find_local(msn_object_get_sha1c(obj));
305
306 if (local_obj != NULL)
307 return local_obj->real_location;
308
309 return NULL;
310 }