luci-app-freifunk-widgets: switch to nixio.fs
[project/luci.git] / applications / luci-app-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 "nixio.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 local file
41 for file in fs.dir("/usr/lib/lua/luci/view/freifunk/widgets/") do
42         if file ~= "." and file ~= ".." then
43                 tmpl:value(file)
44         end
45 end
46
47 local title = wdg:option(Value, "title", translate("Title"))
48 title.rmempty = true
49
50 local width = wdg:option(Value, "width", translate("Width"))
51 width.rmempty = true
52
53 local height = wdg:option(Value, "height", translate("Height"))
54 height.rmempty = true
55
56 local pr = wdg:option(Value, "paddingright", translate("Padding right"))
57 pr.rmempty = true
58
59 function m.on_commit(self)
60         -- clean custom text files whose config has been deleted
61         local dir = "/usr/share/customtext/"
62         local active = {}
63         uci:foreach("freifunk-widgets", "widget", function(s)
64                 if s["template"] == "html" then
65                         table.insert(active, s[".name"])
66                 end
67         end )
68         local file
69         for file in fs.dir(dir) do
70                 local filename = string.gsub(file, ".html", "")
71                 if not utl.contains(active, filename) then
72                         fs.unlink(dir .. file)
73                 end
74         end
75 end
76
77 return m