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;
33 static void cache_file_cb(GObject *source_object, GAsyncResult *res, gpointer _info)
35 cache_file_end_t *info = _info;
37 g_file_copy_finish(G_FILE(source_object), res, &error);
39 g_message("error copying file ([%s]->[%s]): %s",
40 info->url, info->local, error->message);
43 info->callback(info->local, info->user_data);
45 g_object_unref(info->src);
46 g_object_unref(info->dst);
52 static goffset g_file_get_size(GFile *file)
55 GFileInfo *info = g_file_query_info(file,
56 G_FILE_ATTRIBUTE_STANDARD_SIZE, 0, NULL, &error);
58 g_warning("unable to get file size: %s", error->message);
61 goffset size = g_file_info_get_size(info);
62 g_file_info_remove_attribute(info, G_FILE_ATTRIBUTE_STANDARD_SIZE);
68 * Cache a image from Ridge to the local disk
69 * \param path Path to the Ridge file, starting after /ridge/
70 * \return The local path to the cached image
72 void cache_file(char *base, char *path, AWeatherCacheDoneCallback callback, gpointer user_data)
74 gchar *url = g_strconcat(base, path, NULL);
75 gchar *local = g_build_filename(g_get_user_cache_dir(), PACKAGE, path, NULL);
76 GFile *src = g_file_new_for_uri(url);
77 GFile *dst = g_file_new_for_path(local);
79 if (!g_file_test(local, G_FILE_TEST_EXISTS))
80 g_message("Caching file: local does not exist - %s", local);
81 else if (g_file_get_size(src) != g_file_get_size(dst))
82 g_message("Caching file: sizes mismatch - %lld != %lld",
83 g_file_get_size(src), g_file_get_size(dst));
85 callback(local, user_data);
93 char *dir = g_path_get_dirname(local);
94 if (!g_file_test(dir, G_FILE_TEST_IS_DIR))
95 g_mkdir_with_parents(dir, 0755);
97 cache_file_end_t *info = g_malloc0(sizeof(cache_file_end_t));
98 info->callback = callback;
103 info->user_data = user_data;
104 g_file_copy_async(src, dst,
105 G_FILE_COPY_OVERWRITE, // GFileCopyFlags flags,
106 0, // int io_priority,
107 NULL, // GCancellable *cancellable,
108 NULL, // GFileProgressCallback progress_callback,
109 NULL, // gpointer progress_callback_data,
110 cache_file_cb, // GAsyncReadyCallback callback,
111 info); // gpointer user_data