[sound-juicer] Store release date as GstDateTime



commit a0c0787298025f1a8c263f2b7ebcd6d07cf724c7
Author: Christophe Fergeau <cfergeau redhat com>
Date:   Wed Apr 16 19:44:23 2014 +0200

    Store release date as GstDateTime
    
    The big advantage over GDate/GDateTime is that it's possible to indicate
    that the month/day field is not set, which happens in metadata from
    musicbrainz.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=693048

 libjuicer/sj-extractor.c             |   24 ++++++++---
 libjuicer/sj-metadata-musicbrainz5.c |    6 ++-
 libjuicer/sj-structures.c            |    2 +-
 libjuicer/sj-structures.h            |    3 +-
 src/sj-extracting.c                  |    4 +-
 src/sj-main.c                        |   70 +++++++++++++++++++++++++++------
 tests/mb-test.c                      |    7 ++-
 7 files changed, 88 insertions(+), 28 deletions(-)
---
diff --git a/libjuicer/sj-extractor.c b/libjuicer/sj-extractor.c
index 4d318b7..16da7ea 100644
--- a/libjuicer/sj-extractor.c
+++ b/libjuicer/sj-extractor.c
@@ -601,20 +601,30 @@ sj_extractor_extract_track (SjExtractor *extractor, const TrackDetails *track, G
       }
 
       if (track->album->release_date) {
-        GstDateTime *date;
-        date = gst_date_time_new_ymd (g_date_get_year (track->album->release_date),
-                                      g_date_get_month (track->album->release_date),
-                                      g_date_get_day (track->album->release_date));
+        GDate *date;
+        guint year = 1;
+        guint month = 1;
+        guint day = 1;
+        if (gst_date_time_has_year (track->album->release_date)) {
+            year = gst_date_time_get_year (track->album->release_date);
+        }
+        if (gst_date_time_has_month (track->album->release_date)) {
+            month = gst_date_time_get_month (track->album->release_date);
+        }
+        if (gst_date_time_has_day (track->album->release_date)) {
+            day = gst_date_time_get_day (track->album->release_date);
+        }
+        date = g_date_new_dmy (day, month, year);
         /* We set both GST_TAG_DATE_TIME and GST_TAG_DATE as most taggers
          * use GST_TAG__DATE_TIME, but a few (id3v2mux/apemux) are still using
          * GST_TAG_DATE
          */
         gst_tag_setter_add_tags (tagger,
                                  GST_TAG_MERGE_APPEND,
-                                 GST_TAG_DATE_TIME, date,
-                                 GST_TAG_DATE, track->album->release_date,
+                                 GST_TAG_DATE_TIME, track->album->release_date,
+                                 GST_TAG_DATE, date,
                                  NULL);
-        gst_date_time_unref (date);
+        g_date_free (date);
       }
 
       if (track->album->disc_number > 0) {
diff --git a/libjuicer/sj-metadata-musicbrainz5.c b/libjuicer/sj-metadata-musicbrainz5.c
index ba8b94f..cce7503 100644
--- a/libjuicer/sj-metadata-musicbrainz5.c
+++ b/libjuicer/sj-metadata-musicbrainz5.c
@@ -654,8 +654,10 @@ make_album_from_release (SjMetadataMusicbrainz5 *self,
                        &album->artist_id);
 
   GET (date, mb5_release_get_date, release);
-  album->release_date = sj_metadata_helper_scan_date (date);
-  g_free (date);
+  if (date) {
+    album->release_date = gst_date_time_new_from_iso8601_string (date);
+    g_free (date);
+  }
 
   GET (album->asin, mb5_release_get_asin, release);
   mb5_release_get_country (release, buffer, sizeof(buffer));
diff --git a/libjuicer/sj-structures.c b/libjuicer/sj-structures.c
index 09f4009..95ee1e9 100644
--- a/libjuicer/sj-structures.c
+++ b/libjuicer/sj-structures.c
@@ -50,7 +50,7 @@ void album_details_free(AlbumDetails *album)
   g_free (album->composer_sortname);
   g_free (album->genre);
   g_free (album->album_id);
-  if (album->release_date) g_date_free (album->release_date);
+  if (album->release_date) gst_date_time_unref (album->release_date);
   g_list_foreach (album->tracks, (GFunc)track_details_free, NULL);
   g_list_free (album->tracks);
   g_free (album->artist_sortname);
diff --git a/libjuicer/sj-structures.h b/libjuicer/sj-structures.h
index f5c60a8..5cb0b50 100644
--- a/libjuicer/sj-structures.h
+++ b/libjuicer/sj-structures.h
@@ -23,6 +23,7 @@
 #define SJ_STRUCTURES_H
 
 #include <glib.h>
+#include <gst/gst.h>
 
 typedef enum _MetadataSource MetadataSource;
 
@@ -64,7 +65,7 @@ struct _AlbumDetails {
   int   disc_number;
   int   disc_count; /* number of discs in the album */
   GList* tracks;
-  GDate *release_date; /* MusicBrainz support multiple releases per album */
+  GstDateTime *release_date; /* MusicBrainz support multiple releases per album */
   char* album_id;
   char* artist_id;
   GList* labels;
diff --git a/src/sj-extracting.c b/src/sj-extracting.c
index 9fe267b..f68c6b6 100644
--- a/src/sj-extracting.c
+++ b/src/sj-extracting.c
@@ -1030,8 +1030,8 @@ filepath_parse_pattern (const char* pattern, const TrackDetails *track)
                                 filesystem_type);
         break;
       case 'y':
-        if (track->album->release_date && g_date_valid(track->album->release_date)) {
-          tmp = g_strdup_printf ("%d", g_date_get_year (track->album->release_date));
+        if (track->album->release_date && gst_date_time_has_year (track->album->release_date)) {
+          tmp = g_strdup_printf ("%d", gst_date_time_get_year (track->album->release_date));
           string = sanitize_path (tmp, filesystem_type);
           g_free (tmp);
         }
diff --git a/src/sj-main.c b/src/sj-main.c
index f3cc9f6..8910a1a 100644
--- a/src/sj-main.c
+++ b/src/sj-main.c
@@ -652,8 +652,8 @@ static void update_ui_for_album (AlbumDetails *album)
       gtk_entry_set_text (GTK_ENTRY (disc_number_entry), disc_number);
       g_free (disc_number);
     }
-    if (album->release_date && g_date_valid (album->release_date)) {
-      gchar *release_date =  g_strdup_printf ("%d", g_date_get_year (album->release_date));
+    if (album->release_date && gst_date_time_has_year (album->release_date)) {
+      gchar *release_date =  g_strdup_printf ("%d", gst_date_time_get_year (album->release_date));
       gtk_entry_set_text (GTK_ENTRY (year_entry), release_date);
       g_free (release_date);
     }
@@ -779,6 +779,51 @@ static gint collate (const char *a, const char *b)
   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
  */
@@ -807,7 +852,7 @@ static gint sort_release_info (GtkTreeModel *model, GtkTreeIter *a,
 
   if (album_a->release_date) {
     if (album_b->release_date) {
-      ret_val = g_date_compare (album_a->release_date, 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 {
@@ -888,13 +933,13 @@ static char *format_release_details (AlbumDetails *album)
 
   if (!sj_str_is_empty (album->country)) {
     if (album->labels) {
-      if (album->release_date) {
+      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>" */
         details = g_strdup_printf (_("Released: %s in %d on %s"),
                                    album->country,
-                                   g_date_get_year(album->release_date),
+                                   gst_date_time_get_year (album->release_date),
                                    label_text->str);
       } else {
         /* Translators: this string appears when multiple CDs were
@@ -902,32 +947,32 @@ static char *format_release_details (AlbumDetails *album)
          * "Released: <country> on <label>" */
         details = g_strdup_printf (_("Released: %s on %s"), album->country, label_text->str);
       }
-    } else if (album->release_date) {
+    } 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>" */
       details = g_strdup_printf (_("Released: %s in %d"), album->country,
-                                 g_date_get_year (album->release_date));
+                                 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>" */
       details = g_strdup_printf (_("Released: %s"), album->country);
     }
-  } else if (album->release_date) {
+  } 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>" */
         details = g_strdup_printf (_("Released in %d on %s"),
-                                   g_date_get_year(album->release_date),
+                                   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>" */
         details = g_strdup_printf(_("Released in %d"),
-                                  g_date_get_year(album->release_date));
+                                  gst_date_time_get_year (album->release_date));
     }
   } else if (album->labels) {
     /* Translators: this string appears when multiple CDs were
@@ -1850,10 +1895,9 @@ G_MODULE_EXPORT void on_year_edit_changed(GtkEditable *widget, gpointer user_dat
   year = atoi (yearstr);
   if (year > 0) {
     if (current_album->release_date) {
-      g_date_set_dmy (current_album->release_date, 1, 1, year);
-    } else {
-      current_album->release_date = g_date_new_dmy (1, 1, year);
+      gst_date_time_unref (current_album->release_date);
     }
+    current_album->release_date = gst_date_time_new_y (year);
   }
 }
 
diff --git a/tests/mb-test.c b/tests/mb-test.c
index b4a1ec3..7784849 100644
--- a/tests/mb-test.c
+++ b/tests/mb-test.c
@@ -59,6 +59,7 @@ metadata_cb (SjMetadataGetter *metadata, GList *albums, const GError *error)
     AlbumDetails *album;
     album = (AlbumDetails*)albums->data;
     char *disc_number;
+    char *release_date;
     g_print ("Source: %s\n", source_to_str(album->metadata_source));
     if (album->metadata_source == SOURCE_MUSICBRAINZ)
       g_print ("Album ID: %s\n", album->album_id);
@@ -71,8 +72,10 @@ metadata_cb (SjMetadataGetter *metadata, GList *albums, const GError *error)
     if (album->is_spoken_word)
       g_print ("Is spoken word\n");
     disc_number = g_strdup_printf (" (Disc %d)", album->disc_number);
-    g_print ("'%s', by %s%s, released %d-%02d-%02d\n", album->title, album->artist, album->disc_number ? 
disc_number : "",
-             g_date_get_year (album->release_date), g_date_get_month (album->release_date), g_date_get_day 
(album->release_date));
+    release_date = gst_date_time_to_iso8601_string (album->release_date);
+    g_print ("'%s', by %s%s, released %s\n", album->title, album->artist, album->disc_number ? disc_number : 
"",
+             release_date);
+    g_free (release_date);
     g_free (disc_number);
     while (album->tracks) {
       TrackDetails *track = (TrackDetails*)album->tracks->data;


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