22 |
#include <libxml/parser.h> |
#include <libxml/parser.h> |
23 |
#include <libxml/tree.h> |
#include <libxml/tree.h> |
24 |
|
|
25 |
|
#include <glib/gstdio.h> |
26 |
|
|
27 |
|
#include <fcntl.h> |
28 |
|
|
29 |
#ifndef LIBXML_TREE_ENABLED |
#ifndef LIBXML_TREE_ENABLED |
30 |
#error "Tree not enabled in libxml" |
#error "Tree not enabled in libxml" |
31 |
#endif |
#endif |
164 |
printf("gcvote: no references left, freeing request\n"); |
printf("gcvote: no references left, freeing request\n"); |
165 |
if(request->url) g_free(request->url); |
if(request->url) g_free(request->url); |
166 |
|
|
167 |
|
if(request->mem.ptr) |
168 |
|
g_free(request->mem.ptr); |
169 |
|
|
170 |
g_free(request); |
g_free(request); |
171 |
} |
} |
172 |
|
|
179 |
/* worker thread has already reduced its refcounter */ |
/* worker thread has already reduced its refcounter */ |
180 |
if(request->refcount < 1) { |
if(request->refcount < 1) { |
181 |
printf("gcvote: main app isn't listening anymore\n"); |
printf("gcvote: main app isn't listening anymore\n"); |
|
g_free(request->mem.ptr); |
|
182 |
return FALSE; |
return FALSE; |
183 |
} |
} |
184 |
|
|
185 |
if(request->res) { |
if(request->res) { |
186 |
printf("gcvote: curl failed\n"); |
printf("gcvote: curl failed\n"); |
|
g_free(request->mem.ptr); |
|
187 |
request->cb(NULL, request->userdata); |
request->cb(NULL, request->userdata); |
188 |
return FALSE; |
return FALSE; |
189 |
} |
} |
192 |
/* nothing could be parsed, just give up */ |
/* nothing could be parsed, just give up */ |
193 |
if(!request->mem.ptr || !request->mem.len) { |
if(!request->mem.ptr || !request->mem.len) { |
194 |
printf("gcvote: ignoring zero length reply\n"); |
printf("gcvote: ignoring zero length reply\n"); |
|
g_free(request->mem.ptr); |
|
195 |
request->cb(NULL, request->userdata); |
request->cb(NULL, request->userdata); |
196 |
return FALSE; |
return FALSE; |
197 |
} |
} |
202 |
/* parse the file and get the DOM */ |
/* parse the file and get the DOM */ |
203 |
xmlDoc *doc = xmlReadMemory(request->mem.ptr, request->mem.len, |
xmlDoc *doc = xmlReadMemory(request->mem.ptr, request->mem.len, |
204 |
NULL, NULL, 0); |
NULL, NULL, 0); |
|
g_free(request->mem.ptr); |
|
205 |
|
|
206 |
/* nothing could be parsed, just give up */ |
/* nothing could be parsed, just give up */ |
207 |
if(!doc) { |
if(!doc) { |
237 |
return FALSE; |
return FALSE; |
238 |
} |
} |
239 |
|
|
240 |
|
void gcvote_save(appdata_t *appdata, cache_t *cache, curl_mem_t *mem) { |
241 |
|
if(!mem->len || !mem->ptr) return; |
242 |
|
|
243 |
|
/* save data to disk */ |
244 |
|
char *filename = g_strdup_printf("%s%s/gcvote.xml", |
245 |
|
appdata->image_path, |
246 |
|
cache->id); |
247 |
|
if(checkdir(filename) != 0) |
248 |
|
printf("gcvote: unable to create file path\n"); |
249 |
|
else { |
250 |
|
printf("gcvote: write %d bytes to %s\n", mem->len, filename); |
251 |
|
|
252 |
|
int handle = g_open(filename, O_WRONLY | O_CREAT, 0644); |
253 |
|
if(handle >= 0) { |
254 |
|
int len = write(handle, mem->ptr, mem->len); |
255 |
|
close(handle); |
256 |
|
|
257 |
|
/* if write failed, then remove the file */ |
258 |
|
if(len != mem->len) |
259 |
|
g_remove(filename); |
260 |
|
} |
261 |
|
} |
262 |
|
|
263 |
|
free(filename); |
264 |
|
} |
265 |
|
|
266 |
static void *worker_thread(void *ptr) { |
static void *worker_thread(void *ptr) { |
267 |
gcvote_request_t *request = (gcvote_request_t*)ptr; |
gcvote_request_t *request = (gcvote_request_t*)ptr; |
268 |
struct curl_httppost *formpost=NULL; |
struct curl_httppost *formpost=NULL; |
327 |
|
|
328 |
printf("gcvote: worker thread done\n"); |
printf("gcvote: worker thread done\n"); |
329 |
gcvote_request_free(request); |
gcvote_request_free(request); |
330 |
|
|
331 |
/* cause gtk main loop to handle result */ |
/* cause gtk main loop to handle result */ |
332 |
g_idle_add(gcvote_result_handler, request); |
g_idle_add(gcvote_result_handler, request); |
333 |
|
|