]> Pileus Git - grits/blob - src/data.c
(no commit message)
[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         GFile *src;
27         GFile *dst;
28         gchar *user_data;
29 } cache_file_end_t;
30
31 static goffset g_file_get_size(GFile *file)
32 {
33         GError *error = NULL;
34         GFileInfo *info = g_file_query_info(file,
35                         G_FILE_ATTRIBUTE_STANDARD_SIZE, 0, NULL, &error);
36         if (error){
37                 g_warning("unable to get file size: %s", error->message);
38                 g_error_free(error);
39         }
40         goffset size = g_file_info_get_size(info);
41         g_file_info_remove_attribute(info, G_FILE_ATTRIBUTE_STANDARD_SIZE);
42         g_object_unref(info);
43         return size;
44 }
45
46 static void cache_file_cb(GObject *source_object, GAsyncResult *res, gpointer _info)
47 {
48         cache_file_end_t *info = _info;
49         char   *url   = g_file_get_path(info->src);
50         char   *local = g_file_get_path(info->dst);
51         GError *error = NULL;
52         g_file_copy_finish(G_FILE(source_object), res, &error);
53         if (error) {
54                 g_message("error copying file ([%s]->[%s]): %s",
55                         url, local, error->message);
56                 g_error_free(error);
57         } else {
58                 info->callback(local, TRUE, info->user_data);
59         }
60         g_object_unref(info->src);
61         g_object_unref(info->dst);
62         g_free(info);
63         g_free(url);
64         g_free(local);
65 }
66
67 static void do_cache(GFile *src, GFile *dst, char *reason,
68                 AWeatherCacheDoneCallback callback, gpointer user_data)
69 {
70         char *name = g_file_get_basename(dst);
71         g_message("Caching file %s: %s", name, reason);
72         g_free(name);
73
74         GFile *parent = g_file_get_parent(dst);
75         if (!g_file_query_exists(parent, NULL))
76                 g_file_make_directory_with_parents(parent, NULL, NULL);
77         g_object_unref(parent);
78
79         cache_file_end_t *info = g_malloc0(sizeof(cache_file_end_t));
80         info->callback  = callback;
81         info->src       = src;
82         info->dst       = dst;
83         info->user_data = user_data;
84         g_file_copy_async(src, dst,
85                 G_FILE_COPY_OVERWRITE, // GFileCopyFlags flags,
86                 0,                     // int io_priority,
87                 NULL,                  // GCancellable *cancellable,
88                 NULL,                  // GFileProgressCallback progress_callback,
89                 NULL,                  // gpointer progress_callback_data,
90                 cache_file_cb,         // GAsyncReadyCallback callback,
91                 info);                 // gpointer user_data
92         return;
93 }
94
95 /*
96  * Cache a image from Ridge to the local disk
97  * \param  path  Path to the Ridge file, starting after /ridge/
98  * \return The local path to the cached image
99  */
100 void cache_file(char *base, char *path, AWeatherPolicyType update,
101                 AWeatherCacheDoneCallback callback, gpointer user_data)
102 {
103         gchar *url   = g_strconcat(base, path, NULL);
104         gchar *local = g_build_filename(g_get_user_cache_dir(), PACKAGE, path, NULL);
105         GFile *src   = g_file_new_for_uri(url);
106         GFile *dst   = g_file_new_for_path(local);
107
108         if (update == AWEATHER_ALWAYS)
109                 return do_cache(src, dst, "cache forced", callback, user_data);
110
111         if (!g_file_test(local, G_FILE_TEST_EXISTS))
112                 return do_cache(src, dst, "local does not exist", callback, user_data);
113
114         if (update == AWEATHER_AUTOMATIC && g_file_get_size(src) != g_file_get_size(dst))
115                 return do_cache(src, dst, "size mismatch", callback, user_data);
116
117         /* No nead to cache, run the callback now and clean up */
118         callback(local, FALSE, user_data);
119         g_object_unref(src);
120         g_object_unref(dst);
121         g_free(local);
122         g_free(url);
123         return;
124 }