forked from vapormusic/wlnreader.koplugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
217 lines (204 loc) · 7.54 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
-- Import necessary modules
local _ = require("gettext")
local logger = require("logger")
local table = require("table")
local MainConfig = require("config")
local MultiInputDialog = require("ui/widget/multiinputdialog")
local InfoMessage = require("ui/widget/infomessage")
local lfs = require("libs/libkoreader-lfs")
local UIManager = require("ui/uimanager")
local WidgetContainer = require("ui/widget/container/widgetcontainer")
local Menu = require("ui/widget/menu")
--- MangaReader Class
-- Handles the manga reader interface and settings.
local MangaReader = WidgetContainer:extend{
name = "MangaReader",
}
--- Initialize MangaReader
-- Registers the MangaReader to the main menu.
function MangaReader:init()
self.ui.menu:registerToMainMenu(self)
end
--- Add MangaReader to the main menu
-- @param menu_items Table containing menu items
function MangaReader:addToMainMenu(menu_items)
menu_items.MangaReader = {
text = _("Manga Reader"),
sub_item_table = {
{
text_func = function()
return _("Load module")
end,
callback = function()
self:loadModule()
end
},
{
text_func = function()
return _("Settings")
end,
callback = function()
self:loadSettings()
end
},
},
}
end
--- Load available manga reader modules
-- Scans the data directory for available manga reader modules and displays them in a menu.
function MangaReader:loadModule()
local data_dir = require("datastorage"):getDataDir() .. "/plugins/mangareader.koplugin/websites/"
self.results = {}
for lookup_path in lfs.dir(data_dir) do
if string.find(lookup_path, ".lua") then
local module_info = {
text = lookup_path:gsub("%.lua", ""),
path = data_dir .. lookup_path
}
table.insert(self.results, module_info)
end
end
self.menu = Menu:new{
title = _("Modules available:"),
no_title = false,
item_table = self.results,
onMenuSelect = function(_, item)
UIManager:close(self.menu)
local ok, plugin_module = pcall(dofile, item.path)
if not ok or not plugin_module then
logger.info("Error when loading", item.path, plugin_module)
return
end
plugin_module.init()
end,
close_callback = function()
UIManager:close(self.menu)
end,
}
UIManager:show(self.menu)
end
--- Load manga reader settings
-- Displays settings dialogs for MangaNova and MangaPlus.
function MangaReader:loadSettings()
local nova_info = _([[
MangaNova is a manga reading service.
"MangaNova API Token" is a developer setting.
Don't change anything unless you know what you're doing.
]])
local plus_info = _([[
MangaPlus is an official manga reading service.
You can select different quality settings for manga images.
Higher quality requires more bandwidth and storage space.
]])
--- Show MangaNova settings dialog
local function showNovaSettings()
self.settings_dialog = MultiInputDialog:new{
title = _("MangaNova Settings"),
fields = {
{
text = MainConfig.manganova.token or "",
hint = _("MangaNova API Token"),
},
},
buttons = {
{
{
text = _("Cancel"),
id = "close",
callback = function()
self.settings_dialog:onClose()
UIManager:close(self.settings_dialog)
end
},
{
text = _("Info"),
callback = function()
UIManager:show(InfoMessage:new{ text = nova_info })
end
},
{
text = _("Save"),
callback = function()
local fields = self.settings_dialog:getFields()
MainConfig.manganova.token = fields[1]
self:saveConfig(MainConfig)
self.settings_dialog:onClose()
UIManager:close(self.settings_dialog)
UIManager:show(InfoMessage:new{
text = _("Settings saved"),
})
end
},
},
},
}
UIManager:show(self.settings_dialog)
self.settings_dialog:onShowKeyboard()
end
--- Show MangaPlus settings dialog
local function showPlusSettings()
self.plus_dialog = MultiInputDialog:new{
title = _("MangaPlus Settings"),
fields = {
{
text = MainConfig.mangaplus.quality or "medium",
hint = _("Choose quality: low, medium, high, super_high"),
},
},
buttons = {
{
{
text = _("Cancel"),
id = "close",
callback = function()
self.plus_dialog:onClose()
UIManager:close(self.plus_dialog)
end
},
{
text = _("Info"),
callback = function()
UIManager:show(InfoMessage:new{ text = plus_info })
end
},
{
text = _("Save"),
callback = function()
local fields = self.plus_dialog:getFields()
local quality = fields[1]
local valid_qualities = { low = true, medium = true, high = true, super_high = true }
if valid_qualities[quality] then
MainConfig.mangaplus.quality = quality
self:saveConfig(MainConfig)
self.plus_dialog:onClose()
UIManager:close(self.plus_dialog)
UIManager:show(InfoMessage:new{
text = _("Quality set to " .. quality),
})
else
UIManager:show(InfoMessage:new{
text = _("Invalid quality setting. Please choose: low, medium, high, or super_high"),
})
end
end
},
},
},
}
UIManager:show(self.plus_dialog)
self.plus_dialog:onShowKeyboard()
end
local settings_items = {
{ text = _("MangaNova Settings"), callback = showNovaSettings },
{ text = _("MangaPlus Settings"), callback = showPlusSettings },
}
self.settings_menu = Menu:new{
title = _("Manga Reader Settings"),
item_table = settings_items,
close_callback = function()
UIManager:close(self.settings_menu)
end,
}
UIManager:show(self.settings_menu)
end
return MangaReader