]> Pileus Git - grits/blob - src/data.c
Adding copyright statements and a few bug fixes (hacks) for shading radar data
[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 <glib.h>
20 #include <gio/gio.h>
21
22 #include "data.h"
23
24 typedef struct {
25         AWeatherCacheDoneCallback callback;
26         gchar *src;
27         gchar *dst;
28         gchar *user_data;
29 } cache_file_end_t;
30
31 static void cache_file_cb(GObject *source_object, GAsyncResult *res, gpointer _info)
32 {
33         cache_file_end_t *info = _info;
34         GError *error = NULL;
35         g_file_copy_finish(G_FILE(source_object), res, &error);
36         if (error) {
37                 g_message("error copying file ([%s]->[%s]): %s",
38                         info->src, info->dst, error->message);
39         } else {
40                 info->callback(info->dst, info->user_data);
41         }
42         g_free(info->src);
43         g_free(info->dst);
44         g_free(info);
45 }
46
47 static goffset g_file_get_size(GFile *file)
48 {
49         GError *error = NULL;
50         GFileInfo *info = g_file_query_info(file, G_FILE_ATTRIBUTE_STANDARD_SIZE, 0, NULL, &error);
51         if (error)
52                 g_warning("unable to get file size: %s", error->message);
53         return g_file_info_get_size(info);
54 }
55
56 /**
57  * Cache a image from Ridge to the local disk
58  * \param  path  Path to the Ridge file, starting after /ridge/
59  * \return The local path to the cached image
60  */
61 void cache_file(char *base, char *path, AWeatherCacheDoneCallback callback, gpointer user_data)
62 {
63         gchar *url   = g_strconcat(base, path, NULL);
64         gchar *local = g_build_filename(g_get_user_cache_dir(), PACKAGE, path, NULL);
65         GFile *src = g_file_new_for_uri(url);
66         GFile *dst = g_file_new_for_path(local);
67
68         if (!g_file_test(local, G_FILE_TEST_EXISTS))
69                 g_message("Caching file: local does not exist - %s", local);
70         else if (g_file_get_size(src) != g_file_get_size(dst))
71                 g_message("Caching file: sizes mismatch - %lld != %lld",
72                                 g_file_get_size(src), g_file_get_size(dst));
73         else
74                 return callback(local, user_data);
75
76         if (!g_file_test(g_path_get_dirname(local), G_FILE_TEST_IS_DIR))
77                 g_mkdir_with_parents(g_path_get_dirname(local), 0755);
78         cache_file_end_t *info = g_malloc0(sizeof(cache_file_end_t));
79         info->callback  = callback;
80         info->src       = url;
81         info->dst       = local;
82         info->user_data = user_data;
83         g_file_copy_async(src, dst,
84                 G_FILE_COPY_OVERWRITE, // GFileCopyFlags flags,
85                 0,                     // int io_priority,
86                 NULL,                  // GCancellable *cancellable,
87                 NULL,                  // GFileProgressCallback progress_callback,
88                 NULL,                  // gpointer progress_callback_data,
89                 cache_file_cb,         // GAsyncReadyCallback callback,
90                 info);                 // gpointer user_data
91 }