/// <summary> /// Adds the specified list of files to the list view. /// </summary> /// <param name="files">The list of files.</param> /// <param name="first">if set to <c>true</c> the <c>_parsing</c> variable will be modified accordingly.</param> private void AddFilesInternal(IEnumerable <string> files, bool first = true) { foreach (var file in files) { if (Directory.Exists(file)) { AddFilesInternal(Directory.EnumerateFiles(file, "*.*", SearchOption.AllDirectories), false); } else if (Regex.IsMatch(file, @"\.(avi|mkv|mp4|ts|wmv|srt|sub|ass|smi)$", RegexOptions.IgnoreCase)) { var tmp = file; Dispatcher.Invoke((Action)(() => FilesListViewItemCollection.Add(new FileListViewItem { Location = tmp, ShowCheckBox = "Collapsed", ShowStatusImage = "Visible", StatusImage = "/RSTVShowTracker;component/Images/hourglass.png", Information = new ShowFile(tmp) }))); } } if (first) { SetStatus(); _parsing = false; } }
/// <summary> /// Handles the Click event of the removeSelectedButton control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param> private void RemoveSelectedButtonClick(object sender, RoutedEventArgs e) { foreach (var item in listView.SelectedItems.Cast <FileListViewItem>().ToList()) { FilesListViewItemCollection.Remove(item); } SetStatus(); }
/// <summary> /// Sets the status message to the specified message or to the number of files if the message is null. /// </summary> /// <param name="message">The message.</param> /// <param name="activity">if set to <c>true</c> an animating spinner will be displayed.</param> public void SetStatus(string message = null, bool activity = false) { Dispatcher.Invoke((Action)(() => { var id = FilesListViewItemCollection.Count(f => f.Enabled && f.Checked); statusLabel.Content = message ?? Utils.FormatNumber(FilesListViewItemCollection.Count, "file") + " added; " + Utils.FormatNumber(id, "file") + " identified."; startRenamingButton.IsEnabled = id != 0; if (activity && statusThrobber.Visibility != Visibility.Visible) { statusImage.Visibility = Visibility.Hidden; statusThrobber.Visibility = Visibility.Visible; ((Storyboard)statusThrobber.FindResource("statusThrobberSpinner")).Begin(); } else if (!activity && statusThrobber.Visibility != Visibility.Hidden) { ((Storyboard)statusThrobber.FindResource("statusThrobberSpinner")).Stop(); statusThrobber.Visibility = Visibility.Hidden; statusImage.Visibility = Visibility.Visible; } })); }
/// <summary> /// Renames the recognized files. /// </summary> private void RenameRecognizedFiles() { Log.Info(_operationVerb + " of recognized files started..."); var st = DateTime.Now; var i = 0; foreach (var file in FilesListViewItemCollection.Where(f => f.Enabled && f.Checked).ToList()) { var name = Utils.SanitizeFileName(FileNames.Parser.FormatFileName(Format, file.Information)); SetStatus(_operationVerb + " " + name + "...", true); Log.Info(_operationVerb + " file " + Path.GetFileName(file.Location) + " to " + name + "..."); try { ProcessFile(name, file.Location, Path.Combine(TargetDir, name)); } catch (Exception ex) { Log.Warn("Exception while " + _operationVerb.ToLower() + " file " + Path.GetFileName(file.Location) + " to " + name + ".", ex); } file.Enabled = file.Checked = false; i++; } Dispatcher.Invoke((Action)(() => { startRenamingButton.Content = "Start renaming"; startRenamingButton.IsEnabled = FilesListViewItemCollection.Count(f => f.Enabled && f.Checked) != 0; settingsTabItem.IsEnabled = listView.ContextMenu.IsEnabled = true; })); Log.Info(_operationPast + " " + Utils.FormatNumber(i, "file") + " in " + (DateTime.Now - st).TotalSeconds + "s."); SetStatus(_operationPast + " " + Utils.FormatNumber(i, "file") + "!"); _parsing = _renaming = false; }
/// <summary> /// Handles the Click event of the startRenamingButton control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param> private void StartRenamingButtonClick(object sender, RoutedEventArgs e) { if (_renaming) { if (WorkerThread != null && WorkerThread.IsAlive) { try { WorkerThread.Abort(); } catch { } } startRenamingButton.Content = new StackPanel { Orientation = Orientation.Horizontal, Margin = new Thickness(3, 1, 3, 1) }; (startRenamingButton.Content as StackPanel).Children.Add(new Image { Source = new BitmapImage(new Uri("/RSTVShowTracker;component/Images/pencil.png", UriKind.Relative)), Width = 16, Height = 16, Margin = new Thickness(0, 0, 5, 0), }); (startRenamingButton.Content as StackPanel).Children.Add(new TextBlock { Text = "Start renaming", Margin = new Thickness(0, 0, 3, 0), }); startRenamingButton.IsEnabled = FilesListViewItemCollection.Count(f => f.Enabled && f.Checked) != 0; settingsTabItem.IsEnabled = listView.ContextMenu.IsEnabled = true; _parsing = _renaming = false; Log.Info("Canceled " + _operationVerb.ToLower() + " operation."); SetStatus("Canceled " + _operationVerb.ToLower() + " operation."); } else { startRenamingButton.Content = new StackPanel { Orientation = Orientation.Horizontal, Margin = new Thickness(3, 1, 3, 1) }; (startRenamingButton.Content as StackPanel).Children.Add(new Image { Source = new BitmapImage(new Uri("/RSTVShowTracker;component/Images/cross.png", UriKind.Relative)), Width = 16, Height = 16, Margin = new Thickness(0, 0, 5, 0), }); (startRenamingButton.Content as StackPanel).Children.Add(new TextBlock { Text = "Stop renaming", Margin = new Thickness(0, 0, 3, 0), }); settingsTabItem.IsEnabled = listView.ContextMenu.IsEnabled = false; _parsing = _renaming = true; if (WorkerThread != null && WorkerThread.IsAlive) { try { WorkerThread.Abort(); } catch { } } WorkerThread = new Thread(RenameRecognizedFiles); WorkerThread.Start(); } }
/// <summary> /// Handles the Elapsed event of the ParserTimer control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.Timers.ElapsedEventArgs"/> instance containing the event data.</param> private void ParserTimerElapsed(object sender, ElapsedEventArgs e) { if (_parsing) { return; } _parsing = true; var files = FilesListViewItemCollection.Where(f => !f.Processed).ToList(); if (files.Count == 0) { goto end; } Log.Info("Starting to identify files in renamer queue..."); var st = DateTime.Now; foreach (var file in files) { SetStatus("Identifying " + file.Information.Name + "...", true); Log.Debug("Identifying file " + file.Information.Name + "..."); try { file.Information = FileNames.Parser.ParseFile(file.Information.Name, Path.GetDirectoryName(file.Location).Split(new[] { Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries)); file.Checked = file.Recognized = file.Enabled = file.Information.Success; file.Processed = true; if (file.Information.Success) { file.ShowStatusImage = "Collapsed"; file.ShowCheckBox = "Visible"; Log.Debug("Identified file " + file.Information.Name + " as " + file.Information + "."); } else { file.StatusImage = "/RSTVShowTracker;component/Images/exclamation-red.png"; Log.Debug("Unable to identify file " + file.Information.Name + ": " + file.Information + "."); } } catch (Exception ex) { file.Information.ParseError = ShowFile.FailureReasons.ExceptionOccurred; file.Checked = file.Recognized = file.Enabled = false; file.Processed = true; file.StatusImage = "/RSTVShowTracker;component/Images/exclamation-red.png"; Log.Warn("Exception while identifying file " + file.Information.Name + ".", ex); } file.RefreshEnabled(); } Log.Info("File identification queue finished in " + (DateTime.Now - st).TotalSeconds + "s."); SetStatus(); end: _parsing = false; }