Example #1
0
        private async void SubmitMessageForDelayedPreview()
        {
            if (_messageQueued)
            {
                return;
            }
            _messageQueued = true;
            await Task.Delay(500);

            PreviewCommand.Execute(null);
            _messageQueued = false;
        }
Example #2
0
        static NumberizePatternCommands()
        {
            NumberizePatternCommands.PreviewCommand = new RelayCommand <Object>
                                                      (
                delegate
            {
                try
                {
                    for (Int32 i = 0, j = NumberizePatternViewState.Current.NumberStart; i < NumberizePatternViewState.Current.Files.Count; i++, j++)
                    {
                        NumberizePatternViewState.Current.Files[i].PreviewFileName = String.Format(NumberizePatternViewState.Current.NumberFormat, j);
                    }

                    NumberizePatternViewState.Current.IsError = false;
                }
                catch (ArgumentException argumentEx)
                {
                    ExceptionMessageBox.Show(NumberizePatternViewState.Title, "The following error occurred:", argumentEx);

                    NumberizePatternViewState.Current.IsError = true;
                }
            }
                                                      );

            NumberizePatternCommands.ApplyCommand = new RelayCommand <Object>
                                                    (
                delegate
            {
                PreviewCommand.Execute(null);

                if (NumberizePatternViewState.Current.IsError)
                {
                    return;
                }

                foreach (PatternFileInfo item in NumberizePatternViewState.Current.Files)
                {
                    AppState.Current.Files.First(f => f.OriginalFullPath == item.FileInfo.OriginalFullPath).NewFileName = item.PreviewFileName;
                }

                NumberizePatternViewState.Current.Close();
            }
                                                    );
        }
        static InsertTextPatternCommands()
        {
            InsertTextPatternCommands.PreviewCommand = new RelayCommand <Object>
                                                       (
                delegate
            {
                foreach (PatternFileInfo item in InsertTextPatternViewState.Current.Files)
                {
                    Int32 startIndex = (InsertTextPatternViewState.Current.Position > item.FileInfo.NewFileName.Length - 1 ? item.FileInfo.NewFileName.Length : InsertTextPatternViewState.Current.Position);

                    item.PreviewFileName = item.FileInfo.NewFileName.Insert(startIndex, InsertTextPatternViewState.Current.Text);
                }
            },
                delegate
            {
                return(!String.IsNullOrEmpty(InsertTextPatternViewState.Current?.Text.Trim()));
            }
                                                       );

            InsertTextPatternCommands.ApplyCommand = new RelayCommand <Object>
                                                     (
                delegate
            {
                PreviewCommand.Execute(null);

                foreach (PatternFileInfo item in InsertTextPatternViewState.Current.Files)
                {
                    AppState.Current.Files.First(f => f.OriginalFullPath == item.FileInfo.OriginalFullPath).NewFileName = item.PreviewFileName;
                }

                InsertTextPatternViewState.Current.Close();
            },
                delegate
            {
                return(!String.IsNullOrEmpty(InsertTextPatternViewState.Current?.Text.Trim()));
            }
                                                     );
        }
