[gnome-subtitles] Replace All now replaces translations too, besides the normal subtitle text



commit 1be2665cd4445278e93efb870b4eed57f9ef6d0c
Author: Pedro Castro <mail pedrocastro org>
Date:   Sun Jun 7 19:42:54 2009 +0100

    Replace All now replaces translations too, besides the normal subtitle text
---
 gnome-subtitles.mdp                                |    1 +
 src/GnomeSubtitles/Core/Command/CommandTarget.cs   |    7 ++-
 .../Core/Command/ReplaceAllCommand.cs              |   75 +++++++++++++++-----
 src/GnomeSubtitles/Core/Document.cs                |    5 ++
 src/SubLib/Core/Search/SearchOperator.cs           |   62 ++++++++++-------
 src/SubLib/Core/Search/SubtitleReplaceResult.cs    |   52 ++++++++++++++
 6 files changed, 158 insertions(+), 44 deletions(-)

diff --git a/gnome-subtitles.mdp b/gnome-subtitles.mdp
index 52950df..f35f6e9 100644
--- a/gnome-subtitles.mdp
+++ b/gnome-subtitles.mdp
@@ -242,6 +242,7 @@
     <File name="src/SubLib/IO/SubtitleFormats/SubtitleFormatSubViewer1.cs" subtype="Code" buildaction="Compile" />
     <File name="src/SubLib/IO/SubtitleFormats/SubtitleFormatSubViewer2.cs" subtype="Code" buildaction="Compile" />
     <File name="src/SubLib/IO/SubtitleFormats/SubtitleFormatViPlaySubtitleFile.cs" subtype="Code" buildaction="Compile" />
+    <File name="src/SubLib/Core/Search/SubtitleReplaceResult.cs" subtype="Code" buildaction="Compile" />
   </Contents>
   <References>
     <ProjectReference type="Gac" localcopy="True" refto="Mono.Posix, Version=2.0.0.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756" />
diff --git a/src/GnomeSubtitles/Core/Command/CommandTarget.cs b/src/GnomeSubtitles/Core/Command/CommandTarget.cs
index 61ab0b0..5aae362 100644
--- a/src/GnomeSubtitles/Core/Command/CommandTarget.cs
+++ b/src/GnomeSubtitles/Core/Command/CommandTarget.cs
@@ -1,6 +1,6 @@
 /*
  * This file is part of Gnome Subtitles.
- * Copyright (C) 2007-2008 Pedro Castro
+ * Copyright (C) 2007-2009 Pedro Castro
  *
  * Gnome Subtitles is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -28,7 +28,10 @@ public enum CommandTarget {
 	Normal,
 	
 	/// <summary>The command target is the translation.</summary>
-	Translation
+	Translation,
+
+	/// <summary>The command target is both the normal document and the translation.</summary>
+	NormalAndTranslation
 }
 
 }
diff --git a/src/GnomeSubtitles/Core/Command/ReplaceAllCommand.cs b/src/GnomeSubtitles/Core/Command/ReplaceAllCommand.cs
index 524ecf2..abf6b27 100644
--- a/src/GnomeSubtitles/Core/Command/ReplaceAllCommand.cs
+++ b/src/GnomeSubtitles/Core/Command/ReplaceAllCommand.cs
@@ -23,16 +23,15 @@ using Mono.Unix;
 using SubLib.Core.Domain;
 using SubLib.Core.Search;
 using System;
+using System.Collections;
 using System.Text.RegularExpressions;
 
 namespace GnomeSubtitles.Core.Command {
 
 public class ReplaceAllCommand : MultipleSelectionCommand {
 	private static string description = Catalog.GetString("Replacing All");
-	
-	private int[] subtitles = null;
-	private string[] texts  = null;
-	
+
+	private ArrayList replacedSubtitles = null;
 	private Regex regex = null;
 	private string replacement = String.Empty;
 
@@ -44,38 +43,80 @@ public class ReplaceAllCommand : MultipleSelectionCommand {
 
 	public override bool Execute () {
 		SearchOperator searchOp = new SearchOperator(Base.Document.Subtitles);
-		int count = searchOp.ReplaceAll(regex, replacement, out subtitles, out texts);
-		if (count == 0)
+		replacedSubtitles = searchOp.ReplaceAll(regex, replacement);
+		if (replacedSubtitles.Count  == 0)
 			return false;
-		
-		Paths = Util.IntsToPaths(subtitles);
+
+		TreePath[] paths = null;
+		CommandTarget target = CommandTarget.Normal;
+		GetCommandData(out paths, out target);
+
+		SetCommandTarget(target);
+		Paths = paths;
 		if (Paths.Length > 0)
 			Focus = Paths[0];
 		
 		Base.Ui.View.Selection.Select(Paths, Focus, true);
 		return true;
 	}
+
 	
 	public override void Undo () {
-		for (int position = 0 ; position < subtitles.Length ; position++) {
-			int index = subtitles[position];
-			Subtitle subtitle = Base.Document.Subtitles[index];
-			string oldText = subtitle.Text.Get();
-			string newText = texts[position];
-			subtitle.Text.Set(newText);
-			texts[position] = oldText;
+		ArrayList newReplacedSubtitles = new ArrayList();
+
+		/* Get values before replacing */
+		foreach (SubtitleReplaceResult replacedSubtitle in replacedSubtitles) {
+			Subtitle subtitle = Base.Document.Subtitles[replacedSubtitle.Number];
+			string oldText = (replacedSubtitle.Text != null ? subtitle.Text.Get() : null);
+			string oldTranslation = (replacedSubtitle.Translation != null ? subtitle.Translation.Get() : null);
+			newReplacedSubtitles.Add(new SubtitleReplaceResult(replacedSubtitle.Number, oldText, oldTranslation));
 		}
