2
0
mirror of https://github.com/koreader/koreader synced 2024-10-31 21:20:20 +00:00
koreader/reader.lua

232 lines
4.8 KiB
Lua
Raw Normal View History

2012-06-11 16:37:58 +00:00
#!./kpdfview
package.path = "./frontend/?.lua"
2013-03-12 17:18:53 +00:00
require "ui/uimanager"
require "ui/widget/filechooser"
require "ui/widget/infomessage"
require "ui/readerui"
require "document/document"
2013-03-12 17:18:53 +00:00
require "settings"
require "dbg"
2013-02-02 19:41:35 +00:00
HomeMenu = InputContainer:new{
item_table = {},
2013-03-12 15:28:55 +00:00
key_events = {
TapShowMenu = { {"Home"}, doc = "Show Home Menu"},
},
2013-02-02 19:41:35 +00:00
ges_events = {
TapShowMenu = {
GestureRange:new{
ges = "tap",
range = Geom:new{
x = 0, y = 0,
w = Screen:getWidth(),
h = 25,
}
}
},
},
}
function exitReader()
G_reader_settings:close()
input.closeAll()
if util.isEmulated() ==0 then
if Device:isKindle3() or (Device:getModel() == "KindleDXG") then
-- send double menu key press events to trigger screen refresh
os.execute("echo 'send 139' > /proc/keypad;echo 'send 139' > /proc/keypad")
end
end
end
2013-02-02 19:41:35 +00:00
function HomeMenu:setUpdateItemTable()
2013-03-12 04:51:00 +00:00
function readHistDir(order_arg, re)
local pipe_out = io.popen("ls "..order_arg.." -1 ./history")
for f in pipe_out:lines() do
table.insert(re, {
dir = DocSettings:getPathFromHistory(f),
name = DocSettings:getNameFromHistory(f),
})
end
end
local hist_sub_item_table = {}
local last_files = {}
readHistDir("-c", last_files)
for _,v in pairs(last_files) do
table.insert(hist_sub_item_table, {
text = v.name,
callback = function()
showReader(v.dir .. "/" .. v.name)
end
})
end
table.insert(self.item_table, {
text = "Last documents",
sub_item_table = hist_sub_item_table,
})
2013-02-02 19:41:35 +00:00
table.insert(self.item_table, {
text = "Exit",
callback = function()
exitReader()
2013-02-02 19:41:35 +00:00
end
})
end
function HomeMenu:onTapShowMenu()
2013-03-12 04:51:00 +00:00
self.item_table = {}
self:setUpdateItemTable()
2013-02-02 19:41:35 +00:00
local home_menu = Menu:new{
title = "Home menu",
item_table = self.item_table,
width = Screen:getWidth() - 100,
}
local menu_container = CenterContainer:new{
ignore = "height",
dimen = Screen:getSize(),
home_menu,
}
2013-03-12 17:18:53 +00:00
home_menu.close_callback = function ()
2013-02-02 19:41:35 +00:00
UIManager:close(menu_container)
end
UIManager:show(menu_container)
return true
end
2012-06-11 16:37:58 +00:00
function showReader(file, pass)
local document = DocumentRegistry:openDocument(file)
if not document then
UIManager:show(InfoMessage:new{ text = "No reader engine for this file" })
return
end
G_reader_settings:saveSetting("lastfile", file)
local reader = ReaderUI:new{
dialog = readerwindow,
dimen = Screen:getSize(),
2012-06-11 16:37:58 +00:00
document = document,
password = pass
}
UIManager:show(reader)
end
2013-02-02 19:41:35 +00:00
function showHomePage(path)
local exclude_dirs = {"%.sdr$"}
local FileManager = FileChooser:new{
2013-02-02 19:41:35 +00:00
title = "FileManager",
path = path,
2013-02-02 19:41:35 +00:00
width = Screen:getWidth(),
height = Screen:getHeight(),
is_borderless = true,
2013-02-02 19:41:35 +00:00
has_close_button = false,
dir_filter = function(dirname)
for _, pattern in ipairs(exclude_dirs) do
if dirname:match(pattern) then return end
end
return true
end,
2013-03-12 17:18:53 +00:00
file_filter = function(filename)
if DocumentRegistry:getProvider(filename) then
return true
end
end
}
2013-02-02 19:41:35 +00:00
local HomePage = InputContainer:new{
FileManager,
HomeMenu,
}
function FileManager:onFileSelect(file)
showReader(file)
return true
end
function FileManager:onClose()
2013-02-02 19:41:35 +00:00
--UIManager:quit()
return true
end
2013-02-02 19:41:35 +00:00
UIManager:show(HomePage)
end
2012-06-11 16:37:58 +00:00
-- option parsing:
longopts = {
debug = "d",
help = "h",
2012-06-11 16:37:58 +00:00
}
function showusage()
print("usage: ./reader.lua [OPTION] ... path")
print("Read all the books on your E-Ink reader")
print("")
2012-11-01 02:02:53 +00:00
print("-d start in debug mode")
print("-h show this usage help")
2012-06-11 16:37:58 +00:00
print("")
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 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
2012-11-01 02:02:53 +00:00
if ARGV[1] == "-h" then
2012-06-11 16:37:58 +00:00
return showusage()
end
2012-11-01 02:02:53 +00:00
local argidx = 1
if ARGV[1] == "-d" then
2013-03-12 17:18:53 +00:00
Dbg:turnOn()
argidx = argidx + 1
2012-11-01 02:02:53 +00:00
else
2012-06-11 16:37:58 +00:00
DEBUG = function() end
end
if Device:hasNoKeyboard() then
2012-06-27 08:26:06 +00:00
-- remove menu item shortcut for K4
Menu.is_enable_shortcut = false
end
2012-06-12 04:10:44 +00:00
-- set up reader's setting: font
G_reader_settings = DocSettings:open(".reader")
fontmap = G_reader_settings:readSetting("fontmap")
if fontmap ~= nil then
Font.fontmap = fontmap
end
local last_file = G_reader_settings:readSetting("lastfile")
2012-06-11 16:37:58 +00:00
--@TODO we can read version here, refer to commit in master tree: (houqp)
--87712cf0e43fed624f8a9f610be42b1fe174b9fe
2012-06-11 16:37:58 +00:00
if ARGV[argidx] and ARGV[argidx] ~= "" then
2012-11-01 02:02:53 +00:00
if lfs.attributes(ARGV[argidx], "mode") == "directory" then
2013-02-02 19:41:35 +00:00
showHomePage(ARGV[argidx])
2012-11-01 02:02:53 +00:00
elseif lfs.attributes(ARGV[argidx], "mode") == "file" then
2012-11-04 03:20:28 +00:00
showReader(ARGV[argidx])
2012-06-11 16:37:58 +00:00
end
UIManager:run()
elseif last_file and lfs.attributes(last_file, "mode") == "file" then
2012-11-04 03:20:28 +00:00
showReader(last_file)
UIManager:run()
else
return showusage()
end
2013-03-16 02:02:08 +00:00
exitReader()