* Fixing some memory leaks. A few minor ones in AWeather and a major one in RSL
[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 *src;
27         gchar *dst;
28         gchar *user_data;
29 } cache_file_end_t;
30
31 static void cache_file_cb(GObject *source_object, GAsyncResult *res, gpointer _info)
32 {
33         cache_file_end_t *info = _info;
34         GError *error = NULL;
35         g_file_copy_finish(G_FILE(source_object), res, &error);
36         if (error) {
37                 g_message("error copying file ([%s]->[%s]): %s",
38                         info->src, info->dst, error->message);
39         } else {
40                 info->callback(info->dst, info->user_data);
41         }
42         g_free(info->src);
43         g_free(info->dst);
44         g_free(info);
45 }
46
47 static goffset g_file_get_size(GFile *file)
48 {
49         GError *error = NULL;
50         GFileInfo *info = g_file_query_info(file, G_FILE_ATTRIBUTE_STANDARD_SIZE, 0, NULL, &error);
51         if (error)
52                 g_warning("unable to get file size: %s", error->message);
53         return g_file_info_get_size(info);
54 }
55
56 /**
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
60  */
61 void cache_file(char *base, char *path, AWeatherCacheDoneCallback callback, gpointer user_data)
62 {
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);
67
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));
73         else {
74                 callback(local, user_data);
75                 g_free(local);
76                 g_free(url);
77                 return;
78         }
79
80         char *dir = g_path_get_dirname(local);
81         if (!g_file_test(dir, G_FILE_TEST_IS_DIR))
82                 g_mkdir_with_parents(dir, 0755);
83         g_free(dir);
84         cache_file_end_t *info = g_malloc0(sizeof(cache_file_end_t));
85         info->callback  = callback;
86         info->src       = url;
87         info->dst       = local;
88         info->user_data = user_data;
89         g_file_copy_async(src, dst,
90                 G_FILE_COPY_OVERWRITE, // GFileCopyFlags flags,
91                 0,                     // int io_priority,
92                 NULL,                  // GCancellable *cancellable,
93                 NULL,                  // GFileProgressCallback progress_callback,
94                 NULL,                  // gpointer progress_callback_data,
95                 cache_file_cb,         // GAsyncReadyCallback callback,
96                 info);                 // gpointer user_data
97 }