index.gmi: give repo.or.cz url
[gemini.koplugin.git] / schemeproxies.lua
blobab171801b81f5b078b0880f82d196a927650535a
1 local InputDialog = require("ui/widget/inputdialog")
2 local Persist = require("persist")
3 local DataStorage = require("datastorage")
4 local UIManager = require("ui/uimanager")
5 local KeyValuePage = require("ui/widget/keyvaluepage")
6 local _ = require("gettext")
7 local T = require("ffi/util").template
9 local gemini_dir = DataStorage:getDataDir() .. "/gemini"
10 local scheme_proxies_persist = Persist:new{ path = gemini_dir .. "/scheme_proxies.lua" }
12 local SchemeProxies = {
13 scheme_proxies = scheme_proxies_persist:load() or { gopher = {}, ["http(s)"] = {} },
16 function SchemeProxies:get(scheme)
17 local proxy = self.scheme_proxies[scheme]
18 if proxy and proxy.host then
19 return proxy
20 end
21 if scheme == "http" or scheme == "https" then
22 return self:get("http(s)")
23 end
24 end
26 function SchemeProxies:supportedSchemes()
27 local schemes = { "gemini", "about", "titan" }
28 for scheme,proxy in pairs(self.scheme_proxies) do
29 if proxy and proxy.host then
30 if scheme == "http(s)" then
31 table.insert(schemes, "http")
32 table.insert(schemes, "https")
33 else
34 table.insert(schemes, scheme)
35 end
36 end
37 end
38 return schemes
39 end
41 local function basicInputDialog(title, cb, input, is_secret)
42 local input_dialog
43 input_dialog = InputDialog:new{
44 title = title,
45 input = input or "",
46 text_type = is_secret and "password",
47 enter_callback = cb,
48 buttons = {
51 text = _("Cancel"),
52 id = "close",
53 callback = function()
54 UIManager:close(input_dialog)
55 end,
58 text = _("Enter"),
59 callback = cb,
64 return input_dialog
65 end
67 function SchemeProxies:edit()
68 local menu
69 local kv_pairs = {}
70 for scheme,proxy in pairs(self.scheme_proxies) do
71 table.insert(kv_pairs, { scheme, proxy.host or "", callback = function()
72 local input_dialog
73 input_dialog = basicInputDialog(
74 T(_("Set proxy server for %1 URLs"), scheme),
75 function()
76 local host = input_dialog:getInputText()
77 if host == "" then
78 host = nil
79 end
80 self.scheme_proxies[scheme].host = host
81 scheme_proxies_persist:save(self.scheme_proxies)
82 UIManager:close(input_dialog)
83 UIManager:close(menu)
84 self:edit()
85 end,
86 proxy.host or "")
87 UIManager:show(input_dialog)
88 input_dialog:onShowKeyboard()
89 end })
90 end
91 table.insert(kv_pairs, { _("[New]"), _("Select to add new scheme"), callback = function()
92 local input_dialog
93 input_dialog = basicInputDialog(_("Add new scheme to proxy"), function()
94 local scheme = input_dialog:getInputText()
95 UIManager:close(input_dialog)
96 if scheme then
97 self.scheme_proxies[scheme] = {}
98 UIManager:close(menu)
99 self:edit()
101 end)
102 UIManager:show(input_dialog)
103 input_dialog:onShowKeyboard()
104 end })
105 menu = KeyValuePage:new{
106 title = _("Scheme proxies"),
107 kv_pairs = kv_pairs,
109 UIManager:show(menu)
112 return SchemeProxies