--- trunk/src/gcvote.c 2009/11/04 20:28:54 159 +++ trunk/src/gcvote.c 2009/11/05 07:00:52 160 @@ -237,6 +237,15 @@ return FALSE; } +static void gcvotes_free(gcvote_t *votes) { + while(votes) { + gcvote_t *next = votes->next; + if(votes->id) g_free(votes->id); + g_free(votes); + votes = next; + } +} + void gcvote_save(appdata_t *appdata, cache_t *cache, curl_mem_t *mem) { if(!mem->len || !mem->ptr) return; @@ -324,7 +333,7 @@ /* then cleanup the formpost chain */ curl_formfree(formpost); - + printf("gcvote: worker thread done\n"); gcvote_request_free(request); @@ -367,3 +376,48 @@ return request; } +vote_t *gcvote_restore(appdata_t *appdata, cache_t *cache) { + /* load data from disk */ + char *filename = g_strdup_printf("%s%s/gcvote.xml", + appdata->image_path, + cache->id); + + printf("gcvote: trying to restore from %s\n", filename); + + /* no such file? */ + if(!g_file_test(filename, G_FILE_TEST_EXISTS)) { + printf("gcvote: no such file\n"); + free(filename); + return NULL; + } + + LIBXML_TEST_VERSION; + xmlDoc *doc = xmlReadFile(filename, NULL, 0); + + if(doc == NULL) { + printf("gcvote: error, could not parse file %s\n", filename); + free(filename); + return NULL; + } + + free(filename); + + /* in this case this will sure only return one result */ + gcvote_t *votes = parse_doc(doc); + + if(!votes) { + printf("gcvote: error, no vote found\n"); + free(filename); + return NULL; + } + + vote_t *vote = g_new0(vote_t, 1); + vote->quality = votes->quality; + vote->votes = votes->votes; + + printf("gcvote: found vote %d/%d\n", vote->quality, vote->votes); + + gcvotes_free(votes); + + return vote; +}