[easytag/wip/musicbrainz-support] Moved all GTK+ operations to Main Thead Context



commit 939ee7cb19746efea576a575da4e5dbd6093881a
Author: Abhinav <abhijangda hotmail com>
Date:   Sat May 17 20:38:41 2014 +0530

    Moved all GTK+ operations to Main Thead Context

 src/mbentityview.c       |   97 +++++++++++++++++++++++----------------------
 src/musicbrainz_dialog.c |   48 +++++++++++++++--------
 2 files changed, 81 insertions(+), 64 deletions(-)
---
diff --git a/src/mbentityview.c b/src/mbentityview.c
index 2f7fbb7..effa913 100644
--- a/src/mbentityview.c
+++ b/src/mbentityview.c
@@ -69,6 +69,13 @@ typedef struct
     GtkWidget *active_toggle_button;
 } EtMbEntityViewPrivate;
 
+typedef struct
+{
+    EtMbEntityView *entity_view;
+    GNode *child;
+    GtkTreeIter iter;
+} SearchInLevelThreadData;
+
 /**************
  * Prototypes *
  **************/
@@ -181,7 +188,6 @@ add_iter_to_list_store (GtkListStore *list_store, GNode *node)
         entity = ((EtMbEntity *)node->data)->entity;
         switch (type)
         {
-            /* Following code may depend on the search code */
             case MB_ENTITY_TYPE_ARTIST:
                 mb5_artist_get_name ((Mb5Artist)entity, name, sizeof (name));
                 gtk_list_store_append (list_store, &iter);
@@ -381,32 +387,20 @@ search_in_levels_callback (GObject *source, GAsyncResult *res,
     EtMbEntityView *entity_view;
     EtMbEntityViewPrivate *priv;
     GtkWidget *toggle_btn;
-    GtkTreeSelection *selection;
-    int depth;
-    GtkTreeIter iter;
-    GtkTreePath *path;
     gchar *entity_name;
-    GNode *child;
+    SearchInLevelThreadData *thread_data;
 
-    entity_view = ET_MB_ENTITY_VIEW (user_data);
+    thread_data = user_data;
+    entity_view = thread_data->entity_view;
     priv = ET_MB_ENTITY_VIEW_GET_PRIVATE (entity_view);
 
-    /* Depth is 1-based */
-    selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
-    gtk_tree_selection_get_selected (selection, &(priv->list_store), &iter);
-    path = gtk_tree_model_get_path (priv->list_store, &iter);
-    depth = gtk_tree_path_get_depth (path);
-    gtk_tree_path_free (path);
-    child = g_node_nth_child (priv->mb_tree_current_node,
-                              depth - 1);
-
     /* Check if child node has children or not */
-    if (!g_node_first_child (child))
+    if (!g_node_first_child (thread_data->child))
     {
         return;
     }
 
-    priv->mb_tree_current_node = child;
+    priv->mb_tree_current_node = thread_data->child;
 
     if (((EtMbEntity *)(priv->mb_tree_current_node->data))->type ==
         MB_ENTITY_TYPE_TRACK)
@@ -427,59 +421,52 @@ search_in_levels_callback (GObject *source, GAsyncResult *res,
     g_signal_connect (G_OBJECT (toggle_btn), "clicked",
                       G_CALLBACK (toggle_button_clicked), entity_view);
     priv->active_toggle_button = toggle_btn;
-    gtk_tree_model_get (priv->list_store, &iter, 0, &entity_name, -1);
+    
+    gtk_tree_model_get (priv->list_store, &thread_data->iter, 0, &entity_name, -1);
     gtk_button_set_label (GTK_BUTTON (toggle_btn), entity_name);
     gtk_widget_show_all (GTK_WIDGET (priv->bread_crumb_box));
     show_data_in_entity_view (entity_view);
     g_object_unref (res);
+    g_free (thread_data);
 }
 
 static void
 search_in_levels_thread_func (GSimpleAsyncResult *res, GObject *obj,
                               GCancellable *cancellable)
 {
-    /* TODO: Call GTK+ functions from main thread */
-    EtMbEntityView *entity_view;
-    EtMbEntityViewPrivate *priv;
-    GtkTreeSelection *selection;
-    GtkTreeIter iter;
-    GtkTreePath *path;
-    GNode *child;
+    SearchInLevelThreadData *thread_data;
     gchar mbid [NAME_MAX_SIZE];
-    int depth;
     GError *error;
 
-    entity_view = ET_MB_ENTITY_VIEW (g_async_result_get_user_data (G_ASYNC_RESULT (res)));
-    priv = ET_MB_ENTITY_VIEW_GET_PRIVATE (entity_view);
-    selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
-    gtk_tree_selection_get_selected (selection, &(priv->list_store), &iter);
-    path = gtk_tree_model_get_path (priv->list_store, &iter);
-    depth = gtk_tree_path_get_depth (path);
-    gtk_tree_path_free (path);
-    child = g_node_nth_child (priv->mb_tree_current_node,
-                              depth - 1);
-    if (((EtMbEntity *)child->data)->type == MB_ENTITY_TYPE_TRACK)
+    thread_data = g_async_result_get_user_data (G_ASYNC_RESULT (res));
+
+    if (((EtMbEntity *)thread_data->child->data)->type ==
+        MB_ENTITY_TYPE_TRACK)
     {
         return;
     }
-    else if (((EtMbEntity *)child->data)->type == MB_ENTITY_TYPE_ARTIST)
+    else if (((EtMbEntity *)thread_data->child->data)->type ==
+             MB_ENTITY_TYPE_ARTIST)
     {
-        mb5_artist_get_id (((EtMbEntity *)child->data)->entity, mbid, sizeof (mbid));
+        mb5_artist_get_id (((EtMbEntity *)thread_data->child->data)->entity,
+                           mbid, sizeof (mbid));
     }
-    else if (((EtMbEntity *)child->data)->type == MB_ENTITY_TYPE_ALBUM)
+    else if (((EtMbEntity *)thread_data->child->data)->type ==
+             MB_ENTITY_TYPE_ALBUM)
     {
-        mb5_release_get_id (((EtMbEntity *)child->data)->entity, mbid, sizeof (mbid));
+        mb5_release_get_id (((EtMbEntity *)thread_data->child->data)->entity,
+                            mbid, sizeof (mbid));
     }
 
     error = NULL;
 
-    if (!et_musicbrainz_search_in_entity (((EtMbEntity *)child->data)->type + 1,
-                                          ((EtMbEntity *)child->data)->type, mbid,
-                                          child, &error))
+    if (!et_musicbrainz_search_in_entity (((EtMbEntity *)thread_data->child->data)->type + 1,
+                                          ((EtMbEntity *)thread_data->child->data)->type,
+                                          mbid, thread_data->child, &error))
     {
         g_simple_async_report_gerror_in_idle (NULL,
                                               mb5_search_error_callback,
-                                              NULL, error);
+                                              thread_data, error);
     }
 }
 
@@ -497,11 +484,27 @@ tree_view_row_activated (GtkTreeView *tree_view, GtkTreePath *path,
                          GtkTreeViewColumn *column, gpointer user_data)
 {
     /* TODO: Add Cancellable object */
-    /* TODO: Use GSimpleAsyncResult with GError */
     /* TODO: Display Status Bar messages */
+    SearchInLevelThreadData *thread_data;
+    EtMbEntityView *entity_view;
+    EtMbEntityViewPrivate *priv;
+    GNode *child;
+    int depth;
+
+    entity_view = ET_MB_ENTITY_VIEW (user_data);
+    priv = ET_MB_ENTITY_VIEW_GET_PRIVATE (entity_view);
+    depth = gtk_tree_path_get_depth (path);
+    child = g_node_nth_child (priv->mb_tree_current_node,
+                              depth - 1);
+
+    thread_data = g_malloc (sizeof (SearchInLevelThreadData));
+    thread_data->entity_view = ET_MB_ENTITY_VIEW (user_data);
+    thread_data->child = child;
+    gtk_tree_model_get_iter (priv->list_store, &thread_data->iter, path);
+
     async_result = g_simple_async_result_new (NULL,
                                               search_in_levels_callback,
-                                              user_data,
+                                              thread_data,
                                               tree_view_row_activated);
     g_simple_async_result_run_in_thread (async_result,
                                          search_in_levels_thread_func,
diff --git a/src/musicbrainz_dialog.c b/src/musicbrainz_dialog.c
index 51b31d8..08a25e3 100644
--- a/src/musicbrainz_dialog.c
+++ b/src/musicbrainz_dialog.c
@@ -37,6 +37,12 @@
 static GNode *mb_tree_root;
 static GSimpleAsyncResult *async_result;
 
+typedef struct
+{
+    gchar *text_to_search;
+    int type;
+} ManualSearchThreadData;
+
 /*************
  * Functions *
  *************/
@@ -56,11 +62,12 @@ manual_search_callback (GObject *source, GAsyncResult *res,
     et_mb_entity_view_set_tree_root (ET_MB_ENTITY_VIEW (entityView),
                                      mb_tree_root);
     g_object_unref (res);
+    g_free (user_data);
 }
 
 void
 mb5_search_error_callback (GObject *source, GAsyncResult *res,
-                              gpointer user_data)
+                           gpointer user_data)
 {
     GError *dest;
     dest = NULL;
@@ -71,6 +78,7 @@ mb5_search_error_callback (GObject *source, GAsyncResult *res,
     gtk_statusbar_push (GTK_STATUSBAR (gtk_builder_get_object (builder, "statusbar")),
                         0, dest->message);
     g_error_free (dest);
+    g_free (user_data);
 }
 
 /*
@@ -85,25 +93,18 @@ static void
 manual_search_thread_func (GSimpleAsyncResult *res, GObject *obj,
                            GCancellable *cancellable)
 {
-    /* TODO: Call GTK+ functions from main thread */
-    GtkWidget *cb_manual_search;
-    GtkWidget *cb_manual_search_in;
-    int type;
     GError *error;
+    ManualSearchThreadData *thread_data;
+
     error = NULL;
-   
-    cb_manual_search = GTK_WIDGET (gtk_builder_get_object (builder,
-                                                           "cbManualSearch"));
-    cb_manual_search_in = GTK_WIDGET (gtk_builder_get_object (builder,
-                                                              "cbManualSearchIn"));
-    type = gtk_combo_box_get_active (GTK_COMBO_BOX (cb_manual_search_in));
+    thread_data = (ManualSearchThreadData *)g_async_result_get_user_data (G_ASYNC_RESULT (res));
 
-    if (!et_musicbrainz_search (gtk_combo_box_text_get_active_text (GTK_COMBO_BOX_TEXT (cb_manual_search)),
-                                type, mb_tree_root, &error))
+    if (!et_musicbrainz_search (thread_data->text_to_search,
+                                thread_data->type, mb_tree_root, &error))
     {
         g_simple_async_report_gerror_in_idle (NULL,
                                               mb5_search_error_callback,
-                                              NULL, error);
+                                              thread_data, error);
     }
 }
 
@@ -117,18 +118,31 @@ manual_search_thread_func (GSimpleAsyncResult *res, GObject *obj,
 static void
 btn_manual_find_clicked (GtkWidget *btn, gpointer user_data)
 {
+    GtkWidget *cb_manual_search;
+    GtkWidget *cb_manual_search_in;
+    int type;
+    ManualSearchThreadData *thread_data;
+
     if (g_node_first_child (mb_tree_root))
     {
         free_mb_tree (mb_tree_root);
         mb_tree_root = g_node_new (NULL);
     }
-
+  
+    cb_manual_search = GTK_WIDGET (gtk_builder_get_object (builder,
+                                                           "cbManualSearch"));
+    cb_manual_search_in = GTK_WIDGET (gtk_builder_get_object (builder,
+                                                              "cbManualSearchIn"));
+    type = gtk_combo_box_get_active (GTK_COMBO_BOX (cb_manual_search_in));
+    thread_data = g_malloc (sizeof (ManualSearchThreadData));
+    thread_data->type = type;
+    thread_data->text_to_search = gtk_combo_box_text_get_active_text (GTK_COMBO_BOX_TEXT (cb_manual_search));
     cancellable = g_cancellable_new ();
     /* TODO: Add Cancellable object */
-    /* TODO: Use GSimpleAsyncResult with GError */
     /* TODO: Display Status Bar messages */
     async_result = g_simple_async_result_new (NULL, manual_search_callback, 
-                                              NULL, btn_manual_find_clicked);
+                                              thread_data,
+                                              btn_manual_find_clicked);
     g_simple_async_result_run_in_thread (async_result,
                                          manual_search_thread_func, 0, NULL);
 }


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