modules/admin-full: further simplify wifi config
[project/luci.git] / modules / admin-full / luasrc / model / cbi / admin_network / iface_add.lua
index 3dc0ac4..92026b1 100644 (file)
@@ -1,7 +1,7 @@
 --[[
 LuCI - Lua Configuration Interface
 
-Copyright 2009 Jo-Philipp Wich <xm@subsignal.org>
+Copyright 2009-2010 Jo-Philipp Wich <xm@subsignal.org>
 
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
@@ -10,96 +10,95 @@ You may obtain a copy of the License at
        http://www.apache.org/licenses/LICENSE-2.0
 
 $Id$
+
 ]]--
 
-local nw  = require "luci.model.network"
-local fw  = require "luci.model.firewall"
+local nw  = require "luci.model.network".init()
+local fw  = require "luci.model.firewall".init()
+local utl = require "luci.util"
 local uci = require "luci.model.uci".cursor()
 
-m = SimpleForm("network", translate("Create Or Attach Network"),
-       translate("If the interface is attached to an existing network it will be <em>bridged</em> " ..
-               "to the existing interfaces and is covered by the firewall zone of the choosen network.<br />" ..
-               "Uncheck the attach option to define a new standalone network for this interface."
-       ))
-
-nw.init(uci)
-fw.init(uci)
+m = SimpleForm("network", translate("Create Interface"))
+m.redirect = luci.dispatcher.build_url("admin/network/network")
+m.reset = false
 
-attachnet = m:field(Flag, "_attach", translate("Attach to existing network"))
-attachnet.rmempty = false
-attachnet.default = "1"
-
-newnet = m:field(Value, "_netname_new", translate("Name of the new network"),
+newnet = m:field(Value, "_netname", translate("Name of the new interface"),
        translate("The allowed characters are: <code>A-Z</code>, <code>a-z</code>, " ..
                "<code>0-9</code> and <code>_</code>"
        ))
 
 newnet:depends("_attach", "")
-newnet.default = "net_" .. arg[1]:gsub("[^%w_]+", "_")
+newnet.default = arg[1] and "net_" .. arg[1]:gsub("[^%w_]+", "_")
+newnet.datatype = "uciname"
 
-addnet = m:field(Value, "_netname_attach",
-       translate("Network to attach interface to"))
+newproto = m:field(ListValue, "_netproto", translate("Protocol of the new interface"))
 
-addnet.template = "cbi/network_netlist"
-addnet.widget = "radio"
-addnet.nocreate = true
-addnet:depends("_attach", "1")
+netbridge = m:field(Flag, "_bridge", translate("Create a bridge over multiple interfaces"))
 
-fwzone = m:field(Value, "_fwzone",
-       translate("Create / Assign firewall-zone"),
-       translate("Choose the firewall zone you want to assign to this interface. Select <em>unspecified</em> to remove the interface from the associated zone or fill out the <em>create</em> field to define a new zone and attach the interface to it."))
 
-fwzone.template = "cbi/firewall_zonelist"
-addnet.widget = "radio"
-fwzone:depends("_attach", "")
-fwzone.default = "zone_" .. arg[1]:gsub("[^%w_]+", "_")
+sifname = m:field(Value, "_ifname", translate("Cover the following interface"))
 
+sifname.widget = "radio"
+sifname.template  = "cbi/network_ifacelist"
+sifname.nobridges = true
 
-function attachnet.write(self, section, value)
-       local net, zone
 
-       if value == "1" then
-               net = nw:get_network(addnet:formvalue(section))
-               if net then
-                       net:type("bridge")
-               end
-       else
-               local zval = fwzone:formvalue(section)
-               
-               net = nw:add_network(newnet:formvalue(section), { proto  = "none" })
-               zone = fw:get_zone(zval)
-
-               if not zone and zval == '-' then
-                       zval = m:formvalue(fwzone:cbid(section) .. ".newzone")
-                       if zval and #zval > 0 then
-                               zone = fw:add_zone(zval)
-                       else
-                               fw:del_network(arg[1])
-                       end
+mifname = m:field(Value, "_ifnames", translate("Cover the following interfaces"))
+
+mifname.widget = "checkbox"
+mifname.template  = "cbi/network_ifacelist"
+mifname.nobridges = true
+
+
+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
 
-       if not net then
-               self.error = { [section] = "missing" }
-       else
-               net:add_interface(arg[1])
+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
 
-               if zone then
-                       fw:del_network(net:name())
-                       zone:add_network(net:name())
+       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
-
-               uci:save("wireless")
-               uci:save("network")
-               uci:save("firewall")
-               luci.http.redirect(luci.dispatcher.build_url("admin/network/network", net:name()))
+               return nil, translate("The selected protocol needs a device assigned")
        end
+       return value
 end
 
-function fwzone.cfgvalue(self, section)
-       self.iface = section
-       local z = fw:get_zone_by_network(section)
-       return z and z: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
 
 return m