[easytag/wip/musicbrainz-support-merge: 138/180] Implemented Apply Tags for Scan Selected Files and Improved Scan Selected Files



commit 2de8de586ebb95fb5ecd5b27d6d8a02aadb3c9fa
Author: Abhinav <abhijangda hotmail com>
Date:   Fri Aug 22 20:13:09 2014 +0530

    Implemented Apply Tags for Scan Selected Files and Improved Scan Selected Files
    
    Added ApplyTAGs specifically for Scan Selected Files.
    Added et_mb_entity_view_get_selected_entity_list to get a list of selected entities & 
et_mb_entity_view_get_current_entity to get current parent entity in mbentityview.c
    Added helper function et_mb5_recording_get_artists_names in mb_search.c
    Added et_musicbrainz_set_selected_search to set the current search as SelectedSearch
    Changed btn_apply_changes_clicked to include Manual and Selected Files Search

 data/musicbrainz_dialog.ui |   10 +-
 src/mb_search.c            |   41 ++++-
 src/mb_search.h            |    2 +
 src/mbentityview.c         |   76 +++++--
 src/mbentityview.h         |    5 +-
 src/musicbrainz_dialog.c   |  545 ++++++++++++++++++++++++++++----------------
 6 files changed, 450 insertions(+), 229 deletions(-)
---
diff --git a/data/musicbrainz_dialog.ui b/data/musicbrainz_dialog.ui
index 23100ae..df69716 100644
--- a/data/musicbrainz_dialog.ui
+++ b/data/musicbrainz_dialog.ui
@@ -1,6 +1,11 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <interface>
   <!-- interface-requires gtk+ 3.0 -->
+  <object class="GtkImage" id="img_unselect_all">
+    <property name="visible">True</property>
+    <property name="can_focus">False</property>
+    <property name="stock">easytag-unselect-all</property>
+  </object>
   <object class="GtkImage" id="findimage">
     <property name="visible">True</property>
     <property name="can_focus">False</property>
@@ -16,11 +21,6 @@
     <property name="can_focus">False</property>
     <property name="stock">easytag-red-lines</property>
   </object>
-  <object class="GtkImage" id="img_unselect_all">
-    <property name="visible">True</property>
-    <property name="can_focus">False</property>
-    <property name="stock">easytag-unselect-all</property>
-  </object>
   <object class="GtkDialog" id="mbDialog">
     <property name="can_focus">False</property>
     <property name="border_width">5</property>
diff --git a/src/mb_search.c b/src/mb_search.c
index 3398caf..079f3cf 100644
--- a/src/mb_search.c
+++ b/src/mb_search.c
@@ -53,6 +53,45 @@ et_mb5_search_error_quark (void)
 }
 
 gchar *
