]> Pileus Git - grits/blob - src/data.c
fixing more memory leaks
[grits] / src / data.c
1 /*
2  * Copyright (C) 2009 Andy Spencer <spenceal@rose-hulman.edu>
3  * 
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.
8  * 
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.
13  * 
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/>.
16  */
17
18 #include <config.h>
19 #include <glib.h>
20 #include <gio/gio.h>
21
22 #include "data.h"
23
24 typedef struct {
25         AWeatherCacheDoneCallback callback;
26         gchar *url;
27         gchar *local;
28         GFile *src;
29         GFile *dst;
30         gchar *user_data;
31 } cache_file_end_t;
32
33 static void cache_file_cb(GObject *source_object, GAsyncResult *res, gpointer _info)
34 {
35         cache_file_end_t *info = _info;
36         GError *error = NULL;
37         g_file_copy_finish(G_FILE(source_object), res, &error);
38         if (error) {
39                 g_message("error copying file ([%s]->[%s]): %s",
40                         info->url, info->local, error->message);
41                 g_error_free(error);
42         } else {
43                 info->callback(info->local, info->user_data);
44         }
45         g_object_unref(info->src);
46         g_object_unref(info->dst);
47         g_free(info->url);
48         g_free(info->local);
49         g_free(info);
50 }
51
52 static goffset g_file_get_size(GFile *file)
53 {
54         GError *error = NULL;
55         GFileInfo *info = g_file_query_info(file,
56                         G_FILE_ATTRIBUTE_STANDARD_SIZE, 0, NULL, &error);
57         if (error){
58                 g_warning("unable to get file size: %s", error->message);
59                 g_error_free(error);
60         }
61         goffset size = g_file_info_get_size(info);
62         g_file_info_remove_attribute(info, G_FILE_ATTRIBUTE_STANDARD_SIZE);
63         g_object_unref(info);
64         return size;
65 }
66
67 /**
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
71  */
72 void cache_file(char *base, char *path, AWeatherCacheDoneCallback callback, gpointer user_data)
73 {
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);
78
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));
84         else {
85                 callback(local, user_data);
86                 g_object_unref(src);
87                 g_object_unref(dst);
88                 g_free(local);
89                 g_free(url);
90                 return;
91         }
92
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);
96         g_free(dir);
97         cache_file_end_t *info = g_malloc0(sizeof(cache_file_end_t));
98         info->callback  = callback;
99         info->url       = url;
100         info->local     = local;
101         info->src       = src;
102         info->dst       = dst;
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
112 }