private static ConversionItem ParseConversionItem(XElement element)
        {
            ConversionData oldData = new ConversionData();
            ConversionData newData = new ConversionData();

            foreach (XElement child in element.Elements())
            {
                switch (child.Name.LocalName)
                {
                case "Path":
                    newData.FilePath = child.Value;
                    break;

                case "Data":
                    foreach (XElement dataChild in child.Elements())
                    {
                        switch (dataChild.Name.LocalName)
                        {
                        case "Path":
                            oldData.FilePath = dataChild.Value;
                            break;

                        case "Title":
                            oldData.Title = dataChild.Value;
                            break;

                        case "Album":
                            oldData.Album = dataChild.Value;
                            break;

                        case "Artists": {
                            string[] artists = new string[dataChild.Elements().Count()];
                            int      index   = 0;
                            foreach (XElement artistChild in dataChild.Elements())
                            {
                                artists[index++] = artistChild.Value;
                            }
                            oldData.Performers = artists;
                            break;
                        }

                        case "AlbumArtists": {
                            string[] artists = new string[dataChild.Elements().Count()];
                            int      index   = 0;
                            foreach (XElement artistChild in dataChild.Elements())
                            {
                                artists[index++] = artistChild.Value;
                            }
                            oldData.AlbumArtists = artists;
                            break;
                        }
                        }
                    }
                    break;
                }
            }

            return(new ConversionItem(oldData, newData));
        }
 private void WriteConversionData(XmlWriter writer, ConversionData data)
 {
     writer.WriteElementString("Path", data.FilePath);
     writer.WriteElementString("Title", data.Title);
     writer.WriteElementString("Album", data.Album);
     writer.WriteStartElement("Artists");
     foreach (string s in data.Performers)
     {
         writer.WriteElementString("Artist", s);
     }
     writer.WriteEndElement();
     writer.WriteStartElement("AlbumAritsts");
     foreach (string s in data.AlbumArtists)
     {
         writer.WriteElementString("Artist", s);
     }
     writer.WriteEndElement();
 }
 public ConversionItem(ConversionData oldData, ConversionData newData)
 {
     OldData = oldData;
     NewData = newData;
 }
Example #4
0
        public void Convert(IEnumerable <string> files, CancellationToken ct)
        {
            // Convert each file
            foreach (string filePath in files)
            {
                // Check if function has been cancelled if called asynchronously
                if (ct != CancellationToken.None)
                {
                    ct.ThrowIfCancellationRequested();
                }

                if (!File.Exists(filePath))
                {
                    ConversionData nonExistentData = new ConversionData(filePath);
                    ConversionItem item            = new ConversionItem(nonExistentData, null);
                    OnProgressEvent(ProgressEvent.FileDoesNotExist, item);
                    continue;
                }

                // Get file details
                string directoryPath = Path.GetDirectoryName(filePath);
                string extension     = Path.GetExtension(filePath);
                string fileName      = Path.GetFileNameWithoutExtension(filePath);

                // Get tags
                TagLib.File tagFile = null;
                try {
                    tagFile = TagLib.File.Create(filePath);
                } catch (Exception) {
                    // ignored
                }
                string   title        = tagFile?.Tag.Title ?? "";
                string   album        = tagFile?.Tag.Album ?? "";
                string[] performers   = tagFile?.Tag.Performers ?? new string[] {};
                string[] albumArtists = tagFile?.Tag.AlbumArtists ?? new string[] {};

                // Store old conversion data
                ConversionData oldData = new ConversionData(filePath,
                                                            title,
                                                            album,
                                                            // Ensure values remain the same even if array is modified
                                                            (string[])performers.Clone(),
                                                            (string[])albumArtists.Clone());

                // Check if function has been cancelled if called asynchronously
                if (ct != CancellationToken.None)
                {
                    ct.ThrowIfCancellationRequested();
                }

                // Translate
                string newFileName = TextTranslator.Translate(fileName);
                title = TextTranslator.Translate(title);
                album = TextTranslator.Translate(album);

                for (int i = 0; i < performers.Length; i++)
                {
                    performers[i] = TextTranslator.Translate(performers[i]);
                }
                for (int i = 0; i < albumArtists.Length; i++)
                {
                    albumArtists[i] = TextTranslator.Translate(albumArtists[i]);
                }

                // Check if function has been cancelled if called asynchronously
                if (ct != CancellationToken.None)
                {
                    ct.ThrowIfCancellationRequested();
                }

                // Replace illegal filename characters from the new filename
                foreach (string s in IllegalFilenameMap.Keys)
                {
                    string sVal;
                    if (IllegalFilenameMap.TryGetValue(s, out sVal))
                    {
                        newFileName = newFileName.Replace(s, sVal);
                    }
                }

                string newFilePath = directoryPath + Path.DirectorySeparatorChar + newFileName + extension;
                if (File.Exists(newFilePath))
                {
                    ConversionData existingData = new ConversionData(newFilePath);
                    ConversionItem item         = new ConversionItem(oldData, existingData);
                    OnProgressEvent(ProgressEvent.FileAlreadyExists, item);
                    continue;
                }

                // Set new tags
                if (tagFile != null)
                {
                    tagFile.Tag.Title        = title;
                    tagFile.Tag.Album        = album;
                    tagFile.Tag.Performers   = performers;
                    tagFile.Tag.AlbumArtists = albumArtists;
                    tagFile.Save();
                }

                File.Move(filePath, newFilePath);

                // Store new conversion data
                ConversionData newData = new ConversionData(newFilePath,
                                                            title,
                                                            album,
                                                            // Ensure values remain the same even if array is modified
                                                            (string[])performers.Clone(),
                                                            (string[])albumArtists.Clone());

                // Check if function has been cancelled if called asynchronously
                if (ct != CancellationToken.None)
                {
                    ct.ThrowIfCancellationRequested();
                }

                // Update progress
                ConversionItem conversionItem = new ConversionItem(oldData, newData);
                OnProgressEvent(ProgressEvent.Converted, conversionItem);
            }
            OnProgressEvent(ProgressEvent.Completed);
        }