mirror of
https://github.com/koreader/koreader.git
synced 2025-08-10 00:52:38 +00:00
Autostart Profiles (#8049)
Add the possibility to autostart profiles when KOReader is started with filemanager or lastfile. Discussed in #8020 If more profiles are selected, they are started in alphabetically order.
This commit is contained in:
@@ -10,6 +10,8 @@ local WidgetContainer = require("ui/widget/container/widgetcontainer")
|
||||
local _ = require("gettext")
|
||||
local T = FFIUtil.template
|
||||
|
||||
local autostart_done = false
|
||||
|
||||
local Profiles = WidgetContainer:new{
|
||||
name = "profiles",
|
||||
profiles_file = DataStorage:getSettingsDir() .. "/profiles.lua",
|
||||
@@ -21,6 +23,7 @@ local Profiles = WidgetContainer:new{
|
||||
function Profiles:init()
|
||||
Dispatcher:init()
|
||||
self.ui.menu:registerToMainMenu(self)
|
||||
self:executeAutostart()
|
||||
end
|
||||
|
||||
function Profiles:loadProfiles()
|
||||
@@ -105,7 +108,22 @@ function Profiles:getSubMenuItems()
|
||||
end,
|
||||
})
|
||||
end,
|
||||
}
|
||||
},
|
||||
{
|
||||
text = _("Autostart"),
|
||||
help_text = _("Execute this profile when KOReader is started with 'file browser' or 'last file'."),
|
||||
checked_func = function()
|
||||
return self:isAutostartProfile(k)
|
||||
end,
|
||||
separator = true,
|
||||
callback = function()
|
||||
if self:isAutostartProfile(k) then
|
||||
self:deleteAutostartProfile(k)
|
||||
else
|
||||
self:setAutostartProfile(k)
|
||||
end
|
||||
end,
|
||||
},
|
||||
}
|
||||
Dispatcher:addSubMenu(self, sub_items, self.data, k)
|
||||
table.insert(sub_item_table, {
|
||||
@@ -133,6 +151,41 @@ end
|
||||
function Profiles:deleteProfile(name)
|
||||
self.data[name] = nil
|
||||
self.updated = true
|
||||
self:deleteAutostartProfile(name)
|
||||
end
|
||||
|
||||
function Profiles:isAutostartProfile(name)
|
||||
return G_reader_settings:has("autostart_profiles") and G_reader_settings:readSetting("autostart_profiles")[name] == true
|
||||
end
|
||||
|
||||
function Profiles:setAutostartProfile(name)
|
||||
local autostart_table = G_reader_settings:has("autostart_profiles") and G_reader_settings:readSetting("autostart_profiles") or {}
|
||||
autostart_table[name] = true
|
||||
G_reader_settings:saveSetting("autostart_profiles", autostart_table)
|
||||
end
|
||||
|
||||
function Profiles:deleteAutostartProfile(name)
|
||||
local autostart_table = G_reader_settings:has("autostart_profiles") and G_reader_settings:readSetting("autostart_profiles") or {}
|
||||
autostart_table[name] = nil
|
||||
G_reader_settings:saveSetting("autostart_profiles", autostart_table)
|
||||
end
|
||||
|
||||
function Profiles:executeAutostart()
|
||||
if not autostart_done then
|
||||
self:loadProfiles()
|
||||
local autostart_table = G_reader_settings:has("autostart_profiles") and G_reader_settings:readSetting("autostart_profiles") or {}
|
||||
for autostart_profile_name, profile_enabled in pairs(autostart_table) do
|
||||
if self.data[autostart_profile_name] and profile_enabled then
|
||||
UIManager:nextTick(function()
|
||||
Dispatcher:execute(self.data[autostart_profile_name])
|
||||
end)
|
||||
else
|
||||
autostart_table[autostart_profile_name] = nil -- remove deleted profile form autostart_profile
|
||||
G_reader_settings:saveSetting("autostart_profiles", autostart_table)
|
||||
end
|
||||
end
|
||||
autostart_done = true
|
||||
end
|
||||
end
|
||||
|
||||
return Profiles
|
||||
|
||||
Reference in New Issue
Block a user