mirror of https://github.com/koreader/koreader
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
192 lines
6.7 KiB
Lua
192 lines
6.7 KiB
Lua
10 years ago
|
local UIManager = require("ui/uimanager")
|
||
|
local Screen = require("ui/screen")
|
||
|
local DEBUG = require("dbg")
|
||
|
local _ = require("gettext")
|
||
10 years ago
|
|
||
10 years ago
|
local Screensaver = {
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
function Screensaver:getCoverPicture(file)
|
||
10 years ago
|
local contentopf
|
||
|
local contentpath
|
||
|
local epub_folder = "temp/epub"
|
||
|
|
||
|
local function check_extension(cover)
|
||
|
if cover then
|
||
|
local itype = string.lower(string.match(cover, ".+%.([^.]+)") or "")
|
||
10 years ago
|
if not (itype == "png" or itype == "jpg" or itype == "jpeg") then
|
||
10 years ago
|
cover = nil
|
||
|
end
|
||
|
end
|
||
|
return cover
|
||
|
end
|
||
|
|
||
|
local function getValue(zipfile,which_line,to_find,excludecheck)
|
||
|
local f = io.open(zipfile,"r")
|
||
|
local i
|
||
|
|
||
|
if f then
|
||
|
local line = f:read()
|
||
|
while line and not i do
|
||
|
i = line:lower():find(which_line:lower()) -- found something
|
||
|
if i then
|
||
|
f.close()
|
||
|
line = line:match(to_find .. "\"([^\"]+)\".*")
|
||
|
if not excludecheck then line = check_extension(line) end
|
||
|
if line then
|
||
|
return line
|
||
|
else
|
||
|
i = nil
|
||
|
end
|
||
|
end
|
||
|
if not i then line = f:read() end
|
||
|
end
|
||
|
f.close()
|
||
|
end
|
||
|
end
|
||
|
|
||
|
local function guess(extension)
|
||
|
local cover = contentpath .. "Images/cover." .. extension
|
||
|
pcall(os.execute("unzip \"" .. file .. "\" \"" .. cover .. "\" -oq -d " .. epub_folder))
|
||
|
cover = epub_folder .. "/" .. cover
|
||
|
if not io.open(cover,"r") then
|
||
|
cover = nil
|
||
|
end
|
||
|
return cover
|
||
|
end
|
||
|
|
||
|
local function try_content_opf(which_line,to_find,addimage)
|
||
|
local cover = getValue(epub_folder .. "/" .. contentopf,which_line,to_find)
|
||
|
local imageadd
|
||
|
if cover then
|
||
|
if addimage then
|
||
|
imageadd = "Images/"
|
||
|
else
|
||
|
imageadd = ""
|
||
|
end
|
||
|
cover = contentpath .. imageadd .. cover
|
||
|
pcall(os.execute("unzip \"" .. file .. "\" \"" .. cover .. "\" -oq -d " .. epub_folder))
|
||
|
cover = epub_folder .. "/" .. cover
|
||
|
if not io.open(cover,"r") then cover = nil end
|
||
|
end
|
||
|
return check_extension(cover)
|
||
|
end
|
||
10 years ago
|
|
||
10 years ago
|
local function checkoldfile(cover)
|
||
|
if io.open(cover) then
|
||
|
return cover
|
||
|
end
|
||
|
end
|
||
10 years ago
|
|
||
10 years ago
|
local cover
|
||
|
|
||
10 years ago
|
local oldfile = "temp/" .. file:gsub("/","#") .. "."
|
||
|
|
||
|
cover = checkoldfile(oldfile .. "jpg")
|
||
|
if not cover then cover = checkoldfile(oldfile .. "jpeg") end
|
||
|
if not cover then cover = checkoldfile(oldfile .. "png") end
|
||
10 years ago
|
|
||
10 years ago
|
if not cover then
|
||
10 years ago
|
|
||
10 years ago
|
if file then
|
||
|
pcall(lfs.mkdir("temp"))
|
||
|
pcall(os.execute("rm -rf " .. epub_folder))
|
||
|
pcall(lfs.mkdir(epub_folder))
|
||
|
pcall(os.execute("unzip \"" .. file .. "\" cover.jpeg -oq -d " .. epub_folder))
|
||
|
if io.open(epub_folder .. "/cover.jpeg","r") then -- picture in main folder
|
||
|
cover = epub_folder .. "/cover.jpeg" -- found one
|
||
|
else
|
||
|
pcall(os.execute("unzip \"" .. file .. "\" \"META-INF/container.xml\" -oq -d " .. epub_folder)) -- read container.xml
|
||
|
contentopf = getValue(epub_folder .. "/META-INF/container.xml","^%s*<rootfile ","full[-]path=",true)
|
||
|
if contentopf then
|
||
|
contentpath = contentopf:match("(.*)[/][^/]+")
|
||
|
if contentpath then
|
||
|
contentpath = contentpath .. "/"
|
||
|
else
|
||
|
contentpath = ""
|
||
|
end
|
||
|
pcall(os.execute("unzip \"" .. file .. "\" \"" .. contentopf .. "\" -oq -d " .. epub_folder)) -- read content.opf
|
||
10 years ago
|
|
||
10 years ago
|
cover = try_content_opf("^%s*<meta name=\"cover\"","content=",true) -- Make Room
|
||
|
if not cover then cover = try_content_opf('id="cover',"item href=",false) end -- Kishon
|
||
|
if not cover then cover = try_content_opf("cover","href=",true) end
|
||
|
if not cover then cover = try_content_opf("cover","=",true) end
|
||
|
if not cover then cover = try_content_opf("cover","=",false) end
|
||
10 years ago
|
|
||
10 years ago
|
if not cover then guess("jpg") end
|
||
|
if not cover then guess("jpeg") end
|
||
|
if not cover then guess("png") end
|
||
|
end
|
||
10 years ago
|
end
|
||
|
end
|
||
10 years ago
|
cover=check_extension(cover)
|
||
|
if cover then
|
||
|
oldfile = oldfile .. string.lower(string.match(cover, ".+%.([^.]+)"))
|
||
|
pcall(os.execute('mv "' .. cover .. '" "' .. oldfile .. '"'))
|
||
|
cover = oldfile
|
||
|
pcall(os.execute('find temp/#mnt#* -mtime +30 -exec rm -v {} \\;'))
|
||
|
end
|
||
|
pcall(os.execute("rm -rf " .. epub_folder))
|
||
10 years ago
|
end
|
||
10 years ago
|
return cover
|
||
10 years ago
|
end
|
||
|
|
||
10 years ago
|
function Screensaver:getRandomPicture(dir)
|
||
10 years ago
|
local pics = {}
|
||
|
local i = 0
|
||
10 years ago
|
math.randomseed(os.time())
|
||
10 years ago
|
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
|
||
|
return pics[math.random(i)]
|
||
|
end
|
||
|
|
||
10 years ago
|
function Screensaver:show()
|
||
|
DEBUG("show screensaver")
|
||
|
local InfoMessage = require("ui/widget/infomessage")
|
||
|
local ImageWidget = require("ui/widget/imagewidget")
|
||
|
local file = nil
|
||
|
-- first check book cover image
|
||
|
if KOBO_SCREEN_SAVER_LAST_BOOK then
|
||
|
file = self:getCoverPicture(G_reader_settings:readSetting("lastfile"))
|
||
|
-- then screensaver image
|
||
|
elseif type(KOBO_SCREEN_SAVER) == "string" then
|
||
|
file = KOBO_SCREEN_SAVER
|
||
|
if lfs.attributes(file, "mode") == "directory" then
|
||
|
if string.sub(file,string.len(file)) ~= "/" then
|
||
|
file = file .. "/"
|
||
|
end
|
||
|
local dummy = self:getRandomPicture(file)
|
||
|
if dummy then file = file .. dummy end
|
||
|
end
|
||
|
end
|
||
|
if file and lfs.attributes(file, "mode") == "file" then
|
||
|
self.suspend_msg = ImageWidget:new{
|
||
|
file = file,
|
||
|
width = Screen:getWidth(),
|
||
|
height = Screen:getHeight(),
|
||
|
}
|
||
|
end
|
||
|
-- fallback to suspended message
|
||
|
if not self.suspend_msg then
|
||
|
self.suspend_msg = InfoMessage:new{ text = _("Suspended") }
|
||
|
end
|
||
|
UIManager:show(self.suspend_msg)
|
||
|
end
|
||
|
|
||
|
function Screensaver:close()
|
||
|
DEBUG("close screensaver")
|
||
|
if self.suspend_msg then
|
||
|
UIManager:close(self.suspend_msg)
|
||
|
self.suspend_msg = nil
|
||
|
end
|
||
|
end
|
||
|
|
||
|
return Screensaver
|