Add option to change checksum method in KOSync to be Binary or Filename based (#7840)

closes #7836
reviewable/pr7876/r1
Brian Hughes 3 years ago committed by GitHub
parent 61d24879d3
commit 458191993d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -12,6 +12,7 @@ local Screen = Device.screen
local logger = require("logger") local logger = require("logger")
local md5 = require("ffi/sha2").md5 local md5 = require("ffi/sha2").md5
local random = require("random") local random = require("random")
local util = require("util")
local T = require("ffi/util").template local T = require("ffi/util").template
local _ = require("gettext") local _ = require("gettext")
@ -40,6 +41,11 @@ local SYNC_STRATEGY = {
DEFAULT_BACKWARD = 3, DEFAULT_BACKWARD = 3,
} }
local CHECKSUM_METHOD = {
BINARY = 0,
FILENAME = 1
}
local function getNameStrategy(type) local function getNameStrategy(type)
if type == 1 then if type == 1 then
return _("Prompt") return _("Prompt")
@ -113,6 +119,7 @@ function KOSync:onReaderReady()
self.kosync_pages_before_update = settings.pages_before_update self.kosync_pages_before_update = settings.pages_before_update
self.kosync_whisper_forward = settings.whisper_forward or SYNC_STRATEGY.DEFAULT_FORWARD self.kosync_whisper_forward = settings.whisper_forward or SYNC_STRATEGY.DEFAULT_FORWARD
self.kosync_whisper_backward = settings.whisper_backward or SYNC_STRATEGY.DEFAULT_BACKWARD self.kosync_whisper_backward = settings.whisper_backward or SYNC_STRATEGY.DEFAULT_BACKWARD
self.kosync_checksum_method = settings.checksum_method or CHECKSUM_METHOD.BINARY
self.kosync_device_id = G_reader_settings:readSetting("device_id") self.kosync_device_id = G_reader_settings:readSetting("device_id")
--assert(self.kosync_device_id) --assert(self.kosync_device_id)
if self.kosync_auto_sync then if self.kosync_auto_sync then
@ -300,6 +307,29 @@ If set to 0, updating progress based on page turns will be disabled.]]),
UIManager:show(items) UIManager:show(items)
end, end,
}, },
{
text = _("Document matching method"),
sub_item_table = {
{
text = _("Binary. Only identical files will sync progress."),
checked_func = function()
return self.kosync_checksum_method == CHECKSUM_METHOD.BINARY
end,
callback = function()
self:setChecksumMethod(CHECKSUM_METHOD.BINARY)
end,
},
{
text = _("Filename. Files with the same name will sync progress."),
checked_func = function()
return self.kosync_checksum_method == CHECKSUM_METHOD.FILENAME
end,
callback = function()
self:setChecksumMethod(CHECKSUM_METHOD.FILENAME)
end,
},
}
},
} }
} }
end end
@ -325,6 +355,11 @@ function KOSync:setWhisperBackward(strategy)
self:saveSettings() self:saveSettings()
end end
function KOSync:setChecksumMethod(method)
self.kosync_checksum_method = method
self:saveSettings()
end
function KOSync:login() function KOSync:login()
if NetworkMgr:willRerunWhenOnline(function() self:login() end) then if NetworkMgr:willRerunWhenOnline(function() self:login() end) then
return return
@ -507,6 +542,28 @@ function KOSync:getLastProgress()
end end
end end
function KOSync:getDocumentDigest()
if self.kosync_checksum_method == CHECKSUM_METHOD.FILENAME then
return self:getFileNameDigest()
else
return self:getFileDigest()
end
end
function KOSync:getFileDigest()
return self.view.document:fastDigest()
end
function KOSync:getFileNameDigest()
local file = self.ui.document.file
if not file then return end
local file_path, file_name = util.splitFilePathName(file) -- luacheck: no unused
if not file_name then return end
return md5(file_name)
end
function KOSync:syncToProgress(progress) function KOSync:syncToProgress(progress)
logger.dbg("sync to", progress) logger.dbg("sync to", progress)
if self.ui.document.info.has_pages then if self.ui.document.info.has_pages then
@ -533,7 +590,7 @@ function KOSync:updateProgress(manual)
custom_url = self.kosync_custom_server, custom_url = self.kosync_custom_server,
service_spec = self.path .. "/api.json" service_spec = self.path .. "/api.json"
} }
local doc_digest = self.view.document:fastDigest() local doc_digest = self:getDocumentDigest()
local progress = self:getLastProgress() local progress = self:getLastProgress()
local percentage = self:getLastPercent() local percentage = self:getLastPercent()
local ok, err = pcall(client.update_progress, local ok, err = pcall(client.update_progress,
@ -581,7 +638,7 @@ function KOSync:getProgress(manual)
custom_url = self.kosync_custom_server, custom_url = self.kosync_custom_server,
service_spec = self.path .. "/api.json" service_spec = self.path .. "/api.json"
} }
local doc_digest = self.view.document:fastDigest() local doc_digest = self:getDocumentDigest()
local ok, err = pcall(client.get_progress, local ok, err = pcall(client.get_progress,
client, client,
self.kosync_username, self.kosync_username,
@ -701,6 +758,7 @@ function KOSync:saveSettings()
(self.kosync_whisper_backward ~= SYNC_STRATEGY.DEFAULT_BACKWARD (self.kosync_whisper_backward ~= SYNC_STRATEGY.DEFAULT_BACKWARD
and self.kosync_whisper_backward and self.kosync_whisper_backward
or nil), or nil),
checksum_method = self.kosync_checksum_method,
} }
G_reader_settings:saveSetting("kosync", settings) G_reader_settings:saveSetting("kosync", settings)
end end

Loading…
Cancel
Save