X-Git-Url: http://pileus.org/git/?a=blobdiff_plain;f=src%2Fdata.c;h=d57a8c713c706b8a4fbadfe95d69413860ad5f34;hb=678a6fa08ee7cdc98a631b9b6a6fef87be63deab;hp=c36556d7644bd453810a732ffa11982a42bea82f;hpb=8efa2df296f672505211b4360c10279d9d57be22;p=aweather diff --git a/src/data.c b/src/data.c index c36556d..d57a8c7 100644 --- a/src/data.c +++ b/src/data.c @@ -1,3 +1,20 @@ +/* + * Copyright (C) 2009 Andy Spencer + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + #include #include #include @@ -6,55 +23,106 @@ typedef struct { AWeatherCacheDoneCallback callback; - gchar *src; - gchar *dst; + GFile *src; + GFile *dst; gchar *user_data; } cache_file_end_t; +static goffset g_file_get_size(GFile *file) +{ + GError *error = NULL; + GFileInfo *info = g_file_query_info(file, + G_FILE_ATTRIBUTE_STANDARD_SIZE, 0, NULL, &error); + if (error){ + g_warning("unable to get file size: %s", error->message); + g_error_free(error); + } + goffset size = g_file_info_get_size(info); + g_file_info_remove_attribute(info, G_FILE_ATTRIBUTE_STANDARD_SIZE); + g_object_unref(info); + return size; +} + static void cache_file_cb(GObject *source_object, GAsyncResult *res, gpointer _info) { + g_debug("data: cache_file_cb"); cache_file_end_t *info = _info; + char *url = g_file_get_path(info->src); + char *local = g_file_get_path(info->dst); GError *error = NULL; g_file_copy_finish(G_FILE(source_object), res, &error); if (error) { - g_message("error copying file ([%s]->[%s]): %s", - info->src, info->dst, error->message); + g_warning("data: error copying file ([%s]->[%s]): %s", + url, local, error->message); + g_error_free(error); } else { - info->callback(info->dst, info->user_data); + info->callback(local, TRUE, info->user_data); } - g_free(info->src); - g_free(info->dst); + g_object_unref(info->src); + g_object_unref(info->dst); g_free(info); + g_free(url); + g_free(local); } -/** +static void do_cache(GFile *src, GFile *dst, char *reason, + AWeatherCacheDoneCallback callback, gpointer user_data) +{ + char *name = g_file_get_basename(dst); + g_debug("data: do_cache - Caching file %s: %s", name, reason); + g_free(name); + + GFile *parent = g_file_get_parent(dst); + if (!g_file_query_exists(parent, NULL)) { + char *path = g_file_get_path(parent); + g_mkdir_with_parents(path, 0755); + g_free(path); + } + g_object_unref(parent); + + cache_file_end_t *info = g_malloc0(sizeof(cache_file_end_t)); + info->callback = callback; + info->src = src; + info->dst = dst; + info->user_data = user_data; + g_file_copy_async(src, dst, + G_FILE_COPY_OVERWRITE, // GFileCopyFlags flags, + G_PRIORITY_DEFAULT_IDLE, // int io_priority, + NULL, // GCancellable *cancellable, + NULL, // GFileProgressCallback progress_callback, + NULL, // gpointer progress_callback_data, + cache_file_cb, // GAsyncReadyCallback callback, + info); // gpointer user_data + return; +} + +/* * Cache a image from Ridge to the local disk * \param path Path to the Ridge file, starting after /ridge/ * \return The local path to the cached image */ -void cache_file(char *base, char *path, AWeatherCacheDoneCallback callback, gpointer user_data) +void cache_file(char *base, char *path, AWeatherPolicyType update, + AWeatherCacheDoneCallback callback, gpointer user_data) { gchar *url = g_strconcat(base, path, NULL); gchar *local = g_build_filename(g_get_user_cache_dir(), PACKAGE, path, NULL); - if (g_file_test(local, G_FILE_TEST_EXISTS)) { - callback(local, user_data); - } else { - if (!g_file_test(g_path_get_dirname(local), G_FILE_TEST_IS_DIR)) - g_mkdir_with_parents(g_path_get_dirname(local), 0755); - cache_file_end_t *info = g_malloc0(sizeof(cache_file_end_t)); - info->callback = callback; - info->src = url; - info->dst = local; - info->user_data = user_data; - GFile *src = g_file_new_for_uri(url); - GFile *dst = g_file_new_for_path(local); - g_file_copy_async(src, dst, - G_FILE_COPY_OVERWRITE, // GFileCopyFlags flags, - 0, // int io_priority, - NULL, // GCancellable *cancellable, - NULL, // GFileProgressCallback progress_callback, - NULL, // gpointer progress_callback_data, - cache_file_cb, // GAsyncReadyCallback callback, - info); // gpointer user_data - } + GFile *src = g_file_new_for_uri(url); + GFile *dst = g_file_new_for_path(local); + + if (update == AWEATHER_ALWAYS) + return do_cache(src, dst, "cache forced", callback, user_data); + + if (!g_file_test(local, G_FILE_TEST_EXISTS)) + return do_cache(src, dst, "local does not exist", callback, user_data); + + if (update == AWEATHER_AUTOMATIC && g_file_get_size(src) != g_file_get_size(dst)) + return do_cache(src, dst, "size mismatch", callback, user_data); + + /* No nead to cache, run the callback now and clean up */ + callback(local, FALSE, user_data); + g_object_unref(src); + g_object_unref(dst); + g_free(local); + g_free(url); + return; }