void save_Click(object sender, EventArgs e) { if (!IsDirty()) { return; } dict.Name = name.Text; dict.Author = author.Text; dict.Url = url.Text; dict.FirstLanguage = firstLanguage.Text; dict.SecondLanguage = secondLanguage.Text; if (saveOnClose) { try { dict.Save(); } catch (DictionarySaveException ex) { ProgramLog.Default.AddMessage( LogType.Error, "Error saving dictionary information for {0}: {1}", dict.Name, ex.Message); } } Close(); }
private void ImportCompleted(IBilingualDictionary result) { Debug.Assert(!InvokeRequired, "ImportCompleted called on a secondary thread"); try { if (CurrentUI is Controls.ProgressUI) { Controls.ProgressUI prog = (Controls.ProgressUI)CurrentUI; prog.Percent = 100; prog.Message = Properties.Resources.ImportFinished; } this.imported = result; // Modal dialog simplifies things slightly. new DictionaryInfoEditor(imported, false).ShowDialog(); string root = DataStore.UserDataStore.Path; string name = imported.Name ?? Properties.Resources.DefaultDictionaryName; DataStore.UserDataStore.EnsureDirectoryExists(Configuration.DictionariesFolderName); // Attempt to save with a sane name; failing that, use a GUID as the name; otherwise report the error. var ipc = Path.GetInvalidPathChars(); if (name.IndexOfAny(ipc) >= 0) { // TODO: Sanitize file name! } string newPath = Path.Combine(Path.Combine(root, Configuration.DictionariesFolderName), name) + ".dict"; // We don't want to overwrite an existing dictionary. if (File.Exists(newPath)) { name = Guid.NewGuid().ToString("D"); newPath = Path.Combine(Path.Combine(root, Configuration.DictionariesFolderName), name) + ".dict"; } imported.Path = newPath; // If the dictionary can't save, we should delete the half-written file. // TODO: this should probably avoid deleting the file if the error was caused // by the file already existing (say, if it was created between calls). It would be // kind of rare though. try { imported.Save(); } catch (SystemException) { // XXX Lazy. File.Delete(imported.Path); throw; } new LookupForm(imported).Show(); Close(); } catch (ApplicationException ex) { ReportException(ex); } catch (IOException ex) { ReportException(ex); } catch (InvalidOperationException ex) { ReportException(ex); } catch (ArgumentException ex) { ReportException(ex); } }