예제 #1
0
파일: UIHelper.cs 프로젝트: henrikja/EVEMon
        /// <summary>
        /// Displays the character exportation window and then exports it.
        /// Optionally it exports it as it would be after the plan finish.
        /// </summary>
        /// <param name="character">The character.</param>
        /// <param name="plan">The plan.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">character</exception>
        public static async Task ExportCharacterAsync(Character character, Plan plan = null)
        {
            character.ThrowIfNull(nameof(character));

            bool isAfterPlanExport = plan != null;

            // Open the dialog box
            using (SaveFileDialog characterSaveDialog = new SaveFileDialog())
            {
                characterSaveDialog.Title  = $"Save {(isAfterPlanExport ? "After Plan " : String.Empty)}Character Info";
                characterSaveDialog.Filter =
                    @"Text Format|*.txt|CHR Format (EFT)|*.chr|HTML Format|*.html|XML Format (EVEMon)|*.xml";

                if (!isAfterPlanExport)
                {
                    characterSaveDialog.Filter += @"|XML Format (CCP API)|*.xml|PNG Image|*.png";
                }

                characterSaveDialog.FileName =
                    $"{character.Name}{(isAfterPlanExport ? $" (after plan {plan.Name})" : String.Empty)}";

                characterSaveDialog.FilterIndex = isAfterPlanExport
                                                      ? (int)CharacterSaveFormat.EVEMonXML
                                                      : (int)CharacterSaveFormat.CCPXML;

                if (characterSaveDialog.ShowDialog() == DialogResult.Cancel)
                {
                    return;
                }

                // Serialize
                try
                {
                    CharacterSaveFormat format = (CharacterSaveFormat)characterSaveDialog.FilterIndex;

                    // Save character with the chosen format to our file
                    await FileHelper.OverwriteOrWarnTheUserAsync(
                        characterSaveDialog.FileName,
                        async fs =>
                    {
                        if (format == CharacterSaveFormat.PNG)
                        {
                            Image image = CharacterMonitorScreenshot;
                            image.Save(fs, ImageFormat.Png);
                            await fs.FlushAsync();
                            return(true);
                        }

                        string content = CharacterExporter.Export(format, character, plan);
                        if ((format == CharacterSaveFormat.CCPXML) && string.IsNullOrEmpty(content))
                        {
                            MessageBox.Show(
                                @"This character has never been downloaded from CCP, cannot find it in the XML cache.",
                                @"Cannot export the character", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            return(false);
                        }

                        using (StreamWriter sw = new StreamWriter(fs))
                        {
                            await sw.WriteAsync(content);
                            await sw.FlushAsync();
                            await fs.FlushAsync();
                        }
                        return(true);
                    });
                }
                // Handle exception
                catch (IOException exc)
                {
                    ExceptionHandler.LogException(exc, true);
                    MessageBox.Show(@"A problem occurred during exportation. The operation has not been completed.");
                }
            }
        }
예제 #2
0
파일: UIHelper.cs 프로젝트: henrikja/EVEMon
        /// <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);
                }
            }
        }