modules/admin-full: rework interface add dialog, now handles floating protocols like...
authorJo-Philipp Wich <jow@openwrt.org>
Sun, 9 Oct 2011 19:28:30 +0000 (19:28 +0000)
committerJo-Philipp Wich <jow@openwrt.org>
Sun, 9 Oct 2011 19:28:30 +0000 (19:28 +0000)
modules/admin-full/luasrc/model/cbi/admin_network/iface_add.lua

index c3bf664bd0235980bac81a930398c546a51ee1f5..0e62ee88cffad11a980709a3a1272b644cfd0fbf 100644 (file)
@@ -19,6 +19,8 @@ local utl = require "luci.util"
 local uci = require "luci.model.uci".cursor()
 
 m = SimpleForm("network", translate("Create Interface"))
 local uci = require "luci.model.uci".cursor()
 
 m = SimpleForm("network", translate("Create Interface"))
+m.redirect = luci.dispatcher.build_url("admin/network/network")
+m.reset = false
 
 newnet = m:field(Value, "_netname", translate("Name of the new interface"),
        translate("The allowed characters are: <code>A-Z</code>, <code>a-z</code>, " ..
 
 newnet = m:field(Value, "_netname", translate("Name of the new interface"),
        translate("The allowed characters are: <code>A-Z</code>, <code>a-z</code>, " ..
@@ -29,6 +31,8 @@ newnet:depends("_attach", "")
 newnet.default = arg[1] and "net_" .. arg[1]:gsub("[^%w_]+", "_")
 newnet.datatype = "uciname"
 
 newnet.default = arg[1] and "net_" .. arg[1]:gsub("[^%w_]+", "_")
 newnet.datatype = "uciname"
 
+newproto = m:field(ListValue, "_netproto", translate("Protocol of the new interface"))
+
 netbridge = m:field(Flag, "_bridge", translate("Create a bridge over multiple interfaces"))
 
 
 netbridge = m:field(Flag, "_bridge", translate("Create a bridge over multiple interfaces"))
 
 
@@ -36,41 +40,66 @@ sifname = m:field(Value, "_ifname", translate("Cover the following interface"),
        translate("Note: If you choose an interface here which is part of another network, it will be moved into this network."))
 
 sifname.widget = "radio"
        translate("Note: If you choose an interface here which is part of another network, it will be moved into this network."))
 
 sifname.widget = "radio"
-sifname.template = "cbi/network_ifacelist"
+sifname.template  = "cbi/network_ifacelist"
 sifname.nobridges = true
 sifname.nobridges = true
-sifname:depends("_bridge", "")
 
 
 mifname = m:field(Value, "_ifnames", translate("Cover the following interfaces"),
        translate("Note: If you choose an interface here which is part of another network, it will be moved into this network."))
 
 mifname.widget = "checkbox"
 
 
 mifname = m:field(Value, "_ifnames", translate("Cover the following interfaces"),
        translate("Note: If you choose an interface here which is part of another network, it will be moved into this network."))
 
 mifname.widget = "checkbox"
-mifname.template = "cbi/network_ifacelist"
+mifname.template  = "cbi/network_ifacelist"
 mifname.nobridges = true
 mifname.nobridges = true
-mifname:depends("_bridge", "1")
 
 
-function newnet.write(self, section, value)
-       local bridge = netbridge:formvalue(section) == "1"
-       local ifaces = bridge and mifname:formvalue(section) or sifname:formvalue(section)
 
 
-       local nn = nw:add_network(value, { proto = "none" })
-       if nn then
-               if bridge then
-                       nn:set("type", "bridge")
+local _, p
+for _, p in ipairs(nw:get_protocols()) do
+       if p:is_installed() then
+               newproto:value(p:proto(), p:get_i18n())
+               if not p:is_virtual()  then netbridge:depends("_netproto", p:proto()) end
+               if not p:is_floating() then
+                       sifname:depends({ _bridge = "",  _netproto = p:proto()})
+                       mifname:depends({ _bridge = "1", _netproto = p:proto()})
                end
                end
+       end
+end
 
 
-               local iface
-               for iface in utl.imatch(ifaces) do
-                       nn:add_interface(iface)
-                       if not bridge then
-                               break
-                       end
-               end
+function newproto.validate(self, value, section)
+       local name = newnet:formvalue(section)
+       if not name or #name == 0 then
+               newnet:add_error(section, translate("No network name specified"))
+       elseif m:get(name) then
+               newnet:add_error(section, translate("The given network name is not unique"))
+       end
 
 
-               nw:save("network")
-               nw:save("wireless")
+       local proto = nw:get_protocol(value)
+       if proto and not proto:is_floating() then
+               local br = (netbridge:formvalue(section) == "1")
+               local ifn = br and mifname:formvalue(section) or sifname:formvalue(section)
+               for ifn in utl.imatch(ifn) do
+                       return value
+               end
+               return nil, translate("The selected protocol needs a device assigned")
+       end
+       return value
+end
 
 
-               luci.http.redirect(luci.dispatcher.build_url("admin/network/network", nn:name()))
+function newproto.write(self, section, value)
+       local name = newnet:formvalue(section)
+       if name and #name > 0 then
+               local br = (netbridge:formvalue(section) == "1") and "bridge" or nil
+               local net = nw:add_network(name, { proto = value, type = br })
+               if net then
+                       local ifn
+                       for ifn in utl.imatch(
+                               br and mifname:formvalue(section) or sifname:formvalue(section)
+                       ) do
+                               net:add_interface(ifn)
+                       end
+                       nw:save("network")
+                       nw:save("wireless")
+               end
+               luci.http.redirect(luci.dispatcher.build_url("admin/network/network", name))
        end
 end
 
        end
 end