Exemple #1
0
        /// <summary>
        /// Json package representing current selected theme
        /// </summary>
        /// <param name="themeId">Theme id</param>
        /// <returns>Json package</returns>
        public static JsonPackage GetCurrentTheme(string themeId)
        {
            // first try to pull theme metadata from manifest file
            var jp = FileSystem.GetThemeManifest(themeId);

            if (jp != null)
            {
                if (string.IsNullOrEmpty(jp.IconUrl))
                {
                    jp.IconUrl = DefaultIconUrl(jp);
                }
                return(jp);
            }


            // if no manifest file, check themes in online gallery
            // and if found create manifest file using package info
            var srs = new PackagingSource {
                FeedUrl = _feedUrl
            };
            var gPkg = GetPublishedPackages(srs).ToList().Where(p => p.Id == themeId).FirstOrDefault();

            if (gPkg != null)
            {
                if (gPkg.Screenshots != null && gPkg.Screenshots.Count > 0)
                {
                    gPkg.IconUrl = string.Format("http://dnbegallery.org{0}", gPkg.Screenshots[0].ScreenshotUri);
                }

                jp = FileSystem.WriteThemeManifest(gPkg.Id, gPkg.Description, gPkg.Authors, gPkg.ProjectUrl, gPkg.Version, gPkg.IconUrl);
                if (jp != null)
                {
                    return(jp);
                }
            }

            // generate default blank manifest if both methods failed
            var jpw = FileSystem.WriteThemeManifest(themeId);

            if (jpw != null)
            {
                jp = jpw;
            }

            // return default values if theme folder read only
            if (jp == null)
            {
                jp = new JsonPackage {
                    Id = themeId, Authors = "Unknown", IconUrl = Utils.ApplicationRelativeWebRoot + "pics/Theme.png"
                }
            }
            ;

            return(jp);
        }
Exemple #2
0
        /// <summary>
        /// Installed packages
        /// </summary>
        /// <param name="pkgType">Package type</param>
        /// <returns>List of installed packages</returns>
        public static List <JsonPackage> InstalledPackages(string pkgType)
        {
            var installedThemes = new Dictionary <string, JsonPackage>();
            var path            = HttpContext.Current.Server.MapPath(string.Format("{0}themes/", Utils.ApplicationRelativeWebRoot));

            // read themes directory
            foreach (var d in Directory.GetDirectories(path))
            {
                var index   = d.LastIndexOf(Path.DirectorySeparatorChar) + 1;
                var themeId = d.Substring(index);

                // first try to pull theme metadata from manifest file
                var p = FileSystem.GetThemeManifest(themeId);
                if (p == null)
                {
                    p    = new JsonPackage();
                    p.Id = themeId;
                }

                if (p.Id != BlogSettings.Instance.Theme &&
                    p.Id != BlogSettings.Instance.MobileTheme &&
                    p.Id != "RazorHost")
                {
                    if (string.IsNullOrEmpty(p.IconUrl))
                    {
                        p.IconUrl = DefaultIconUrl(p);
                    }
                    installedThemes.Add(p.Id, p);
                }
            }

            // add package metadata from online repository
            var srs = new PackagingSource {
                FeedUrl = _feedUrl
            };

            try
            {
                var pkgList = GetPublishedPackages(srs);

                foreach (var pkg in pkgList.ToList())
                {
                    if (pkg.PackageType != pkgType || !pkg.IsLatestVersion)
                    {
                        continue;
                    }

                    if (installedThemes.ContainsKey(pkg.Id))
                    {
                        installedThemes[pkg.Id].Title       = pkg.Title;
                        installedThemes[pkg.Id].Description = pkg.Description;
                        installedThemes[pkg.Id].Tags        = pkg.Tags;
                        installedThemes[pkg.Id].Authors     = pkg.Authors;
                        installedThemes[pkg.Id].Website     = pkg.ProjectUrl;
                        installedThemes[pkg.Id].LastUpdated = pkg.LastUpdated.ToString("dd MMM yyyy");
                        installedThemes[pkg.Id].Version     = pkg.Version;

                        if (pkg.PackageType == "Theme" && pkg.Screenshots != null && pkg.Screenshots.Count > 0)
                        {
                            installedThemes[pkg.Id].IconUrl = string.Format("http://dnbegallery.org{0}", pkg.Screenshots[0].ScreenshotUri);
                        }
                    }
                }

                // return combined result
                return(installedThemes.Select(t => t.Value).ToList());
            }
            catch (Exception)
            {
                // no connection - return local
                return(installedThemes.Select(t => t.Value).ToList());
            }
        }