From 0bc3eadcae4c7b91dc32d9dbbeac2f436c2ef537 Mon Sep 17 00:00:00 2001 From: chrox Date: Wed, 27 Aug 2014 11:07:25 +0800 Subject: [PATCH] refactoring: use Document API getCoverPageImage to get cover image --- .../apps/filemanager/filemanagersearch.lua | 64 ++++--- frontend/document/djvudocument.lua | 4 + frontend/document/document.lua | 4 + frontend/document/koptinterface.lua | 10 ++ frontend/document/pdfdocument.lua | 4 + frontend/ui/screensaver.lua | 168 ++++-------------- frontend/ui/widget/imagewidget.lua | 24 ++- frontend/ui/widget/infomessage.lua | 22 ++- 8 files changed, 130 insertions(+), 170 deletions(-) diff --git a/frontend/apps/filemanager/filemanagersearch.lua b/frontend/apps/filemanager/filemanagersearch.lua index a5fabea6c..c7d99918c 100644 --- a/frontend/apps/filemanager/filemanagersearch.lua +++ b/frontend/apps/filemanager/filemanagersearch.lua @@ -1,14 +1,15 @@ local CenterContainer = require("ui/widget/container/centercontainer") local InputContainer = require("ui/widget/container/inputcontainer") +local DocumentRegistry = require("document/documentregistry") local InputDialog = require("ui/widget/inputdialog") local InfoMessage = require("ui/widget/infomessage") -local Screensaver = require("ui/screensaver") local lfs = require("libs/libkoreader-lfs") local UIManager = require("ui/uimanager") local Menu = require("ui/widget/menu") local Screen = require("ui/screen") local util = require("ffi/util") local Font = require("ui/font") +local DEBUG = require("dbg") local _ = require("gettext") local calibre = "metadata.calibre" @@ -40,19 +41,23 @@ local Search = InputContainer:new{ local function findcalibre(root) local t = nil - for entity in lfs.dir(root) do - if t then - break - else - if entity ~= "." and entity ~= ".." then - local fullPath=root .. "/" .. entity - local mode = lfs.attributes(fullPath,"mode") - if mode == "file" then - if entity == calibre or entity == "." .. calibre then - t = root .. "/" .. entity + -- protect lfs.dir which will raise error on no-permission directory + local ok, iter, dir_obj = pcall(lfs.dir, root) + if ok then + for entity in iter, dir_obj do + if t then + break + else + if entity ~= "." and entity ~= ".." then + local fullPath=root .. "/" .. entity + local mode = lfs.attributes(fullPath,"mode") + if mode == "file" then + if entity == calibre or entity == "." .. calibre then + t = root .. "/" .. entity + end + elseif mode == "directory" then + t = findcalibre(fullPath) end - elseif mode == "directory" then - t = findcalibre(fullPath) end end end @@ -61,15 +66,16 @@ local function findcalibre(root) end function Search:getCalibre() --- check if we find the calibre file --- check 1st file + -- check if we find the calibre file + -- check 1st file if SEARCH_LIBRARY_PATH == nil then - self.metafile_1 = findcalibre("/mnt") - if not self.metafile_1 then - self.error = "SEARCH_LIBRARY_PATH in DEFAULTS.LUA is not set!" - else - settings_changed = true - end + DEBUG("search Calibre database") + self.metafile_1 = findcalibre("/mnt") + if not self.metafile_1 then + self.error = "SEARCH_LIBRARY_PATH in DEFAULTS.LUA is not set!" + else + settings_changed = true + end else if string.sub(SEARCH_LIBRARY_PATH,string.len(SEARCH_LIBRARY_PATH)) ~= "/" then SEARCH_LIBRARY_PATH = SEARCH_LIBRARY_PATH .. "/" @@ -77,6 +83,7 @@ function Search:getCalibre() if io.open(SEARCH_LIBRARY_PATH .. calibre,"r") == nil then if io.open(SEARCH_LIBRARY_PATH .. "." .. calibre,"r") == nil then self.error = SEARCH_LIBRARY_PATH .. calibre .. " not found!" + DEBUG(self.error) else self.metafile_1 = SEARCH_LIBRARY_PATH .. "." .. calibre end @@ -94,7 +101,7 @@ function Search:getCalibre() end end end --- check 2nd file + -- check 2nd file local dummy if string.sub(SEARCH_LIBRARY_PATH2,string.len(SEARCH_LIBRARY_PATH2)) ~= "/" then @@ -471,8 +478,19 @@ function Search:onMenuHold(item) end item.notchecked = false end + local thumbnail = nil + local doc = DocumentRegistry:openDocument(item.path) + if doc then + thumbnail = doc:getCoverPageImage() + doc:close() + end local thumbwidth = math.min(240, Screen:getWidth()/3) - UIManager:show(InfoMessage:new{text = item.info,image = Screensaver:getCoverPicture(item.path), image_width = thumbwidth,image_height = thumbwidth/2*3}) + UIManager:show(InfoMessage:new{ + text = item.info, + image = thumbnail, + image_width = thumbwidth, + image_height = thumbwidth/2*3 + }) end end diff --git a/frontend/document/djvudocument.lua b/frontend/document/djvudocument.lua index f4aec66cb..dae6dd57a 100644 --- a/frontend/document/djvudocument.lua +++ b/frontend/document/djvudocument.lua @@ -102,6 +102,10 @@ function DjvuDocument:getPageDimensions(pageno, zoom, rotation) return self.koptinterface:getPageDimensions(self, pageno, zoom, rotation) end +function DjvuDocument:getCoverPageImage() + return self.koptinterface:getCoverPageImage(self) +end + function DjvuDocument:renderPage(pageno, rect, zoom, rotation, gamma, render_mode) return self.koptinterface:renderPage(self, pageno, rect, zoom, rotation, gamma, render_mode) end diff --git a/frontend/document/document.lua b/frontend/document/document.lua index 6fa8b86fe..f20dacf73 100644 --- a/frontend/document/document.lua +++ b/frontend/document/document.lua @@ -212,6 +212,10 @@ function Document:getOCRWord(pageno, rect) return nil end +function Document:getCoverPageImage() + return nil +end + function Document:renderPage(pageno, rect, zoom, rotation, gamma, render_mode) local hash = "renderpg|"..self.file.."|"..pageno.."|"..zoom.."|"..rotation.."|"..gamma.."|"..render_mode local page_size = self:getPageDimensions(pageno, zoom, rotation) diff --git a/frontend/document/koptinterface.lua b/frontend/document/koptinterface.lua index 7c4eeb12d..2193f5e29 100644 --- a/frontend/document/koptinterface.lua +++ b/frontend/document/koptinterface.lua @@ -248,6 +248,16 @@ function KoptInterface:getRFPageDimensions(doc, pageno, zoom, rotation) return Geom:new{ w = fullwidth, h = fullheight } end +--[[ +get first page image +--]] +function KoptInterface:getCoverPageImage(doc) + local tile = self:renderPage(doc, 1, nil, 1, 0, 1, 0) + if tile then + return tile.bb + end +end + function KoptInterface:renderPage(doc, pageno, rect, zoom, rotation, gamma, render_mode) if doc.configurable.text_wrap == 1 then return self:renderReflowedPage(doc, pageno, rect, zoom, rotation, render_mode) diff --git a/frontend/document/pdfdocument.lua b/frontend/document/pdfdocument.lua index ea88d92d7..3b8db572e 100644 --- a/frontend/document/pdfdocument.lua +++ b/frontend/document/pdfdocument.lua @@ -210,6 +210,10 @@ function PdfDocument:getPageDimensions(pageno, zoom, rotation) return self.koptinterface:getPageDimensions(self, pageno, zoom, rotation) end +function PdfDocument:getCoverPageImage() + return self.koptinterface:getCoverPageImage(self) +end + function PdfDocument:renderPage(pageno, rect, zoom, rotation, gamma, render_mode) return self.koptinterface:renderPage(self, pageno, rect, zoom, rotation, gamma, render_mode) end diff --git a/frontend/ui/screensaver.lua b/frontend/ui/screensaver.lua index c7a3889a7..bc8be484a 100644 --- a/frontend/ui/screensaver.lua +++ b/frontend/ui/screensaver.lua @@ -1,3 +1,4 @@ +local DocumentRegistry = require("document/documentregistry") local UIManager = require("ui/uimanager") local Screen = require("ui/screen") local DEBUG = require("dbg") @@ -6,132 +7,24 @@ local _ = require("gettext") local Screensaver = { } -function Screensaver:getCoverPicture(file) - 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 "") - if not (itype == "png" or itype == "jpg" or itype == "jpeg") then - 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 - - local function checkoldfile(cover) - if io.open(cover) then - return cover - end - end - - local cover - - 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 - - if not cover then - - 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*