Code

screen_browser: added browser_cmd()
[ncmpc.git] / src / easy_download.c
1 /*      by Qball
2  *  
3  *  This program is free software; you can redistribute it and/or modify
4  *  it under the terms of the GNU General Public License as published by
5  *  the Free Software Foundation; either version 2 of the License, or
6  *  (at your option) any later version.
7  *
8  *  This program is distributed in the hope that it will be useful,
9  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  *  GNU General Public License for more details.
12  *
13  *  You should have received a copy of the GNU General Public License
14  *  along with this program; if not, write to the Free Software
15  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16  */
18 #include "ncmpc.h"
20 #ifdef ENABLE_LYRICS_SCREEN
21 #include "easy_download.h"
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <glib.h>
28 #include <curl/curl.h>
30 static size_t write_data(void *buffer, size_t size,
31                          size_t nmemb, void *data)
32 {
33         easy_download_struct *dld = data;
35         if(dld->data == NULL)
36         {
37                 dld->size = 0;
38         }
39         dld->data = g_realloc(dld->data,(gulong)(size*nmemb+dld->size));
41         memset(&(dld->data)[dld->size], '\0', (size*nmemb));
42         memcpy(&(dld->data)[dld->size], buffer, size*nmemb);
44         dld->size += size*nmemb;
45         if(dld->size >= dld->max_size && dld->max_size > 0)
46         {
47                 return 0;
48         }
49         return size*nmemb;
50 }
52 int easy_download(char *url,easy_download_struct *dld,
53                 curl_progress_callback cb)
54 {
55         CURL *curl = NULL;
56         int res;
57         if(!dld) return 0;
58         easy_download_clean(dld);
59         /* initialize curl */
60         curl = curl_easy_init();
61         if(!curl) return 0;
62         /* set uri */
63         curl_easy_setopt(curl, CURLOPT_URL, url);
64         /* set callback data */
65         curl_easy_setopt(curl, CURLOPT_WRITEDATA, dld);
66         /* set callback function */
67         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
68         curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
69         curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, cb);
70         /* run */
71         res = curl_easy_perform(curl);
72         /* cleanup */
73         curl_easy_cleanup(curl);
74         if(!res)
75         {
76                 return 1;
77         }
78         if(dld->data) g_free(dld->data);
79         return 0;
80 }
82 void easy_download_clean(easy_download_struct *dld)
83 {
84         if(dld->data)g_free(dld->data);
85         dld->data = NULL;
86         dld->size = 0;
87         dld->max_size = -1;
88 }
90 #endif