1 /*
2   KeePass Password Safe - The Open-Source Password Manager
3   Copyright (C) 2003-2021 Dominik Reichl <dominik.reichl@t-online.de>
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 of the License, or
8   (at your option) 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 this program; if not, write to the Free Software
17   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18 */
19 
20 using System;
21 using System.Collections.Generic;
22 using System.Diagnostics;
23 using System.IO;
24 using System.IO.Compression;
25 using System.Text;
26 using System.Windows.Forms;
27 using System.Xml;
28 
29 using KeePassLib.Utility;
30 
31 using TrlUtil.App;
32 using TrlUtil.App.Configuration;
33 
34 namespace TrlUtil
35 {
36 	public static class Program
37 	{
38 		private static TceConfig m_cfg = null;
39 		public static TceConfig Config
40 		{
41 			get
42 			{
43 				if(m_cfg == null) { Debug.Assert(false); m_cfg = new TceConfig(); }
44 				return m_cfg;
45 			}
46 		}
47 
48 		[STAThread]
Main(string[] args)49 		public static void Main(string[] args)
50 		{
51 			try
52 			{
53 				Application.EnableVisualStyles();
54 				Application.SetCompatibleTextRenderingDefault(false);
55 
56 				KeePass.Program.EnableTranslation = false; // We need English
57 				if(!KeePass.Program.CommonInit()) return;
58 
59 				m_cfg = (TceConfig.Load() ?? new TceConfig());
60 
61 				MainPriv(args);
62 			}
63 			catch(Exception ex)
64 			{
65 				MessageBox.Show(ex.Message, TuDefs.ProductName,
66 					MessageBoxButtons.OK, MessageBoxIcon.Warning);
67 			}
68 
69 			TceConfig.Save(m_cfg);
70 
71 			try { KeePass.Program.CommonTerminate(); }
72 			catch(Exception) { Debug.Assert(false); }
73 		}
74 
MainPriv(string[] args)75 		private static void MainPriv(string[] args)
76 		{
77 			if((args != null) && (args.Length == 2))
78 			{
79 				ExecuteCmd(args[0], args[1]);
80 				return;
81 			}
82 
83 			Application.Run(new MainForm());
84 		}
85 
ExecuteCmd(string strCmd, string strFile)86 		private static void ExecuteCmd(string strCmd, string strFile)
87 		{
88 			if(strCmd == "convert_resx")
89 			{
90 				StreamWriter swOut = new StreamWriter(strFile + ".lng.xml",
91 					false, new UTF8Encoding(false));
92 
93 				XmlDocument xmlIn = XmlUtilEx.CreateXmlDocument();
94 				xmlIn.Load(strFile);
95 
96 				foreach(XmlNode xmlChild in xmlIn.DocumentElement.ChildNodes)
97 				{
98 					if(xmlChild.Name != "data") continue;
99 
100 					swOut.Write("<Data Name=\"" + xmlChild.Attributes["name"].Value +
101 						"\">\r\n\t<Value>" + xmlChild.SelectSingleNode("value").InnerXml +
102 						"</Value>\r\n</Data>\r\n");
103 				}
104 
105 				swOut.Close();
106 			}
107 			/* else if(strCmd == "compress")
108 			{
109 				byte[] pbData = File.ReadAllBytes(strFile);
110 
111 				FileStream fs = new FileStream(strFile + ".lngx", FileMode.Create,
112 					FileAccess.Write, FileShare.None);
113 				GZipStream gz = new GZipStream(fs, CompressionMode.Compress);
114 
115 				gz.Write(pbData, 0, pbData.Length);
116 				gz.Close();
117 				fs.Close();
118 			} */
119 			else if(strCmd == "src_from_xml")
120 			{
121 				XmlDocument xmlIn = XmlUtilEx.CreateXmlDocument();
122 				xmlIn.Load(strFile);
123 
124 				foreach(XmlNode xmlTable in xmlIn.DocumentElement.SelectNodes("StringTable"))
125 				{
126 					StreamWriter swOut = new StreamWriter(xmlTable.Attributes["Name"].Value +
127 						".Generated.cs", false, new UTF8Encoding(false));
128 
129 					swOut.WriteLine("// This is a generated file!");
130 					swOut.WriteLine("// Do not edit manually, changes will be overwritten.");
131 					swOut.WriteLine();
132 					swOut.WriteLine("using System;");
133 					swOut.WriteLine("using System.Collections.Generic;");
134 					swOut.WriteLine();
135 					swOut.WriteLine("namespace " + xmlTable.Attributes["Namespace"].Value);
136 					swOut.WriteLine("{");
137 					swOut.WriteLine("\t/// <summary>");
138 					swOut.WriteLine("\t/// A strongly-typed resource class, for looking up localized strings, etc.");
139 					swOut.WriteLine("\t/// </summary>");
140 					swOut.WriteLine("\tpublic static partial class " + xmlTable.Attributes["Name"].Value);
141 					swOut.WriteLine("\t{");
142 
143 					swOut.WriteLine("\t\tprivate static string TryGetEx(Dictionary<string, string> dictNew,");
144 					swOut.WriteLine("\t\t\tstring strName, string strDefault)");
145 					swOut.WriteLine("\t\t{");
146 					swOut.WriteLine("\t\t\tstring strTemp;");
147 					swOut.WriteLine();
148 					swOut.WriteLine("\t\t\tif(dictNew.TryGetValue(strName, out strTemp))");
149 					swOut.WriteLine("\t\t\t\treturn strTemp;");
150 					swOut.WriteLine();
151 					swOut.WriteLine("\t\t\treturn strDefault;");
152 					swOut.WriteLine("\t\t}");
153 					swOut.WriteLine();
154 
155 					swOut.WriteLine("\t\tpublic static void SetTranslatedStrings(Dictionary<string, string> dictNew)");
156 					swOut.WriteLine("\t\t{");
157 					swOut.WriteLine("\t\t\tif(dictNew == null) throw new ArgumentNullException(\"dictNew\");");
158 					swOut.WriteLine();
159 
160 #if DEBUG
161 					string strLastName = string.Empty;
162 #endif
163 					foreach(XmlNode xmlData in xmlTable.SelectNodes("Data"))
164 					{
165 						string strName = xmlData.Attributes["Name"].Value;
166 
167 						swOut.WriteLine("\t\t\tm_str" + strName +
168 							" = TryGetEx(dictNew, \"" + strName +
169 							"\", m_str" + strName + ");");
170 
171 #if DEBUG
172 						Debug.Assert((string.Compare(strLastName, strName, true) < 0),
173 							"Data names not sorted: " + strLastName + " - " + strName + ".");
174 						strLastName = strName;
175 #endif
176 					}
177 
178 					swOut.WriteLine("\t\t}");
179 					swOut.WriteLine();
180 
181 					swOut.WriteLine("\t\tprivate static readonly string[] m_vKeyNames = {");
182 					XmlNodeList xNodes = xmlTable.SelectNodes("Data");
183 					for(int i = 0; i < xNodes.Count; ++i)
184 					{
185 						XmlNode xmlData = xNodes.Item(i);
186 						swOut.WriteLine("\t\t\t\"" + xmlData.Attributes["Name"].Value +
187 							"\"" + ((i != xNodes.Count - 1) ? "," : string.Empty));
188 					}
189 
190 					swOut.WriteLine("\t\t};");
191 					swOut.WriteLine();
192 
193 					swOut.WriteLine("\t\tpublic static string[] GetKeyNames()");
194 					swOut.WriteLine("\t\t{");
195 					swOut.WriteLine("\t\t\treturn m_vKeyNames;");
196 					swOut.WriteLine("\t\t}");
197 
198 					foreach(XmlNode xmlData in xmlTable.SelectNodes("Data"))
199 					{
200 						string strName = xmlData.Attributes["Name"].Value;
201 						string strValue = xmlData.SelectSingleNode("Value").InnerText;
202 						if(strValue.Contains("\""))
203 						{
204 							// Console.WriteLine(strValue);
205 							strValue = strValue.Replace("\"", "\"\"");
206 						}
207 
208 						swOut.WriteLine();
209 						swOut.WriteLine("\t\tprivate static string m_str" +
210 							strName + " =");
211 						swOut.WriteLine("\t\t\t@\"" + strValue + "\";");
212 
213 						swOut.WriteLine("\t\t/// <summary>");
214 						swOut.WriteLine("\t\t/// Look up a localized string similar to");
215 						swOut.WriteLine("\t\t/// '" + StrUtil.StringToHtml(strValue) + "'.");
216 						swOut.WriteLine("\t\t/// </summary>");
217 						swOut.WriteLine("\t\tpublic static string " +
218 							strName);
219 						swOut.WriteLine("\t\t{");
220 						swOut.WriteLine("\t\t\tget { return m_str" + strName +
221 							"; }");
222 						swOut.WriteLine("\t\t}");
223 					}
224 
225 					swOut.WriteLine("\t}"); // Close class
226 					swOut.WriteLine("}");
227 
228 					swOut.Close();
229 				}
230 			}
231 		}
232 	}
233 }
234