[gnome-subtitles] Allow to set the default subtitle format in the Save As dialog



commit 0133bc61c27d93800f6df06de686b7c2f1117e8c
Author: Pedro Castro <mail>
Date:   Sat Jan 30 14:42:38 2010 +0000

    Allow to set the default subtitle format in the Save As dialog

 data/gnome-subtitles.schemas                       |   24 +++-
 gnome-subtitles.mdp                                |    1 +
 src/Glade/FileSaveAsDialog.glade                   |    6 +-
 src/Glade/PreferencesDialog.glade                  |    2 +-
 src/GnomeSubtitles/Core/Config.cs                  |   20 +++
 src/GnomeSubtitles/Dialog/PreferencesDialog.cs     |   41 +++++-
 .../Dialog/SubtitleFileSaveAsDialog.cs             |  154 +++++++------------
 .../Ui/Component/EncodingComboBox.cs               |    9 +-
 .../Ui/Component/SubtitleFormatComboBox.cs         |  164 ++++++++++++++++++++
 src/SubLib/Core/Domain/Subtitles.cs                |   40 ++++--
 src/SubLib/IO/SubtitleFormats/SubtitleFormat.cs    |    2 +-
 11 files changed, 345 insertions(+), 118 deletions(-)
---
diff --git a/data/gnome-subtitles.schemas b/data/gnome-subtitles.schemas
index 4cc62f4..79fe96d 100644
--- a/data/gnome-subtitles.schemas
+++ b/data/gnome-subtitles.schemas
@@ -131,7 +131,29 @@
 			<default></default>
 			<locale name="C">
 				<short>File Save encoding</short>
-				<long>Encoding to use when saving files. Besides the actual encoding, used when "RememberLastUsed" or "Specific" are set in the file_save_encoding_option key, another possible value is "CurrentLocale".</long>
+				<long>Encoding to use when saving files. Besides the actual encoding, used when "RememberLastUsed" or "Specific" are set in the file_save_encoding_option key, other possible values are "KeepExisting" and "CurrentLocale".</long>
+			</locale>
+		</schema>
+		<schema>
+			<key>/schemas/apps/gnome-subtitles/preferences/defaults/file_save_format_option</key>
+			<applyto>/apps/gnome-subtitles/preferences/defaults/file_save_format_option</applyto>
+			<owner>gnome-subtitles</owner>
+			<type>string</type>
+			<default>KeepExisting</default>
+			<locale name="C">
+				<short>File Save format option</short>
+				<long>Subtitle format option to use by default when saving files. Possible values are: "KeepExisting" to use the existing file's format, "RememberLastUsed" to remember the last used format, and "Specific" to use a specific format. The subtitle format for "RememberLastUsed" and "Specific" is stored in the file_save_format key.</long>
+			</locale>
+		</schema>
+		<schema>
+			<key>/schemas/apps/gnome-subtitles/preferences/defaults/file_save_format</key>
+			<applyto>/apps/gnome-subtitles/preferences/defaults/file_save_format</applyto>
+			<owner>gnome-subtitles</owner>
+			<type>string</type>
+			<default></default>
+			<locale name="C">
+				<short>File Save format</short>
+				<long>Subtitle format to use when saving files. Besides the actual format, used when "RememberLastUsed" or "Specific" are set in the file_save_format_option key, other possible values are "KeepExisting" and "CurrentLocale".</long>
 			</locale>
 		</schema>
 	</schemalist>
diff --git a/gnome-subtitles.mdp b/gnome-subtitles.mdp
index 8e5985d..b88f9b2 100644
--- a/gnome-subtitles.mdp
+++ b/gnome-subtitles.mdp
@@ -246,6 +246,7 @@
     <File name="src/GnomeSubtitles/Ui/Component" subtype="Directory" buildaction="Compile" />
     <File name="src/GnomeSubtitles/Ui/Component/EncodingComboBox.cs" subtype="Code" buildaction="Compile" />
     <File name="src/GnomeSubtitles/Ui/Component/ComboBoxUtil.cs" subtype="Code" buildaction="Compile" />
+    <File name="src/GnomeSubtitles/Ui/Component/SubtitleFormatComboBox.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/Glade/FileSaveAsDialog.glade b/src/Glade/FileSaveAsDialog.glade
index 30b45aa..f390848 100644
--- a/src/Glade/FileSaveAsDialog.glade
+++ b/src/Glade/FileSaveAsDialog.glade
@@ -9,10 +9,10 @@
     <property name="destroy_with_parent">True</property>
     <property name="type_hint">dialog</property>
     <property name="has_separator">False</property>
