public Track(PersistentID persistentID, string filePath, string artist, string albumArtist, string album, string title, int trackNumber, int artworkCount)
        {
            this._persistentID = persistentID;
            this._filePath     = (string.IsNullOrEmpty(filePath) ? string.Empty : filePath.Trim());
            this._artist       = (string.IsNullOrEmpty(artist) ? string.Empty : artist.Trim());
            this._albumArtist  = (string.IsNullOrEmpty(albumArtist) ? string.Empty : albumArtist.Trim());
            this._album        = (string.IsNullOrEmpty(album) ? string.Empty : album.Trim());
            this._title        = (string.IsNullOrEmpty(title) ? string.Empty : title.Trim());
            this._trackNumber  = trackNumber;
            this._artworkCount = artworkCount;

            CheckFilePath();
        }
        public static Track GetTrack(IITFileOrCDTrack iFCT, IiTunes iTunesApp)
        {
            string       filePath     = iFCT.Location;
            string       artist       = iFCT.Artist;
            string       albumArtist  = iFCT.AlbumArtist;
            string       album        = iFCT.Album;
            string       title        = iFCT.Name;
            int          trackNumber  = iFCT.TrackNumber;
            int          artworkCount = iFCT.Artwork.Count;
            PersistentID persistentID = PersistentID.GetTrackPersistentID(iTunesApp, iFCT);

            Track t = new Track(persistentID, filePath, artist, albumArtist, album, title, trackNumber, artworkCount);

            return(t);
        }
 private static IITTrack GetTrackByPersistentID(iTunesLibrary library, PersistentID persistentID)
 {
     return(library.GetLibraryPlaylist().Tracks.get_ItemByPersistentID(persistentID.HighBits, persistentID.LowBits));
 }
        private static OutputMessage ExportDirectoryArtwork(iTunesLibrary library, List <Track> trackList, string directory)
        {
            List <Track> directoryTrackList = trackList.Where(t => t.Directory == directory).ToList();

            int trackCount = directoryTrackList.Count();

            if (trackCount == 0)
            {
                return(new OutputMessage("No tracks found in the library for the directory: \"" + directory + "\".", MessageSeverity.Error));
            }

            // Minimum # of tracks in a directory (regardless of whether they have artwork attached)
            int minTracks = iTunes_Artwork_Export.Properties.Settings.Default.MinTracks;

            if (trackCount < minTracks)
            {
                return(new OutputMessage("Not enough tracks in the following directory: \"" + directory + "\" (track count is " + trackCount.ToString() + "; minimum is " + minTracks.ToString() + ").", MessageSeverity.Warning));
            }

            // Check that at least 1 track has artwork attached
            if (directoryTrackList.Where(t => t.ArtworkCount > 0).Count() == 0)
            {
                return(new OutputMessage("No artwork found for the tracks in directory: \"" + directory + "\".", MessageSeverity.Warning));
            }

            Track artworkTrack = null;

            try
            {
                artworkTrack = GetArtworkTrack(directoryTrackList);
            }
            catch (ArgumentException argEx)
            {
                return(new OutputMessage(argEx.Message, MessageSeverity.Error));
            }
            catch (ApplicationException appEx)
            {
                return(new OutputMessage(appEx.Message, MessageSeverity.Warning));
            }
            catch (Exception ex)
            {
                return(new OutputMessage(ex.Message, MessageSeverity.Error));
            }

            if (artworkTrack == null)
            {
                return(new OutputMessage("Error: No artwork track returned for directory \"" + directory + "\".", MessageSeverity.Error));
            }

            PersistentID artworkTrackPersistentID = artworkTrack.PersistentID;
            IITTrack     itArtworkTrack           = null;

            try
            {
                itArtworkTrack = ExportArtwork.GetTrackByPersistentID(library, artworkTrackPersistentID);
            }
            catch (Exception ex)
            {
                return(new OutputMessage("Error when retrieving track by Persistent ID: " + ex.Message + ".  Track: " + artworkTrack.ToString(true, true), MessageSeverity.Error));
            }

            if (itArtworkTrack != null)
            {
                // Found the iTunes Track in the library with the Artwork Track's persistent ID.

                IITArtwork iaArt = GetTrackArtwork(itArtworkTrack);
                if (iaArt != null)
                {
                    // Found the artwork on that track.

                    DirectoryInfo trackDirectoryInfo = new DirectoryInfo(directory);
                    if (trackDirectoryInfo.Exists)
                    {
                        string outputFileName = directory + "\\" + iTunes_Artwork_Export.Properties.Settings.Default.FileName;
                        try
                        {
                            FileInfo fiOutputFile = new FileInfo(outputFileName);
                            if (fiOutputFile.Exists)
                            {
                                return(new OutputMessage("File \"" + outputFileName + "\" already exists!", MessageSeverity.Debug));
                            }
                            else
                            {
                                try
                                {
                                    iaArt.SaveArtworkToFile(outputFileName);
                                    return(new OutputMessage("Saved artwork to: \"" + outputFileName + "\".  Track: " + artworkTrack.ToString(true, false), MessageSeverity.Success));
                                }
                                catch (Exception ex)
                                {
                                    return(new OutputMessage("Error when attempting to save artwork to: \"" + outputFileName + "\": " + ex.Message, MessageSeverity.Error));
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            return(new OutputMessage("Could not output artwork to \"" + outputFileName + "\": " + ex.Message + ".  Track: " + artworkTrack.ToString(true, false), MessageSeverity.Error));
                        }
                    }
                    else
                    {
                        return(new OutputMessage("Could not output artwork to directory \"" + directory + "\": directory does not exist.  Track: " + artworkTrack.ToString(true, false), MessageSeverity.Error));
                    }
                }
                else
                {
                    return(new OutputMessage("Could not retrieve artwork for Track: " + artworkTrack.ToString(true, false), MessageSeverity.Error));
                }
            }
            else
            {
                return(new OutputMessage("Could not retrieve Track by Persistent ID: " + artworkTrack.ToString(true, true), MessageSeverity.Error));
            }
        }