Re: [Banshee-List] New INotify plugin - question re LibraryTrackRemovedArgs



Jacob,

Hi.

> 1) I am probably missing something...?

No, you're right on from what I can remember.  If a single track was
removed 'LibraryTrackRemovedArgs.Track' will be non-null, if multiple
tracks were removed 'LibraryTrackRemovedArgs.Tracks' will be non-null.

Looking at the class def...

<snip>
    public class LibraryTrackRemovedArgs : EventArgs
    {
        public LibraryTrackInfo Track;
        public ICollection Tracks;
    }
</snip>

It is possible that both 'Track' and 'Tracks' could be null.  A
modification to prevent this case could look something like the attached
file...  Which is only an example, one that I haven't tested or even
compiled.  Just an example.

> 2) Why are there two fields LibraryTrackRemovedArgs.Track and
> LibraryTrackRemovedArgs.Tracks, anyway?

It's an efficiency thing.  Instead of raising an event for each track
removed, the 'Tracks' field groups all tracks that were removed as the
result of the same operation.  So in the case of say a UI or DB app, you
wouldn't have to update the display or write to the DB n-times.
Instead, you can handle the removal of all of the tracks at once and
then commit to the db / update the display only as many times as
absolutely necessary.

~Mike


/***************************************************************************
 *  LibraryTrackRemovedArgs.cs
 *
 *  Copyright (C) 2007 Michael C. Urbanski
 *  Written by Mike Urbanski <michael c urbanski gmail com>
 ****************************************************************************/

/*  THIS FILE IS LICENSED UNDER THE MIT LICENSE AS OUTLINED IMMEDIATELY BELOW: 
 *
 *  Permission is hereby granted, free of charge, to any person obtaining a
 *  copy of this software and associated documentation files (the "Software"),  
 *  to deal in the Software without restriction, including without limitation  
 *  the rights to use, copy, modify, merge, publish, distribute, sublicense,  
 *  and/or sell copies of the Software, and to permit persons to whom the  
 *  Software is furnished to do so, subject to the following conditions:
 *
 *  The above copyright notice and this permission notice shall be included in 
 *  all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 *  DEALINGS IN THE SOFTWARE.
 */

using System;
using System.Collections;
using System.Collections.Generics;

namespace Banshee.Base
{
    public class LibraryTrackRemovedArgs : EventArgs
    {
        private readonly LibraryTrackInfo track;
        private readonly ICollection<LibraryTrackInfo> tracks;
        
        public LibraryTrackInfo Track {
            get { return track; }
            protected internal set {
                TestCombination (value, tracks);
                track = value;
            }            
        }
        
        public ICollection<LibraryTrackInfo> Tracks {
            get { return tracks; }   
            protected internal set {
                TestCombination (track, value);
                tracks = value;
            }
        }
        
        public LibraryTrackRemovedArgs (LibraryTrackInfo track)
            : this (track, null) {}        
 
        public LibraryTrackRemovedArgs (ICollection<LibraryTrackInfo> tracks)
            : this (null, tracks) {}
 
        protected internal LibraryTrackRemovedArgs () {}   
        protected internal LibraryTrackRemovedArgs (LibraryTrackInfo track, 
                                                    ICollection<LibraryTrackInfo> tracks)
        {
            TestCombination (track, tracks);
            
            this.track = track;
            this.tracks = tracks;
        }
        
        private void TestCombination (LibraryTrackInfo track, 
                                      ICollection<LibraryTrackInfo> tracks)
        {
            if (track != null && tracks != null) {
                throw new ArgumentException (
                    "Either Track or Tracks must be non-null"
                );
            } else if (track == null && tracks == null) { 
                // Possibly an 'ArgumentNullException' in the case of 
                // the constructor.
                throw new InvalidOperationException (
                    "Both Track and Tracks may not be null"
                );
            }
        }    
    }
}



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