+et_mb5_recording_get_artists_names (Mb5Recording recording)
+{
+    GString *artist;
+    Mb5ArtistCredit artist_credit;
+    int i;
+    int size;
+    gchar title [NAME_MAX_SIZE];
+
+    artist_credit = mb5_recording_get_artistcredit (recording);
+    artist = g_string_new ("");
+
+    if (artist_credit)
+    {
+        Mb5NameCreditList name_list;
+
+        name_list = mb5_artistcredit_get_namecreditlist (artist_credit);
+
+        for (i = 0; i < mb5_namecredit_list_size (name_list); i++)
+        {
+            Mb5NameCredit name_credit;
+            Mb5Artist name_credit_artist;
+
+            name_credit = mb5_namecredit_list_item (name_list, i);
+            name_credit_artist = mb5_namecredit_get_artist (name_credit);
+            size = mb5_artist_get_name (name_credit_artist, title,
+                                        sizeof (title));
+            g_string_append_len (artist, title, size);
+
+            if (i + 1 < mb5_namecredit_list_size (name_list))
+            {
+                g_string_append_len (artist, ", ", 2);
+            }
+        }
+    }
+
+    return g_string_free (artist, FALSE);
+}
+
+gchar *
 et_mb5_release_get_artists_names (Mb5Release release)
 {
     GString *album_artist;
@@ -232,7 +271,7 @@ et_musicbrainz_search_in_entity (MbEntityKind child_type,
                 Mb5Release release;
                 release = mb5_metadata_get_release (metadata);
                 list = mb5_release_get_mediumlist (release);
-                param_values[0] = "releases artists";
+                param_values[0] = "releases artists artist-credits release-groups";
 
                 for (i = 0; i < mb5_medium_list_size (list); i++)
                 {
diff --git a/src/mb_search.h b/src/mb_search.h
index 21cb092..8170512 100644
--- a/src/mb_search.h
+++ b/src/mb_search.h
@@ -102,6 +102,8 @@ et_musicbrainz_search (gchar *string, MbEntityKind type, GNode *root,
                        GError **error, GCancellable *cancellable);
 gchar *
 et_mb5_release_get_artists_names (Mb5Release release);
+gchar *
+et_mb5_recording_get_artists_names (Mb5Recording recording);
 void
 free_mb_tree (GNode **node);
 #endif /* __MB_SEARCH_H__ */
diff --git a/src/mbentityview.c b/src/mbentityview.c
index b071157..fda7d43 100644
--- a/src/mbentityview.c
+++ b/src/mbentityview.c
@@ -1254,7 +1254,18 @@ et_mb_entity_view_finalize (GObject *object)
 }
 
 EtMbEntity *
-et_mb_entity_view_get_selected_entity (EtMbEntityView *entity_view)
+et_mb_entity_view_get_current_entity (EtMbEntityView *entity_view)
+{
+    EtMbEntityViewPrivate *priv;
+    
+    priv = ET_MB_ENTITY_VIEW_GET_PRIVATE (entity_view);
+
+    return priv->mb_tree_current_node->data;
+}
+
+int
+et_mb_entity_view_get_selected_entity_list (EtMbEntityView *entity_view,
+                                            GList **list)
 {
     EtMbEntityViewPrivate *priv;
     GNode *child;
@@ -1263,33 +1274,56 @@ et_mb_entity_view_get_selected_entity (EtMbEntityView *entity_view)
     GtkTreeIter iter;
     GtkTreeSelection *selection;
     GList* list_sel_rows;
-
+    int count;
+    
+    *list = NULL;
     priv = ET_MB_ENTITY_VIEW_GET_PRIVATE (entity_view);
     selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
-    if (!gtk_tree_selection_count_selected_rows (selection))
-    {
-        return NULL;
-    }
+    count = gtk_tree_selection_count_selected_rows (selection);
 
-    list_sel_rows = gtk_tree_selection_get_selected_rows (selection, &priv->filter);
-    gtk_tree_model_get_iter (priv->filter,
-                                &filter_iter,
-                                (GtkTreePath *)g_list_first (list_sel_rows)->data);
-    g_list_free_full (list_sel_rows,
-                      (GDestroyNotify)gtk_tree_path_free);
+    if (count > 0 && count < g_node_n_children (priv->mb_tree_current_node))
+    {
+        GList *l;
 
-    gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (priv->filter),
-                                                      &iter, &filter_iter);
-    depth = 0;
+        list_sel_rows = gtk_tree_selection_get_selected_rows (selection,
+                                                              &priv->filter);
 
-    while (gtk_tree_model_iter_previous (priv->list_store, &iter))
-    {
-        depth++;
+        for (l = list_sel_rows; l != NULL; l = g_list_next (l))
+        {
+            gtk_tree_model_get_iter (priv->filter,
+                                     &filter_iter,
+                                     (GtkTreePath *)g_list_first (l)->data);
+
+            gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (priv->filter),
+                                                              &iter, &filter_iter);
+            depth = 0;
+        
+            while (gtk_tree_model_iter_previous (priv->list_store, &iter))
+            {
+                depth++;
+            }
+        
+            child = g_node_nth_child (priv->mb_tree_current_node,
+                                      depth);
+            *list = g_list_prepend (*list, child->data);
+        }
+    
+        g_list_free_full (list_sel_rows,
+                          (GDestroyNotify)gtk_tree_path_free);
     }
+    else
+    {
+        count = g_node_n_children (priv->mb_tree_current_node);
+        child = g_node_first_child (priv->mb_tree_current_node);
 
-    child = g_node_nth_child (priv->mb_tree_current_node,
-                              depth);
+        do
+        {
+            *list = g_list_prepend (*list, child->data);
+            child = g_node_next_sibling (child);
+        }
+        while (child != NULL);
+    }
 
-    return ((EtMbEntity *)child->data);
+    return count;
 }
 #endif /* ENABLE_MUSICBRAINZ */
diff --git a/src/mbentityview.h b/src/mbentityview.h
index 9a97569..fb4eeda 100644
--- a/src/mbentityview.h
+++ b/src/mbentityview.h
@@ -130,8 +130,11 @@ void
 et_mb_entity_view_select_down (EtMbEntityView *entity_view);
 void
 et_mb_entity_view_clear_all (EtMbEntityView *entity_view);
+int
+et_mb_entity_view_get_selected_entity_list (EtMbEntityView *entity_view,
+                                            GList **list);
 EtMbEntity *
-et_mb_entity_view_get_selected_entity (EtMbEntityView *entity_view);
+et_mb_entity_view_get_current_entity (EtMbEntityView *entity_view);
 G_END_DECLS
 
 #endif /* ENABLE_MUSICBRAINZ */
diff --git a/src/musicbrainz_dialog.c b/src/musicbrainz_dialog.c
index ae9c19e..03069ec 100644
--- a/src/musicbrainz_dialog.c
+++ b/src/musicbrainz_dialog.c
@@ -34,6 +34,7 @@
 #include "mb_search.h"
 #include "browser.h"
 #include "application_window.h"
+#include "dlm.h"
 
 /***************
  * Declaration *
@@ -51,10 +52,11 @@ enum TagChoiceColumns
     TAG_CHOICE_COUNTRY,
     TAG_CHOICE_COLS_N
 };
-        
+ 
 typedef enum
 {
     ET_MB_SEARCH_TYPE_MANUAL,
+    ET_MB_SEARCH_TYPE_SELECTED,
 } EtMbSearchType;
 
 typedef struct
@@ -80,9 +82,8 @@ typedef struct
     GNode *mb_tree_root;
     GSimpleAsyncResult *async_result;
     EtMbSearch *search;
-    GtkBuilder *tag_choices_builder;
-    GtkWidget *tag_choices_dialog;
-    GtkTreeModel *tag_choices_list_store;
+    GtkTreeModel *tag_choice_store;
+    GtkWidget *tag_choice_dialog;
 } MusicBrainzDialogPrivate;
 
 static MusicBrainzDialogPrivate *mb_dialog_priv;
@@ -103,7 +104,8 @@ typedef struct
 /****************
  * Declarations *
  ***************/
-
+static gboolean
+et_apply_track_tag_to_et_file (Mb5Recording recording, ET_File *et_file);
 static void
 btn_close_clicked (GtkWidget *button, gpointer data);
 
@@ -132,6 +134,27 @@ et_musicbrainz_set_search_manual (EtMbSearch **search, gchar *to_search,
     ((EtMbManualSearch *)(*search))->to_search_type = type;
 }
 
+static void
+et_musicbrainz_set_selected_search (EtMbSearch **search, gchar *to_search,
+                                    GNode *node, MbEntityKind type)
+{
+    if (*search)
+    {
+        if ((*search)->type == ET_MB_SEARCH_TYPE_SELECTED)
+        {
+            g_free (((EtMbManualSearch *)(*search))->to_search);
+        }
+
+        g_free (*search);
+    }
+
+    *search = g_malloc (sizeof (EtMbManualSearch));
+    ((EtMbManualSearch *)(*search))->to_search = g_strdup (to_search);
+    (*search)->type = ET_MB_SEARCH_TYPE_SELECTED;
+    ((EtMbManualSearch *)(*search))->parent_node = node;
+    ((EtMbManualSearch *)(*search))->to_search_type = type;
+}
+
 /*
  * manual_search_callback:
  * @source: Source Object
@@ -521,7 +544,7 @@ selected_find_callback (GObject *source, GAsyncResult *res,
     if (!g_simple_async_result_get_op_res_gboolean (G_SIMPLE_ASYNC_RESULT (res)))
     {
         g_object_unref (res);
-        g_object_unref (((SelectedFindThreadData *)user_data)->hash_table);
+        g_hash_table_destroy (((SelectedFindThreadData *)user_data)->hash_table);
         g_free (user_data);
         free_mb_tree (&mb_dialog_priv->mb_tree_root);
         mb_dialog_priv->mb_tree_root = g_node_new (NULL);
@@ -541,6 +564,8 @@ selected_find_callback (GObject *source, GAsyncResult *res,
     {
         btn_close_clicked (NULL, NULL);
     }
+
+    et_musicbrainz_set_selected_search (&mb_dialog_priv->search, "AAAA", NULL, MB_ENTITY_KIND_TRACK);
 }
 
 static void
@@ -590,21 +615,19 @@ selected_find_thread_func (GSimpleAsyncResult *res, GObject *obj,
     g_simple_async_result_set_op_res_gboolean (res, TRUE);
 }
 
-static void
-bt_selected_find_clicked (GtkWidget *widget, gpointer user_data)
+static int
+get_selected_iter_list (GtkTreeView *tree_view, GList **list)
 {
-    GtkListStore *tree_model;
+    GtkTreeModel *tree_model;
     GtkTreeSelection *selection;
     int count;
-    GList *iter_list;
     GList *l;
     GHashTable *hash_table;
     SelectedFindThreadData *thread_data;
     GtkTreeView *browser_list;
 
     selection = et_application_window_browser_get_selection (ET_APPLICATION_WINDOW (MainWindow));
-    browser_list = gtk_tree_selection_get_tree_view (selection);       
-    iter_list = NULL;
+    browser_list = gtk_tree_selection_get_tree_view (selection);
     tree_model = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(browser_list)));
     count = gtk_tree_selection_count_selected_rows(selection);
 
@@ -612,7 +635,8 @@ bt_selected_find_clicked (GtkWidget *widget, gpointer user_data)
     {
         GList* list_sel_rows;
 
-        list_sel_rows = gtk_tree_selection_get_selected_rows(selection, NULL);
+        list_sel_rows = gtk_tree_selection_get_selected_rows (selection,
+                                                              &tree_model);
 
         for (l = list_sel_rows; l != NULL; l = g_list_next (l))
         {
@@ -622,8 +646,8 @@ bt_selected_find_clicked (GtkWidget *widget, gpointer user_data)
                                         &iter,
                                         (GtkTreePath *) l->data))
             {
-                iter_list = g_list_prepend (iter_list,
-                                            gtk_tree_iter_copy (&iter));
+                *list = g_list_prepend (*list,
+                                        gtk_tree_iter_copy (&iter));
             }
         }
 
@@ -639,13 +663,13 @@ bt_selected_find_clicked (GtkWidget *widget, gpointer user_data)
                                            &current_iter))
         {
             /* No row is present, return */
-            return;
+            return 0;
         }
 
         do
         {
-            iter_list = g_list_prepend (iter_list,
-                                        gtk_tree_iter_copy (&current_iter));
+            *list = g_list_prepend (*list,
+                                    gtk_tree_iter_copy (&current_iter));
         }
         while (gtk_tree_model_iter_next(GTK_TREE_MODEL(tree_model),
                &current_iter));
