]> Pileus Git - grits/blob - src/data.c
compiler fixes
[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         g_debug("data: cache_file_cb");
49         cache_file_end_t *info = _info;
50         char   *url   = g_file_get_path(info->src);
51         char   *local = g_file_get_path(info->dst);
52         GError *error = NULL;
53         g_file_copy_finish(G_FILE(source_object), res, &error);
54         if (error) {
55                 g_warning("data: error copying file ([%s]->[%s]): %s",
56                         url, local, error->message);
57                 g_error_free(error);
58         } else {
59                 info->callback(local, TRUE, info->user_data);
60         }
61         g_object_unref(info->src);
62         g_object_unref(info->dst);
63         g_free(info);
64         g_free(url);
65         g_free(local);
66 }
67
68 static void do_cache(GFile *src, GFile *dst, char *reason,
69                 AWeatherCacheDoneCallback callback, gpointer user_data)
70 {
71         char *name = g_file_get_basename(dst);
72         g_debug("data: do_cache - Caching file %s: %s", name, reason);
73         g_free(name);
74
75         GFile *parent = g_file_get_parent(dst);
76         if (!g_file_query_exists(parent, NULL)) {
77                 char *path = g_file_get_path(parent);
78                 g_mkdir_with_parents(path, 0755);
79                 g_free(path);
80         }
81         g_object_unref(parent);
82
83         cache_file_end_t *info = g_malloc0(sizeof(cache_file_end_t));
84         info->callback  = callback;
85         info->src       = src;
86         info->dst       = dst;
87         info->user_data = user_data;
88         g_file_copy_async(src, dst,
89                 G_FILE_COPY_OVERWRITE,   // GFileCopyFlags flags,
90                 G_PRIORITY_DEFAULT_IDLE, // int io_priority,
91                 NULL,                    // GCancellable *cancellable,
92                 NULL,                    // GFileProgressCallback progress_callback,
93                 NULL,                    // gpointer progress_callback_data,
94                 cache_file_cb,           // GAsyncReadyCallback callback,
95                 info);                   // gpointer user_data
96         return;
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, AWeatherPolicyType update,
105                 AWeatherCacheDoneCallback callback, gpointer user_data)
106 {
107         gchar *url   = g_strconcat(base, path, NULL);
108         gchar *local = g_build_filename(g_get_user_cache_dir(), PACKAGE, path, NULL);
109         GFile *src   = g_file_new_for_uri(url);
110         GFile *dst   = g_file_new_for_path(local);
111
112         if (update == AWEATHER_ALWAYS)
113                 return do_cache(src, dst, "cache forced", callback, user_data);
114
115         if (!g_file_test(local, G_FILE_TEST_EXISTS))
116                 return do_cache(src, dst, "local does not exist", callback, user_data);
117
118         if (update == AWEATHER_AUTOMATIC && g_file_get_size(src) != g_file_get_size(dst))
119                 return do_cache(src, dst, "size mismatch", callback, user_data);
120
121         /* No nead to cache, run the callback now and clean up */
122         callback(local, FALSE, user_data);
123         g_object_unref(src);
124         g_object_unref(dst);
125         g_free(local);
126         g_free(url);
127         return;
128 }