2011-12-08 16:56:07 +00:00
|
|
|
require "rendertext"
|
|
|
|
require "keys"
|
|
|
|
require "graphics"
|
2012-03-10 08:41:23 +00:00
|
|
|
require "font"
|
2012-02-18 12:32:38 +00:00
|
|
|
require "filesearcher"
|
2012-02-18 10:21:03 +00:00
|
|
|
require "inputbox"
|
2012-02-26 02:42:04 +00:00
|
|
|
require "selectmenu"
|
2011-12-08 16:56:07 +00:00
|
|
|
|
|
|
|
FileChooser = {
|
2011-12-08 18:01:40 +00:00
|
|
|
-- Class vars:
|
2012-03-20 19:15:24 +00:00
|
|
|
|
2011-12-08 18:01:40 +00:00
|
|
|
-- spacing between lines
|
|
|
|
spacing = 40,
|
|
|
|
|
|
|
|
-- state buffer
|
2011-12-08 16:56:07 +00:00
|
|
|
dirs = nil,
|
2011-12-08 18:01:40 +00:00
|
|
|
files = nil,
|
|
|
|
items = 0,
|
|
|
|
path = "",
|
|
|
|
page = 1,
|
|
|
|
current = 1,
|
|
|
|
oldcurrent = 0,
|
2012-03-04 22:03:04 +00:00
|
|
|
exception_message = nil
|
2011-12-08 16:56:07 +00:00
|
|
|
}
|
|
|
|
|
2012-03-04 22:03:04 +00:00
|
|
|
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
|
2012-04-18 16:16:49 +00:00
|
|
|
--debug("rel: '"..aPath.."' abs:'"..abs_path.."'")
|
2012-03-04 22:03:04 +00:00
|
|
|
end
|
|
|
|
return abs_path
|
|
|
|
end
|
|
|
|
|
2012-03-20 19:15:24 +00:00
|
|
|
function FileChooser:readDir()
|
2011-12-08 16:56:07 +00:00
|
|
|
self.dirs = {}
|
|
|
|
self.files = {}
|
2011-12-08 18:01:40 +00:00
|
|
|
for f in lfs.dir(self.path) do
|
2012-03-04 22:03:04 +00:00
|
|
|
if lfs.attributes(self.path.."/"..f, "mode") == "directory" and f ~= "." and not (f==".." and self.path=="/") and not string.match(f, "^%.[^.]") then
|
2012-04-18 16:16:49 +00:00
|
|
|
--debug(self.path.." -> adding: '"..f.."'")
|
2011-12-08 16:56:07 +00:00
|
|
|
table.insert(self.dirs, f)
|
2012-03-19 21:35:16 +00:00
|
|
|
else
|
|
|
|
local file_type = string.lower(string.match(f, ".+%.([^.]+)") or "")
|
2012-04-05 02:31:31 +00:00
|
|
|
if file_type == "djvu"
|
|
|
|
or file_type == "pdf" or file_type == "xps" or file_type == "cbz"
|
|
|
|
or file_type == "epub" or file_type == "txt" or file_type == "rtf"
|
2012-04-21 12:18:23 +00:00
|
|
|
or file_type == "htm" or file_type == "html" or file_type == "mobi"
|
2012-04-24 12:44:59 +00:00
|
|
|
or file_type == "fb2" or file_type == "chm" or file_type == "doc"
|
|
|
|
or file_type == "zip" then
|
2012-03-19 21:35:16 +00:00
|
|
|
table.insert(self.files, f)
|
|
|
|
end
|
2011-12-08 16:56:07 +00:00
|
|
|
end
|
|
|
|
end
|
2012-02-17 05:46:40 +00:00
|
|
|
--@TODO make sure .. is sortted to the first item 16.02 2012
|
2011-12-08 16:56:07 +00:00
|
|
|
table.sort(self.dirs)
|
|
|
|
table.sort(self.files)
|
|
|
|
end
|
|
|
|
|
2011-12-08 18:01:40 +00:00
|
|
|
function FileChooser:setPath(newPath)
|
2012-03-04 22:03:04 +00:00
|
|
|
local curr_path = self.path
|
|
|
|
self.path = getAbsolutePath(newPath)
|
2012-03-20 19:15:24 +00:00
|
|
|
local readdir_ok, exc = pcall(self.readDir,self)
|
2012-03-04 22:03:04 +00:00
|
|
|
if(not readdir_ok) then
|
2012-04-18 16:16:49 +00:00
|
|
|
debug("readDir error: "..tostring(exc))
|
2012-03-04 22:03:04 +00:00
|
|
|
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
|
2011-12-08 18:01:40 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function FileChooser:choose(ypos, height)
|
2012-03-04 22:03:04 +00:00
|
|
|
local perpage = math.floor(height / self.spacing) - 2
|
2011-12-08 18:01:40 +00:00
|
|
|
local pagedirty = true
|
|
|
|
local markerdirty = false
|
2012-02-05 03:09:22 +00:00
|
|
|
|
2012-02-16 07:19:56 +00:00
|
|
|
local prevItem = function ()
|
2012-02-05 03:09:22 +00:00
|
|
|
if self.current == 1 then
|
|
|
|
if self.page > 1 then
|
|
|
|
self.current = perpage
|
|
|
|
self.page = self.page - 1
|
|
|
|
pagedirty = true
|
|
|
|
end
|
|
|
|
else
|
|
|
|
self.current = self.current - 1
|
|
|
|
markerdirty = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-02-16 07:19:56 +00:00
|
|
|
local nextItem = function ()
|
2012-02-05 03:09:22 +00:00
|
|
|
if self.current == perpage then
|
|
|
|
if self.page < (self.items / perpage) then
|
|
|
|
self.current = 1
|
|
|
|
self.page = self.page + 1
|
|
|
|
pagedirty = true
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if self.page ~= math.floor(self.items / perpage) + 1
|
|
|
|
or self.current + (self.page-1)*perpage < self.items then
|
|
|
|
self.current = self.current + 1
|
|
|
|
markerdirty = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-12-08 16:56:07 +00:00
|
|
|
while true do
|
2012-04-09 17:04:26 +00:00
|
|
|
local cface = Font:getFace("cfont", 25)
|
|
|
|
local fface = Font:getFace("ffont", 16)
|
2012-03-10 08:41:23 +00:00
|
|
|
|
2011-12-08 16:56:07 +00:00
|
|
|
if pagedirty then
|
|
|
|
fb.bb:paintRect(0, ypos, fb.bb:getWidth(), height, 0)
|
|
|
|
local c
|
|
|
|
for c = 1, perpage do
|
2011-12-08 18:01:40 +00:00
|
|
|
local i = (self.page - 1) * perpage + c
|
2011-12-08 16:56:07 +00:00
|
|
|
if i <= #self.dirs then
|
2011-12-08 18:01:40 +00:00
|
|
|
-- resembles display in midnight commander: adds "/" prefix for directories
|
2012-04-09 17:04:26 +00:00
|
|
|
renderUtf8Text(fb.bb, 39, ypos + self.spacing*c, cface, "/", true)
|
|
|
|
renderUtf8Text(fb.bb, 50, ypos + self.spacing*c, cface, self.dirs[i], true)
|
2011-12-08 18:01:40 +00:00
|
|
|
elseif i <= self.items then
|
2012-04-09 17:04:26 +00:00
|
|
|
renderUtf8Text(fb.bb, 50, ypos + self.spacing*c, cface, self.files[i-#self.dirs], true)
|
2011-12-08 16:56:07 +00:00
|
|
|
end
|
|
|
|
end
|
2012-04-18 10:37:00 +00:00
|
|
|
all_page = math.ceil(self.items/perpage)
|
2012-04-09 17:04:26 +00:00
|
|
|
renderUtf8Text(fb.bb, 5, ypos + self.spacing * perpage + 42, fface,
|
2012-04-18 10:37:00 +00:00
|
|
|
"Page "..self.page.." of "..all_page, true)
|
2012-03-04 22:03:04 +00:00
|
|
|
local msg = self.exception_message and self.exception_message:match("[^%:]+:%d+: (.*)") or "Path: "..self.path
|
|
|
|
self.exception_message = nil
|
2012-04-09 17:04:26 +00:00
|
|
|
renderUtf8Text(fb.bb, 5, ypos + self.spacing * (perpage+1) + 27, fface, msg, true)
|
2011-12-08 16:56:07 +00:00
|
|
|
markerdirty = true
|
|
|
|
end
|
|
|
|
if markerdirty then
|
2011-12-08 18:01:40 +00:00
|
|
|
if not pagedirty then
|
|
|
|
if self.oldcurrent > 0 then
|
|
|
|
fb.bb:paintRect(30, ypos + self.spacing*self.oldcurrent + 10, fb.bb:getWidth() - 60, 3, 0)
|
|
|
|
fb:refresh(1, 30, ypos + self.spacing*self.oldcurrent + 10, fb.bb:getWidth() - 60, 3)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
fb.bb:paintRect(30, ypos + self.spacing*self.current + 10, fb.bb:getWidth() - 60, 3, 15)
|
|
|
|
if not pagedirty then
|
|
|
|
fb:refresh(1, 30, ypos + self.spacing*self.current + 10, fb.bb:getWidth() - 60, 3)
|
2011-12-08 16:56:07 +00:00
|
|
|
end
|
2011-12-08 18:01:40 +00:00
|
|
|
self.oldcurrent = self.current
|
2011-12-08 16:56:07 +00:00
|
|
|
markerdirty = false
|
|
|
|
end
|
2011-12-08 18:01:40 +00:00
|
|
|
if pagedirty then
|
|
|
|
fb:refresh(0, 0, ypos, fb.bb:getWidth(), height)
|
|
|
|
pagedirty = false
|
|
|
|
end
|
2012-03-02 10:38:34 +00:00
|
|
|
|
2012-04-12 19:00:44 +00:00
|
|
|
local ev = input.saveWaitForEvent()
|
2012-04-18 16:16:49 +00:00
|
|
|
--debug("key code:"..ev.code)
|
2012-03-02 10:38:34 +00:00
|
|
|
ev.code = adjustKeyEvents(ev)
|
2011-12-08 16:56:07 +00:00
|
|
|
if ev.type == EV_KEY and ev.value == EVENT_VALUE_KEY_PRESS then
|
2012-03-02 10:38:34 +00:00
|
|
|
if ev.code == KEY_FW_UP then
|
2012-02-17 06:52:54 +00:00
|
|
|
prevItem()
|
2011-12-08 16:56:07 +00:00
|
|
|
elseif ev.code == KEY_FW_DOWN then
|
2012-02-17 06:52:54 +00:00
|
|
|
nextItem()
|
2012-02-19 07:29:51 +00:00
|
|
|
elseif ev.code == KEY_F then -- invoke fontchooser menu
|
2012-04-09 17:04:26 +00:00
|
|
|
local fonts_menu = SelectMenu:new{
|
2012-02-26 02:42:04 +00:00
|
|
|
menu_title = "Fonts Menu",
|
2012-04-09 17:04:26 +00:00
|
|
|
item_array = Font:getFontList(),
|
2012-02-26 02:42:04 +00:00
|
|
|
}
|
2012-04-09 17:04:26 +00:00
|
|
|
local re, font = fonts_menu:choose(0, height)
|
2012-02-28 15:05:26 +00:00
|
|
|
if re then
|
2012-04-09 17:04:26 +00:00
|
|
|
Font.fontmap["cfont"] = font
|
2012-03-10 08:41:23 +00:00
|
|
|
Font:update()
|
2012-02-28 15:05:26 +00:00
|
|
|
end
|
2012-02-16 07:19:56 +00:00
|
|
|
pagedirty = true
|
2012-02-19 07:29:51 +00:00
|
|
|
elseif ev.code == KEY_S then -- invoke search input
|
2012-02-18 23:21:50 +00:00
|
|
|
keywords = InputBox:input(height-100, 100, "Search:")
|
2012-03-20 19:15:24 +00:00
|
|
|
if keywords then
|
2012-03-08 15:28:16 +00:00
|
|
|
-- call FileSearcher
|
|
|
|
--[[
|
|
|
|
This might looks a little bit dirty for using callback.
|
|
|
|
But I cannot come up with a better solution for renewing
|
2012-04-19 01:49:46 +00:00
|
|
|
the height argument according to screen rotation mode.
|
2012-03-08 15:28:16 +00:00
|
|
|
|
2012-03-20 19:15:24 +00:00
|
|
|
The callback might also be useful for calling system
|
2012-03-08 15:28:16 +00:00
|
|
|
settings menu in the future.
|
|
|
|
--]]
|
|
|
|
return nil, function()
|
2012-04-20 12:48:17 +00:00
|
|
|
InfoMessage:show("Searching...",0)
|
2012-03-08 15:28:16 +00:00
|
|
|
FileSearcher:init( self.path )
|
2012-04-08 06:31:23 +00:00
|
|
|
FileSearcher:choose(keywords)
|
2012-02-18 23:21:50 +00:00
|
|
|
end
|
|
|
|
end
|
2012-02-17 05:46:40 +00:00
|
|
|
pagedirty = true
|
2012-03-26 12:19:48 +00:00
|
|
|
elseif ev.code == KEY_PGFWD or ev.code == KEY_LPGFWD then
|
2011-12-08 18:01:40 +00:00
|
|
|
if self.page < (self.items / perpage) then
|
|
|
|
if self.current + self.page*perpage > self.items then
|
|
|
|
self.current = self.items - self.page*perpage
|
2011-12-08 16:56:07 +00:00
|
|
|
end
|
2011-12-08 18:01:40 +00:00
|
|
|
self.page = self.page + 1
|
2011-12-08 16:56:07 +00:00
|
|
|
pagedirty = true
|
|
|
|
else
|
2011-12-08 18:01:40 +00:00
|
|
|
self.current = self.items - (self.page-1)*perpage
|
2011-12-08 16:56:07 +00:00
|
|
|
markerdirty = true
|
|
|
|
end
|
2012-03-26 12:19:48 +00:00
|
|
|
elseif ev.code == KEY_PGBCK or ev.code == KEY_LPGBCK then
|
2011-12-08 18:01:40 +00:00
|
|
|
if self.page > 1 then
|
|
|
|
self.page = self.page - 1
|
2011-12-08 16:56:07 +00:00
|
|
|
pagedirty = true
|
|
|
|
else
|
2011-12-08 18:01:40 +00:00
|
|
|
self.current = 1
|
2011-12-08 16:56:07 +00:00
|
|
|
markerdirty = true
|
|
|
|
end
|
2011-12-08 23:22:09 +00:00
|
|
|
elseif ev.code == KEY_ENTER or ev.code == KEY_FW_PRESS then
|
2011-12-08 18:01:40 +00:00
|
|
|
local newdir = self.dirs[perpage*(self.page-1)+self.current]
|
2011-12-08 16:56:07 +00:00
|
|
|
if newdir == ".." then
|
2011-12-08 18:01:40 +00:00
|
|
|
local path = string.gsub(self.path, "(.*)/[^/]+/?$", "%1")
|
|
|
|
self:setPath(path)
|
2011-12-08 16:56:07 +00:00
|
|
|
elseif newdir then
|
2011-12-08 18:01:40 +00:00
|
|
|
local path = self.path.."/"..newdir
|
|
|
|
self:setPath(path)
|
2011-12-08 16:56:07 +00:00
|
|
|
else
|
2011-12-08 18:01:40 +00:00
|
|
|
return self.path.."/"..self.files[perpage*(self.page-1)+self.current - #self.dirs]
|
2011-12-08 16:56:07 +00:00
|
|
|
end
|
2011-12-08 18:01:40 +00:00
|
|
|
pagedirty = true
|
2012-03-06 19:07:04 +00:00
|
|
|
elseif ev.code == KEY_BACK or ev.code == KEY_HOME then
|
2011-12-08 16:56:07 +00:00
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|