* Added QoS
[project/luci.git] / core / 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 require("ffluci.i18n")
32 require("ffluci.config")
33 require("ffluci.model.ipkg")
34
35 -- Default modelpath
36 modelpath = ffluci.config.path .. "/model/menu/"
37
38 -- Menu definition extra scope
39 scope = {
40         translate = ffluci.i18n.translate,
41         loadtrans = ffluci.i18n.loadc,
42         isfile    = ffluci.fs.mtime
43 }
44
45 -- Local menu database
46 local menu = {}
47
48 -- The current pointer
49 local menuc = {}
50
51 -- Adds a menu category to the current menu and selects it
52 function add(cat, controller, title, order)
53         order = order or 100
54         if not menu[cat] then
55                 menu[cat] = {}
56         end
57         
58         local entry = {}
59         entry[".descr"] = title
60         entry[".order"] = order
61         entry[".contr"] = controller
62         
63         menuc = entry
64
65         local i = 0                     
66         for k,v in ipairs(menu[cat]) do
67                 if v[".order"] > entry[".order"] then
68                         break
69                 end  
70                 i = k
71         end     
72         table.insert(menu[cat], i+1, entry)
73                 
74         return true
75 end
76
77 -- Adds an action to the current menu
78 function act(action, title)
79         table.insert(menuc, {action = action, descr = title})
80         return true
81 end
82
83 -- Selects a menu category
84 function sel(cat, controller)
85         if not menu[cat] then
86                 return nil
87         end
88         menuc = menu[cat]
89         
90         local stat = nil
91         for k,v in ipairs(menuc) do
92                 if v[".contr"] == controller then
93                         menuc = v
94                         stat = true
95                 end
96         end
97         
98         return stat
99 end
100
101
102 -- Collect all menu information provided in the model dir
103 function collect()
104         for k, menu in pairs(ffluci.fs.dir(modelpath)) do
105                 if menu:sub(1, 1) ~= "." then
106                         local f = loadfile(modelpath.."/"..menu)
107                         local env = ffluci.util.clone(scope)
108                         
109                         env.add = add
110                         env.sel = sel
111                         env.act = act
112                         
113                         setfenv(f, env)
114                         f()
115                 end
116         end
117 end
118
119 -- Returns the menu information
120 function get()
121         collect()
122         return menu
123 end