Merge pull request #278 from nmav/ocserv
[project/luci.git] / applications / luci-freifunk-widgets / luasrc / model / cbi / freifunk / widgets / widgets_overview.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2012 Manuel Munz <freifunk at somakoma dot de>
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10         http://www.apache.org/licenses/LICENSE-2.0
11 ]]--
12
13 local uci = require "luci.model.uci".cursor()
14 local fs = require "luci.fs"
15 local utl = require "luci.util"
16 m = Map("freifunk-widgets", translate("Widgets"),
17         translate("Configure installed widgets."))
18
19 wdg = m:section(TypedSection, "widget", translate("Widgets"))
20 wdg.addremove = true
21 wdg.extedit   = luci.dispatcher.build_url("admin/freifunk/widgets/widget/%s")
22 wdg.template  = "cbi/tblsection"
23 wdg.sortable  = true
24
25 --[[
26 function wdg.create(...)
27         local sid = TypedSection.create(...)
28         luci.http.redirect(wdg.extedit % sid)
29 end
30 ]]--
31
32 local en = wdg:option(Flag, "enabled", translate("Enable"))
33 en.rmempty = false
34 --en.default = "0"
35 function en.cfgvalue(self, section)
36         return Flag.cfgvalue(self, section) or "0"
37 end
38
39 local tmpl = wdg:option(ListValue, "template", translate("Template"))
40 for k, v in ipairs(fs.dir('/usr/lib/lua/luci/view/freifunk/widgets/')) do
41         if v ~= "." and v ~= ".." then
42                 tmpl:value(v)
43         end
44 end
45
46 local title = wdg:option(Value, "title", translate("Title"))
47 title.rmempty = true
48
49 local width = wdg:option(Value, "width", translate("Width"))
50 width.rmempty = true
51
52 local height = wdg:option(Value, "height", translate("Height"))
53 height.rmempty = true
54
55 local pr = wdg:option(Value, "paddingright", translate("Padding right"))
56 pr.rmempty = true
57
58 function m.on_commit(self)
59         -- clean custom text files whose config has been deleted
60         local dir = "/usr/share/customtext/"
61         local active = {}
62         uci:foreach("freifunk-widgets", "widget", function(s)
63                 if s["template"] == "html" then
64                         table.insert(active, s[".name"])
65                 end
66         end )
67         for k, v in ipairs(fs.dir(dir)) do
68                 filename = string.gsub(v, ".html", "")
69                 if not utl.contains(active, filename) then
70                         fs.unlink(dir .. v)
71                 end
72         end
73 end
74
75 return m