]> git.street.me.uk Git - andy/viking.git/blame - src/curl_download.c
Use less precise images when wanted image is not present
[andy/viking.git] / src / curl_download.c
CommitLineData
3292ba8b
GB
1/*
2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3 *
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#ifdef HAVE_CONFIG_H
23#include "config.h"
24#endif
25
8c060406
MA
26#include <stdio.h>
27
45acf79e
MA
28#include <glib.h>
29#include <glib/gstdio.h>
4c77d5e0 30#include <glib/gi18n.h>
6a2692be 31#include <glib/gprintf.h>
3292ba8b
GB
32#include <gtk/gtk.h>
33
3292ba8b 34#include <curl/curl.h>
3292ba8b 35
7711383f 36#include "background.h"
80d0ff2b 37#include "file.h"
2936913d 38#include "globals.h"
3292ba8b
GB
39#include "curl_download.h"
40
6a2692be
JJ
41gchar *curl_download_user_agent;
42
3a0c074a
MA
43/*
44 * Even if writing to FILE* is supported by libcurl by default,
45 * it seems that it is non-portable (win32 DLL specific).
46 *
47 * So, we provide our own trivial CURLOPT_WRITEFUNCTION.
48 */
49static size_t curl_write_func(void *ptr, size_t size, size_t nmemb, FILE *stream)
50{
51 return fwrite(ptr, size, nmemb, stream);
52}
53
7711383f
JJ
54static int curl_progress_func(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow)
55{
56 return a_background_testcancel(NULL);
57}
58
80d0ff2b
QT
59static gchar *get_cookie_file(gboolean init)
60{
61 static gchar *cookie_file = NULL;
62 static GMutex *mutex = NULL;
63
64 if (init) { /* to make sure it's thread safe */
65 mutex = g_mutex_new();
66 static gchar *cookie_fn = "cookies.txt";
67 const gchar *viking_dir = a_get_viking_dir();
6f2b61bb 68 cookie_file = g_build_filename(viking_dir, cookie_fn, NULL);
45acf79e 69 g_unlink(cookie_file);
80d0ff2b
QT
70 return NULL;
71 }
72
73 g_assert(cookie_file != NULL);
74
75 g_mutex_lock(mutex);
45acf79e 76 if (g_file_test(cookie_file, G_FILE_TEST_EXISTS) == FALSE) { /* file not there */
109c6557 77 gchar * name_tmp = NULL;
c7f6cb25 78 FILE * out_file = tmpfile();
109c6557
MA
79 if (out_file == NULL) {
80 // Something wrong with previous call (unsuported?)
81 name_tmp = g_strdup_printf("%s.tmp", cookie_file);
82 out_file = g_fopen(name_tmp, "w+b");
83 }
80d0ff2b
QT
84 CURLcode res;
85 CURL *curl = curl_easy_init();
2936913d
GB
86 if (vik_verbose)
87 curl_easy_setopt ( curl, CURLOPT_VERBOSE, 1 );
80d0ff2b 88 curl_easy_setopt(curl, CURLOPT_URL, "http://maps.google.com/"); /* google.com sets "PREF" cookie */
da309dc2 89 curl_easy_setopt ( curl, CURLOPT_WRITEDATA, out_file );
3a0c074a 90 curl_easy_setopt ( curl, CURLOPT_WRITEFUNCTION, curl_write_func);
80d0ff2b
QT
91 curl_easy_setopt(curl, CURLOPT_COOKIEJAR, cookie_file);
92 res = curl_easy_perform(curl);
93 if (res != CURLE_OK) {
4c77d5e0 94 g_warning(_("%s() Curl perform failed: %s"), __PRETTY_FUNCTION__,
80d0ff2b 95 curl_easy_strerror(res));
45acf79e 96 g_unlink(cookie_file);
80d0ff2b
QT
97 }
98 curl_easy_cleanup(curl);
8c060406
MA
99 fclose(out_file);
100 out_file = NULL;
109c6557
MA
101 if (name_tmp != NULL) {
102 g_remove(name_tmp);
103 g_free(name_tmp);
104 name_tmp = NULL;
105 }
80d0ff2b
QT
106 }
107 g_mutex_unlock(mutex);
108
109 return(cookie_file);
110}
111
112/* This should to be called from main() to make sure thread safe */
113void curl_download_init()
114{
115 curl_global_init(CURL_GLOBAL_ALL);
116 get_cookie_file(TRUE);
6a2692be 117 curl_download_user_agent = g_strdup_printf ("%s/%s %s", PACKAGE, VERSION, curl_version());
80d0ff2b
QT
118}
119
a3188993 120int curl_download_uri ( const char *uri, FILE *f, DownloadOptions *options )
3292ba8b 121{
3292ba8b 122 CURL *curl;
11f88b69 123 CURLcode res = CURLE_FAILED_INIT;
80d0ff2b 124 const gchar *cookie_file;
3292ba8b 125
2936913d
GB
126 g_debug("%s: uri=%s", __PRETTY_FUNCTION__, uri);
127
3292ba8b
GB
128 curl = curl_easy_init ();
129 if ( curl )
130 {
2936913d
GB
131 if (vik_verbose)
132 curl_easy_setopt ( curl, CURLOPT_VERBOSE, 1 );
3292ba8b 133 curl_easy_setopt ( curl, CURLOPT_URL, uri );
da309dc2 134 curl_easy_setopt ( curl, CURLOPT_WRITEDATA, f );
3a0c074a 135 curl_easy_setopt ( curl, CURLOPT_WRITEFUNCTION, curl_write_func);
7711383f
JJ
136 curl_easy_setopt ( curl, CURLOPT_NOPROGRESS, 0 );
137 curl_easy_setopt ( curl, CURLOPT_PROGRESSDATA, NULL );
138 curl_easy_setopt ( curl, CURLOPT_PROGRESSFUNCTION, curl_progress_func);
1a8143e6
BZ
139 if (options != NULL) {
140 if(options->referer != NULL)
141 curl_easy_setopt ( curl, CURLOPT_REFERER, options->referer);
142 if(options->follow_location != 0) {
143 curl_easy_setopt ( curl, CURLOPT_FOLLOWLOCATION, 1);
144 curl_easy_setopt ( curl, CURLOPT_MAXREDIRS, options->follow_location);
145 }
146 }
6a2692be 147 curl_easy_setopt ( curl, CURLOPT_USERAGENT, curl_download_user_agent );
494eb388 148 if ((cookie_file = get_cookie_file(FALSE)) != NULL)
80d0ff2b 149 curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookie_file);
3292ba8b
GB
150 res = curl_easy_perform ( curl );
151 curl_easy_cleanup ( curl );
152 }
11f88b69 153 return(res);
3292ba8b
GB
154}
155
0c1044e9 156int curl_download_get_url ( const char *hostname, const char *uri, FILE *f, DownloadOptions *options, gboolean ftp )
3292ba8b
GB
157{
158 int ret;
159 gchar *full = NULL;
160
161 /* Compose the full url */
0c1044e9 162 full = g_strdup_printf ( "%s://%s%s", (ftp?"ftp":"http"), hostname, uri );
a3188993 163 ret = curl_download_uri ( full, f, options );
3292ba8b
GB
164 g_free ( full );
165 full = NULL;
166
11f88b69 167 return (ret ? -2 : 0); /* -2 HTTP error */
3292ba8b 168}