* CBI: updates
[project/luci.git] / src / ffluci / cbi.lua
index 85b9f49e9dc56b46f8ce4614c1f883cff0094800..a535dc8b72711f9349d42cb509f01f00df1ffb2d 100644 (file)
@@ -41,10 +41,10 @@ function load(cbimap)
        require("ffluci.i18n")
        
        local cbidir = ffluci.fs.dirname(ffluci.util.__file__()) .. "model/cbi/"
-       local func = loadfile(cbidir..cbimap..".lua")
+       local func, err = loadfile(cbidir..cbimap..".lua")
        
        if not func then
-               error("Unable to load CBI map: " .. cbimap)
+               error(err)
                return nil
        end
        
@@ -102,6 +102,27 @@ function Map.__init__(self, config, ...)
        Node.__init__(self, ...)
        self.config = config
        self.template = "cbi/map"
+       self.uci = ffluci.model.uci.Session()
+end
+
+function Map.parse(self)
+       self.ucidata = self.uci:show(self.config)
+       if not self.ucidata then
+               error("Unable to read UCI data: " .. self.config)
+       else
+               self.ucidata = self.ucidata[self.config]
+       end
+       Node.parse(self)
+end
+
+function Map.render(self)
+       self.ucidata = self.uci:show(self.config)
+       if not self.ucidata then
+               error("Unable to read UCI data: " .. self.config)
+       else
+               self.ucidata = self.ucidata[self.config]
+       end
+       Node.render(self)       
 end
 
 function Map.section(self, class, ...)
@@ -116,9 +137,8 @@ function Map.section(self, class, ...)
        end
 end
 
-function Map.read(self)
-       self.ucidata = self.ucidata or ffluci.model.uci.show(self.config)[self.config]
-       return self.ucidata
+function Map.set(self, section, option, value)
+       return self.uci:set(self.config, section, option, value)
 end
 
 --[[
@@ -181,6 +201,15 @@ function TypedSection.__init__(self, ...)
        self.valid     = nil
 end
 
+function TypedSection.parse(self)
+       for k, v in pairs(self:ucisections()) do
+               for i, node in ipairs(self.children) do
+                       node.section = k
+                       node:parse()
+               end 
+       end
+end
+
 function TypedSection.render_children(self, section)
        for k, node in ipairs(self.children) do
                node.section = section
@@ -190,7 +219,7 @@ end
 
 function TypedSection.ucisections(self)
        local sections = {}
-       for k, v in pairs(self.map:read()) do
+       for k, v in pairs(self.map.ucidata) do
                if v[".type"] == self.sectiontype then
                        sections[k] = v
                end
@@ -217,14 +246,20 @@ function AbstractValue.__init__(self, option, ...)
        self.default = nil
 end
 
-
 function AbstractValue.formvalue(self)
-       local key = "uci."..self.map.config.."."..self.section.."."..self.option
+       local key = "cbid."..self.map.config.."."..self.section.."."..self.option
        return ffluci.http.formvalue(key)
 end
 
+function AbstractValue.parse(self)
+       local fvalue = self:validate(self:formvalue())
+       if fvalue and not (fvalue == self:ucivalue()) then
+               self:write(fvalue)
+       end 
+end
+
 function AbstractValue.ucivalue(self)
-       return self.map:read()[self.section][self.option]
+       return self.map.ucidata[self.section][self.option]
 end
 
 function AbstractValue.validate(self, value)
@@ -232,7 +267,7 @@ function AbstractValue.validate(self, value)
 end
 
 function AbstractValue.write(self, value)
-       ffluci.model.uci.set(self.config, self.section, self.option, value)
+       return self.map:set(self.section, self.option, value)
 end
 
 
@@ -266,7 +301,7 @@ function ListValue.__init__(self, ...)
        self.list = {}
 end
 
-function ListValue.addValue(self, key, val)
+function ListValue.add_value(self, key, val)
        val = val or key
        self.list[key] = val
 end
\ No newline at end of file