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 |
149 |
curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, cred); |
curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, cred); |
150 |
g_free(cred); |
g_free(cred); |
151 |
} |
} |
152 |
} |
} else |
153 |
|
printf("gcvote: no proxy configured\n"); |
154 |
} |
} |
155 |
|
|
156 |
void gcvote_request_free(gcvote_request_t *request) { |
void gcvote_request_free(gcvote_request_t *request) { |
165 |
printf("gcvote: no references left, freeing request\n"); |
printf("gcvote: no references left, freeing request\n"); |
166 |
if(request->url) g_free(request->url); |
if(request->url) g_free(request->url); |
167 |
|
|
168 |
|
if(request->mem.ptr) |
169 |
|
g_free(request->mem.ptr); |
170 |
|
|
171 |
g_free(request); |
g_free(request); |
172 |
} |
} |
173 |
|
|
180 |
/* worker thread has already reduced its refcounter */ |
/* worker thread has already reduced its refcounter */ |
181 |
if(request->refcount < 1) { |
if(request->refcount < 1) { |
182 |
printf("gcvote: main app isn't listening anymore\n"); |
printf("gcvote: main app isn't listening anymore\n"); |
|
g_free(request->mem.ptr); |
|
183 |
return FALSE; |
return FALSE; |
184 |
} |
} |
185 |
|
|
186 |
if(request->res) { |
if(request->res) { |
187 |
printf("gcvote: curl failed\n"); |
printf("gcvote: curl failed\n"); |
|
g_free(request->mem.ptr); |
|
188 |
request->cb(NULL, request->userdata); |
request->cb(NULL, request->userdata); |
189 |
return FALSE; |
return FALSE; |
190 |
} |
} |
193 |
/* nothing could be parsed, just give up */ |
/* nothing could be parsed, just give up */ |
194 |
if(!request->mem.ptr || !request->mem.len) { |
if(!request->mem.ptr || !request->mem.len) { |
195 |
printf("gcvote: ignoring zero length reply\n"); |
printf("gcvote: ignoring zero length reply\n"); |
|
g_free(request->mem.ptr); |
|
196 |
request->cb(NULL, request->userdata); |
request->cb(NULL, request->userdata); |
197 |
return FALSE; |
return FALSE; |
198 |
} |
} |
203 |
/* parse the file and get the DOM */ |
/* parse the file and get the DOM */ |
204 |
xmlDoc *doc = xmlReadMemory(request->mem.ptr, request->mem.len, |
xmlDoc *doc = xmlReadMemory(request->mem.ptr, request->mem.len, |
205 |
NULL, NULL, 0); |
NULL, NULL, 0); |
|
g_free(request->mem.ptr); |
|
206 |
|
|
207 |
/* nothing could be parsed, just give up */ |
/* nothing could be parsed, just give up */ |
208 |
if(!doc) { |
if(!doc) { |
238 |
return FALSE; |
return FALSE; |
239 |
} |
} |
240 |
|
|
241 |
|
static void gcvotes_free(gcvote_t *votes) { |
242 |
|
while(votes) { |
243 |
|
gcvote_t *next = votes->next; |
244 |
|
if(votes->id) g_free(votes->id); |
245 |
|
g_free(votes); |
246 |
|
votes = next; |
247 |
|
} |
248 |
|
} |
249 |
|
|
250 |
|
void gcvote_save(appdata_t *appdata, cache_t *cache, curl_mem_t *mem) { |
251 |
|
if(!mem->len || !mem->ptr) return; |
252 |
|
|
253 |
|
/* save data to disk */ |
254 |
|
char *filename = g_strdup_printf("%s%s/gcvote.xml", |
255 |
|
appdata->image_path, |
256 |
|
cache->id); |
257 |
|
if(checkdir(filename) != 0) |
258 |
|
printf("gcvote: unable to create file path\n"); |
259 |
|
else { |
260 |
|
printf("gcvote: write %d bytes to %s\n", mem->len, filename); |
261 |
|
|
262 |
|
int handle = g_open(filename, O_WRONLY | O_CREAT, 0644); |
263 |
|
if(handle >= 0) { |
264 |
|
int len = write(handle, mem->ptr, mem->len); |
265 |
|
close(handle); |
266 |
|
|
267 |
|
/* if write failed, then remove the file */ |
268 |
|
if(len != mem->len) |
269 |
|
g_remove(filename); |
270 |
|
} |
271 |
|
} |
272 |
|
|
273 |
|
free(filename); |
274 |
|
} |
275 |
|
|
276 |
static void *worker_thread(void *ptr) { |
static void *worker_thread(void *ptr) { |
277 |
gcvote_request_t *request = (gcvote_request_t*)ptr; |
gcvote_request_t *request = (gcvote_request_t*)ptr; |
278 |
struct curl_httppost *formpost=NULL; |
struct curl_httppost *formpost=NULL; |
334 |
|
|
335 |
/* then cleanup the formpost chain */ |
/* then cleanup the formpost chain */ |
336 |
curl_formfree(formpost); |
curl_formfree(formpost); |
337 |
|
|
338 |
printf("gcvote: worker thread done\n"); |
printf("gcvote: worker thread done\n"); |
339 |
gcvote_request_free(request); |
gcvote_request_free(request); |
340 |
|
|
341 |
/* cause gtk main loop to handle result */ |
/* cause gtk main loop to handle result */ |
342 |
g_idle_add(gcvote_result_handler, request); |
g_idle_add(gcvote_result_handler, request); |
343 |
|
|
360 |
return NULL; |
return NULL; |
361 |
} |
} |
362 |
|
|
363 |
|
request->proxy = appdata->proxy; |
364 |
request->id += strlen(ID_PATTERN); |
request->id += strlen(ID_PATTERN); |
365 |
request->refcount = 2; // master and worker hold a reference |
request->refcount = 2; // master and worker hold a reference |
366 |
request->cb = cb; |
request->cb = cb; |
378 |
return request; |
return request; |
379 |
} |
} |
380 |
|
|
381 |
|
vote_t *gcvote_restore(appdata_t *appdata, cache_t *cache) { |
382 |
|
/* load data from disk */ |
383 |
|
char *filename = g_strdup_printf("%s%s/gcvote.xml", |
384 |
|
appdata->image_path, |
385 |
|
cache->id); |
386 |
|
|
387 |
|
printf("gcvote: trying to restore from %s\n", filename); |
388 |
|
|
389 |
|
/* no such file? */ |
390 |
|
if(!g_file_test(filename, G_FILE_TEST_EXISTS)) { |
391 |
|
printf("gcvote: no such file\n"); |
392 |
|
free(filename); |
393 |
|
return NULL; |
394 |
|
} |
395 |
|
|
396 |
|
LIBXML_TEST_VERSION; |
397 |
|
xmlDoc *doc = xmlReadFile(filename, NULL, 0); |
398 |
|
|
399 |
|
if(doc == NULL) { |
400 |
|
printf("gcvote: error, could not parse file %s\n", filename); |
401 |
|
free(filename); |
402 |
|
return NULL; |
403 |
|
} |
404 |
|
|
405 |
|
free(filename); |
406 |
|
|
407 |
|
/* in this case this will sure only return one result */ |
408 |
|
gcvote_t *votes = parse_doc(doc); |
409 |
|
|
410 |
|
if(!votes) { |
411 |
|
printf("gcvote: error, no vote found\n"); |
412 |
|
free(filename); |
413 |
|
return NULL; |
414 |
|
} |
415 |
|
|
416 |
|
vote_t *vote = g_new0(vote_t, 1); |
417 |
|
vote->quality = votes->quality; |
418 |
|
vote->votes = votes->votes; |
419 |
|
|
420 |
|
printf("gcvote: found vote %d/%d\n", vote->quality, vote->votes); |
421 |
|
|
422 |
|
gcvotes_free(votes); |
423 |
|
|
424 |
|
return vote; |
425 |
|
} |