public static string GetLibrarySizeMessage(iTunesLibrary library)
        {
            double librarySize        = library.GetLibrarySize();
            string librarySizeMessage = "iTunes Library Size: " + librarySize.ToString() + " Tracks.";

            return(librarySizeMessage);
        }
        private static List <Track> BuildLibraryTrackList(iTunesLibrary library, DisplayMessage dm, BackgroundWorker bgw, DoWorkEventArgs bgwArgs)
        {
            dm(new OutputMessage("Scanning track library...", MessageSeverity.Always));

            List <Track> trackList = new List <Track>();

            IITPlaylist libraryPlaylist = library.GetLibraryPlaylist();

            if (libraryPlaylist != null)
            {
                int trackCount        = libraryPlaylist.Tracks.Count;
                int currentTrackIndex = 0;

                foreach (IITTrack track in libraryPlaylist.Tracks)
                {
                    if (bgw.CancellationPending == true)
                    {
                        break;
                    }

                    double percentComplete    = Math.Floor(((double)currentTrackIndex / (double)trackCount) * 100.0);
                    int    intPercentComplete = (int)percentComplete;
                    bgw.ReportProgress(intPercentComplete);
                    currentTrackIndex++;

                    if (
                        track is IITFileOrCDTrack &&
                        ((IITFileOrCDTrack)track).Kind == ITTrackKind.ITTrackKindFile &&
                        ((IITFileOrCDTrack)track).VideoKind == ITVideoKind.ITVideoKindNone &&
                        ((IITFileOrCDTrack)track).Podcast == false &&
                        string.Compare(((IITFileOrCDTrack)track).Genre, "Voice Memo") != 0
                        )
                    {
                        try
                        {
                            IITFileOrCDTrack fileTrack = (IITFileOrCDTrack)track;
                            Track            tr        = Track.GetTrack(fileTrack, library.GetITunesApp());
                            trackList.Add(tr);
                        }
                        catch (Exception ex)
                        {
                            dm(new OutputMessage("Error: Could not get track information for Track \"" + track.Name + "\", Artist = \"" + track.Artist + "\", Album = \"" + track.Album + "\": " + ex.Message, MessageSeverity.Warning));
                        }
                    }
                    else
                    {
                        dm(new OutputMessage("Found excluded track \"" + track.Name + "\", Artist = \"" + track.Artist + "\", Album = \"" + track.Album + "\".", MessageSeverity.Debug));
                    }
                }
            }
            else
            {
                dm(new OutputMessage("Could not retrieve the library playlist from the iTunes library.", MessageSeverity.Error));
            }

            return(trackList);
        }
        public static void DoExport(iTunesLibrary library, BackgroundWorker bgw, DoWorkEventArgs bgwArgs, DisplayMessage dm)
        {
            List <Track> trackList = BuildLibraryTrackList(library, dm, bgw, bgwArgs);

            if (bgw.CancellationPending == true)
            {
                bgwArgs.Cancel = true;
                dm(new OutputMessage("Operation was canceled before export had begun.", MessageSeverity.Debug));
                return;
            }

            if (trackList.Count == 0)
            {
                dm(new OutputMessage("No tracks could be found in this iTunes library!", MessageSeverity.Error));
            }

            List <string> directoryList         = GetDirectoryList(trackList).OrderBy(d => d).ToList();
            int           directoryListSize     = directoryList.Count;
            int           currentDirectoryIndex = 0;

            bgw.ReportProgress(0);
            dm(new OutputMessage("Starting export...", MessageSeverity.Always));

            foreach (string directory in directoryList)
            {
                double percentComplete    = Math.Floor(((double)currentDirectoryIndex / (double)directoryListSize) * 100.0);
                int    intPercentComplete = (int)percentComplete;
                bgw.ReportProgress(intPercentComplete);
                currentDirectoryIndex++;

                if (bgw.CancellationPending == true)
                {
                    bgwArgs.Cancel = true;
                    dm(new OutputMessage("Operation was canceled in the middle of the export.", MessageSeverity.Debug));
                    return;
                }

                try
                {
                    OutputMessage outputMessage = ExportDirectoryArtwork(library, trackList, directory);
                    if (outputMessage != null)
                    {
                        dm(outputMessage);
                    }
                    else
                    {
                        dm(new OutputMessage("Error while processing directory \"" + directory + "\": No OutputMessage object specified from OutputDirectoryArtwork().", MessageSeverity.Error));
                    }
                }
                catch (Exception ex)
                {
                    dm(new OutputMessage("Error while processing directory \"" + directory + "\": " + ex.Message, MessageSeverity.Error));
                }
            }

            bgw.ReportProgress(100);
        }
 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));
            }
        }