[gnome-subtitles] Fix bug #607227 - Filenames for new translations. Code cleanup.



commit 2891dd3fd9fed1f8a7d1dda144e25f1a76fad1de
Author: Pedro Castro <mail pedrocastro org>
Date:   Sun Dec 19 16:44:21 2010 +0000

    Fix bug #607227 - Filenames for new translations.
    Code cleanup.

 src/Glade/MainWindow.glade                         |   18 ++--
 src/GnomeSubtitles/Core/Backup.cs                  |    5 +-
 src/GnomeSubtitles/Core/Base.cs                    |    4 +-
 src/GnomeSubtitles/Core/Document.cs                |   84 +++++++++++++++-----
 src/GnomeSubtitles/Core/EventHandlers.cs           |    2 +-
 .../Dialog/FileTranslationOpenDialog.cs            |    2 +-
 .../Dialog/SubtitleFileSaveAsDialog.cs             |   18 +++--
 .../Dialog/Unmanaged/SaveConfirmationDialog.cs     |   11 ++-
 src/GnomeSubtitles/Ui/MainUi.cs                    |   13 +--
 9 files changed, 105 insertions(+), 52 deletions(-)
---
diff --git a/src/Glade/MainWindow.glade b/src/Glade/MainWindow.glade
index 639e5c5..2476873 100644
--- a/src/Glade/MainWindow.glade
+++ b/src/Glade/MainWindow.glade
@@ -248,8 +248,8 @@
                         <property name="use_underline">True</property>
                         <property name="use_stock">True</property>
                         <signal name="activate" handler="OnEditRedo"/>
-                        <accelerator key="Z" signal="activate" modifiers="GDK_SHIFT_MASK | GDK_CONTROL_MASK"/>
                         <accelerator key="Y" signal="activate" modifiers="GDK_CONTROL_MASK"/>
+                        <accelerator key="Z" signal="activate" modifiers="GDK_SHIFT_MASK | GDK_CONTROL_MASK"/>
                       </widget>
                     </child>
                     <child>
@@ -532,8 +532,8 @@
                         <property name="label" translatable="yes">Find Ne_xt</property>
                         <property name="use_underline">True</property>
                         <signal name="activate" handler="OnSearchFindNext"/>
-                        <accelerator key="g" signal="activate" modifiers="GDK_CONTROL_MASK"/>
                         <accelerator key="F3" signal="activate"/>
+                        <accelerator key="g" signal="activate" modifiers="GDK_CONTROL_MASK"/>
                       </widget>
                     </child>
                     <child>
@@ -543,8 +543,8 @@
                         <property name="label" translatable="yes">Find Pre_vious</property>
                         <property name="use_underline">True</property>
                         <signal name="activate" handler="OnSearchFindPrevious"/>
-                        <accelerator key="g" signal="activate" modifiers="GDK_SHIFT_MASK | GDK_CONTROL_MASK"/>
                         <accelerator key="F3" signal="activate" modifiers="GDK_SHIFT_MASK"/>
+                        <accelerator key="g" signal="activate" modifiers="GDK_SHIFT_MASK | GDK_CONTROL_MASK"/>
                       </widget>
                     </child>
                     <child>
@@ -788,8 +788,8 @@
                         <property name="use_underline">True</property>
                         <property name="use_stock">False</property>
                         <signal name="activate" handler="OnVideoPlayPause"/>
-                        <accelerator key="p" signal="activate" modifiers="GDK_CONTROL_MASK"/>
                         <accelerator key="F5" signal="activate"/>
+                        <accelerator key="p" signal="activate" modifiers="GDK_CONTROL_MASK"/>
                         <child internal-child="image">
                           <widget class="GtkImage" id="videoPlayPauseImage">
                             <property name="visible">True</property>
@@ -816,8 +816,8 @@
                         <property name="use_underline">True</property>
                         <property name="use_stock">False</property>
                         <signal name="activate" handler="OnVideoRewind"/>
-                        <accelerator key="k" signal="activate" modifiers="GDK_CONTROL_MASK"/>
                         <accelerator key="F6" signal="activate"/>
