commit 533666aef411172a51ed02561fb6a71fbdbc1aa0
Author: phantomjinx <p.g.richard...@phantomjinx.co.uk>
Date:   Sun Sep 4 23:17:17 2011 +0100

    Support for updating clarity when a track is modified
    
    * When a track has its meta-data changed then this should update the display
      according to the correct affect of the change, eg. no effect is unrelated
      to the album key, move the track to a new album etc..
    
    * album_model.*
     * remove_track function should not need to refind the relevant album item
       when in fact it has already been found and could be passed in as a 
parameter
     * it is not an error for the _get_index function to fail to find a key so
       return -1 in an if condition rather than using g_return...
     * search_for_track function simply iterates through all album items in an
       attempt to find the given track. Long winded and slow but necessary if a
       track has sufficiently changed to make looking it up in the index 
impossible
    
    * claritiy_canvas.c
     * Need to ensure that like the remove function, the covers are refreshed.
       The animate function provides this perfectly.
    
    * clarity_widget.c
     * Separate out _add and _remove functions as both used by respective 
callbacks
       and by the track_updated callback.
     * Given we already know the album item in _remove function it is 
unnecessary
       to find it again so pass it in as a parameter.
     * Completion of track_updated callback.

 plugins/clarity/album_model.c    |   28 +++++++-
 plugins/clarity/album_model.h    |    3 +-
 plugins/clarity/clarity_canvas.c |    2 +
 plugins/clarity/clarity_widget.c |  126 ++++++++++++++++++++++++++++++++-----
 4 files changed, 137 insertions(+), 22 deletions(-)
---
diff --git a/plugins/clarity/album_model.c b/plugins/clarity/album_model.c
index 821ba40..2779f6a 100644
--- a/plugins/clarity/album_model.c
+++ b/plugins/clarity/album_model.c
@@ -274,14 +274,14 @@ gboolean album_model_add_track(AlbumModel *model, Track 
*track) {
     return _insert_track(priv, track);
 }
 
