private void OKCancelBox_OKClicked(object sender, EventArgs e)
        {
            this.release.DateAudioModified = DateTime.Now;

            IEncoderFactory encoderFactory;

            if (this.CollectionManager.Settings.NetworkEncoding)
            {
                encoderFactory = new RemoteFlacEncoderFactory(this.networkBox.Servers, 8, this.SettingsManager.Settings.ActualLocalConcurrencyLevel, true, true);
            }
            else
            {
                encoderFactory = new NativeFlacEncoderFactory(8, this.SettingsManager.Settings.ActualLocalConcurrencyLevel, true, true);
            }

            IEncoderFactory replayGainFactory = new DspEncoderFactory(this.SettingsManager.Settings.ActualLocalConcurrencyLevel, true, true);


            ReplayGainTask rgTask = new ReplayGainTask(replayGainFactory, this.release, true, true, true);

            List <IParallelTask> tasks = new List <IParallelTask>();

            for (int i = 0; i < this.items.Count; ++i)
            {
                Track             track            = this.release.Tracklist[i];
                IImportSourceItem importSourceItem = this.items[i];

                track.RelativeFilename = FilenameGenerator.PatternToFilename(
                    this.CollectionManager.Settings.FileNamingPattern,
                    this.release, track) + ".flac";

                AudioFileTag tag = new AudioFileTag(this.release, track);

                string filename = Path.Combine(this.CollectionManager.Settings.MusicDirectory, track.RelativeFilename);

                var task = new FileEncodeTask(
                    encoderFactory,
                    () => importSourceItem.GetAudioSource(),
                    filename,
                    tag
                    );
                tasks.Add(task);
                rgTask.AddItem(track, task);
            }

            tasks.Add(rgTask);

            int concurrency = Math.Max(encoderFactory.ThreadCount, replayGainFactory.ThreadCount);
            EncoderController controller = new EncoderController(tasks.ToArray(), concurrency);
            EncodingWindow    window     = new EncodingWindow(controller);

            if (window.ShowDialog(this) == true)
            {
                this.release.UpdateDynamicProperties();
                this.CollectionManager.Save(this.release);

                this.Close();
            }

            CollectionManagerGlobal.OnCollectionChanged();
        }
        private void btnSync_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            bool successfullyDeletedFiles = true;

            foreach (string fileToDelete in this.scanResult.FilesToDelete)
            {
                successfullyDeletedFiles = Utility.TryDeleteFile(fileToDelete) && successfullyDeletedFiles;
                successfullyDeletedFiles = Utility.TryDeleteEmptyFoldersToTheRoot(Path.GetDirectoryName(fileToDelete)) && successfullyDeletedFiles;
            }
            if (!successfullyDeletedFiles)
            {
                Dialogs.Inform("Some files or folders were not successfully deleted. Please unlock them and rerun the sync.");
            }

            if (this.scanResult.ReleasesToEncode.Count > 0)
            {
                int localConcurrencyLevel = this.SettingsManager.Settings.ActualLocalConcurrencyLevel;

                IEncoderFactory encoderFactory;
                if (this.CollectionManager.Settings.NetworkEncoding)
                {
                    encoderFactory = new RemoteMp3EncoderFactory(this.networkBox.Servers, this.encodingTarget.Mp3Settings.VbrQuality, localConcurrencyLevel, false, false);
                }
                else
                {
                    encoderFactory = new LocalMp3EncoderFactory(this.encodingTarget.Mp3Settings.VbrQuality, localConcurrencyLevel, false, false);
                }

                IEncoderFactory replayGainFactory = new DspEncoderFactory(this.SettingsManager.Settings.ActualLocalConcurrencyLevel, false, false);

                List <IParallelTask> tasks = new List <IParallelTask>();
                foreach (var release in this.scanResult.ReleasesToEncode)
                {
                    var rgTask = new ReplayGainTask(replayGainFactory, release, false, false, false);

                    if (!release.Tracklist.All(t => AudioHelper.IsSupportedAudioSource(t.RelativeFilename)))
                    {
                        continue;
                    }

                    foreach (Track track in release.Tracklist)
                    {
                        string sourceFilename = Path.Combine(this.CollectionManager.Settings.MusicDirectory, track.RelativeFilename);

                        string targetRelativeFilename = FilenameGenerator.PatternToFilename(this.encodingTarget.FileNamingPattern, release, track) + this.encodingTarget.Extension;
                        string targetFilename         = Path.Combine(this.encodingTarget.TargetDirectory, targetRelativeFilename);

                        AudioFileTag tag = new AudioFileTag(release, track);

                        FileEncodeTask encodeTask = new FileEncodeTask(
                            encoderFactory,
                            () => AudioHelper.GetAudioSourceForFile(sourceFilename),
                            targetFilename,
                            tag
                            );

                        encodeTask.ProgressChanged += (_sender, _e) =>
                        {
                            if (encodeTask.Status == EncodeTaskStatus.Completed)
                            {
                                FileInfo originalFileInfo = new FileInfo(sourceFilename);
                                File.SetCreationTime(targetFilename, originalFileInfo.CreationTime);
                                File.SetLastWriteTime(targetFilename, originalFileInfo.LastWriteTime);
                                File.SetLastAccessTime(targetFilename, originalFileInfo.LastAccessTime);
                            }
                        };

                        tasks.Add(encodeTask);
                        rgTask.AddItem(track, encodeTask);
                    }

                    tasks.Add(rgTask);
                }

                int concurrency = Math.Max(encoderFactory.ThreadCount, replayGainFactory.ThreadCount);
                EncoderController controller = new EncoderController(tasks.ToArray(), concurrency);
                controller.DeleteSuccessfullyEncodedItemsIfFailure = false;
                EncodingWindow window = new EncodingWindow(controller);
                window.ShowDialog(this);
            }

            this.Close();
        }
        private void OKCancelBox_OKClicked(object sender, System.EventArgs e)
        {
            this.UpdateRelease();

            IEncoderFactory encoderFactory;

            if (this.CollectionManager.Settings.NetworkEncoding)
            {
                encoderFactory = new RemoteFlacEncoderFactory(this.networkBox.Servers, 8, this.SettingsManager.Settings.ActualLocalConcurrencyLevel, true, true);
            }
            else
            {
                encoderFactory = new NativeFlacEncoderFactory(8, this.SettingsManager.Settings.ActualLocalConcurrencyLevel, true, true);
            }

            IEncoderFactory replayGainFactory = new DspEncoderFactory(this.SettingsManager.Settings.ActualLocalConcurrencyLevel, true, true);

            ReplayGainTask rgTask = new ReplayGainTask(replayGainFactory, this.DatabaseRelease, true, true, true);

            List <IParallelTask> tasks = new List <IParallelTask>();

            foreach (Track track in this.DatabaseRelease.Tracklist)
            {
                track.RelativeFilename = FilenameGenerator.PatternToFilename(
                    this.CollectionManager.Settings.FileNamingPattern,
                    this.DatabaseRelease, track) + ".flac";

                AudioFileTag      tag        = new AudioFileTag(this.DatabaseRelease, track);
                IImportSourceItem sourceItem = this.Release.Discs[track.Disc - 1][track.Position - 1].SourceItem;

                string filename = Path.Combine(this.CollectionManager.Settings.MusicDirectory, track.RelativeFilename);

                var task = new FileEncodeTask(
                    encoderFactory,
                    () => sourceItem.GetAudioSource(),
                    filename,
                    tag
                    );
                tasks.Add(task);
                rgTask.AddItem(track, task);
            }

            tasks.Add(rgTask);

            int concurrency = Math.Max(encoderFactory.ThreadCount, replayGainFactory.ThreadCount);
            EncoderController controller = new EncoderController(tasks.ToArray(), concurrency);
            EncodingWindow    window     = new EncodingWindow(controller);

            if (window.ShowDialog(this) == true)
            {
                this.DatabaseRelease.UpdateDynamicProperties();

                this.CollectionManager.Save(this.DatabaseRelease);
                this.InsertedReleaseId = this.DatabaseRelease.Id;

                this.imagesEditor.WriteFiles();


                CollectionManagerGlobal.OnCollectionChanged();

                this.completed    = true;
                this.DialogResult = true;
            }
        }