]> Pileus Git - grits/blob - src/gis-data.c
Reorganize BMNG and SRTM into plugins
[grits] / src / gis-data.c
1 /*
2  * Copyright (C) 2009 Andy Spencer <spenceal@rose-hulman.edu>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include <config.h>
19 #include <stdio.h>
20 #include <glib.h>
21 #include <libsoup/soup.h>
22
23 #include "gis-data.h"
24
25 typedef struct {
26         gchar *uri;
27         gchar *local;
28         FILE  *fp;
29         GisDataCacheDoneCallback  user_done_cb;
30         GisDataCacheChunkCallback user_chunk_cb;
31         gpointer user_data;
32 } cache_file_end_t;
33
34 /*
35  * Open a file, creating parent directories if needed
36  */
37 static FILE *fopen_p(const gchar *path, const gchar *mode)
38 {
39         gchar *parent = g_path_get_dirname(path);
40         if (!g_file_test(parent, G_FILE_TEST_EXISTS))
41                 g_mkdir_with_parents(parent, 0755);
42         g_free(parent);
43         return fopen(path, mode);
44 }
45
46 static void done_cb(SoupSession *session, SoupMessage *message, gpointer _info)
47 {
48         cache_file_end_t *info = _info;
49         g_debug("data: done_cb");
50
51         if (message->status_code == 416)
52                 /* Range unsatisfiable, file already complete */
53                 info->user_done_cb(info->local, FALSE, info->user_data);
54         else if (SOUP_STATUS_IS_SUCCESSFUL(message->status_code))
55                 info->user_done_cb(info->local, TRUE, info->user_data);
56         else
57                 g_warning("data: done_cb - error copying file, status=%d\n"
58                                 "\tsrc=%s\n"
59                                 "\tdst=%s",
60                                 message->status_code, info->uri, info->local);
61         g_free(info->uri);
62         g_free(info->local);
63         fclose(info->fp);
64         g_free(info);
65         //g_object_unref(session); This is probably leaking
66 }
67
68 void chunk_cb(SoupMessage *message, SoupBuffer *chunk, gpointer _info)
69 {
70         cache_file_end_t *info = _info;
71         if (!SOUP_STATUS_IS_SUCCESSFUL(message->status_code))
72                 return;
73
74         if (!fwrite(chunk->data, chunk->length, 1, info->fp))
75                 g_error("data: chunk_cb - Unable to write data");
76         goffset cur   = ftell(info->fp);
77         //goffset total = soup_message_headers_get_range(message->response_headers);
78         goffset start=0, end=0, total=0;
79         soup_message_headers_get_content_range(message->response_headers,
80                         &start, &end, &total);
81
82         if (info->user_chunk_cb)
83                 info->user_chunk_cb(info->local, cur, total, info->user_data);
84 }
85
86 static SoupSession *do_cache(cache_file_end_t *info, gboolean truncate, gchar *reason)
87 {
88         char *name = g_path_get_basename(info->uri);
89         g_debug("data: do_cache - Caching file %s: %s", name, reason);
90         g_free(name);
91
92         /* TODO: move this to callback so we don't end up with 0 byte files
93          * Then change back to check for valid file after download */
94         if (truncate) info->fp = fopen_p(info->local, "w");
95         else          info->fp = fopen_p(info->local, "a");
96         long bytes = ftell(info->fp);
97
98         SoupSession *session = soup_session_async_new();
99         g_object_set(session, "user-agent", PACKAGE_STRING, NULL);
100         SoupMessage *message = soup_message_new("GET", info->uri);
101         if (message == NULL)
102                 g_error("message is null, cannot parse uri");
103         g_signal_connect(message, "got-chunk", G_CALLBACK(chunk_cb), info);
104         soup_message_headers_set_range(message->request_headers, bytes, -1);
105         soup_session_queue_message(session, message, done_cb, info);
106         return session;
107 }
108
109 /*
110  * Cache a image from Ridge to the local disk
111  * \param  path  Path to the Ridge file, starting after /ridge/
112  * \return The local path to the cached image
113  */
114 SoupSession *cache_file(char *base, char *path, GisDataCacheType update,
115                 GisDataCacheChunkCallback user_chunk_cb,
116                 GisDataCacheDoneCallback user_done_cb,
117                 gpointer user_data)
118 {
119         g_debug("GisData: cache_file - base=%s, path=%s", base, path);
120         if (base == NULL) {
121                 g_warning("GisData: cache_file - base is null");
122                 return NULL;
123         }
124         if (path == NULL) {
125                 g_warning("GisData: cache_file - base is null");
126                 return NULL;
127         }
128         cache_file_end_t *info = g_malloc0(sizeof(cache_file_end_t));
129         info->uri           = g_strconcat(base, path, NULL);
130         info->local         = g_build_filename(g_get_user_cache_dir(), PACKAGE, path, NULL);
131         info->fp            = NULL;
132         info->user_chunk_cb = user_chunk_cb;
133         info->user_done_cb  = user_done_cb;
134         info->user_data     = user_data;
135
136         if (update == GIS_REFRESH)
137                 return do_cache(info, TRUE, "cache forced");
138
139         if (update == GIS_UPDATE)
140                 return do_cache(info, FALSE, "attempting updating");
141
142         if (update == GIS_ONCE && !g_file_test(info->local, G_FILE_TEST_EXISTS))
143                 return do_cache(info, TRUE, "local does not exist");
144
145         /* No nead to cache, run the callback now and clean up */
146         user_done_cb(info->local, FALSE, user_data);
147         g_free(info->uri);
148         g_free(info->local);
149         g_free(info);
150         return NULL;
151 }