-gboolean album_model_remove_track(AlbumModel *model, Track *track) {
+gboolean album_model_remove_track(AlbumModel *model, AlbumItem *item, Track 
*track) {
     g_return_val_if_fail(model, -1);
+    g_return_val_if_fail(item, -1);
     g_return_val_if_fail(track, -1);
 
     AlbumModelPrivate *priv = ALBUM_MODEL_GET_PRIVATE(model);
 
-    AlbumItem *item = album_model_get_item_with_track(model, track);
-    if (!item || !item->tracks) {
+    if (!item->tracks) {
         return FALSE;
     }
 
@@ -345,7 +345,8 @@ static gint _get_index(AlbumModelPrivate *priv, gchar 
*trk_key) {
     GList *key_list = priv->album_key_list;
 
     GList *key = g_list_find_custom(key_list, trk_key, (GCompareFunc) 
_compare_album_keys);
-    g_return_val_if_fail (key, -1);
+    if (!key)
+        return -1;
 
     gint index = g_list_position(key_list, key);
 
@@ -384,4 +385,23 @@ gint album_model_get_size(AlbumModel *model) {
     return g_list_length(priv->album_key_list);
 }
 
+AlbumItem *album_model_search_for_track(AlbumModel *model, Track *track) {
+    g_return_val_if_fail(model, NULL);
+    g_return_val_if_fail(track, NULL);
+
+    AlbumModelPrivate *priv = ALBUM_MODEL_GET_PRIVATE (model);
+    GList *album_items = g_hash_table_get_values(priv->album_hash);
+    while (album_items) {
+        AlbumItem *item = album_items->data;
+
+        if (g_list_index(item->tracks, track) > -1) {
+            return item;
+        }
+
+        album_items = album_items->next;
+    }
+
+    return NULL;
+}
+
 #endif /* ALBUM_MODEL_C_ */
diff --git a/plugins/clarity/album_model.h b/plugins/clarity/album_model.h
index 8e20fa8..00acc8b 100644
--- a/plugins/clarity/album_model.h
+++ b/plugins/clarity/album_model.h
@@ -105,7 +105,7 @@ gboolean album_model_add_track(AlbumModel *model, Track 
*track);
  * @return gboolean: whether an album model was removed.
  *
  */
-gboolean album_model_remove_track(AlbumModel *model, Track *track);
+gboolean album_model_remove_track(AlbumModel *model, AlbumItem *item, Track 
*track);
 
 AlbumItem *album_model_get_item_with_index(AlbumModel *model, gint index);
 
@@ -119,5 +119,6 @@ gint album_model_get_size(AlbumModel *model);
 
 gint compare_tracks(Track *a, Track *b);
 
+AlbumItem *album_model_search_for_track(AlbumModel *model, Track *track);
 
 #endif /* ALBUM_MODEL_H_ */
diff --git a/plugins/clarity/clarity_canvas.c b/plugins/clarity/clarity_canvas.c
index bb42a68..26b86fe 100644
--- a/plugins/clarity/clarity_canvas.c
+++ b/plugins/clarity/clarity_canvas.c
@@ -641,6 +641,8 @@ void clarity_canvas_add_album_item(ClarityCanvas *self, 
AlbumItem *item) {
 
     _init_album_item(item, index, self);
 
+    _animate_indices(priv, 0, 0);
+
     _set_loading_complete(priv, TRUE);
 }
 
diff --git a/plugins/clarity/clarity_widget.c b/plugins/clarity/clarity_widget.c
index 6981216..ca50d74 100644
--- a/plugins/clarity/clarity_widget.c
+++ b/plugins/clarity/clarity_widget.c
@@ -67,9 +67,6 @@ enum {
  *
  * popup menu
  * drag n drop
- * track added
- * track removed
- * track updated
  * set cover from file
  */
 
@@ -458,6 +455,19 @@ void clarity_widget_tracks_selected_cb(GtkPodApp *app, 
gpointer tks, gpointer da
     gtk_range_set_value(GTK_RANGE (priv->cdslider), album_index);
 }
 
+static void _add_track(ClarityWidgetPrivate *priv, Track *track) {
+    ClarityCanvas *ccanvas = CLARITY_CANVAS(priv->draw_area);
+
+    if (clarity_canvas_is_loading(ccanvas))
+        return;
+
+    if (album_model_add_track(priv->album_model, track)) {
+        AlbumItem *item = album_model_get_item_with_track(priv->album_model, 
track);
+        clarity_canvas_add_album_item(CLARITY_CANVAS(priv->draw_area), item);
+        _init_slider_range(priv);
+    }
+}
+
 void clarity_widget_track_added_cb(GtkPodApp *app, gpointer tk, gpointer data) 
{
     g_return_if_fail(CLARITY_IS_WIDGET(data));
 
@@ -475,16 +485,29 @@ void clarity_widget_track_added_cb(GtkPodApp *app, 
gpointer tk, gpointer data) {
         return;
     }
 
+    _add_track(priv, track);
+}
+
+static void _remove_track(ClarityWidgetPrivate *priv, AlbumItem *item, Track 
*track) {
+    g_return_if_fail(priv);
+
     ClarityCanvas *ccanvas = CLARITY_CANVAS(priv->draw_area);
 
     if (clarity_canvas_is_loading(ccanvas))
         return;
 
-    if (album_model_add_track(priv->album_model, track)) {
-        AlbumItem *item = album_model_get_item_with_track(priv->album_model, 
track);
-        clarity_canvas_add_album_item(CLARITY_CANVAS(priv->draw_area), item);
-        _init_slider_range(priv);
+    if(!item)
+        return;
+
+    if(g_list_length(item->tracks) == 1) {
+        // Last track in album item so remove canvas cover first
+        clarity_canvas_remove_album_item(CLARITY_CANVAS(priv->draw_area), 
item);
     }
+
+    // Remove the track from the model
+    album_model_remove_track(priv->album_model, item, track);
+
+    _init_slider_range(priv);
 }
 
 void clarity_widget_track_removed_cb(GtkPodApp *app, gpointer tk, gpointer 
data) {
@@ -498,23 +521,92 @@ void clarity_widget_track_removed_cb(GtkPodApp *app, 
gpointer tk, gpointer data)
     if (!track)
         return;
 
+    AlbumItem *item = album_model_get_item_with_track(priv->album_model, 
track);
+
+    _remove_track(priv, item, track);
+}
+
+void clarity_widget_track_updated_cb(GtkPodApp *app, gpointer tk, gpointer 
data) {
+    g_return_if_fail(CLARITY_IS_WIDGET(data));
+
+    ClarityWidget *cw = CLARITY_WIDGET(data);
+    ClarityWidgetPrivate *priv = CLARITY_WIDGET_GET_PRIVATE(cw);
+    Track *track = tk;
+
+    if (!track)
+        return;
+
     ClarityCanvas *ccanvas = CLARITY_CANVAS(priv->draw_area);
 
     if (clarity_canvas_is_loading(ccanvas))
         return;
 
-    AlbumItem *item = album_model_get_item_with_track(priv->album_model, 
track);
-    if(g_list_length(item->tracks) == 1) {
-        // Last track in album item so remove canvas cover first
-        clarity_canvas_remove_album_item(CLARITY_CANVAS(priv->draw_area), 
item);
-    }
+    AlbumItem *item;
+    gboolean findremove = FALSE;
 
-    // Remove the track from the model
-    album_model_remove_track(priv->album_model, track);
+    gint index = album_model_get_index_with_track(priv->album_model, track);
+    if (index == -1) {
+        /* The track's key could not be found according to the track!
+         * The ONLY way this could happen is if the user changed the
+         * components of the track's key. Well it should be rare but the only
+         * way to remove it from its "old" album item is to search each one
+         */
+        findremove = TRUE;
+    }
+    else {
+        /* Track has a valid key so can get the album back.
+         * Either has happened:
+         * a) Artist/Album key has been changed so the track is being moved
+         *     to another existing album
+         * b) Some other change has occurred that is irrelevant to this code.
+         *
+         * To determine if a) is the case need to determine whether track 
exists
+         * in the album items track list. If it does then b) is true and 
nothing
+         * more is required.
+         */
+        item = album_model_get_item_with_track(priv->album_model, track);
+        g_return_if_fail (item);
+
+        index = g_list_index(item->tracks, track);
+        if (index != -1) {
+            /* Track exists in the album list so ignore the change and return 
*/
+            ExtraTrackData *etd;
+            etd = track->userdata;
+            if (etd->tartwork_changed == TRUE) {
+                etd->tartwork_changed = FALSE;
+            }
+
+            return;
+        }
+        else {
+            /* Track does not exist in the album list so the artist/album
+             * key has definitely changed */
+            findremove = TRUE;
+        }
+    }
 
-    _init_slider_range(priv);
+    if (findremove) {
+        /* It has been determined that the track has had its key changed
+         * and thus a search must be performed to find the "original" album
+         * that the track belonged to, remove it then add the track to the
+         * new album.
+         */
+        item = album_model_search_for_track(priv->album_model, track);
+        /* item represents the album item containing the track */
+        if (item) {
+            g_warning("Item %s %s", item->albumname, item->artist);
+
+            /* The track is in this album so remove it in preparation for
+             * readding under the new album key
+             */
+            _remove_track(priv, item, track);
+        }
+
+        /* Create a new album item or find existing album to house the
+         * "brand new" track
+         */
+        _add_track(priv, track);
+    }
 }
 
-void clarity_widget_track_updated_cb(GtkPodApp *app, gpointer tk, gpointer 
data) {}
-
 

------------------------------------------------------------------------------
Special Offer -- Download ArcSight Logger for FREE!
Finally, a world-class log management solution at an even better 
price-free! And you'll get a free "Love Thy Logs" t-shirt when you
download Logger. Secure your free ArcSight Logger TODAY!
http://p.sf.net/sfu/arcsisghtdev2dev
_______________________________________________
gtkpod-cvs2 mailing list
gtkpod-cvs2@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/gtkpod-cvs2

Reply via email to