[sound-juicer] Use SjAlbumChooserDialog



commit a37d77380dceaf3c8d9d2fb3552b8531eee0b621
Author: Phillip Wood <phillip wood dunelm org uk>
Date:   Wed Aug 10 10:43:49 2016 +0100

    Use SjAlbumChooserDialog
    
    Remove the old multiple dialog implementation and use
    SjAlbumChooserDialog instead.

 data/sound-juicer.ui |   88 ----------
 src/sj-main.c        |  449 ++------------------------------------------------
 2 files changed, 14 insertions(+), 523 deletions(-)
---
diff --git a/data/sound-juicer.ui b/data/sound-juicer.ui
index c304531..237bae3 100644
--- a/data/sound-juicer.ui
+++ b/data/sound-juicer.ui
@@ -590,94 +590,6 @@ audio-volume-medium</property>
       </object>
     </child>
   </object>
-  <object class="GtkDialog" id="multiple_dialog">
-    <property name="border_width">5</property>
-    <property name="title" translatable="yes">Multiple Albums Found</property>
-    <property name="default_width">540</property>
-    <property name="default_height">375</property>
-    <property name="type_hint">dialog</property>
-    <child internal-child="vbox">
-      <object class="GtkVBox" id="dialog-vbox2">
-        <property name="visible">True</property>
-        <property name="orientation">vertical</property>
-        <property name="spacing">2</property>
-        <child>
-          <object class="GtkVBox" id="vbox4">
-            <property name="visible">True</property>
-            <property name="border_width">5</property>
-            <property name="orientation">vertical</property>
-            <property name="spacing">6</property>
-            <child>
-              <object class="GtkLabel" id="label10">
-                <property name="visible">True</property>
-                <property name="label" translatable="yes">This CD could be more than one album. Please 
select which album it is below and press &lt;i&gt;Continue&lt;/i&gt;.</property>
-                <property name="use_markup">True</property>
-                <property name="wrap">True</property>
-              </object>
-              <packing>
-                <property name="expand">False</property>
-                <property name="fill">False</property>
-                <property name="position">0</property>
-              </packing>
-            </child>
-            <child>
-              <object class="GtkScrolledWindow" id="scrolledwindow2">
-                <property name="visible">True</property>
-                <property name="can_focus">True</property>
-                <property name="hscrollbar_policy">automatic</property>
-                <property name="vscrollbar_policy">automatic</property>
-                <property name="shadow_type">in</property>
-                <property name="expand">True</property>
-                <child>
-                  <object class="GtkTreeView" id="albums_listview">
-                    <property name="visible">True</property>
-                    <property name="can_focus">True</property>
-                    <property name="expand">True</property>
-                  </object>
-                </child>
-              </object>
-              <packing>
-                <property name="position">1</property>
-              </packing>
-            </child>
-          </object>
-          <packing>
-            <property name="position">1</property>
-          </packing>
-        </child>
-        <child internal-child="action_area">
-          <object class="GtkHButtonBox" id="dialog-action_area2">
-            <property name="visible">True</property>
-            <property name="layout_style">end</property>
-            <child>
-              <object class="GtkButton" id="ok_button">
-                <property name="visible">True</property>
-                <property name="label" translatable="yes">_Continue</property>
-                <property name="use_underline">True</property>
-                <property name="can_focus">True</property>
-                <property name="can_default">True</property>
-                <property name="has_default">True</property>
-                <property name="receives_default">False</property>
-              </object>
-              <packing>
-                <property name="expand">False</property>
-                <property name="fill">False</property>
-                <property name="position">0</property>
-              </packing>
-            </child>
-          </object>
-          <packing>
-            <property name="expand">False</property>
-            <property name="pack_type">end</property>
-            <property name="position">0</property>
-          </packing>
-        </child>
-      </object>
-    </child>
-    <action-widgets>
-      <action-widget response="-5">ok_button</action-widget>
-    </action-widgets>
-  </object>
   <object class="GtkDialog" id="prefs_dialog">
     <property name="border_width">5</property>
     <property name="title" translatable="yes">Preferences</property>
