]> Pileus Git - grits/blobdiff - src/data/grits-http.c
Add grits_http_abort function
[grits] / src / data / grits-http.c
index 3b0c83ebd6c9fa8774db46cd63b009043a87fc92..45b30df0f767b94b7cf98fa50735f860dd0a239a 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2009-2010 Andy Spencer <andy753421@gmail.com>
+ * Copyright (C) 2009-2011 Andy Spencer <andy753421@gmail.com>
  *
  * 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
@@ -58,6 +58,19 @@ GritsHttp *grits_http_new(const gchar *prefix)
        return http;
 }
 
+/**
+ * grits_http_abort:
+ * @http: the #GritsHttp to abort
+ *
+ * Cancels any pending requests and prevents new requests.
+ */
+void grits_http_abort(GritsHttp *http)
+{
+       g_debug("GritsHttp: abort - %s", http->prefix);
+       http->aborted = TRUE;
+       soup_session_abort(http->soup);
+}
+
 /**
  * grits_http_free:
  * @http: the #GritsHttp to free
@@ -67,7 +80,6 @@ GritsHttp *grits_http_new(const gchar *prefix)
 void grits_http_free(GritsHttp *http)
 {
        g_debug("GritsHttp: free - %s", http->prefix);
-       soup_session_abort(http->soup);
        g_object_unref(http->soup);
        g_free(http->prefix);
        g_free(http);
@@ -148,6 +160,10 @@ gchar *grits_http_fetch(GritsHttp *http, const gchar *uri, const char *local,
                GritsCacheType mode, GritsChunkCallback callback, gpointer user_data)
 {
        g_debug("GritsHttp: fetch - %s mode=%d", local, mode);
+       if (http->aborted) {
+               g_debug("GritsPluginSat: _load_tile_thread - aborted");
+               return NULL;
+       }
        gchar *path = _get_cache_path(http, local);
 
        /* Unlink the file if we're refreshing it */
@@ -192,26 +208,28 @@ gchar *grits_http_fetch(GritsHttp *http, const gchar *uri, const char *local,
 
                /* Close file */
                fclose(fp);
-               if (path != part && SOUP_STATUS_IS_SUCCESSFUL(message->status_code)) {
-                       g_rename(part, path);
+               if (path != part) {
+                       if (SOUP_STATUS_IS_SUCCESSFUL(message->status_code))
+                               g_rename(part, path);
                        g_free(part);
                }
 
                /* Finished */
-               if (message->status_code == SOUP_STATUS_CANCELLED) {
+               guint status = message->status_code;
+               g_object_unref(message);
+               if (status == SOUP_STATUS_CANCELLED) {
                        return NULL;
-               } else if (message->status_code == SOUP_STATUS_REQUESTED_RANGE_NOT_SATISFIABLE) {
+               } else if (status == SOUP_STATUS_REQUESTED_RANGE_NOT_SATISFIABLE) {
                        /* Range unsatisfiable, file already complete */
-               } else if (!SOUP_STATUS_IS_SUCCESSFUL(message->status_code)) {
+               } else if (!SOUP_STATUS_IS_SUCCESSFUL(status)) {
                        g_warning("GritsHttp: done_cb - error copying file, status=%d\n"
                                        "\tsrc=%s\n"
                                        "\tdst=%s",
-                                       message->status_code, uri, path);
+                                       status, uri, path);
                        return NULL;
                }
        }
 
-
        /* TODO: free everything.. */
        return path;
 }
@@ -247,11 +265,12 @@ GList *grits_http_available(GritsHttp *http,
                const gchar *file;
                gchar *path = _get_cache_path(http, cache);
                GDir  *dir  = g_dir_open(path, 0, NULL);
-               while ((file = g_dir_read_name(dir)))
+               while (dir && (file = g_dir_read_name(dir)))
                        if (g_regex_match(filter_re, file, 0, NULL))
                                files = g_list_prepend(files, g_strdup(file));
                g_free(path);
-               g_dir_close(dir);
+               if (dir)
+                       g_dir_close(dir);
        }
 
        /* Add online files if online */