From 5bbcb9ea790836acf241e5db9ea8871ed1c01563 Mon Sep 17 00:00:00 2001 From: zwim <36999612+zwim@users.noreply.github.com> Date: Sun, 15 Aug 2021 13:25:19 +0200 Subject: [PATCH] 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. --- plugins/profiles.koplugin/main.lua | 55 +++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/plugins/profiles.koplugin/main.lua b/plugins/profiles.koplugin/main.lua index 13eea1cf2..67e242b55 100644 --- a/plugins/profiles.koplugin/main.lua +++ b/plugins/profiles.koplugin/main.lua @@ -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