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 cache_file_end_t *info = _info;
49 char *url = g_file_get_path(info->src);
50 char *local = g_file_get_path(info->dst);
52 g_file_copy_finish(G_FILE(source_object), res, &error);
54 g_message("error copying file ([%s]->[%s]): %s",
55 url, local, error->message);
58 info->callback(local, TRUE, info->user_data);
60 g_object_unref(info->src);
61 g_object_unref(info->dst);
67 static void do_cache(GFile *src, GFile *dst, char *reason,
68 AWeatherCacheDoneCallback callback, gpointer user_data)
70 char *name = g_file_get_basename(dst);
71 g_message("Caching file %s: %s", name, reason);
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);
79 cache_file_end_t *info = g_malloc0(sizeof(cache_file_end_t));
80 info->callback = callback;
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
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
100 void cache_file(char *base, char *path, AWeatherPolicyType update,
101 AWeatherCacheDoneCallback callback, gpointer user_data)
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);
108 if (update == AWEATHER_ALWAYS)
109 return do_cache(src, dst, "cache forced", callback, user_data);
111 if (!g_file_test(local, G_FILE_TEST_EXISTS))
112 return do_cache(src, dst, "local does not exist", callback, user_data);
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);
117 /* No nead to cache, run the callback now and clean up */
118 callback(local, FALSE, user_data);