2012-05-19 10:59:38 +00:00
|
|
|
require "rendertext"
|
|
|
|
require "keys"
|
|
|
|
require "graphics"
|
|
|
|
require "font"
|
|
|
|
require "inputbox"
|
|
|
|
require "dialog"
|
|
|
|
require "filesearcher"
|
|
|
|
require "settings"
|
2012-10-02 22:45:45 +00:00
|
|
|
require "dialog"
|
2012-05-19 10:59:38 +00:00
|
|
|
|
|
|
|
FileHistory = {
|
2012-10-02 22:45:45 +00:00
|
|
|
title_H = 40, -- title height
|
|
|
|
spacing = 36, -- spacing between lines
|
|
|
|
foot_H = 28, -- foot height
|
|
|
|
margin_H = 10, -- horisontal margin
|
2012-05-19 10:59:38 +00:00
|
|
|
|
|
|
|
-- state buffer
|
|
|
|
history_files = {},
|
|
|
|
files = {},
|
|
|
|
result = {},
|
|
|
|
items = 0,
|
|
|
|
page = 0,
|
|
|
|
current = 1,
|
|
|
|
oldcurrent = 1,
|
2012-10-02 22:45:45 +00:00
|
|
|
commands = nil,
|
2012-05-19 10:59:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function FileHistory:init(history_path)
|
2012-10-03 15:13:33 +00:00
|
|
|
self:setPath("history")
|
2012-10-02 22:45:45 +00:00
|
|
|
-- to initialize only once
|
|
|
|
if not self.commands then self:addAllCommands() end
|
2012-05-19 10:59:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function FileHistory:setPath(newPath)
|
|
|
|
self.path = newPath
|
|
|
|
self:readDir("-c ")
|
|
|
|
self.items = #self.files
|
2012-10-02 22:45:45 +00:00
|
|
|
if self.items == 1 then
|
2012-05-19 10:59:38 +00:00
|
|
|
return nil
|
|
|
|
end
|
|
|
|
self.page = 1
|
|
|
|
self.current = 1
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
function FileHistory:readDir(order_criteria)
|
2012-10-03 15:13:33 +00:00
|
|
|
self.history_files = {}
|
|
|
|
self.files = {}
|
2012-09-24 08:04:57 +00:00
|
|
|
local p = io.popen("ls "..order_criteria.."-1 "..self.path)
|
|
|
|
for f in p:lines() do
|
2012-05-19 10:59:38 +00:00
|
|
|
-- insert history files
|
2012-10-02 22:45:45 +00:00
|
|
|
table.insert(self.history_files, {dir=self.path, name=f})
|
2012-05-19 10:59:38 +00:00
|
|
|
-- and corresponding path & file items
|
2012-10-02 22:45:45 +00:00
|
|
|
table.insert(self.files, {dir=HistoryToPath(f), name=HistoryToName(f)})
|
2012-05-19 10:59:38 +00:00
|
|
|
end
|
2012-09-24 08:04:57 +00:00
|
|
|
p:close()
|
2012-05-19 10:59:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function FileHistory:setSearchResult(keywords)
|
2012-10-03 15:13:33 +00:00
|
|
|
self.result = {}
|
2012-05-19 10:59:38 +00:00
|
|
|
if keywords == "" or keywords == " " then
|
2012-10-02 22:45:45 +00:00
|
|
|
-- show all history
|
2012-05-19 10:59:38 +00:00
|
|
|
self.result = self.files
|
|
|
|
else
|
|
|
|
-- select history files with keywords in the filename
|
|
|
|
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.keywords = keywords
|
|
|
|
self.items = #self.result
|
|
|
|
self.page = 1
|
|
|
|
self.current = 1
|
2012-10-02 22:45:45 +00:00
|
|
|
return self.items
|
2012-05-19 10:59:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function FileHistory:prevItem()
|
|
|
|
if self.current == 1 then
|
|
|
|
if self.page > 1 then
|
|
|
|
self.current = self.perpage
|
|
|
|
self.page = self.page - 1
|
|
|
|
self.pagedirty = true
|
|
|
|
end
|
|
|
|
else
|
|
|
|
self.current = self.current - 1
|
|
|
|
self.markerdirty = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function FileHistory:nextItem()
|
|
|
|
if self.current == self.perpage then
|
|
|
|
if self.page < (self.items / self.perpage) then
|
|
|
|
self.current = 1
|
|
|
|
self.page = self.page + 1
|
|
|
|
self.pagedirty = true
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if self.page ~= math.floor(self.items / self.perpage) + 1
|
|
|
|
or self.current + (self.page-1)*self.perpage < self.items then
|
|
|
|
self.current = self.current + 1
|
|
|
|
self.markerdirty = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function FileHistory:addAllCommands()
|
|
|
|
self.commands = Commands:new{}
|
|
|
|
self.commands:add(KEY_H, nil, "H",
|
|
|
|
"show help page",
|
|
|
|
function(self)
|
|
|
|
HelpPage:show(0, G_height, self.commands)
|
|
|
|
self.pagedirty = true
|
|
|
|
end
|
|
|
|
)
|
|
|
|
self.commands:add(KEY_FW_UP, nil, "joypad up",
|
2012-10-02 22:45:45 +00:00
|
|
|
"previous item",
|
2012-05-19 10:59:38 +00:00
|
|
|
function(self)
|
|
|
|
self:prevItem()
|
|
|
|
end
|
|
|
|
)
|
|
|
|
self.commands:add(KEY_FW_DOWN, nil, "joypad down",
|
2012-10-02 22:45:45 +00:00
|
|
|
"next item",
|
2012-05-19 10:59:38 +00:00
|
|
|
function(self)
|
|
|
|
self:nextItem()
|
|
|
|
end
|
|
|
|
)
|
2012-10-02 22:45:45 +00:00
|
|
|
-- NuPogodi, 01.10.12: fast jumps to items at positions 10, 20, .. 90, 0% within the list
|
|
|
|
local numeric_keydefs, i = {}
|
|
|
|
for i=1, 10 do numeric_keydefs[i]=Keydef:new(KEY_1+i-1, nil, tostring(i%10)) end
|
|
|
|
self.commands:addGroup("[1, 2 .. 9, 0]", numeric_keydefs,
|
|
|
|
"item at position 0%, 10% .. 90%, 100%",
|
|
|
|
function(self)
|
|
|
|
local target_item = math.ceil(self.items * (keydef.keycode-KEY_1) / 9)
|
|
|
|
self.current, self.page, self.markerdirty, self.pagedirty =
|
|
|
|
gotoTargetItem(target_item, self.items, self.current, self.page, self.perpage)
|
|
|
|
end
|
|
|
|
)
|
2012-05-19 10:59:38 +00:00
|
|
|
self.commands:add({KEY_PGFWD, KEY_LPGFWD}, nil, ">",
|
|
|
|
"next page",
|
|
|
|
function(self)
|
|
|
|
if self.page < (self.items / self.perpage) then
|
|
|
|
if self.current + self.page*self.perpage > self.items then
|
|
|
|
self.current = self.items - self.page*self.perpage
|
|
|
|
end
|
|
|
|
self.page = self.page + 1
|
|
|
|
self.pagedirty = true
|
|
|
|
else
|
|
|
|
self.current = self.items - (self.page-1)*self.perpage
|
|
|
|
self.markerdirty = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
|
|
|
self.commands:add({KEY_PGBCK, KEY_LPGBCK}, nil, "<",
|
|
|
|
"previous page",
|
|
|
|
function(self)
|
|
|
|
if self.page > 1 then
|
|
|
|
self.page = self.page - 1
|
|
|
|
self.pagedirty = true
|
|
|
|
else
|
|
|
|
self.current = 1
|
|
|
|
self.markerdirty = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
2012-10-02 22:45:45 +00:00
|
|
|
self.commands:add(KEY_G, nil, "G", -- NuPogodi, 01.10.12: goto page No.
|
|
|
|
"goto page",
|
|
|
|
function(self)
|
|
|
|
local n = math.ceil(self.items / self.perpage)
|
|
|
|
local page = NumInputBox:input(G_height-100, 100, "Page:", "current page "..self.page.." of "..n, true)
|
|
|
|
if pcall(function () page = math.floor(page) end) -- convert string to number
|
|
|
|
and page ~= self.page and page > 0 and page <= n then
|
|
|
|
self.page = page
|
|
|
|
if self.current + (page-1)*self.perpage > self.items then
|
|
|
|
self.current = self.items - (page-1)*self.perpage
|
|
|
|
end
|
|
|
|
end
|
|
|
|
self.pagedirty = true
|
|
|
|
end
|
|
|
|
)
|
|
|
|
self.commands:add(KEY_FW_RIGHT, nil, "joypad right",
|
|
|
|
"document details",
|
|
|
|
function(self)
|
|
|
|
local file_entry = self.result[self.perpage*(self.page-1)+self.current]
|
|
|
|
if file_entry.name == ".." then
|
|
|
|
warningUnsupportedFunction()
|
|
|
|
return
|
|
|
|
end -- do not show details
|
|
|
|
FileInfo:show(file_entry.dir,file_entry.name)
|
|
|
|
self.pagedirty = true
|
|
|
|
end
|
|
|
|
)
|
|
|
|
self.commands:add(KEY_S, nil, "S",
|
|
|
|
"invoke search inputbox",
|
|
|
|
function(self)
|
|
|
|
-- NuPogodi, 30.09.12: be sure that something is found
|
|
|
|
local old_keywords = self.keywords
|
|
|
|
local old_data = self.result
|
|
|
|
local old_page, old_current = self.page, self.current
|
|
|
|
self.keywords = InputBox:input(G_height - 100, 100, "Search:", old_keywords)
|
|
|
|
if self.keywords then
|
|
|
|
self:setSearchResult(self.keywords)
|
2012-10-04 22:22:08 +00:00
|
|
|
else
|
|
|
|
self.keywords = old_keywords
|
2012-10-02 22:45:45 +00:00
|
|
|
end
|
2012-10-03 15:13:33 +00:00
|
|
|
if #self.result < 1 then
|
2012-10-05 09:59:37 +00:00
|
|
|
InfoMessage:inform("No search hits ", 2000, 1, MSG_WARN,
|
|
|
|
"The search has given no results")
|
2012-10-02 22:45:45 +00:00
|
|
|
-- restoring the original data
|
|
|
|
self.result = old_data
|
|
|
|
self.items = #self.result
|
|
|
|
self.keywords = old_keywords
|
|
|
|
self.page = old_page
|
|
|
|
self.current = old_current
|
|
|
|
end
|
|
|
|
self.pagedirty = true
|
|
|
|
end
|
|
|
|
)
|
|
|
|
self.commands:add({KEY_F, KEY_AA}, nil, "F, Aa",
|
2012-09-06 18:04:20 +00:00
|
|
|
"change font faces",
|
2012-05-19 10:59:38 +00:00
|
|
|
function(self)
|
2012-09-06 18:04:20 +00:00
|
|
|
Font:chooseFonts()
|
2012-05-19 10:59:38 +00:00
|
|
|
self.pagedirty = true
|
|
|
|
end
|
|
|
|
)
|
|
|
|
self.commands:add({KEY_ENTER, KEY_FW_PRESS}, nil, "Enter",
|
2012-10-02 22:45:45 +00:00
|
|
|
"open selected document",
|
2012-05-19 10:59:38 +00:00
|
|
|
function(self)
|
2012-10-02 22:45:45 +00:00
|
|
|
local file_entry = self.result[self.perpage*(self.page-1)+self.current]
|
2012-05-19 10:59:38 +00:00
|
|
|
file_full_path = file_entry.dir .. "/" .. file_entry.name
|
2012-10-04 17:21:40 +00:00
|
|
|
if FileExists(file_full_path) then
|
|
|
|
openFile(file_full_path)
|
|
|
|
--reset height and item index if screen has been rotated
|
|
|
|
local item_no = self.perpage * (self.page - 1) + self.current
|
|
|
|
self.perpage = math.floor(G_height / self.spacing) - 2
|
|
|
|
self.current = item_no % self.perpage
|
|
|
|
self.page = math.floor(item_no / self.perpage) + 1
|
|
|
|
self.pagedirty = true
|
|
|
|
else
|
2012-10-04 17:37:46 +00:00
|
|
|
InfoMessage:inform("File does not exist ", 2000, 1, MSG_ERROR)
|
2012-10-04 17:21:40 +00:00
|
|
|
end
|
2012-05-19 10:59:38 +00:00
|
|
|
end
|
|
|
|
)
|
2012-10-03 15:13:33 +00:00
|
|
|
self.commands:add(KEY_DEL, nil, "Del",
|
2012-08-27 14:14:15 +00:00
|
|
|
"delete history entry",
|
2012-05-19 10:59:38 +00:00
|
|
|
function(self)
|
2012-10-02 22:45:45 +00:00
|
|
|
local file_entry = self.result[self.perpage*(self.page-1)+self.current]
|
2012-05-19 10:59:38 +00:00
|
|
|
local file_to_del = file_entry.dir .. "/" .. file_entry.name
|
2012-10-02 22:45:45 +00:00
|
|
|
if InfoMessage.InfoMethod[MSG_CONFIRM] == 0 then -- silent regime
|
|
|
|
os.remove(DocToHistory(file_to_del))
|
|
|
|
self:init()
|
|
|
|
self:setSearchResult(self.keywords)
|
|
|
|
else
|
|
|
|
InfoMessage:inform("Press 'Y' to confirm ", nil, 0, MSG_CONFIRM,
|
|
|
|
"Please, press key Y to delete the book history")
|
2012-11-05 20:10:52 +00:00
|
|
|
if ReturnKey() == KEY_Y then
|
2012-10-02 22:45:45 +00:00
|
|
|
os.remove(DocToHistory(file_to_del))
|
|
|
|
self:init()
|
|
|
|
self:setSearchResult(self.keywords)
|
|
|
|
end
|
|
|
|
end
|
2012-08-27 14:14:15 +00:00
|
|
|
self.pagedirty = true
|
2012-10-03 15:13:33 +00:00
|
|
|
if self.items == 0 then
|
|
|
|
return "break"
|
|
|
|
end
|
2012-05-19 10:59:38 +00:00
|
|
|
end
|
|
|
|
)
|
2012-10-05 09:59:37 +00:00
|
|
|
self.commands:add(KEY_SPACE, nil, "Space",
|
2012-05-19 10:59:38 +00:00
|
|
|
"refresh page manually",
|
|
|
|
function(self)
|
|
|
|
self.pagedirty = true
|
|
|
|
end
|
|
|
|
)
|
2012-10-03 15:13:33 +00:00
|
|
|
self.commands:add(KEY_BACK, nil, "Back",
|
2012-09-06 18:04:20 +00:00
|
|
|
"back",
|
2012-05-19 10:59:38 +00:00
|
|
|
function(self)
|
2012-09-06 18:04:20 +00:00
|
|
|
return "break"
|
2012-05-19 10:59:38 +00:00
|
|
|
end
|
2012-09-06 18:04:20 +00:00
|
|
|
)
|
2012-05-19 10:59:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function FileHistory:choose(keywords)
|
|
|
|
self.perpage = math.floor(G_height / self.spacing) - 2
|
|
|
|
self.pagedirty = true
|
|
|
|
self.markerdirty = false
|
|
|
|
|
2012-10-02 22:45:45 +00:00
|
|
|
-- NuPogodi, 30.09.12: immediate quit (no redraw), if empty
|
2012-10-03 15:13:33 +00:00
|
|
|
if self:setSearchResult(keywords) < 1 then
|
2012-10-05 09:59:37 +00:00
|
|
|
InfoMessage:inform("No reading history ", 2000, 1, MSG_WARN, "The reading history is empty!")
|
2012-10-02 22:45:45 +00:00
|
|
|
return nil
|
|
|
|
end
|
2012-05-19 10:59:38 +00:00
|
|
|
|
|
|
|
while true do
|
|
|
|
local cface = Font:getFace("cfont", 22)
|
|
|
|
local tface = Font:getFace("tfont", 25)
|
|
|
|
local fface = Font:getFace("ffont", 16)
|
|
|
|
|
|
|
|
if self.pagedirty then
|
|
|
|
self.markerdirty = true
|
|
|
|
fb.bb:paintRect(0, 0, G_width, G_height, 0)
|
|
|
|
-- draw header
|
2012-10-03 15:13:33 +00:00
|
|
|
local header = "Last Documents ("..tostring(self.items).." items)"
|
2012-05-19 10:59:38 +00:00
|
|
|
if self.keywords ~= "" and self.keywords ~= " " then
|
|
|
|
--header = header .. " (filter: \'" .. string.upper(self.keywords) .. "\')"
|
|
|
|
header = "Search Results for \'"..string.upper(self.keywords).."\'"
|
|
|
|
end
|
2012-10-02 22:45:45 +00:00
|
|
|
DrawTitle(header,self.margin_H,0,self.title_H,3,tface)
|
2012-05-19 10:59:38 +00:00
|
|
|
-- draw found results
|
2012-10-02 22:45:45 +00:00
|
|
|
for c = 1, self.perpage do
|
|
|
|
local i = (self.page - 1) * self.perpage + c
|
|
|
|
if i <= self.items then
|
|
|
|
y = self.title_H + (self.spacing * c) + 4
|
|
|
|
local ftype = string.lower(string.match(self.result[i].name, ".+%.([^.]+)") or "")
|
|
|
|
DrawFileItem(self.result[i].name,self.margin_H,y,ftype)
|
2012-05-19 10:59:38 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
-- draw footer
|
|
|
|
all_page = math.ceil(self.items/self.perpage)
|
|
|
|
DrawFooter("Page "..self.page.." of "..all_page,fface,self.foot_H)
|
|
|
|
end
|
|
|
|
|
|
|
|
if self.markerdirty then
|
|
|
|
if not self.pagedirty then
|
|
|
|
if self.oldcurrent > 0 then
|
|
|
|
y = self.title_H + (self.spacing * self.oldcurrent) + 12
|
|
|
|
fb.bb:paintRect(self.margin_H, y, G_width - 2 * self.margin_H, 3, 0)
|
|
|
|
fb:refresh(1, self.margin_H, y, G_width - 2 * self.margin_H, 3)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- draw new marker line
|
|
|
|
y = self.title_H + (self.spacing * self.current) + 12
|
|
|
|
fb.bb:paintRect(self.margin_H, y, G_width - 2 * self.margin_H, 3, 15)
|
|
|
|
if not self.pagedirty then
|
|
|
|
fb:refresh(1, self.margin_H, y, G_width - 2 * self.margin_H, 3)
|
|
|
|
end
|
|
|
|
self.oldcurrent = self.current
|
|
|
|
self.markerdirty = false
|
|
|
|
end
|
|
|
|
|
|
|
|
if self.pagedirty then
|
|
|
|
fb:refresh(0)
|
|
|
|
self.pagedirty = false
|
|
|
|
end
|
|
|
|
|
|
|
|
local ev = input.saveWaitForEvent()
|
|
|
|
ev.code = adjustKeyEvents(ev)
|
|
|
|
if ev.type == EV_KEY and ev.value ~= EVENT_VALUE_KEY_RELEASE then
|
|
|
|
keydef = Keydef:new(ev.code, getKeyModifier())
|
2012-06-03 22:49:23 +00:00
|
|
|
Debug("key pressed: "..tostring(keydef))
|
2012-05-19 10:59:38 +00:00
|
|
|
|
|
|
|
command = self.commands:getByKeydef(keydef)
|
|
|
|
if command ~= nil then
|
2012-06-03 22:49:23 +00:00
|
|
|
Debug("command to execute: "..tostring(command))
|
2012-05-19 10:59:38 +00:00
|
|
|
ret_code = command.func(self, keydef)
|
|
|
|
else
|
2012-06-03 22:49:23 +00:00
|
|
|
Debug("command not found: "..tostring(command))
|
2012-05-19 10:59:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if ret_code == "break" then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
|
|
|
|
if self.selected_item ~= nil then
|
2012-06-03 22:49:23 +00:00
|
|
|
Debug("# selected "..self.selected_item)
|
2012-05-19 10:59:38 +00:00
|
|
|
return self.selected_item
|
|
|
|
end
|
|
|
|
end -- if
|
|
|
|
end -- while true
|
|
|
|
return nil
|
|
|
|
end
|