Fixed issues opened by Pedro Castro int he firswt apply iteration
[gn-sub.git] / src / GnomeSubtitles / Core / Command / TranslatorCommand.cs
blob7a4e4ad2fc6aed85dc8552c9561e8bc85a786437
1 /*
2 * This file is part of Gnome Subtitles.
3 * Copyright (C) 2007-2008 Pedro Castro
5 * Gnome Subtitles is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * Gnome Subtitles is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 using GnomeSubtitles.Ui.Edit;
21 using Mono.Unix;
22 using SubLib;
23 using SubLib.Core;
24 using SubLib.Exceptions;
25 using System;
26 using GnomeSubtitles.Dialog;
27 using GnomeSubtitles.Dialog.Unmanaged;
28 using Gtk;
30 namespace GnomeSubtitles.Core.Command {
32 public class TranslatorCommand : FixedSingleSelectionCommand {
33 private string translatedText; // translated string
34 private string formerText; // former text replaced by translation
35 private bool leftToRight; // direction of translating
36 private static string description = Catalog.GetString("Translating text using Google Translate.");
38 public TranslatorCommand (bool leftToRight) : base(description, false, false) {
39 this.translatedText = String.Empty;
40 this.formerText = String.Empty;
41 this.leftToRight = leftToRight;
43 SetStopsGrouping(true);
46 /* Public members */
48 public override bool CanGroupWith (Command command) {
49 return default(bool);
52 public override Command MergeWith (Command command) {
53 return this;
56 protected override bool ChangeValues () {
57 if (Base.Ui.View.Selection.Count != 1)
58 return false;
60 if (leftToRight)
61 formerText = Base.Ui.View.Selection.Subtitle.Translation.Get();
62 else
63 formerText = Base.Ui.View.Selection.Subtitle.Text.Get();
65 try
67 if (!Base.SpellLanguages.HasActiveTextLanguage)
69 Base.Dialogs.Get(typeof(SetTextLanguageDialog)).Show();
70 if (!Base.SpellLanguages.HasActiveTextLanguage)
71 return false;
73 if (!Base.SpellLanguages.HasActiveTranslationLanguage)
75 Base.Dialogs.Get(typeof(SetTranslationLanguageDialog)).Show();
76 if (!Base.SpellLanguages.HasActiveTranslationLanguage)
77 return false;
80 string translated;
81 if (leftToRight)
82 translated = Translator.TranslateText(Base.Ui.View.Selection.Subtitle.Text.Get(), /*"en_US", "de_DE",*/ Base.SpellLanguages.ActiveTextLanguage.ID, Base.SpellLanguages.ActiveTranslationLanguage.ID, Translator.TIMEOUT);
83 else
84 translated = Translator.TranslateText(Base.Ui.View.Selection.Subtitle.Translation.Get(), Base.SpellLanguages.ActiveTextLanguage.ID, Base.SpellLanguages.ActiveTranslationLanguage.ID, Translator.TIMEOUT);
86 translatedText = translated;
87 if (leftToRight)
88 Base.Ui.View.Selection.Subtitle.Translation.Set(translated);
89 else
90 Base.Ui.View.Selection.Subtitle.Text.Set(translated);
92 return true;
94 catch (TranslatorException e)
96 Gtk.MessageDialog md = new Gtk.MessageDialog(Base.Ui.Window,
97 DialogFlags.DestroyWithParent,
98 MessageType.Error,
99 ButtonsType.Close, e.Message);
100 md.Run();
101 md.Destroy();
103 return false;
107 public override void Undo () {
108 if (leftToRight)
109 Base.Ui.View.Selection.Subtitle.Translation.Set(formerText);
110 else
111 Base.Ui.View.Selection.Subtitle.Text.Set(formerText);
113 PostProcess();
116 public override void Redo () {
117 if (leftToRight)
118 Base.Ui.View.Selection.Subtitle.Translation.Set(translatedText);
119 else
120 Base.Ui.View.Selection.Subtitle.Text.Set(translatedText);
122 PostProcess();
125 /* Protected members */
127 protected override void PostProcess () {
128 Base.Ui.View.Selection.Reselect();
129 Base.Ui.View.Refresh();
133 protected bool GetLeftToRight () {
134 return leftToRight;
137 protected string GetTranslatedText () {
138 return translatedText;
141 protected string GetFormerText () {
142 return formerText;