From 04184a59501b75bb07431fc2bf6d2c701b2d11db Mon Sep 17 00:00:00 2001 From: traycold Date: Sun, 4 Mar 2012 23:03:04 +0100 Subject: [PATCH] improved filechooser; enable "single file" mode of kindlepdfviewer with this mode it's possible to start kindlepdfviewer opening last viewed file, bypassing filechooser --- filechooser.lua | 60 +++++++++++++++++++++++++++++++++++++---------- launchpad/kpdf.sh | 2 +- reader.lua | 29 ++++++++++++++++------- 3 files changed, 68 insertions(+), 23 deletions(-) diff --git a/filechooser.lua b/filechooser.lua index 68c14d263..d9b4e7f2c 100644 --- a/filechooser.lua +++ b/filechooser.lua @@ -33,13 +33,37 @@ FileChooser = { page = 1, current = 1, oldcurrent = 0, + exception_message = nil } +function getAbsolutePath(aPath) + local abs_path + if not aPath then + abs_path = aPath + elseif aPath:match('^//') then + abs_path = aPath:sub(2) + elseif aPath:match('^/') then + abs_path = aPath + elseif #aPath == 0 then + abs_path = '/' + else + local curr_dir = lfs.currentdir() + abs_path = aPath + if lfs.chdir(aPath) then + abs_path = lfs.currentdir() + lfs.chdir(curr_dir) + end + --print("rel: '"..aPath.."' abs:'"..abs_path.."'") + end + return abs_path +end + function FileChooser:readdir() self.dirs = {} self.files = {} for f in lfs.dir(self.path) do - if lfs.attributes(self.path.."/"..f, "mode") == "directory" and f ~= "." and not string.match(f, "^%.[^.]") then + if lfs.attributes(self.path.."/"..f, "mode") == "directory" and f ~= "." and not (f==".." and self.path=="/") and not string.match(f, "^%.[^.]") then + --print(self.path.." -> adding: '"..f.."'") table.insert(self.dirs, f) elseif string.match(f, ".+%.[pP][dD][fF]$") or string.match(f, ".+%.[dD][jJ][vV][uU]$")then table.insert(self.files, f) @@ -51,15 +75,22 @@ function FileChooser:readdir() end function FileChooser:setPath(newPath) - self.path = newPath - self:readdir() - self.items = #self.dirs + #self.files - if self.items == 0 then - return nil + local curr_path = self.path + self.path = getAbsolutePath(newPath) + local readdir_ok, exc = pcall(self.readdir,self) + if(not readdir_ok) then + print("readdir error: "..tostring(exc)) + self.exception_message = exc + return self:setPath(curr_path) + else + self.items = #self.dirs + #self.files + if self.items == 0 then + return nil + end + self.page = 1 + self.current = 1 + return true end - self.page = 1 - self.current = 1 - return true end function FileChooser:updateFont() @@ -74,7 +105,7 @@ function FileChooser:updateFont() end function FileChooser:choose(ypos, height) - local perpage = math.floor(height / self.spacing) - 1 + local perpage = math.floor(height / self.spacing) - 2 local pagedirty = true local markerdirty = false @@ -122,8 +153,11 @@ function FileChooser:choose(ypos, height) renderUtf8Text(fb.bb, 50, ypos + self.spacing*c, self.face, self.fhash, self.files[i-#self.dirs], true) end end - renderUtf8Text(fb.bb, 39, ypos + self.spacing * perpage + 32, self.fface, self.ffhash, - "Page "..self.page.." of "..(math.floor(self.items / perpage)+1), true) + renderUtf8Text(fb.bb, 5, ypos + self.spacing * perpage + 42, self.fface, self.ffhash, + "Page "..self.page.." of "..(math.floor(self.items / perpage)+1), true) + local msg = self.exception_message and self.exception_message:match("[^%:]+:%d+: (.*)") or "Path: "..self.path + self.exception_message = nil + renderUtf8Text(fb.bb, 5, ypos + self.spacing * (perpage+1) + 27, self.fface, self.ffhash, msg, true) markerdirty = true end if markerdirty then @@ -146,7 +180,7 @@ function FileChooser:choose(ypos, height) end local ev = input.waitForEvent() - print("key code:"..ev.code) + --print("key code:"..ev.code) ev.code = adjustKeyEvents(ev) if ev.type == EV_KEY and ev.value == EVENT_VALUE_KEY_PRESS then if ev.code == KEY_FW_UP then diff --git a/launchpad/kpdf.sh b/launchpad/kpdf.sh index e89a329fd..d493b9739 100755 --- a/launchpad/kpdf.sh +++ b/launchpad/kpdf.sh @@ -2,5 +2,5 @@ echo unlock > /proc/keypad echo unlock > /proc/fiveway cd /mnt/us/kindlepdfviewer/ -./reader.lua /mnt/us/documents +./reader.lua $1 echo 1 > /proc/eink_fb/update_display diff --git a/reader.lua b/reader.lua index 5ad66dcdd..1c2b1fee8 100755 --- a/reader.lua +++ b/reader.lua @@ -39,20 +39,21 @@ function openFile(filename) page_num = DJVUReader.settings:readsetting("last_page") or 1 DJVUReader:goto(tonumber(page_num)) DJVUReader:inputloop() + reader_settings:savesetting("lastfile", filename) end elseif file_type == "pdf" then if PDFReader:open(filename,"") then -- TODO: query for password page_num = PDFReader.settings:readsetting("last_page") or 1 PDFReader:goto(tonumber(page_num)) PDFReader:inputloop() + reader_settings:savesetting("lastfile", filename) end end end -optarg, optind = alt_getopt.get_opts(ARGV, "p:G:hg:d:", longopts) -if optarg["h"] or ARGV[optind] == nil then - print("usage: ./reader.lua [OPTION] ... DOCUMENT.PDF") - print("Read PDFs on your E-Ink reader") +function showusage() + print("usage: ./reader.lua [OPTION] ... path") + print("Read PDFs and DJVUs on your E-Ink reader") print("") print("-p, --password=PASSWORD set password for reading PDF document") print("-g, --goto=page start reading on page") @@ -63,14 +64,21 @@ if optarg["h"] or ARGV[optind] == nil then print(" \"emu\" (DXG emulation)") print("-h, --help show this usage help") print("") - print("If you give the name of a directory instead of a path, a file") - print("chooser will show up and let you select a PDF file") + print("If you give the name of a directory instead of a file path, a file") + print("chooser will show up and let you select a PDF|DJVU file") + print("") + print("If you don't pass any path, the last viewed document will be opened") print("") print("This software is licensed under the GPLv3.") print("See http://github.com/hwhw/kindlepdfviewer for more info.") return end +optarg, optind = alt_getopt.get_opts(ARGV, "p:G:hg:d:", longopts) +if optarg["h"] then + return showusage() +end + if optarg["d"] == "k3" then -- for now, the only difference is the additional input device @@ -116,9 +124,10 @@ PDFReader:init() DJVUReader:init() -- display directory or open file -if lfs.attributes(ARGV[optind], "mode") == "directory" then +local patharg = ARGV[optind] or reader_settings:readsetting("lastfile") +if patharg and lfs.attributes(patharg, "mode") == "directory" then local running = true - FileChooser:setPath(ARGV[optind]) + FileChooser:setPath(patharg) while running do local file = FileChooser:choose(0,height) if file ~= nil then @@ -127,8 +136,10 @@ if lfs.attributes(ARGV[optind], "mode") == "directory" then running = false end end +elseif patharg then + openFile(patharg, optarg["p"]) else - openFile(ARGV[optind], optarg["p"]) + return showusage() end