예제 #1
0
        /// <summary>
        /// The export.
        /// </summary>
        /// <param name="filename">
        /// The filename.
        /// </param>
        /// <param name="preset">
        /// The preset.
        /// </param>
        /// <param name="configuration">
        /// The configuration.
        /// </param>
        public void Export(string filename, Preset preset, HBConfiguration configuration)
        {
            // TODO Add support for multiple export
            PresetTransportContainer container = JsonPresetFactory.ExportPreset(preset, configuration);

            HandBrakePresetService.ExportPreset(filename, container);
        }
예제 #2
0
 private void HandlePresetListsForSave(List <Preset> processList, Dictionary <string, HBPresetCategory> presetCategories, List <HBPreset> uncategorisedPresets)
 {
     foreach (Preset item in processList)
     {
         if (string.IsNullOrEmpty(item.Category))
         {
             uncategorisedPresets.Add(JsonPresetFactory.CreateHbPreset(item, HBConfigurationFactory.Create()));
         }
         else
         {
             HBPreset preset = JsonPresetFactory.CreateHbPreset(item, HBConfigurationFactory.Create());
             if (presetCategories.ContainsKey(item.Category))
             {
                 presetCategories[item.Category].ChildrenArray.Add(preset);
             }
             else
             {
                 presetCategories[item.Category] = new HBPresetCategory
                 {
                     ChildrenArray = new List <HBPreset> {
                         preset
                     },
                     Folder     = true,
                     PresetName = item.Category,
                     Type       = item.IsBuildIn ? 0 : 1
                 };
             }
         }
     }
 }
예제 #3
0
        public void UpdateBuiltInPresets()
        {
            // Clear the current built in Presets and now parse the temporary Presets file.
            this.ClearBuiltIn();

            IList <HBPresetCategory> presetCategories = HandBrakePresetService.GetBuiltInPresets();

            foreach (var category in presetCategories)
            {
                foreach (var hbpreset in category.ChildrenArray)
                {
                    Preset preset = JsonPresetFactory.ImportPreset(hbpreset);
                    preset.IsBuildIn = true;
                    preset.Category  = category.PresetName;
                    preset.Task.AllowedPassthruOptions = new AllowedPassthru(true); // We don't want to override the built-in preset
                    preset.IsPresetDisabled            = this.IsPresetDisabled(preset) || hbpreset.PresetDisabled;

                    this.Add(preset, true);
                }
            }

            // Verify we have presets.
            if (this.presets.Count == 0)
            {
                this.ServiceLogMessage("Failed to load built-in presets.");
                throw new GeneralApplicationException("Failed to load built-in presets.", "Restarting HandBrake may resolve this issue", null);
            }

            // Store the changes to disk
            this.SavePresetFiles();
        }
예제 #4
0
        private Preset ConvertHbPreset(HBPreset hbPreset)
        {
            Preset preset = JsonPresetFactory.ImportPreset(hbPreset);

            preset.Category = UserPresetCatgoryName; // TODO can we get this from the preset?

            return(preset);
        }
예제 #5
0
        private Preset ConvertHbPreset(HBPreset hbPreset, string categoryName)
        {
            Preset preset = JsonPresetFactory.ImportPreset(hbPreset);

            preset.Category = !string.IsNullOrEmpty(categoryName) ? categoryName : UserPresetCategoryName;

            return(preset);
        }
예제 #6
0
        public void Export(string filename, string presetName)
        {
            Preset foundPreset = this.flatPresetList.FirstOrDefault(s => s.Name == presetName);

            if (foundPreset != null)
            {
                PresetTransportContainer container = JsonPresetFactory.ExportPreset(foundPreset);
                HandBrakePresetService.ExportPreset(filename, container);
            }
        }
