2014-01-15 15:36:47 +00:00
|
|
|
local InputContainer = require("ui/widget/container/inputcontainer")
|
|
|
|
local GestureRange = require("ui/gesturerange")
|
2014-05-02 08:50:43 +00:00
|
|
|
local LinkBox = require("ui/widget/linkbox")
|
|
|
|
local UIManager = require("ui/uimanager")
|
2014-01-15 15:36:47 +00:00
|
|
|
local Geom = require("ui/geometry")
|
2014-10-30 18:42:18 +00:00
|
|
|
local Screen = require("device").screen
|
|
|
|
local Device = require("device")
|
2017-01-15 20:55:06 +00:00
|
|
|
local logger = require("logger")
|
2014-01-15 15:36:47 +00:00
|
|
|
local Event = require("ui/event")
|
2014-08-17 12:29:38 +00:00
|
|
|
local _ = require("gettext")
|
2017-01-15 20:55:06 +00:00
|
|
|
local T = require("ffi/util").template
|
2014-01-15 15:36:47 +00:00
|
|
|
|
2014-01-18 07:04:24 +00:00
|
|
|
local ReaderLink = InputContainer:new{
|
2016-03-27 22:39:47 +00:00
|
|
|
location_stack = {}
|
2014-01-18 07:04:24 +00:00
|
|
|
}
|
2014-01-15 15:36:47 +00:00
|
|
|
|
|
|
|
function ReaderLink:init()
|
2014-03-13 13:52:43 +00:00
|
|
|
if Device:isTouchDevice() then
|
|
|
|
self:initGesListener()
|
|
|
|
end
|
2014-10-09 09:41:23 +00:00
|
|
|
self.ui:registerPostInitCallback(function()
|
|
|
|
self.ui.menu:registerToMainMenu(self)
|
|
|
|
end)
|
2014-01-15 15:36:47 +00:00
|
|
|
end
|
|
|
|
|
2014-01-22 11:59:53 +00:00
|
|
|
function ReaderLink:onReadSettings(config)
|
2014-03-13 13:52:43 +00:00
|
|
|
-- called when loading new document
|
2016-03-27 22:39:47 +00:00
|
|
|
self.location_stack = {}
|
2014-01-22 11:59:53 +00:00
|
|
|
end
|
|
|
|
|
2014-01-15 15:36:47 +00:00
|
|
|
function ReaderLink:initGesListener()
|
2014-03-13 13:52:43 +00:00
|
|
|
if Device:isTouchDevice() then
|
|
|
|
self.ges_events = {
|
|
|
|
Tap = {
|
|
|
|
GestureRange:new{
|
|
|
|
ges = "tap",
|
|
|
|
range = Geom:new{
|
|
|
|
x = 0, y = 0,
|
|
|
|
w = Screen:getWidth(),
|
|
|
|
h = Screen:getHeight()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Swipe = {
|
|
|
|
GestureRange:new{
|
|
|
|
ges = "swipe",
|
|
|
|
range = Geom:new{
|
|
|
|
x = 0, y = 0,
|
|
|
|
w = Screen:getWidth(),
|
|
|
|
h = Screen:getHeight(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
end
|
2014-01-15 15:36:47 +00:00
|
|
|
end
|
|
|
|
|
2016-12-03 13:02:04 +00:00
|
|
|
local function isFollowLinksOn()
|
2015-04-01 10:11:14 +00:00
|
|
|
return G_reader_settings:readSetting("follow_links") ~= false
|
|
|
|
end
|
|
|
|
|
2016-12-03 13:02:04 +00:00
|
|
|
local function isSwipeToGoBackEnabled()
|
2015-10-03 16:20:41 +00:00
|
|
|
return G_reader_settings:readSetting("swipe_to_go_back") == true
|
2015-04-01 10:11:14 +00:00
|
|
|
end
|
|
|
|
|
2016-12-03 13:02:04 +00:00
|
|
|
local function isSwipeToFollowFirstLinkEnabled()
|
|
|
|
return G_reader_settings:readSetting("swipe_to_follow_first_link") == true
|
|
|
|
end
|
|
|
|
|
2014-08-17 12:29:38 +00:00
|
|
|
function ReaderLink:addToMainMenu(tab_item_table)
|
|
|
|
-- insert table to main reader menu
|
|
|
|
table.insert(tab_item_table.navi, {
|
|
|
|
text = _("Follow links"),
|
2015-04-01 10:11:14 +00:00
|
|
|
sub_item_table = {
|
|
|
|
{
|
|
|
|
text_func = function()
|
2016-12-03 13:02:04 +00:00
|
|
|
return isFollowLinksOn() and _("Disable") or _("Enable")
|
2015-04-01 10:11:14 +00:00
|
|
|
end,
|
|
|
|
callback = function()
|
|
|
|
G_reader_settings:saveSetting("follow_links",
|
2016-12-03 13:02:04 +00:00
|
|
|
not isFollowLinksOn())
|
2015-04-01 10:11:14 +00:00
|
|
|
end
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = _("Go back"),
|
2016-03-27 22:39:47 +00:00
|
|
|
enabled_func = function() return #self.location_stack > 0 end,
|
2015-04-01 10:11:14 +00:00
|
|
|
callback = function() self:onGoBackLink() end,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = _("Swipe to go back"),
|
2016-12-03 13:02:04 +00:00
|
|
|
checked_func = isSwipeToGoBackEnabled,
|
2015-04-01 10:11:14 +00:00
|
|
|
callback = function()
|
|
|
|
G_reader_settings:saveSetting("swipe_to_go_back",
|
2016-12-03 13:02:04 +00:00
|
|
|
not isSwipeToGoBackEnabled())
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = _("Swipe to follow first link"),
|
|
|
|
checked_func = isSwipeToFollowFirstLinkEnabled,
|
|
|
|
callback = function()
|
|
|
|
G_reader_settings:saveSetting("swipe_to_follow_first_link",
|
|
|
|
not isSwipeToFollowFirstLinkEnabled())
|
2015-04-01 10:11:14 +00:00
|
|
|
end,
|
|
|
|
},
|
|
|
|
}
|
2014-08-17 12:29:38 +00:00
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2014-01-15 15:36:47 +00:00
|
|
|
function ReaderLink:onSetDimensions(dimen)
|
2014-03-13 13:52:43 +00:00
|
|
|
-- update listening according to new screen dimen
|
|
|
|
if Device:isTouchDevice() then
|
|
|
|
self:initGesListener()
|
|
|
|
end
|
2014-01-15 15:36:47 +00:00
|
|
|
end
|
|
|
|
|
2016-03-27 22:39:47 +00:00
|
|
|
function ReaderLink:onTap(_, ges)
|
2016-12-03 13:02:04 +00:00
|
|
|
if not isFollowLinksOn() then return end
|
2014-03-13 13:52:43 +00:00
|
|
|
if self.ui.document.info.has_pages then
|
|
|
|
local pos = self.view:screenToPageTransform(ges.pos)
|
|
|
|
if pos then
|
2014-05-11 12:19:00 +00:00
|
|
|
-- link box in native page
|
2014-05-02 08:50:43 +00:00
|
|
|
local link, lbox = self.ui.document:getLinkFromPosition(pos.page, pos)
|
|
|
|
if link and lbox then
|
|
|
|
-- screen box that holds the link
|
2014-05-11 12:19:00 +00:00
|
|
|
local sbox = self.view:pageToScreenTransform(pos.page,
|
|
|
|
self.ui.document:nativeToPageRectTransform(pos.page, lbox))
|
2014-05-02 08:50:43 +00:00
|
|
|
if sbox then
|
|
|
|
UIManager:show(LinkBox:new{
|
|
|
|
box = sbox,
|
2014-12-03 04:55:53 +00:00
|
|
|
timeout = FOLLOW_LINK_TIMEOUT,
|
2014-05-02 08:50:43 +00:00
|
|
|
callback = function() self:onGotoLink(link) end
|
|
|
|
})
|
|
|
|
return true
|
|
|
|
end
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
local link = self.ui.document:getLinkFromPosition(ges.pos)
|
|
|
|
if link ~= "" then
|
|
|
|
return self:onGotoLink(link)
|
|
|
|
end
|
|
|
|
end
|
2014-01-18 07:04:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function ReaderLink:onGotoLink(link)
|
2017-01-15 20:55:06 +00:00
|
|
|
logger.dbg("onGotoLink:", link)
|
2014-03-13 13:52:43 +00:00
|
|
|
if self.ui.document.info.has_pages then
|
2017-01-15 20:55:06 +00:00
|
|
|
-- internal pdf links have a "page" attribute, while external ones have an "uri" attribute
|
|
|
|
if link.page then -- Internal link
|
|
|
|
logger.dbg("Internal link:", link)
|
|
|
|
table.insert(self.location_stack, self.ui.paging:getBookLocation())
|
|
|
|
self.ui:handleEvent(Event:new("GotoPage", link.page + 1))
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
link = link.uri -- external link
|
|
|
|
else
|
|
|
|
-- For crengine, internal links may look like :
|
|
|
|
-- #_doc_fragment_0_Organisation (link from anchor)
|
|
|
|
-- /body/DocFragment/body/ul[2]/li[5]/text()[3].16 (xpointer from full-text search)
|
|
|
|
-- If the XPointer does not exist (or is a full url), we will jump to page 1
|
|
|
|
-- Best to check that this link exists in document with the following,
|
|
|
|
-- which accepts both of the above legitimate xpointer as input.
|
|
|
|
if self.ui.document:isXPointerInDocument(link) then
|
|
|
|
logger.dbg("Internal link:", link)
|
|
|
|
table.insert(self.location_stack, self.ui.rolling:getBookLocation())
|
|
|
|
self.ui:handleEvent(Event:new("GotoXPointer", link))
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
logger.dbg("External link:", link)
|
|
|
|
-- Check if it is a wikipedia link
|
|
|
|
local wiki_lang, wiki_page = link:match([[https?://([^%.]+).wikipedia.org/wiki/([^/]+)]])
|
|
|
|
if wiki_lang and wiki_page then
|
|
|
|
logger.dbg("Wikipedia link:", wiki_lang, wiki_page)
|
|
|
|
-- Ask for user confirmation before launching lookup (on a
|
|
|
|
-- wikipedia page saved as epub, full of wikipedia links, it's
|
|
|
|
-- too easy to click on links when wanting to change page...)
|
|
|
|
local ConfirmBox = require("ui/widget/confirmbox")
|
|
|
|
UIManager:show(ConfirmBox:new{
|
|
|
|
text = T(_("Would you like to read this Wikipedia %1 full page?\n\n%2\n"), wiki_lang:upper(), wiki_page:gsub("_", " ")),
|
|
|
|
cancel_on_tap_outside = true,
|
|
|
|
ok_callback = function()
|
|
|
|
UIManager:nextTick(function()
|
|
|
|
self.ui:handleEvent(Event:new("LookupWikipedia", wiki_page, false, true, wiki_lang))
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
})
|
2014-03-13 13:52:43 +00:00
|
|
|
else
|
2017-01-15 20:55:06 +00:00
|
|
|
-- local Notification = require("ui/widget/notification")
|
|
|
|
local InfoMessage = require("ui/widget/infomessage")
|
|
|
|
UIManager:show(InfoMessage:new{
|
|
|
|
text = T(_("Invalid or external link:\n%1"), link),
|
|
|
|
timeout = 1.0,
|
|
|
|
})
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2017-01-15 20:55:06 +00:00
|
|
|
-- don't propagate, user will notice and tap elsewhere if he wants to change page
|
2014-03-13 13:52:43 +00:00
|
|
|
return true
|
2014-01-18 07:04:24 +00:00
|
|
|
end
|
|
|
|
|
2015-04-01 10:11:14 +00:00
|
|
|
function ReaderLink:onGoBackLink()
|
2016-03-27 22:39:47 +00:00
|
|
|
local saved_location = table.remove(self.location_stack)
|
|
|
|
if saved_location then
|
|
|
|
self.ui:handleEvent(Event:new('RestoreBookLocation', saved_location))
|
2016-08-12 01:23:23 +00:00
|
|
|
return true
|
2015-04-01 10:11:14 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-27 22:39:47 +00:00
|
|
|
function ReaderLink:onSwipe(_, ges)
|
2016-12-03 13:02:04 +00:00
|
|
|
if ges.direction == "east" then
|
|
|
|
if isSwipeToGoBackEnabled() then
|
|
|
|
return self:onGoBackLink()
|
|
|
|
end
|
|
|
|
elseif ges.direction == "west" then
|
|
|
|
if isSwipeToFollowFirstLinkEnabled() then
|
|
|
|
return self:onGoToFirstLink(ges)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function ReaderLink:onGoToFirstLink(ges)
|
|
|
|
if not isFollowLinksOn() then return end
|
|
|
|
local firstlink = nil
|
|
|
|
if self.ui.document.info.has_pages then
|
|
|
|
local pos = self.view:screenToPageTransform(ges.pos)
|
|
|
|
if not pos then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local links = self.ui.document:getPageLinks(pos.page)
|
|
|
|
if #links == 0 then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
-- DEBUG("PDF Page links : ", links)
|
|
|
|
-- We may get multiple links: internal ones (with "page" key)
|
|
|
|
-- that we're interested in, but also external links (no "page", but
|
|
|
|
-- a "uri" key) that we don't care about.
|
|
|
|
-- [2] = {
|
|
|
|
-- ["y1"] = 107.88977050781,
|
|
|
|
-- ["x1"] = 176.60360717773,
|
|
|
|
-- ["y0"] = 97.944396972656,
|
|
|
|
-- ["x0"] = 97,
|
|
|
|
-- ["page"] = 347
|
|
|
|
-- },
|
|
|
|
-- Links may not be in the order they are in the page, so let's
|
|
|
|
-- find the one with the smallest y0.
|
|
|
|
local first_y0 = nil
|
|
|
|
for _, link in ipairs(links) do
|
|
|
|
if link["page"] then
|
|
|
|
if first_y0 == nil or link["y0"] < first_y0 then
|
|
|
|
-- onGotoLink()'s GotoPage event needs the link
|
|
|
|
-- itself, and will use its "page" value
|
|
|
|
firstlink = link
|
|
|
|
first_y0 = link["y0"]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
local links = self.ui.document:getPageLinks()
|
|
|
|
if #links == 0 then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
-- DEBUG("CRE Page links : ", links)
|
|
|
|
-- We may get multiple links: internal ones (they have a "section" key)
|
|
|
|
-- that we're interested in, but also external links (no "section", but
|
|
|
|
-- a "uri" key) that we don't care about.
|
|
|
|
-- [1] = {
|
|
|
|
-- ["end_x"] = 825,
|
|
|
|
-- ["uri"] = "",
|
|
|
|
-- ["end_y"] = 333511,
|
|
|
|
-- ["start_x"] = 90,
|
|
|
|
-- ["start_y"] = 333511
|
|
|
|
-- },
|
|
|
|
-- [2] = {
|
|
|
|
-- ["end_x"] = 366,
|
|
|
|
-- ["section"] = "#_doc_fragment_19_ftn_fn6",
|
|
|
|
-- ["end_y"] = 1201,
|
|
|
|
-- ["start_x"] = 352,
|
|
|
|
-- ["start_y"] = 1201
|
|
|
|
-- },
|
|
|
|
-- links may not be in the order they are in the page, so let's
|
|
|
|
-- find the one with the smallest start_y.
|
|
|
|
local first_start_y = nil
|
|
|
|
for _, link in ipairs(links) do
|
|
|
|
if link["section"] then
|
|
|
|
if first_start_y == nil or link["start_y"] < first_start_y then
|
|
|
|
-- onGotoLink()'s GotoXPointer event needs
|
|
|
|
-- the "section" value
|
|
|
|
firstlink = link["section"]
|
|
|
|
first_start_y = link["start_y"]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- cre.cpp getPageLinks() does highlight found links :
|
|
|
|
-- sel.add( new ldomXRange(*links[i]) ); // highlight
|
|
|
|
-- and we'll find them highlighted when back from link.
|
|
|
|
-- So let's clear them now.
|
|
|
|
self.ui.document:clearSelection()
|
|
|
|
end
|
|
|
|
if firstlink then
|
|
|
|
return self:onGotoLink(firstlink)
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2014-01-15 15:36:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return ReaderLink
|