-	
+
+		/* Replace the values */
+		foreach (SubtitleReplaceResult replacedSubtitle in replacedSubtitles) {
+			Subtitle subtitle = Base.Document.Subtitles[replacedSubtitle.Number];
+
+			if (replacedSubtitle.Text != null)
+				subtitle.Text.Set(replacedSubtitle.Text);
+
+			if (replacedSubtitle.Translation != null)
+				subtitle.Translation.Set(replacedSubtitle.Translation);
+		}
+
+		replacedSubtitles = newReplacedSubtitles;
 		Base.Ui.View.Selection.Select(Paths, Focus, true);
 	}
 	
 	public override void Redo () {
 		Undo();
 	}
-	
+
+	/* Private members */
 
 
+	private void GetCommandData (out TreePath[] paths, out CommandTarget target) {
+		ArrayList foundPaths = new ArrayList();		
+		bool foundText = false;
+		bool foundTranslation = false;
+		
+		foreach (SubtitleReplaceResult replacedSubtitle in replacedSubtitles) {
+			foundPaths.Add(Util.IntToPath(replacedSubtitle.Number));
+
+			if ((!foundText) && (replacedSubtitle.Text != null))
+				foundText = true;
 
+			if ((!foundTranslation) && (replacedSubtitle.Translation != null))
+				foundTranslation = true;
+		}
+		paths = foundPaths.ToArray(typeof(TreePath)) as TreePath[];
+
+		if (foundText && foundTranslation)
+			target = CommandTarget.NormalAndTranslation;
+		else if (foundText)
+			target = CommandTarget.Normal;
+		else
+			target = CommandTarget.Translation;
+	}
 
 }
 
diff --git a/src/GnomeSubtitles/Core/Document.cs b/src/GnomeSubtitles/Core/Document.cs
index 178475c..6fe1595 100644
--- a/src/GnomeSubtitles/Core/Document.cs
+++ b/src/GnomeSubtitles/Core/Document.cs
@@ -240,6 +240,11 @@ public class Document {
 			wasTranslationModified = true;
 			EmitModificationStatusChangedEvent(true);
 		}