예제 #7
0
        private void ProcessPresetList(PresetTransportContainer container, bool ignoreOldBuiltIn)
        {
            // The presets file loaded was OK, so process it.
            foreach (var item in container.PresetList)
            {
                object deserialisedItem = JsonConvert.DeserializeObject <PresetCategory>(item.ToString());

                // Handle Categorised Presets.
                PresetCategory category = deserialisedItem as PresetCategory;
                if (category != null && category.Folder)
                {
                    foreach (HBPreset hbpreset in category.ChildrenArray)
                    {
                        Preset preset = JsonPresetFactory.ImportPreset(hbpreset);

                        if (preset.IsBuildIn && ignoreOldBuiltIn)
                        {
                            continue;
                        }

                        // Migration
                        preset.Category  = category.PresetName == "User Presets" ? UserPresetCatgoryName : category.PresetName;
                        preset.IsBuildIn = hbpreset.Type == 0;

                        // IF we are using Source Max, Set the Max Width / Height values.
                        if (preset.PictureSettingsMode == PresetPictureSettingsMode.SourceMaximum)
                        {
                            preset.Task.MaxWidth  = preset.Task.Height;
                            preset.Task.MaxHeight = preset.Task.Width;
                        }

                        this.Add(preset, true);
                    }
                }

                // Uncategorised Presets
                deserialisedItem = JsonConvert.DeserializeObject <HBPreset>(item.ToString());
                HBPreset hbPreset = deserialisedItem as HBPreset;
                if (hbPreset != null && !hbPreset.Folder)
                {
                    Preset preset = JsonPresetFactory.ImportPreset(hbPreset);
                    preset.Category  = UserPresetCatgoryName;
                    preset.IsBuildIn = hbPreset.Type == 1;

                    // IF we are using Source Max, Set the Max Width / Height values.
                    if (preset.PictureSettingsMode == PresetPictureSettingsMode.SourceMaximum)
                    {
                        preset.Task.MaxWidth  = preset.Task.Height;
                        preset.Task.MaxHeight = preset.Task.Width;
                    }

                    this.Add(preset, true);
                }
            }
        }
예제 #8
0
        private Preset ConvertHbPreset(HBPreset hbPreset)
        {
            Preset preset = null;

            preset          = JsonPresetFactory.ImportPreset(hbPreset);
            preset.Category = UserPresetCatgoryName; // TODO can we get this from the preset?

            // IF we are using Source Max, Set the Max Width / Height values.
            if (preset.PictureSettingsMode == PresetPictureSettingsMode.SourceMaximum)
            {
                preset.Task.MaxWidth  = preset.Task.Height;
                preset.Task.MaxHeight = preset.Task.Width;
            }

            return(preset);
        }
예제 #9
0
        /// <summary>
        /// Reads the CLI's CLI output format and load's them into the preset List Preset
        /// </summary>
        public void UpdateBuiltInPresets()
        {
            // Clear the current built in Presets and now parse the tempory Presets file.
            this.ClearBuiltIn();

            IList <PresetCategory> presetCategories = HandBrakePresetService.GetBuiltInPresets();

            foreach (var item in presetCategories)
            {
                foreach (var hbpreset in item.ChildrenArray)
                {
                    Preset preset = JsonPresetFactory.ImportPreset(hbpreset);
                    preset.Version           = VersionHelper.GetVersion();
                    preset.UsePictureFilters = true;
                    preset.IsBuildIn         = true; // Older versions did not have this flag so explicitly make sure it is set.
                    preset.Category          = item.PresetName;

                    if (preset.Name == "iPod")
                    {
                        preset.Task.KeepDisplayAspect = true;
                    }

                    preset.Task.AllowedPassthruOptions = new AllowedPassthru(true); // We don't want to override the built-in preset

                    if (preset.Name == "Normal")
                    {
                        preset.IsDefault = true;
                    }

                    this.presets.Add(preset);
                }
            }

            // Verify we have presets.
            if (this.presets.Count == 0)
            {
                throw new GeneralApplicationException("Failed to load built-in presets.", "Restarting HandBrake may resolve this issue", null);
            }

            // Store the changes to disk
            this.UpdatePresetFiles();
        }
