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 void cache_file_cb(GObject *source_object, GAsyncResult *res, gpointer _info)
33 cache_file_end_t *info = _info;
35 g_file_copy_finish(G_FILE(source_object), res, &error);
37 g_message("error copying file ([%s]->[%s]): %s",
38 info->src, info->dst, error->message);
40 info->callback(info->dst, info->user_data);
47 static goffset g_file_get_size(GFile *file)
50 GFileInfo *info = g_file_query_info(file, G_FILE_ATTRIBUTE_STANDARD_SIZE, 0, NULL, &error);
52 g_warning("unable to get file size: %s", error->message);
53 return g_file_info_get_size(info);
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
61 void cache_file(char *base, char *path, AWeatherCacheDoneCallback callback, gpointer user_data)
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);
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));
74 return callback(local, user_data);
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;
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