@@ -654,8 +678,32 @@ bt_selected_find_clicked (GtkWidget *widget, gpointer user_data)
                                                NULL);
     }
 
-    hash_table = g_hash_table_new (g_str_hash,
-                                   g_str_equal);
+    return count;
+}
+
+static void
+bt_selected_find_clicked (GtkWidget *widget, gpointer user_data)
+{
+    GList *iter_list;
+    GList *l;
+    GHashTable *hash_table;
+    SelectedFindThreadData *thread_data;
+    GtkTreeView *browser_list;
+    GtkTreeSelection *selection;
+
+    selection = et_application_window_browser_get_selection (ET_APPLICATION_WINDOW (MainWindow));
+    browser_list = gtk_tree_selection_get_tree_view (selection);
+    iter_list = NULL;
+    l = NULL;
+
+    if (!get_selected_iter_list (GTK_TREE_VIEW (browser_list), &iter_list))
+    {
+        gtk_statusbar_push (GTK_STATUSBAR (gtk_builder_get_object (builder, "statusbar")),
+                        0, _("No Files Selected"));
+        return;
+    }
+
+    hash_table = g_hash_table_new (g_str_hash, g_str_equal);
 
     for (l = iter_list; l != NULL; l = g_list_next (l))
     {
@@ -976,221 +1024,246 @@ et_set_file_tag (ET_File *et_file, gchar *title, gchar *artist,
 static void
 btn_apply_changes_clicked (GtkWidget *widget, gpointer data)
 {
-    if (mb_dialog_priv->search->type == ET_MB_SEARCH_TYPE_MANUAL)
+    GList *file_iter_list;
+    GList *track_iter_list;
+    GList *list_iter1;
+    GList *list_iter2;
+    GtkTreeView *browser_list;
+    GtkTreeSelection *selection;
+
+    selection = et_application_window_browser_get_selection (ET_APPLICATION_WINDOW (MainWindow));
+    browser_list = gtk_tree_selection_get_tree_view (selection);       
+    file_iter_list = NULL;
+    track_iter_list = NULL;
+
+    if (!get_selected_iter_list (GTK_TREE_VIEW (browser_list),
+                                 &file_iter_list))
     {
-        ET_File *et_file;
-        EtMbEntity *et_entity;
+        gtk_statusbar_push (GTK_STATUSBAR (gtk_builder_get_object (builder, "statusbar")),
+                        0, _("No Files Selected"));
+        return;
+    }
 
-        et_entity = et_mb_entity_view_get_selected_entity (ET_MB_ENTITY_VIEW (entityView));
+    if (!et_mb_entity_view_get_selected_entity_list (ET_MB_ENTITY_VIEW (entityView),
+                                                     &track_iter_list))
+    {
+        gtk_statusbar_push (GTK_STATUSBAR (gtk_builder_get_object (builder, "statusbar")),
+                        0, _("No Track Selected"));
+        g_list_free_full (file_iter_list, (GDestroyNotify)gtk_tree_iter_free);
 
-        if (!et_entity || et_entity->type != MB_ENTITY_KIND_TRACK)
-        {
-            gtk_statusbar_push (GTK_STATUSBAR (gtk_builder_get_object (builder, "statusbar")),
-                                0, _("Please select a Track."));
-            return;
-        }
+        return;
+    }
 
-        get_first_selected_file (&et_file);
+    if (((EtMbEntity *)track_iter_list->data)->type !=
+        MB_ENTITY_KIND_TRACK)
+    {
+        gtk_statusbar_push (GTK_STATUSBAR (gtk_builder_get_object (builder, "statusbar")),
+                        0, _("No Track Selected"));
+        g_list_free_full (file_iter_list, (GDestroyNotify)gtk_tree_iter_free);
+        g_list_free (track_iter_list);
 
-        if (et_file)
-        {
-            int size;
-            Mb5ReleaseList *release_list;
-            GtkListStore *tag_choice_list_store;
-            GtkWidget *tag_choice_dialog;
-            GtkWidget *tag_choice_tree_view;
-            GtkCellRenderer *renderer;
-            GtkTreeViewColumn *column;
-            Mb5ArtistCredit artist_credit;
-            int i;
-            gchar title [NAME_MAX_SIZE];
-            gchar *album_artist;
-            gchar album[NAME_MAX_SIZE];
-            gchar date[NAME_MAX_SIZE];
-            gchar country[NAME_MAX_SIZE];
+        return;
+    }
 
-            GString *artist;
-            GtkTreeIter iter;
-            GtkTreeSelection *selection;
+    if (mb_dialog_priv->search->type == ET_MB_SEARCH_TYPE_MANUAL ||
+        mb_dialog_priv->search->type == ET_MB_SEARCH_TYPE_SELECTED)
+    {
+        EtMbEntity *et_entity;
+        EtMbEntity *album_entity;
+        GHashTable *hash_table;
+        gchar album [NAME_MAX_SIZE];
 
-            release_list = mb5_recording_get_releaselist (et_entity->entity);
-            artist_credit = mb5_recording_get_artistcredit (et_entity->entity);
-            artist = g_string_new ("");
+        album_entity = et_mb_entity_view_get_current_entity (ET_MB_ENTITY_VIEW (entityView));
+        hash_table = g_hash_table_new (g_str_hash, g_str_equal);
+        mb5_release_get_title (album_entity->entity, album, sizeof (album));
 
-            if (artist_credit)
+        for (list_iter1 = track_iter_list; list_iter1;
+             list_iter1 = g_list_next (list_iter1))
+        {
+            ET_File *best_et_file;
+            gchar *filename;
+            gchar title [NAME_MAX_SIZE];
+            File_Tag *file_tag;
+            int min_distance;
+
+            min_distance = 0xFFFF;
+            filename = NULL;
+            best_et_file = NULL;
+            et_entity = list_iter1->data;
+            mb5_recording_get_title (et_entity->entity, title,
+                                     sizeof (title));
+
+            for (list_iter2 = file_iter_list; list_iter2 != NULL;
+                 list_iter2 = g_list_next (list_iter2))
             {
-                Mb5NameCreditList name_list;
+                int distance;
+                ET_File *et_file;
 
-                name_list = mb5_artistcredit_get_namecreditlist (artist_credit);
+                et_file = et_application_window_browser_get_et_file_from_iter (ET_APPLICATION_WINDOW 
(MainWindow),
+                                                                               list_iter2->data);
+                filename = ((File_Name *)et_file->FileNameCur->data)->value;
+                file_tag = (File_Tag *)et_file->FileTag->data;
 
-                for (i = 0; i < mb5_namecredit_list_size (name_list); i++)
+                if (g_hash_table_contains (hash_table, filename))
                 {
-                    Mb5NameCredit name_credit;
-                    Mb5Artist name_credit_artist;
-
-                    name_credit = mb5_namecredit_list_item (name_list, i);
-                    name_credit_artist = mb5_namecredit_get_artist (name_credit);
-                    size = mb5_artist_get_name (name_credit_artist, title,
-                                                sizeof (title));
-                    g_string_append_len (artist, title, size);
-
-                    if (i + 1 < mb5_namecredit_list_size (name_list))
-                    {
-                        g_string_append_len (artist, ", ", 2);
-                    }
+                    continue;
                 }
-            }
-
-            size = mb5_recording_get_title (et_entity->entity, title,
-                                            sizeof (title));
-            title [size] = '\0';
-            size = mb5_release_list_size (release_list);
 
-            if (size > 1)
-            {
-                /* More than one releases show Dialog and let user decide */
-                tag_choice_dialog = GTK_WIDGET (gtk_builder_get_object (builder,
-                                                                        "tag_choice_dialog"));
-                tag_choice_tree_view = GTK_WIDGET (gtk_builder_get_object (builder,
-                                                                           "tag_choice_treeview"));
-                tag_choice_list_store = gtk_list_store_new (TAG_CHOICE_COLS_N,
-                                                            G_TYPE_STRING,
-                                                            G_TYPE_STRING,
-                                                            G_TYPE_STRING,
-                                                            G_TYPE_STRING,
-                                                            G_TYPE_STRING,
-                                                            G_TYPE_STRING);
-                renderer = gtk_cell_renderer_text_new ();
-                column = gtk_tree_view_column_new_with_attributes ("Title",
-                                                           renderer, "text",
-                                                           TAG_CHOICE_TITLE,
-                                                           NULL);
-                gtk_tree_view_append_column (GTK_TREE_VIEW (tag_choice_tree_view),
-                                             column);
-
-                renderer = gtk_cell_renderer_text_new ();
-                column = gtk_tree_view_column_new_with_attributes ("Album",
-                                                           renderer, "text",
-                                                           TAG_CHOICE_ALBUM,
-                                                           NULL);
-                gtk_tree_view_append_column (GTK_TREE_VIEW (tag_choice_tree_view),
-                                             column);
-
-                renderer = gtk_cell_renderer_text_new ();
-                column = gtk_tree_view_column_new_with_attributes ("Artist",
-                                                           renderer, "text",
-                                                           TAG_CHOICE_ARTIST,
-                                                           NULL);
-                gtk_tree_view_append_column (GTK_TREE_VIEW (tag_choice_tree_view),
-                                             column);
-
-                renderer = gtk_cell_renderer_text_new ();
-                column = gtk_tree_view_column_new_with_attributes ("Album Artist",
-                                                           renderer, "text",
-                                                           TAG_CHOICE_ALBUM_ARTIST,
-                                                           NULL);
-                gtk_tree_view_append_column (GTK_TREE_VIEW (tag_choice_tree_view),
-                                             column);
-
-                renderer = gtk_cell_renderer_text_new ();
-                column = gtk_tree_view_column_new_with_attributes ("Date",
-                                                           renderer, "text",
-                                                           TAG_CHOICE_DATE,
-                                                           NULL);
-                gtk_tree_view_append_column (GTK_TREE_VIEW (tag_choice_tree_view),
-                                             column);
-
-                renderer = gtk_cell_renderer_text_new ();
-                column = gtk_tree_view_column_new_with_attributes ("Country",
-                                                           renderer, "text",
-                                                           TAG_CHOICE_COUNTRY,
-                                                           NULL);
-                gtk_tree_view_append_column (GTK_TREE_VIEW (tag_choice_tree_view),
-                                             column);
-                gtk_tree_view_set_model (GTK_TREE_VIEW (tag_choice_tree_view),
-                                         GTK_TREE_MODEL (tag_choice_list_store));
-
-                for (size--; size >= 0; size--)
+                if (!file_tag->album && g_strcmp0 (file_tag->album, album))
                 {
-                    Mb5Release release;
-
-                    release = mb5_release_list_item (release_list, size);
-                    mb5_release_get_title (release, album, sizeof (album));
-                    album_artist = et_mb5_release_get_artists_names (release);
-                    mb5_release_get_date (release, date, sizeof (date));
-                    mb5_release_get_country (release, country, sizeof (country));
-
-                    gtk_list_store_insert_with_values (GTK_LIST_STORE (tag_choice_list_store),
-                                                       &iter, -1, TAG_CHOICE_TITLE, title,
-                                                       TAG_CHOICE_ALBUM, album,
-                                                       TAG_CHOICE_ARTIST, artist->str,
-                                                       TAG_CHOICE_ALBUM_ARTIST, album_artist,
-                                                       TAG_CHOICE_DATE, date,
-                                                       TAG_CHOICE_COUNTRY, country, -1);
-                    g_free (album_artist);
+                    continue;
                 }
 
-                gtk_widget_set_size_request (tag_choice_dialog, 600, 200);
-                gtk_widget_show_all (tag_choice_dialog);
-
-                if (gtk_dialog_run (GTK_DIALOG (tag_choice_dialog)) == 0)
+                if (file_tag->title)
+                {
+                    distance = dlm (file_tag->title, title);
+                }
+                else
                 {
-                    g_string_free (artist, TRUE);
-                    gtk_widget_destroy (tag_choice_dialog);
+                    distance = dlm (filename, title);
+                }
 
-                    return;
+                if (distance < min_distance)
+                {
+                    min_distance = distance;
+                    best_et_file = et_file;
                 }
+            }
 
-                selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (tag_choice_tree_view));
-                if (gtk_tree_selection_get_selected (selection,
-                                                     (GtkTreeModel **)&tag_choice_list_store,
-                                                     &iter))
+            if (best_et_file)
+            {
+                if (et_apply_track_tag_to_et_file (et_entity->entity,
+                                                   best_et_file))
                 {
-                    gchar *ret_album;
-                    gchar *ret_album_artist;
-                    gchar *ret_date;
-                    gchar *ret_country;
-
-                    gtk_tree_model_get (GTK_TREE_MODEL (tag_choice_list_store),
-                                        &iter, TAG_CHOICE_ALBUM, &ret_album,
-                                        TAG_CHOICE_ALBUM_ARTIST,&ret_album_artist,
-                                        TAG_CHOICE_DATE, &ret_date,
-                                        TAG_CHOICE_COUNTRY, &ret_country,
-                                        -1);
-                    et_set_file_tag (et_file, title, artist->str,
-                                     ret_album, ret_album_artist,
-                                     ret_date, ret_country);
-                    g_free (ret_album);
-                    g_free (ret_album_artist);
-                    g_free (ret_date);
-                    g_free (ret_country);
+                    g_hash_table_add (hash_table,
+                                      ((File_Name *)best_et_file->FileNameCur->data)->value);
+ 
+                    while (gtk_events_pending ())
+                    {
+                        gtk_main_iteration ();
+                    }
                 }
+            }
+        }
+    }
 
-                g_string_free (artist, TRUE);
-                gtk_widget_destroy (tag_choice_dialog);
+    g_list_free_full (file_iter_list, (GDestroyNotify)gtk_tree_iter_free);
+    g_list_free (track_iter_list);
+}
 
-                return;
-            }
+static gboolean
+et_apply_track_tag_to_et_file (Mb5Recording recording, ET_File *et_file)
+{
+    int size;
+    Mb5ReleaseList *release_list;
+    gchar title [NAME_MAX_SIZE];
+    gchar *album_artist;
+    gchar album[NAME_MAX_SIZE];
+    gchar date[NAME_MAX_SIZE];
+    gchar country[NAME_MAX_SIZE];
+    gchar *artist;
+    GtkTreeIter iter;
+    GtkTreeSelection *selection;
+    GtkWidget *tag_choice_tree_view;
 
-            mb5_release_get_title (mb5_release_list_item (release_list, 0),
-                                   album, sizeof (album));
-            album_artist = et_mb5_release_get_artists_names (mb5_release_list_item (release_list, 0));
-            mb5_release_get_date (mb5_release_list_item (release_list, 0),
-                                  date, sizeof (date));
-            mb5_release_get_country (mb5_release_list_item (release_list, 0),
-                                     country, sizeof (country));
-            et_set_file_tag (et_file, title, artist->str,
-                             album, album_artist,
-                             date, country);
+    tag_choice_tree_view = GTK_WIDGET (gtk_builder_get_object (builder, "tag_choice_treeview"));
+    release_list = mb5_recording_get_releaselist (recording);
+    artist = et_mb5_recording_get_artists_names (recording);
+    size = mb5_recording_get_title (recording, title,
+                                    sizeof (title));
+    title [size] = '\0';
+    size = mb5_release_list_size (release_list);
+
+    if (size > 1)
+    {
+        /* More than one releases show Dialog and let user decide */
+ 
+        for (size--; size >= 0; size--)
+        {
+            Mb5Release release;
+
+            release = mb5_release_list_item (release_list, size);
+            mb5_release_get_title (release, album, sizeof (album));
+            album_artist = et_mb5_release_get_artists_names (release);
+            mb5_release_get_date (release, date, sizeof (date));
+            mb5_release_get_country (release, country, sizeof (country));
+
+            gtk_list_store_insert_with_values (GTK_LIST_STORE (mb_dialog_priv->tag_choice_store),
+                                               &iter, -1, TAG_CHOICE_TITLE, title,
+                                               TAG_CHOICE_ALBUM, album,
+                                               TAG_CHOICE_ARTIST, artist,
+                                               TAG_CHOICE_ALBUM_ARTIST, album_artist,
+                                               TAG_CHOICE_DATE, date,
+                                               TAG_CHOICE_COUNTRY, country, -1);
             g_free (album_artist);
-            g_string_free (artist, TRUE);
         }
+
+        gtk_widget_set_size_request (mb_dialog_priv->tag_choice_dialog,
+                                     600, 200);
+        gtk_widget_show_all (mb_dialog_priv->tag_choice_dialog);
+
+        if (gtk_dialog_run (GTK_DIALOG (mb_dialog_priv->tag_choice_dialog)) == 0)
+        {
+            g_free (artist);
+            gtk_widget_hide (mb_dialog_priv->tag_choice_dialog);
+
+            return FALSE;
+        }
+
+        selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (tag_choice_tree_view));
+
+        if (gtk_tree_selection_get_selected (selection,
+                                             &mb_dialog_priv->tag_choice_store,
+                                             &iter))
+        {
+            gchar *ret_album;
+            gchar *ret_album_artist;
+            gchar *ret_date;
+            gchar *ret_country;
+
+            gtk_tree_model_get (GTK_TREE_MODEL (mb_dialog_priv->tag_choice_store),
+                                &iter, TAG_CHOICE_ALBUM, &ret_album,
+                                TAG_CHOICE_ALBUM_ARTIST,&ret_album_artist,
+                                TAG_CHOICE_DATE, &ret_date,
+                                TAG_CHOICE_COUNTRY, &ret_country,
+                                -1);
+            et_set_file_tag (et_file, title, artist,
+                             ret_album, ret_album_artist,
+                             ret_date, ret_country);
+            g_free (ret_album);
+            g_free (ret_album_artist);
+            g_free (ret_date);
+            g_free (ret_country);
+        }
+
+        g_free (artist);
+
+        gtk_widget_hide (mb_dialog_priv->tag_choice_dialog);
+        gtk_list_store_clear (GTK_LIST_STORE (mb_dialog_priv->tag_choice_store));
+
+        return TRUE;
     }
+
+    mb5_release_get_title (mb5_release_list_item (release_list, 0),
+                           album, sizeof (album));
+    album_artist = et_mb5_release_get_artists_names (mb5_release_list_item (release_list, 0));
+    mb5_release_get_date (mb5_release_list_item (release_list, 0),
+                          date, sizeof (date));
+    mb5_release_get_country (mb5_release_list_item (release_list, 0),
+                             country, sizeof (country));
+    et_set_file_tag (et_file, title, artist,
+                     album, album_artist,
+                     date, country);
+    g_free (album_artist);
+    g_free (artist);
+
+    return TRUE;
 }
 
 void
 et_music_brainz_dialog_destroy (GtkWidget *widget)
 {
+    gtk_widget_destroy (mb_dialog_priv->tag_choice_dialog);
     gtk_widget_destroy (widget);
     g_object_unref (G_OBJECT (builder));
     free_mb_tree (&mb_dialog_priv->mb_tree_root);
@@ -1198,6 +1271,75 @@ et_music_brainz_dialog_destroy (GtkWidget *widget)
     mb_dialog_priv = NULL;
 }
 
+static void
+et_initialize_tag_choice_dialog (void)
+{
+    GtkWidget *tag_choice_list;
+    GtkCellRenderer *renderer;
+    GtkTreeViewColumn *column;
+    GtkListStore *list_store;
+
+    mb_dialog_priv->tag_choice_dialog = GTK_WIDGET (gtk_builder_get_object (builder,
+                                                                            "tag_choice_dialog"));
+    tag_choice_list = GTK_WIDGET (gtk_builder_get_object (builder,
+                                                          "tag_choice_treeview"));
+    list_store = gtk_list_store_new (TAG_CHOICE_COLS_N, G_TYPE_STRING,
+                                     G_TYPE_STRING, G_TYPE_STRING,
+                                     G_TYPE_STRING, G_TYPE_STRING,
+                                     G_TYPE_STRING);
+    mb_dialog_priv->tag_choice_store = GTK_TREE_MODEL (list_store);
+    gtk_tree_view_set_model (GTK_TREE_VIEW (tag_choice_list),
+                             mb_dialog_priv->tag_choice_store);
+
+    renderer = gtk_cell_renderer_text_new ();
+    column = gtk_tree_view_column_new_with_attributes ("Title",
+                                               renderer, "text",
+                                               TAG_CHOICE_TITLE,
+                                               NULL);
+    gtk_tree_view_append_column (GTK_TREE_VIEW (tag_choice_list),
+                                 column);
+
+    renderer = gtk_cell_renderer_text_new ();
+    column = gtk_tree_view_column_new_with_attributes ("Album",
+                                                       renderer, "text",
+                                                       TAG_CHOICE_ALBUM,
+                                                       NULL);
+    gtk_tree_view_append_column (GTK_TREE_VIEW (tag_choice_list),
+                                 column);
+
+    renderer = gtk_cell_renderer_text_new ();
+    column = gtk_tree_view_column_new_with_attributes ("Artist",
+                                                       renderer, "text",
+                                                       TAG_CHOICE_ARTIST,
+                                                       NULL);
+    gtk_tree_view_append_column (GTK_TREE_VIEW (tag_choice_list),
+                                 column);
+
+    renderer = gtk_cell_renderer_text_new ();
+    column = gtk_tree_view_column_new_with_attributes ("Album Artist",
+                                                       renderer, "text",
+                                                       TAG_CHOICE_ALBUM_ARTIST,
+                                                       NULL);
+    gtk_tree_view_append_column (GTK_TREE_VIEW (tag_choice_list),
+                                 column);
+
+    renderer = gtk_cell_renderer_text_new ();
+    column = gtk_tree_view_column_new_with_attributes ("Date",
+                                                       renderer, "text",
+                                                       TAG_CHOICE_DATE,
+                                                       NULL);
+    gtk_tree_view_append_column (GTK_TREE_VIEW (tag_choice_list),
+                                 column);
+
+    renderer = gtk_cell_renderer_text_new ();
+    column = gtk_tree_view_column_new_with_attributes ("Country",
+                                                       renderer, "text",
+                                                       TAG_CHOICE_COUNTRY,
+                                                       NULL);
+    gtk_tree_view_append_column (GTK_TREE_VIEW (tag_choice_list),
+                                 column);
+}
+
 /*
  * et_open_musicbrainz_dialog:
  *
@@ -1236,6 +1378,7 @@ et_open_musicbrainz_dialog ()
     gtk_box_pack_start (GTK_BOX (gtk_builder_get_object (builder, "centralBox")),
                         entityView, TRUE, TRUE, 2);
 
+    et_initialize_tag_choice_dialog ();
     cb_search = GTK_WIDGET (gtk_builder_get_object (builder, "cbManualSearch"));
     g_signal_connect (gtk_bin_get_child (GTK_BIN (cb_search)), "activate",
                       G_CALLBACK (btn_manual_find_clicked), NULL);



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