]> Pileus Git - aweather/blobdiff - src/data.c
cleanup
[aweather] / src / data.c
index e4f2a8ce265bbfa0ea2f7dad2b1507d0770c047e..9def903b86004d867f515eeee0e620f13d22b6b8 100644 (file)
  */
 
 #include <config.h>
+#include <stdio.h>
 #include <glib.h>
-#include <gio/gio.h>
+#include <libsoup/soup.h>
 
 #include "data.h"
 
 typedef struct {
-       AWeatherCacheDoneCallback callback;
-       gchar *url;
+       gchar *uri;
        gchar *local;
-       GFile *src;
-       GFile *dst;
-       gchar *user_data;
+       FILE  *fp;
+       AWeatherCacheDoneCallback  user_done_cb;
+       AWeatherCacheChunkCallback user_chunk_cb;
+       gpointer user_data;
 } cache_file_end_t;
 
-static void cache_file_cb(GObject *source_object, GAsyncResult *res, gpointer _info)
+/*
+ * Open a file, creating parent directories if needed
+ */
+static FILE *fopen_p(const gchar *path, const gchar *mode)
+{
+       gchar *parent = g_path_get_dirname(path);
+       if (!g_file_test(parent, G_FILE_TEST_EXISTS))
+               g_mkdir_with_parents(parent, 0755);
+       g_free(parent);
+       return fopen(path, mode);
+}
+
+static void done_cb(SoupSession *session, SoupMessage *message, gpointer _info)
 {
        cache_file_end_t *info = _info;
-       GError *error = NULL;
-       g_file_copy_finish(G_FILE(source_object), res, &error);
-       if (error) {
-               g_message("error copying file ([%s]->[%s]): %s",
-                       info->url, info->local, error->message);
-               g_error_free(error);
-       } else {
-               info->callback(info->local, info->user_data);
-       }
-       g_object_unref(info->src);
-       g_object_unref(info->dst);
-       g_free(info->url);
+       g_debug("data: done_cb");
+
+       if (message->status_code == 416)
+               /* Range unsatisfiable, file already complete */
+               info->user_done_cb(info->local, FALSE, info->user_data);
+       else if (SOUP_STATUS_IS_SUCCESSFUL(message->status_code))
+               info->user_done_cb(info->local, TRUE, info->user_data);
+       else
+               g_warning("data: done_cb - error copying file, status=%d\n"
+                               "\tsrc=%s\n"
+                               "\tdst=%s",
+                               message->status_code, info->uri, info->local);
+       g_free(info->uri);
        g_free(info->local);
+       fclose(info->fp);
        g_free(info);
+       //g_object_unref(session); This is probably leaking
 }
 
-static goffset g_file_get_size(GFile *file)
+void chunk_cb(SoupMessage *message, SoupBuffer *chunk, gpointer _info)
 {
-       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;
+       cache_file_end_t *info = _info;
+       if (!SOUP_STATUS_IS_SUCCESSFUL(message->status_code))
+               return;
+
+       if (!fwrite(chunk->data, chunk->length, 1, info->fp))
+               g_error("data: chunk_cb - Unable to write data");
+       goffset cur   = ftell(info->fp);
+       //goffset total = soup_message_headers_get_range(message->response_headers);
+       goffset start=0, end=0, total=0;
+       soup_message_headers_get_content_range(message->response_headers,
+                       &start, &end, &total);
+
+       if (info->user_chunk_cb)
+               info->user_chunk_cb(info->local, cur, total, info->user_data);
 }
 
-/**
+static SoupSession *do_cache(cache_file_end_t *info, gboolean truncate, gchar *reason)
+{
+       char *name = g_path_get_basename(info->uri);
+       g_debug("data: do_cache - Caching file %s: %s", name, reason);
+       g_free(name);
+
+       /* TODO: move this to callback so we don't end up with 0 byte files
+        * Then change back to check for valid file after download */
+       if (truncate) info->fp = fopen_p(info->local, "w");
+       else          info->fp = fopen_p(info->local, "a");
+       long bytes = ftell(info->fp);
+
+       SoupSession *session = soup_session_async_new();
+       SoupMessage *message = soup_message_new("GET", info->uri);
+       if (message == NULL)
+               g_error("message is null, cannot parse uri");
+       g_signal_connect(message, "got-chunk", G_CALLBACK(chunk_cb), info);
+       soup_message_headers_set_range(message->request_headers, bytes, -1);
+       soup_session_queue_message(session, message, done_cb, info);
+       return session;
+}
+
+/*
  * 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)
+SoupSession *cache_file(char *base, char *path, AWeatherCacheType update,
+               AWeatherCacheChunkCallback user_chunk_cb,
+               AWeatherCacheDoneCallback user_done_cb,
+               gpointer user_data)
 {
-       gchar *url   = g_strconcat(base, path, NULL);
-       gchar *local = g_build_filename(g_get_user_cache_dir(), PACKAGE, path, NULL);
-       GFile *src   = g_file_new_for_uri(url);
-       GFile *dst   = g_file_new_for_path(local);
-
-       if (!g_file_test(local, G_FILE_TEST_EXISTS))
-               g_message("Caching file: local does not exist - %s", local);
-       else if (g_file_get_size(src) != g_file_get_size(dst))
-               g_message("Caching file: sizes mismatch - %lld != %lld",
-                               g_file_get_size(src), g_file_get_size(dst));
-       else {
-               callback(local, user_data);
-               g_object_unref(src);
-               g_object_unref(dst);
-               g_free(local);
-               g_free(url);
-               return;
-       }
 
-       char *dir = g_path_get_dirname(local);
-       if (!g_file_test(dir, G_FILE_TEST_IS_DIR))
-               g_mkdir_with_parents(dir, 0755);
-       g_free(dir);
        cache_file_end_t *info = g_malloc0(sizeof(cache_file_end_t));
-       info->callback  = callback;
-       info->url       = url;
-       info->local     = local;
-       info->src       = src;
-       info->dst       = dst;
-       info->user_data = user_data;
-       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
+       info->uri           = g_strconcat(base, path, NULL);
+       info->local         = g_build_filename(g_get_user_cache_dir(), PACKAGE, path, NULL);
+       info->fp            = NULL;
+       info->user_chunk_cb = user_chunk_cb;
+       info->user_done_cb  = user_done_cb;
+       info->user_data     = user_data;
+
+       if (update == AWEATHER_REFRESH)
+               return do_cache(info, TRUE, "cache forced");
+
+       if (update == AWEATHER_UPDATE)
+               return do_cache(info, FALSE, "attempting updating");
+
+       if (update == AWEATHER_ONCE && !g_file_test(info->local, G_FILE_TEST_EXISTS))
+               return do_cache(info, TRUE, "local does not exist");
+
+       /* No nead to cache, run the callback now and clean up */
+       user_done_cb(info->local, FALSE, user_data);
+       g_free(info->uri);
+       g_free(info->local);
+       g_free(info);
+       return NULL;
 }