Fix broken repository link in target/makeccs
[librecmc/librecmc.git] / package / luci / modules / luci-mod-system / luasrc / model / cbi / admin_system / system.lua
1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Copyright 2011 Jo-Philipp Wich <jow@openwrt.org>
3 -- Licensed to the public under the Apache License 2.0.
4
5 local sys   = require "luci.sys"
6 local zones = require "luci.sys.zoneinfo"
7 local fs    = require "nixio.fs"
8 local conf  = require "luci.config"
9
10 local m, s, o
11 local has_ntpd = fs.access("/usr/sbin/ntpd")
12 local has_zram = fs.access("/etc/init.d/zram")
13
14 m = Map("system", translate("System"), translate("Here you can configure the basic aspects of your device like its hostname or the timezone."))
15 m:chain("luci")
16
17
18 s = m:section(TypedSection, "system", translate("System Properties"))
19 s.anonymous = true
20 s.addremove = false
21
22 s:tab("general",  translate("General Settings"))
23 s:tab("logging",  translate("Logging"))
24 s:tab("language", translate("Language and Style"))
25 s:tab("advanced", translate("Advanced"))
26 if has_zram then s:tab("zram", translate("ZRam Settings")) end
27
28 --
29 -- System Properties
30 --
31
32 o = s:taboption("general", DummyValue, "_systime", translate("Local Time"))
33 o.template = "admin_system/clock_status"
34
35
36 o = s:taboption("general", Value, "hostname", translate("Hostname"))
37 o.datatype = "hostname"
38
39 function o.write(self, section, value)
40         Value.write(self, section, value)
41         sys.hostname(value)
42 end
43
44
45 o = s:taboption("general", ListValue, "zonename", translate("Timezone"))
46 o:value("UTC")
47
48 for i, zone in ipairs(zones.TZ) do
49         o:value(zone[1])
50 end
51
52 function o.write(self, section, value)
53         local function lookup_zone(title)
54                 for _, zone in ipairs(zones.TZ) do
55                         if zone[1] == title then return zone[2] end
56                 end
57         end
58
59         AbstractValue.write(self, section, value)
60         local timezone = lookup_zone(value) or "GMT0"
61         self.map.uci:set("system", section, "timezone", timezone)
62         fs.writefile("/etc/TZ", timezone .. "\n")
63 end
64
65
66 --
67 -- Logging
68 --
69
70 o = s:taboption("logging", Value, "log_size", translate("System log buffer size"), "kiB")
71 o.optional    = true
72 o.placeholder = 16
73 o.datatype    = "uinteger"
74
75 o = s:taboption("logging", Value, "log_ip", translate("External system log server"))
76 o.optional    = true
77 o.placeholder = "0.0.0.0"
78 o.datatype    = "ip4addr"
79
80 o = s:taboption("logging", Value, "log_port", translate("External system log server port"))
81 o.optional    = true
82 o.placeholder = 514
83 o.datatype    = "port"
84
85 o = s:taboption("logging", ListValue, "log_proto", translate("External system log server protocol"))
86 o:value("udp", "UDP")
87 o:value("tcp", "TCP")
88
89 o = s:taboption("logging", Value, "log_file", translate("Write system log to file"))
90 o.optional    = true
91 o.placeholder = "/tmp/system.log"
92
93 o = s:taboption("logging", ListValue, "conloglevel", translate("Log output level"))
94 o:value(8, translate("Debug"))
95 o:value(7, translate("Info"))
96 o:value(6, translate("Notice"))
97 o:value(5, translate("Warning"))
98 o:value(4, translate("Error"))
99 o:value(3, translate("Critical"))
100 o:value(2, translate("Alert"))
101 o:value(1, translate("Emergency"))
102
103 o = s:taboption("logging", ListValue, "cronloglevel", translate("Cron Log Level"))
104 o.default = 8
105 o:value(5, translate("Debug"))
106 o:value(8, translate("Normal"))
107 o:value(9, translate("Warning"))
108
109
110 --
111 -- Zram Properties
112 --
113 if has_zram then
114         o = s:taboption("zram", Value, "zram_size_mb", translate("ZRam Size"), translate("Size of the ZRam device in megabytes"))
115         o.optional    = true
116         o.placeholder = 16
117         o.datatype    = "uinteger"
118         
119         o = s:taboption("zram", ListValue, "zram_comp_algo", translate("ZRam Compression Algorithm"))
120         o.optional    = true
121         o.placeholder = lzo
122         o:value("lzo", "lzo")
123         o:value("lz4", "lz4")
124         o:value("deflate", "deflate")
125         
126         o = s:taboption("zram", Value, "zram_comp_streams", translate("ZRam Compression Streams"), translate("Number of parallel threads used for compression"))
127         o.optional    = true
128         o.placeholder = 1
129         o.datatype    = "uinteger"
130 end
131
132
133 --
134 -- Language & Style
135 --
136
137 o = s:taboption("language", ListValue, "_lang", translate("Language"))
138 o:value("auto")
139
140 local i18ndir = luci.i18n.i18ndir .. "base."
141 for k, v in luci.util.kspairs(conf.languages) do
142         local file = i18ndir .. k:gsub("_", "-")
143         if k:sub(1, 1) ~= "." and fs.access(file .. ".lmo") then
144                 o:value(k, v)
145         end
146 end
147
148 function o.cfgvalue(...)
149         return m.uci:get("luci", "main", "lang")
150 end
151
152 function o.write(self, section, value)
153         m.uci:set("luci", "main", "lang", value)
154 end
155
156
157 o = s:taboption("language", ListValue, "_mediaurlbase", translate("Theme"))
158 for k, v in pairs(conf.themes) do
159         if k:sub(1, 1) ~= "." then
160                 o:value(v, k)
161         end
162 end
163
164 function o.cfgvalue(...)
165         return m.uci:get("luci", "main", "mediaurlbase")
166 end
167
168 function o.write(self, section, value)
169         m.uci:set("luci", "main", "mediaurlbase", value)
170 end
171
172
173 --
174 -- Advanced
175 --
176
177 o = s:taboption("advanced", Value, "_pollinterval",
178         translate("Polling interval"),
179         translate("Polling interval for status queries in seconds"))
180 o.datatype = "range(3, 20)"
181 o.default = 5
182 o:value("3")
183 o:value("5")
184 o:value("10")
185
186 function o.cfgvalue(...)
187         return m.uci:get("luci", "main", "pollinterval")
188 end
189
190 function o.write(self, section, value)
191         m.uci:set("luci", "main", "pollinterval", value)
192 end
193
194
195 --
196 -- NTP
197 --
198
199 if has_ntpd then
200
201         -- timeserver setup was requested, create section and reload page
202         if m:formvalue("cbid.system._timeserver._enable") then
203                 m.uci:section("system", "timeserver", "ntp",
204                         {
205                         server = { "0.openwrt.pool.ntp.org", "1.openwrt.pool.ntp.org", "2.openwrt.pool.ntp.org", "3.openwrt.pool.ntp.org" }
206                         }
207                 )
208
209                 m.uci:save("system")
210                 luci.http.redirect(luci.dispatcher.build_url("admin/system", arg[1]))
211                 return
212         end
213
214         local has_section = false
215         m.uci:foreach("system", "timeserver", 
216                 function(s) 
217                         has_section = true 
218                         return false
219         end)
220
221         if not has_section then
222
223                 s = m:section(TypedSection, "timeserver", translate("Time Synchronization"))
224                 s.anonymous   = true
225                 s.cfgsections = function() return { "_timeserver" } end
226
227                 x = s:option(Button, "_enable")
228                 x.title      = translate("Time Synchronization is not configured yet.")
229                 x.inputtitle = translate("Set up Time Synchronization")
230                 x.inputstyle = "apply"
231
232         else
233                 
234                 s = m:section(TypedSection, "timeserver", translate("Time Synchronization"))
235                 s.anonymous = true
236                 s.addremove = false
237
238                 o = s:option(Flag, "enable", translate("Enable NTP client"))
239                 o.rmempty = false
240
241                 function o.cfgvalue(self)
242                         return sys.init.enabled("sysntpd")
243                                 and self.enabled or self.disabled
244                 end
245
246                 function o.write(self, section, value)
247                         if value == self.enabled then
248                                 sys.init.enable("sysntpd")
249                                 sys.call("env -i /etc/init.d/sysntpd start >/dev/null")
250                         else
251                                 sys.call("env -i /etc/init.d/sysntpd stop >/dev/null")
252                                 sys.init.disable("sysntpd")
253                         end
254                 end
255
256
257                 o = s:option(Flag, "enable_server", translate("Provide NTP server"))
258                 o:depends("enable", "1")
259
260
261                 o = s:option(DynamicList, "server", translate("NTP server candidates"))
262                 o.datatype = "host(0)"
263                 o:depends("enable", "1")
264
265                 -- retain server list even if disabled
266                 function o.remove() end
267
268         end
269 end
270
271 return m