/// <summary>
        /// Returns the theme which is specified via URL.
        /// </summary>
        /// <returns>Theme.</returns>
        private GitHubInMemoryTheme GetUrlSpecifiedTheme()
        {
            GitHubInMemoryTheme ret = null;
            string inMemoryThemeUrl = ThemeSource.GetThemeUrlFromRequest(HttpContext.Current.Request);

            if (!string.IsNullOrEmpty(inMemoryThemeUrl))
            {
                ret = GitHubInMemoryThemeCache.Read();

                if (ret == null || string.Compare(inMemoryThemeUrl, ret.Uri, true) != 0)
                {
                    ret = ThemeSource.CreateThemeInstance <GitHubInMemoryTheme>(inMemoryThemeUrl, t =>
                    {
                        t.Uri     = inMemoryThemeUrl;
                        t.Content = Encoding.UTF8.GetBytes(ThemeSource.DownloadString(inMemoryThemeUrl));
                    });

                    if (ret.Content != null && ret.Content.Length > 0)
                    {
                        GitHubInMemoryThemeCache.Update(ret);
                    }
                    else
                    {
                        ret = null;
                    }
                }
            }

            return(ret);
        }
Beispiel #2
0
        /// <summary>
        /// Clones the given theme.
        /// </summary>
        /// <param name="sourceId">An Id of the theme to clone.</param>
        /// <param name="name">The name for a new theme.</param>
        /// <param name="overrides">Theme overrides.</param>
        /// <returns>New theme.</returns>
        public UserTheme CloneTheme(string sourceId, string name, ThemeMetadata overrides)
        {
            UserTheme ret              = null;
            int       userId           = GetUserId();
            string    cssText          = string.Empty;
            string    fullPhysicalPath = string.Empty;
            string    newId            = ThemeSource.MakeId(name);

            ThemeMetadata.ExCSSStylesheet stylesheet = null;
            string fullPhysicalPathDirectoryName     = string.Empty;
            string fullPath = string.Format("~/App_Data/Themes/{0}/{1}.css", userId, name);

            if (userId > 0)
            {
                stylesheet = new ThemeMetadata.ExCSSStylesheet(sourceId, ThemeMetadata.ResolveThemeContents(sourceId));

                if (overrides != null)
                {
                    stylesheet.FontFamily      = overrides.FontFamily;
                    stylesheet.FontColor       = overrides.FontColor;
                    stylesheet.AccentColor1    = overrides.AccentColor1;
                    stylesheet.AccentColor2    = overrides.AccentColor2;
                    stylesheet.AccentColor3    = overrides.AccentColor3;
                    stylesheet.AccentColor4    = overrides.AccentColor4;
                    stylesheet.BackgroundColor = overrides.BackgroundColor;
                    stylesheet.BackgroundImage = overrides.BackgroundImage ?? string.Empty;
                    stylesheet.Logo            = overrides.Logo;
                }

                cssText = stylesheet.ToString();
                cssText = Regex.Replace(cssText, string.Concat("\\.theme-", sourceId), string.Concat(".theme-", newId), RegexOptions.IgnoreCase);

                fullPhysicalPath = HttpContext.Current.Server.MapPath(fullPath);
                fullPhysicalPathDirectoryName = System.IO.Path.GetDirectoryName(fullPhysicalPath);

                if (!System.IO.Directory.Exists(fullPhysicalPathDirectoryName))
                {
                    System.IO.Directory.CreateDirectory(fullPhysicalPathDirectoryName);
                }

                System.IO.File.WriteAllText(fullPhysicalPath, cssText, System.Text.Encoding.UTF8);

                ret = ThemeSource.CreateThemeInstance <UserTheme>(fullPhysicalPath, t => t.PhysicalPath = fullPhysicalPath, true);
            }

            return(ret);
        }