/// <summary>
        /// Creates a new instance of <see cref="profile"/> from <paramref name="xml"/>
        /// </summary>
        /// <param name="xml">The xml text that represents the SnippitInstal</param>
        /// <returns><see cref="profile"/> instance</returns>
        /// <exception cref="System.Exception">If there are Validation errors with the xml</exception>
        public static SnippitInstal FromXml(string xml)
        {
            ValidationResult vResult = ValidateXml.ValidateSnipitInstalXml(xml);

            if (vResult.HasErrors == true)
            {
                var ex = new System.Exception(vResult.ToString());
                throw ex;
            }

            XmlSerializer serializer = new XmlSerializer(typeof(SnippitInstal));

            var reader = new System.IO.StringReader(xml);
            var si     = (SnippitInstal)serializer.Deserialize(reader);

            reader.Close();
            if (si.profile != null && si.plugin != null)
            {
                System.Threading.Tasks.Parallel.ForEach(si.plugin, plg => {
                    lock (si)
                    {
                        plg.ParentProfile = si.profile;
                    }
                });
            }

            return(si);
        }
        private void Dialog_ClickOkMouseButton(object sender, EventArgs e)
        {
            if (_dialog.Tag.ToString() != ToString())
            {
                return;
            }
            switch (_fileMethod)
            {
            case FileMethod.Add:
                File.Copy(_fileName, _destFilePath, true);
                AddFilesToSettings(_destFilePath);
                break;

            case FileMethod.Delete:
                File.Delete(_fileName);
                DeleteFilesFromSettings(_destFilePath);
                break;

            case FileMethod.Replace:
                // ReSharper disable once NotAccessedVariable
                switch (_view.ListBoxFiles.SelectedIndex)
                {
                case 0:
                    // ReSharper disable once RedundantAssignment
                    _ = new ValidateXml(_fileName, ValidateXml.XmlType.Auto);
                    //MessageBox.Show("ValidateXml.Passed=" + ValidateXml.Passed);
                    if (ValidateXml.Passed)
                    {
                        File.Copy(_fileName, _destFilePath, true);
                        UpdateAutoTabInUserSettings();
                        _settingsServiceXmlSerialization.AutoShownTabs =
                            _settingsServiceXmlSerialization.AutoShownTabs;         //To trigger ( PresenterMainSplContPanelUpTabs.SettingsServiceXmlSerialization_PropertyChanged) when new values
                    }
                    break;

                // ReSharper disable once RedundantAssignment
                case 1:
                    _ = new ValidateXml(_fileName, ValidateXml.XmlType.Manual);
                    if (ValidateXml.Passed)
                    {
                        File.Copy(_fileName, _destFilePath, true);
                        UpdateManualTabInUserSettings();
                        _settingsServiceXmlSerialization.ManuallyShownTabs =
                            _settingsServiceXmlSerialization.ManuallyShownTabs;         //To trigger ( PresenterMainSplContPanelUpTabs.SettingsServiceXmlSerialization_PropertyChanged) when new values
                    }
                    break;

                case 2:
                    File.Copy(_fileName, _destFilePath, true);
                    break;
                }
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(sender),
                                                      Resources.PresenterUserSettingsRightAdvanced_Dialog_ClickOkMouseButton_An_error_occured_while_Dialog_ClickOkMouseButton_);
            }
        }
Beispiel #3
0
        private void ValidatePropfile()
        {
            var result = ValidateXml.ValidateProfileFile(txtFile.Text);

            txtResult.Clear();
            if (result.HasErrors)
            {
                foreach (var strError in result.Errors)
                {
                    txtResult.AppendText(strError + Environment.NewLine);
                }
            }
            else
            {
                txtResult.Text = Properties.Resources.ValidatePluginSuccess;
            }
        }
        /// <summary>
        /// Creates a new instance of <see cref="profile"/> from <paramref name="xml"/>
        /// </summary>
        /// <param name="xml">The xml text that represents the profile</param>
        /// <returns><see cref="profile"/> instance</returns>
        /// <exception cref="System.Exception">If there are Validation errors with the xml</exception>
        public static profile FromXml(string xml)
        {
            ValidationResult vResult = ValidateXml.ValidateProfileXml(xml);

            if (vResult.HasErrors == true)
            {
                var ex = new System.Exception(vResult.ToString());
                throw ex;
            }

            XmlSerializer serializer = new XmlSerializer(typeof(profile));

            var reader = new System.IO.StringReader(xml);
            var p      = (profile)serializer.Deserialize(reader);

            reader.Close();
            return(p);
        }
Beispiel #5
0
        /// <summary>
        /// Creates a new instance of <see cref="root"/> from <paramref name="FileName"/>
        /// </summary>
        /// <param name="FileName">The FileName to the xml that represents the profile</param>
        /// <returns><see cref="root"/> instance</returns>
        /// <exception cref="System.Exception">If there are Validation errors with the xml</exception>
        public static plugin FromFile(string FileName)
        {
            ValidationResult vResult = ValidateXml.ValidatePluginFile(FileName);

            if (vResult.HasErrors == true)
            {
                var ex = new System.Exception(vResult.ToString());
                throw ex;
            }

            XmlSerializer serializer = new XmlSerializer(typeof(plugin));

            System.IO.StreamReader reader = new System.IO.StreamReader(FileName);
            var r = (plugin)serializer.Deserialize(reader);

            reader.Close();
            r.File = FileName;
            return(r);
        }
Beispiel #6
0
        /// <summary>
        /// Deletes all the plugins in the SnipitInstall and the Profile as well if DeleteProfle is true
        /// </summary>
        /// <param name="inst">SnippitInstall instance</param>
        /// <param name="DeleteProfile">If true the profile will also be deleted if all the plugins are deleted for this profile</param>
        /// <param name="DeleteEmpytFolders">If true then empty folder will be deleted as well</param>
        public static void DeleteInstal(SnippitInstal inst, bool DeleteProfile, bool DeleteEmpytFolders)
        {
            if (inst == null)
            {
                return;
            }
            if (inst.profile == null)
            {
                throw new ArgumentException("Inst.Profile cannot be null");
            }
            // bool hasProfile = inst.profile != null;
            if (inst.plugin != null)
            {
                bool SnipsFolderExist   = Directory.Exists(inst.profile.codeLanguage.paths.SnipsFullPath);
                bool PluginsFolderExist = Directory.Exists(inst.profile.codeLanguage.paths.PluginFullPath);
                if (Directory.Exists(inst.profile.codeLanguage.paths.PluginFullPath) == true)
                {
                    string searchP = "*.ahk";
                    var    files   = Directory.GetFiles(inst.profile.codeLanguage.paths.PluginFullPath, searchP);
                    Parallel.ForEach(files, f => {
                        File.Delete(f);
                    });
                    if (DeleteEmpytFolders == true && IsDirectoryEmpty(inst.profile.codeLanguage.paths.PluginFullPath) == true)
                    {
                        Directory.Delete(inst.profile.codeLanguage.paths.PluginFullPath);
                    }
                }

                if (Directory.Exists(inst.profile.codeLanguage.paths.PluginIncludeFullPath) == true)
                {
                    string searchP = "*.ahk";
                    var    files   = Directory.GetFiles(inst.profile.codeLanguage.paths.PluginIncludeFullPath, searchP);
                    Parallel.ForEach(files, f => {
                        File.Delete(f);
                    });
                    if (DeleteEmpytFolders == true && IsDirectoryEmpty(inst.profile.codeLanguage.paths.PluginIncludeFullPath) == true)
                    {
                        Directory.Delete(inst.profile.codeLanguage.paths.PluginIncludeFullPath);
                    }
                }

                if (Directory.Exists(inst.profile.codeLanguage.paths.SnippitInlinePath))
                {
                    string searchP = "*" + AppCommon.Instance.DefaultSnippitExt;
                    var    files   = Directory.GetFiles(inst.profile.codeLanguage.paths.SnippitInlinePath, searchP);
                    Parallel.ForEach(files, f => {
                        File.Delete(f);
                    });
                    if (DeleteEmpytFolders == true && IsDirectoryEmpty(inst.profile.codeLanguage.paths.SnippitInlinePath) == true)
                    {
                        Directory.Delete(inst.profile.codeLanguage.paths.SnippitInlinePath);
                    }
                }

                if (inst.plugin != null)
                {
                    foreach (var plg in inst.plugin)
                    {
                        if (string.IsNullOrEmpty(plg.File) == false && File.Exists(plg.File) == true)
                        {
                            File.Delete(plg.File);
                        }
                    }
                }

                //if (Directory.Exists(inst.profile.codeLanguage.paths.MainDataFullPath))
                //{
                //    string searchP = "*.xml";
                //    var files = Directory.GetFiles(inst.profile.codeLanguage.paths.MainDataFullPath, searchP);
                //    Parallel.ForEach(files, f => {
                //        File.Delete(f);
                //    });
                //}

                if (DeleteEmpytFolders == true && Directory.Exists(inst.profile.codeLanguage.paths.MainDataFullPath))
                {
                    if (IsDirectoryEmpty(inst.profile.codeLanguage.paths.MainDataFullPath))
                    {
                        Directory.Delete(inst.profile.codeLanguage.paths.MainDataFullPath);
                    }
                }
            }


            if (DeleteProfile == true)
            {
                // check and see if all the XML file have been removed from the data folder before deleting profile
                if (Directory.Exists(inst.profile.codeLanguage.paths.MainDataFullPath))
                {
                    // if the folder exist we have to test for any valid plugin
                    var  files        = Directory.GetFiles(inst.profile.codeLanguage.paths.MainDataFullPath, "*.xml");
                    bool bPluginExist = false;
                    foreach (var file in files)
                    {
                        var vResult = ValidateXml.ValidatePluginFile(file);
                        if (vResult.HasErrors == false)
                        {
                            bPluginExist = true;
                            break;
                        }
                    }
                    if ((bPluginExist == false) && (File.Exists(inst.profile.File)))
                    {
                        File.Delete(inst.profile.File);
                    }
                }
                else
                {
                    if (File.Exists(inst.profile.File))
                    {
                        File.Delete(inst.profile.File);
                    }
                }
            }
        }
Beispiel #7
0
        /// <summary>
        /// Saves a profile to disk
        /// </summary>
        /// <param name="p">The profile to save</param>
        /// <exception cref="Exceptions.ProfileVersionException">If Profile min version is greater than scrip version</exception>
        public static void SaveProfile(profile p)
        {
            lock (ProfileLock)
            {
                if (p.FullMinVersion > ScriptVersion)
                {
                    throw new Exceptions.ProfileVersionException(Properties.Resources.ErrorProfileMinVersionNewer);
                }
                string sFile = Path.Combine(AppCommon.Instance.PathProfiles, p.name + ".xml");

                if (string.IsNullOrEmpty(p.codeLanguage.paths.mainData))
                {
                    p.codeLanguage.paths.mainData = p.name;
                }
                else
                {
                    if (p.codeLanguage.paths.mainData.IndexOf(Path.DirectorySeparatorChar) > -1)
                    {
                        if (!Directory.Exists(p.codeLanguage.paths.mainData))
                        {
                            p.codeLanguage.paths.mainData = p.name;
                        }
                        else
                        {
                            sFile = Path.Combine(p.codeLanguage.paths.mainData, p.name + ".xml");
                        }
                    }
                    else
                    {
                        p.codeLanguage.paths.mainData = p.name;
                    }
                }
                // check for an existing profile before overwriting
                if (File.Exists(sFile))
                {
                    // test the existing profile to see if it is valid
                    // test to see if the current existing file is a valid plugin
                    ValidationResult vFile = ValidateXml.ValidateProfileFile(sFile);
                    if (vFile.HasErrors == false)
                    {
                        // now that we have an existing plugin and it is valid lets check is version against the current plugin version.
                        var ExistingProfile = profile.FromFile(sFile);
                        if (ExistingProfile.FullVersion >= p.FullVersion)
                        {
                            // The existing plugin version is newer or the same as this plugin version
                            // skip, not a newer version.
                            return;
                        }
                        else
                        {
                            ExistingProfile = null;
                        }
                    }
                }

                XmlSerializer writer = new XmlSerializer(typeof(profile));
                FileStream    file   = File.Create(sFile);
                writer.Serialize(file, p);
                file.Close();
            }
        }