private async void UpdateTheme(ThemeDefinition Def, string TabID)
        {
            try
            {
                this.themeId = Def.Id;

                if (this.Step <= 0)
                {
                    this.Step = 1;
                }

                this.Updated = DateTime.Now;
                await Database.Update(this);

                Gateway.HttpServer.ETagSalt = this.Updated.Ticks.ToString();

                ClientEvents.PushEvent(new string[] { TabID }, "ThemeOk", JSON.Encode(new KeyValuePair <string, object>[]
                {
                    new KeyValuePair <string, object>("themeId", Def.Id),
                    new KeyValuePair <string, object>("cssUrl", Def.CSSX),
                }, false), true, "User");
            }
            catch (Exception ex)
            {
                Log.Critical(ex);
            }
        }
        /// <summary>
        /// Gets available theme definitions.
        /// </summary>
        /// <returns>Array of theme definitions.</returns>
        public static ThemeDefinition[] GetDefinitions()
        {
            ThemeDefinition[] Result = new ThemeDefinition[themeDefinitions.Count];
            themeDefinitions.Values.CopyTo(Result, 0);

            Array.Sort <ThemeDefinition>(Result, (t1, t2) => t1.Title.CompareTo(t2.Title));

            return(Result);
        }
        /// <summary>
        /// Initializes the setup object.
        /// </summary>
        /// <param name="WebServer">Current Web Server object.</param>
        public override async Task InitSetup(HttpServer WebServer)
        {
            XmlSchema       Schema       = XSL.LoadSchema(typeof(Gateway).Namespace + ".Schema.Theme.xsd", typeof(Gateway).Assembly);
            string          ThemesFolder = Path.Combine(Gateway.AppDataFolder, "Root", "Themes");
            ThemeDefinition Def;

            await base.InitSetup(WebServer);

            WebServer.ETagSalt = this.Updated.Ticks.ToString();

            if (Directory.Exists(ThemesFolder))
            {
                foreach (string FileName in Directory.GetFiles(ThemesFolder, "*.xml", SearchOption.AllDirectories))
                {
                    try
                    {
                        XmlDocument Doc = new XmlDocument();
                        Doc.Load(FileName);

                        XSL.Validate(FileName, Doc, "Theme", "http://waher.se/Schema/Theme.xsd", Schema);

                        Def = new ThemeDefinition(Doc);
                        themeDefinitions[Def.Id] = Def;
                    }
                    catch (Exception ex)
                    {
                        Log.Critical(ex, FileName);
                        continue;
                    }
                }
            }

            bool Update = false;

            if (!string.IsNullOrEmpty(this.themeId) && !themeDefinitions.ContainsKey(this.themeId))
            {
                this.themeId   = string.Empty;
                this.Step      = 0;
                this.Completed = DateTime.MinValue;
                this.Complete  = false;

                Update = true;
            }

            if (string.IsNullOrEmpty(this.themeId) && themeDefinitions.Count == 1)
            {
                foreach (ThemeDefinition Def2 in themeDefinitions.Values)
                {
                    this.themeId = Def2.Id;

                    await this.MakeCompleted();

                    Update = false;

                    break;
                }
            }

            if (Update)
            {
                this.Updated = DateTime.Now;
                await Database.Update(this);
            }

            if (!string.IsNullOrEmpty(this.themeId) && themeDefinitions.TryGetValue(this.themeId, out Def))
            {
                Theme.CurrerntTheme = Def;
            }
            else if (themeDefinitions.TryGetValue("CactusRose", out Def))
            {
                Theme.CurrerntTheme = Def;
            }
            else
            {
                foreach (ThemeDefinition Def2 in themeDefinitions.Values)
                {
                    Theme.CurrerntTheme = Def2;
                    break;
                }
            }

            this.setTheme = WebServer.Register("/Settings/SetTheme", null, this.SetTheme, true, false, true);
        }