-    <property name="use_preview_label">False</property>
     <property name="preview_widget_active">False</property>
-    <property name="action">save</property>
     <property name="do_overwrite_confirmation">True</property>
+    <property name="action">save</property>
+    <property name="use_preview_label">False</property>
     <child internal-child="vbox">
       <widget class="GtkVBox" id="dialogVBox">
         <property name="visible">True</property>
@@ -55,7 +55,7 @@
                   </packing>
                 </child>
                 <child>
-                  <widget class="GtkComboBox" id="formatComboBox">
+                  <widget class="GtkComboBox" id="subtitleFormatComboBox">
                     <property name="visible">True</property>
                     <property name="items" translatable="yes"></property>
                     <signal name="changed" handler="OnFormatChanged"/>
diff --git a/src/Glade/PreferencesDialog.glade b/src/Glade/PreferencesDialog.glade
index 5a583f4..2cfb778 100644
--- a/src/Glade/PreferencesDialog.glade
+++ b/src/Glade/PreferencesDialog.glade
@@ -211,7 +211,7 @@
                           </packing>
                         </child>
                         <child>
-                          <widget class="GtkComboBox" id="subtitleFormatComboBox">
+                          <widget class="GtkComboBox" id="fileSaveFormatComboBox">
                             <property name="visible">True</property>
                           </widget>
                           <packing>
diff --git a/src/GnomeSubtitles/Core/Config.cs b/src/GnomeSubtitles/Core/Config.cs
index 9284750..6112503 100644
--- a/src/GnomeSubtitles/Core/Config.cs
+++ b/src/GnomeSubtitles/Core/Config.cs
@@ -18,6 +18,7 @@
  */
 
 using GConf;
