File: http_download.c

package info (click to toggle)
cardpeek 0.8.3-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,184 kB
  • ctags: 1,275
  • sloc: ansic: 10,467; sh: 4,241; makefile: 32; xml: 19
file content (72 lines) | stat: -rw-r--r-- 1,882 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "config.h"
#include "misc.h"
#include "a_string.h"
#include "http_download.h"
#include "gui_inprogress.h"
#include <curl/curl.h>
#include <string.h>
#include <errno.h>

static int progress_download(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow)
{
    GtkWidget *progress = (GtkWidget *)clientp;
    UNUSED(ultotal);
    UNUSED(ulnow);

    if (dltotal==0)
        return !gui_inprogress_pulse(progress);
    return !gui_inprogress_set_fraction(progress,dlnow/dltotal);
}


int http_download(const char *src_url, const char *dst_filename)
{
    CURL *curl;
    CURLcode res;
    GtkWidget *progress;  
    a_string_t *user_agent;
    a_string_t *progress_title;
    FILE *temp_fd;
    
    curl = curl_easy_init();
    
    if (!curl)
        return 0;

    progress_title    = a_strnew(NULL); 
    a_sprintf(progress_title,"Updating %s",dst_filename);
    
    user_agent = a_strnew(NULL);     
    a_sprintf(user_agent,"cardpeek/%s",VERSION);

    progress = gui_inprogress_new(a_strval(progress_title),"Please wait...");
    
    temp_fd = fopen(dst_filename,"wb");

    curl_easy_setopt(curl,CURLOPT_URL,src_url);
    curl_easy_setopt(curl,CURLOPT_WRITEDATA, temp_fd);
    curl_easy_setopt(curl,CURLOPT_USERAGENT, a_strval(user_agent));
    curl_easy_setopt(curl,CURLOPT_FAILONERROR, 1L);
    curl_easy_setopt(curl,CURLOPT_NOPROGRESS, 0L);
    curl_easy_setopt(curl,CURLOPT_PROGRESSFUNCTION, progress_download);
    curl_easy_setopt(curl,CURLOPT_PROGRESSDATA, progress);

    res = curl_easy_perform(curl);

    fclose(temp_fd);

    if (res!=CURLE_OK)
    {
        log_printf(LOG_ERROR,"Failed to fetch %s: %s", src_url, curl_easy_strerror(res));
        unlink(dst_filename);
    }

    curl_easy_cleanup(curl);

    gui_inprogress_free(progress);

    a_strfree(user_agent);
    a_strfree(progress_title);

    return (res==CURLE_OK);
}