2
0
mirror of https://github.com/koreader/koreader synced 2024-11-13 19:11:25 +00:00
koreader/filesearcher.lua

274 lines
6.8 KiB
Lua
Raw Normal View History

2012-02-18 12:35:03 +00:00
require "rendertext"
require "keys"
require "graphics"
2012-02-23 16:16:20 +00:00
require "fontchooser"
2012-02-18 12:35:03 +00:00
FileSearcher = {
2012-02-23 16:16:20 +00:00
-- font for displaying toc item names
fsize = 25,
face = nil,
fhash = nil,
2012-02-18 23:21:50 +00:00
-- font for page title
2012-02-23 16:16:20 +00:00
tfsize = 30,
tface = nil,
tfhash = nil,
2012-02-18 12:35:03 +00:00
-- font for paging display
2012-02-23 16:16:20 +00:00
ffsize = 16,
fface = nil,
ffhash = nil,
2012-02-18 23:21:50 +00:00
-- title height
title_H = 45,
2012-02-18 12:35:03 +00:00
-- spacing between lines
spacing = 40,
2012-02-18 23:21:50 +00:00
-- foot height
foot_H = 27,
2012-02-18 12:35:03 +00:00
-- state buffer
dirs = {},
files = {},
result = {},
2012-02-19 06:41:44 +00:00
items = 0,
page = 0,
current = 1,
2012-02-18 23:21:50 +00:00
oldcurrent = 1,
2012-02-18 12:35:03 +00:00
}
function FileSearcher:readdir()
self.dirs = {self.path}
self.files = {}
while #self.dirs ~= 0 do
new_dirs = {}
-- handle each dir
for __, d in pairs(self.dirs) do
-- handle files in d
for f in lfs.dir(d) do
if lfs.attributes(d.."/"..f, "mode") == "directory"
and f ~= "." and f~= ".." and not string.match(f, "^%.[^.]") then
table.insert(new_dirs, d.."/"..f)
2012-03-03 14:56:54 +00:00
elseif string.match(f, ".+%.[pP][dD][fF]$") or string.match(f, ".+%.[dD][jJ][vV][uU]$") then
file_entry = {dir=d, name=f,}
table.insert(self.files, file_entry)
--print("file:"..d.."/"..f)
end
end
end
self.dirs = new_dirs
end
end
function FileSearcher:setPath(newPath)
self.path = newPath
self:readdir()
self.items = #self.files
--@TODO check none found 19.02 2012
if self.items == 0 then
return nil
end
self.page = 1
self.current = 1
return true
end
2012-02-23 16:16:20 +00:00
function FileSearcher:updateFont()
if self.fhash ~= FontChooser.cfont..self.fsize then
self.face = freetype.newBuiltinFace(FontChooser.cfont, self.fsize)
self.fhash = FontChooser.cfont..self.fsize
end
if self.tfhash ~= FontChooser.tfont..self.tfsize then
self.tface = freetype.newBuiltinFace(FontChooser.tfont, self.tfsize)
self.tfhash = FontChooser.tfont..self.tfsize
end
if self.ffhash ~= FontChooser.ffont..self.ffsize then
self.fface = freetype.newBuiltinFace(FontChooser.ffont, self.ffsize)
self.ffhash = FontChooser.ffont..self.ffsize
end
end
function FileSearcher:setSearchResult(keywords)
2012-02-19 06:41:44 +00:00
self.result = {}
if keywords == " " then -- one space to show all files
self.result = self.files
else
for __,f in pairs(self.files) do
if string.find(string.lower(f.name), keywords) then
table.insert(self.result, f)
end
end
end
self.items = #self.result
self.page = 1
self.current = 1
end
2012-02-19 06:41:44 +00:00
function FileSearcher:init(search_path)
if search_path then
self:setPath(search_path)
else
self:setPath("/mnt/us/documents")
end
2012-02-18 12:35:03 +00:00
end
2012-02-18 23:21:50 +00:00
function FileSearcher:choose(ypos, height, keywords)
local perpage = math.floor(height / self.spacing) - 2
2012-02-18 12:35:03 +00:00
local pagedirty = true
local markerdirty = false
local prevItem = function ()
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
local nextItem = function ()
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
-- if given keywords, set new result according to keywords.
-- Otherwise, display the previous search result.
if keywords then
self:setSearchResult(keywords)
end
2012-02-18 23:21:50 +00:00
2012-02-18 12:35:03 +00:00
while true do
2012-02-28 15:05:26 +00:00
self:updateFont()
2012-02-18 12:35:03 +00:00
if pagedirty then
2012-02-19 06:41:44 +00:00
markerdirty = true
2012-02-18 12:35:03 +00:00
fb.bb:paintRect(0, ypos, fb.bb:getWidth(), height, 0)
2012-02-18 23:21:50 +00:00
-- draw menu title
renderUtf8Text(fb.bb, 30, ypos + self.title_H, self.tface, self.tfhash,
2012-02-19 06:41:44 +00:00
"Search Result for: "..keywords, true)
2012-02-18 23:21:50 +00:00
-- draw results
2012-02-18 12:35:03 +00:00
local c
2012-02-19 06:41:44 +00:00
if self.items == 0 then -- nothing found
y = ypos + self.title_H + self.spacing * 2
renderUtf8Text(fb.bb, 20, y, self.face, self.fhash,
2012-02-23 16:39:39 +00:00
"Sorry, no match found.", true)
2012-02-19 06:41:44 +00:00
renderUtf8Text(fb.bb, 20, y + self.spacing, self.face, self.fhash,
"Please try a different keyword.", true)
markerdirty = false
else -- found something, draw it
for c = 1, perpage do
local i = (self.page - 1) * perpage + c
if i <= self.items then
y = ypos + self.title_H + (self.spacing * c)
renderUtf8Text(fb.bb, 50, y, self.face, self.fhash,
self.result[i].name, true)
end
2012-02-18 12:35:03 +00:00
end
end
-- draw footer
2012-02-18 23:21:50 +00:00
y = ypos + self.title_H + (self.spacing * perpage) + self.foot_H
x = (fb.bb:getWidth() / 2) - 50
all_page = (math.floor(self.items / perpage)+1)
2012-02-23 16:16:20 +00:00
renderUtf8Text(fb.bb, x, y, self.fface, self.ffhash,
"Page "..self.page.." of "..all_page, true)
2012-02-18 12:35:03 +00:00
end
2012-02-18 23:21:50 +00:00
2012-02-18 12:35:03 +00:00
if markerdirty then
if not pagedirty then
if self.oldcurrent > 0 then
2012-02-18 23:21:50 +00:00
y = ypos + self.title_H + (self.spacing * self.oldcurrent) + 10
fb.bb:paintRect(30, y, fb.bb:getWidth() - 60, 3, 0)
fb:refresh(1, 30, y, fb.bb:getWidth() - 60, 3)
2012-02-18 12:35:03 +00:00
end
end
2012-02-18 23:21:50 +00:00
-- draw new marker line
y = ypos + self.title_H + (self.spacing * self.current) + 10
fb.bb:paintRect(30, y, fb.bb:getWidth() - 60, 3, 15)
2012-02-18 12:35:03 +00:00
if not pagedirty then
2012-02-18 23:21:50 +00:00
fb:refresh(1, 30, y, fb.bb:getWidth() - 60, 3)
2012-02-18 12:35:03 +00:00
end
self.oldcurrent = self.current
markerdirty = false
end
2012-02-18 23:21:50 +00:00
2012-02-18 12:35:03 +00:00
if pagedirty then
fb:refresh(0, 0, ypos, fb.bb:getWidth(), height)
pagedirty = false
end
2012-02-18 23:21:50 +00:00
2012-02-18 12:35:03 +00:00
local ev = input.waitForEvent()
ev.code = adjustKeyEvents(ev)
2012-02-18 12:35:03 +00:00
if ev.type == EV_KEY and ev.value == EVENT_VALUE_KEY_PRESS then
if ev.code == KEY_FW_UP then
2012-02-18 12:35:03 +00:00
prevItem()
elseif ev.code == KEY_FW_DOWN then
nextItem()
elseif ev.code == KEY_PGFWD then
if self.page < (self.items / perpage) then
if self.current + self.page*perpage > self.items then
self.current = self.items - self.page*perpage
end
self.page = self.page + 1
pagedirty = true
else
self.current = self.items - (self.page-1)*perpage
markerdirty = true
end
elseif ev.code == KEY_PGBCK then
if self.page > 1 then
self.page = self.page - 1
pagedirty = true
else
self.current = 1
markerdirty = true
end
elseif ev.code == KEY_S then
2012-02-19 06:41:44 +00:00
old_keywords = keywords
keywords = InputBox:input(height-100, 100, "Search:", old_keywords)
if keywords then
self:setSearchResult(keywords)
else
keywords = old_keywords
end
pagedirty = true
2012-02-28 14:51:37 +00:00
elseif ev.code == KEY_F then -- invoke fontchooser menu
fonts_menu = SelectMenu:new{
menu_title = "Fonts Menu",
item_array = FontChooser.fonts,
}
2012-02-28 15:05:26 +00:00
local re = fonts_menu:choose(0, height)
if re then
FontChooser.cfont = FontChooser.fonts[re]
FontChooser:init()
end
2012-02-28 14:51:37 +00:00
pagedirty = true
2012-02-18 12:35:03 +00:00
elseif ev.code == KEY_ENTER or ev.code == KEY_FW_PRESS then
file_entry = self.result[perpage*(self.page-1)+self.current]
2012-03-03 14:56:54 +00:00
file_full_path = file_entry.dir .. "/" .. file_entry.name
openFile(file_full_path)
pagedirty = true
2012-02-18 12:35:03 +00:00
elseif ev.code == KEY_BACK then
return nil
end
end
end
end