private void OnFileOpen(object sender, EventArgs e) { OpenFileDialogEx ofd = UIUtil.CreateOpenFileDialog("Open KeePass Translation", m_strFileFilter, 1, null, false, AppDefs.FileDialogContext.Attachments); if(ofd.ShowDialog() != DialogResult.OK) return; KPTranslation kpTrl = null; try { XmlSerializerEx xs = new XmlSerializerEx(typeof(KPTranslation)); kpTrl = KPTranslation.LoadFromFile(ofd.FileName, xs); } catch(Exception ex) { MessageBox.Show(ex.Message, "TrlUtil", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } m_strFile = ofd.FileName; StringBuilder sbUnusedText = new StringBuilder(); if(kpTrl.UnusedText.Length > 0) { if(kpTrl.UnusedText.EndsWith("\r") || kpTrl.UnusedText.EndsWith("\n")) sbUnusedText.Append(kpTrl.UnusedText); else sbUnusedText.AppendLine(kpTrl.UnusedText); } m_trl.Properties = kpTrl.Properties; foreach(KPStringTable kpstNew in kpTrl.StringTables) { foreach(KPStringTable kpstInto in m_trl.StringTables) { if(kpstInto.Name == kpstNew.Name) MergeInStringTable(kpstInto, kpstNew, sbUnusedText); } } FormTrlMgr.MergeForms(m_trl.Forms, kpTrl.Forms, sbUnusedText); m_tbNameEng.Text = m_trl.Properties.NameEnglish; m_tbNameLcl.Text = m_trl.Properties.NameNative; m_tbLangID.Text = m_trl.Properties.Iso6391Code; m_tbAuthorName.Text = m_trl.Properties.AuthorName; m_tbAuthorContact.Text = m_trl.Properties.AuthorContact; m_cbRtl.Checked = m_trl.Properties.RightToLeft; m_rtbUnusedText.Text = sbUnusedText.ToString(); this.UpdateStringTableUI(); this.UpdateStatusImages(null); this.UpdatePreviewForm(); }
private void OnFileSave(object sender, EventArgs e) { UpdateInternalTranslation(); if(m_strFile.Length == 0) { OnFileSaveAs(sender, e); return; } PrepareSave(); try { XmlSerializerEx xs = new XmlSerializerEx(typeof(KPTranslation)); KPTranslation.SaveToFile(m_trl, m_strFile, xs); m_bModified = false; } catch(Exception ex) { MessageBox.Show(ex.Message, "TrlUtil", MessageBoxButtons.OK, MessageBoxIcon.Warning); } }
private static bool SaveConfigFileEx(AppConfigEx tConfig, string strFilePath, bool bRemoveConfigPref) { tConfig.OnSavePre(); XmlSerializerEx xmlSerial = new XmlSerializerEx(typeof(AppConfigEx)); bool bResult = true; // FileStream fs = null; IOConnectionInfo iocPath = IOConnectionInfo.FromPath(strFilePath); FileTransactionEx fts = new FileTransactionEx(iocPath, true); Stream fs = null; // Temporarily remove user file preference (restore after saving) bool bConfigPref = tConfig.Meta.PreferUserConfiguration; if(bRemoveConfigPref) tConfig.Meta.PreferUserConfiguration = false; XmlWriterSettings xws = new XmlWriterSettings(); xws.Encoding = StrUtil.Utf8; xws.Indent = true; xws.IndentChars = "\t"; try { // fs = new FileStream(strFilePath, FileMode.Create, // FileAccess.Write, FileShare.None); fs = fts.OpenWrite(); if(fs == null) throw new InvalidOperationException(); XmlWriter xw = XmlWriter.Create(fs, xws); xmlSerial.Serialize(xw, tConfig); xw.Close(); } catch(Exception) { bResult = false; } // Do not assert if(fs != null) { fs.Close(); fs = null; } if(bResult) { try { fts.CommitWrite(); } catch(Exception) { Debug.Assert(false); } } if(bRemoveConfigPref) tConfig.Meta.PreferUserConfiguration = bConfigPref; tConfig.OnSavePost(); return bResult; }
private static AppConfigEx LoadConfigFileEx(string strFilePath, XmlDocument xdEnforced) { if(string.IsNullOrEmpty(strFilePath)) return null; AppConfigEx tConfig = null; XmlSerializerEx xmlSerial = new XmlSerializerEx(typeof(AppConfigEx)); if(xdEnforced == null) { FileStream fs = null; try { fs = new FileStream(strFilePath, FileMode.Open, FileAccess.Read, FileShare.Read); tConfig = (AppConfigEx)xmlSerial.Deserialize(fs); } catch(Exception) { } // Do not assert if(fs != null) { fs.Close(); fs = null; } } else // Enforced configuration { try { XmlDocument xd = new XmlDocument(); xd.Load(strFilePath); XmlUtil.MergeNodes(xd, xd.DocumentElement, xdEnforced.DocumentElement); MemoryStream msAsm = new MemoryStream(); xd.Save(msAsm); MemoryStream msRead = new MemoryStream(msAsm.ToArray(), false); tConfig = (AppConfigEx)xmlSerial.Deserialize(msRead); msRead.Close(); msAsm.Close(); } catch(FileNotFoundException) { } catch(Exception) { Debug.Assert(false); } } if(tConfig != null) tConfig.OnLoad(); return tConfig; }
public static AppConfigEx Load() { AppConfigSerializer.GetConfigPaths(); // AppConfigEx cfgEnf = LoadConfigFileEx(m_strEnforcedConfigFile); // if(cfgEnf != null) // { // cfgEnf.Meta.IsEnforcedConfiguration = true; // return cfgEnf; // } XmlDocument xdEnforced = LoadEnforcedConfigFile(); AppConfigEx cfgGlobal = LoadConfigFileEx(m_strGlobalConfigFile, xdEnforced); AppConfigEx cfgUser = LoadConfigFileEx(m_strUserConfigFile, xdEnforced); if((cfgGlobal == null) && (cfgUser == null)) { if(xdEnforced != null) { XmlSerializerEx xmlSerial = new XmlSerializerEx(typeof(AppConfigEx)); try { MemoryStream msEnf = new MemoryStream(); xdEnforced.Save(msEnf); MemoryStream msRead = new MemoryStream(msEnf.ToArray(), false); AppConfigEx cfgEnf = (AppConfigEx)xmlSerial.Deserialize(msRead); cfgEnf.OnLoad(); msRead.Close(); msEnf.Close(); return cfgEnf; } catch(Exception) { Debug.Assert(false); } } AppConfigEx cfgNew = new AppConfigEx(); cfgNew.OnLoad(); // Create defaults return cfgNew; } else if((cfgGlobal != null) && (cfgUser == null)) return cfgGlobal; else if((cfgGlobal == null) && (cfgUser != null)) return cfgUser; cfgUser.Meta.PreferUserConfiguration = cfgGlobal.Meta.PreferUserConfiguration; return (cfgGlobal.Meta.PreferUserConfiguration ? cfgUser : cfgGlobal); }
private void GetAvailableTranslations(string strPath, List<string> vList) { try { DirectoryInfo di = new DirectoryInfo(strPath); FileInfo[] vFiles = di.GetFiles(); foreach(FileInfo fi in vFiles) { string strFullName = fi.FullName; if(strFullName.EndsWith("." + KPTranslation.FileExtension, StrUtil.CaseIgnoreCmp)) { string strFileName = UrlUtil.GetFileName(strFullName); bool bFound = false; foreach(string strExisting in vList) { if(strExisting.Equals(strFileName, StrUtil.CaseIgnoreCmp)) { bFound = true; break; } } if(bFound) continue; try { XmlSerializerEx xs = new XmlSerializerEx(typeof(KPTranslation)); KPTranslation kpTrl = KPTranslation.LoadFromFile( strFullName, xs); ListViewItem lvi = m_lvLanguages.Items.Add( kpTrl.Properties.NameEnglish, 0); lvi.SubItems.Add(kpTrl.Properties.ApplicationVersion); lvi.SubItems.Add(kpTrl.Properties.AuthorName); lvi.SubItems.Add(kpTrl.Properties.AuthorContact); lvi.Tag = strFileName; vList.Add(strFileName); } catch(Exception ex) { MessageService.ShowWarning(ex.Message); } } } } catch(Exception) { } // Directory might not exist or cause access violation }
private static void LoadTranslation() { string strLangFile = m_appConfig.Application.LanguageFile; if(!string.IsNullOrEmpty(strLangFile)) { string[] vLangDirs = new string[]{ AppConfigSerializer.AppDataDirectory, AppConfigSerializer.LocalAppDataDirectory, UrlUtil.GetFileDirectory(WinUtil.GetExecutable(), false, false) }; foreach(string strLangDir in vLangDirs) { string strLangPath = UrlUtil.EnsureTerminatingSeparator( strLangDir, false) + strLangFile; try { XmlSerializerEx xs = new XmlSerializerEx(typeof(KPTranslation)); m_kpTranslation = KPTranslation.LoadFromFile(strLangPath, xs); KPRes.SetTranslatedStrings( m_kpTranslation.SafeGetStringTableDictionary( "KeePass.Resources.KPRes")); KLRes.SetTranslatedStrings( m_kpTranslation.SafeGetStringTableDictionary( "KeePassLib.Resources.KLRes")); StrUtil.RightToLeft = m_kpTranslation.Properties.RightToLeft; break; } catch(DirectoryNotFoundException) { } // Ignore catch(FileNotFoundException) { } // Ignore catch(Exception) { Debug.Assert(false); } } } }