2015-03-09 12:20:32 +00:00
|
|
|
local InputContainer = require("ui/widget/container/inputcontainer")
|
|
|
|
local LoginDialog = require("ui/widget/logindialog")
|
|
|
|
local InfoMessage = require("ui/widget/infomessage")
|
|
|
|
local ConfirmBox = require("ui/widget/confirmbox")
|
|
|
|
local DocSettings = require("docsettings")
|
|
|
|
local NetworkMgr = require("ui/networkmgr")
|
|
|
|
local UIManager = require("ui/uimanager")
|
|
|
|
local Screen = require("device").screen
|
|
|
|
local Device = require("device")
|
|
|
|
local Event = require("ui/event")
|
2015-03-10 07:09:42 +00:00
|
|
|
local Math = require("optmath")
|
2015-03-09 12:20:32 +00:00
|
|
|
local DEBUG = require("dbg")
|
|
|
|
local T = require("ffi/util").template
|
|
|
|
local _ = require("gettext")
|
|
|
|
local md5 = require("MD5")
|
|
|
|
|
2015-03-10 07:49:33 +00:00
|
|
|
local l10n = {
|
|
|
|
_("Unknown server error."),
|
|
|
|
_("Unauthorized"),
|
|
|
|
_("Username is already registered."),
|
|
|
|
}
|
|
|
|
|
2015-03-09 12:20:32 +00:00
|
|
|
local KOSync = InputContainer:new{
|
|
|
|
name = "kosync",
|
2015-03-10 07:49:33 +00:00
|
|
|
title = _("Register/login to Koreader server"),
|
2015-03-09 12:20:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function KOSync:init()
|
|
|
|
local settings = G_reader_settings:readSetting("kosync") or {}
|
2015-09-17 15:27:19 +00:00
|
|
|
self.kosync_custom_server = settings.custom_server
|
2015-03-10 07:49:33 +00:00
|
|
|
self.kosync_username = settings.username
|
2015-03-09 12:20:32 +00:00
|
|
|
self.kosync_userkey = settings.userkey
|
2015-03-11 04:23:23 +00:00
|
|
|
self.kosync_auto_sync = not (settings.auto_sync == false)
|
2015-03-09 12:20:32 +00:00
|
|
|
self.ui:registerPostInitCallback(function()
|
2015-03-10 07:09:42 +00:00
|
|
|
if self.kosync_auto_sync then
|
|
|
|
UIManager:scheduleIn(1, function() self:getProgress() end)
|
|
|
|
end
|
2015-03-09 12:20:32 +00:00
|
|
|
end)
|
|
|
|
self.ui.menu:registerToMainMenu(self)
|
|
|
|
end
|
|
|
|
|
|
|
|
function KOSync:addToMainMenu(tab_item_table)
|
|
|
|
table.insert(tab_item_table.plugins, {
|
2015-03-10 07:12:44 +00:00
|
|
|
text = _("Progress sync"),
|
2015-03-09 12:20:32 +00:00
|
|
|
sub_item_table = {
|
|
|
|
{
|
|
|
|
text_func = function()
|
|
|
|
return self.kosync_userkey and (_("Logout"))
|
|
|
|
or _("Register") .. " / " .. _("Login")
|
|
|
|
end,
|
|
|
|
callback_func = function()
|
|
|
|
return self.kosync_userkey and
|
|
|
|
function() self:logout() end or
|
|
|
|
function() self:login() end
|
|
|
|
end,
|
|
|
|
},
|
2015-03-10 07:09:42 +00:00
|
|
|
{
|
2015-03-10 07:49:33 +00:00
|
|
|
text = _("Auto sync"),
|
2015-03-10 07:09:42 +00:00
|
|
|
checked_func = function() return self.kosync_auto_sync end,
|
|
|
|
callback = function()
|
|
|
|
self.kosync_auto_sync = not self.kosync_auto_sync
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = _("Sync now"),
|
|
|
|
enabled_func = function()
|
|
|
|
return self.kosync_userkey ~= nil
|
|
|
|
end,
|
|
|
|
callback = function()
|
|
|
|
self:updateProgress()
|
|
|
|
self:getProgress(true)
|
|
|
|
end,
|
2015-09-17 15:27:19 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
text = _("Custom sync server"),
|
|
|
|
tap_input = {
|
|
|
|
title = _("Custom progress sync server address"),
|
|
|
|
input = self.kosync_custom_server or "https://",
|
|
|
|
type = "text",
|
|
|
|
callback = function(input)
|
|
|
|
self:setCustomServer(input)
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
},
|
2015-03-09 12:20:32 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2015-09-17 15:27:19 +00:00
|
|
|
function KOSync:setCustomServer(server)
|
|
|
|
DEBUG("set custom server", server)
|
|
|
|
self.kosync_custom_server = server ~= "" and server or nil
|
|
|
|
self:onSaveSettings()
|
|
|
|
end
|
|
|
|
|
2015-03-09 12:20:32 +00:00
|
|
|
function KOSync:login()
|
|
|
|
if NetworkMgr:getWifiStatus() == false then
|
|
|
|
NetworkMgr:promptWifiOn()
|
|
|
|
end
|
|
|
|
self.login_dialog = LoginDialog:new{
|
2015-03-10 07:49:33 +00:00
|
|
|
title = self.title,
|
2015-03-09 12:20:32 +00:00
|
|
|
username = self.kosync_username or "",
|
|
|
|
buttons = {
|
|
|
|
{
|
|
|
|
{
|
|
|
|
text = _("Cancel"),
|
|
|
|
enabled = true,
|
|
|
|
callback = function()
|
|
|
|
self:closeDialog()
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = _("Login"),
|
|
|
|
enabled = true,
|
|
|
|
callback = function()
|
|
|
|
local username, password = self:getCredential()
|
|
|
|
self:closeDialog()
|
|
|
|
UIManager:scheduleIn(0.5, function()
|
|
|
|
self:doLogin(username, password)
|
|
|
|
end)
|
|
|
|
|
|
|
|
UIManager:show(InfoMessage:new{
|
|
|
|
text = _("Logging in. Please wait..."),
|
|
|
|
timeout = 1,
|
|
|
|
})
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = _("Register"),
|
2015-03-10 07:49:33 +00:00
|
|
|
enabled = true,
|
2015-03-09 12:20:32 +00:00
|
|
|
callback = function()
|
|
|
|
local username, password = self:getCredential()
|
|
|
|
self:closeDialog()
|
|
|
|
UIManager:scheduleIn(0.5, function()
|
|
|
|
self:doRegister(username, password)
|
|
|
|
end)
|
|
|
|
|
|
|
|
UIManager:show(InfoMessage:new{
|
|
|
|
text = _("Registering. Please wait..."),
|
|
|
|
timeout = 1,
|
|
|
|
})
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
width = Screen:getWidth() * 0.8,
|
|
|
|
height = Screen:getHeight() * 0.4,
|
|
|
|
}
|
|
|
|
|
|
|
|
self.login_dialog:onShowKeyboard()
|
|
|
|
UIManager:show(self.login_dialog)
|
|
|
|
end
|
|
|
|
|
|
|
|
function KOSync:closeDialog()
|
|
|
|
self.login_dialog:onClose()
|
|
|
|
UIManager:close(self.login_dialog)
|
|
|
|
end
|
|
|
|
|
|
|
|
function KOSync:getCredential()
|
|
|
|
return self.login_dialog:getCredential()
|
|
|
|
end
|
|
|
|
|
|
|
|
function KOSync:doRegister(username, password)
|
|
|
|
local KOSyncClient = require("KOSyncClient")
|
|
|
|
local client = KOSyncClient:new{
|
2015-09-17 15:27:19 +00:00
|
|
|
custom_url = self.kosync_custom_server,
|
2015-03-09 12:20:32 +00:00
|
|
|
service_spec = self.path .. "/api.json"
|
|
|
|
}
|
|
|
|
local userkey = md5:sum(password)
|
|
|
|
local ok, status, body = pcall(client.register, client, username, userkey)
|
|
|
|
if not ok and status then
|
|
|
|
UIManager:show(InfoMessage:new{
|
|
|
|
text = _("An error occurred while registering:") ..
|
|
|
|
"\n" .. status,
|
|
|
|
})
|
|
|
|
elseif ok then
|
|
|
|
if status then
|
|
|
|
self.kosync_username = username
|
|
|
|
self.kosync_userkey = userkey
|
|
|
|
UIManager:show(InfoMessage:new{
|
|
|
|
text = _("Registered to Koreader server successfully."),
|
|
|
|
})
|
|
|
|
else
|
|
|
|
UIManager:show(InfoMessage:new{
|
2015-03-21 05:18:34 +00:00
|
|
|
text = _(body and body.message or "Unknown server error"),
|
2015-03-09 12:20:32 +00:00
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
self:onSaveSettings()
|
|
|
|
end
|
|
|
|
|
|
|
|
function KOSync:doLogin(username, password)
|
|
|
|
local KOSyncClient = require("KOSyncClient")
|
|
|
|
local client = KOSyncClient:new{
|
2015-09-17 15:27:19 +00:00
|
|
|
custom_url = self.kosync_custom_server,
|
2015-03-09 12:20:32 +00:00
|
|
|
service_spec = self.path .. "/api.json"
|
|
|
|
}
|
|
|
|
local userkey = md5:sum(password)
|
|
|
|
local ok, status, body = pcall(client.authorize, client, username, userkey)
|
|
|
|
if not ok and status then
|
|
|
|
UIManager:show(InfoMessage:new{
|
|
|
|
text = _("An error occurred while logging in:") ..
|
|
|
|
"\n" .. status,
|
|
|
|
})
|
|
|
|
elseif ok then
|
|
|
|
if status then
|
|
|
|
self.kosync_username = username
|
|
|
|
self.kosync_userkey = userkey
|
|
|
|
UIManager:show(InfoMessage:new{
|
|
|
|
text = _("Logged in to Koreader server successfully."),
|
|
|
|
})
|
|
|
|
else
|
|
|
|
UIManager:show(InfoMessage:new{
|
2015-03-21 05:18:34 +00:00
|
|
|
text = _(body and body.message or "Unknown server error"),
|
2015-03-09 12:20:32 +00:00
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
self:onSaveSettings()
|
|
|
|
end
|
|
|
|
|
|
|
|
function KOSync:logout()
|
|
|
|
self.kosync_userkey = nil
|
2015-03-10 07:09:42 +00:00
|
|
|
self.kosync_auto_sync = true
|
2015-03-09 12:20:32 +00:00
|
|
|
self:onSaveSettings()
|
|
|
|
end
|
|
|
|
|
|
|
|
function KOSync:getLastPercent()
|
|
|
|
if self.ui.document.info.has_pages then
|
|
|
|
return self.ui.paging:getLastPercent()
|
|
|
|
else
|
|
|
|
return self.ui.rolling:getLastPercent()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function KOSync:getLastProgress()
|
|
|
|
if self.ui.document.info.has_pages then
|
|
|
|
return self.ui.paging:getLastProgress()
|
|
|
|
else
|
|
|
|
return self.ui.rolling:getLastProgress()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function KOSync:syncToProgress(progress)
|
|
|
|
DEBUG("sync to", progress)
|
|
|
|
if self.ui.document.info.has_pages then
|
|
|
|
self.ui:handleEvent(Event:new("GotoPage", tonumber(progress)))
|
|
|
|
else
|
|
|
|
self.ui:handleEvent(Event:new("GotoXPointer", progress))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function KOSync:updateProgress()
|
|
|
|
if self.kosync_username and self.kosync_userkey then
|
|
|
|
local KOSyncClient = require("KOSyncClient")
|
|
|
|
local client = KOSyncClient:new{
|
2015-09-17 15:27:19 +00:00
|
|
|
custom_url = self.kosync_custom_server,
|
2015-03-09 12:20:32 +00:00
|
|
|
service_spec = self.path .. "/api.json"
|
|
|
|
}
|
|
|
|
local doc_digest = self.view.document:fastDigest()
|
|
|
|
local progress = self:getLastProgress()
|
|
|
|
local percentage = self:getLastPercent()
|
|
|
|
local ok, err = pcall(client.update_progress, client,
|
|
|
|
self.kosync_username, self.kosync_userkey,
|
|
|
|
doc_digest, progress, percentage, Device.model,
|
|
|
|
function(ok, body)
|
|
|
|
DEBUG("update progress for", self.view.document.file, ok)
|
|
|
|
end)
|
|
|
|
if not ok and err then
|
|
|
|
DEBUG("err:", err)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-03-10 07:09:42 +00:00
|
|
|
function KOSync:getProgress(manual)
|
2015-03-09 12:20:32 +00:00
|
|
|
if self.kosync_username and self.kosync_userkey then
|
|
|
|
local KOSyncClient = require("KOSyncClient")
|
|
|
|
local client = KOSyncClient:new{
|
2015-09-17 15:27:19 +00:00
|
|
|
custom_url = self.kosync_custom_server,
|
2015-03-09 12:20:32 +00:00
|
|
|
service_spec = self.path .. "/api.json"
|
|
|
|
}
|
|
|
|
local doc_digest = self.view.document:fastDigest()
|
|
|
|
local ok, err = pcall(client.get_progress, client,
|
|
|
|
self.kosync_username, self.kosync_userkey,
|
|
|
|
doc_digest, function(ok, body)
|
|
|
|
DEBUG("get progress for", self.view.document.file, ok, body)
|
|
|
|
if body and body.percentage then
|
2015-03-10 07:09:42 +00:00
|
|
|
local progress = self:getLastProgress()
|
2015-03-09 12:20:32 +00:00
|
|
|
local percentage = self:getLastPercent()
|
|
|
|
DEBUG("current progress", percentage)
|
2015-03-16 13:47:10 +00:00
|
|
|
if body.percentage > percentage
|
|
|
|
and tostring(body.progress) ~= tostring(progress) then
|
2015-03-09 12:20:32 +00:00
|
|
|
UIManager:show(ConfirmBox:new{
|
2015-03-10 07:09:42 +00:00
|
|
|
text = T(_("Sync to furthest location %1% from device '%2'?"),
|
|
|
|
Math.round(body.percentage*100), body.device),
|
2015-03-09 12:20:32 +00:00
|
|
|
ok_callback = function()
|
|
|
|
self:syncToProgress(body.progress)
|
|
|
|
end,
|
|
|
|
})
|
2015-03-10 07:09:42 +00:00
|
|
|
elseif manual and body.progress == progress then
|
|
|
|
UIManager:show(InfoMessage:new{
|
|
|
|
text = _("We are already synchronized."),
|
2015-03-10 07:49:33 +00:00
|
|
|
timeout = 3,
|
2015-03-10 07:09:42 +00:00
|
|
|
})
|
2015-03-09 12:20:32 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
if not ok and err then
|
|
|
|
DEBUG("err:", err)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function KOSync:onSaveSettings()
|
|
|
|
local settings = {
|
2015-09-17 15:27:19 +00:00
|
|
|
custom_server = self.kosync_custom_server,
|
2015-03-09 12:20:32 +00:00
|
|
|
username = self.kosync_username,
|
|
|
|
userkey = self.kosync_userkey,
|
2015-03-10 07:09:42 +00:00
|
|
|
auto_sync = self.kosync_auto_sync,
|
2015-03-09 12:20:32 +00:00
|
|
|
}
|
|
|
|
G_reader_settings:saveSetting("kosync", settings)
|
|
|
|
end
|
|
|
|
|
|
|
|
function KOSync:onCloseDocument()
|
|
|
|
DEBUG("on close document")
|
2015-03-10 07:09:42 +00:00
|
|
|
if self.kosync_auto_sync then
|
|
|
|
self:updateProgress()
|
|
|
|
end
|
2015-03-09 12:20:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return KOSync
|