diff --git a/src/sj-main.c b/src/sj-main.c
index a7ae3c9..2ab10f6 100644
--- a/src/sj-main.c
+++ b/src/sj-main.c
@@ -25,6 +25,7 @@
 #endif
 
 #include "sound-juicer.h"
+#include "sj-album-chooser-dialog.h"
 
 #include <string.h>
 #include <unistd.h>
@@ -199,8 +200,7 @@ disc_ejected_cb (void)
   stop_playback ();
   stop_ui_hack ();
   update_ui_for_album (NULL);
-  if (multiple_album_dialog != NULL &&
-      gtk_widget_get_mapped (multiple_album_dialog))
+  if (multiple_album_dialog != NULL)
     gtk_dialog_response (GTK_DIALOG (multiple_album_dialog),
                          SJ_RESPONSE_EJECTED);
   if (reread_cancellable != NULL) {
@@ -673,447 +673,26 @@ static void update_ui_for_album (AlbumDetails *album)
 }
 
 /**
- * Callback that gets fired when a user double clicks on a row
- */
-static void album_row_activated (GtkTreeView *treeview,
-                                 GtkTreePath *arg1,
-                                 GtkTreeViewColumn *arg2,
-                                 gpointer user_data)
-{
-  GtkDialog *dialog = GTK_DIALOG (user_data);
-  gtk_dialog_response (dialog, GTK_RESPONSE_OK);
-}
-
-/**
- * Callback that gets fired when an the selection changes. We use this to
- * change the sensitivity of the continue button
- */
-static void selected_album_changed (GtkTreeSelection *selection,
-                                    gpointer *user_data)
-{
-  GtkWidget *ok_button = GTK_WIDGET (user_data);
-
-  if (gtk_tree_selection_get_selected (selection, NULL, NULL)) {
-    gtk_widget_set_sensitive (ok_button, TRUE);
-  } else {
-    gtk_widget_set_sensitive (ok_button, FALSE);
-  }
-}
-
-
-/**
- * NULL safe utility to collate utf8 strings
- */
-static gint collate (const char *a, const char *b)
-{
-  gint ret_val = 0;
-
-  if (a) {
-    if (b) {
-      ret_val = g_utf8_collate (a, b);
-    } else {
-      ret_val = 1;
-    }
-  } else if (b) {
-    ret_val = -1;
-  }
-  return ret_val;
-}
-
-static gint sj_gst_date_time_compare_field (GstDateTime *lhs, GstDateTime *rhs,
-                                            gboolean (*has_field) (const GstDateTime *datetime),
-                                            gint (*get_field) (const GstDateTime *datetime))
-{
-  gint field_lhs = -1;
-  gint field_rhs = -1;
-
-  if (has_field (lhs)) {
-    field_lhs = get_field (lhs);
-  }
-  if (has_field (rhs)) {
-    field_rhs = get_field (rhs);
-  }
-
-  return (field_lhs - field_rhs);
-}
-
-static gint sj_gst_date_time_compare (gpointer lhs, gpointer rhs)
-{
-  GstDateTime *date_lhs = (GstDateTime *)lhs;
-  GstDateTime *date_rhs = (GstDateTime *)rhs;
-
-  int comparison;
-
-  comparison = sj_gst_date_time_compare_field (date_lhs, date_rhs,
-                                               gst_date_time_has_year,
-                                               gst_date_time_get_year);
-  if (comparison != 0) {
-      return comparison;
-  }
-
-  comparison = sj_gst_date_time_compare_field (date_lhs, date_rhs,
-                                               gst_date_time_has_month,
-                                               gst_date_time_get_month);
-  if (comparison != 0) {
-      return comparison;
-  }
-
-  comparison = sj_gst_date_time_compare_field (date_lhs, date_rhs,
-                                               gst_date_time_has_day,
-                                               gst_date_time_get_day);
-
-  return comparison;
-}
-
-/**
- * Utility function to sort albums in multiple_album_dialog
- */
-static gint sort_release_info (GtkTreeModel *model, GtkTreeIter *a,
-                               GtkTreeIter *b, gpointer user_data)
-{
-  AlbumDetails *album_a, *album_b;
-  GList *label_a, *label_b;
-  gint ret_val = 0;
-  const gint column = GPOINTER_TO_INT (user_data);
-
-  gtk_tree_model_get (model, a, column, &album_a, -1);
-  gtk_tree_model_get (model, b, column, &album_b, -1);
-
-  ret_val = collate (album_a->title, album_b->title);
-  if (ret_val)
-    return ret_val;
-
-  ret_val = collate (album_a->artist_sortname, album_b->artist_sortname);
-  if (ret_val)
-    return ret_val;
-
-  ret_val = collate (album_a->country, album_b->country);
-  if (ret_val)
-    return ret_val;
-
-  if (album_a->release_date) {
-    if (album_b->release_date) {
-      ret_val = sj_gst_date_time_compare (album_a->release_date, album_b->release_date);
-      if (ret_val)
-        return ret_val;
-    } else {
-      return -1;
-    }
-  } else if (album_b->release_date) {
-    return 1;
-  }
-
-  label_a = album_a->labels;
-  label_b = album_b->labels;
-  while (label_a && label_b) {
-    LabelDetails *a = label_a->data;
-    LabelDetails *b = label_b->data;
-    ret_val = collate (a->sortname,b->sortname);
-    if (ret_val)
-      return ret_val;
-
-    label_a = label_a->next;
-    label_b = label_b->next;
-  }
-  if (label_a && !label_b)
-    return -1;
-  if (!label_a && label_b)
-    return 1;
-
-  ret_val = (album_a->disc_number < album_b->disc_number) ? -1 :
-    ((album_a->disc_number > album_b->disc_number) ? 1 : 0);
-  if (ret_val)
-    return ret_val;
-
-  return (album_a->disc_count < album_b->disc_count) ? -1 :
-    ((album_a->disc_count > album_b->disc_count) ? 1 : 0);
-}
-
-
-/**
- * Utility function to format label string for multiple_album_dialog
- */
-static GString* format_label_text (GList* labels)
-{
-  int count;
-  GString *label_text;
-
-  if (labels == NULL)
-    return NULL;
-
-  label_text = g_string_new (NULL);
-  count = g_list_length (labels);
-  while (count > 2) {
-    g_string_append (label_text, ((LabelDetails*)labels->data)->name);
-    g_string_append (label_text, ", ");
-    labels = labels->next;
-    count--;
-  }
-
-  if (count > 1) {
-    g_string_append (label_text, ((LabelDetails*)labels->data)->name);
-    g_string_append (label_text, " & ");
-    labels = labels->next;
-  }
-
-  g_string_append (label_text, ((LabelDetails*)labels->data)->name);
-
-  return label_text;
-}
-
-/**
- * Utility function for multiple_album_dialog to format the
- * catalog number and barcode of a release.
- */
-static gchar* format_catalog_number_text (GList* labels)
-{
-   int count;
-   GString *catalog_text;
-   GList *l;
-
-   if (labels == NULL)
-     return NULL;
-
-   /* Count how may label entries actually have a catalog number entry */
-   count = 0;
-   for (l = labels; l != NULL; l = l->next) {
-     if (((LabelDetails*)l->data)->catalog_number != NULL)
-         count++;
-   }
-
-   if (count == 0)
-      return NULL;
-
-   /* Translators: this string is a list of catalog number(s) used by
-      the label(s) to identify the release */
-   catalog_text = g_string_new (ngettext("Catalog No.: ",
-                                         "Catalog Nos.: ",
-                                         count));
-   for(l = labels; l != NULL; l = l->next) {
-     char *catalog_number = ((LabelDetails*)l->data)->catalog_number;
-     if (catalog_number != NULL) {
-       if (count > 1) {
-           g_string_append_printf (catalog_text, "%s, ", catalog_number);
-       } else {
-           g_string_append (catalog_text, catalog_number);
-       }
-       count--;
-     }
-   }
-
-   return g_string_free (catalog_text, FALSE);
-}
-
-/**
- * Utility function for multiple_album_dialog to format the
- * release label, date and country.
- */
-static char *format_release_details (AlbumDetails *album)
-{
-  GString *details;
-  GString *label_text = NULL;
-  gchar *catalog_number_text = NULL;
-
-  details = g_string_new(NULL);
-
-  if (album->labels != NULL) {
-    label_text = format_label_text (album->labels);
-    catalog_number_text = format_catalog_number_text (album->labels);
-  }
-
-  if (!sj_str_is_empty (album->country)) {
-    if (album->labels) {
-      if (album->release_date && gst_date_time_has_year (album->release_date)) {
-        /* Translators: this string appears when multiple CDs were
-           found in musicbrainz online database, it corresponds to
-           "Released: <country> in <year> on <label>" */
-        g_string_append_printf (details, _("Released: %s in %d on %s"),
-                                album->country,
-                                gst_date_time_get_year (album->release_date),
-                                label_text->str);
-      } else {
-        /* Translators: this string appears when multiple CDs were
-           found in musicbrainz online database, it corresponds to
-           "Released: <country> on <label>" */
-        g_string_append_printf (details, _("Released: %s on %s"),
-                                album->country, label_text->str);
-      }
-    } else if (album->release_date && gst_date_time_has_year (album->release_date)) {
-      /* Translators: this string appears when multiple CDs were
-         found in musicbrainz online database, it corresponds to
-         "Released: <country> in <year>" */
-      g_string_append_printf (details, _("Released: %s in %d"),
-                              album->country,
-                              gst_date_time_get_year (album->release_date));
-    } else {
-      /* Translators: this string appears when multiple CDs were
-         found in musicbrainz online database, it corresponds to
-         "Released: <country>" */
-      g_string_append_printf (details, _("Released: %s"), album->country);
-    }
-  } else if (album->release_date && gst_date_time_has_year (album->release_date)) {
-    if (album->labels) {
-        /* Translators: this string appears when multiple CDs were
-           found in musicbrainz online database, it corresponds to
-           "Released in <year> on <label>" */
-       g_string_append_printf (details, _("Released in %d on %s"),
-                               gst_date_time_get_year (album->release_date),
-                               label_text->str);
-    } else {
-        /* Translators: this string appears when multiple CDs were
-           found in musicbrainz online database, it corresponds to
-           "Released in <year>" */
-        g_string_append_printf (details, _("Released in %d"),
-                                gst_date_time_get_year (album->release_date));
-    }
-  } else if (album->labels) {
-    /* Translators: this string appears when multiple CDs were
-       found in musicbrainz online database, it corresponds to
-       "Released on <label>" */
-    g_string_append_printf (details, _("Released on %s"), label_text->str);
-  } else {
-    g_string_append_printf (details,
-                            _("Release label, year & country unknown"));
-  }
-
-  if (catalog_number_text != NULL) {
-    if (album->barcode != NULL) {
-      g_string_append_printf (details,
-                              "\n%s, %s %s",
-                              catalog_number_text,
-                              /* Translators: this string identifies
-                                 the number of the barcode printed on
-                                 the release */
-                              _("Barcode:"),
-                              album->barcode);
-    } else {
-      g_string_append_printf (details, "\n%s", catalog_number_text);
-    }
-  } else {
-    if (album->barcode != NULL) {
-      g_string_append_printf (details,
-                              "\n%s %s",
-                              _("Barcode:"),
-                              album->barcode);
-    }
-  }
-
-  if (label_text)
-    g_string_free (label_text, TRUE);
-
-  g_free (catalog_number_text);
-
-  return g_string_free (details, FALSE);
-}
-
-/**
- * Utility function for when there are more than one albums available
+ * Utility function for when there are more than one album available
  */
 static AlbumDetails*
 choose_album(GList *albums)
 {
-  static GtkWidget *albums_listview;
-  static GtkListStore *albums_store;
-  static GtkTreeSelection *selection;
   AlbumDetails *album;
-  GtkTreeIter iter;
-  int response;
-  GtkWidget *ok_button = NULL;
-  enum COLUMNS
-  {
-    COLUMN_RELEASE,
-    COLUMN_DETAILS,
-    COLUMN_COUNT
-  };
-
-  if (multiple_album_dialog == NULL) {
-    GtkTreeViewColumn *column = gtk_tree_view_column_new ();
-    GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
-
-    multiple_album_dialog = GET_WIDGET ("multiple_dialog");
-    g_assert (multiple_album_dialog != NULL);
-    gtk_window_set_transient_for (GTK_WINDOW (multiple_album_dialog),
-                                  GTK_WINDOW (main_window));
-    albums_listview = GET_WIDGET ("albums_listview");
-    ok_button       = GET_WIDGET ("ok_button");
+  gint response;
 
-    gtk_tree_view_column_set_title (column, _("Albums"));
-    gtk_tree_view_column_pack_start (column, renderer, TRUE);
-    gtk_tree_view_column_add_attribute (column, renderer,
-                                        "markup", COLUMN_RELEASE);
-
-    g_signal_connect (albums_listview, "row-activated",
-                      G_CALLBACK (album_row_activated), multiple_album_dialog);
-
-    albums_store = gtk_list_store_new (COLUMN_COUNT,
-                                       G_TYPE_STRING,
-                                       G_TYPE_POINTER);
-    gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (albums_store),
-                                     COLUMN_DETAILS, sort_release_info,
-                                     GINT_TO_POINTER (COLUMN_DETAILS), NULL);
-    gtk_tree_view_append_column (GTK_TREE_VIEW (albums_listview), column);
-
-    gtk_tree_view_set_model (GTK_TREE_VIEW (albums_listview),
-                             GTK_TREE_MODEL (albums_store));
-    selection = gtk_tree_view_get_selection(GTK_TREE_VIEW (albums_listview));
-    gtk_tree_selection_set_mode(selection, GTK_SELECTION_BROWSE);
-    gtk_widget_set_sensitive (ok_button, FALSE);
-    g_signal_connect (selection, "changed", (GCallback)selected_album_changed,
-                      ok_button);
-  }
-
-  gtk_list_store_clear (albums_store);
-  for (; albums ; albums = g_list_next (albums)) {
-    GtkTreeIter iter;
-    AlbumDetails *album = (AlbumDetails*)(albums->data);
-    GString *album_title = g_string_new (album->title);
-    gchar *release_details = format_release_details (album);
-    gchar *markup;
-
-    if (album->disc_number > 0 && album->disc_count > 1)
-      g_string_append_printf (album_title,_(" (Disc %d/%d)"),
-                              album->disc_number, album->disc_count);
-
-    markup = g_markup_printf_escaped ("<b>%s</b>\n<b><i>%s</i></b>\n%s", album_title->str,
-                                                                  album->artist,
-                                                                  release_details);
-
-    gtk_list_store_append (albums_store, &iter);
-    gtk_list_store_set (albums_store, &iter,
-                        COLUMN_RELEASE, markup,
-                        COLUMN_DETAILS, album,
-                        -1);
-    g_free (markup);
-    g_string_free (album_title, TRUE);
-    g_free (release_details);
-  }
-
-  /* Sort the model */
-  gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (albums_store),
-                                        COLUMN_DETAILS, GTK_SORT_ASCENDING);
-
-  /* Select the first album */
-  if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (albums_store), &iter))
-  {
-    gtk_tree_selection_select_iter (selection, &iter);
-  }
-
-  gtk_widget_show_all (multiple_album_dialog);
+  multiple_album_dialog = sj_album_chooser_dialog_new (GTK_WINDOW (main_window),
+                                                       albums);
   response = gtk_dialog_run (GTK_DIALOG (multiple_album_dialog));
-  gtk_widget_hide (multiple_album_dialog);
-
-  if (response == GTK_RESPONSE_DELETE_EVENT ||
-      response == SJ_RESPONSE_EJECTED) {
-    return NULL;
-  }
+  if (response == SJ_RESPONSE_EJECTED || response == GTK_RESPONSE_NONE)
+    album = NULL;
+  else
+    album = sj_album_chooser_dialog_get_selected_album (SJ_ALBUM_CHOOSER_DIALOG (multiple_album_dialog));
+  if (response != GTK_RESPONSE_NONE)
+    gtk_widget_destroy (GTK_WIDGET (multiple_album_dialog));
+  multiple_album_dialog = NULL;
 
-  if (gtk_tree_selection_get_selected (selection, NULL, &iter)) {
-    gtk_tree_model_get (GTK_TREE_MODEL (albums_store), &iter, COLUMN_DETAILS,
-                        &album, -1);
-    return album;
-  } else {
-    return NULL;
-  }
+  return album;
 }
 
 /**


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