/
main.lua
422 lines (404 loc) · 15.2 KB
/
main.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
local ConfirmBox = require("ui/widget/confirmbox")
local DataStorage = require("datastorage")
local Dispatcher = require("dispatcher")
local FFIUtil = require("ffi/util")
local InfoMessage = require("ui/widget/infomessage")
local InputDialog = require("ui/widget/inputdialog")
local LuaSettings = require("luasettings")
local UIManager = require("ui/uimanager")
local WidgetContainer = require("ui/widget/container/widgetcontainer")
local _ = require("gettext")
local T = FFIUtil.template
local util = require("util")
local autostart_done = false
local Profiles = WidgetContainer:extend{
name = "profiles",
prefix = "profile_exec_",
profiles_file = DataStorage:getSettingsDir() .. "/profiles.lua",
profiles = nil,
data = nil,
updated = false,
}
function Profiles:init()
Dispatcher:init()
self.ui.menu:registerToMainMenu(self)
self:onDispatcherRegisterActions()
self:executeAutostart()
end
function Profiles:loadProfiles()
if self.profiles then
return
end
self.profiles = LuaSettings:open(self.profiles_file)
self.data = self.profiles.data
-- ensure profile name
for k, v in pairs(self.data) do
if not v.settings then
self.data[k].settings = {}
end
if not self.data[k].settings.name then
self.data[k].settings.name = k
self.updated = true
end
end
self:onFlushSettings()
end
function Profiles:onFlushSettings()
if self.profiles and self.updated then
self.profiles:flush()
self.updated = false
end
end
local function dispatcherRegisterProfile(name)
Dispatcher:registerAction(Profiles.prefix..name,
{category="none", event="ProfileExecute", arg=name, title=T(_("Profile %1"), name), general=true})
end
local function dispatcherUnregisterProfile(name)
Dispatcher:removeAction(Profiles.prefix..name)
end
function Profiles:onDispatcherRegisterActions()
self:loadProfiles()
for k, v in pairs(self.data) do
if v.settings.registered then
dispatcherRegisterProfile(k)
end
end
end
function Profiles:addToMainMenu(menu_items)
menu_items.profiles = {
text = _("Profiles"),
sub_item_table_func = function()
return self:getSubMenuItems()
end,
}
end
function Profiles:getSubMenuItems()
self:loadProfiles()
local sub_item_table = {
{
text = _("New"),
keep_menu_open = true,
callback = function(touchmenu_instance)
local function editCallback(new_name)
self.data[new_name] = {["settings"] = {["name"] = new_name}}
self.updated = true
touchmenu_instance.item_table = self:getSubMenuItems()
touchmenu_instance.page = 1
touchmenu_instance:updateItems()
end
self:editProfileName(editCallback)
end,
},
{
text = _("New with current document settings"),
enabled = self.ui.file_chooser == nil,
keep_menu_open = true,
callback = function(touchmenu_instance)
local function editCallback(new_name)
self.data[new_name] = self:getProfileFromCurrentDocument(new_name)
self.updated = true
touchmenu_instance.item_table = self:getSubMenuItems()
touchmenu_instance.page = 1
touchmenu_instance:updateItems()
end
self:editProfileName(editCallback)
end,
separator = true,
},
}
for k, v in FFIUtil.orderedPairs(self.data) do
local edit_actions_sub_items = {}
Dispatcher:addSubMenu(self, edit_actions_sub_items, self.data, k)
local sub_items = {
{
text = _("Execute"),
callback = function(touchmenu_instance)
touchmenu_instance:onClose()
self:onProfileExecute(k, { qm_show = false })
end,
},
{
text = _("Show as QuickMenu"),
callback = function(touchmenu_instance)
touchmenu_instance:onClose()
self:onProfileExecute(k, { qm_show = true })
end,
},
{
text = _("Autostart"),
help_text = _("Execute this profile when KOReader is started with 'file browser' or 'last file'."),
checked_func = function()
return G_reader_settings:getSettingForExt("autostart_profiles", k)
end,
callback = function()
local new_value = not G_reader_settings:getSettingForExt("autostart_profiles", k) or nil
G_reader_settings:saveSettingForExt("autostart_profiles", new_value, k)
end,
separator = true,
},
{
text = _("Show in action list"),
checked_func = function()
return v.settings.registered
end,
callback = function(touchmenu_instance)
if v.settings.registered then
dispatcherUnregisterProfile(k)
self:updateProfiles(self.prefix..k)
self.data[k].settings.registered = nil
else
dispatcherRegisterProfile(k)
self.data[k].settings.registered = true
end
self.updated = true
local actions_sub_menu = {}
Dispatcher:addSubMenu(self, actions_sub_menu, self.data, k)
touchmenu_instance.item_table[5].sub_item_table = actions_sub_menu -- "Edit actions" submenu (item #5)
touchmenu_instance.item_table_stack[#touchmenu_instance.item_table_stack] = self:getSubMenuItems()
end,
},
{
text_func = function() return T(_("Edit actions: (%1)"), Dispatcher:menuTextFunc(v)) end,
sub_item_table = edit_actions_sub_items,
separator = true,
},
{
text = T(_("Rename: %1"), k),
keep_menu_open = true,
callback = function(touchmenu_instance)
local function editCallback(new_name)
self.data[new_name] = util.tableDeepCopy(v)
self.data[new_name].settings.name = new_name
self:updateAutostart(k, new_name)
if v.settings.registered then
dispatcherUnregisterProfile(k)
dispatcherRegisterProfile(new_name)
self:updateProfiles(self.prefix..k, self.prefix..new_name)
end
self.data[k] = nil
self.updated = true
touchmenu_instance.item_table = self:getSubMenuItems()
touchmenu_instance:updateItems()
table.remove(touchmenu_instance.item_table_stack)
end
self:editProfileName(editCallback, k)
end,
},
{
text = _("Duplicate"),
keep_menu_open = true,
callback = function(touchmenu_instance)
local function editCallback(new_name)
self.data[new_name] = util.tableDeepCopy(v)
self.data[new_name].settings.name = new_name
if v.settings.registered then
dispatcherRegisterProfile(new_name)
end
self.updated = true
touchmenu_instance.item_table = self:getSubMenuItems()
touchmenu_instance:updateItems()
table.remove(touchmenu_instance.item_table_stack)
end
self:editProfileName(editCallback, k)
end,
},
{
text = _("Delete"),
keep_menu_open = true,
separator = true,
callback = function(touchmenu_instance)
UIManager:show(ConfirmBox:new{
text = _("Do you want to delete this profile?"),
ok_text = _("Delete"),
ok_callback = function()
self:updateAutostart(k)
if v.settings.registered then
dispatcherUnregisterProfile(k)
self:updateProfiles(self.prefix..k)
end
self.data[k] = nil
self.updated = true
touchmenu_instance.item_table = self:getSubMenuItems()
touchmenu_instance:updateItems()
table.remove(touchmenu_instance.item_table_stack)
end,
})
end,
},
}
table.insert(sub_item_table, {
text_func = function()
return (v.settings.show_as_quickmenu and "\u{F0CA} " or "\u{F144} ") .. k
end,
hold_keep_menu_open = false,
sub_item_table = sub_items,
hold_callback = function()
self:onProfileExecute(k)
end,
})
end
return sub_item_table
end
function Profiles:onProfileExecute(name, exec_props)
Dispatcher:execute(self.data[name], exec_props)
end
function Profiles:editProfileName(editCallback, old_name)
local name_input
name_input = InputDialog:new{
title = _("Enter profile name"),
input = old_name,
buttons = {{
{
text = _("Cancel"),
id = "close",
callback = function()
UIManager:close(name_input)
end,
},
{
text = _("Save"),
callback = function()
local new_name = name_input:getInputText()
if new_name == "" or new_name == old_name then return end
UIManager:close(name_input)
if self.data[new_name] then
UIManager:show(InfoMessage:new{
text = T(_("Profile already exists: %1"), new_name),
})
else
editCallback(new_name)
end
end,
},
}},
}
UIManager:show(name_input)
name_input:onShowKeyboard()
end
function Profiles:getProfileFromCurrentDocument(new_name)
local document_settings
if self.ui.rolling then
document_settings = {
"rotation_mode",
"set_font",
"font_size",
"font_gamma",
"font_base_weight",
"font_hinting",
"font_kerning",
"word_spacing",
"word_expansion",
"visible_pages",
"h_page_margins",
"sync_t_b_page_margins",
"t_page_margin",
"b_page_margin",
"view_mode",
"block_rendering_mode",
"render_dpi",
"line_spacing",
"embedded_css",
"embedded_fonts",
"smooth_scaling",
"nightmode_images",
"status_line",
}
else
document_settings = {
"rotation_mode",
"kopt_text_wrap",
"kopt_trim_page",
"kopt_page_margin",
"kopt_zoom_overlap_h",
"kopt_zoom_overlap_v",
"kopt_max_columns",
"kopt_zoom_mode_genus",
"kopt_zoom_mode_type",
"kopt_zoom_factor",
"kopt_zoom_direction",
"kopt_page_scroll",
"kopt_line_spacing",
"kopt_font_size",
"kopt_contrast",
"kopt_quality",
}
end
local setting_needs_arg = {
["sync_t_b_page_margins"] = true,
["view_mode"] = true,
["embedded_css"] = true,
["embedded_fonts"] = true,
["smooth_scaling"] = true,
["nightmode_images"] = true,
["kopt_trim_page"] = true,
["kopt_zoom_mode_genus"] = true,
["kopt_zoom_mode_type"] = true,
["kopt_page_scroll"] = true,
}
local profile = { settings = { name = new_name, order = document_settings } }
for _, v in ipairs(document_settings) do
-- document configurable settings do not have prefixes
local value = self.document.configurable[v:gsub("^kopt_", "")]
if setting_needs_arg[v] then
value = Dispatcher:getArgFromValue(v, value)
end
profile[v] = value
end
if self.ui.rolling then
profile["set_font"] = self.ui.font.font_face -- not in configurable settings
end
return profile
end
function Profiles:updateProfiles(action_old_name, action_new_name)
for _, profile in pairs(self.data) do
if profile[action_old_name] then
if profile.settings and profile.settings.order then
for i, action in ipairs(profile.settings.order) do
if action == action_old_name then
if action_new_name then
profile.settings.order[i] = action_new_name
else
table.remove(profile.settings.order, i)
if #profile.settings.order == 0 then
profile.settings.order = nil
end
end
break
end
end
end
profile[action_old_name] = nil
if action_new_name then
profile[action_new_name] = true
end
self.updated = true
end
end
if self.ui.gestures then -- search and update the profile action in assigned gestures
self.ui.gestures:updateProfiles(action_old_name, action_new_name)
end
end
function Profiles:updateAutostart(old_name, new_name)
if G_reader_settings:getSettingForExt("autostart_profiles", old_name) then
G_reader_settings:saveSettingForExt("autostart_profiles", nil, old_name)
if new_name then
G_reader_settings:saveSettingForExt("autostart_profiles", true, new_name)
end
end
end
function Profiles:executeAutostart()
if autostart_done then return end
self:loadProfiles()
local autostart_table = 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
self:updateAutostart(autostart_profile_name) -- remove deleted profile from autostart_profile
end
end
autostart_done = true
end
return Profiles