+		else if ((args.Target == CommandTarget.NormalAndTranslation) && ((!wasTextModified) || (!wasTranslationModified))) {
+			wasTextModified = true;			
+			wasTranslationModified = true;
+			EmitModificationStatusChangedEvent(true);
+		}
     }
     	
 	private void EmitModificationStatusChangedEvent (bool modified) {
diff --git a/src/SubLib/Core/Search/SearchOperator.cs b/src/SubLib/Core/Search/SearchOperator.cs
index ad36110..b312b8b 100644
--- a/src/SubLib/Core/Search/SearchOperator.cs
+++ b/src/SubLib/Core/Search/SearchOperator.cs
@@ -44,47 +44,53 @@ public class SearchOperator {
 		else
 			return FindForward(options);	
 	}
-	
+
 	/// <summary>Replaces all occurences of some text with the specified replacement.</summary>
 	/// <param name="regex">A regular expression used to find the text to be replaced.</param>
 	/// <param name="replacement">The text that will be used as a replacement.</param>
-	/// <param name="replacedSubtitles">The numbers of the subtitles that were replaced.</param>
-	/// <param name="oldTexts">The texts of the subtitles that were replaced, before replacement was done.</param>
+	/// <param name="lineBreak">The line break to use between multiple lines of text in each subtitle.</param>
 	/// <remarks>The newline (\n) char is used as the line break.</remarks>
-	/// <returns>The number of replaced subtitles.</returns>
-	public int ReplaceAll (Regex regex, string replacement, out int[] replacedSubtitles, out string[] oldTexts) {
-		return ReplaceAll(regex, replacement, "\n", out replacedSubtitles, out oldTexts);
+	/// <returns>The previous text of the replaced subtitles.</returns>
+	public ArrayList ReplaceAll (Regex regex, string replacement) {
+		return ReplaceAll(regex, replacement, "\n");
 	}
 
 	/// <summary>Replaces all occurences of some text with the specified replacement.</summary>
 	/// <param name="regex">A regular expression used to find the text to be replaced.</param>
 	/// <param name="replacement">The text that will be used as a replacement.</param>
 	/// <param name="lineBreak">The line break to use between multiple lines of text in each subtitle.</param>
-	/// <param name="replacedSubtitles">The numbers of the subtitles that were replaced.</param>
-	/// <param name="oldTexts">The texts of the subtitles that were replaced, before replacement was done.</param>
-	/// <remarks>The newline (\n) char is used as the line break.</remarks>
-	/// <returns>The number of replaced subtitles.</returns>
-	public int ReplaceAll (Regex regex, string replacement, string lineBreak, out int[] replacedSubtitles, out string[] oldTexts) {
+	/// <returns>The previous text of the replaced subtitles.</returns>
+	public ArrayList ReplaceAll (Regex regex, string replacement, string lineBreak) {
 		ArrayList replaced = new ArrayList();
-		ArrayList texts = new ArrayList();
 		MatchEvaluationCounter counter = new MatchEvaluationCounter(replacement);
-		int subtitleNumber = 0;
-		foreach (Subtitle subtitle in subtitles.Collection) {
+
+		for (int subtitleNumber = 0 ; subtitleNumber < subtitles.Collection.Count ; subtitleNumber++) {
+			Subtitle subtitle = subtitles.Collection[subtitleNumber];
+
+			/* Handle text */
 			string oldText = subtitle.Text.Get(lineBreak);
-			counter.EvaluationOccured = false;
-			string newText = regex.Replace(oldText, counter.Evaluator);
-			if (counter.EvaluationOccured) {
-				replaced.Add(subtitleNumber);
-				texts.Add(oldText);
-				subtitle.Text.Set(newText, lineBreak, false);				
-				counter.EvaluationOccured = false;			
+			string newText = ReplaceText(oldText, regex, replacement, counter);
+			string textToStore = null;
+			if (newText != null) {
+				subtitle.Text.Set(newText, lineBreak, false);
+				textToStore = oldText;
+			}
+
+			/* Handle translation */
+			string oldTranslation = subtitle.Translation.Get(lineBreak);
+			string newTranslation = ReplaceText(oldTranslation, regex, replacement, counter);
+			string translationToStore = null;
+			if (newTranslation != null) {
+				subtitle.Translation.Set(newTranslation, lineBreak, false);
+				translationToStore = oldTranslation;
+			}
+
+			if ((textToStore != null) || (translationToStore != null)) {
+				replaced.Add(new SubtitleReplaceResult(subtitleNumber, textToStore, translationToStore));
 			}
-			subtitleNumber++;
 		}
 
-		replacedSubtitles = (int[])replaced.ToArray(typeof(int));
-		oldTexts = (string[])texts.ToArray(typeof(string));
-		return counter.Count;	
+		return replaced;
 	}
 	
 	/// <summary>Finds the subtitle that contains the specified time position.</summary>
@@ -303,6 +309,12 @@ public class SearchOperator {
 		else
 			return null;
 	}
+
+	private string ReplaceText (string text, Regex regex, string replacement, MatchEvaluationCounter counter) {
+		counter.EvaluationOccured = false;
+		string newText = regex.Replace(text, counter.Evaluator);
+		return (counter.EvaluationOccured ? newText : null);
+	}
 	
 	
 }
diff --git a/src/SubLib/Core/Search/SubtitleReplaceResult.cs b/src/SubLib/Core/Search/SubtitleReplaceResult.cs
new file mode 100644
index 0000000..b91366c
--- /dev/null
+++ b/src/SubLib/Core/Search/SubtitleReplaceResult.cs
@@ -0,0 +1,52 @@
+/*
+ * This file is part of SubLib.
+ * Copyright (C) 2009 Pedro Castro
+ *
+ * SubLib is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * SubLib is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+using SubLib.Core.Domain;
+using System;
+
+namespace SubLib.Core.Search {
+
+public class SubtitleReplaceResult {
+	private int number = -1;
+	private String text = String.Empty;
+	private String translation = String.Empty;
+
+	public SubtitleReplaceResult (int number, String text, String translation) {
+		this.number = number;
+		this.text = text;
+		this.translation = translation;
+	}
+	
+	/* Public properties */
+
+	public int Number {
+		get { return number; }
+	}
+
+	public String Text {
+		get { return text; }
+	}
+
+	public String Translation {
+		get { return translation; }
+	}
+
+}
+
+}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]