Example #4
0
        static FindAndReplacePatternCommands()
        {
            FindAndReplacePatternCommands.PreviewCommand = new RelayCommand <Object>
                                                           (
                delegate
            {
                try
                {
                    foreach (PatternFileInfo item in FindAndReplacePatternViewState.Current.Files)
                    {
                        if (!FindAndReplacePatternViewState.Current.UseRegex)
                        {
                            item.PreviewFileName = item.FileInfo.NewFileName.Replace(FindAndReplacePatternViewState.Current.FindWhat,
                                                                                     FindAndReplacePatternViewState.Current.ReplaceWith,
                                                                                     !FindAndReplacePatternViewState.Current.MatchCase);
                        }
                        else
                        {
                            item.PreviewFileName = Regex.Replace(item.FileInfo.NewFileName,
                                                                 FindAndReplacePatternViewState.Current.FindWhat,
                                                                 FindAndReplacePatternViewState.Current.ReplaceWith,
                                                                 !FindAndReplacePatternViewState.Current.MatchCase ? RegexOptions.IgnoreCase : RegexOptions.None);
                        }
                    }

                    FindAndReplacePatternViewState.Current.IsError = false;
                }
                catch (ArgumentException argumentEx)
                {
                    ExceptionMessageBox.Show(FindAndReplacePatternViewState.Title, "The following error occurred:", argumentEx);

                    FindAndReplacePatternViewState.Current.IsError = true;
                }
            },
                delegate
            {
                return(!String.IsNullOrEmpty(FindAndReplacePatternViewState.Current?.FindWhat));
            }
                                                           );

            FindAndReplacePatternCommands.ApplyCommand = new RelayCommand <Object>
                                                         (
                delegate
            {
                PreviewCommand.Execute(null);

                if (FindAndReplacePatternViewState.Current.IsError)
                {
                    return;
                }

                foreach (PatternFileInfo item in FindAndReplacePatternViewState.Current.Files)
                {
                    AppState.Current.Files.First(f => f.OriginalFullPath == item.FileInfo.OriginalFullPath).NewFileName = item.PreviewFileName;
                }

                FindAndReplacePatternViewState.Current.Close();
            },
                delegate
            {
                return(!String.IsNullOrEmpty(FindAndReplacePatternViewState.Current?.FindWhat));
            }
                                                         );
        }
        static WebMatchPatternCommands()
        {
            WebMatchPatternCommands.RetrieveCommand = new RelayCommand <Object>
                                                      (
                delegate
            {
                WebMatchPatternViewState.Current.IsReady = false;

                WebClient webClient = new WebClient()
                {
                    Encoding = Encoding.UTF8
                };

                webClient.DownloadStringCompleted += WebClient_DownloadStringCompleted;

                try
                {
                    Uri uri = new Uri(WebMatchPatternViewState.Current.WebURL);

                    webClient.DownloadStringAsync(uri);
                }
                catch (UriFormatException uriFormatEx)
                {
                    ExceptionMessageBox.Show(WebMatchPatternViewState.Title, "The format of the URL " +
                                             $"{Environment.NewLine}{Environment.NewLine}{WebMatchPatternViewState.Current.WebURL}{Environment.NewLine}{Environment.NewLine}" +
                                             " is not correct.", uriFormatEx);

                    WebMatchPatternViewState.Current.IsReady = true;
                }
                catch (Exception ex)
                {
                    ExceptionMessageBox.Show(WebMatchPatternViewState.Title, "The following error occurred:", ex);

                    WebMatchPatternViewState.Current.IsReady = true;
                }
                finally
                {
                    webClient.Dispose();
                }
            },
                delegate
            {
                return(!String.IsNullOrEmpty(WebMatchPatternViewState.Current?.WebURL.Trim()) &&
                       !String.IsNullOrEmpty(WebMatchPatternViewState.Current?.WebRegex.Trim()));
            }
                                                      );

            WebMatchPatternCommands.OpenCommand = new RelayCommand <Object>
                                                  (
                delegate
            {
                OpenFileDialog dlg = new OpenFileDialog()
                {
                    Filter = "Web Match Files (*.webmatch)|*.webmatch|All Files (*.*)|*.*"
                };

                if (dlg.ShowDialog() == true)
                {
                    Int32 i         = 0;
                    String[] values = new String[5];

                    using (StreamReader streamReader = new StreamReader(dlg.FileName, AdditionalEncodings.UTF8WithoutBOM))
                    {
                        using (XmlReader xmlReader = XmlReader.Create(streamReader))
                        {
                            while (xmlReader.Read())
                            {
                                if (xmlReader.NodeType == XmlNodeType.Text)
                                {
                                    values[i++] = xmlReader.Value;
                                }
                            }
                        }
                    }

                    WebMatchPatternViewState.Current.WebURL   = values[0];
                    WebMatchPatternViewState.Current.WebRegex = values[1];

                    WebMatchPatternViewState.Current.FileNameRegex       = values[2];
                    WebMatchPatternViewState.Current.FileNameReplacement = values[3];
                }
            }
                                                  );

            WebMatchPatternCommands.SaveCommand = new RelayCommand <Object>
                                                  (
                delegate
            {
                SaveFileDialog dlg = new SaveFileDialog()
                {
                    Filter = "Web Match Files (*.webmatch)|*.webmatch|All Files (*.*)|*.*"
                };

                if (dlg.ShowDialog() == true)
                {
                    using (StreamWriter streamWriter = new StreamWriter(dlg.FileName, false, AdditionalEncodings.UTF8WithoutBOM))
                    {
                        XmlWriterSettings xmlWriterSettings = new XmlWriterSettings()
                        {
                            Indent              = true,
                            IndentChars         = "\t",
                            NewLineOnAttributes = true
                        };

                        using (XmlWriter xmlWriter = XmlWriter.Create(streamWriter, xmlWriterSettings))
                        {
                            xmlWriter.WriteStartDocument();
                            xmlWriter.WriteStartElement("BatchRenamer");
                            xmlWriter.WriteStartElement("WebMatch");

                            xmlWriter.WriteStartElement("WebURL");
                            xmlWriter.WriteString(WebMatchPatternViewState.Current.WebURL);
                            xmlWriter.WriteEndElement();
                            xmlWriter.WriteStartElement("WebRegex");
                            xmlWriter.WriteString(WebMatchPatternViewState.Current.WebRegex);
                            xmlWriter.WriteEndElement();
                            xmlWriter.WriteStartElement("FileNameRegex");
                            xmlWriter.WriteString(WebMatchPatternViewState.Current.FileNameRegex);
                            xmlWriter.WriteEndElement();
                            xmlWriter.WriteStartElement("FileNameReplacement");
                            xmlWriter.WriteString(WebMatchPatternViewState.Current.FileNameReplacement);
                            xmlWriter.WriteEndElement();

                            xmlWriter.WriteEndElement();
                            xmlWriter.WriteEndElement();
                            xmlWriter.WriteEndDocument();
                        }
                    }
                }
            },
                delegate
            {
                return(!String.IsNullOrEmpty(WebMatchPatternViewState.Current?.WebURL.Trim()) &&
                       !String.IsNullOrEmpty(WebMatchPatternViewState.Current?.WebRegex.Trim()) &&
                       !String.IsNullOrEmpty(WebMatchPatternViewState.Current?.FileNameRegex.Trim()) &&
                       !String.IsNullOrEmpty(WebMatchPatternViewState.Current?.FileNameReplacement.Trim()));
            }
                                                  );

            WebMatchPatternCommands.PreviewCommand = new RelayCommand <Object>
                                                     (
                delegate
            {
                String matchGroupName = WebMatchPatternViewState.Current.Groups[WebMatchPatternViewState.Current.SelectedGroupIndex];
                Dictionary <String, PatternFileInfo> fileMatches = new Dictionary <String, PatternFileInfo>();

                try
                {
                    foreach (PatternFileInfo item in WebMatchPatternViewState.Current.Files)
                    {
                        Match match = Regex.Match(item.FileInfo.NewFileName, WebMatchPatternViewState.Current.FileNameRegex);

                        foreach (Group group in match.Groups)
                        {
                            if (group.Name == matchGroupName &&
                                !fileMatches.ContainsKey(group.Value))
                            {
                                fileMatches.Add(group.Value, item);
                            }
                        }
                    }
                }
                catch (ArgumentException argumentEx)
                {
                    ExceptionMessageBox.Show(WebMatchPatternViewState.Title, "The format of the expression " +
                                             $"{Environment.NewLine}{Environment.NewLine}{WebMatchPatternViewState.Current.FileNameRegex}{Environment.NewLine}{Environment.NewLine}" +
                                             " is not correct.", argumentEx);

                    WebMatchPatternViewState.Current.IsError = true;
                }

                try
                {
                    if (fileMatches.Count > 0)
                    {
                        foreach (KeyValuePair <String, PatternFileInfo> pair in fileMatches)
                        {
                            WebRegexMatch webMatch = WebMatchPatternViewState.Current.Matches.FirstOrDefault
                                                         (m => (m.Children.FirstOrDefault(g => g.Key == matchGroupName).Value == pair.Key));

                            if (webMatch != null)
                            {
                                pair.Value.PreviewFileName = Regex.Replace(webMatch.Value,
                                                                           WebMatchPatternViewState.Current.LastWebRegex,
                                                                           WebMatchPatternViewState.Current.FileNameReplacement);
                            }
                        }

                        WebMatchPatternViewState.Current.IsError = false;
                    }
                    else
                    {
                        MessageBox.Show($"The source expression does not have matched group name {matchGroupName}.", WebMatchPatternViewState.Title, MessageBoxButton.OK, MessageBoxImage.Warning);

                        WebMatchPatternViewState.Current.IsError = true;
                    }
                }
                catch (ArgumentException argumentEx)
                {
                    ExceptionMessageBox.Show(WebMatchPatternViewState.Title, "The format of the replacement pattern " +
                                             $"{Environment.NewLine}{Environment.NewLine}{WebMatchPatternViewState.Current.FileNameReplacement}{Environment.NewLine}{Environment.NewLine}" +
                                             " is not correct.", argumentEx);

                    WebMatchPatternViewState.Current.IsError = true;
                }
            },
                delegate
            {
                return(!String.IsNullOrEmpty(WebMatchPatternViewState.Current?.FileNameRegex.Trim()) &&
                       !String.IsNullOrEmpty(WebMatchPatternViewState.Current?.FileNameReplacement.Trim()));
            }
                                                     );

            WebMatchPatternCommands.ApplyCommand = new RelayCommand <Object>
                                                   (
                delegate
            {
                PreviewCommand.Execute(null);

                if (WebMatchPatternViewState.Current.IsError)
                {
                    return;
                }

                foreach (PatternFileInfo item in WebMatchPatternViewState.Current.Files)
                {
                    AppState.Current.Files.First(f => f.OriginalFullPath == item.FileInfo.OriginalFullPath).NewFileName = item.PreviewFileName;
                }

                WebMatchPatternViewState.Current.Close();
            },
                delegate
            {
                return(!String.IsNullOrEmpty(WebMatchPatternViewState.Current?.WebURL.Trim()) &&
                       !String.IsNullOrEmpty(WebMatchPatternViewState.Current?.WebRegex.Trim()) &&
                       !String.IsNullOrEmpty(WebMatchPatternViewState.Current?.FileNameRegex.Trim()) &&
                       !String.IsNullOrEmpty(WebMatchPatternViewState.Current?.FileNameReplacement.Trim()));
            }
                                                   );
        }