2 * Copyright (C) 2009 Andy Spencer <spenceal@rose-hulman.edu>
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.
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.
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/>.
25 AWeatherCacheDoneCallback callback;
31 static goffset g_file_get_size(GFile *file)
34 GFileInfo *info = g_file_query_info(file,
35 G_FILE_ATTRIBUTE_STANDARD_SIZE, 0, NULL, &error);
37 g_warning("unable to get file size: %s", error->message);
40 goffset size = g_file_info_get_size(info);
41 g_file_info_remove_attribute(info, G_FILE_ATTRIBUTE_STANDARD_SIZE);
46 static void cache_file_cb(GObject *source_object, GAsyncResult *res, gpointer _info)
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);
53 g_file_copy_finish(G_FILE(source_object), res, &error);
55 g_warning("data: error copying file ([%s]->[%s]): %s",
56 url, local, error->message);
59 info->callback(local, TRUE, info->user_data);
61 g_object_unref(info->src);
62 g_object_unref(info->dst);
68 static void do_cache(GFile *src, GFile *dst, char *reason,
69 AWeatherCacheDoneCallback callback, gpointer user_data)
71 char *name = g_file_get_basename(dst);
72 g_debug("data: do_cache - Caching file %s: %s", name, reason);
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);
81 g_object_unref(parent);
83 cache_file_end_t *info = g_malloc0(sizeof(cache_file_end_t));
84 info->callback = callback;
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
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
104 void cache_file(char *base, char *path, AWeatherPolicyType update,
105 AWeatherCacheDoneCallback callback, gpointer user_data)
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);
112 if (update == AWEATHER_ALWAYS)
113 return do_cache(src, dst, "cache forced", callback, user_data);
115 if (!g_file_test(local, G_FILE_TEST_EXISTS))
116 return do_cache(src, dst, "local does not exist", callback, user_data);
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);
121 /* No nead to cache, run the callback now and clean up */
122 callback(local, FALSE, user_data);