7b192aaea906d09df4aad8536fd1c203a70eb408
[project/luci.git] / src / ffluci / menu.lua
1 --[[
2 FFLuCI - Menu Builder
3
4 Description:
5 Collects menu building information from controllers
6
7 FileId:
8 $Id$
9
10 License:
11 Copyright 2008 Steven Barth <steven@midlink.org>
12
13 Licensed under the Apache License, Version 2.0 (the "License");
14 you may not use this file except in compliance with the License.
15 You may obtain a copy of the License at 
16
17         http://www.apache.org/licenses/LICENSE-2.0 
18
19 Unless required by applicable law or agreed to in writing, software
20 distributed under the License is distributed on an "AS IS" BASIS,
21 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 See the License for the specific language governing permissions and
23 limitations under the License.
24
25 ]]--
26 module("ffluci.menu", package.seeall)
27
28 require("ffluci.fs")
29 require("ffluci.util")
30 require("ffluci.template")
31
32 ctrldir   = ffluci.fs.dirname(ffluci.util.__file__()) .. "controller/"
33 modelpath = ffluci.fs.dirname(ffluci.util.__file__()) .. "model/menudata.lua"
34
35 -- Cache menudata into a Luafile instead of recollecting it at every pageload
36 -- Warning: Make sure the menudata cache gets deleted everytime you update
37 -- the menu information of any module or add or remove a module
38 builder_enable_cache = false
39
40
41 -- Builds the menudata file
42 function build()
43         local data = collect()
44         ffluci.fs.writefile(modelpath, dump(data, "m"))
45         return data
46 end
47
48
49 -- Collect all menu information provided in the controller modules
50 function collect()
51         local m = {}
52         for k,cat in pairs(ffluci.fs.dir(ctrldir)) do
53                 m[cat] = {}
54                 for k,con in pairs(ffluci.fs.dir(ctrldir .. "/" .. cat)) do
55                         if con:sub(-4) == ".lua" then
56                                 con = con:sub(1, con:len()-4)
57                                 local mod = require("ffluci.controller." .. cat .. "." .. con)
58                                 if mod.menu and mod.menu.descr
59                                 and mod.menu.entries and mod.menu.order then
60                                         local entry = {}
61                                         entry[".descr"] = mod.menu.descr
62                                         entry[".order"] = mod.menu.order
63                                         entry[".contr"] = con
64                                         for k,v in pairs(mod.menu.entries) do
65                                                 entry[k] = v
66                                         end
67                                         local i = 0                     
68                                         for k,v in ipairs(m[cat]) do
69                                                 if v[".order"] > entry[".order"] then
70                                                         break
71                                                 end  
72                                                 i = k
73                                         end     
74                                         table.insert(m[cat], i+1, entry)
75                                 end
76                         end
77                 end
78         end
79         return m
80 end
81
82
83 -- Dumps a table into a string of Lua code
84 function dump(tbl, name)
85         local src = name .. "={}\n"
86         for k,v in pairs(tbl) do
87                 if type(k) == "string" then
88                         k = ffluci.util.escape(k)
89                         k = "'" .. ffluci.util.escape(k, "'") .. "'"
90                 end             
91                 if type(v) == "string" then
92                         v = ffluci.util.escape(v)
93                         v = ffluci.util.escape(v, "'")
94                         src = src .. name .. "[" .. k .. "]='" .. v .. "'\n"
95                 elseif type(v) == "number" then
96                         src = src .. name .. "[" .. k .. "]=" .. v .. "\n"
97                 elseif type(v) == "table" then
98                         src = src .. dump(v, name .. "[" .. k .. "]")
99                 end
100         end
101         return src
102 end
103
104 -- Returns the menu information
105 function get()
106         if builder_enable_cache then
107                 local cachemt = ffluci.fs.mtime(modelpath)
108                 local data = nil
109                 
110                 if cachemt == nil then
111                         data = build()
112                 else
113                         local fenv = {}
114                         local f    = loadfile(modelpath)
115                         setfenv(f, fenv)
116                         f()
117                         data = fenv.m
118                 end
119                 
120                 return data
121         else
122                 return collect()
123         end
124 end