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

254 lines
5.4 KiB
Lua
Raw Normal View History

#!./koreader-base
2012-06-11 16:37:58 +00:00
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-04-07 08:13:08 +00:00
require "gettext"
2013-02-02 19:41:35 +00:00
HomeMenu = InputContainer:new{
item_table = {},
2013-03-12 15:28:55 +00:00
key_events = {
2013-04-07 08:13:08 +00:00
TapShowMenu = { {_("Home")}, doc = _("Show Home Menu")},
2013-03-12 15:28:55 +00:00
},
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
if Device:isTouchDevice() and Device.survive_screen_saver then
-- hack the swipe to unlock screen
local dev = Device:getTouchInputDev()
if dev then
local width, height = Screen:getWidth(), Screen:getHeight()
2013-04-07 08:13:08 +00:00
input.fakeTapInput(dev,
math.min(width, height)/2,
math.max(width, height)-30
)
end
end
end
2013-04-07 08:13:08 +00:00
os.exit(0)
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, {
2013-04-07 08:13:08 +00:00
text = _("Last documents"),
2013-03-12 04:51:00 +00:00
sub_item_table = hist_sub_item_table,
})
2013-02-02 19:41:35 +00:00
table.insert(self.item_table, {
2013-04-07 08:13:08 +00:00
text = _("Exit"),
2013-02-02 19:41:35 +00:00
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
2013-03-16 18:17:42 +00:00
local menu_container = CenterContainer:new{
ignore = "height",
dimen = Screen:getSize(),
}
2013-02-02 19:41:35 +00:00
local home_menu = Menu:new{
2013-03-16 18:17:42 +00:00
show_parent = menu_container,
2013-04-07 08:13:08 +00:00
title = _("Home menu"),
2013-02-02 19:41:35 +00:00
item_table = self.item_table,
width = Screen:getWidth() - 100,
}
2013-03-16 18:17:42 +00:00
menu_container[1] = 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
2013-04-07 08:13:08 +00:00
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$"}
2013-03-16 18:17:42 +00:00
local HomePage = InputContainer:new{
}
local FileManager = FileChooser:new{
2013-03-16 18:17:42 +00:00
show_parent = HomePage,
2013-04-07 08:13:08 +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-03-16 18:17:42 +00:00
table.insert(HomePage, FileManager)
table.insert(HomePage, HomeMenu)
2013-02-02 19:41:35 +00:00
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()
2013-04-07 08:13:08 +00:00
print(_("usage: ./reader.lua [OPTION] ... path"))
print(_("Read all the books on your E-Ink reader"))
2012-06-11 16:37:58 +00:00
print("")
2013-04-07 08:13:08 +00:00
print(_("-d start in debug mode"))
print(_("-h show this usage help"))
2012-06-11 16:37:58 +00:00
print("")
2013-04-07 08:13:08 +00:00
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"))
2012-06-11 16:37:58 +00:00
print("")
2013-04-07 08:13:08 +00:00
print(_("If you don't pass any path, the last viewed document will be opened"))
2012-06-11 16:37:58 +00:00
print("")
2013-04-07 08:13:08 +00:00
print(_("This software is licensed under the GPLv3."))
print(_("See http://github.com/koreader/kindlepdfviewer for more info."))
2012-06-11 16:37:58 +00:00
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()