Diff of /trunk/src/gcvote.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 159 by harbaum, Wed Nov 4 20:28:54 2009 UTC revision 165 by harbaum, Sun Nov 8 20:32:55 2009 UTC
# Line 149  static void curl_set_proxy(CURL *curl, p Line 149  static void curl_set_proxy(CURL *curl, p
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) {
# Line 176  static gboolean gcvote_result_handler(gp Line 177  static gboolean gcvote_result_handler(gp
177    
178    printf("gcvote: result handler\n");    printf("gcvote: result handler\n");
179    
180    /* worker thread has already reduced its refcounter */    if(request->refcount < 2) {
   if(request->refcount < 1) {  
181      printf("gcvote: main app isn't listening anymore\n");      printf("gcvote: main app isn't listening anymore\n");
182        gcvote_request_free(request);
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");
188      request->cb(NULL, request->userdata);      request->cb(NULL, request->userdata);
189        gcvote_request_free(request);
190      return FALSE;      return FALSE;
191    }    }
192    
# Line 193  static gboolean gcvote_result_handler(gp Line 195  static gboolean gcvote_result_handler(gp
195    if(!request->mem.ptr || !request->mem.len) {    if(!request->mem.ptr || !request->mem.len) {
196      printf("gcvote: ignoring zero length reply\n");      printf("gcvote: ignoring zero length reply\n");
197      request->cb(NULL, request->userdata);      request->cb(NULL, request->userdata);
198        gcvote_request_free(request);
199      return FALSE;      return FALSE;
200    }    }
201    
# Line 206  static gboolean gcvote_result_handler(gp Line 209  static gboolean gcvote_result_handler(gp
209    /* nothing could be parsed, just give up */    /* nothing could be parsed, just give up */
210    if(!doc) {    if(!doc) {
211      request->cb(NULL, request->userdata);      request->cb(NULL, request->userdata);
212        gcvote_request_free(request);
213      return FALSE;      return FALSE;
214    }    }
215    
# Line 234  static gboolean gcvote_result_handler(gp Line 238  static gboolean gcvote_result_handler(gp
238    } else    } else
239      printf("gcvote: no vote found\n");      printf("gcvote: no vote found\n");
240    
241      gcvote_request_free(request);
242    return FALSE;    return FALSE;
243  }  }
244    
245    static void gcvotes_free(gcvote_t *votes) {
246      while(votes) {
247        gcvote_t *next = votes->next;
248        if(votes->id) g_free(votes->id);
249        g_free(votes);
250        votes = next;
251      }
252    }
253    
254  void gcvote_save(appdata_t *appdata, cache_t *cache, curl_mem_t *mem) {  void gcvote_save(appdata_t *appdata, cache_t *cache, curl_mem_t *mem) {
255    if(!mem->len || !mem->ptr) return;    if(!mem->len || !mem->ptr) return;
256    
# Line 289  static void *worker_thread(void *ptr) { Line 303  static void *worker_thread(void *ptr) {
303    CURL *curl = curl_easy_init();    CURL *curl = curl_easy_init();
304    if(!curl) {    if(!curl) {
305      curl_formfree(formpost);      curl_formfree(formpost);
     gcvote_request_free(request);  
306    
307      /* callback anyway, so main loop can also clean up */      /* callback anyway, so main loop can also clean up */
308      g_idle_add(gcvote_result_handler, request);      g_idle_add(gcvote_result_handler, request);
# Line 319  static void *worker_thread(void *ptr) { Line 332  static void *worker_thread(void *ptr) {
332    
333    /* always cleanup */    /* always cleanup */
334    curl_easy_cleanup(curl);    curl_easy_cleanup(curl);
335    
336    curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &request->response);    printf("gcvote: 1\n");
337    
338    /* then cleanup the formpost chain */    /* then cleanup the formpost chain */
339    curl_formfree(formpost);    curl_formfree(formpost);
340    
341      printf("gcvote: 2\n");
342    
343      /* cause gtk main loop to handle result only if main loop */
344      /* is still interested. Don't free request then, since the */
345      /* gcvote_result_handler will do this */
346      if(request->refcount > 1)
347        g_idle_add(gcvote_result_handler, request);
348      else
349        gcvote_request_free(request);
350    
351    printf("gcvote: worker thread done\n");    printf("gcvote: worker thread done\n");
   gcvote_request_free(request);  
352    
   /* cause gtk main loop to handle result */  
   g_idle_add(gcvote_result_handler, request);  
   
   printf("gcvote: thread terminating\n");  
   
353    return NULL;    return NULL;
354  }  }
355    
# Line 350  gcvote_request_t *gcvote_request(appdata Line 367  gcvote_request_t *gcvote_request(appdata
367      return NULL;      return NULL;
368    }    }
369    
370      request->proxy = appdata->proxy;
371    request->id += strlen(ID_PATTERN);    request->id += strlen(ID_PATTERN);
372    request->refcount = 2;   // master and worker hold a reference    request->refcount = 2;   // master and worker hold a reference
373    request->cb = cb;    request->cb = cb;
# Line 367  gcvote_request_t *gcvote_request(appdata Line 385  gcvote_request_t *gcvote_request(appdata
385    return request;    return request;
386  }  }
387    
388    vote_t *gcvote_restore(appdata_t *appdata, cache_t *cache) {
389      /* load data from disk */
390      char *filename = g_strdup_printf("%s%s/gcvote.xml",
391                                       appdata->image_path,
392                                       cache->id);
393    
394      printf("gcvote: trying to restore from %s\n", filename);
395    
396      /* no such file? */
397      if(!g_file_test(filename, G_FILE_TEST_EXISTS)) {
398        printf("gcvote: no such file\n");
399        free(filename);
400        return NULL;
401      }
402    
403      LIBXML_TEST_VERSION;
404      xmlDoc *doc = xmlReadFile(filename, NULL, 0);
405    
406      if(doc == NULL) {
407        printf("gcvote: error, could not parse file %s\n", filename);
408        free(filename);
409        return NULL;
410      }
411    
412      free(filename);
413    
414      /* in this case this will sure only return one result */
415      gcvote_t *votes = parse_doc(doc);
416    
417      if(!votes) {
418        printf("gcvote: error, no vote found\n");
419        free(filename);
420        return NULL;
421      }
422    
423      vote_t *vote = g_new0(vote_t, 1);
424      vote->quality = votes->quality;
425      vote->votes = votes->votes;
426    
427      printf("gcvote: found vote %d/%d\n", vote->quality, vote->votes);
428    
429      gcvotes_free(votes);
430    
431      return vote;
432    }

Legend:
Removed from v.159  
changed lines
  Added in v.165