private void DownloadTranslations(object sender, EventArgs e) { if (dataGridView_translations.SelectedRows.Count == 0) { MessageBox.Show( Translation.GetTranslation("TL_NO_TLS_SELECTED"), Translation.GetTranslation("TL_NO_TLS_SELECTED_TITLE"), MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } string translationsPath = Path.Combine(MaidFiddler.DATA_PATH, Translation.TRANSLATIONS_PATH); LoadingBarGUI loadingBarGui = new LoadingBarGUI( Translation.GetTranslation("LOADING"), $"{Translation.GetTranslation("TL_TRANSLATION_DOWNLOAD")}", true, g => { g.Timer.Start(); Thread downloaderThread = new Thread( () => { foreach (DataGridViewRow selectedRow in dataGridView_translations.SelectedRows) { string tlFileName = langCodes[selectedRow.Index]; g.TextLabel.Text = $"{Translation.GetTranslation("TL_TRANSLATION_DOWNLOAD")} {selectedRow.Cells[0]}"; Debugger.WriteLine(LogLevel.Info, $"Downloading language ID {selectedRow.Index}"); try { HttpWebRequest webRequest = (HttpWebRequest) WebRequest.Create($"{MaidFiddler.RESOURCE_URL}/Resources/Translations/{tlFileName}.txt"); Debugger.WriteLine( LogLevel.Info, $"Getting translation file from {MaidFiddler.RESOURCE_URL}/Resources/Translations/{tlFileName}.txt"); HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse(); Debugger.WriteLine(LogLevel.Info, "Got response!"); Debugger.WriteLine(LogLevel.Info, $"Response: {response.StatusCode}"); if (response.StatusCode == HttpStatusCode.NotFound) { MessageBox.Show( "Failed to retreive translation: File not found.", "Boop!", MessageBoxButtons.OK, MessageBoxIcon.Error); g.DialogResult = DialogResult.Abort; g.Timer.Stop(); g.Close(); return; } Stream s = response.GetResponseStream(); Debugger.WriteLine(LogLevel.Info, "Reading response"); byte[] responseBuffer = new byte[1024]; List <byte> stringBuffer = new List <byte>(); int read; do { read = s.Read(responseBuffer, 0, responseBuffer.Length); stringBuffer.AddRange(responseBuffer, 0, read); } while (read > 0); using (TextWriter tw = File.CreateText(Path.Combine(translationsPath, $"{tlFileName}.txt"))) tw.Write(Encoding.UTF8.GetString(stringBuffer.ToArray())); } catch (WebException we) { MessageBox.Show( $"Failed to retreive translation.\nResponse: {we.Message}", "Boop!", MessageBoxButtons.OK, MessageBoxIcon.Error); g.DialogResult = DialogResult.Abort; g.Timer.Stop(); g.Close(); } catch (Exception ex) { MessageBox.Show( $"Could not download the translation.\nInfo: {ex}", "Boop!", MessageBoxButtons.OK, MessageBoxIcon.Error); g.DialogResult = DialogResult.Abort; g.Timer.Stop(); g.Close(); } } g.DialogResult = DialogResult.OK; g.Timer.Stop(); g.Close(); }); downloaderThread.Start(); }); DialogResult result = loadingBarGui.ShowDialog(this); loadingBarGui.Dispose(); if (result != DialogResult.OK) { return; } MessageBox.Show( Translation.GetTranslation("TL_DOWNLOAD_DONE"), Translation.GetTranslation("TL_DOWNLOAD_DONE_TITLE"), MessageBoxButtons.OK); InitLanguageTable(); }
private void OpenTranslationDownloadUrl(object sender, EventArgs e) { Uri uri = null; TextDialog tdURL = new TextDialog( Translation.GetTranslation("TL_URL_TITLE"), Translation.GetTranslation("TL_URL_TEXT"), string.Empty, s => { try { uri = new Uri(s); return uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps; } catch (Exception) { return false; } }, Translation.GetTranslation("OK"), Translation.GetTranslation("CANCEL")); DialogResult promptDialog = tdURL.ShowDialog(this); tdURL.Dispose(); if (promptDialog != DialogResult.OK) return; string translationsPath = Path.Combine(MaidFiddler.DATA_PATH, Translation.TRANSLATIONS_PATH); LoadingBarGUI loadingBarGui = new LoadingBarGUI( Translation.GetTranslation("LOADING"), Translation.GetTranslation("TL_URL_DOWNLOADING"), true, g => { HttpWebRequest webRequest = (HttpWebRequest) WebRequest.Create(uri); g.Timer.Start(); Debugger.WriteLine(LogLevel.Info, "Getting translation..."); webRequest.BeginGetResponse( ar => { try { HttpWebResponse response = (HttpWebResponse) webRequest.EndGetResponse(ar); Debugger.WriteLine(LogLevel.Info, "Got response!"); Debugger.WriteLine(LogLevel.Info, $"Response: {response.StatusCode}"); if (response.StatusCode == HttpStatusCode.NotFound) { MessageBox.Show( "Failed to download the translation: File not found", "Boop!", MessageBoxButtons.OK, MessageBoxIcon.Error); g.DialogResult = DialogResult.Abort; g.Timer.Stop(); g.Close(); return; } Stream s = response.GetResponseStream(); Debugger.WriteLine(LogLevel.Info, "Reading response"); StringBuilder sb = new StringBuilder(); byte[] responseBuffer = new byte[1024]; int read; do { read = s.Read(responseBuffer, 0, responseBuffer.Length); sb.Append(Encoding.UTF8.GetString(responseBuffer, 0, read)); } while (read > 0); using (TextReader tr = new StringReader(sb.ToString())) { if (!Translation.TagPattern.Match(tr.ReadLine()).Success) { Debugger.WriteLine(LogLevel.Error, "Failed to parse the translation: No tag found!"); MessageBox.Show( "The file does not contain the translation tag and thus cannot be recognised as a Maid Fiddler translation file.\nThe file has not been saved.", "Boop!", MessageBoxButtons.OK, MessageBoxIcon.Error); g.DialogResult = DialogResult.Abort; g.Timer.Stop(); g.Close(); return; } } string tlFileName = Path.GetFileNameWithoutExtension(uri.AbsolutePath); Debugger.WriteLine($"File name: {tlFileName}"); if (tlFileName == string.Empty || File.Exists(Path.Combine(translationsPath, $"{tlFileName}.txt"))) { char[] invalidChars = Path.GetInvalidFileNameChars(); TextDialog tdFileName = new TextDialog( Translation.GetTranslation("TL_NAME_CHANGE_TITLE"), Translation.GetTranslation("TL_NAME_CHANGE"), tlFileName, s1 => !s1.Any(c => invalidChars.Contains(c)), Translation.GetTranslation("OK"), Translation.GetTranslation("CANCEL")); if (tdFileName.ShowDialog(g) == DialogResult.OK) tlFileName = tdFileName.Input; if (tlFileName == string.Empty) tlFileName = FiddlerUtils.GenerateFileName(); tdFileName.Dispose(); } string path = Path.Combine(translationsPath, $"{tlFileName}.txt"); Debugger.WriteLine($"Writing translation to {path}"); using (TextWriter tw = File.CreateText(path)) tw.Write(sb.ToString()); g.DialogResult = DialogResult.OK; } catch (WebException we) { MessageBox.Show( $"Failed to retreive translation.\nResponse: {we.Message}", "Boop!", MessageBoxButtons.OK, MessageBoxIcon.Error); g.DialogResult = DialogResult.Abort; } catch (Exception ex) { MessageBox.Show( $"Unknown error occurred.\nInfo: {ex.ToString()}", "Boop!", MessageBoxButtons.OK, MessageBoxIcon.Error); g.DialogResult = DialogResult.Abort; } finally { g.Timer.Stop(); g.Close(); } }, null); }); DialogResult result = loadingBarGui.ShowDialog(this); loadingBarGui.Dispose(); if (result != DialogResult.OK) return; MessageBox.Show( Translation.GetTranslation("TL_DOWNLOAD_DONE"), Translation.GetTranslation("TL_DOWNLOAD_DONE_TITLE"), MessageBoxButtons.OK); LoadTranslations(Translation.CurrentTranslationFile); }
private void OpenTranslationDownloadGithub(object sender, EventArgs e) { string list = string.Empty; LoadingBarGUI loadingBarGui = new LoadingBarGUI( Translation.GetTranslation("LOADING"), Translation.GetTranslation("TL_LIST_LOADING"), true, g => { HttpWebRequest webRequest = (HttpWebRequest) WebRequest.Create($"{MaidFiddler.RESOURCE_URL}/Resources/Translations/translation_list.txt"); g.Timer.Start(); Debugger.WriteLine(LogLevel.Info, "Getting translation list..."); webRequest.BeginGetResponse( ar => { try { HttpWebResponse response = (HttpWebResponse) webRequest.EndGetResponse(ar); Debugger.WriteLine(LogLevel.Info, "Got response!"); Debugger.WriteLine(LogLevel.Info, $"Response: {response.StatusCode}"); if (response.StatusCode == HttpStatusCode.NotFound) { MessageBox.Show( "Failed to retreive translation list: List not found.", "Boop!", MessageBoxButtons.OK, MessageBoxIcon.Error); } Stream s = response.GetResponseStream(); Debugger.WriteLine(LogLevel.Info, "Reading response"); StringBuilder sb = new StringBuilder(); byte[] responseBuffer = new byte[1024]; int read; do { read = s.Read(responseBuffer, 0, responseBuffer.Length); sb.Append(Encoding.UTF8.GetString(responseBuffer, 0, read)); } while (read > 0); list = sb.ToString(); g.DialogResult = DialogResult.OK; } catch (WebException we) { MessageBox.Show( $"Failed to retreive translation list.\nResponse: {we.Message}", "Boop!", MessageBoxButtons.OK, MessageBoxIcon.Error); g.DialogResult = DialogResult.Abort; } catch (Exception ex) { MessageBox.Show( $"Unknown error occurred.\nInfo: {ex.ToString()}", "Boop!", MessageBoxButtons.OK, MessageBoxIcon.Error); g.DialogResult = DialogResult.Abort; } finally { g.Timer.Stop(); g.Close(); } }, null); }); DialogResult result = loadingBarGui.ShowDialog(this); loadingBarGui.Dispose(); if (result != DialogResult.OK) return; GithubTranslationsGUI tlGui = new GithubTranslationsGUI(list.Remove(0, 1)); tlGui.ShowDialog(this); tlGui.Dispose(); LoadTranslations(Translation.CurrentTranslationFile); }
private void DownloadTranslations(object sender, EventArgs e) { if (dataGridView_translations.SelectedRows.Count == 0) { MessageBox.Show( Translation.GetTranslation("TL_NO_TLS_SELECTED"), Translation.GetTranslation("TL_NO_TLS_SELECTED_TITLE"), MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } string translationsPath = Path.Combine(MaidFiddler.DATA_PATH, Translation.TRANSLATIONS_PATH); LoadingBarGUI loadingBarGui = new LoadingBarGUI( Translation.GetTranslation("LOADING"), $"{Translation.GetTranslation("TL_TRANSLATION_DOWNLOAD")}", true, g => { g.Timer.Start(); Thread downloaderThread = new Thread( () => { foreach (DataGridViewRow selectedRow in dataGridView_translations.SelectedRows) { string tlFileName = langCodes[selectedRow.Index]; g.TextLabel.Text = $"{Translation.GetTranslation("TL_TRANSLATION_DOWNLOAD")} {selectedRow.Cells[0]}"; Debugger.WriteLine(LogLevel.Info, $"Downloading language ID {selectedRow.Index}"); try { HttpWebRequest webRequest = (HttpWebRequest) WebRequest.Create($"{MaidFiddler.RESOURCE_URL}/Resources/Translations/{tlFileName}.txt"); Debugger.WriteLine( LogLevel.Info, $"Getting translation file from {MaidFiddler.RESOURCE_URL}/Resources/Translations/{tlFileName}.txt"); HttpWebResponse response = (HttpWebResponse) webRequest.GetResponse(); Debugger.WriteLine(LogLevel.Info, "Got response!"); Debugger.WriteLine(LogLevel.Info, $"Response: {response.StatusCode}"); if (response.StatusCode == HttpStatusCode.NotFound) { MessageBox.Show( "Failed to retreive translation: File not found.", "Boop!", MessageBoxButtons.OK, MessageBoxIcon.Error); g.DialogResult = DialogResult.Abort; g.Timer.Stop(); g.Close(); return; } Stream s = response.GetResponseStream(); Debugger.WriteLine(LogLevel.Info, "Reading response"); byte[] responseBuffer = new byte[1024]; StringBuilder translationText = new StringBuilder(); int read; do { read = s.Read(responseBuffer, 0, responseBuffer.Length); translationText.Append(Encoding.UTF8.GetString(responseBuffer, 0, read)); } while (read > 0); using (TextWriter tw = File.CreateText(Path.Combine(translationsPath, $"{tlFileName}.txt"))) tw.Write(translationText.ToString()); } catch (WebException we) { MessageBox.Show( $"Failed to retreive translation.\nResponse: {we.Message}", "Boop!", MessageBoxButtons.OK, MessageBoxIcon.Error); g.DialogResult = DialogResult.Abort; g.Timer.Stop(); g.Close(); } catch (Exception ex) { MessageBox.Show( $"Could not download the translation.\nInfo: {ex}", "Boop!", MessageBoxButtons.OK, MessageBoxIcon.Error); g.DialogResult = DialogResult.Abort; g.Timer.Stop(); g.Close(); } } g.DialogResult = DialogResult.OK; g.Timer.Stop(); g.Close(); }); downloaderThread.Start(); }); DialogResult result = loadingBarGui.ShowDialog(this); loadingBarGui.Dispose(); if (result != DialogResult.OK) return; MessageBox.Show( Translation.GetTranslation("TL_DOWNLOAD_DONE"), Translation.GetTranslation("TL_DOWNLOAD_DONE_TITLE"), MessageBoxButtons.OK); InitLanguageTable(); }
private void OpenTranslationDownloadUrl(object sender, EventArgs e) { Uri uri = null; TextDialog tdURL = new TextDialog( Translation.GetTranslation("TL_URL_TITLE"), Translation.GetTranslation("TL_URL_TEXT"), string.Empty, s => { try { uri = new Uri(s); return(uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps); } catch (Exception) { return(false); } }, Translation.GetTranslation("OK"), Translation.GetTranslation("CANCEL")); DialogResult promptDialog = tdURL.ShowDialog(this); tdURL.Dispose(); if (promptDialog != DialogResult.OK) { return; } string translationsPath = Path.Combine(MaidFiddler.DATA_PATH, Translation.TRANSLATIONS_PATH); LoadingBarGUI loadingBarGui = new LoadingBarGUI( Translation.GetTranslation("LOADING"), Translation.GetTranslation("TL_URL_DOWNLOADING"), true, g => { HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri); g.Timer.Start(); Debugger.WriteLine(LogLevel.Info, "Getting translation..."); webRequest.BeginGetResponse( ar => { try { HttpWebResponse response = (HttpWebResponse)webRequest.EndGetResponse(ar); Debugger.WriteLine(LogLevel.Info, "Got response!"); Debugger.WriteLine(LogLevel.Info, $"Response: {response.StatusCode}"); if (response.StatusCode == HttpStatusCode.NotFound) { MessageBox.Show( "Failed to download the translation: File not found", "Boop!", MessageBoxButtons.OK, MessageBoxIcon.Error); g.DialogResult = DialogResult.Abort; g.Timer.Stop(); g.Close(); return; } Stream s = response.GetResponseStream(); Debugger.WriteLine(LogLevel.Info, "Reading response"); List <byte> resultBuffer = new List <byte>(); byte[] responseBuffer = new byte[1024]; int read; do { read = s.Read(responseBuffer, 0, responseBuffer.Length); resultBuffer.AddRange(responseBuffer, 0, read); } while (read > 0); string translationString = Encoding.UTF8.GetString(resultBuffer.ToArray()); using (TextReader tr = new StringReader(translationString)) { if (!Translation.TagPattern.Match(tr.ReadLine()).Success) { Debugger.WriteLine(LogLevel.Error, "Failed to parse the translation: No tag found!"); MessageBox.Show( "The file does not contain the translation tag and thus cannot be recognised as a Maid Fiddler translation file.\nThe file has not been saved.", "Boop!", MessageBoxButtons.OK, MessageBoxIcon.Error); g.DialogResult = DialogResult.Abort; g.Timer.Stop(); g.Close(); return; } } string tlFileName = Path.GetFileNameWithoutExtension(uri.AbsolutePath); Debugger.WriteLine($"File name: {tlFileName}"); if (tlFileName == string.Empty || File.Exists(Path.Combine(translationsPath, $"{tlFileName}.txt"))) { char[] invalidChars = Path.GetInvalidFileNameChars(); TextDialog tdFileName = new TextDialog( Translation.GetTranslation("TL_NAME_CHANGE_TITLE"), Translation.GetTranslation("TL_NAME_CHANGE"), tlFileName, s1 => !s1.Any(c => invalidChars.Contains(c)), Translation.GetTranslation("OK"), Translation.GetTranslation("CANCEL")); if (tdFileName.ShowDialog(g) == DialogResult.OK) { tlFileName = tdFileName.Input; } if (tlFileName == string.Empty) { tlFileName = FiddlerUtils.GenerateFileName(); } tdFileName.Dispose(); } string path = Path.Combine(translationsPath, $"{tlFileName}.txt"); Debugger.WriteLine($"Writing translation to {path}"); using (TextWriter tw = File.CreateText(path)) tw.Write(translationString); g.DialogResult = DialogResult.OK; } catch (WebException we) { MessageBox.Show( $"Failed to retreive translation.\nResponse: {we.Message}", "Boop!", MessageBoxButtons.OK, MessageBoxIcon.Error); g.DialogResult = DialogResult.Abort; } catch (Exception ex) { MessageBox.Show( $"Unknown error occurred.\nInfo: {ex.ToString()}", "Boop!", MessageBoxButtons.OK, MessageBoxIcon.Error); g.DialogResult = DialogResult.Abort; } finally { g.Timer.Stop(); g.Close(); } }, null); }); DialogResult result = loadingBarGui.ShowDialog(this); loadingBarGui.Dispose(); if (result != DialogResult.OK) { return; } MessageBox.Show( Translation.GetTranslation("TL_DOWNLOAD_DONE"), Translation.GetTranslation("TL_DOWNLOAD_DONE_TITLE"), MessageBoxButtons.OK); LoadTranslations(Translation.CurrentTranslationFile); }
private void OpenTranslationDownloadGithub(object sender, EventArgs e) { string list = string.Empty; LoadingBarGUI loadingBarGui = new LoadingBarGUI( Translation.GetTranslation("LOADING"), Translation.GetTranslation("TL_LIST_LOADING"), true, g => { HttpWebRequest webRequest = (HttpWebRequest) WebRequest.Create($"{MaidFiddler.RESOURCE_URL}/Resources/Translations/translation_list.txt"); g.Timer.Start(); Debugger.WriteLine(LogLevel.Info, "Getting translation list..."); webRequest.BeginGetResponse( ar => { try { HttpWebResponse response = (HttpWebResponse)webRequest.EndGetResponse(ar); Debugger.WriteLine(LogLevel.Info, "Got response!"); Debugger.WriteLine(LogLevel.Info, $"Response: {response.StatusCode}"); if (response.StatusCode == HttpStatusCode.NotFound) { MessageBox.Show( "Failed to retreive translation list: List not found.", "Boop!", MessageBoxButtons.OK, MessageBoxIcon.Error); } Stream s = response.GetResponseStream(); Debugger.WriteLine(LogLevel.Info, "Reading response"); byte[] responseBuffer = new byte[1024]; List <byte> byteBuffer = new List <byte>(); int read; do { read = s.Read(responseBuffer, 0, responseBuffer.Length); byteBuffer.AddRange(responseBuffer, 0, read); } while (read > 0); list = Encoding.UTF8.GetString(byteBuffer.ToArray()); g.DialogResult = DialogResult.OK; } catch (WebException we) { MessageBox.Show( $"Failed to retreive translation list.\nResponse: {we.Message}", "Boop!", MessageBoxButtons.OK, MessageBoxIcon.Error); g.DialogResult = DialogResult.Abort; } catch (Exception ex) { MessageBox.Show( $"Unknown error occurred.\nInfo: {ex.ToString()}", "Boop!", MessageBoxButtons.OK, MessageBoxIcon.Error); g.DialogResult = DialogResult.Abort; } finally { g.Timer.Stop(); g.Close(); } }, null); }); DialogResult result = loadingBarGui.ShowDialog(this); loadingBarGui.Dispose(); if (result != DialogResult.OK) { return; } GithubTranslationsGUI tlGui = new GithubTranslationsGUI(list.Remove(0, 1)); tlGui.ShowDialog(this); tlGui.Dispose(); LoadTranslations(Translation.CurrentTranslationFile); }