+using SubLib.Core.Domain;
 using System;
 
 namespace GnomeSubtitles.Core {
@@ -28,6 +29,8 @@ public enum ConfigFileOpenEncoding { AutoDetect = 0, CurrentLocale = 2, Fixed =
 public enum ConfigFileOpenFallbackEncoding { CurrentLocale = 0, Fixed = 1 };
 public enum ConfigFileSaveEncodingOption { KeepExisting = 0, RememberLastUsed = 1, CurrentLocale = 3, Specific = 4 }; //Values match ordering where the options are used
 public enum ConfigFileSaveEncoding { KeepExisting = -1, CurrentLocale = 0, Fixed = 1 }; //KeepExisting=-1 because it doesn't appear
+public enum ConfigFileSaveFormatOption { KeepExisting = 0, RememberLastUsed = 1, Specific = 3 }; //Values match ordering where the options are used
+public enum ConfigFileSaveFormat { KeepExisting = -1, Fixed = 0 }; //KeepExisting=-1 because it doesn't appear
 
 public class Config {
 	private Client client = null;
@@ -54,6 +57,8 @@ public class Config {
 	private const string keyPrefsDefaultsFileOpenFallbackEncoding = keyPrefsDefaults + "file_open_fallback";
 	private const string keyPrefsDefaultsFileSaveEncodingOption = keyPrefsDefaults + "file_save_encoding_option";
 	private const string keyPrefsDefaultsFileSaveEncoding = keyPrefsDefaults + "file_save_encoding";
+	private const string keyPrefsDefaultsFileSaveFormatOption = keyPrefsDefaults + "file_save_format_option";
+	private const string keyPrefsDefaultsFileSaveFormat = keyPrefsDefaults + "file_save_format";
 
 	public Config () {
 		client = new Client();
@@ -142,6 +147,21 @@ public class Config {
 		set { Set(keyPrefsDefaultsFileSaveEncoding, value); }
 	}
 
+	public ConfigFileSaveFormatOption PrefsDefaultsFileSaveFormatOption {
+		get { return (ConfigFileSaveFormatOption)GetEnumValue(keyPrefsDefaultsFileSaveFormatOption, ConfigFileSaveFormatOption.KeepExisting); }
+		set { Set(keyPrefsDefaultsFileSaveFormatOption, value.ToString()); }
+	}
+
+	public ConfigFileSaveFormat PrefsDefaultsFileSaveFormat {
+		get { return (ConfigFileSaveFormat)GetEnumValueFromSuperset(keyPrefsDefaultsFileSaveFormat, ConfigFileSaveFormat.Fixed); }
+		set { Set(keyPrefsDefaultsFileSaveFormat, value.ToString()); }
+	}
+
+	/* Uses the same key as PrefsDefaultsFileSaveFormat but is used when there's a specific format set */
+	public SubtitleType PrefsDefaultsFileSaveFormatFixed {
+		get { return (SubtitleType)GetEnumValueFromSuperset(keyPrefsDefaultsFileSaveFormat, SubtitleType.SubRip); }
+		set { Set(keyPrefsDefaultsFileSaveFormat, value.ToString()); }
+	}
 
 	
 	/* Private members */
diff --git a/src/GnomeSubtitles/Dialog/PreferencesDialog.cs b/src/GnomeSubtitles/Dialog/PreferencesDialog.cs
index a69b4c6..1bbc213 100644
--- a/src/GnomeSubtitles/Dialog/PreferencesDialog.cs
+++ b/src/GnomeSubtitles/Dialog/PreferencesDialog.cs
@@ -22,6 +22,7 @@ using GnomeSubtitles.Core;
 using GnomeSubtitles.Ui.Component;
 using Gtk;
 using Mono.Unix;
+using SubLib.Core.Domain;
 using System;
 
 namespace GnomeSubtitles.Dialog {
@@ -35,12 +36,14 @@ public class PreferencesDialog : GladeDialog {
 	private EncodingComboBox fileOpenEncoding = null;
 	private EncodingComboBox fileOpenFallbackEncoding = null;
 	private EncodingComboBox fileSaveEncoding = null;
+	private SubtitleFormatComboBox fileSaveFormat = null;
 
 	/* Widgets */
 	[WidgetAttribute] private CheckButton videoAutoChooseFileCheckButton = null;
 	[WidgetAttribute] private ComboBox fileOpenEncodingComboBox = null;
 	[WidgetAttribute] private ComboBox fileOpenFallbackEncodingComboBox = null;
 	[WidgetAttribute] private ComboBox fileSaveEncodingComboBox = null;
+	[WidgetAttribute] private ComboBox fileSaveFormatComboBox = null;
 
 
 	public PreferencesDialog () : base(gladeFilename, false) {
@@ -54,6 +57,7 @@ public class PreferencesDialog : GladeDialog {
 		SetDefaultsFileOpenEncoding();
 		SetDefaultsFileOpenFallbackEncoding();
 		SetDefaultsFileSaveEncoding();
+		SetDefaultsFileSaveFormat();
 
 		/* Video Auto choose file */
 		videoAutoChooseFileCheckButton.Active = Base.Config.PrefsVideoAutoChooseFile;
@@ -109,6 +113,21 @@ public class PreferencesDialog : GladeDialog {
 		fileSaveEncoding.SelectionChanged += OnDefaultsFileSaveEncodingChanged;
 	}
 
+	private void SetDefaultsFileSaveFormat () {
+		string[] additionalActions = { Catalog.GetString("Keep Existing"), Catalog.GetString("Remember Last Used") }; //TODO change label
+		SubtitleType fixedFormat = SubtitleType.Unknown;
+		ConfigFileSaveFormatOption fileSaveFormatOption = Base.Config.PrefsDefaultsFileSaveFormatOption;
+		if (fileSaveFormatOption == ConfigFileSaveFormatOption.Specific) {
+			fixedFormat = Base.Config.PrefsDefaultsFileSaveFormatFixed;
+		}
+
+		fileSaveFormat = new SubtitleFormatComboBox(fileSaveFormatComboBox, fixedFormat, additionalActions);
+		if (fileSaveFormatOption != ConfigFileSaveFormatOption.Specific) {
+			fileSaveFormat.ActiveSelection = (int)fileSaveFormatOption;
+		}
+		fileSaveFormat.SelectionChanged += OnDefaultsFileSaveFormatChanged;
+	}
+
 	
 	/* Event members */
 
@@ -152,7 +171,7 @@ public class PreferencesDialog : GladeDialog {
 	private void OnDefaultsFileSaveEncodingChanged (object o, EventArgs args) {
 		int active = fileSaveEncoding.ActiveSelection;
 		ConfigFileSaveEncodingOption activeOption = (ConfigFileSaveEncodingOption)Enum.ToObject(typeof(ConfigFileSaveEncodingOption), active);
-		if (((int)activeOption) > ((int)ConfigFileOpenEncodingOption.Specific)) //Positions higher than specific are always specific too
+		if (((int)activeOption) > ((int)ConfigFileSaveEncodingOption.Specific)) //Positions higher than specific are always specific too
 			activeOption = ConfigFileSaveEncodingOption.Specific;
 
 		Base.Config.PrefsDefaultsFileSaveEncodingOption = activeOption;
@@ -173,6 +192,26 @@ public class PreferencesDialog : GladeDialog {
 		}
 	}
 
+	private void OnDefaultsFileSaveFormatChanged (object o, EventArgs args) {
+		int active = fileSaveFormat.ActiveSelection;
+		ConfigFileSaveFormatOption activeOption = (ConfigFileSaveFormatOption)Enum.ToObject(typeof(ConfigFileSaveFormatOption), active);
+		if (((int)activeOption) > ((int)ConfigFileSaveFormatOption.Specific)) //Positions higher than specific are always specific too
+			activeOption = ConfigFileSaveFormatOption.Specific;
+
+		Base.Config.PrefsDefaultsFileSaveFormatOption = activeOption;
+		/* If format is specific, formatOption=Specific and format holds the format name */
+		if (activeOption == ConfigFileSaveFormatOption.Specific) {
+			SubtitleType chosenFormat = fileSaveFormat.ChosenSubtitleType;
+			if (!chosenFormat.Equals(SubtitleType.Unknown)) {
+				Base.Config.PrefsDefaultsFileSaveFormatFixed = chosenFormat;
+			}
+		}
+		else {
+			/* If encoding option is keep existing or remember last, use keep existing */
+			Base.Config.PrefsDefaultsFileSaveFormat = ConfigFileSaveFormat.KeepExisting;
+		}
+	}
+
 	private void OnVideoAutoChooseFileToggled (object o, EventArgs args) {
 		Base.Config.PrefsVideoAutoChooseFile = videoAutoChooseFileCheckButton.Active;
 	}
diff --git a/src/GnomeSubtitles/Dialog/SubtitleFileSaveAsDialog.cs b/src/GnomeSubtitles/Dialog/SubtitleFileSaveAsDialog.cs
index d6490f0..a795c02 100644
--- a/src/GnomeSubtitles/Dialog/SubtitleFileSaveAsDialog.cs
+++ b/src/GnomeSubtitles/Dialog/SubtitleFileSaveAsDialog.cs
@@ -36,7 +36,6 @@ public abstract class SubtitleFileSaveAsDialog : GladeDialog {
 	private EncodingDescription chosenEncoding = EncodingDescription.Empty;
 	private SubtitleTextType textType;
 	private SubtitleType chosenSubtitleType;
-	private SubtitleTypeInfo[] subtitleTypes = null;
 	private NewlineType chosenNewlineType;
 
 	/* Constant strings */
@@ -44,10 +43,11 @@ public abstract class SubtitleFileSaveAsDialog : GladeDialog {
 
 	/* Components */
 	private EncodingComboBox encodingComboBox = null;
+	private SubtitleFormatComboBox formatComboBox = null;
 
 	/* Widgets */
 	[WidgetAttribute] private ComboBox fileEncodingComboBox = null;
-	[WidgetAttribute] private ComboBox formatComboBox = null;
+	[WidgetAttribute] private ComboBox subtitleFormatComboBox = null;
 	[WidgetAttribute] private ComboBox newlineTypeComboBox = null;
 
 
@@ -58,33 +58,12 @@ public abstract class SubtitleFileSaveAsDialog : GladeDialog {
 		SetTitle();
 
 		InitEncodingComboBox();
+		InitFormatComboBox();
 
-		FillFormatComboBox();
 		FillNewlineTypeComboBox();
+		UpdateContents(); //TODO check 
 	}
 
-	private void InitEncodingComboBox () {
-		int fixedEncoding = GetFixedEncoding();
-		ConfigFileSaveEncoding encodingConfig = Base.Config.PrefsDefaultsFileSaveEncoding;
-		if (encodingConfig == ConfigFileSaveEncoding.Fixed) {
-			string encodingName = Base.Config.PrefsDefaultsFileSaveEncodingFixed;
-			EncodingDescription encodingDescription = EncodingDescription.Empty;
-			Encodings.Find(encodingName, ref encodingDescription);
-			fixedEncoding = encodingDescription.CodePage;
-		}
-
-		this.encodingComboBox = new EncodingComboBox(fileEncodingComboBox, false, null, fixedEncoding);
-
-		/* Only need to handle the case of currentLocale, as Fixed and Keep Existent is handled before */
-		if (encodingConfig == ConfigFileSaveEncoding.CurrentLocale)
-			encodingComboBox.ActiveSelection = (int)encodingConfig;
-	}
-
-	/* Overriden members */
-
-	/*public override DialogScope Scope {
-		get { return DialogScope.Document; }
-	}*/
 
 	/* Public properties */
 
@@ -103,16 +82,40 @@ public abstract class SubtitleFileSaveAsDialog : GladeDialog {
 	public NewlineType NewlineType {
 		get { return chosenNewlineType; }
 	}
+
 	
-	/* Public methods */
+	/* Private members */
 	
-	public override void Show () {
-		UpdateContents();
-		base.Show();		
+	private void InitEncodingComboBox () {
+		int fixedEncoding = GetFixedEncoding();
+		ConfigFileSaveEncoding encodingConfig = Base.Config.PrefsDefaultsFileSaveEncoding;
+		if (encodingConfig == ConfigFileSaveEncoding.Fixed) {
+			string encodingName = Base.Config.PrefsDefaultsFileSaveEncodingFixed;
+			EncodingDescription encodingDescription = EncodingDescription.Empty;
+			Encodings.Find(encodingName, ref encodingDescription);
+			fixedEncoding = encodingDescription.CodePage;
+		}
+
+		this.encodingComboBox = new EncodingComboBox(fileEncodingComboBox, false, null, fixedEncoding);
+
+		/* Only need to handle the case of currentLocale, as Fixed and Keep Existent is handled before */
+		if (encodingConfig == ConfigFileSaveEncoding.CurrentLocale)
+			encodingComboBox.ActiveSelection = (int)encodingConfig;
 	}
 
-	
-	/* Private members */
+	private void InitFormatComboBox () {
+		SubtitleType fixedSubtitleType = GetFixedSubtitleType();
+		ConfigFileSaveFormat formatConfig = Base.Config.PrefsDefaultsFileSaveFormat;
+		if (formatConfig == ConfigFileSaveFormat.Fixed) {
+			fixedSubtitleType = Base.Config.PrefsDefaultsFileSaveFormatFixed;
+		}
+		/* Check if fixed subtitle type has been correctly identified */
+		if (fixedSubtitleType == SubtitleType.Unknown) {
+			fixedSubtitleType = SubtitleType.SubRip;
+		}
+
+		this.formatComboBox = new SubtitleFormatComboBox(subtitleFormatComboBox, fixedSubtitleType, null);
+	}
 	
 	private void SetTitle () {
 		if (textType == SubtitleTextType.Text)
@@ -129,6 +132,15 @@ public abstract class SubtitleFileSaveAsDialog : GladeDialog {
 			return -1;
 		}
 	}
+
+	private SubtitleType GetFixedSubtitleType () {
+		try {
+			return Base.Document.TextFile.SubtitleType;
+		}
+		catch (NullReferenceException) {
+			return SubtitleType.Unknown;
+		}
+	}
 	
 	private void UpdateContents () {
 		FileProperties fileProperties = (textType == SubtitleTextType.Text ? Base.Document.TextFile : Base.Document.TranslationFile);
@@ -143,57 +155,9 @@ public abstract class SubtitleFileSaveAsDialog : GladeDialog {
 		/* There seems to be a bug in GTK that makes the dialog return null for currentFolder and currentFilename
 		   while in the constructor. After constructing it works fine. */
 
-		SetActiveFormat();
 		SetActiveNewlineType();
 	}
-
-	private void FillFormatComboBox () {
-		subtitleTypes = Subtitles.AvailableTypesSorted;
-		
-		foreach (SubtitleTypeInfo typeInfo in subtitleTypes) {
-			formatComboBox.AppendText(typeInfo.Name + " (" + typeInfo.ExtensionsAsText + ")");
-		}	
-	}
-
-	private void SetActiveFormat () {
-		SubtitleType subtitleType = Base.Document.TextFile.SubtitleType; //The type of the subtitle file
-		int position = FindSubtitleTypePosition(subtitleType);
-		if (position != -1) {
-			formatComboBox.Active = position;
-			return;
-		}
-		
-		/* The current subtitle type was not found, trying the most common based on the TimingMode */
-		TimingMode timingMode = Base.TimingMode;
-		
-		/* If timing mode is Frames, set to MicroDVD */
-		if (timingMode == TimingMode.Frames) {
-			position = FindSubtitleTypePosition(SubtitleType.MicroDVD);
-			if (position != -1) {
-				formatComboBox.Active = position;
-				return;
-			}
-		}
-		
-		/* If SubRip subtitle type is found, use it */
-		position = FindSubtitleTypePosition(SubtitleType.SubRip);
-		if (position != -1) {
-			formatComboBox.Active = position;
-			return;
-		}
-		
-		/* All options tried to no aval, selecting the first */
-		formatComboBox.Active = 0;
-	}
 	
-	private int FindSubtitleTypePosition (SubtitleType type) {
-		for (int position = 0 ; position < subtitleTypes.Length ; position++) {
-			SubtitleType current = subtitleTypes[position].Type;
-			if (current == type)
-				return position;
-		}
-		return -1;
-	}
 	
 	private string UpdateFilenameExtension (string filename, SubtitleType type) {
 		SubtitleTypeInfo typeInfo = Subtitles.GetAvailableType(type);
@@ -206,7 +170,7 @@ public abstract class SubtitleFileSaveAsDialog : GladeDialog {
 			return filename;
 		else if (index == -1) //filename doesn't have an extension, appending
 			return filename + newExtensionDotted;
-		else if (IsSubtitleExtension(extensionDotted))  { //filename's extension is a subtitle extension
+		else if (Subtitles.IsSubtitleExtension(extensionDotted))  { //filename's extension is a subtitle extension
 			int dotIndex = index - 1;
 			return filename.Substring(0, dotIndex) + newExtensionDotted;
 		}
@@ -226,15 +190,6 @@ public abstract class SubtitleFileSaveAsDialog : GladeDialog {
 			return filename + "." + typeInfo.PreferredExtension;
 	}
 	
-	private bool IsSubtitleExtension (string dottedExtension) {
-		string extension = dottedExtension.Substring(1); //Remove the starting dot
-		foreach (SubtitleTypeInfo type in subtitleTypes) {
-			if (type.HasExtension(extension))
-				return true;
-		}
-		return false;
-	}
-
 	/// <summary>Returns the extension for the specified filename.</summary>
 	private string GetFilenameExtension (string filename, out int index) {
 		int dotIndex = filename.LastIndexOf('.');
@@ -329,34 +284,38 @@ public abstract class SubtitleFileSaveAsDialog : GladeDialog {
 
 	protected override bool ProcessResponse (ResponseType response) {
 		if (response == ResponseType.Ok) {
-			int formatIndex = formatComboBox.Active;
-			chosenSubtitleType = subtitleTypes[formatIndex].Type;
-			chosenFilename = AddExtensionIfNeeded(chosenSubtitleType);
 
+			/* Check chosen encoding */
 			chosenEncoding = encodingComboBox.ChosenEncoding;
 			if (Base.Config.PrefsDefaultsFileSaveEncodingOption == ConfigFileSaveEncodingOption.RememberLastUsed) {
 				int activeAction = encodingComboBox.ActiveSelection;
-				System.Console.WriteLine("Active action: " + activeAction);
 				ConfigFileSaveEncoding activeOption = (ConfigFileSaveEncoding)Enum.ToObject(typeof(ConfigFileSaveEncoding), activeAction);
 				if (((int)activeOption) >= ((int)ConfigFileSaveEncoding.Fixed)) {
-					System.Console.WriteLine("Chosen encoding: " + chosenEncoding.Name);
 					Base.Config.PrefsDefaultsFileSaveEncodingFixed = chosenEncoding.Name;
 				}
 				else {
-					System.Console.WriteLine("Active option: " + activeOption);
 					Base.Config.PrefsDefaultsFileSaveEncoding = activeOption;
 				}
 			}
 
+			/* Check chosen subtitle format */
+			chosenSubtitleType = formatComboBox.ChosenSubtitleType;
+			if (Base.Config.PrefsDefaultsFileSaveFormatOption == ConfigFileSaveFormatOption.RememberLastUsed) {
+				Base.Config.PrefsDefaultsFileSaveFormatFixed = chosenSubtitleType;
+			}
+
+			/* Check chosen newline type */
 			chosenNewlineType = GetChosenNewlineType();
 
+			/* Check chosen filename */
+			chosenFilename = AddExtensionIfNeeded(chosenSubtitleType);
+
 			SetReturnValue(true);
 		}
 		return false;
 	}
 
 	private void OnFormatChanged (object o, EventArgs args) {
-		SubtitleType type = subtitleTypes[formatComboBox.Active].Type;
 		string filename = dialog.Filename;
 		if ((filename == null) || (filename == String.Empty))
 			return;
@@ -366,7 +325,8 @@ public abstract class SubtitleFileSaveAsDialog : GladeDialog {
 			filename = filename.Substring(folder.Length + 1);
 		}
 
-		filename = UpdateFilenameExtension(filename, type);
+		SubtitleType subtitleType = formatComboBox.ChosenSubtitleType;
+		filename = UpdateFilenameExtension(filename, subtitleType);
 		dialog.CurrentName = filename;
 	}
 
diff --git a/src/GnomeSubtitles/Ui/Component/EncodingComboBox.cs b/src/GnomeSubtitles/Ui/Component/EncodingComboBox.cs
index a09120f..aa2e5f7 100644
--- a/src/GnomeSubtitles/Ui/Component/EncodingComboBox.cs
+++ b/src/GnomeSubtitles/Ui/Component/EncodingComboBox.cs
@@ -52,6 +52,11 @@ public class EncodingComboBox {
 
 	public EncodingComboBox (ComboBox comboBox, bool hasAutoDetect) : this(comboBox, hasAutoDetect, null, -1) {
 	}
+
+
+	/* Events */
+	
+	public event EventHandler SelectionChanged;
 	
 
 	/* Public properties */
@@ -88,10 +93,6 @@ public class EncodingComboBox {
 		set { SetActiveItem(value, false); }
 	}
 
-	/* Events */
-	
-	public event EventHandler SelectionChanged;
-
 
 	/* Private members */
 
diff --git a/src/GnomeSubtitles/Ui/Component/SubtitleFormatComboBox.cs b/src/GnomeSubtitles/Ui/Component/SubtitleFormatComboBox.cs
new file mode 100644
index 0000000..c4672f7
--- /dev/null
+++ b/src/GnomeSubtitles/Ui/Component/SubtitleFormatComboBox.cs
@@ -0,0 +1,164 @@
+/*
+ * This file is part of Gnome Subtitles.
+ * Copyright (C) 2006-2010 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
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Gnome Subtitles 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 Gtk;
+using SubLib.Core.Domain;
+using System;
+
+namespace GnomeSubtitles.Ui.Component {
+
+public class SubtitleFormatComboBox {
+
+	private ComboBox comboBox = null;
+	private SubtitleTypeInfo[] subtitleTypes = null;
+	private string[] additionalActions = null; //
+	private SubtitleType fixedSubtitleType = SubtitleType.Unknown; //A subtitle type that must be selected
+
+	public SubtitleFormatComboBox (ComboBox comboBox, SubtitleType fixedSubtitleType, string[] additionalActions) {
+		this.comboBox = comboBox;
+		this.fixedSubtitleType = fixedSubtitleType;
+		this.additionalActions = additionalActions;
+
+		InitComboBoxModel();
+		SetComboBox();
+		ConnectHandlers();
+	}
+
+	/* Events */
+
+	public event EventHandler SelectionChanged;
+
+
+	/* Public properties */
+
+	public bool HasChosenAction {
+		get { return comboBox.Active < GetActionCount(); }
+	}
+
+	public int ChosenAction {
+		get { return (HasChosenAction ? comboBox.Active : -1); }
+	}
+
+	public SubtitleType ChosenSubtitleType {
+		get {
+			int active = comboBox.Active;
+			int actionCount = GetActionCount();
+			if (active < actionCount) //An action is active
+				return SubtitleType.Unknown;
+			else
+				return subtitleTypes[active - (actionCount > 0 ? actionCount + 1 : 0)].Type; //1 for break line
+		}
+	}
+
+	public int ActiveSelection {
+		get { return comboBox.Active; }
+		set { SetActiveItem(value, false); }
+	}
+
+
+	/* Private members */
+
+	private void InitComboBoxModel () {
+		ComboBoxUtil.InitComboBox(comboBox);
+	}
+
+	private void SetComboBox () {
+		subtitleTypes = Subtitles.AvailableTypesSorted;
+		FillComboBox();
+	}
+
+	private void FillComboBox () {
+		DisconnectComboBoxChangedSignal();
+
+		(comboBox.Model as ListStore).Clear();
+
+		int currentItem = 0;
+		int activeItem = 0;
+
+		/* Add additional actions */
+		if (additionalActions != null) {
+			foreach (string additionalAction in additionalActions) {
+				comboBox.AppendText(additionalAction);
+				currentItem++;
+			}
+		}
+
+		if (currentItem != 0) {
+			comboBox.AppendText("-");
+			currentItem++;
+		}
+
+		/* Add subtitle formats */
+		foreach (SubtitleTypeInfo typeInfo in subtitleTypes) {
+			comboBox.AppendText(typeInfo.Name + " (" + typeInfo.ExtensionsAsText + ")");
+			if (typeInfo.Type == fixedSubtitleType) {
+				activeItem = currentItem;
+			}
+			currentItem++;
+		}
+
+		SetActiveItem(activeItem, false); //Don't use silent change because the signal is already disabled
+
+		ConnectComboBoxChangedSignal();
+	}
+
+	private void SetActiveItem (int item, bool silent) {
+		int itemCount = comboBox.Model.IterNChildren();
+		if (itemCount == 0)
+			return;
+
+		if (silent)
+			DisconnectComboBoxChangedSignal();
+
+		comboBox.Active = item;
+
+		if (silent)
+			ConnectComboBoxChangedSignal();
+	}
+
+	private int GetActionCount () {
+		return (additionalActions != null ? additionalActions.Length : 0);
+	}
+
+
+	/* Event members */
+	
+	#pragma warning disable 169		//Disables warning about handlers not being used
+
+	private void ConnectHandlers () {
+		comboBox.RowSeparatorFunc = ComboBoxUtil.SeparatorFunc;
+	}
+
+	private void ConnectComboBoxChangedSignal () {
+		comboBox.Changed += OnComboBoxChanged;
+	}
+
+	private void DisconnectComboBoxChangedSignal () {
+		comboBox.Changed -= OnComboBoxChanged;
+	}
+
+	private void OnComboBoxChanged (object o, EventArgs args) {
+		if (SelectionChanged != null) {
+			SelectionChanged(o, args);
+		}
+	}
+
+}
+
+}
diff --git a/src/SubLib/Core/Domain/Subtitles.cs b/src/SubLib/Core/Domain/Subtitles.cs
index bdfb7e9..82df589 100644
--- a/src/SubLib/Core/Domain/Subtitles.cs
+++ b/src/SubLib/Core/Domain/Subtitles.cs
@@ -1,6 +1,6 @@
 /*
  * This file is part of SubLib.
- * Copyright (C) 2005-2008 Pedro Castro
+ * Copyright (C) 2005-2010 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
@@ -31,6 +31,10 @@ namespace SubLib.Core.Domain {
 public class Subtitles {
 	private SubtitleCollection collection = null;
 	private SubtitleProperties properties = null;
+
+	/* Variable cache */
+	private static SubtitleTypeInfo[] availableTypes = null;
+	private static SubtitleTypeInfo[] availableTypesSorted = null;
 	
 	/// <summary>A collection which contains the subtitles.</summary>
 	public SubtitleCollection Collection {
@@ -47,21 +51,27 @@ public class Subtitles {
 	/// <summary>Information about the available subtitle types.</summary>
 	public static SubtitleTypeInfo[] AvailableTypes {
 		get {
-			SubtitleFormat[] formats = BuiltInSubtitleFormats.SubtitleFormats;
-			SubtitleTypeInfo[] types = new SubtitleTypeInfo[formats.Length];
-			for (int count = 0 ; count < formats.Length ; count++)
-				types[count] = new SubtitleTypeInfo(formats[count]);
-
-			return types;	
+			if (availableTypes == null) {
+				SubtitleFormat[] formats = BuiltInSubtitleFormats.SubtitleFormats;
+				SubtitleTypeInfo[] types = new SubtitleTypeInfo[formats.Length];
+				for (int index = 0 ; index < formats.Length ; index++) {
+					types[index] = new SubtitleTypeInfo(formats[index]);
+				}
+				availableTypes = types;
+			}
+			return availableTypes;
 		}
 	}
 
 	/// <summary>Information about the available subtitle types, sorted by their names.</summary>
 	public static SubtitleTypeInfo[] AvailableTypesSorted {
 		get {
-			SubtitleTypeInfo[] types = AvailableTypes;
-			Array.Sort(types);
-			return types;	
+			if (availableTypesSorted == null) {
+				SubtitleTypeInfo[] types = AvailableTypes;
+				Array.Sort(types);
+				availableTypesSorted = types;
+			}
+			return availableTypesSorted;	
 		}
 	}
 	
@@ -72,6 +82,16 @@ public class Subtitles {
 		SubtitleFormat format = BuiltInSubtitleFormats.GetFormat(type);
 		return new SubtitleTypeInfo(format);
 	}
+
+	public static bool IsSubtitleExtension (string dottedExtension) {
+		string extension = dottedExtension.Substring(1); //Remove the starting dot
+		foreach (SubtitleTypeInfo type in AvailableTypes) {
+			if (type.HasExtension(extension))
+				return true;
+		}
+		return false;
+	}
+
 	
 	public override string ToString(){
 		return Collection.ToString() + "\n-------------------------------------------\n" + Properties.ToString();
diff --git a/src/SubLib/IO/SubtitleFormats/SubtitleFormat.cs b/src/SubLib/IO/SubtitleFormats/SubtitleFormat.cs
index 9211f7e..d8caf03 100644
--- a/src/SubLib/IO/SubtitleFormats/SubtitleFormat.cs
+++ b/src/SubLib/IO/SubtitleFormats/SubtitleFormat.cs
@@ -1,6 +1,6 @@
 /*
  * This file is part of SubLib.
- * Copyright (C) 2005-2008 Pedro Castro
+ * Copyright (C) 2005-2010 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



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