Exemplo n.º 1
0
        public static ThemeResult TryLoad(string themeId)
        {
            string jsonPath = Path.Combine("themes", themeId, "theme.json");

            if (!File.Exists(jsonPath))
            {
                return(new ThemeResult(new NoThemeJSON(themeId)));
            }

            ThemeConfig theme;

            try
            {
                theme = JsonConvert.DeserializeObject <ThemeConfig>(File.ReadAllText(jsonPath));
            }
            catch (JsonException e)
            {
                return(new ThemeResult(new InvalidThemeJSON(themeId, e.Message)));
            }

            if (theme == null)
            {
                return(new ThemeResult(new InvalidThemeJSON(themeId, _("Empty JSON file"))));
            }

            theme.themeId = themeId;
            return(ThemeJsonValidator.ValidateQuick(theme));
        }
Exemplo n.º 2
0
        public static ThemeResult ExtractTheme(string zipPath, string themeId)
        {
            if (!File.Exists(zipPath))
            {
                return(new ThemeResult(new FailedToFindLocation(themeId, zipPath)));
            }

            string themePath = Path.Combine("themes", themeId);

            Directory.CreateDirectory(themePath);

            try
            {
                using (ZipArchive archive = ZipFile.OpenRead(zipPath))
                {
                    try
                    {
                        ZipArchiveEntry themeJson = archive.Entries.Single(
                            entry => Path.GetExtension(entry.Name) == ".json");
                        themeJson.ExtractToFile(Path.Combine(themePath, "theme.json"), true);
                    }
                    catch (InvalidOperationException)
                    {
                        return(RollbackInstall(new NoThemeJSONInZIP(themeId, zipPath)));
                    }

                    return(TryLoad(themeId).Match(RollbackInstall, theme =>
                    {
                        ZipArchiveEntry[] imageEntries = archive.Entries.Where(
                            entry => Path.GetDirectoryName(entry.FullName) == "" &&
                            Path.GetExtension(entry.Name) != ".json").ToArray();

                        if (imageEntries.Length == 0)
                        {
                            return RollbackInstall(new NoImagesInZIP(themeId, zipPath));
                        }

                        foreach (ZipArchiveEntry imageEntry in imageEntries)
                        {
                            imageEntry.ExtractToFile(Path.Combine(themePath, imageEntry.Name), true);
                        }

                        return ThemeJsonValidator.ValidateFull(theme).Match(RollbackInstall,
                                                                            theme2 => new ThemeResult(theme2));
                    }));
                }
            }
            catch (InvalidDataException)
            {
                return(RollbackInstall(new InvalidZIP(themeId, zipPath)));
            }
        }
Exemplo n.º 3
0
        public static ThemeResult CopyLocalTheme(string jsonPath, string themeId)
        {
            if (!File.Exists(jsonPath))
            {
                return(new ThemeResult(new FailedToFindLocation(themeId, jsonPath)));
            }

            string themePath = Path.Combine("themes", themeId);

            Directory.CreateDirectory(themePath);
            File.Copy(jsonPath, Path.Combine(themePath, "theme.json"), true);

            return(TryLoad(themeId).Match(RollbackInstall, theme =>
            {
                string sourcePath = Path.GetDirectoryName(jsonPath);
                string[] imagePaths = Directory.GetFiles(sourcePath, theme.imageFilename);

                if (imagePaths.Length == 0)
                {
                    return RollbackInstall(new NoImagesInFolder(themeId, sourcePath));
                }

                foreach (string imagePath in imagePaths)
                {
                    try
                    {
                        File.Copy(imagePath, Path.Combine(themePath, Path.GetFileName(imagePath)), true);
                    }
                    catch
                    {
                        return RollbackInstall(new FailedToCopyImage(themeId, imagePath));
                    }
                }

                return ThemeJsonValidator.ValidateFull(theme).Match(RollbackInstall, theme2 => new ThemeResult(theme2));
            }));
        }
Exemplo n.º 4
0
 public static bool IsTheme4Segment(ThemeConfig theme)
 {
     return(!ThemeJsonValidator.IsNullOrEmpty(theme.sunriseImageList) &&
            !ThemeJsonValidator.IsNullOrEmpty(theme.sunsetImageList));
 }