private void AddToiTunes()
{
var newdir = String.Format("{0}\\iTunes\\iTunes Media\\Automatically Add to iTunes\\{1}.{2}",
Settings.CustomITunesLocation ?? Environment.GetFolderPath(Environment.SpecialFolder.MyMusic), GetCleanFileName(_title),
MainResource.AbsolutePath.Substring(MainResource.AbsolutePath.Length-3));
if (File.Exists(newdir))
{
// Insert random string into dest filename as to not overwrite old file
var endingIndex = (newdir.Length - 1) - 4;
var ending = newdir.Substring(endingIndex);
newdir = newdir.Remove(endingIndex) + GenerateRandomString(3) + ending;
}
try
{
switch (Settings.TunesTransfer)
{
case Settings.TunesSetting.Move:
{
BugSenseHandler.Instance.LeaveBreadCrumb("Moving song to iTunes");
File.Move(MainResource.AbsolutePath, newdir);
// Delete the artist folder if empty
if (Settings.AuthorFolder && MainResource.AbsolutePath.StartsWith(Settings.DownloadFolder + _author)
&& !Directory.EnumerateFileSystemEntries(Settings.DownloadFolder + _author).Any())
{
Directory.Delete(Settings.DownloadFolder + _author);
}
break;
}
case Settings.TunesSetting.Copy:
BugSenseHandler.Instance.LeaveBreadCrumb("Copying song to iTunes");
File.Copy(MainResource.AbsolutePath, newdir);
break;
}
}
catch (DirectoryNotFoundException)
{
// Find iTunes location if it exists
var dialog = new YesNoDialog(Resources.ErrorCantFindITunes, "Locate", "Disable")
{
ResponseCallback = result =>
{
if (result)
{
// User would like to find installation on disk
var folderBrowser = new FolderBrowserDialog
{
Description = Resources.FolderBrowserDescriptionFindITunes
};
folderBrowser.ShowDialog();
// TODO: Better validate if this is a correct iTunes directory
if (folderBrowser.SelectedPath.EndsWith("iTunes"))
{
// Valid iTunes installation
Settings.CustomITunesLocation =
folderBrowser.SelectedPath;
}
} else
{
// User wants to disable iTunes functionality
Settings.TunesTransfer =
Settings.TunesSetting.Nothing;
}
}
};
dialog.Show();
}
}