[sound-juicer] Make sj_metadata_getter_list_albums() cancellable



commit e76048deaa53d304ee46f7e1369e3de71ac4a6a0
Author: Phillip Wood <phillip wood dunelm org uk>
Date:   Tue Aug 11 10:49:59 2015 +0100

    Make sj_metadata_getter_list_albums() cancellable
    
    This stops the UI from displaying stale disc information if the disc is
    ejected or the drive is changed while a lookup is in progress.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=753554

 libjuicer/sj-metadata-getter.c |   86 ++++++++++++++++++++++++++++++++++++++++
 libjuicer/sj-metadata-getter.h |    2 +
 2 files changed, 88 insertions(+), 0 deletions(-)
---
diff --git a/libjuicer/sj-metadata-getter.c b/libjuicer/sj-metadata-getter.c
index b6a9967..0de05b4 100644
--- a/libjuicer/sj-metadata-getter.c
+++ b/libjuicer/sj-metadata-getter.c
@@ -274,3 +274,89 @@ sj_metadata_getter_get_submit_url (SjMetadataGetter *mdg)
     return g_strdup (priv->url);
   return NULL;
 }
+
+GList *
+sj_metadata_getter_list_albums_finish (SjMetadataGetter  *getter,
+                                       GAsyncResult      *result,
+                                       gchar            **url,
+                                       GError           **error)
+{
+  GList *albums;
+  GTask *task;
+
+  g_return_val_if_fail (g_task_is_valid (result, getter), NULL);
+  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
+
+  task = (GTask*) result;
+  albums = g_task_propagate_pointer (task, error);
+  if (albums != NULL)
+    *url = g_strdup (g_task_get_task_data (task));
+
+  return albums;
+}
+
+static void
+free_album_list (gpointer albums)
+{
+  g_list_free_full (albums, (GDestroyNotify) album_details_free);
+}
+
+static void
+list_albums_thread_cb (GTask        *task,
+                       gpointer      source,
+                       gpointer      task_data,
+                       GCancellable *cancellable)
+{
+  guint i;
+  GError *error = NULL;
+  GList *albums = NULL;
+  gchar *url = NULL;
+  GType types[] = {
+#ifdef HAVE_MUSICBRAINZ5
+    SJ_TYPE_METADATA_MUSICBRAINZ5,
+#endif /* HAVE_MUSICBRAINZ5 */
+    SJ_TYPE_METADATA_GVFS
+  };
+
+  for (i = 0; albums == NULL && i < G_N_ELEMENTS (types); i++) {
+    SjMetadata *metadata;
+
+    metadata = g_object_new (types[i],
+                             "device", task_data,
+                             NULL);
+    bind_http_proxy_settings (metadata);
+    if (url == NULL)
+      albums = sj_metadata_list_albums (metadata, &url, cancellable, &error);
+    else
+      albums = sj_metadata_list_albums (metadata, NULL, cancellable, &error);
+
+    g_object_unref (metadata);
+
+    if (error != NULL) {
+      free_album_list (albums);
+      g_free (url);
+      g_task_return_error (task, error);
+      return;
+    }
+  }
+
+  g_task_set_task_data (task, url, g_free);
+  g_task_return_pointer (task, albums, free_album_list);
+}
+
+void
+sj_metadata_getter_list_albums_async (SjMetadataGetter    *mdg,
+                                      GCancellable        *cancellable,
+                                      GAsyncReadyCallback  callback,
+                                      gpointer             user_data)
+{
+  GTask *task;
+  SjMetadataGetterPrivate *priv;
+
+  priv = GETTER_PRIVATE (mdg);
+  task = g_task_new (mdg, cancellable, callback, user_data);
+  g_task_set_task_data (task, g_strdup (priv->cdrom), g_free);
+  g_task_set_return_on_cancel (task, TRUE);
+  g_task_run_in_thread (task, list_albums_thread_cb);
+  g_object_unref (task);
+}
diff --git a/libjuicer/sj-metadata-getter.h b/libjuicer/sj-metadata-getter.h
index a732bc6..e799e00 100644
--- a/libjuicer/sj-metadata-getter.h
+++ b/libjuicer/sj-metadata-getter.h
@@ -52,6 +52,8 @@ SjMetadataGetter *sj_metadata_getter_new (void);
 void sj_metadata_getter_set_cdrom (SjMetadataGetter *mdg, const char* device);
 gboolean sj_metadata_getter_list_albums (SjMetadataGetter *mdg, GError **error);
 char *sj_metadata_getter_get_submit_url (SjMetadataGetter *mdg);
+void sj_metadata_getter_list_albums_async (SjMetadataGetter *mdg, GCancellable *cancellable, 
GAsyncReadyCallback callback, gpointer user_data);
+GList *sj_metadata_getter_list_albums_finish (SjMetadataGetter *mdg, GAsyncResult *result, gchar **uri,  
GError **error);
 
 G_END_DECLS
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]