+                        <accelerator key="k" signal="activate" modifiers="GDK_CONTROL_MASK"/>
                         <child internal-child="image">
                           <widget class="GtkImage" id="videoRewindImage">
                             <property name="visible">True</property>
@@ -835,8 +835,8 @@
                         <property name="use_underline">True</property>
                         <property name="use_stock">False</property>
                         <signal name="activate" handler="OnVideoForward"/>
-                        <accelerator key="l" signal="activate" modifiers="GDK_CONTROL_MASK"/>
                         <accelerator key="F7" signal="activate"/>
+                        <accelerator key="l" signal="activate" modifiers="GDK_CONTROL_MASK"/>
                         <child internal-child="image">
                           <widget class="GtkImage" id="videoForwardImage">
                             <property name="visible">True</property>
@@ -876,8 +876,8 @@
                         <property name="label" translatable="yes">Seek _to Selection</property>
                         <property name="use_underline">True</property>
                         <signal name="activate" handler="OnVideoSeekToSelection"/>
-                        <accelerator key="r" signal="activate" modifiers="GDK_SHIFT_MASK | GDK_CONTROL_MASK"/>
                         <accelerator key="F4" signal="activate" modifiers="GDK_SHIFT_MASK | GDK_CONTROL_MASK"/>
+                        <accelerator key="r" signal="activate" modifiers="GDK_SHIFT_MASK | GDK_CONTROL_MASK"/>
                       </widget>
                     </child>
                     <child>
@@ -887,8 +887,8 @@
                         <property name="label" translatable="yes">Select Nearest Subtitle</property>
                         <property name="use_underline">True</property>
                         <signal name="activate" handler="OnVideoSelectNearestSubtitle"/>
-                        <accelerator key="F4" signal="activate" modifiers="GDK_CONTROL_MASK"/>
                         <accelerator key="r" signal="activate" modifiers="GDK_CONTROL_MASK"/>
+                        <accelerator key="F4" signal="activate" modifiers="GDK_CONTROL_MASK"/>
                       </widget>
                     </child>
                     <child>
@@ -897,8 +897,8 @@
                         <property name="label" translatable="yes">Auto Select Subtitle</property>
                         <property name="use_underline">True</property>
                         <signal name="toggled" handler="OnVideoAutoSelectSubtitles"/>
-                        <accelerator key="F3" signal="activate" modifiers="GDK_CONTROL_MASK"/>
                         <accelerator key="j" signal="activate" modifiers="GDK_CONTROL_MASK"/>
+                        <accelerator key="F3" signal="activate" modifiers="GDK_CONTROL_MASK"/>
                       </widget>
                     </child>
                     <child>
