]> Pileus Git - grits/blob - src/data.c
* Removing glade (pure gtk-builder)
[grits] / src / 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 "data.h"
24
25 typedef struct {
26         AWeatherCacheDoneCallback callback;
27         gpointer user_data;
28         gchar *local;
29         FILE  *fp;
30 } cache_file_end_t;
31
32 /*
33  * Open a file, creating parent directories if needed
34  */
35 static FILE *fopen_p(const gchar *path, const gchar *mode)
36 {
37         gchar *parent = g_path_get_dirname(path);
38         if (!g_file_test(parent, G_FILE_TEST_EXISTS))
39                 g_mkdir_with_parents(parent, 0755);
40         g_free(parent);
41         return fopen(path, mode);
42 }
43
44 static void cache_file_cb(SoupSession *session, SoupMessage *message, gpointer _info)
45 {
46         cache_file_end_t *info = _info;
47         gchar *uri = soup_uri_to_string(soup_message_get_uri(message), FALSE);
48         g_debug("data: cache_file_cb");
49
50         if (message->status_code == 416) {
51                 /* Range unsatisfiable, file already complete */
52                 info->callback(info->local, FALSE, info->user_data);
53         } else if (SOUP_STATUS_IS_SUCCESSFUL(message->status_code)) {
54                 gint wrote = fwrite(message->response_body->data,  1,
55                                 message->response_body->length, info->fp);
56                 g_debug("data: status=%u wrote=%d/%lld",
57                                 message->status_code,
58                                 wrote, message->response_body->length);
59                 fclose(info->fp);
60                 info->callback(info->local, TRUE, info->user_data);
61         } else {
62                 g_warning("data: cache_file_cb - error copying file, status=%d\n"
63                                 "\tsrc=%s\n"
64                                 "\tdst=%s",
65                                 message->status_code, uri, info->local);
66         }
67         g_free(uri);
68         g_free(info->local);
69         g_object_unref(session);
70 }
71
72 static void do_cache(gchar *uri, gchar *local, gboolean truncate, gchar *reason,
73                 AWeatherCacheDoneCallback callback, gpointer user_data)
74 {
75         char *name = g_path_get_basename(uri);
76         g_debug("data: do_cache - Caching file %s: %s", name, reason);
77         g_free(name);
78
79         cache_file_end_t *info = g_malloc0(sizeof(cache_file_end_t));
80         info->callback  = callback;
81         info->user_data = user_data;
82         info->local     = local;
83
84         /* TODO: move this to callback so we don't end up with 0 byte files
85          * Then change back to check for valid file after download */
86         if (truncate) info->fp = fopen_p(local, "w");
87         else          info->fp = fopen_p(local, "a");
88         long bytes = ftell(info->fp);
89
90         SoupSession *session = soup_session_async_new();
91         SoupMessage *message = soup_message_new("GET", uri);
92         if (message == NULL)
93                 g_error("message is null, cannot parse uri");
94         if (bytes != 0)
95                 soup_message_headers_set_range(message->request_headers, bytes, -1);
96         soup_session_queue_message(session, message, cache_file_cb, info);
97 }
98
99 /*
100  * Cache a image from Ridge to the local disk
101  * \param  path  Path to the Ridge file, starting after /ridge/
102  * \return The local path to the cached image
103  */
104 void cache_file(char *base, char *path, AWeatherCacheType update,
105                 AWeatherCacheDoneCallback callback, gpointer user_data)
106 {
107         gchar *uri   = g_strconcat(base, path, NULL);
108         gchar *local = g_build_filename(g_get_user_cache_dir(), PACKAGE, path, NULL);
109
110         if (update == AWEATHER_REFRESH)
111                 return do_cache(uri, local, TRUE, "cache forced",
112                                 callback, user_data);
113
114         if (update == AWEATHER_UPDATE)
115                 return do_cache(uri, local, FALSE, "attempting updating",
116                                 callback, user_data);
117
118         if (update == AWEATHER_ONCE && !g_file_test(local, G_FILE_TEST_EXISTS))
119                 return do_cache(uri, local, TRUE, "local does not exist",
120                                 callback, user_data);
121
122         /* No nead to cache, run the callback now and clean up */
123         callback(local, FALSE, user_data);
124         g_free(local);
125         g_free(uri);
126         return;
127 }