예제 #10
0
        private void ProcessPresetList(PresetTransportContainer container)
        {
            // The presets file loaded was OK, so process it.
            foreach (var item in container.PresetList)
            {
                object deserializedItem = JsonSerializer.Deserialize <HBPresetCategory>(item.ToString(), JsonSettings.Options);

                // Handle Categorised Presets.
                HBPresetCategory category = deserializedItem as HBPresetCategory;
                if (category != null && category.Folder)
                {
                    foreach (HBPreset hbpreset in category.ChildrenArray)
                    {
                        Preset preset = JsonPresetFactory.ImportPreset(hbpreset);

                        // Migration
                        preset.Category         = category.PresetName == "User Presets" ? UserPresetCategoryName : category.PresetName;
                        preset.IsBuildIn        = hbpreset.Type == 0;
                        preset.IsPresetDisabled = this.IsPresetDisabled(preset);

                        this.Add(preset, true);
                    }
                }

                // Uncategorised Presets
                deserializedItem = JsonSerializer.Deserialize <HBPreset>(item.ToString(), JsonSettings.Options);
                HBPreset hbPreset = deserializedItem as HBPreset;
                if (hbPreset != null && !hbPreset.Folder)
                {
                    Preset preset = JsonPresetFactory.ImportPreset(hbPreset);
                    preset.Category         = UserPresetCategoryName;
                    preset.IsBuildIn        = hbPreset.Type == 0;
                    preset.IsPresetDisabled = this.IsPresetDisabled(preset);

                    this.Add(preset, true);
                }
            }
        }
예제 #11
0
        public void UpdateBuiltInPresets()
        {
            // Clear the current built in Presets and now parse the temporary Presets file.
            this.ClearBuiltIn();

            bool hasUserDefault = this.flatPresetDict.Values.FirstOrDefault(f => f.IsDefault) != null;

            IList <HBPresetCategory> presetCategories = HandBrakePresetService.GetBuiltInPresets();

            foreach (var category in presetCategories)
            {
                foreach (var hbpreset in category.ChildrenArray)
                {
                    Preset preset = JsonPresetFactory.ImportPreset(hbpreset);
                    preset.IsBuildIn        = true;
                    preset.Category         = category.PresetName;
                    preset.IsPresetDisabled = this.IsPresetDisabled(preset) || hbpreset.PresetDisabled;

                    if (hbpreset.Default && hasUserDefault)
                    {
                        preset.IsDefault = false;
                    }

                    this.Add(preset, true);
                }
            }

            // Verify we have presets.
            if (this.presets.Count == 0)
            {
                this.ServiceLogMessage("Failed to load built-in presets.");
                throw new GeneralApplicationException("Failed to load built-in presets.", "Restarting HandBrake may resolve this issue", null);
            }

            // Store the changes to disk
            this.SavePresetFiles();
        }