diff --git a/src/GnomeSubtitles/Core/Backup.cs b/src/GnomeSubtitles/Core/Backup.cs
index b374bae..0c96641 100644
--- a/src/GnomeSubtitles/Core/Backup.cs
+++ b/src/GnomeSubtitles/Core/Backup.cs
@@ -68,9 +68,10 @@ public class Backup {
 	}
 	
 	private bool DoBackup() {
-		if (!Base.IsDocumentLoaded)
+		if (!Base.IsDocumentLoaded) {
 			return true;
-		
+		}
+
 		BackupThreadArgs backupThreadArgs = new BackupThreadArgs();
 		if (Base.Document.CanTextBeSaved) {
 			backupThreadArgs.TextFilePropertiesClone = Base.Document.TextFile.Clone() as SubLib.Core.Domain.FileProperties;
diff --git a/src/GnomeSubtitles/Core/Base.cs b/src/GnomeSubtitles/Core/Base.cs
index 8294108..5ec258e 100644
--- a/src/GnomeSubtitles/Core/Base.cs
+++ b/src/GnomeSubtitles/Core/Base.cs
@@ -164,11 +164,11 @@ public class Base {
 		executionContext.QuitApplication();
 	}
 	
-	public static void NewDocument (string path) {
+	public static void NewDocument () {
 		if (IsDocumentLoaded)
 			CloseDocument();
 			
-		document = new Document(path);
+		document = new Document();
 		EmitDocumentLoadedEvent();
 		
 		if (document.Subtitles.Count == 0)
diff --git a/src/GnomeSubtitles/Core/Document.cs b/src/GnomeSubtitles/Core/Document.cs
index 4a49e03..7f582bb 100644
--- a/src/GnomeSubtitles/Core/Document.cs
+++ b/src/GnomeSubtitles/Core/Document.cs
@@ -21,6 +21,7 @@ using GnomeSubtitles.Core.Command;
 using Mono.Unix;
 using SubLib.Core;
 using SubLib.Core.Domain;
+using System;
 using System.IO;
 using System.Text;
 
@@ -31,6 +32,7 @@ public delegate void DocumentModificationStatusChangedHandler (bool modified);
 
 public class Document {
 	private Ui.View.Subtitles subtitles = null;
+	private bool isTranslationLoaded = false; //Whether a translation document is loaded, either using New or Open Translation
 	private bool wasTextModified = false;
 	private bool wasTranslationModified = false;
 
@@ -40,8 +42,8 @@ public class Document {
 	private bool canTranslationBeSaved = false; //Whether the translation document can be saved with existing translationFile properties
 
 
-	public Document (string path) {
-		New(path);
+	public Document () {
+		New();
 		ConnectInitSignals();
 	}
 	
@@ -57,7 +59,14 @@ public class Document {
 	/* Public properties */
 	
 	public FileProperties TextFile {
-		get { return textFile; }
+		get {
+			/*Note: this must be changed if the unsaved text filename is to be dynamically generated on Save time.
+			  For now its creation is enforced as its name is shown in the title bar. */
+			if (textFile == null) {
+				textFile = CreateNewTextFileProperties();
+			}
+			return textFile;
+		}
 	}
 	
 	public FileProperties TranslationFile {
@@ -65,6 +74,16 @@ public class Document {
 	}
 	
 	public bool IsTranslationLoaded {
+		get { return isTranslationLoaded; }
+	}
+	
+	/* Whether the text file properties is set */
+	public bool HasTextFileProperties {
+		get { return textFile != null; }
+	}
+	
+	/* Whether the translation file properties is set */
+	public bool HasTranslationFileProperties {
 		get { return translationFile != null; }
 	}
 
@@ -88,6 +107,24 @@ public class Document {
 		get { return wasTranslationModified; }
 	}
 	
+	public string UnsavedTextFilename {
+		get {
+			//To translators: this is the filename for new files (before being saved for the first time)
+			return Catalog.GetString("Unsaved Subtitles");
+		}
+	}
+	
+	public string UnsavedTranslationFilename {
+		get {
+			string filename = (((textFile == null) || (textFile.Filename == String.Empty)) ? this.UnsavedTextFilename : textFile.FilenameWithoutExtension);
+			string language = (Base.SpellLanguages.HasActiveTranslationLanguage ? Base.SpellLanguages.ActiveTranslationLanguage.ID : String.Empty);
+			//To translators: this defines the name of a translation file. {0}=filename, {1}=language. Example: MovieName (fr translation)
+			string translatedString = (language != String.Empty ? Catalog.GetString("{0} ({1} translation)") : Catalog.GetString("{0} (translation)"));
+			object[] args = {filename, language};			
+			return Core.Util.GetFormattedText(translatedString, args);
+		}
+	}
+	
 
 	/* Public methods */
 
@@ -97,16 +134,16 @@ public class Document {
 		
 		textFile = saver.FileProperties;		
 		canTextBeSaved = true;
-	
+
 		ClearTextModified();
 		return true;
 	}
 
 	public void NewTranslation () {
-		if (this.IsTranslationLoaded)
+		if (this.isTranslationLoaded) {
 			CloseTranslation();
-
-		CreateNewTranslationFileProperties();
+		}
+		this.isTranslationLoaded = true;
 	}
 
 	public void OpenTranslation (string path, Encoding encoding) {
@@ -128,7 +165,8 @@ public class Document {
 		if (newTranslationFile.SubtitleType != SubtitleType.Unknown)
 			canTranslationBeSaved = true;
 	
-		translationFile = newTranslationFile;
+		this.translationFile = newTranslationFile;
+		this.isTranslationLoaded = true;
 	}
 	
 	public void Close () {
@@ -154,15 +192,20 @@ public class Document {
 
 	/* Private methods */
 	
-	/* Used in the object construction */
+	/* Used in the object construction. Path is used when running from the command line specifying an inexistent file. */
 	private void New (string path) {
+		New();
+		textFile = new FileProperties(path);
+	}
+
+	/* Used in the object construction */
+	private void New () {
 		SubtitleFactory factory = new SubtitleFactory();
 		factory.Verbose = true;
 		
 		subtitles = new Ui.View.Subtitles(factory.New());
-		textFile = new FileProperties(path);
 	}
-	
+
 	/* Used in the object construction */
 	private void Open (string path, Encoding encoding) {
 		SubtitleFactory factory = new SubtitleFactory();
@@ -199,21 +242,16 @@ public class Document {
 			EmitModificationStatusChangedEvent(false);
 	}
 	
-	private void CreateNewTranslationFileProperties () {
-		string filename = Catalog.GetString("Unsaved Translation");
-		string path = (textFile.IsPathRooted ? Path.Combine(textFile.Directory, filename) : filename);
-		translationFile = new FileProperties(path, textFile.Encoding, textFile.SubtitleType, textFile.TimingMode, textFile.NewlineType);
-	}
-	
 	private void RemoveTranslationFromSubtitles () {
 		Translations translations = new Translations();
 		translations.Clear(subtitles);
 	}
 	
 	private void ClearTranslationStatus () {
-		wasTranslationModified = false;
-		translationFile = null;
-		canTranslationBeSaved = false;
+		this.isTranslationLoaded = false;
+		this.wasTranslationModified = false;
+		this.translationFile = null;
+		this.canTranslationBeSaved = false;
 		
 		ClearTranslationModified();
 	}
@@ -236,6 +274,12 @@ public class Document {
 		}
 	}
 	
+	private FileProperties CreateNewTextFileProperties () {
+		return new FileProperties(this.UnsavedTextFilename);
+	}
+	
+	
+	
 	/* Event members */
 	
 	private void ConnectInitSignals () {
diff --git a/src/GnomeSubtitles/Core/EventHandlers.cs b/src/GnomeSubtitles/Core/EventHandlers.cs
index 30d5483..6da0a20 100644
--- a/src/GnomeSubtitles/Core/EventHandlers.cs
+++ b/src/GnomeSubtitles/Core/EventHandlers.cs
@@ -35,7 +35,7 @@ public class EventHandlers {
 	/* File Menu */
 	
 	public void OnFileNew (object o, EventArgs args) {
-		Base.Ui.New(String.Empty);
+		Base.Ui.New();
 	}
 	
 	public void OnFileOpen (object o, EventArgs args) {
diff --git a/src/GnomeSubtitles/Dialog/FileTranslationOpenDialog.cs b/src/GnomeSubtitles/Dialog/FileTranslationOpenDialog.cs
index 086f51a..f91df69 100644
--- a/src/GnomeSubtitles/Dialog/FileTranslationOpenDialog.cs
+++ b/src/GnomeSubtitles/Dialog/FileTranslationOpenDialog.cs
@@ -30,7 +30,7 @@ public class FileTranslationOpenDialog : FileOpenDialog {
 	/* Public members */
 	
 	protected override string GetStartFolder () {
-		if (Base.IsDocumentLoaded && Base.Document.IsTranslationLoaded && Base.Document.TranslationFile.IsPathRooted)
+		if (Base.IsDocumentLoaded && Base.Document.IsTranslationLoaded && Base.Document.HasTranslationFileProperties && Base.Document.TranslationFile.IsPathRooted)
 			return Base.Document.TranslationFile.Directory;
 		else
 			return base.GetStartFolder();
diff --git a/src/GnomeSubtitles/Dialog/SubtitleFileSaveAsDialog.cs b/src/GnomeSubtitles/Dialog/SubtitleFileSaveAsDialog.cs
index 9e72fe3..34dc097 100644
--- a/src/GnomeSubtitles/Dialog/SubtitleFileSaveAsDialog.cs
+++ b/src/GnomeSubtitles/Dialog/SubtitleFileSaveAsDialog.cs
@@ -154,14 +154,20 @@ public abstract class SubtitleFileSaveAsDialog : GladeDialog {
 	}
 	
 	private void SetDialogFromFileProperties () {
-		FileProperties fileProperties = (textType == SubtitleTextType.Text ? Base.Document.TextFile : Base.Document.TranslationFile);
-
-		if (fileProperties.IsPathRooted)
-			dialog.SetCurrentFolder(fileProperties.Directory);
+		/* Set folder */
+		if ((textType == SubtitleTextType.Translation) && Base.Document.HasTranslationFileProperties && Base.Document.TranslationFile.IsPathRooted)
+			dialog.SetCurrentFolder(Base.Document.TranslationFile.Directory);
+		else if (Base.Document.HasTextFileProperties && Base.Document.TextFile.IsPathRooted)
+			dialog.SetCurrentFolder(Base.Document.TextFile.Directory);
 		else
 			dialog.SetCurrentFolder(Environment.GetFolderPath(Environment.SpecialFolder.Personal));
-			
-		dialog.CurrentName = fileProperties.Filename;
+		
+		/* Set filename */
+		FileProperties fileProperties = (textType == SubtitleTextType.Text ? Base.Document.TextFile : Base.Document.TranslationFile);
+		if ((fileProperties != null) && (fileProperties.Filename != String.Empty))
+			dialog.CurrentName = fileProperties.Filename;
+		else
+			dialog.CurrentName = (textType == SubtitleTextType.Text ? Base.Document.UnsavedTextFilename : Base.Document.UnsavedTranslationFilename);
 	}
 	
 	
diff --git a/src/GnomeSubtitles/Dialog/Unmanaged/SaveConfirmationDialog.cs b/src/GnomeSubtitles/Dialog/Unmanaged/SaveConfirmationDialog.cs
index 49558d9..144ed8b 100644
--- a/src/GnomeSubtitles/Dialog/Unmanaged/SaveConfirmationDialog.cs
+++ b/src/GnomeSubtitles/Dialog/Unmanaged/SaveConfirmationDialog.cs
@@ -34,8 +34,15 @@ public abstract class SaveConfirmationDialog : WarningDialog {
 	public SaveConfirmationDialog (string primaryText, SubtitleTextType textType) : base() {
 		this.textType = textType;
 	
-		string fileName = (textType == SubtitleTextType.Text ? Base.Document.TextFile.Filename : Base.Document.TranslationFile.Filename);
-		SetText(primaryText, secondaryText, fileName);
+		string filename = String.Empty;
+		if (textType == SubtitleTextType.Text)
+			filename = Base.Document.TextFile.Filename;
+		else if (Base.Document.HasTranslationFileProperties)
+			filename = Base.Document.TranslationFile.Filename;
+		else
+			filename = Base.Document.UnsavedTranslationFilename;
+		
+		SetText(primaryText, secondaryText, filename);
 	}
 	
 	
diff --git a/src/GnomeSubtitles/Ui/MainUi.cs b/src/GnomeSubtitles/Ui/MainUi.cs
index 576541c..19adc4e 100644
--- a/src/GnomeSubtitles/Ui/MainUi.cs
+++ b/src/GnomeSubtitles/Ui/MainUi.cs
@@ -126,17 +126,12 @@ public class MainUi {
 
 	/// <summary>Creates a new subtitles document for the specified path.</summary>
 	/// <param name="path">The subtitles' filename. If it's an empty string, 'Unsaved Subtitles' will be used instead.</param>
-	/// <remarks>If there's a document opened with unsaved changes, a warning dialog is shown.</remarks>
-    public void New (string path) {
+	/// <remarks>If there's a document open with unsaved changes, a warning dialog is shown.</remarks>
+    public void New () {
     	if (!ToCreateNewAfterWarning())
     		return;
-
-		if (path == String.Empty) {
-			//To translators: this is the filename for new files (before being saved for the first time)
-			path = Catalog.GetString("Unsaved Subtitles");
-		}
-
-		Base.NewDocument(path);
+		else
+			Base.NewDocument();
     }
     
     /// <summary>Shows the open dialog and possibly opens a subtitle.</summary>



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