/// <summary> /// Saves the plans to a file. /// </summary> /// <param name="plans">The plans.</param> public static async Task SavePlansAsync(IList <Plan> plans) { Character character = (Character)plans.First().Character; // Prompt the user to pick a file name using (SaveFileDialog sfdSave = new SaveFileDialog()) { sfdSave.FileName = $"{character.Name} - Plans Backup"; sfdSave.Title = @"Save to File"; sfdSave.Filter = @"EVEMon Plans Backup Format (*.epb)|*.epb"; sfdSave.FilterIndex = (int)PlanFormat.Emp; if (sfdSave.ShowDialog() == DialogResult.Cancel) { return; } try { string content = PlanIOHelper.ExportAsXML(plans); // Moves to the final file await FileHelper.OverwriteOrWarnTheUserAsync( sfdSave.FileName, async fs => { // Emp is actually compressed xml Stream stream = new GZipStream(fs, CompressionMode.Compress); using (StreamWriter writer = new StreamWriter(stream, Encoding.UTF8)) { await writer.WriteAsync(content); await writer.FlushAsync(); await stream.FlushAsync(); await fs.FlushAsync(); } return(true); }); } catch (IOException err) { ExceptionHandler.LogException(err, false); MessageBox.Show($"There was an error writing out the file:\n\n{err.Message}", @"Save Failed", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
/// <summary> /// Displays the plan exportation window and then exports it. /// </summary> /// <param name="plan">The plan.</param> /// <param name="character">The character.</param> /// <returns></returns> /// <exception cref="System.ArgumentNullException"></exception> /// <exception cref="System.NotImplementedException"></exception> private static async Task ExportPlanAsync(Plan plan, Character character) { plan.ThrowIfNull(nameof(plan)); character.ThrowIfNull(nameof(character)); // Assemble an initial filename and remove prohibited characters string planSaveName = $"{character.Name} - {plan.Name}"; char[] invalidFileChars = Path.GetInvalidFileNameChars(); int fileInd = planSaveName.IndexOfAny(invalidFileChars); while (fileInd != -1) { planSaveName = planSaveName.Replace(planSaveName[fileInd], '-'); fileInd = planSaveName.IndexOfAny(invalidFileChars); } // Prompt the user to pick a file name using (SaveFileDialog sfdSave = new SaveFileDialog()) { sfdSave.FileName = planSaveName; sfdSave.Title = @"Save to File"; sfdSave.Filter = @"EVEMon Plan Format (*.emp)|*.emp|XML Format (*.xml)|*.xml|Text Format (*.txt)|*.txt"; sfdSave.FilterIndex = (int)PlanFormat.Emp; if (sfdSave.ShowDialog() == DialogResult.Cancel) { return; } // Serialize try { PlanFormat format = (PlanFormat)sfdSave.FilterIndex; string content; switch (format) { case PlanFormat.Emp: case PlanFormat.Xml: content = PlanIOHelper.ExportAsXML(plan); break; case PlanFormat.Text: // Prompts the user and returns if canceled PlanExportSettings settings = PromptUserForPlanExportSettings(plan); if (settings == null) { return; } content = PlanIOHelper.ExportAsText(plan, settings); break; default: throw new NotImplementedException(); } // Moves to the final file await FileHelper.OverwriteOrWarnTheUserAsync( sfdSave.FileName, async fs => { Stream stream = fs; // Emp is actually compressed text if (format == PlanFormat.Emp) { stream = new GZipStream(fs, CompressionMode.Compress); } using (StreamWriter writer = new StreamWriter(stream, Encoding.UTF8)) { await writer.WriteAsync(content); await writer.FlushAsync(); await stream.FlushAsync(); await fs.FlushAsync(); } return(true); }); } catch (IOException err) { ExceptionHandler.LogException(err, true); MessageBox.Show($"There was an error writing out the file:\n\n{err.Message}", @"Save Failed", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }