mirror of
https://github.com/koreader/koreader.git
synced 2025-08-10 00:52:38 +00:00
Add API to set OS level file associations (#6615)
This commit is contained in:
@@ -61,6 +61,7 @@ local Device = {
|
||||
canSuspend = yes,
|
||||
canReboot = no,
|
||||
canPowerOff = no,
|
||||
canAssociateFileExtensions = no,
|
||||
|
||||
-- use these only as a last resort. We should abstract the functionality
|
||||
-- and have device dependent implementations in the corresponting
|
||||
@@ -311,6 +312,11 @@ end
|
||||
-- and only if allowed state is true at the time of waitForEvents() invocation.
|
||||
function Device:setAutoStandby(isAllowed) end
|
||||
|
||||
-- Hardware specific method to set OS-level file associations to launch koreader. Expects boolean map.
|
||||
function Device:associateFileExtensions(exts)
|
||||
logger.dbg("Device:associateFileExtensions():", util.tableSize(exts), "entries, OS handler missing")
|
||||
end
|
||||
|
||||
-- Hardware specific method to handle usb plug in event
|
||||
function Device:usbPlugIn() end
|
||||
|
||||
|
||||
@@ -148,6 +148,18 @@ function DocumentRegistry:getProviders(file)
|
||||
end
|
||||
end
|
||||
|
||||
--- Get mapping of file extensions to providers
|
||||
-- @treturn table mapping file extensions to a list of providers
|
||||
function DocumentRegistry:getExtensions()
|
||||
local t = {}
|
||||
for _, provider in ipairs(self.providers) do
|
||||
local ext = provider.extension
|
||||
t[ext] = t[ext] or {}
|
||||
table.insert(t[ext], provider)
|
||||
end
|
||||
return t
|
||||
end
|
||||
|
||||
--- Sets the preferred registered document handler.
|
||||
-- @string file
|
||||
-- @bool all
|
||||
|
||||
@@ -48,6 +48,14 @@ if Device:canToggleMassStorage() then
|
||||
common_settings.mass_storage_actions = MassStorage:getActionsMenuTable()
|
||||
end
|
||||
|
||||
-- Associate OS level file extensions (must be off by default, because we're not associated initially)
|
||||
if Device:canAssociateFileExtensions() then
|
||||
common_settings.file_ext_assoc = {
|
||||
text = _("Associate file extensions"),
|
||||
sub_item_table = require("ui/elements/file_ext_assoc"):getSettingsMenuTable()
|
||||
}
|
||||
end
|
||||
|
||||
-- This affects the topmenu, we want to be able to access it even if !Device:setDateTime()
|
||||
common_settings.time = {
|
||||
text = _("Time and date"),
|
||||
|
||||
71
frontend/ui/elements/file_ext_assoc.lua
Normal file
71
frontend/ui/elements/file_ext_assoc.lua
Normal file
@@ -0,0 +1,71 @@
|
||||
local Device = require("device")
|
||||
local DocumentRegistry = require("document/documentregistry")
|
||||
local _ = require("gettext")
|
||||
|
||||
local ExtAssoc = {
|
||||
assoc = G_reader_settings:readSetting("file_ext_assoc") or {},
|
||||
}
|
||||
|
||||
function ExtAssoc:commit()
|
||||
G_reader_settings:saveSetting("file_ext_assoc", self.assoc):flush()
|
||||
-- Translate the boolean map back to map of providers the OS backend can inquire further
|
||||
local t = {}
|
||||
for k, v in pairs(DocumentRegistry:getExtensions()) do
|
||||
if self.assoc[k] then t[k] = v end
|
||||
end
|
||||
Device:associateFileExtensions(t)
|
||||
end
|
||||
|
||||
function ExtAssoc:setAll(state)
|
||||
for k, dummy in pairs(DocumentRegistry:getExtensions()) do
|
||||
self:setOne(k, state)
|
||||
end
|
||||
self:commit()
|
||||
end
|
||||
|
||||
function ExtAssoc:setOne(ext, state)
|
||||
self.assoc[ext] = state and true or nil
|
||||
end
|
||||
|
||||
function ExtAssoc:getSettingsMenuTable()
|
||||
local ret = {
|
||||
{
|
||||
keep_menu_open = true,
|
||||
text = _("Enable all"),
|
||||
callback = function(menu)
|
||||
self:setAll(true)
|
||||
menu:updateItems()
|
||||
end,
|
||||
},
|
||||
{
|
||||
keep_menu_open = true,
|
||||
text = _("Disable all"),
|
||||
callback = function(menu)
|
||||
self:setAll(false)
|
||||
menu:updateItems()
|
||||
end,
|
||||
separator = true,
|
||||
},
|
||||
}
|
||||
local exts = DocumentRegistry:getExtensions()
|
||||
local keys = {}
|
||||
for k, dummy in pairs(exts) do
|
||||
table.insert(keys, k)
|
||||
end
|
||||
table.sort(keys)
|
||||
for dummy, k in ipairs(keys) do
|
||||
table.insert(ret, {
|
||||
keep_menu_open = true,
|
||||
text = k,
|
||||
checked_func = function() return self.assoc[k] end,
|
||||
callback = function()
|
||||
self:setOne(k, not self.assoc[k])
|
||||
self:commit()
|
||||
end
|
||||
})
|
||||
end
|
||||
return ret
|
||||
end
|
||||
|
||||
return ExtAssoc
|
||||
|
||||
@@ -47,6 +47,7 @@ local order = {
|
||||
"ignore_sleepcover",
|
||||
"ignore_open_sleepcover",
|
||||
"mass_storage_settings",
|
||||
"file_ext_assoc",
|
||||
"screenshot",
|
||||
},
|
||||
navigation = {
|
||||
|
||||
@@ -67,6 +67,7 @@ local order = {
|
||||
"ignore_sleepcover",
|
||||
"ignore_open_sleepcover",
|
||||
"mass_storage_settings",
|
||||
"file_ext_assoc",
|
||||
"screenshot",
|
||||
},
|
||||
navigation = {
|
||||
|
||||
Reference in New Issue
Block a user