<commit_before>local DocumentRegistry = require("document/documentregistry")
local UIManager = require("ui/uimanager")
local Screen = require("ui/screen")
local DEBUG = require("dbg")
local _ = require("gettext")

local Screensaver = {
}

function Screensaver:getCoverImage(file)
    local ImageWidget = require("ui/widget/imagewidget")
    local doc = DocumentRegistry:openDocument(file)
    if doc then
        local image = doc:getCoverPageImage()
        doc:close()
        if image then
            return ImageWidget:new{
                image = image,
                width = Screen:getWidth(),
                height = Screen:getHeight(),
            }
        end
    end
end

function Screensaver:getRandomImage(dir)
    local ImageWidget = require("ui/widget/imagewidget")
    local pics = {}
    local i = 0
    math.randomseed(os.time())
    for entry in lfs.dir(dir) do
        if lfs.attributes(dir .. entry, "mode") == "file" then
            local extension = string.lower(string.match(entry, ".+%.([^.]+)") or "")
            if extension == "jpg" or extension == "jpeg" or extension == "png" then
                i = i + 1
                pics[i] = entry
            end
        end
    end
    local image = pics[math.random(i)]
    if image then
        image = dir .. image
        if lfs.attributes(image, "mode") == "file" then
            return ImageWidget:new{
                file = image,
                width = Screen:getWidth(),
                height = Screen:getHeight(),
            }
        end
    end
end

function Screensaver:show()
    DEBUG("show screensaver")
    local InfoMessage = require("ui/widget/infomessage")
    -- first check book cover image
    if KOBO_SCREEN_SAVER_LAST_BOOK then
        local lastfile = G_reader_settings:readSetting("lastfile")
        self.suspend_msg = self:getCoverImage(lastfile)
    end
    -- then screensaver directory or file image
    if not self.suspend_msg then
        if type(KOBO_SCREEN_SAVER) == "string" then
            local file = KOBO_SCREEN_SAVER
            if lfs.attributes(file, "mode") == "directory" then
                if string.sub(file,string.len(file)) ~= "/" then
                   file = file .. "/"
                end
                self.suspend_msg = self:getRandomImage(file)
            elseif lfs.attributes(file, "mode") == "file" then
                local ImageWidget = require("ui/widget/imagewidget")
                self.suspend_msg = ImageWidget:new{
                    file = file,
                    width = Screen:getWidth(),
                    height = Screen:getHeight(),
                }
            end
        end
    end
    -- fallback to suspended message
    if not self.suspend_msg then<commit_msg>Add "exclude cover from screensaver" functionality<commit_after>local DocumentRegistry = require("document/documentregistry")
local UIManager = require("ui/uimanager")
local Screen = require("ui/screen")
local DocSettings = require("docsettings")
local DEBUG = require("dbg")
local _ = require("gettext")

local Screensaver = {
}

function Screensaver:getCoverImage(file)
    local ImageWidget = require("ui/widget/imagewidget")
    local doc = DocumentRegistry:openDocument(file)
    if doc then
        local image = doc:getCoverPageImage()
        doc:close()
        if image then
            return ImageWidget:new{
                image = image,
                width = Screen:getWidth(),
                height = Screen:getHeight(),
            }
        end
    end
end

function Screensaver:getRandomImage(dir)
    local ImageWidget = require("ui/widget/imagewidget")
    local pics = {}
    local i = 0
    math.randomseed(os.time())
    for entry in lfs.dir(dir) do
        if lfs.attributes(dir .. entry, "mode") == "file" then
            local extension = string.lower(string.match(entry, ".+%.([^.]+)") or "")
            if extension == "jpg" or extension == "jpeg" or extension == "png" then
                i = i + 1
                pics[i] = entry
            end
        end
    end
    local image = pics[math.random(i)]
    if image then
        image = dir .. image
        if lfs.attributes(image, "mode") == "file" then
            return ImageWidget:new{
                file = image,
                width = Screen:getWidth(),
                height = Screen:getHeight(),
            }
        end
    end
end

function Screensaver:show()
    DEBUG("show screensaver")
    local InfoMessage = require("ui/widget/infomessage")
    -- first check book cover image
    if KOBO_SCREEN_SAVER_LAST_BOOK then
        local lastfile = G_reader_settings:readSetting("lastfile")
        local data = DocSettings:open(lastfile)
        local exclude = data:readSetting("exclude_screensaver")
        if not exclude then
            self.suspend_msg = self:getCoverImage(lastfile)
        end

    end
    -- then screensaver directory or file image
    if not self.suspend_msg then
        if type(KOBO_SCREEN_SAVER) == "string" then
            local file = KOBO_SCREEN_SAVER
            if lfs.attributes(file, "mode") == "directory" then
                if string.sub(file,string.len(file)) ~= "/" then
                   file = file .. "/"
                end
                self.suspend_msg = self:getRandomImage(file)
            elseif lfs.attributes(file, "mode") == "file" then
                local ImageWidget = require("ui/widget/imagewidget")
                self.suspend_msg = ImageWidget:new{
                    file = file,
                    width = Screen:getWidth(),
                    height = Screen:getHeight(),
                }
            end
        end
    end
    -- fallback to suspended message
    if not self.suspend_msg then
{'fraction_non_alphanumeric': 0.10302606262025538, 'fraction_numerical': 0.0006996676578625153, 'mean_word_length': 1.971933471933472, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1976044', 'n_tokens_mistral': 1582, 'n_tokens_neox': 1554, 'n_words': 402}