Advertisement
tankcr

MusicLibUtil

May 20th, 2014
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 41.14 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Reflection;
  7. using System.Drawing;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13. using System.Text.RegularExpressions;
  14. using TagLib;
  15. using System.Xml.Serialization;
  16. using iTunesLib;
  17. using System.Xml;
  18. using System.Xml.Linq;
  19. using System.Diagnostics;
  20.  
  21. namespace MusicLibUtility
  22. {
  23.    
  24.    
  25. private class ExtensionInfo
  26.     {
  27.     public string Extension { get; private set; }
  28.     public string Description { get; private set; }
  29.     public bool IsIncluded { get; set; }
  30.     public ExtensionInfo(string extension, string description)
  31.         {
  32.             Extension = extension;
  33.             Description = description;
  34.         }
  35.     }
  36.  
  37.     public partial class Form1 : Form
  38.     {
  39.         private BackgroundWorker worker = new BackgroundWorker();
  40.         private AutoResetEvent _resetEvent = new AutoResetEvent(false);
  41.         public static iTunesAppClass itunes = new iTunesAppClass();
  42.         iTunesApp itunesApp = new iTunesLib.iTunesApp();
  43.         public static IITLibraryPlaylist mainLibrary = itunes.LibraryPlaylist;
  44.         public static IITTrackCollection ittracks = mainLibrary.Tracks;
  45.         List<string> folders = new List<string>();
  46.         List<string> files = new List<string>();
  47.         List<string> musicfiles = new List<string>();
  48.         List<string> badfiles = new List<string>();
  49.         List<string> songlist = new List<string>();
  50.         FileInfo[] filesacc = null;
  51.         DirectoryInfo[] subDirs = null;
  52.         string currfilePath;
  53.         string currDirPath;
  54.         DataTable MLTable;
  55.         DataTable badTable;
  56.  
  57.         private Dictionary<string, ExtensionInfo> extensionInfo = new Dictionary<string, ExtensionInfo>
  58.         {
  59.             {".mp3",new ExtensionInfo(".mp3", "MP3 Audio")},
  60.             {".m4a",new ExtensionInfo(".m4a", "iTunes AAC Audio")},
  61.             {".m4b",new ExtensionInfo(".m4b", "iTunes Audio Book" )},
  62.             {".m4v",new ExtensionInfo(".m4v", "iTunes Video" )},
  63.             {".m4p",new ExtensionInfo(".m4p", "iTunes Protected Audio" )},
  64.             {".flac",new ExtensionInfo(".flac", "Lossless Flac Audio")},
  65.             {".wav",new ExtensionInfo(".wav", "Windows Audio/Video" )},
  66.             {".mp4",new ExtensionInfo(".mp4", "Mp4 Audio/Video")}
  67.         };
  68.  
  69.  
  70.         public Form1()
  71.         {
  72.             InitializeComponent();
  73.             try
  74.             {label17.Text = itunes.CurrentTrack.Name;}
  75.             catch
  76.             { }
  77.             try
  78.             {
  79.                 label14.Text = itunes.CurrentTrack.Artist;
  80.             }
  81.             catch
  82.             { }
  83.             try
  84.             {
  85.                 if (itunes.CurrentTrack.Kind == ITTrackKind.ITTrackKindFile)
  86.                 {
  87.                     IITFileOrCDTrack file = (IITFileOrCDTrack)itunes.CurrentTrack;
  88.                     if (file.Location != null)
  89.                     {
  90.                         FileInfo fi = new FileInfo(file.Location);
  91.                         if (fi.Exists)
  92.                         {
  93.                             currfilePath = file.Location;
  94.                             currDirPath = fi.Directory.ToString();
  95.                         }
  96.                         else
  97.                             currfilePath = "not found " + file.Location;
  98.                     }
  99.                 }
  100.                 label13.Text = currfilePath;
  101.             }
  102.             catch{}
  103.             try
  104.             {
  105.                 try {
  106.                         var file = TagLib.File.Create(currfilePath);
  107.                         if (file.Tag.Pictures.Length >= 1)
  108.                         {
  109.                             var bin = (byte[])(file.Tag.Pictures[0].Data.Data);
  110.                             panel7.BackgroundImage = Image.FromStream(new MemoryStream(bin)).GetThumbnailImage(95, 95, null, IntPtr.Zero);
  111.                         }
  112.                         else { int albumart = 0; panel7.BackgroundImage = MusicLibUtility.Properties.Resources.nocover1; }
  113.                     }
  114.                 catch
  115.                 {
  116.                     int albumart = 0;
  117.                     panel7.BackgroundImage = MusicLibUtility.Properties.Resources.nocover1;
  118.                 }
  119.             }
  120.             catch { }
  121.             var myiTunes = new iTunesAppClass();
  122.             this.pictureBox1.Image = Properties.Resources.skinitunes;
  123.             //string statestring = null;
  124.             //int state = ((int)itunes.PlayerState);
  125.             //if (state < 1) { statestring = "Stopped"; };
  126.             //if (state == 1) { statestring = "Playing"; };
  127.  
  128.             itunesApp.OnPlayerPlayEvent += new _IiTunesEvents_OnPlayerPlayEventEventHandler(itunesApp_OnPlayerPlayEvent);      
  129.             dataGridView1.DataSource = dataTable1;
  130.             backgroundWorker4.RunWorkerAsync();
  131.  
  132.         }
  133.  
  134.         private void itunesApp_OnPlayerPlayEvent(object iTrack)
  135.         {
  136.             IITTrack currentTrack = (IITTrack)iTrack;
  137.             string playerstate = itunes.PlayerState.ToString();
  138.             string trackName = currentTrack.Name;
  139.             string artist = currentTrack.Artist;
  140.             string album = currentTrack.Album;
  141.             label17.Text = trackName;
  142.             label14.Text = artist;
  143.             if (itunes.CurrentTrack.Kind == ITTrackKind.ITTrackKindFile)
  144.             {
  145.                 IITFileOrCDTrack file = (IITFileOrCDTrack)itunes.CurrentTrack;
  146.                 if (file.Location != null)
  147.                 {
  148.                     FileInfo fi = new FileInfo(file.Location);
  149.                     if (fi.Exists)
  150.                     {
  151.                         currfilePath = file.Location;
  152.                         currDirPath = fi.Directory.ToString();
  153.                     }
  154.                     else
  155.                         currfilePath = "not found " + file.Location;
  156.                 }
  157.             }
  158.             label13.Text = currfilePath;
  159.             try
  160.             { panel7.BackgroundImage.Dispose(); }
  161.             catch { }
  162.                 try
  163.                 {
  164.                     var file = TagLib.File.Create(currfilePath);
  165.                     if (file.Tag.Pictures.Length >= 1)
  166.                     {
  167.                         var bin = (byte[])(file.Tag.Pictures[0].Data.Data);
  168.                         panel7.BackgroundImage = Image.FromStream(new MemoryStream(bin)).GetThumbnailImage(95, 95, null, IntPtr.Zero);
  169.                     }
  170.                     else { int albumart = 0; panel7.BackgroundImage = MusicLibUtility.Properties.Resources.nocover1; }
  171.                 }
  172.                 catch { int albumart = 0; panel7.BackgroundImage = MusicLibUtility.Properties.Resources.nocover1; }
  173.         }
  174.         private void label1_Click(object sender, EventArgs e)
  175.         {
  176.             label1.Text = itunes.LibraryXMLPath;
  177.         }
  178.  
  179.         private void textBox1_TextChanged(object sender, EventArgs e)
  180.         {
  181.  
  182.         }
  183.  
  184.         private void folderBrowserDialog1_HelpRequest(object sender, EventArgs e)
  185.         {
  186.  
  187.         }
  188.  
  189.         private void folderPath_Click(object sender, EventArgs e)
  190.         {
  191.             if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
  192.             {
  193.                 this.label4.Text = folderBrowserDialog1.SelectedPath;
  194.                 string dir = label4.Text;
  195.             }
  196.         }
  197.         private void Form1_Load(object sender, EventArgs e)
  198.         {
  199.             iTunesAppClass itunes = new iTunesAppClass();
  200.             string iTXMLPath = itunes.LibraryXMLPath;
  201.             label1.Text = iTXMLPath;
  202.             label4.Text = Path.GetDirectoryName(iTXMLPath);
  203.             if (!backgroundWorker4.IsBusy)
  204.             { backgroundWorker4.RunWorkerAsync(); }
  205.             if ((int)itunes.PlayerState == 0)
  206.             {
  207.                 button5.Image = MusicLibUtility.Properties.Resources.playTrans;
  208.             }
  209.            
  210.             else if ((int)itunes.PlayerState == 1)
  211.             {
  212.                 button5.Image = MusicLibUtility.Properties.Resources.pauseTrans;
  213.             }
  214.         }
  215.  
  216.         private void groupBox2_Enter(object sender, EventArgs e)
  217.         {
  218.  
  219.         }
  220.  
  221.         private void button1_Click(object sender, EventArgs e)
  222.         {
  223.  
  224.             Application.DoEvents();
  225.             label6.Text = null;
  226.             label11.Text = null;
  227.             label12.Text = null;
  228.             pictureBox4.Image = null;
  229.             pictureBox6.Image = null;
  230.             pictureBox7.Image = null;
  231.             pictureBox3.Image = null;
  232.             pictureBox5.Image = null;
  233.             pictureBox8.Image = null;
  234.             pictureBox1.Image = Properties.Resources.running;
  235.             string filepath = label4.Text;
  236.             DirectoryInfo rootDir = new DirectoryInfo(filepath);
  237.             if (!backgroundWorker1.IsBusy)
  238.             { backgroundWorker1.RunWorkerAsync(); }
  239.             pictureBox3.Image = Properties.Resources.scanning;
  240.             if (!backgroundWorker2.IsBusy)
  241.             { backgroundWorker2.RunWorkerAsync(); }
  242.             if (!backgroundWorker3.IsBusy)
  243.             { backgroundWorker3.RunWorkerAsync(); }
  244.             if (!backgroundWorker7.IsBusy)
  245.             { backgroundWorker7.RunWorkerAsync(); }
  246.  
  247.             //label6.Text = "Task Completed";
  248.             //this.pictureBox1.Image = Properties.Resources.skinitunes;
  249.  
  250.         }
  251.         private void pictureBox1_Click_1(object sender, EventArgs e)
  252.         {
  253.  
  254.         }
  255.  
  256.         private void label4_Click(object sender, EventArgs e)
  257.         {
  258.  
  259.         }
  260.  
  261.         private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
  262.         {
  263.             Control.CheckForIllegalCrossThreadCalls = false;
  264.             backgroundWorker2.Dispose();
  265.             backgroundWorker3.Dispose();
  266.             backgroundWorker1.WorkerSupportsCancellation = true;
  267.             label6.Text = null;
  268.             label7.Text = null;
  269.            
  270.             {
  271.  
  272.                 string filepath = label4.Text;
  273.                 DirectoryInfo rootDir = new DirectoryInfo(filepath);
  274.  
  275.                 foreach (var file in rootDir.GetFiles())
  276.                 {
  277.                     ExtensionInfo info;
  278.                     if (extensionInfo.TryGetValue(file.Extension, out info) && info.IsIncluded)
  279.                     {
  280.                         label5.Text = info.Description;
  281.                     }
  282.                 }
  283.  
  284. //                foreach (string ext in extensions)
  285. //                {
  286. //                    try
  287. //                    {
  288. //                        filesacc = rootDir.GetFiles(ext);
  289. //                        if (ext == "*.mp3")
  290. //                        { label5.Text = "MP3 Audio"; }
  291. //                        else if (ext == "*.m4a")
  292. //                        { label5.Text = "iTunes AAC Audio"; }
  293. //                        else if (ext == "*.m4b")
  294. //                        { label5.Text = "iTunes Audio Book"; }
  295. //                        else if (ext == "*.m4v")
  296. //                        { label5.Text = "iTunes Video"; }
  297. //                        else if (ext == "*.m4p")
  298. //                        { label5.Text = "iTunes Protected Audio"; }
  299. //                        else if (ext == "*.flac")
  300. //                        { label5.Text = "Lossless Flac Audio"; }
  301.  //                       else if (ext == "*.wav")
  302.  //                       { label5.Text = "Windows Audio"; }
  303. //                        else if (ext == "*.mp4")
  304. //                        { label5.Text = "Mp4 Audio/Video"; }
  305. //                    }
  306. //                    catch (UnauthorizedAccessException)
  307. //                    { }
  308.                     if (filesacc != null)
  309.                     {
  310.                         foreach (FileInfo fi in filesacc)
  311.                         {
  312.                             files.Add(fi.FullName.ToString());
  313.                             label8.Text = fi.FullName;
  314.                         }
  315.                     }
  316.                     subDirs = rootDir.GetDirectories();
  317.                     foreach (DirectoryInfo dirInfo in subDirs)
  318.                     {
  319.                         try
  320.                         {
  321.                             foreach (string GFI in Directory.GetFiles(dirInfo.FullName, ext, SearchOption.AllDirectories))
  322.                             {
  323.                                 label7.Text = Path.GetDirectoryName(GFI);
  324.                                 label8.Text = Path.GetFileName(GFI);
  325.                                 files.Add(GFI);
  326.                                 label7.Text = "Directory";
  327.                                 label8.Text = "File";
  328.                             }
  329.                         }
  330.                         catch (UnauthorizedAccessException) { }
  331.                     }
  332.  
  333.                 }
  334.  
  335.             this.pictureBox3.Image = null;
  336.             Int32 filecount = files.Count;
  337.             label7.Text = "Directories";
  338.             this.pictureBox4.Image = Properties.Resources.music16;
  339.             pictureBox5.Image = MusicLibUtility.Properties.Resources.scanning;
  340.             this.pictureBox1.Image = Properties.Resources.skinitunes;
  341.             label11.Text = "Preparing to scan for corupt media files";
  342.             Thread.Sleep(2000);
  343.  
  344.         }
  345.  
  346.         private void label6_Click(object sender, EventArgs e)
  347.         {
  348.             label6.Text = "";
  349.         }
  350.  
  351.         private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  352.         {
  353.  
  354.             pictureBox3.Image = null;    
  355.             label8.Text = "Complete";
  356.             backgroundWorker1.CancelAsync();
  357.  
  358.         }
  359.  
  360.         private void label7_Click(object sender, EventArgs e)
  361.         {
  362.             try
  363.             {            
  364.                 Process.Start(@label7.Text.ToString());
  365.             }
  366.             catch { }
  367.         }
  368.  
  369.         private void label5_Click(object sender, EventArgs e)
  370.         {
  371.  
  372.         }
  373.  
  374.         private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
  375.         {
  376.             do
  377.             {
  378.  
  379.                 label6.Text = "Your Directories are Being Scanned";
  380.                 Thread.Sleep(200);
  381.  
  382.                 label6.Text = "Your Directories are Being Scanned.";
  383.                 Thread.Sleep(200);
  384.  
  385.                 label6.Text = "Your Directories are Being Scanned..";
  386.                 Thread.Sleep(200);
  387.  
  388.                 label6.Text = "Your Directories are Being Scanned...";
  389.                 Thread.Sleep(200);
  390.  
  391.                 label6.Text = "Your Directories are Being Scanned....";
  392.                 Thread.Sleep(200);
  393.             }
  394.             while (backgroundWorker1.IsBusy);
  395.             label6.Text = "Directory Scan Complete " + files.Count.ToString() + " Media Files located";
  396.             string ITPlaypat = "ITPlayerState";
  397.             int state = ((int)itunes.PlayerState);
  398.             string statestring = null;
  399.             if (state < 1) { statestring = "Stopped"; };
  400.             if (state == 1) { statestring = "Playing"; };
  401.             if (state == 1)
  402.             {
  403.                 do
  404.                 {
  405.                     label17.Text = ("Pausing iTunes to maintain file integrity");
  406.                     itunes.Pause();
  407.                     state = (int)itunes.PlayerState;
  408.                 }
  409.                 while (state == 1);
  410.             }
  411.             if (state < 1) { statestring = "Stopped"; };
  412.             if (state == 1) { statestring = "Playing"; };
  413.  
  414.             //label5.Text = "Itunes is now " + Regex.Replace(itunes.PlayerState.ToString(), ITPlaypat, "");
  415.  
  416.             pictureBox1.Image = Properties.Resources.running;
  417.             foreach (string file in files)
  418.             {
  419.                 label8.Text = "Scanning "+ file;
  420.                 try { string taglibfile = TagLib.File.Create(file).Tag.Title; musicfiles.Add(file); Console.WriteLine(taglibfile); }
  421.                 catch { badfiles.Add(file); }
  422.             }
  423.             XDocument baddoc = new XDocument
  424.             (new XElement("Corrupt",
  425.                badfiles.Select(badfile =>
  426.                new XElement("File", badfile))));
  427.             baddoc.Save(label4.Text + "\\badfiles.xml");
  428.         }
  429.         private void backgroundWorker2_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  430.         {
  431.  
  432.             pictureBox5.Image = null;
  433.             pictureBox6.Image = Properties.Resources.music16;
  434.             backgroundWorker2.CancelAsync();
  435.  
  436.         }
  437.         private void backgroundWorker3_DoWork(object sender, DoWorkEventArgs e)
  438.         {
  439.  
  440.             do { Thread.Sleep(800); }
  441.             while (backgroundWorker1.IsBusy);
  442.             do
  443.             {
  444.  
  445.                 label11.Text = "Scanning For Corrupt Files";
  446.                 Thread.Sleep(200);
  447.                 label11.Text = "Scanning For Corrupt Files.";
  448.                 Thread.Sleep(200);
  449.                 label11.Text = "Scanning For Corrupt Files..";
  450.                 Thread.Sleep(200);
  451.                 label11.Text = "Scanning For Corrupt Files...";
  452.                 Thread.Sleep(200);
  453.                 label11.Text = "Scanning For Corrupt Files....";
  454.                 Thread.Sleep(200);
  455.             }
  456.             while (backgroundWorker2.IsBusy);
  457.             pictureBox5.Image = null;
  458.             pictureBox6.Image = Properties.Resources.music16;
  459.             label11.Text = badfiles.Count + " Corrupt Files Found";
  460.             pictureBox8.Image = MusicLibUtility.Properties.Resources.scanning;
  461.             XDocument doc = new XDocument();
  462.             XElement songsElement = new XElement("Songs");
  463.             foreach (var musicfile in musicfiles)
  464.             {
  465.                 XElement songElement = new XElement("Song");
  466.                 string songTitle;
  467.                 try { songTitle = (TagLib.File.Create(musicfile).Tag.Title); }
  468.                 catch { songTitle = "Missing"; }
  469.                 uint songTNint;
  470.                 try { songTNint = (TagLib.File.Create(musicfile).Tag.Track); }
  471.                 catch { songTNint = 00; }
  472.                 string songTN = songTNint.ToString();
  473.                 string songPath = musicfile;
  474.                 string songArtist;
  475.                 try { songArtist = (TagLib.File.Create(musicfile).Tag.Performers[0]); }
  476.                 catch { songArtist = "Missing"; }
  477.                 List<string> songGenres = new List<string>();
  478.                 foreach (string Genre in (TagLib.File.Create(musicfile).Tag.Genres))
  479.                 { songGenres.Add(Genre); }
  480.                 string songGenre;
  481.                 if (songGenres.Count > 1) { songGenre = (songGenres[0] + "/" + songGenres[1]); }
  482.                 else { try { songGenre = songGenres[0]; } catch { songGenre = "Missing"; } }
  483.                 //songGenre = string.Join(songGenres.Select(songGenre=>songGenre+"/"));
  484.                 songArtist = Regex.Replace(songArtist, @"[^\u0020-\u007E]", string.Empty);
  485.                 //songlist.Add("Adding "+songTitle+", Artist "+songArtist+", at path "+songPath+" to DirLib file.");
  486.                 XElement titleElement = new XElement("Title", songTitle);
  487.                 XElement tnElement = new XElement("Track", songTN);
  488.                 XElement pathElement = new XElement("Path", musicfile);
  489.                 XElement artistElement = new XElement("Artist", songArtist);
  490.                 XElement genreElement = new XElement("Genre", songGenre);
  491.                 songElement.Add(titleElement);
  492.                 songElement.Add(tnElement);
  493.                 songElement.Add(pathElement);
  494.                 songElement.Add(artistElement);
  495.                 songElement.Add(genreElement);
  496.                 songsElement.Add(songElement);
  497.             }
  498.             //System.IO.File.WriteAllLines(@libraryPath+"\\WriteLines.txt", songlist);    
  499.             doc.Add(songsElement);
  500.             doc.Save(label4.Text + "\\DirLib.xml");
  501.             itunes.Play();
  502.         }
  503.         private void backgroundWorker3_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  504.         {
  505.  
  506.             pictureBox8.Image = null;
  507.             pictureBox7.Image = Properties.Resources.music16;
  508.             label12.Text = "Media Library Database Created";
  509.             pictureBox1.Image = MusicLibUtility.Properties.Resources.skinitunes;
  510.             backgroundWorker3.CancelAsync();
  511.  
  512.  
  513.         }
  514.         private void pictureBox3_Click(object sender, EventArgs e)
  515.         {
  516.  
  517.         }
  518.  
  519.         private void pictureBox2_Click(object sender, EventArgs e)
  520.         {
  521.  
  522.         }
  523.  
  524.         private void pictureBox3_Click_1(object sender, EventArgs e)
  525.         {
  526.  
  527.         }
  528.  
  529.         private void label8_Click(object sender, EventArgs e)
  530.         {
  531.  
  532.         }
  533.  
  534.         private void listView1_SelectedIndexChanged(object sender, EventArgs e)
  535.         {
  536.  
  537.         }
  538.  
  539.         private void button2_Click(object sender, EventArgs e)
  540.         {
  541.            
  542.             backgroundWorker1.CancelAsync();
  543.         }
  544.         private void pictureBox5_Click(object sender, EventArgs e)
  545.         {
  546.  
  547.         }
  548.  
  549.         private void panel3_Paint(object sender, PaintEventArgs e)
  550.         {
  551.  
  552.         }
  553.  
  554.         private void pictureBox7_Click(object sender, EventArgs e)
  555.         {
  556.  
  557.         }
  558.  
  559.          private void label13_Click(object sender, EventArgs e)
  560.         {
  561.        
  562.         }
  563.         private void comboBox1_Paint(object sender, EventArgs e)
  564.          {
  565.  
  566.          }
  567.        
  568.        
  569.         private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
  570.         {
  571.             if (comboBox2.Text.ToString() == "iTunes Playlists")
  572.             {
  573.                 comboBox1.Enabled = true;
  574.                 string playlist = comboBox1.SelectedItem.ToString();
  575.                 foreach (IITPlaylist pl in itunes.LibrarySource.Playlists)
  576.                 {
  577.                     if (pl.Name == playlist)
  578.                     {
  579.                         GetTracks(pl);
  580.                         break;
  581.                     }
  582.                 }
  583.             }
  584.         }
  585.  
  586.         private void button4_Click(object sender, EventArgs e)
  587.         {
  588.         //    IITPlaylistCollection selection =
  589.         }
  590.         private void GetTracks(IITPlaylist playlist)
  591.         {
  592.             long totalfilesize = 0;
  593.             dataTable1.Rows.Clear();
  594.             IITTrackCollection tracks = playlist.Tracks;
  595.             int numTracks = tracks.Count;
  596.             for (int currTrackIndex = 1; currTrackIndex <= numTracks; currTrackIndex++)
  597.             {
  598.                 DataRow drnew = dataTable1.NewRow();
  599.                 IITTrack currTrack = tracks[currTrackIndex];
  600.                 if (currTrack.Artist == null) { drnew["artist"] = "missing"; } else { drnew["artist"] = currTrack.Artist; }
  601.                 if (currTrack.Name == null) { drnew["song name"] = "missing"; } else { drnew["song name"] = currTrack.Name; }
  602.                 drnew["album"] = currTrack.Album;
  603.                 drnew["genre"] = currTrack.Genre;
  604.  
  605.  
  606.                 if (currTrack.Kind == ITTrackKind.ITTrackKindFile)
  607.                 {
  608.                     IITFileOrCDTrack file = (IITFileOrCDTrack)currTrack;
  609.                     if (file.Location != null)
  610.                     {
  611.                         FileInfo fi = new FileInfo(file.Location);
  612.                         if (fi.Exists)
  613.                         {
  614.                             drnew["FileLocation"] = file.Location;
  615.                             totalfilesize += fi.Length;
  616.                         }
  617.                         else
  618.                             drnew["FileLocation"] = "not found " + file.Location;
  619.                     }
  620.                 }
  621.  
  622.                 dataTable1.Rows.Add(drnew);
  623.             }
  624.             Control.CheckForIllegalCrossThreadCalls = false;
  625.             lbl_numsongs.Text = dataTable1.Rows.Count.ToString() + " Songs" + ", " + (totalfilesize / 1024.00 / 1024.00).ToString("###,### mb");
  626.         }
  627.  
  628.         private void dataGridView1_Load(object sender, DataGridViewCellEventArgs e)
  629.         {
  630.         }
  631.         private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
  632.         {
  633.  
  634.         }
  635.  
  636.         private void button3_Click(object sender, EventArgs e)
  637.         {
  638.             backgroundWorker8.RunWorkerAsync();
  639.         }
  640.  
  641.         private void label13_Click_1(object sender, EventArgs e)
  642.         {
  643.  
  644.         }
  645.  
  646.         private void backgroundWorker4_DoWork(object sender, DoWorkEventArgs e)
  647.         {
  648.             Control.CheckForIllegalCrossThreadCalls = false;
  649.             backgroundWorker4.WorkerSupportsCancellation = true;
  650.             foreach (IITPlaylist pl in itunes.LibrarySource.Playlists)
  651.             {
  652.                 comboBox1.Items.Add(pl.Name);
  653.             }
  654.         }
  655.         private void backgroundWorker4_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  656.         {
  657.             if (!backgroundWorker5.IsBusy)
  658.             { backgroundWorker5.RunWorkerAsync(); }
  659.         }
  660.         private void backgroundWorker5_DoWork(object sender, DoWorkEventArgs e)
  661.         {
  662.             Control.CheckForIllegalCrossThreadCalls = false;
  663.         }
  664.  
  665.         private void panel1_Paint(object sender, PaintEventArgs e)
  666.         {
  667.  
  668.         }
  669.  
  670.         private void backgroundWorker6_DoWork(object sender, DoWorkEventArgs e)
  671.         {
  672.             IITPlaylist playlist = itunes.LibraryPlaylist;
  673.             {
  674.                 long totalfilesize = 0;
  675.                 dataTable1.Rows.Clear();
  676.                 IITTrackCollection tracks = playlist.Tracks;
  677.                 int numTracks = tracks.Count;
  678.                 for (int currTrackIndex = 1; currTrackIndex <= numTracks; currTrackIndex++)
  679.                 {
  680.                     DataRow drnew = dataTable1.NewRow();
  681.                     IITTrack currTrack = tracks[currTrackIndex];
  682.                     if (currTrack.Artist == null) { drnew["artist"] = "missing"; } else { drnew["artist"] = currTrack.Artist; }
  683.                     if (currTrack.Name == null) { drnew["song name"] = "missing"; } else { drnew["song name"] = currTrack.Name; }
  684.                     drnew["album"] = currTrack.Album;
  685.                     drnew["genre"] = currTrack.Genre;
  686.  
  687.  
  688.                     if (currTrack.Kind == ITTrackKind.ITTrackKindFile)
  689.                     {
  690.                         IITFileOrCDTrack file = (IITFileOrCDTrack)currTrack;
  691.                         if (file.Location != null)
  692.                         {
  693.                             FileInfo fi = new FileInfo(file.Location);
  694.                             if (fi.Exists)
  695.                             {
  696.                                 drnew["FileLocation"] = file.Location;
  697.                                 totalfilesize += fi.Length;
  698.                             }
  699.                             else
  700.                                 drnew["FileLocation"] = "not found " + file.Location;
  701.                         }
  702.                     }
  703.  
  704.                     dataTable1.Rows.Add(drnew);
  705.                 }
  706.                 Control.CheckForIllegalCrossThreadCalls = false;
  707.                 lbl_numsongs.Text = dataTable1.Rows.Count.ToString() + " Songs" + ", " + (totalfilesize / 1024.00 / 1024.00).ToString("###,### mb");
  708.             }
  709.         }
  710.  
  711.         private void label12_Click(object sender, EventArgs e)
  712.         {
  713.  
  714.         }
  715.  
  716.         private void label18_Click(object sender, EventArgs e)
  717.         {
  718.  
  719.         }
  720.  
  721.        
  722.         private void button5_Click(object sender, EventArgs e)
  723.         {
  724.             string playerstate = itunes.PlayerState.ToString();
  725.             if (playerstate == "ITPlayerStatePlaying")
  726.             {
  727.                 itunes.Pause();
  728.                 button5.Image = MusicLibUtility.Properties.Resources.playTrans;
  729.                 //label5.Text = "iTunes is now Paused";
  730.                 playerstate = null;
  731.             }
  732.             else if (playerstate == "ITPlayerStateStopped")
  733.             {
  734.                 itunes.Play();
  735.                 button5.Image = MusicLibUtility.Properties.Resources.pauseTrans;
  736.                 //label5.Text = "iTunes is now Playing";
  737.                 playerstate = null;
  738.             }
  739.  
  740.            
  741.         }
  742.  
  743.         private void label17_Click(object sender, EventArgs e)
  744.         {
  745.             try
  746.             { label17.Text = itunes.CurrentTrack.Name; }
  747.             catch
  748.             {
  749.                 //Nothing Here! this is just so your the app doesn't blow up if iTunes is busy. instead it will just try again in 1 second
  750.             }
  751.         }
  752.  
  753.         private void button8_Click(object sender, EventArgs e)
  754.         {
  755.             itunes.NextTrack();
  756.         }
  757.  
  758.         private void button9_Click(object sender, EventArgs e)
  759.         {
  760.             itunes.BackTrack();
  761.         }
  762.  
  763.         private void label14_Click(object sender, EventArgs e)
  764.         {
  765.  
  766.         }
  767.  
  768.         private void label13_Click_2(object sender, EventArgs e)
  769.         {
  770.  
  771.         }
  772.  
  773.         private void pictureBox12_Click(object sender, EventArgs e)
  774.         {
  775.             try
  776.             {
  777.                 var file = TagLib.File.Create(currfilePath);
  778.                 if (file.Tag.Pictures.Length >= 1)
  779.                 {
  780.                     var bin = (byte[])(file.Tag.Pictures[0].Data.Data);
  781.                     Image artwork = Image.FromStream(new MemoryStream(bin)).GetThumbnailImage(400, 400, null, IntPtr.Zero);
  782.                     string temp = (label4.Text + "\\temp.jpg");
  783.                     artwork.Save(temp);
  784.  
  785.                     Process.Start(@temp);
  786.                 }
  787.             }
  788.             catch { int albumart = 0; }
  789.         }
  790.  
  791.         private void label13_Click_3(object sender, EventArgs e)
  792.         {
  793.             try
  794.             {
  795.                 Process.Start(@currDirPath);
  796.             }
  797.             catch
  798.             { }
  799.         }
  800.  
  801.         private void button10_Click(object sender, EventArgs e)
  802.         {
  803.             try
  804.             {
  805.                 Process.Start(@currDirPath);
  806.             }
  807.             catch
  808.             { }
  809.         }
  810.  
  811.         private void label11_Click(object sender, EventArgs e)
  812.         {
  813.             try
  814.             { Process.Start(label4.Text + "\\badfiles.xml"); }
  815.             catch { }
  816.         }
  817.  
  818.         private void backgroundWorker7_DoWork(object sender, DoWorkEventArgs e)
  819.         {
  820.             do {Thread.Sleep(800);}
  821.             while (backgroundWorker1.IsBusy);
  822.             do { Thread.Sleep(800); }
  823.             while (backgroundWorker2.IsBusy);
  824.             do {
  825.             label12.Text = "Creating Media File Report";
  826.             }
  827.             while(backgroundWorker3.IsBusy);
  828.             label12.Text = "Media Library Database Created";
  829.             pictureBox8.Image = null;
  830.             pictureBox7.Image = Properties.Resources.music16;
  831.             pictureBox1.Image = MusicLibUtility.Properties.Resources.skinitunes;
  832.         }
  833.         private void pictureBox8_Click(object sender, EventArgs e)
  834.         {
  835.  
  836.         }
  837.  
  838.         private void label5_Click_1(object sender, EventArgs e)
  839.         {
  840.  
  841.         }
  842.  
  843.         private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
  844.         {
  845.             {
  846.                 try { dataTable1.Rows.Clear(); }
  847.                 catch { }
  848.                 try { MLTable.Rows.Clear(); }
  849.                 catch { }
  850.                 try { badTable.Rows.Clear(); }
  851.                 catch { }
  852.  
  853.  
  854.                 if (comboBox2.SelectedItem.ToString() == "iTunes Playlists")
  855.                 {
  856.                     comboBox1.Enabled = true;
  857.                     comboBox1.Text = "Playlists";
  858.                     dataGridView1.DataSource = dataTable1;
  859.                 }
  860.                 else if (comboBox2.SelectedItem.ToString() == "Media Library Database")
  861.                 {
  862.                     comboBox1.Enabled = false;
  863.                     comboBox1.Text = "Media Files";
  864.                     lbl_numsongs.Text = null;
  865.                     try
  866.                     {
  867.                         MLDataset.Clear();
  868.                         this.MLDataset.ReadXml(this.label4.Text + "\\DirLib.xml");
  869.                     }
  870.                     catch { }
  871.                     try
  872.                     {
  873.                         DataTable MLTable = MLDataset.Tables[0];
  874.                         int rowcount = MLTable.Rows.Count; ;
  875.                         lbl_numsongs.Text = rowcount.ToString();
  876.                         dataGridView1.DataSource = MLTable;
  877.                     }
  878.                     catch
  879.                     {
  880.                         comboBox1.Text = "Library File Inaccessible";
  881.                         dataTable1.Rows.Clear();
  882.                     }
  883.                     comboBox1.Enabled = false;
  884.                 }
  885.                 else if (comboBox2.SelectedItem.ToString() == "iTunes Database Backup")
  886.                 {
  887.                     comboBox1.Enabled = false;
  888.                     comboBox1.Text = "iTunes Library";
  889.                     lbl_numsongs.Text = null;
  890.                     try
  891.                     {
  892.                         MLDataset.Clear();
  893.                         this.MLDataset.ReadXml(this.label4.Text + "\\ItunesLib.xml");
  894.                     }
  895.                     catch { }
  896.                     try
  897.                     {
  898.                         DataTable MLTable = MLDataset.Tables[0];
  899.                         int rowcount = MLTable.Rows.Count; ;
  900.                         lbl_numsongs.Text = rowcount.ToString();
  901.                         dataGridView1.DataSource = MLTable;
  902.                     }
  903.                     catch
  904.                     {
  905.                         comboBox1.Text = "Library File Inaccessible";
  906.                         dataTable1.Rows.Clear();
  907.                     }
  908.                     comboBox1.Enabled = false;
  909.                 }
  910.  
  911.                 else if (comboBox2.SelectedItem.ToString() == "Bad Media Files")
  912.                 {
  913.                     comboBox1.Enabled = false;
  914.                     lbl_numsongs.Text = null;
  915.                     try
  916.                     {
  917.                         comboBox1.Text = "Corrupt Media Files";
  918.                         BadMediaDataset.Clear();
  919.                         this.BadMediaDataset.ReadXml(this.label4.Text + "\\badfiles.xml");
  920.                     }
  921.                     catch { }
  922.                     try
  923.                     {
  924.                         DataTable badTable = BadMediaDataset.Tables[0];
  925.                         dataGridView1.DataSource = badTable;
  926.                     }
  927.                     catch
  928.                     {
  929.                         comboBox1.Text = "Library File Inaccessible";
  930.                         dataTable1.Rows.Clear();
  931.                     }
  932.                     comboBox1.Enabled = false;
  933.                 }
  934.             }
  935.         }
  936.         private void checkBox1_CheckedChanged(object sender, EventArgs e)
  937.         {
  938.             var checkBox = sender as CheckBox;
  939.             if (checkBox == null)
  940.             {
  941.                 return;
  942.             }
  943.             extensionInfo[".mp3"].IsIncluded = checkBox.Checked;
  944.         }
  945.  
  946.         private void checkBox2_CheckedChanged(object sender, EventArgs e)
  947.         {
  948.             var checkBox = sender as CheckBox;
  949.             if (checkBox == null)
  950.             {
  951.                 return;
  952.             }
  953.             extensionInfo[".mp4"].IsIncluded = checkBox.Checked;
  954.         }
  955.  
  956.         private void checkBox3_CheckedChanged(object sender, EventArgs e)
  957.         {
  958.             var checkBox = sender as CheckBox;
  959.             if (checkBox == null)
  960.             {
  961.                 return;
  962.             }
  963.             extensionInfo[".m4a"].IsIncluded = checkBox.Checked;
  964.         }
  965.  
  966.         private void checkBox4_CheckedChanged(object sender, EventArgs e)
  967.         {
  968.             var checkBox = sender as CheckBox;
  969.             if (checkBox == null)
  970.             {
  971.                 return;
  972.             }
  973.             extensionInfo[".m4v"].IsIncluded = checkBox.Checked;
  974.         }
  975.  
  976.         private void checkBox5_CheckedChanged(object sender, EventArgs e)
  977.         {
  978.             var checkBox = sender as CheckBox;
  979.             if (checkBox == null)
  980.             {
  981.                 return;
  982.             }
  983.             extensionInfo[".m4p"].IsIncluded = checkBox.Checked;
  984.         }
  985.  
  986.         private void checkBox6_CheckedChanged(object sender, EventArgs e)
  987.         {
  988.             var checkBox = sender as CheckBox;
  989.             if (checkBox == null)
  990.             {
  991.                 return;
  992.             }
  993.             extensionInfo[".m4b"].IsIncluded = checkBox.Checked;
  994.         }
  995.  
  996.         private void checkBox7_CheckedChanged(object sender, EventArgs e)
  997.         {
  998.             var checkBox = sender as CheckBox;
  999.             if (checkBox == null)
  1000.             {
  1001.                 return;
  1002.             }
  1003.             extensionInfo[".flac"].IsIncluded = checkBox.Checked;
  1004.         }
  1005.  
  1006.         private void checkBox8_CheckedChanged(object sender, EventArgs e)
  1007.         {
  1008.             var checkBox = sender as CheckBox;
  1009.             if (checkBox == null)
  1010.             {
  1011.                 return;
  1012.             }
  1013.             extensionInfo[".wav"].IsIncluded = checkBox.Checked;
  1014.         }
  1015.  
  1016.         private void panel7_Paint(object sender, PaintEventArgs e)
  1017.         {
  1018.  
  1019.         }
  1020.  
  1021.         private void pictureBox20_Click(object sender, EventArgs e)
  1022.         {
  1023.             Form form2 = new Form2
  1024.             {
  1025.                 Text = "Media Player",
  1026.                 Location = new Point(Right, Top),
  1027.                 StartPosition = FormStartPosition.Manual,
  1028.             };
  1029.             form2.Show();
  1030.         }
  1031.  
  1032.         private void backgroundWorker8_DoWork(object sender, DoWorkEventArgs e)
  1033.         {
  1034.             {
  1035.                 XDocument doc = new XDocument();
  1036.                 XElement songsElement = new XElement("Songs");
  1037.                 IITPlaylist playlist = itunes.LibraryPlaylist;
  1038.                 IITTrackCollection tracks = playlist.Tracks;
  1039.                 int numtracks = tracks.Count;
  1040.                 char[] delimiters = new char[] { ' ', '\\', '/' };
  1041.                 for (int currtrackindex = 1; currtrackindex <= numtracks; currtrackindex++)
  1042.                 {
  1043.                     IITTrack currTrack = tracks[currtrackindex];
  1044.                     string songPath;
  1045.                     try
  1046.                     {
  1047.                         IITFileOrCDTrack file = (IITFileOrCDTrack)currTrack;
  1048.                         songPath = file.Location.ToString();
  1049.                     }
  1050.                     catch { songPath = "Missing"; }
  1051.                     XElement songElement = new XElement("Song");
  1052.                      string songTitle;
  1053.                     try { songTitle = currTrack.Name; }
  1054.                     catch { songTitle = "Missing"; }
  1055.                     int songTNint;
  1056.                     try { songTNint = (currTrack.TrackNumber); }
  1057.                     catch { songTNint = 00; }
  1058.                     string songTN = songTNint.ToString();
  1059.                     string songArtist;
  1060.                     try { songArtist = (currTrack.Artist); }
  1061.                     catch { songArtist = "Missing"; }
  1062.                     string songGenre;
  1063.                     List<string> songGenres = new List<string>();
  1064.                     try
  1065.                     {
  1066.                         string G = currTrack.Genre.ToString();
  1067.                         string[] Genres = G.Split(delimiters);
  1068.                         foreach (string Genre in Genres)
  1069.                         { songGenres.Add(Genre); }
  1070.                     }
  1071.                     catch { songGenre = "Missing"; };
  1072.                     if (songGenres.Count > 1) { songGenre = (songGenres[0] + "/" + songGenres[1]); }
  1073.                     else { try { songGenre = songGenres[0]; } catch { songGenre = "Missing"; } }
  1074.                     //songGenre = string.Join(songGenres.Select(songGenre=>songGenre+"/"));
  1075.                     try
  1076.                     {
  1077.                         songArtist = Regex.Replace(songArtist, @"[^\u0020-\u007E]", string.Empty);
  1078.                     }
  1079.                     catch { };
  1080.                     //songlist.Add("Adding "+songTitle+", Artist "+songArtist+", at path "+songPath+" to DirLib file.");
  1081.                     XElement titleElement = new XElement("Title", songTitle);
  1082.                     XElement tnElement = new XElement("Track", songTN);
  1083.                     XElement pathElement = new XElement("Path", songPath);
  1084.                     XElement artistElement = new XElement("Artist", songArtist);
  1085.                     XElement genreElement = new XElement("Genre", songGenre);
  1086.                     songElement.Add(titleElement);
  1087.                     songElement.Add(tnElement);
  1088.                     songElement.Add(pathElement);
  1089.                     songElement.Add(artistElement);
  1090.                     songElement.Add(genreElement);
  1091.                     songsElement.Add(songElement);
  1092.                 }
  1093.  
  1094.                 //pictureBox5.Image = null;
  1095.                 //pictureBox6.Image = Properties.Resources.music16;
  1096.                 //label11.Text = badfiles.Count + " Corrupt Files Found";
  1097.                 //pictureBox8.Image = MusicLibUtility.Properties.Resources.scanning;
  1098.                 //System.IO.File.WriteAllLines(@libraryPath+"\\WriteLines.txt", songlist);    
  1099.                 doc.Add(songsElement);
  1100.                 doc.Save(label4.Text + "\\ItunesLib.xml");
  1101.             }
  1102.         }
  1103.  
  1104.         private void backgroundWorker9_DoWork(object sender, DoWorkEventArgs e)
  1105.         {
  1106.            
  1107.             do { Thread.Sleep(800); }
  1108.             while (backgroundWorker8.IsBusy);
  1109.         }
  1110.     }
  1111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement