/// <summary>
        /// Exports the settings.
        /// </summary>
        /// <returns>Returns the exported EditorProviderSettings</returns>
        private EditorProviderSettings ExportSettings()
        {
            var exportSettings = new EditorProviderSettings { SettingMode = SettingsMode.Default };

            exportSettings.SettingMode = this.CurrentSettingsMode;

            // Export all Editor config settings
            foreach (
                PropertyInfo info in
                    SettingsUtil.GetEditorConfigProperties())
            {
                switch (info.PropertyType.Name)
                {
                    case "String":
                        {
                            var textBox =
                                Utility.FindControl<TextBox>(this.EditorConfigHolder, info.Name);

                            if (!string.IsNullOrEmpty(textBox.Text))
                            {
                                info.SetValue(exportSettings.Config, textBox.Text, null);
                            }
                        }

                        break;
                    case "Int32":
                        {
                            var textBox =
                                Utility.FindControl<TextBox>(this.EditorConfigHolder, info.Name);

                            if (!string.IsNullOrEmpty(textBox.Text))
                            {
                                info.SetValue(exportSettings.Config, int.Parse(textBox.Text), null);
                            }
                        }

                        break;
                    case "Decimal":
                        {
                            var textBox =
                                Utility.FindControl<TextBox>(this.EditorConfigHolder, info.Name);

                            if (!string.IsNullOrEmpty(textBox.Text))
                            {
                                info.SetValue(exportSettings.Config, decimal.Parse(textBox.Text), null);
                            }
                        }

                        break;
                    case "Boolean":
                        {
                            var checkBox =
                                Utility.FindControl<CheckBox>(this.EditorConfigHolder, info.Name);

                            info.SetValue(
                                exportSettings.Config,
                                checkBox.Checked,
                                null);
                        }

                        break;
                }

                switch (info.Name)
                {
                    case "ToolbarLocation":
                        {
                            var dropDownList = Utility.FindControl<DropDownList>(this.EditorConfigHolder, info.Name);

                            if (dropDownList.SelectedItem != null)
                            {
                                info.SetValue(
                                    exportSettings.Config,
                                    (ToolBarLocation)Enum.Parse(typeof(ToolBarLocation), dropDownList.SelectedValue),
                                    null);
                            }
                        }

                        break;
                    case "DefaultLinkType":
                        {
                            var dropDownList = Utility.FindControl<DropDownList>(this.EditorConfigHolder, info.Name);

                            if (dropDownList.SelectedItem != null)
                            {
                                info.SetValue(
                                    exportSettings.Config,
                                    (LinkType)Enum.Parse(typeof(LinkType), dropDownList.SelectedValue),
                                    null);
                            }
                        }

                        break;
                    case "EnterMode":
                    case "ShiftEnterMode":
                        {
                            var dropDownList = Utility.FindControl<DropDownList>(this.EditorConfigHolder, info.Name);

                            if (dropDownList.SelectedItem != null)
                            {
                                info.SetValue(
                                    exportSettings.Config,
                                    (EnterModus)Enum.Parse(typeof(EnterModus), dropDownList.SelectedValue),
                                    null);
                            }
                        }

                        break;
                    case "ContentsLangDirection":
                        {
                            var dropDownList = Utility.FindControl<DropDownList>(this.EditorConfigHolder, info.Name);

                            if (dropDownList.SelectedItem != null)
                            {
                                info.SetValue(
                                    exportSettings.Config,
                                    (LanguageDirection)Enum.Parse(typeof(LanguageDirection), dropDownList.SelectedValue),
                                    null);
                            }
                        }

                        break;
                    case "CodeMirror":
                        {
                            foreach (var codeMirrorInfo in
                                typeof(CodeMirror).GetProperties()
                                    .Where(codeMirrorInfo => !codeMirrorInfo.Name.Equals("Theme")))
                            {
                                switch (codeMirrorInfo.PropertyType.Name)
                                {
                                    case "String":
                                        {
                                            var textBox = Utility.FindControl<TextBox>(
                                                this.EditorConfigHolder,
                                                codeMirrorInfo.Name);

                                            if (!string.IsNullOrEmpty(textBox.Text))
                                            {
                                                codeMirrorInfo.SetValue(
                                                    exportSettings.Config.CodeMirror,
                                                    textBox.Text,
                                                    null);
                                            }
                                        }

                                        break;

                                    case "Boolean":
                                        {
                                            var checkBox = Utility.FindControl<CheckBox>(
                                                this.EditorConfigHolder,
                                                codeMirrorInfo.Name);

                                            codeMirrorInfo.SetValue(
                                                exportSettings.Config.CodeMirror,
                                                checkBox.Checked,
                                                null);
                                        }

                                        break;
                                }
                            }
                        }

                        break;
                    case "WordCount":
                        {
                            foreach (var wordCountInfo in typeof(WordCountConfig).GetProperties())
                            {
                                switch (wordCountInfo.PropertyType.Name)
                                {
                                    case "String":
                                        {
                                            var textBox = Utility.FindControl<TextBox>(
                                                this.EditorConfigHolder,
                                                wordCountInfo.Name);

                                            if (!string.IsNullOrEmpty(textBox.Text))
                                            {
                                                wordCountInfo.SetValue(
                                                    exportSettings.Config.WordCount,
                                                    textBox.Text,
                                                    null);
                                            }
                                        }

                                        break;

                                    case "Boolean":
                                        {
                                            var checkBox = Utility.FindControl<CheckBox>(
                                                this.EditorConfigHolder,
                                                wordCountInfo.Name);

                                            wordCountInfo.SetValue(
                                                exportSettings.Config.WordCount,
                                                checkBox.Checked,
                                                null);
                                        }

                                        break;
                                }
                            }
                        }

                        break;
                }
            }
            ///////////////////

            exportSettings.Config.Skin = this.ddlSkin.SelectedValue;
            exportSettings.Config.CodeMirror.Theme = this.CodeMirrorTheme.SelectedValue;
            exportSettings.Browser = this.ddlBrowser.SelectedValue;
            exportSettings.FileListViewMode =
                (FileListView)Enum.Parse(typeof(FileListView), this.FileListViewMode.SelectedValue);
            exportSettings.DefaultLinkMode =
                (LinkMode)Enum.Parse(typeof(LinkMode), this.DefaultLinkMode.SelectedValue);
            exportSettings.UseAnchorSelector = this.UseAnchorSelector.Checked;
            exportSettings.ShowPageLinksTabFirst = this.ShowPageLinksTabFirst.Checked;
            exportSettings.OverrideFileOnUpload = this.OverrideFileOnUpload.Checked;
            exportSettings.SubDirs = this.cbBrowserDirs.Checked;
            exportSettings.BrowserRootDirId = int.Parse(this.BrowserRootDir.SelectedValue);
            exportSettings.UploadDirId = int.Parse(this.UploadDir.SelectedValue);

            if (Utility.IsNumeric(this.FileListPageSize.Text))
            {
                exportSettings.FileListPageSize = int.Parse(this.FileListPageSize.Text);
            }

            if (Utility.IsNumeric(this.txtResizeWidth.Text))
            {
                exportSettings.ResizeWidth = int.Parse(this.txtResizeWidth.Text);
            }

            if (Utility.IsNumeric(this.txtResizeHeight.Text))
            {
                exportSettings.ResizeHeight = int.Parse(this.txtResizeHeight.Text);
            }

            exportSettings.InjectSyntaxJs = this.InjectSyntaxJs.Checked;

            if (Utility.IsUnit(this.txtWidth.Text))
            {
                exportSettings.EditorWidth = this.txtWidth.Text;
            }

            if (Utility.IsUnit(this.txtHeight.Text))
            {
                exportSettings.EditorHeight = this.txtHeight.Text;
            }

            exportSettings.BlankText = this.txtBlanktext.Text;
            exportSettings.Config.StylesSet = this.StylesURL.Url;
            exportSettings.Config.ContentsCss = this.CssUrl.Url;
            exportSettings.Config.Templates_Files = this.TemplUrl.Url;
            exportSettings.CustomJsFile = this.CustomJsFile.Url;
            exportSettings.Config.CustomConfig = this.ConfigUrl.Url;

            string sRoles = this.chblBrowsGr.Items.Cast<ListItem>().Where(item => item.Selected).Aggregate(
                string.Empty, (current, item) => current + (item.Value + ";"));

            if (sRoles != string.Empty)
            {
                exportSettings.BrowserRoles = sRoles;
            }

            List<ToolbarRoles> listToolbarRoles = new List<ToolbarRoles>();

            // Save Toolbar Setting for every Role
            for (int i = 0; i < this.gvToolbars.Rows.Count; i++)
            {
                Label label = (Label)this.gvToolbars.Rows[i].Cells[0].FindControl("lblRoleName");

                DropDownList ddLToolB = (DropDownList)this.gvToolbars.Rows[i].Cells[1].FindControl("ddlToolbars");

                if (label == null || ddLToolB == null)
                {
                    continue;
                }

                if (label.Text.Equals("Unauthenticated Users"))
                {
                    listToolbarRoles.Add(new ToolbarRoles { RoleId = -1, Toolbar = ddLToolB.SelectedValue });
                }
                else
                {
                    RoleInfo objRole = this.objRoleController.GetRoleByName(this._portalSettings.PortalId, label.Text);

                    listToolbarRoles.Add(new ToolbarRoles { RoleId = objRole.RoleID, Toolbar = ddLToolB.SelectedValue });
                }
            }

            exportSettings.ToolBarRoles = listToolbarRoles;

            return exportSettings;
        }
        /// <summary>
        /// Fills the setting controls with the loaded Setting Values.
        /// </summary>
        /// <param name="importedSettings">The imported settings.</param>
        /// <param name="changeMode">if set to <c>true</c> [change mode].</param>
        private void FillSettings(EditorProviderSettings importedSettings, bool changeMode = true)
        {
            // Editor config settings
            foreach (
                PropertyInfo info in
                    SettingsUtil.GetEditorConfigProperties())
            {
                object value = null;

                if (!info.Name.Equals("CodeMirror") && !info.Name.Equals("WordCount"))
                {
                    value = info.GetValue(importedSettings.Config, null);

                    if (value == null)
                    {
                        continue;
                    }
                }

                switch (info.PropertyType.Name)
                {
                    case "Decimal":
                    case "Int32":
                    case "String":
                        {
                            var textBox = Utility.FindControl<TextBox>(this.EditorConfigHolder, info.Name);

                            if (textBox != null)
                            {
                                textBox.Text = value.ToString();
                            }
                        }

                        break;
                    case "Boolean":
                        {
                            var checkBox = Utility.FindControl<CheckBox>(this.EditorConfigHolder, info.Name);

                            if (checkBox != null)
                            {
                                checkBox.Checked = (bool)value;
                            }
                        }

                        break;
                }

                switch (info.Name)
                {
                    case "ContentsLangDirection":
                    case "EnterMode":
                    case "ShiftEnterMode":
                    case "ToolbarLocation":
                    case "DefaultLinkType":
                        {
                            var dropDownList = Utility.FindControl<DropDownList>(this.EditorConfigHolder, info.Name);

                            if (dropDownList != null)
                            {
                                if (dropDownList.Items.FindByValue(value.ToString()) != null)
                                {
                                    dropDownList.ClearSelection();
                                    dropDownList.Items.FindByValue(value.ToString()).Selected = true;
                                }
                            }
                        }

                        break;
                    case "CodeMirror":
                        {
                            foreach (
                           var codeMirrorInfo in
                               typeof(CodeMirror).GetProperties()
                                                 .Where(codeMirrorInfo => !codeMirrorInfo.Name.Equals("Theme")))
                            {
                                value = codeMirrorInfo.GetValue(importedSettings.Config.CodeMirror, null);

                                if (value == null)
                                {
                                    continue;
                                }

                                switch (codeMirrorInfo.PropertyType.Name)
                                {
                                    case "String":
                                        {
                                            var textBox = Utility.FindControl<TextBox>(this.EditorConfigHolder, codeMirrorInfo.Name);

                                            if (textBox != null)
                                            {
                                                textBox.Text = value.ToString();
                                            }
                                        }

                                        break;

                                    case "Boolean":
                                        {
                                            var checkBox = Utility.FindControl<CheckBox>(this.EditorConfigHolder, codeMirrorInfo.Name);

                                            if (checkBox != null)
                                            {
                                                checkBox.Checked = (bool)value;
                                            }
                                        }

                                        break;
                                }
                            }
                        }

                        break;
                    case "WordCount":
                        {
                             foreach (var wordCountInfo in typeof(WordCountConfig).GetProperties())
                             {
                                 value = wordCountInfo.GetValue(importedSettings.Config.WordCount, null);

                                 if (value == null)
                                 {
                                     continue;
                                 }

                                 switch (wordCountInfo.PropertyType.Name)
                                 {
                                     case "String":
                                         {
                                             var textBox = Utility.FindControl<TextBox>(this.EditorConfigHolder, wordCountInfo.Name);

                                             if (textBox != null)
                                             {
                                                 textBox.Text = value.ToString();
                                             }
                                         }

                                         break;

                                     case "Boolean":
                                         {
                                             var checkBox = Utility.FindControl<CheckBox>(this.EditorConfigHolder, wordCountInfo.Name);

                                             if (checkBox != null)
                                             {
                                                 checkBox.Checked = (bool)value;
                                             }
                                         }

                                         break;
                                 }
                             }
                        }

                        break;
                }
            }
            ///////////////////

            if (!string.IsNullOrEmpty(importedSettings.Config.Skin)
                && this.ddlSkin.Items.FindByValue(importedSettings.Config.Skin) != null)
            {
                this.ddlSkin.ClearSelection();
                this.ddlSkin.SelectedValue = importedSettings.Config.Skin;
            }

            if (!string.IsNullOrEmpty(importedSettings.Config.CodeMirror.Theme)
                && this.CodeMirrorTheme.Items.FindByValue(importedSettings.Config.CodeMirror.Theme) != null)
            {
                this.CodeMirrorTheme.ClearSelection();
                this.CodeMirrorTheme.SelectedValue = importedSettings.Config.CodeMirror.Theme;
            }

            if (!string.IsNullOrEmpty(importedSettings.Browser)
                && this.ddlBrowser.Items.FindByValue(importedSettings.Browser) != null)
            {
                this.ddlBrowser.ClearSelection();
                this.ddlBrowser.SelectedValue = importedSettings.Browser;
            }

            this.FileListPageSize.Text = importedSettings.FileListPageSize.ToString();

            this.FileListViewMode.SelectedValue = importedSettings.FileListViewMode.ToString();
            this.DefaultLinkMode.SelectedValue = importedSettings.DefaultLinkMode.ToString();
            this.UseAnchorSelector.Checked = importedSettings.UseAnchorSelector;
            this.ShowPageLinksTabFirst.Checked = importedSettings.ShowPageLinksTabFirst;

            this.cbBrowserDirs.Checked = importedSettings.SubDirs;

            this.OverrideFileOnUpload.Checked = importedSettings.OverrideFileOnUpload;

            this.BrowserRootDir.SelectedValue =
                 this.BrowserRootDir.Items.FindByValue(importedSettings.BrowserRootDirId.ToString()) != null
                     ? importedSettings.BrowserRootDirId.ToString()
                     : "-1";

            this.UploadDir.SelectedValue = this.UploadDir.Items.FindByValue(importedSettings.UploadDirId.ToString())
                                           != null
                                               ? importedSettings.UploadDirId.ToString()
                                               : "-1";

            var configFolderInfo =
                Utility.ConvertFilePathToFolderInfo(
                    !string.IsNullOrEmpty(this.configFolder)
                        ? Path.Combine(this._portalSettings.HomeDirectoryMapPath, this.configFolder)
                        : this._portalSettings.HomeDirectoryMapPath,
                    this._portalSettings);

            this.ExportDir.SelectedValue = configFolderInfo != null
                                           &&
                                           this.ExportDir.Items.FindByValue(configFolderInfo.FolderID.ToString())
                                           != null
                                               ? configFolderInfo.FolderID.ToString()
                                               : "-1";

            this.ExportFileName.Text = string.Format("CKEditorSettings-{0}.xml", importedSettings.SettingMode);

            switch (importedSettings.SettingMode)
            {
                case SettingsMode.Portal:
                    this.ExportFileName.Text = string.Format(
                        "CKEditorSettings-{0}-{1}.xml", importedSettings.SettingMode, this._portalSettings.PortalId);
                    break;
                case SettingsMode.Page:
                    this.ExportFileName.Text = string.Format(
                        "CKEditorSettings-{0}-{1}.xml", importedSettings.SettingMode, this.CurrentOrSelectedTabId);
                    break;
                case SettingsMode.ModuleInstance:
                    this.ExportFileName.Text = string.Format(
                        "CKEditorSettings-{0}-{1}.xml", importedSettings.SettingMode, this.ModuleId);
                    break;
            }

            this.txtResizeHeight.Text = importedSettings.ResizeWidth.ToString();

            this.txtResizeHeight.Text = importedSettings.ResizeHeight.ToString();

            this.InjectSyntaxJs.Checked = importedSettings.InjectSyntaxJs;

            if (Utility.IsUnit(importedSettings.Config.Width))
            {
                this.txtWidth.Text = importedSettings.Config.Width;
            }

            if (Utility.IsUnit(importedSettings.Config.Height))
            {
                this.txtHeight.Text = importedSettings.Config.Height;
            }

            if (!string.IsNullOrEmpty(importedSettings.BlankText))
            {
                this.txtBlanktext.Text = importedSettings.BlankText;
            }

            List<ToolbarRoles> imporToolbarRoles = importedSettings.ToolBarRoles;

            // Load Toolbar Setting for Each Portal Role
            foreach (ToolbarRoles objToolbRoles in imporToolbarRoles)
            {
                if (objToolbRoles.RoleId.Equals(-1))
                {
                    for (int i = 0; i < this.gvToolbars.Rows.Count; i++)
                    {
                        Label label = (Label)this.gvToolbars.Rows[i].Cells[0].FindControl("lblRoleName");

                        if (label == null || !label.Text.Equals("Unauthenticated Users"))
                        {
                            continue;
                        }

                        DropDownList ddLToolB =
                            (DropDownList)this.gvToolbars.Rows[i].Cells[1].FindControl("ddlToolbars");

                        ddLToolB.ClearSelection();

                        if (ddLToolB.Items.FindByValue(objToolbRoles.Toolbar) != null)
                        {
                            ddLToolB.SelectedValue = objToolbRoles.Toolbar;
                        }
                    }
                }
                else
                {
                    RoleInfo objRole = this.objRoleController.GetRole(
                        objToolbRoles.RoleId, this._portalSettings.PortalId);

                    if (objRole == null)
                    {
                        continue;
                    }

                    for (int i = 0; i < this.gvToolbars.Rows.Count; i++)
                    {
                        Label label = (Label)this.gvToolbars.Rows[i].Cells[0].FindControl("lblRoleName");

                        if (label == null || !label.Text.Equals(objRole.RoleName))
                        {
                            continue;
                        }

                        DropDownList ddLToolB =
                            (DropDownList)this.gvToolbars.Rows[i].Cells[1].FindControl("ddlToolbars");

                        ddLToolB.ClearSelection();

                        if (ddLToolB.Items.FindByValue(objToolbRoles.Toolbar) != null)
                        {
                            ddLToolB.SelectedValue = objToolbRoles.Toolbar;
                        }
                    }
                }
            }

            if (!string.IsNullOrEmpty(importedSettings.Config.StylesSet))
            {
                this.StylesURL.Url = this.ReFormatURL(importedSettings.Config.StylesSet);
            }

            if (!string.IsNullOrEmpty(importedSettings.Config.ContentsCss))
            {
                this.CssUrl.Url = this.ReFormatURL(importedSettings.Config.ContentsCss);
            }

            if (!string.IsNullOrEmpty(importedSettings.Config.Templates_Files))
            {
                this.TemplUrl.Url = this.ReFormatURL(importedSettings.Config.Templates_Files);
            }

            if (!string.IsNullOrEmpty(importedSettings.CustomJsFile))
            {
                this.CustomJsFile.Url = this.ReFormatURL(importedSettings.CustomJsFile);
            }

            if (!string.IsNullOrEmpty(importedSettings.Config.CustomConfig))
            {
                this.ConfigUrl.Url = this.ReFormatURL(importedSettings.Config.CustomConfig);
            }

            if (!string.IsNullOrEmpty(importedSettings.BrowserRoles))
            {
                string sRoles = importedSettings.BrowserRoles;

                if (sRoles.Length >= 1 && sRoles.Contains(";"))
                {
                    string[] roles = sRoles.Split(';');

                    foreach (string sRoleName in roles)
                    {
                        if (Utility.IsNumeric(sRoleName))
                        {
                            if (this.chblBrowsGr.Items.FindByValue(sRoleName) != null)
                            {
                                this.chblBrowsGr.Items.FindByValue(sRoleName).Selected = true;
                            }
                        }
                        else
                        {
                            if (this.chblBrowsGr.Items.FindByText(sRoleName) != null)
                            {
                                this.chblBrowsGr.Items.FindByText(sRoleName).Selected = true;
                            }
                        }
                    }
                }
            }

            if (!changeMode)
            {
                return;
            }

            switch (importedSettings.SettingMode)
            {
                case SettingsMode.Portal:
                    this.rBlSetMode.SelectedIndex = 0;
                    break;
                case SettingsMode.Page:
                    this.rBlSetMode.SelectedIndex = 1;
                    break;
                case SettingsMode.ModuleInstance:
                    this.rBlSetMode.SelectedIndex = 2;
                    break;
            }
        }
        /// <summary>
        /// Load All Editor Settings
        /// </summary>
        /// <param name="currentMode">The current mode.</param>
        /// <param name="changeMode">if set to <c>true</c> [change mode].</param>
        private void LoadSettings(int currentMode, bool changeMode = true)
        {
            this.CurrentSettingsMode = (SettingsMode)Enum.Parse(typeof(SettingsMode), currentMode.ToString());

            this.lnkRemoveAll.Visible = !currentMode.Equals(0);
            this.lnkRemoveChild.Visible = !currentMode.Equals(0);
            this.CopyToAllChild.Visible = !currentMode.Equals(0);

            this.lnkRemove.Text = string.Format(
                Localization.GetString("Remove.Text", this.ResXFile, this.LangCode),
                this.rBlSetMode.Items[currentMode].Text);
            this.lnkRemoveAll.Text =
                string.Format(
                    Localization.GetString("RemoveAll.Text", this.ResXFile, this.LangCode),
                    this.rBlSetMode.Items[currentMode].Text);

            this.lnkRemove.ToolTip = string.Format(
                Localization.GetString("Remove.Help", this.ResXFile, this.LangCode),
                this.rBlSetMode.Items[currentMode].Text);
            this.lnkRemoveAll.ToolTip =
                string.Format(
                    Localization.GetString("RemoveAll.Help", this.ResXFile, this.LangCode),
                    this.rBlSetMode.Items[currentMode].Text);

            this.LoadDefaultSettings();

            var settingsDictionary = Utility.GetEditorHostSettings();
            var portalRoles = new RoleController().GetPortalRoles(this._portalSettings.PortalId);

            var portalKey = string.Format("DNNCKP#{0}#", this._portalSettings.PortalId);
            var pageKey = string.Format("DNNCKT#{0}#", this.CurrentOrSelectedTabId);
            var moduleKey = string.Format("DNNCKMI#{0}#INS#{1}#", this.ModuleId, this.moduleInstanceName);

            var providerConfiguration = ProviderConfiguration.GetProviderConfiguration("htmlEditor");
            var objProvider = (Provider)providerConfiguration.Providers[providerConfiguration.DefaultProvider];

            var currentSettings = SettingsUtil.GetDefaultSettings(
                this._portalSettings,
                this._portalSettings.HomeDirectoryMapPath,
                objProvider.Attributes["ck_configFolder"],
                portalRoles);

            switch (this.CurrentSettingsMode)
            {
                case SettingsMode.Portal:
                    {
                        // Load Portal Settings ?!
                        if (SettingsUtil.CheckExistsPortalOrPageSettings(settingsDictionary, portalKey))
                        {
                            currentSettings = new EditorProviderSettings();

                            currentSettings = SettingsUtil.LoadPortalOrPageSettings(
                                this._portalSettings, currentSettings, settingsDictionary, portalKey, portalRoles);

                            // Set Current Mode to Portal
                            currentSettings.SettingMode = SettingsMode.Portal;

                            this.lnkRemove.Enabled = true;
                        }
                        else
                        {
                            this.lnkRemove.Enabled = false;
                        }
                    }

                    break;
                case SettingsMode.Page:
                    {
                        // Load Page Settings ?!
                        if (SettingsUtil.CheckExistsPortalOrPageSettings(settingsDictionary, pageKey))
                        {
                            currentSettings = new EditorProviderSettings();

                            currentSettings = SettingsUtil.LoadPortalOrPageSettings(
                                this._portalSettings, currentSettings, settingsDictionary, pageKey, portalRoles);

                            // Set Current Mode to Page
                            currentSettings.SettingMode = SettingsMode.Page;

                            this.lnkRemove.Enabled = true;
                        }
                        else
                        {
                            this.lnkRemove.Enabled = false;
                        }

                        var currentTab = new TabController().GetTab(
                            this.CurrentOrSelectedTabId, this._portalSettings.PortalId, false);

                        this.lnkRemoveChild.Enabled = currentTab.HasChildren;

                        this.lnkRemoveChild.Text = Localization.GetString(
                                "RemovePageChild.Text", this.ResXFile, this.LangCode);
                        this.lnkRemoveChild.ToolTip = Localization.GetString(
                            "RemovePageChild.Help", this.ResXFile, this.LangCode);

                        this.CopyToAllChild.Enabled = currentTab.HasChildren;

                        this.CopyToAllChild.Text = Localization.GetString(
                                "CopyPageChild.Text", this.ResXFile, this.LangCode);
                        this.CopyToAllChild.ToolTip = Localization.GetString(
                            "CopyPageChild.Help", this.ResXFile, this.LangCode);
                    }

                    break;
                case SettingsMode.ModuleInstance:
                    {
                        // Load Module Settings ?!
                        if (SettingsUtil.CheckExistsModuleInstanceSettings(moduleKey, this.ModuleId))
                        {
                            currentSettings = new EditorProviderSettings();

                            currentSettings = SettingsUtil.LoadModuleSettings(
                                this._portalSettings, currentSettings, moduleKey, this.ModuleId, portalRoles);

                            currentSettings.SettingMode = SettingsMode.ModuleInstance;

                            this.lnkRemove.Enabled = true;
                        }
                        else
                        {
                            this.lnkRemove.Enabled = false;
                        }

                        this.lnkRemoveChild.Enabled = true;

                        this.lnkRemoveChild.Text = Localization.GetString(
                            "RemoveModuleChild.Text", this.ResXFile, this.LangCode);
                        this.lnkRemoveChild.ToolTip = Localization.GetString(
                            "RemoveModuleChild.Help", this.ResXFile, this.LangCode);
                    }

                    break;
            }

            if (currentSettings != null)
            {
                this.FillSettings(currentSettings, changeMode);
            }
        }
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            this.SortAscending.CssClass = this.SortFilesDescending ? "ButtonNormal" : "ButtonSelected";
            this.SortDescending.CssClass = !this.SortFilesDescending ? "ButtonNormal" : "ButtonSelected";

            this.extensionWhiteList = HostController.Instance.GetString("FileExtensions").ToLower();

            if (!string.IsNullOrEmpty(this.request.QueryString["mode"]))
            {
                this.currentSettings.SettingMode =
                    (SettingsMode)Enum.Parse(typeof(SettingsMode), this.request.QueryString["mode"]);
            }

            ProviderConfiguration providerConfiguration = ProviderConfiguration.GetProviderConfiguration("htmlEditor");
            Provider objProvider = (Provider)providerConfiguration.Providers[providerConfiguration.DefaultProvider];

            var settingsDictionary = Utility.GetEditorHostSettings();
            var portalRoles = new RoleController().GetPortalRoles(this._portalSettings.PortalId);

            switch (this.currentSettings.SettingMode)
            {
                case SettingsMode.Default:
                    // Load Default Settings
                    this.currentSettings = SettingsUtil.GetDefaultSettings(
                        this._portalSettings,
                        this._portalSettings.HomeDirectoryMapPath,
                        objProvider.Attributes["ck_configFolder"],
                        portalRoles);
                    break;
                case SettingsMode.Portal:
                    this.currentSettings = SettingsUtil.LoadPortalOrPageSettings(
                        this._portalSettings,
                        this.currentSettings,
                        settingsDictionary,
                        string.Format("DNNCKP#{0}#", this.request.QueryString["PortalID"]),
                        portalRoles);
                    break;
                case SettingsMode.Page:
                    this.currentSettings = SettingsUtil.LoadPortalOrPageSettings(
                        this._portalSettings,
                        this.currentSettings,
                        settingsDictionary,
                        string.Format("DNNCKT#{0}#", this.request.QueryString["tabid"]),
                        portalRoles);
                    break;
                case SettingsMode.ModuleInstance:
                    this.currentSettings = SettingsUtil.LoadModuleSettings(
                        this._portalSettings,
                        this.currentSettings,
                        string.Format(
                            "DNNCKMI#{0}#INS#{1}#", this.request.QueryString["mid"], this.request.QueryString["ckId"]),
                        int.Parse(this.request.QueryString["mid"]),
                        portalRoles);
                    break;
            }

            if (this.currentSettings.BrowserMode.Equals(Constants.Browser.StandardBrowser)
                && HttpContext.Current.Request.IsAuthenticated)
            {
                string command = null;

                try
                {
                    if (this.request.QueryString["Command"] != null)
                    {
                        command = this.request.QueryString["Command"];
                    }
                }
                catch (Exception)
                {
                    command = null;
                }

                try
                {
                    if (this.request.QueryString["Type"] != null)
                    {
                        this.browserModus = this.request.QueryString["Type"];
                        this.lblModus.Text = string.Format("Browser-Modus: {0}", this.browserModus);

                        if (!this.IsPostBack)
                        {
                            this.OverrideFile.Checked = this.currentSettings.OverrideFileOnUpload;

                            this.title.InnerText = string.Format("{0} - WatchersNET.FileBrowser", this.lblModus.Text);

                            this.AnchorList.Visible = this.currentSettings.UseAnchorSelector;
                            this.LabelAnchor.Visible = this.currentSettings.UseAnchorSelector;

                            this.ListViewState.Value = this.currentSettings.FileListViewMode.ToString();

                            // Set default link mode
                            switch (this.currentSettings.DefaultLinkMode)
                            {
                                case LinkMode.RelativeURL:
                                    this.rblLinkType.SelectedValue = "relLink";
                                    break;
                                case LinkMode.AbsoluteURL:
                                    this.rblLinkType.SelectedValue = "absLnk";
                                    break;
                                case LinkMode.RelativeSecuredURL:
                                    this.rblLinkType.SelectedValue = "lnkClick";
                                    break;
                                case LinkMode.AbsoluteSecuredURL:
                                    this.rblLinkType.SelectedValue = "lnkAbsClick";
                                    break;
                            }

                            switch (this.browserModus)
                            {
                                case "Link":
                                    this.BrowserMode.Visible = true;

                                    if (this.currentSettings.ShowPageLinksTabFirst)
                                    {
                                        this.BrowserMode.SelectedValue = "page";
                                        this.panLinkMode.Visible = false;
                                        this.panPageMode.Visible = true;

                                        this.TrackClicks.Visible = false;
                                        this.lblModus.Text = string.Format(
                                            "Browser-Modus: {0}", string.Format("Page {0}", this.browserModus));
                                        this.title.InnerText = string.Format(
                                            "{0} - WatchersNET.FileBrowser", this.lblModus.Text);

                                        this.RenderTabs();
                                    }
                                    else
                                    {
                                        this.BrowserMode.SelectedValue = "file";
                                        this.panPageMode.Visible = false;
                                    }

                                    break;
                                case "Image":
                                    this.BrowserMode.Visible = false;
                                    this.panPageMode.Visible = false;
                                    break;
                                case "Flash":
                                    this.BrowserMode.Visible = false;
                                    this.panPageMode.Visible = false;
                                    break;
                                default:
                                    this.BrowserMode.Visible = false;
                                    this.panPageMode.Visible = false;
                                    break;
                            }
                        }
                    }
                }
                catch (Exception)
                {
                    this.browserModus = null;
                }

                if (command != null)
                {
                    if (!command.Equals("FileUpload") && !command.Equals("FlashUpload")
                        && !command.Equals("ImageUpload"))
                    {
                        return;
                    }

                    var uploadedFile =
                        HttpContext.Current.Request.Files[HttpContext.Current.Request.Files.AllKeys[0]];

                    if (uploadedFile != null)
                    {
                        this.UploadFile(uploadedFile, command);
                    }
                }
                else
                {
                    if (!this.IsPostBack)
                    {
                        this.SetLanguage();

                        this.GetLanguageList();

                        var startFolder = this.StartingDir();

                        if (!Utility.IsInRoles(this._portalSettings.AdministratorRoleName, this._portalSettings))
                        {
                            // Hide physical file Path
                            this.lblCurrentDir.Visible = false;
                            this.lblCurrent.Visible = false;
                        }

                        this.FillFolderTree(startFolder);

                        bool folderSelected = false;

                        if (!string.IsNullOrEmpty(ckFileUrl))
                        {
                            try
                            {
                                folderSelected = this.SelectFolderFile(ckFileUrl);
                                ckFileUrl = null;
                            }
                            catch (Exception)
                            {
                                folderSelected = false;
                                ckFileUrl = null;
                            }
                        }

                        if (!folderSelected)
                        {
                            this.lblCurrentDir.Text = startFolder.PhysicalPath;

                            this.ShowFilesIn(startFolder);
                        }
                    }

                    this.FillQualityPrecentages();
                }
            }
            else
            {
                var errorScript = string.Format(
                    "javascript:alert('{0}');self.close();",
                    Localization.GetString("Error1.Text", this.ResXFile, this.LanguageCode));

                this.Response.Write("<script type=\"text/javascript\">");
                this.Response.Write(errorScript);
                this.Response.Write("</script>");

                this.Response.End();
            }
        }
        /// <summary>
        /// Load Portal/Page/Module Settings
        /// </summary>
        private void LoadAllSettings()
        {
            var settingsDictionary = Utility.GetEditorHostSettings();
            var portalRoles = new RoleController().GetPortalRoles(this._portalSettings.PortalId);

            // Load Default Settings
            this.currentSettings = SettingsUtil.GetDefaultSettings(
                this._portalSettings, this._portalSettings.HomeDirectoryMapPath, this._settings["configFolder"], portalRoles);

            // Set Current Mode to Default
            this.currentSettings.SettingMode = SettingsMode.Default;

            var portalKey = string.Format("DNNCKP#{0}#", this._portalSettings.PortalId);
            var pageKey = string.Format("DNNCKT#{0}#", this._portalSettings.ActiveTab.TabID);
            var moduleKey = string.Format("DNNCKMI#{0}#INS#{1}#", this.parentModulId, this.ID);

            // Load Portal Settings ?!
            if (SettingsUtil.CheckExistsPortalOrPageSettings(settingsDictionary, portalKey))
            {
                this.currentSettings = SettingsUtil.LoadPortalOrPageSettings(
                    this._portalSettings, this.currentSettings, settingsDictionary, portalKey, portalRoles);

                // Set Current Mode to Portal
                this.currentSettings.SettingMode = SettingsMode.Portal;
            }

            // Load Page Settings ?!
            if (SettingsUtil.CheckExistsPortalOrPageSettings(settingsDictionary, pageKey))
            {
                this.currentSettings = SettingsUtil.LoadPortalOrPageSettings(
                    this._portalSettings, this.currentSettings, settingsDictionary, pageKey, portalRoles);

                // Set Current Mode to Page
                this.currentSettings.SettingMode = SettingsMode.Page;
            }

            // Load Module Settings ?!
            if (!SettingsUtil.CheckExistsModuleInstanceSettings(moduleKey, this.parentModulId))
            {
                return;
            }

            this.currentSettings = SettingsUtil.LoadModuleSettings(
                this._portalSettings, this.currentSettings, moduleKey, this.parentModulId, portalRoles);

            // Set Current Mode to Module Instance
            this.currentSettings.SettingMode = SettingsMode.ModuleInstance;
        }
        /// <summary>
        /// Creates the default settings file.
        /// </summary>
        internal static void CreateDefaultSettingsFile()
        {
            var newSettings = new EditorProviderSettings();

            var serializer = new XmlSerializer(typeof(EditorProviderSettings));

            var textWriter =
                new StreamWriter(
                    new FileStream(
                        Path.Combine(Globals.HostMapPath, SettingConstants.XmlDefaultFileName),
                        FileMode.OpenOrCreate,
                        FileAccess.ReadWrite,
                        FileShare.ReadWrite));

            serializer.Serialize(textWriter, newSettings);

            textWriter.Close();
        }
        /// <summary>
        /// Loads the portal or page settings.
        /// </summary>
        /// <param name="portalSettings">The current portal settings.</param>
        /// <param name="currentSettings">The current settings.</param>
        /// <param name="editorHostSettings">The editor host settings.</param>
        /// <param name="key">The Portal or Page key.</param>
        /// <param name="portalRoles">The Portal Roles</param>
        /// <returns>
        /// Returns the Filled Settings
        /// </returns>
        internal static EditorProviderSettings LoadPortalOrPageSettings(
            PortalSettings portalSettings,
            EditorProviderSettings currentSettings,
            List<EditorHostSetting> editorHostSettings,
            string key,
            ArrayList portalRoles)
        {
            var roleController = new RoleController();

            var roles = new ArrayList();

            // Import all Editor config settings
            foreach (PropertyInfo info in GetEditorConfigProperties())
            {
                if (!editorHostSettings.Any(s => s.Name.Equals(string.Format("{0}{1}", key, info.Name))))
                {
                    continue;
                }

                if (
                    string.IsNullOrEmpty(
                        editorHostSettings.FirstOrDefault(s => s.Name.Equals(string.Format("{0}{1}", key, info.Name)))
                            .Value))
                {
                    continue;
                }

                if (!info.Name.Equals("CodeMirror") && !info.Name.Equals("WordCount"))
                {
                    continue;
                }

                var settingValue =
                    editorHostSettings.FirstOrDefault(
                        setting => setting.Name.Equals(string.Format("{0}{1}", key, info.Name))).Value;

                switch (info.PropertyType.Name)
                {
                    case "String":
                        info.SetValue(currentSettings.Config, settingValue, null);
                        break;
                    case "Int32":
                        info.SetValue(currentSettings.Config, int.Parse(settingValue), null);
                        break;
                    case "Decimal":
                        info.SetValue(currentSettings.Config, decimal.Parse(settingValue), null);
                        break;
                    case "Boolean":
                        info.SetValue(currentSettings.Config, bool.Parse(settingValue), null);
                        break;
                }

                switch (info.Name)
                {
                    case "ToolbarLocation":
                        info.SetValue(
                            currentSettings.Config,
                            (ToolBarLocation)Enum.Parse(typeof(ToolBarLocation), settingValue),
                            null);
                        break;
                    case "DefaultLinkType":
                        info.SetValue(
                            currentSettings.Config,
                            (LinkType)Enum.Parse(typeof(LinkType), settingValue),
                            null);
                        break;
                    case "EnterMode":
                    case "ShiftEnterMode":
                        info.SetValue(
                            currentSettings.Config,
                            (EnterModus)Enum.Parse(typeof(EnterModus), settingValue),
                            null);
                        break;
                    case "ContentsLangDirection":
                        info.SetValue(
                            currentSettings.Config,
                            (LanguageDirection)Enum.Parse(typeof(LanguageDirection), settingValue),
                            null);
                        break;
                    case "CodeMirror":
                        foreach (var codeMirrorInfo in
                            typeof(CodeMirror).GetProperties()
                                .Where(codeMirrorInfo => !codeMirrorInfo.Name.Equals("Theme")))
                        {
                            switch (codeMirrorInfo.PropertyType.Name)
                            {
                                case "String":
                                    if (
                                        editorHostSettings.Any(
                                            s => s.Name.Equals(string.Format("{0}{1}", key, codeMirrorInfo.Name))))
                                    {
                                        codeMirrorInfo.SetValue(currentSettings.Config.CodeMirror, settingValue, null);
                                    }

                                    break;
                                case "Boolean":
                                    if (
                                        editorHostSettings.Any(
                                            s => s.Name.Equals(string.Format("{0}{1}", key, codeMirrorInfo.Name))))
                                    {
                                        codeMirrorInfo.SetValue(
                                            currentSettings.Config.CodeMirror,
                                            bool.Parse(settingValue),
                                            null);
                                    }

                                    break;
                            }
                        }

                        break;
                    case "WordCount":
                        foreach (var wordCountInfo in typeof(WordCountConfig).GetProperties())
                        {
                            switch (wordCountInfo.PropertyType.Name)
                            {
                                case "String":
                                    if (
                                        editorHostSettings.Any(
                                            s => s.Name.Equals(string.Format("{0}{1}", key, wordCountInfo.Name))))
                                    {
                                        wordCountInfo.SetValue(currentSettings.Config.WordCount, settingValue, null);
                                    }

                                    break;
                                case "Boolean":
                                    if (
                                        editorHostSettings.Any(
                                            s => s.Name.Equals(string.Format("{0}{1}", key, wordCountInfo.Name))))
                                    {
                                        wordCountInfo.SetValue(
                                            currentSettings.Config.WordCount,
                                            bool.Parse(settingValue),
                                            null);
                                    }

                                    break;
                            }
                        }

                        break;
                }
            }

            /////////////////

            if (
                editorHostSettings.Any(
                    setting => setting.Name.Equals(string.Format("{0}{1}", key, SettingConstants.SKIN))))
            {
                var settingValue =
                    editorHostSettings.FirstOrDefault(
                        s => s.Name.Equals(string.Format("{0}{1}", key, SettingConstants.SKIN))).Value;

                if (!string.IsNullOrEmpty(settingValue))
                {
                    currentSettings.Config.Skin = settingValue;
                }
            }

            if (
                editorHostSettings.Any(
                    setting => setting.Name.Equals(string.Format("{0}{1}", key, SettingConstants.CODEMIRRORTHEME))))
            {
                var settingValue =
                    editorHostSettings.FirstOrDefault(
                        s => s.Name.Equals(string.Format("{0}{1}", key, SettingConstants.CODEMIRRORTHEME))).Value;

                if (!string.IsNullOrEmpty(settingValue))
                {
                    currentSettings.Config.CodeMirror.Theme = settingValue;
                }
            }

            List<ToolbarRoles> listToolbarRoles = (from RoleInfo objRole in portalRoles
                                                   where
                                                       editorHostSettings.Any(
                                                           setting =>
                                                           setting.Name.Equals(
                                                               string.Format(
                                                                   "{0}{2}#{1}",
                                                                   key,
                                                                   objRole.RoleID,
                                                                   SettingConstants.TOOLB)))
                                                   where
                                                       !string.IsNullOrEmpty(
                                                           editorHostSettings.FirstOrDefault(
                                                               s =>
                                                               s.Name.Equals(
                                                                   string.Format(
                                                                       "{0}{2}#{1}",
                                                                       key,
                                                                       objRole.RoleID,
                                                                       SettingConstants.TOOLB))).Value)
                                                   let sToolbar =
                                                       editorHostSettings.FirstOrDefault(
                                                           s =>
                                                           s.Name.Equals(
                                                               string.Format(
                                                                   "{0}{2}#{1}",
                                                                   key,
                                                                   objRole.RoleID,
                                                                   SettingConstants.TOOLB))).Value
                                                   select
                                                       new ToolbarRoles { RoleId = objRole.RoleID, Toolbar = sToolbar })
                .ToList();

            if (
                editorHostSettings.Any(
                    setting => setting.Name.Equals(string.Format("{0}{2}#{1}", key, "-1", SettingConstants.TOOLB))))
            {
                var settingValue =
                    editorHostSettings.FirstOrDefault(
                        s => s.Name.Equals(string.Format("{0}{2}#{1}", key, "-1", SettingConstants.TOOLB))).Value;

                if (!string.IsNullOrEmpty(settingValue))
                {
                    listToolbarRoles.Add(new ToolbarRoles { RoleId = -1, Toolbar = settingValue });
                }
            }

            currentSettings.ToolBarRoles = listToolbarRoles;

            if (
                editorHostSettings.Any(
                    setting => setting.Name.Equals(string.Format("{0}{1}", key, SettingConstants.ROLES))))
            {
                var settingValue =
                    editorHostSettings.FirstOrDefault(
                        s => s.Name.Equals(string.Format("{0}{1}", key, SettingConstants.ROLES))).Value;

                if (!string.IsNullOrEmpty(settingValue))
                {
                    string sRoles = settingValue;

                    currentSettings.BrowserRoles = sRoles;

                    string[] rolesA = sRoles.Split(';');

                    foreach (string sRoleName in rolesA)
                    {
                        if (Utility.IsNumeric(sRoleName))
                        {
                            RoleInfo roleInfo = roleController.GetRole(int.Parse(sRoleName), portalSettings.PortalId);

                            if (roleInfo != null)
                            {
                                roles.Add(roleInfo.RoleName);
                            }
                        }
                        else
                        {
                            roles.Add(sRoleName);
                        }
                    }
                }
            }

            if (
                editorHostSettings.Any(
                    setting => setting.Name.Equals(string.Format("{0}{1}", key, SettingConstants.BROWSER))))
            {
                var settingValue =
                    editorHostSettings.FirstOrDefault(
                        s => s.Name.Equals(string.Format("{0}{1}", key, SettingConstants.BROWSER))).Value;

                if (!string.IsNullOrEmpty(settingValue))
                {
                    currentSettings.Browser = settingValue;

                    switch (currentSettings.Browser)
                    {
                        case "ckfinder":
                            foreach (string sRoleName in roles)
                            {
                                if (PortalSecurity.IsInRoles(sRoleName))
                                {
                                    currentSettings.BrowserMode = Browser.CKFinder;

                                    break;
                                }

                                currentSettings.BrowserMode = Browser.None;
                            }

                            break;
                        case "standard":
                            foreach (string sRoleName in roles)
                            {
                                if (PortalSecurity.IsInRoles(sRoleName))
                                {
                                    currentSettings.BrowserMode = Browser.StandardBrowser;

                                    break;
                                }

                                currentSettings.BrowserMode = Browser.None;
                            }

                            break;
                        case "none":
                            currentSettings.BrowserMode = Browser.None;
                            break;
                    }
                }
            }

            if (
                editorHostSettings.Any(
                    setting => setting.Name.Equals(string.Format("{0}{1}", key, SettingConstants.INJECTJS))))
            {
                var settingValue =
                    editorHostSettings.FirstOrDefault(
                        s => s.Name.Equals(string.Format("{0}{1}", key, SettingConstants.INJECTJS))).Value;

                if (!string.IsNullOrEmpty(settingValue))
                {
                    bool bResult;
                    if (bool.TryParse(settingValue, out bResult))
                    {
                        currentSettings.InjectSyntaxJs = bResult;
                    }
                }
            }

            if (
                editorHostSettings.Any(
                    setting => setting.Name.Equals(string.Format("{0}{1}", key, SettingConstants.WIDTH))))
            {
                var settingValue =
                    editorHostSettings.FirstOrDefault(
                        s => s.Name.Equals(string.Format("{0}{1}", key, SettingConstants.WIDTH))).Value;

                if (!string.IsNullOrEmpty(settingValue))
                {
                    currentSettings.Config.Width = settingValue;
                }
            }

            if (
                editorHostSettings.Any(
                    setting => setting.Name.Equals(string.Format("{0}{1}", key, SettingConstants.HEIGHT))))
            {
                var settingValue =
                    editorHostSettings.FirstOrDefault(
                        s => s.Name.Equals(string.Format("{0}{1}", key, SettingConstants.HEIGHT))).Value;

                if (!string.IsNullOrEmpty(settingValue))
                {
                    currentSettings.Config.Height = settingValue;
                }
            }

            if (
                editorHostSettings.Any(
                    setting => setting.Name.Equals(string.Format("{0}{1}", key, SettingConstants.BLANKTEXT))))
            {
                var settingValue =
                    editorHostSettings.FirstOrDefault(
                        s => s.Name.Equals(string.Format("{0}{1}", key, SettingConstants.BLANKTEXT))).Value;

                if (!string.IsNullOrEmpty(settingValue))
                {
                    currentSettings.BlankText = settingValue;
                }
            }

            if (
                editorHostSettings.Any(
                    setting => setting.Name.Equals(string.Format("{0}{1}", key, SettingConstants.STYLES))))
            {
                var settingValue =
                    editorHostSettings.FirstOrDefault(
                        s => s.Name.Equals(string.Format("{0}{1}", key, SettingConstants.STYLES))).Value;

                if (!string.IsNullOrEmpty(settingValue))
                {
                    currentSettings.Config.StylesSet = settingValue;
                }
            }

            if (
                editorHostSettings.Any(
                    setting => setting.Name.Equals(string.Format("{0}{1}", key, SettingConstants.CSS))))
            {
                var settingValue =
                    editorHostSettings.FirstOrDefault(
                        s => s.Name.Equals(string.Format("{0}{1}", key, SettingConstants.CSS))).Value;

                if (!string.IsNullOrEmpty(settingValue))
                {
                    currentSettings.Config.ContentsCss = settingValue;
                }
            }

            if (
                editorHostSettings.Any(
                    setting => setting.Name.Equals(string.Format("{0}{1}", key, SettingConstants.TEMPLATEFILES))))
            {
                var settingValue =
                    editorHostSettings.FirstOrDefault(
                        s => s.Name.Equals(string.Format("{0}{1}", key, SettingConstants.TEMPLATEFILES))).Value;

                if (!string.IsNullOrEmpty(settingValue))
                {
                    currentSettings.Config.Templates_Files = settingValue;
                }
            }

            if (
                editorHostSettings.Any(
                    setting => setting.Name.Equals(string.Format("{0}{1}", key, SettingConstants.CUSTOMJSFILE))))
            {
                var settingValue =
                    editorHostSettings.FirstOrDefault(
                        s => s.Name.Equals(string.Format("{0}{1}", key, SettingConstants.CUSTOMJSFILE))).Value;

                if (!string.IsNullOrEmpty(settingValue))
                {
                    currentSettings.CustomJsFile = settingValue;
                }
            }

            if (
                editorHostSettings.Any(
                    setting => setting.Name.Equals(string.Format("{0}{1}", key, SettingConstants.CONFIG))))
            {
                var settingValue =
                    editorHostSettings.FirstOrDefault(
                        s => s.Name.Equals(string.Format("{0}{1}", key, SettingConstants.CONFIG))).Value;

                if (!string.IsNullOrEmpty(settingValue))
                {
                    currentSettings.Config.CustomConfig = settingValue;
                }
            }

            if (
                editorHostSettings.Any(
                    setting => setting.Name.Equals(string.Format("{0}{1}", key, SettingConstants.FILELISTPAGESIZE))))
            {
                var settingValue =
                    editorHostSettings.FirstOrDefault(
                        s => s.Name.Equals(string.Format("{0}{1}", key, SettingConstants.FILELISTPAGESIZE))).Value;

                if (!string.IsNullOrEmpty(settingValue))
                {
                    currentSettings.FileListPageSize = int.Parse(settingValue);
                }
            }

            if (
                editorHostSettings.Any(
                    setting => setting.Name.Equals(string.Format("{0}{1}", key, SettingConstants.FILELISTVIEWMODE))))
            {
                var settingValue =
                    editorHostSettings.FirstOrDefault(
                        s => s.Name.Equals(string.Format("{0}{1}", key, SettingConstants.FILELISTVIEWMODE))).Value;

                if (!string.IsNullOrEmpty(settingValue))
                {
                    currentSettings.FileListViewMode = (FileListView)Enum.Parse(typeof(FileListView), settingValue);
                }
            }

            if (
                editorHostSettings.Any(
                    setting => setting.Name.Equals(string.Format("{0}{1}", key, SettingConstants.DEFAULTLINKMODE))))
            {
                var settingValue =
                    editorHostSettings.FirstOrDefault(
                        s => s.Name.Equals(string.Format("{0}{1}", key, SettingConstants.DEFAULTLINKMODE))).Value;

                if (!string.IsNullOrEmpty(settingValue))
                {
                    currentSettings.DefaultLinkMode = (LinkMode)Enum.Parse(typeof(LinkMode), settingValue);
                }
            }

            if (
                editorHostSettings.Any(
                    setting => setting.Name.Equals(string.Format("{0}{1}", key, SettingConstants.USEANCHORSELECTOR))))
            {
                var settingValue =
                    editorHostSettings.FirstOrDefault(
                        s => s.Name.Equals(string.Format("{0}{1}", key, SettingConstants.USEANCHORSELECTOR))).Value;

                if (!string.IsNullOrEmpty(settingValue))
                {
                    bool bResult;
                    if (bool.TryParse(settingValue, out bResult))
                    {
                        currentSettings.UseAnchorSelector = bResult;
                    }
                }
            }

            if (
                editorHostSettings.Any(
                    setting => setting.Name.Equals(string.Format("{0}{1}", key, SettingConstants.SHOWPAGELINKSTABFIRST))))
            {
                var settingValue =
                    editorHostSettings.FirstOrDefault(
                        s => s.Name.Equals(string.Format("{0}{1}", key, SettingConstants.SHOWPAGELINKSTABFIRST))).Value;

                if (!string.IsNullOrEmpty(settingValue))
                {
                    bool bResult;
                    if (bool.TryParse(settingValue, out bResult))
                    {
                        currentSettings.ShowPageLinksTabFirst = bResult;
                    }
                }
            }

            if (
                editorHostSettings.Any(
                    setting => setting.Name.Equals(string.Format("{0}{1}", key, SettingConstants.OVERRIDEFILEONUPLOAD))))
            {
                var settingValue =
                    editorHostSettings.FirstOrDefault(
                        s => s.Name.Equals(string.Format("{0}{1}", key, SettingConstants.OVERRIDEFILEONUPLOAD))).Value;

                if (!string.IsNullOrEmpty(settingValue))
                {
                    bool bResult;
                    if (bool.TryParse(settingValue, out bResult))
                    {
                        currentSettings.OverrideFileOnUpload = bResult;
                    }
                }
            }

            if (
                editorHostSettings.Any(
                    setting => setting.Name.Equals(string.Format("{0}{1}", key, SettingConstants.SUBDIRS))))
            {
                var settingValue =
                    editorHostSettings.FirstOrDefault(
                        s => s.Name.Equals(string.Format("{0}{1}", key, SettingConstants.SUBDIRS))).Value;

                if (!string.IsNullOrEmpty(settingValue))
                {
                    bool bResult;
                    if (bool.TryParse(settingValue, out bResult))
                    {
                        currentSettings.SubDirs = bResult;
                    }
                }
            }

            if (
                editorHostSettings.Any(
                    setting => setting.Name.Equals(string.Format("{0}{1}", key, SettingConstants.BROWSERROOTDIRID))))
            {
                var settingValue =
                    editorHostSettings.FirstOrDefault(
                        s => s.Name.Equals(string.Format("{0}{1}", key, SettingConstants.BROWSERROOTDIRID))).Value;

                if (!string.IsNullOrEmpty(settingValue))
                {
                    try
                    {
                        currentSettings.BrowserRootDirId = int.Parse(settingValue);
                    }
                    catch (Exception)
                    {
                        currentSettings.BrowserRootDirId = -1;
                    }
                }
            }

            if (
                editorHostSettings.Any(
                    setting => setting.Name.Equals(string.Format("{0}{1}", key, SettingConstants.UPLOADDIRID))))
            {
                var settingValue =
                    editorHostSettings.FirstOrDefault(
                        s => s.Name.Equals(string.Format("{0}{1}", key, SettingConstants.UPLOADDIRID))).Value;

                if (!string.IsNullOrEmpty(settingValue))
                {
                    try
                    {
                        currentSettings.UploadDirId = int.Parse(settingValue);
                    }
                    catch (Exception)
                    {
                        currentSettings.UploadDirId = -1;
                    }
                }
            }

            if (
                editorHostSettings.Any(
                    setting => setting.Name.Equals(string.Format("{0}{1}", key, SettingConstants.RESIZEWIDTH))))
            {
                var settingValue =
                    editorHostSettings.FirstOrDefault(
                        s => s.Name.Equals(string.Format("{0}{1}", key, SettingConstants.RESIZEWIDTH))).Value;

                if (!string.IsNullOrEmpty(settingValue))
                {
                    try
                    {
                        currentSettings.ResizeWidth = int.Parse(settingValue);
                    }
                    catch (Exception)
                    {
                        currentSettings.ResizeWidth = -1;
                    }
                }
            }

            if (
                editorHostSettings.Any(
                    setting => setting.Name.Equals(string.Format("{0}{1}", key, SettingConstants.RESIZEHEIGHT))))
            {
                var settingValue =
                    editorHostSettings.FirstOrDefault(
                        s => s.Name.Equals(string.Format("{0}{1}", key, SettingConstants.RESIZEHEIGHT))).Value;

                if (!string.IsNullOrEmpty(settingValue))
                {
                    try
                    {
                        currentSettings.ResizeHeight = int.Parse(settingValue);
                    }
                    catch (Exception)
                    {
                        currentSettings.ResizeHeight = -1;
                    }
                }
            }

            return currentSettings;
        }
        /// <summary>
        /// Imports the old SettingsBase Xml File
        /// </summary>
        /// <param name="homeDirPath">The home folder path.</param>
        /// <param name="isDefaultXmlFile">if set to <c>true</c> [is default XML file].</param>
        internal static void ImportSettingBaseXml(string homeDirPath, bool isDefaultXmlFile = false)
        {
            var oldXmlPath = Path.Combine(homeDirPath, SettingConstants.XmlDefaultFileName);

            var oldSerializer = new XmlSerializer(typeof(SettingBase));
            var reader = new XmlTextReader(new FileStream(oldXmlPath, FileMode.Open, FileAccess.Read, FileShare.Read));

            if (!oldSerializer.CanDeserialize(reader))
            {
                reader.Close();

                return;
            }

            var oldDefaultSettings = (SettingBase)oldSerializer.Deserialize(reader);

            reader.Close();

            // Fix for old skins
            if (oldDefaultSettings.sSkin.Equals("office2003")
                            || oldDefaultSettings.sSkin.Equals("BootstrapCK-Skin")
                            || oldDefaultSettings.sSkin.Equals("chris")
                            || oldDefaultSettings.sSkin.Equals("v2"))
            {
                oldDefaultSettings.sSkin = "moono";
            }

            // Migrate Settings
            var importedSettings = new EditorProviderSettings
                {
                    FileListPageSize = oldDefaultSettings.FileListPageSize,
                    FileListViewMode = oldDefaultSettings.FileListViewMode,
                    UseAnchorSelector = oldDefaultSettings.UseAnchorSelector,
                    ShowPageLinksTabFirst = oldDefaultSettings.ShowPageLinksTabFirst,
                    SubDirs = oldDefaultSettings.bSubDirs,
                    InjectSyntaxJs = oldDefaultSettings.injectSyntaxJs,
                    BrowserRootDirId = oldDefaultSettings.BrowserRootDirId,
                    UploadDirId = oldDefaultSettings.UploadDirId,
                    ResizeHeight = oldDefaultSettings.iResizeHeight,
                    ResizeWidth = oldDefaultSettings.iResizeWidth,
                    ToolBarRoles = oldDefaultSettings.listToolbRoles,
                    BlankText = oldDefaultSettings.sBlankText,
                    BrowserRoles = oldDefaultSettings.sBrowserRoles,
                    Browser = oldDefaultSettings.sBrowser,
                    Config =
                        {
                            CustomConfig = oldDefaultSettings.sConfig,
                            ContentsCss = oldDefaultSettings.sCss,
                            Skin = oldDefaultSettings.sSkin,
                            StylesSet = oldDefaultSettings.sStyles,
                            Templates_Files = oldDefaultSettings.sTemplates,
                            Height = oldDefaultSettings.uHeight.ToString(),
                            Width = oldDefaultSettings.uWidth.ToString(),
                            FileBrowserWindowHeight = oldDefaultSettings.BrowserHeight,
                            FileBrowserWindowWidth = oldDefaultSettings.BrowserWidth,
                            AutoParagraph = true,
                            AutoUpdateElement = true,
                            BasicEntities = true,
                            BrowserContextMenuOnCtrl = true,
                            ColorButton_EnableMore = true,
                            DisableNativeSpellChecker = true,
                            DisableNativeTableHandles = true,
                            EnableTabKeyTools = true,
                            Entities = true,
                            Entities_Greek = true,
                            Entities_Latin = true,
                            FillEmptyBlocks = true,
                            IgnoreEmptyParagraph = true,
                            Image_RemoveLinkByEmptyURL = true,
                            PasteFromWordRemoveFontStyles = true,
                            PasteFromWordRemoveStyles = true,
                            Resize_Enabled = true,
                            StartupShowBorders = true,
                            ToolbarGroupCycling = true,
                            ToolbarStartupExpanded = true,
                            UseComputedState = true,
                            AutoGrow_BottomSpace = 0,
                            AutoGrow_MaxHeight = 0,
                            AutoGrow_MinHeight = 200,
                            BaseFloatZIndex = 10000,
                            Dialog_MagnetDistance = 20,
                            IndentOffset = 40,
                            Menu_SubMenuDelay = 400,
                            Resize_MaxHeight = 600,
                            Resize_MaxWidth = 3000,
                            Resize_MinHeight = 250,
                            Resize_MinWidth = 750,
                            Scayt_MaxSuggestions = 5,
                            Smiley_columns = 8,
                            SourceAreaTabSize = 20,
                            TabIndex = 0,
                            TabSpaces = 0,
                            UndoStackSize = 20
                        }
                };

            // Delete Old File
            File.Delete(oldXmlPath);

            // Save new xml file
            var newSerializer = new XmlSerializer(typeof(EditorProviderSettings));

            using (
                var textWriter =
                    new StreamWriter(
                        new FileStream(
                            Path.Combine(
                               homeDirPath, isDefaultXmlFile ? SettingConstants.XmlDefaultFileName : SettingConstants.XmlSettingsFileName),
                            FileMode.OpenOrCreate,
                            FileAccess.ReadWrite,
                            FileShare.ReadWrite)))
            {
                newSerializer.Serialize(textWriter, importedSettings);

                textWriter.Close();
            }
        }