|
|
@ -6,7 +6,7 @@ local DocSettings = require("docsettings")
|
|
|
|
local NetworkMgr = require("ui/network/manager")
|
|
|
|
local NetworkMgr = require("ui/network/manager")
|
|
|
|
local UIManager = require("ui/uimanager")
|
|
|
|
local UIManager = require("ui/uimanager")
|
|
|
|
local Screen = require("device").screen
|
|
|
|
local Screen = require("device").screen
|
|
|
|
local Device = require("device")
|
|
|
|
local DeviceModel = require("device").model
|
|
|
|
local Event = require("ui/event")
|
|
|
|
local Event = require("ui/event")
|
|
|
|
local Math = require("optmath")
|
|
|
|
local Math = require("optmath")
|
|
|
|
local DEBUG = require("dbg")
|
|
|
|
local DEBUG = require("dbg")
|
|
|
@ -14,12 +14,6 @@ local T = require("ffi/util").template
|
|
|
|
local _ = require("gettext")
|
|
|
|
local _ = require("gettext")
|
|
|
|
local md5 = require("ffi/MD5")
|
|
|
|
local md5 = require("ffi/MD5")
|
|
|
|
|
|
|
|
|
|
|
|
local l10n = {
|
|
|
|
|
|
|
|
_("Unknown server error."),
|
|
|
|
|
|
|
|
_("Unauthorized"),
|
|
|
|
|
|
|
|
_("Username is already registered."),
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local KOSync = InputContainer:new{
|
|
|
|
local KOSync = InputContainer:new{
|
|
|
|
name = "kosync",
|
|
|
|
name = "kosync",
|
|
|
|
title = _("Register/login to KOReader server"),
|
|
|
|
title = _("Register/login to KOReader server"),
|
|
|
@ -31,12 +25,18 @@ function KOSync:init()
|
|
|
|
self.kosync_username = settings.username
|
|
|
|
self.kosync_username = settings.username
|
|
|
|
self.kosync_userkey = settings.userkey
|
|
|
|
self.kosync_userkey = settings.userkey
|
|
|
|
self.kosync_auto_sync = not (settings.auto_sync == false)
|
|
|
|
self.kosync_auto_sync = not (settings.auto_sync == false)
|
|
|
|
|
|
|
|
self.kosync_device_id = G_reader_settings:readSetting("device_id")
|
|
|
|
|
|
|
|
assert(self.kosync_device_id)
|
|
|
|
self.ui:registerPostInitCallback(function()
|
|
|
|
self.ui:registerPostInitCallback(function()
|
|
|
|
if self.kosync_auto_sync then
|
|
|
|
if self.kosync_auto_sync then
|
|
|
|
UIManager:scheduleIn(1, function() self:getProgress() end)
|
|
|
|
UIManager:scheduleIn(1, function() self:getProgress() end)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
self.ui.menu:registerToMainMenu(self)
|
|
|
|
self.ui.menu:registerToMainMenu(self)
|
|
|
|
|
|
|
|
-- Make sure checksum has been calculated at the very first time a document has been opened, to
|
|
|
|
|
|
|
|
-- avoid document saving feature to impact the checksum, and eventually impact the document
|
|
|
|
|
|
|
|
-- identity in the progress sync feature.
|
|
|
|
|
|
|
|
self.view.document:fastDigest()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function KOSync:addToMainMenu(tab_item_table)
|
|
|
|
function KOSync:addToMainMenu(tab_item_table)
|
|
|
@ -55,19 +55,36 @@ function KOSync:addToMainMenu(tab_item_table)
|
|
|
|
end,
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{
|
|
|
|
text = _("Auto sync"),
|
|
|
|
text = _("Auto sync now and future"),
|
|
|
|
checked_func = function() return self.kosync_auto_sync end,
|
|
|
|
checked_func = function() return self.kosync_auto_sync end,
|
|
|
|
callback = function()
|
|
|
|
callback = function()
|
|
|
|
self.kosync_auto_sync = not self.kosync_auto_sync
|
|
|
|
self.kosync_auto_sync = not self.kosync_auto_sync
|
|
|
|
|
|
|
|
if self.kosync_auto_sync then
|
|
|
|
|
|
|
|
-- since we will update the progress when closing document, we should pull
|
|
|
|
|
|
|
|
-- current progress now to avoid to overwrite it silently.
|
|
|
|
|
|
|
|
self:getProgress(true)
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
-- since we won't update the progress when closing document, we should push
|
|
|
|
|
|
|
|
-- current progress now to avoid to lose it silently.
|
|
|
|
|
|
|
|
self:updateProgress(true)
|
|
|
|
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{
|
|
|
|
text = _("Sync now"),
|
|
|
|
text = _("Push progress from this device"),
|
|
|
|
|
|
|
|
enabled_func = function()
|
|
|
|
|
|
|
|
return self.kosync_userkey ~= nil
|
|
|
|
|
|
|
|
end,
|
|
|
|
|
|
|
|
callback = function()
|
|
|
|
|
|
|
|
self:updateProgress(true)
|
|
|
|
|
|
|
|
end,
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
text = _("Pull progress from other devices"),
|
|
|
|
enabled_func = function()
|
|
|
|
enabled_func = function()
|
|
|
|
return self.kosync_userkey ~= nil
|
|
|
|
return self.kosync_userkey ~= nil
|
|
|
|
end,
|
|
|
|
end,
|
|
|
|
callback = function()
|
|
|
|
callback = function()
|
|
|
|
self:updateProgress()
|
|
|
|
|
|
|
|
self:getProgress(true)
|
|
|
|
self:getProgress(true)
|
|
|
|
end,
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
},
|
|
|
@ -225,11 +242,15 @@ function KOSync:logout()
|
|
|
|
self:onSaveSettings()
|
|
|
|
self:onSaveSettings()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local function roundPercent(percent)
|
|
|
|
|
|
|
|
return math.floor(percent * 10000) / 10000
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function KOSync:getLastPercent()
|
|
|
|
function KOSync:getLastPercent()
|
|
|
|
if self.ui.document.info.has_pages then
|
|
|
|
if self.ui.document.info.has_pages then
|
|
|
|
return self.ui.paging:getLastPercent()
|
|
|
|
return roundPercent(self.ui.paging:getLastPercent())
|
|
|
|
else
|
|
|
|
else
|
|
|
|
return self.ui.rolling:getLastPercent()
|
|
|
|
return roundPercent(self.ui.rolling:getLastPercent())
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
@ -250,7 +271,21 @@ function KOSync:syncToProgress(progress)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function KOSync:updateProgress()
|
|
|
|
local function promptLogin()
|
|
|
|
|
|
|
|
UIManager:show(InfoMessage:new{
|
|
|
|
|
|
|
|
text = _("Please register / login before using progress synchronization feature."),
|
|
|
|
|
|
|
|
timeout = 3,
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local function showSyncError()
|
|
|
|
|
|
|
|
UIManager:show(InfoMessage:new{
|
|
|
|
|
|
|
|
text = _("Something went wrong when syncing progress, please check your network connection and try again later."),
|
|
|
|
|
|
|
|
timeout = 3,
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function KOSync:updateProgress(manual)
|
|
|
|
if self.kosync_username and self.kosync_userkey then
|
|
|
|
if self.kosync_username and self.kosync_userkey then
|
|
|
|
local KOSyncClient = require("KOSyncClient")
|
|
|
|
local KOSyncClient = require("KOSyncClient")
|
|
|
|
local client = KOSyncClient:new{
|
|
|
|
local client = KOSyncClient:new{
|
|
|
@ -260,15 +295,34 @@ function KOSync:updateProgress()
|
|
|
|
local doc_digest = self.view.document:fastDigest()
|
|
|
|
local doc_digest = self.view.document:fastDigest()
|
|
|
|
local progress = self:getLastProgress()
|
|
|
|
local progress = self:getLastProgress()
|
|
|
|
local percentage = self:getLastPercent()
|
|
|
|
local percentage = self:getLastPercent()
|
|
|
|
local ok, err = pcall(client.update_progress, client,
|
|
|
|
local ok, err = pcall(client.update_progress,
|
|
|
|
self.kosync_username, self.kosync_userkey,
|
|
|
|
client,
|
|
|
|
doc_digest, progress, percentage, Device.model,
|
|
|
|
self.kosync_username,
|
|
|
|
|
|
|
|
self.kosync_userkey,
|
|
|
|
|
|
|
|
doc_digest,
|
|
|
|
|
|
|
|
progress,
|
|
|
|
|
|
|
|
percentage,
|
|
|
|
|
|
|
|
DeviceModel,
|
|
|
|
|
|
|
|
self.kosync_device_id,
|
|
|
|
function(ok, body)
|
|
|
|
function(ok, body)
|
|
|
|
DEBUG("update progress for", self.view.document.file, ok)
|
|
|
|
DEBUG("update progress for", self.view.document.file, ok)
|
|
|
|
|
|
|
|
if manual then
|
|
|
|
|
|
|
|
if ok then
|
|
|
|
|
|
|
|
UIManager:show(InfoMessage:new{
|
|
|
|
|
|
|
|
text = _("Progress has been pushed."),
|
|
|
|
|
|
|
|
timeout = 3,
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
showSyncError()
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
if not ok and err then
|
|
|
|
if not ok then
|
|
|
|
DEBUG("err:", err)
|
|
|
|
if manual then showSyncError() end
|
|
|
|
|
|
|
|
if err then DEBUG("err:", err) end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
elseif manual then
|
|
|
|
|
|
|
|
promptLogin()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
@ -280,34 +334,57 @@ function KOSync:getProgress(manual)
|
|
|
|
service_spec = self.path .. "/api.json"
|
|
|
|
service_spec = self.path .. "/api.json"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
local doc_digest = self.view.document:fastDigest()
|
|
|
|
local doc_digest = self.view.document:fastDigest()
|
|
|
|
local ok, err = pcall(client.get_progress, client,
|
|
|
|
local ok, err = pcall(client.get_progress,
|
|
|
|
self.kosync_username, self.kosync_userkey,
|
|
|
|
client,
|
|
|
|
doc_digest, function(ok, body)
|
|
|
|
self.kosync_username,
|
|
|
|
|
|
|
|
self.kosync_userkey,
|
|
|
|
|
|
|
|
doc_digest,
|
|
|
|
|
|
|
|
function(ok, body)
|
|
|
|
DEBUG("get progress for", self.view.document.file, ok, body)
|
|
|
|
DEBUG("get progress for", self.view.document.file, ok, body)
|
|
|
|
if body and body.percentage then
|
|
|
|
if body then
|
|
|
|
local progress = self:getLastProgress()
|
|
|
|
if body.percentage then
|
|
|
|
local percentage = self:getLastPercent()
|
|
|
|
if body.device ~= DeviceModel
|
|
|
|
DEBUG("current progress", percentage)
|
|
|
|
or body.device_id ~= self.kosync_device_id then
|
|
|
|
if body.percentage > percentage
|
|
|
|
body.percentage = roundPercent(body.percentage)
|
|
|
|
and tostring(body.progress) ~= tostring(progress) then
|
|
|
|
local progress = self:getLastProgress()
|
|
|
|
UIManager:show(ConfirmBox:new{
|
|
|
|
local percentage = self:getLastPercent()
|
|
|
|
text = T(_("Sync to furthest location %1% from device '%2'?"),
|
|
|
|
DEBUG("current progress", percentage)
|
|
|
|
Math.round(body.percentage*100), body.device),
|
|
|
|
if body.percentage > percentage and body.progress ~= progress then
|
|
|
|
ok_callback = function()
|
|
|
|
UIManager:show(ConfirmBox:new{
|
|
|
|
self:syncToProgress(body.progress)
|
|
|
|
text = T(_("Sync to furthest location %1% from device '%2'?"),
|
|
|
|
end,
|
|
|
|
Math.round(body.percentage*100), body.device),
|
|
|
|
})
|
|
|
|
ok_callback = function()
|
|
|
|
elseif manual and body.progress == progress then
|
|
|
|
self:syncToProgress(body.progress)
|
|
|
|
|
|
|
|
end,
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
elseif manual then
|
|
|
|
|
|
|
|
UIManager:show(InfoMessage:new{
|
|
|
|
|
|
|
|
text = _("Already synchronized."),
|
|
|
|
|
|
|
|
timeout = 3,
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
elseif manual then
|
|
|
|
|
|
|
|
UIManager:show(InfoMessage:new{
|
|
|
|
|
|
|
|
text = _("Latest progress is coming from this device."),
|
|
|
|
|
|
|
|
timeout = 3,
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
elseif manual then
|
|
|
|
UIManager:show(InfoMessage:new{
|
|
|
|
UIManager:show(InfoMessage:new{
|
|
|
|
text = _("Already synchronized."),
|
|
|
|
text = _("No progress found for this document."),
|
|
|
|
timeout = 3,
|
|
|
|
timeout = 3,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
elseif manual then
|
|
|
|
|
|
|
|
showSyncError()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
if not ok and err then
|
|
|
|
if not ok then
|
|
|
|
DEBUG("err:", err)
|
|
|
|
if manual then showSyncError() end
|
|
|
|
|
|
|
|
if err then DEBUG("err:", err) end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
elseif manual then
|
|
|
|
|
|
|
|
promptLogin()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|