예제 #12
0
        /// <summary>
        /// Load in the Built-in and User presets into the collection
        /// </summary>
        private void LoadPresets()
        {
            // First clear the Presets arraylists
            this.presets.Clear();

            // Load the presets file.
            try
            {
                // If we don't have a presets file. Create one for first load.
                if (!File.Exists(this.presetFile))
                {
                    // If this is a nightly, and we don't have a presets file, try port the main version if it exists.
                    string releasePresetFile = Path.Combine(DirectoryUtilities.GetUserStoragePath(false), "presets.json");
                    if (VersionHelper.IsNightly() && File.Exists(releasePresetFile))
                    {
                        File.Copy(releasePresetFile, DirectoryUtilities.GetUserStoragePath(true));
                    }
                    else
                    {
                        this.UpdateBuiltInPresets();
                        return; // Update built-in presets stores the presets locally, so just return.
                    }
                }

                // Otherwise, we already have a file, so lets try load it.
                PresetTransportContainer container = null;
                using (StreamReader reader = new StreamReader(this.presetFile))
                {
                    try
                    {
                        container = JsonConvert.DeserializeObject <PresetTransportContainer>(reader.ReadToEnd());
                    }
                    catch (Exception exc)
                    {
                        Debug.WriteLine("Failed to parse presets file: " + exc);
                    }
                }

                // Sanity Check. Did the container deserialise.
                if (container == null || container.PresetList == null)
                {
                    string filename = this.RecoverFromCorruptedPresetFile(this.presetFile);
                    this.errorService.ShowMessageBox(
                        Resources.PresetService_UnableToLoadPresets + filename,
                        Resources.PresetService_UnableToLoad,
                        MessageBoxButton.OK,
                        MessageBoxImage.Exclamation);

                    this.UpdateBuiltInPresets();
                    return; // Update built-in presets stores the presets locally, so just return.
                }

                // Version Check
                // If we have old presets, or the container wasn't parseable, or we have a version mismatch, backup the user preset file
                // incase something goes wrong and reset built-in presets, then re-save.
                if (container.VersionMajor != Constants.PresetVersionMajor || container.VersionMinor != Constants.PresetVersionMinor || container.VersionMicro != Constants.PresetVersionMicro)
                {
                    string fileName = this.ArchivePresetFile(this.presetFile);
                    this.errorService.ShowMessageBox(
                        Resources.PresetService_PresetsOutOfDate
                        + Environment.NewLine + Environment.NewLine + Resources.PresetService_ArchiveFile + fileName,
                        Resources.PresetService_UnableToLoad,
                        MessageBoxButton.OK,
                        MessageBoxImage.Exclamation);
                    this.UpdateBuiltInPresets(); // Update built-in presets stores the presets locally, so just return.
                    return;
                }

                // Force Upgrade of presets
                if (this.userSettingService.GetUserSetting <int>(UserSettingConstants.ForcePresetReset) < ForcePresetReset)
                {
                    this.userSettingService.SetUserSetting(UserSettingConstants.ForcePresetReset, ForcePresetReset);

                    string fileName = this.ArchivePresetFile(this.presetFile);
                    this.errorService.ShowMessageBox(
                        Resources.Presets_PresetForceReset
                        + Environment.NewLine + Environment.NewLine + Resources.PresetService_ArchiveFile + fileName,
                        Resources.PresetService_UnableToLoad,
                        MessageBoxButton.OK,
                        MessageBoxImage.Exclamation);
                    this.UpdateBuiltInPresets(); // Update built-in presets stores the presets locally, so just return.
                    return;
                }

                // The presets file loaded was OK, so process it.
                foreach (var item in container.PresetList)
                {
                    object deserialisedItem = JsonConvert.DeserializeObject <PresetCategory>(item.ToString());

                    // Handle Categorised Presets.
                    PresetCategory category = deserialisedItem as PresetCategory;
                    if (category != null && category.Folder)
                    {
                        foreach (HBPreset hbpreset in category.ChildrenArray)
                        {
                            Preset preset = JsonPresetFactory.ImportPreset(hbpreset);

                            // Migration
                            if (category.PresetName == "User Presets")
                            {
                                preset.Category = UserPresetCatgoryName;
                            }
                            else
                            {
                                preset.Category = category.PresetName;
                            }
                            preset.IsBuildIn = hbpreset.Type == 0;

                            // IF we are using Source Max, Set the Max Width / Height values.
                            if (preset.PictureSettingsMode == PresetPictureSettingsMode.SourceMaximum)
                            {
                                preset.Task.MaxWidth  = preset.Task.Height;
                                preset.Task.MaxHeight = preset.Task.Width;
                            }

                            this.Add(preset, true);
                        }
                    }

                    // Uncategorised Presets
                    deserialisedItem = JsonConvert.DeserializeObject <HBPreset>(item.ToString());
                    HBPreset hbPreset = deserialisedItem as HBPreset;
                    if (hbPreset != null && !hbPreset.Folder)
                    {
                        Preset preset = JsonPresetFactory.ImportPreset(hbPreset);
                        preset.Category  = UserPresetCatgoryName;
                        preset.IsBuildIn = hbPreset.Type == 1;

                        // IF we are using Source Max, Set the Max Width / Height values.
                        if (preset.PictureSettingsMode == PresetPictureSettingsMode.SourceMaximum)
                        {
                            preset.Task.MaxWidth  = preset.Task.Height;
                            preset.Task.MaxHeight = preset.Task.Width;
                        }

                        this.Add(preset, true);
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                this.RecoverFromCorruptedPresetFile(this.presetFile);
                this.UpdateBuiltInPresets();
            }
        }
예제 #13
0
        /// <summary>
        /// Load in the Built-in and User presets into the collection
        /// </summary>
        private void LoadPresets()
        {
            // First clear the Presets arraylists
            this.presets.Clear();

            // Load the presets file.
            try
            {
                // If we don't have a presets file. Create one for first load.
                if (!File.Exists(this.presetFile))
                {
                    this.UpdateBuiltInPresets();
                    return;
                }

                // Otherwise, we already have a file, so lets try load it.
                bool versionCheckChange = false;
                using (StreamReader reader = new StreamReader(this.presetFile))
                {
                    // New Preset Format.
                    PresetTransportContainer container = null;
                    try
                    {
                        container = JsonConvert.DeserializeObject <PresetTransportContainer>(reader.ReadToEnd());
                    }
                    catch (Exception)
                    {
                        // ignored
                    }

                    // Sanity Check. Did the container deserialise.
                    if (container?.PresetList == null)
                    {
                        // Close and Dispose of early.
                        reader.Close();
                        reader.Dispose();

                        string filename = this.RecoverFromCorruptedPresetFile(this.presetFile);
                        this.errorService.ShowMessageBox(
                            Resources.PresetService_UnableToLoadPresets + filename,
                            Resources.PresetService_UnableToLoad,
                            MessageBoxButton.OK,
                            MessageBoxImage.Exclamation);

                        this.UpdateBuiltInPresets();
                        return;
                    }

                    // Version Check
                    // If we have old presets, or the container wasn't parseable, or we have a version mismatch, backup the user preset file
                    // incase something goes wrong.
                    if (container.VersionMajor != Constants.PresetVersionMajor || container.VersionMinor != Constants.PresetVersionMinor || container.VersionMicro != Constants.PresetVersionMicro)
                    {
                        string fileName = this.ArchivePresetFile(this.presetFile);
                        this.errorService.ShowMessageBox(
                            Resources.PresetService_PresetsOutOfDate
                            + Environment.NewLine + Environment.NewLine + Resources.PresetService_ArchiveFile + fileName,
                            Resources.PresetService_UnableToLoad,
                            MessageBoxButton.OK,
                            MessageBoxImage.Exclamation);
                        versionCheckChange = true;
                    }

                    // Process the presets.
                    foreach (var item in container.PresetList)
                    {
                        object deserialisedItem = JsonConvert.DeserializeObject <PresetCategory>(item.ToString());;

                        // Handle Categorised Presets.
                        PresetCategory category = deserialisedItem as PresetCategory;
                        if (category != null && category.Folder)
                        {
                            foreach (HBPreset hbpreset in category.ChildrenArray)
                            {
                                Preset preset = JsonPresetFactory.ImportPreset(hbpreset);
                                preset.Category  = category.PresetName;
                                preset.IsBuildIn = hbpreset.Type == 0;

                                // IF we are using Source Max, Set the Max Width / Height values.
                                if (preset.PictureSettingsMode == PresetPictureSettingsMode.SourceMaximum)
                                {
                                    preset.Task.MaxWidth  = preset.Task.Height;
                                    preset.Task.MaxHeight = preset.Task.Width;
                                }

                                this.presets.Add(preset);
                            }
                        }

                        // Uncategorised Presets
                        deserialisedItem = JsonConvert.DeserializeObject <HBPreset>(item.ToString());
                        HBPreset hbPreset = deserialisedItem as HBPreset;
                        if (hbPreset != null && !hbPreset.Folder)
                        {
                            Preset preset = JsonPresetFactory.ImportPreset(hbPreset);
                            preset.Category  = UserPresetCatgoryName;
                            preset.IsBuildIn = hbPreset.Type == 1;

                            // IF we are using Source Max, Set the Max Width / Height values.
                            if (preset.PictureSettingsMode == PresetPictureSettingsMode.SourceMaximum)
                            {
                                preset.Task.MaxWidth  = preset.Task.Height;
                                preset.Task.MaxHeight = preset.Task.Width;
                            }

                            this.presets.Add(preset);
                        }
                    }
                }

                // Resave the presets if we failed the version check to update the container
                if (versionCheckChange)
                {
                    this.SavePresetFiles();
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                this.RecoverFromCorruptedPresetFile(this.presetFile);
                this.UpdateBuiltInPresets();
            }
        }
예제 #14
0
        public void ExportCategories(string filename, IList <PresetDisplayCategory> categories, HBConfiguration configuration)
        {
            PresetTransportContainer container = JsonPresetFactory.ExportPresetCategories(categories, configuration);

            HandBrakePresetService.ExportPreset(filename, container);
        }
예제 #15
0
        /// <summary>
        /// The import.
        /// </summary>
        /// <param name="filename">
        /// The filename.
        /// </param>
        public void Import(string filename)
        {
            if (!string.IsNullOrEmpty(filename))
            {
                PresetTransportContainer container = HandBrakePresetService.GetPresetFromFile(filename);

                if (container?.PresetList == null || container.PresetList.Count == 0)
                {
                    this.errorService.ShowError(Resources.Main_PresetImportFailed, Resources.Main_PresetImportFailedSolution, string.Empty);
                    return;
                }

                // HBPreset Handling
                if (container.PresetList != null)
                {
                    foreach (var objectPreset in container.PresetList)
                    {
                        HBPreset hbPreset = JsonConvert.DeserializeObject <HBPreset>(objectPreset.ToString());

                        Preset preset = null;
                        try
                        {
                            preset          = JsonPresetFactory.ImportPreset(hbPreset);
                            preset.Category = UserPresetCatgoryName;

                            // IF we are using Source Max, Set the Max Width / Height values.
                            if (preset.PictureSettingsMode == PresetPictureSettingsMode.SourceMaximum)
                            {
                                preset.Task.MaxWidth  = preset.Task.Height;
                                preset.Task.MaxHeight = preset.Task.Width;
                            }
                        }
                        catch (Exception exc)
                        {
                            this.errorService.ShowError(Resources.Main_PresetImportFailed, Resources.Main_PresetImportFailedSolution, exc);
                        }

                        if (preset == null)
                        {
                            this.errorService.ShowError(Resources.Main_PresetImportFailed, Resources.Main_PresetImportFailedSolution, string.Empty);
                            return;
                        }

                        if (this.CheckIfPresetExists(preset.Name))
                        {
                            if (!this.CanUpdatePreset(preset.Name))
                            {
                                MessageBox.Show(Resources.Main_PresetErrorBuiltInName, Resources.Error, MessageBoxButton.OK, MessageBoxImage.Error);
                                return;
                            }

                            MessageBoxResult result = MessageBox.Show(Resources.Main_PresetOverwriteWarning, Resources.Overwrite, MessageBoxButton.YesNo, MessageBoxImage.Warning);
                            if (result == MessageBoxResult.Yes)
                            {
                                this.Update(preset);
                            }
                        }
                        else
                        {
                            this.Add(preset);
                        }
                    }
                }

                // Category Handling.
                // TODO maybe for a future release.
            }
        }
예제 #16
0
        /// <summary>
        /// The import.
        /// </summary>
        /// <param name="filename">
        /// The filename.
        /// </param>
        public void Import(string filename)
        {
            // TODO needs a tidy up but will do for now.
            if (!string.IsNullOrEmpty(filename))
            {
                PresetTransportContainer container = HandBrakePresetService.GetPresetFromFile(filename);

                if (container == null || container.PresetList == null || container.PresetList.Count == 0)
                {
                    this.errorService.ShowError(Resources.Main_PresetImportFailed, Resources.Main_PresetImportFailedSolution, string.Empty);
                    return;
                }

                // HBPreset Handling
                IList <HBPreset> hbPresets = container.PresetList as IList <HBPreset>;
                if (hbPresets != null)
                {
                    foreach (var hbPreset in hbPresets)
                    {
                        Preset preset = null;
                        try
                        {
                            preset          = JsonPresetFactory.ImportPreset(hbPreset);
                            preset.Category = UserPresetCatgoryName;

                            // IF we are using Source Max, Set the Max Width / Height values.
                            if (preset.PictureSettingsMode == PresetPictureSettingsMode.SourceMaximum)
                            {
                                preset.Task.MaxWidth  = preset.Task.Height;
                                preset.Task.MaxHeight = preset.Task.Width;
                            }
                        }
                        catch (Exception exc)
                        {
                            this.errorService.ShowError(Resources.Main_PresetImportFailed, Resources.Main_PresetImportFailedSolution, exc);
                        }

                        if (preset == null)
                        {
                            this.errorService.ShowError(Resources.Main_PresetImportFailed, Resources.Main_PresetImportFailedSolution, string.Empty);
                            return;
                        }

                        // TODO Better version checking.

                        if (this.CheckIfPresetExists(preset.Name))
                        {
                            if (!CanUpdatePreset(preset.Name))
                            {
                                MessageBox.Show(Resources.Main_PresetErrorBuiltInName, Resources.Error, MessageBoxButton.OK, MessageBoxImage.Error);
                                return;
                            }

                            MessageBoxResult result = MessageBox.Show(Resources.Main_PresetOverwriteWarning, Resources.Overwrite, MessageBoxButton.YesNo, MessageBoxImage.Warning);
                            if (result == MessageBoxResult.Yes)
                            {
                                Update(preset);
                            }
                        }
                        else
                        {
                            Add(preset);
                        }
                    }
                }

                // Category Handling.
            }
        }
예제 #17
0
        /// <summary>
        /// Update the preset files
        /// </summary>
        private void SavePresetFiles()
        {
            try
            {
                // Verify Directories.
                string directory = Path.GetDirectoryName(this.presetFile);
                if (directory != null && !Directory.Exists(directory))
                {
                    Directory.CreateDirectory(directory);
                }

                // Organise the Presets list into Json Equivalent objects.
                Dictionary <string, PresetCategory> presetCategories = new Dictionary <string, PresetCategory>();
                List <HBPreset> uncategorisedPresets = new List <HBPreset>();

                // Handle User Presets first.
                foreach (Preset item in this.flatPresetList.Values.OrderBy(o => o.IsBuildIn))
                {
                    if (string.IsNullOrEmpty(item.Category))
                    {
                        uncategorisedPresets.Add(JsonPresetFactory.CreateHbPreset(item, HBConfigurationFactory.Create()));
                    }
                    else
                    {
                        HBPreset preset = JsonPresetFactory.CreateHbPreset(item, HBConfigurationFactory.Create());
                        if (presetCategories.ContainsKey(item.Category))
                        {
                            presetCategories[item.Category].ChildrenArray.Add(preset);
                        }
                        else
                        {
                            presetCategories[item.Category] = new PresetCategory
                            {
                                ChildrenArray = new List <HBPreset> {
                                    preset
                                },
                                Folder     = true,
                                PresetName = item.Category,
                                Type       = item.IsBuildIn ? 0 : 1
                            };
                        }
                    }
                }

                // Wrap the categories in a container.
                JsonSerializerSettings settings = new JsonSerializerSettings {
                    MissingMemberHandling = MissingMemberHandling.Ignore
                };
                PresetTransportContainer container = new PresetTransportContainer(
                    Constants.PresetVersionMajor,
                    Constants.PresetVersionMinor,
                    Constants.PresetVersionMicro)
                {
                    PresetList = new List <object>()
                };
                container.PresetList.AddRange(presetCategories.Values);
                container.PresetList.AddRange(uncategorisedPresets);

                // Write the preset container out to file.
                using (FileStream strm = new FileStream(this.presetFile, FileMode.Create, FileAccess.Write))
                {
                    string presetsJson = JsonConvert.SerializeObject(container, Formatting.Indented, settings);
                    using (StreamWriter writer = new StreamWriter(strm))
                    {
                        writer.WriteLine(presetsJson);
                    }
                }
            }
            catch (Exception exc)
            {
                Debug.WriteLine(exc);
                throw new GeneralApplicationException("Unable to write to the presets file.", "The details section below may indicate why this error has occurred.", exc);
            }
        }