[cosmetics] update date in GPL header
[vuplus_xbmc] / tools / Translator / Translator / TranslationManager.cs
1 /*
2  *      Copyright (C) 2006-2013 Team XBMC
3  *      http://www.xbmc.org
4  *
5  *  This Program 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, or (at your option)
8  *  any later version.
9  *
10  *  This Program 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.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, write to
17  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18  *  http://www.gnu.org/copyleft/gpl.html
19  *
20  */
21
22 using System;
23 using System.IO;
24 using System.Collections;
25
26 namespace TeamXBMC.TranslatorCore
27 {
28         /// <summary>
29         /// Object to communicate with the gui.
30         /// </summary>
31         public sealed class TranslationManager
32         {
33                 private TranslatorArray strings = new TranslatorArray();
34                 private static TranslationManager instance=null;
35
36                 private TranslationManager()
37                 {
38
39                 }
40
41                 /// <summary>
42                 /// Loads the current language
43                 /// </summary>
44                 public void Initialize()
45                 {
46                         strings.Clear();
47                         strings.Load();
48                 }
49
50                 #region Language file processing
51
52                 /// <summary>
53                 /// Create a new language
54                 /// </summary>
55                 public void CreateLanguage(string name)
56                 {
57                         if (File.Exists(Settings.Instance.LanguageFolder+@"\"+name+@"\strings.xml"))
58                                 throw new TranslatorException("The language \""+name+"\" already exists.");
59
60                         try
61                         {
62                                 Directory.CreateDirectory(Settings.Instance.LanguageFolder+@"\"+name);
63                         }
64                         catch (Exception e)
65                         {
66                                 throw new TranslatorException("Unable to create the directory "+Settings.Instance.LanguageFolder+@"\"+name, e);
67                         }
68
69
70                         // Save an empty strings.xml file
71                         StringArray stringsNew=new StringArray();
72                         stringsNew.Save(Settings.Instance.LanguageFolder+@"\"+name+@"\strings.xml");
73
74                         LanguageInfo langinfo=new LanguageInfo();
75                         langinfo.Save(Settings.Instance.LanguageFolder+@"\"+name+@"\langinfo.xml");
76                 }
77
78                 /// <summary>
79                 /// Save the current language
80                 /// </summary>
81                 public void SaveTranslated()
82                 {
83                         strings.Save();
84                 }
85
86                 #endregion
87
88                 #region Properties
89
90                 /// <summary>
91                 /// Gets the instance of the TranslationManager
92                 /// </summary>
93                 public static TranslationManager Instance
94                 {
95                         get
96                         {
97                                 if (instance==null)
98                                 {
99                                         instance=new TranslationManager();
100                                 }
101
102                                 return instance;
103                         }
104                 }
105
106                 /// <summary>
107                 /// Get the strings of the current language with the state translated
108                 /// </summary>
109                 public TranslatorArray Translated
110                 {
111                         get 
112                         {
113                                 strings.Enumerator=TranslatorArrayEnumerator.Translated;
114                                 return strings;
115                         }
116                 }
117
118                 /// <summary>
119                 /// Get the strings of the current language with the state untranslated
120                 /// </summary>
121                 public TranslatorArray Untranslated
122                 {
123                         get
124                         {
125                                 strings.Enumerator=TranslatorArrayEnumerator.Untranslated;
126                                 return strings;
127                         }
128                 }
129
130                 /// <summary>
131                 /// Get the strings of the current language with the state changed
132                 /// </summary>
133                 public TranslatorArray Changed
134                 {
135                         get
136                         {
137                                 strings.Enumerator=TranslatorArrayEnumerator.Changed;
138                                 return strings;
139                         }
140                 }
141
142                 /// <summary>
143                 /// Get all strings of the current language
144                 /// </summary>
145                 public TranslatorArray All
146                 {
147                         get
148                         {
149                                 strings.Enumerator=TranslatorArrayEnumerator.All;
150                                 return strings;
151                         }
152                 }
153
154                 /// <summary>
155                 /// Get the name of the current language
156                 /// </summary>
157                 public string LanguageTranslated
158                 {
159                         get { return Settings.Instance.Language; }
160                 }
161
162                 /// <summary>
163                 /// Get the name of the master language
164                 /// </summary>
165                 public string LanguageOriginal
166                 {
167                         get { return Settings.Instance.LanguageOriginal; }
168                 }
169
170                 /// <summary>
171                 /// Get all available languages
172                 /// </summary>
173                 public string[] Languages
174                 {
175                         get
176                         {
177                                 string root=Settings.Instance.LanguageFolder;
178                                 string[] dirs=Directory.GetDirectories(root);
179                                 ArrayList languages=new ArrayList();
180
181                                 foreach (string dir in dirs)
182                                 {
183                                         if (File.Exists(dir+@"\strings.xml"))
184                                         {
185                                                 // Extract language name from path
186                                                 string language=dir.Substring(root.Length+1, dir.Length-root.Length-1);
187                                                 if (!language.Equals(Settings.Instance.LanguageOriginal) )
188                                                         languages.Add(language);
189                                         }
190                                 }
191
192                                 return (string[])languages.ToArray(typeof(string));
193                         }
194                 }
195
196                 /// <summary>
197                 /// Returns true if the currently edited language file is has been changed by the user
198                 /// </summary>
199                 public bool IsModified
200                 {
201                         get { return strings.IsModified; }
202                 }
203
204                 #endregion
205         }
206 }