// ======================================================================
// libbone.js
//
// Summary: New network query routines that use the binder
//
// Description:
// This file is meant to be loaded from the root index.html and therefore
// be available to all BeIA scripts as beos.realTop.network_* .
//
// Maintainer: Dan Sandler <dsandler@be.com>
// ======================================================================

// DONT_APPLY_ON_SWITCH : if true, Apply() will never be called
// when the interface is switched between ppp and dhcp.  Debugging
// only.
var DONT_APPLY_ON_SWITCH = false;

var HACK_STATIC_IP_REMEMBERS_SETTINGS = true;

// network_binder() -> <BinderNode object>|null
// Utility to grab the binder node or carp if it can't be found.
function network_binder() {
	var network = beos.binder.service.network;
	if ( network == "undefined" || network == null) {
		beos.dprintf('network_binder(): could not locate binder node /service/network');
		return null;
	}
	return network;
}

function network_default_interface() {
	var network = network_binder();
	if (network != null) {
		var if_node = network.interfaces;
		var if_first = if_node[0];
		var iface = if_first.value;
		return iface;
	} else {
		return null;
	}
}

function network_default_route() {
	var network = network_binder();
	if (network != null) {
		return network.routes.default_route;
	} else {
		return null;
	}
}

function network_default_dial_profile() {
	var network = network_binder();
	if (network != null) {
		var default_name = network.dialer.default_profile;
		var profile = network.dialer.profiles[default_name];
		return profile;
	} else { return null; }
}

function network_flush_settings() {
	var network = network_binder();
	if (network != null) network["@sync"]();
}

// class NetworkInfoObject : mini-object for use with network_get_info()
function NetworkInfoObject() {
	//	this.connection =
	//	this.dhcp =		this.ppp = 		this.ip =
	//	this.gw = 		this.user = 	this.pass =
	//	this.phone = 	this.init =		this.status =
	//	"unknown";
}

NetworkInfoObject.prototype.toString =
function NetworkInfoObject__toString() {
	return ("[object NetworkInfoObject{status=" + this.status
		+ "; ip=" + this.ip
		+ "; connection="
		+ this.connection + "}]");
}

// network_get_info() -> <NetworkInfoObject>
function network_get_info()
{
	var netinfo = new NetworkInfoObject();
	var network;
	
	if ( null == (network = network_binder()) ) return netinfo; // bail out with a valid object
	
	var dialup_profile = network.dialer.profiles[network.dialer['default_profile']];
	
	var iface = beos.realTop.network_default_interface();

	if (null == iface) {
		beos.dprintf('network_get_info(): error: could not read interface (is BeIA net_server available?)');
		return netinfo;
	}
	if (null == dialup_profile) {
		beos.dprintf('network_get_info(): error: could not read dialer (is BeIA net_server available?)');
		return netinfo;
	}
	
	if (network.dialer.use_ppp.valueOf() == "1") { // was dialup_profile.autodial
		//if (dialup_profile.ppp_type.valueOf() == "pppoe")
		if (network.dialer.default_profile.valueOf() == "pppoe_profile")
			netinfo.connection = "dialup-pppoe";
		else
			netinfo.connection = "dialup-ppp";
	} else if (iface.dhcp.valueOf() == "1") {
		netinfo.connection = "ethernet-dhcp";
	} else {
		netinfo.connection = "ethernet-static";
	}

	netinfo.ppp = network.pppstatus.valueOf();
	netinfo.dhcp = "offline";
	
	// I don't really use this
	//	if (netinfo.ppp != "up" && network.status.valueOf() == "up") {
	//		netinfo.dhcp = "online";
	//	}
	
	var r 			= network_default_route();
	netinfo.ip 		= r.address.valueOf(); //iface.address.valueOf();
	if (r == null || r.gateway == undefined) netinfo.gw = "unknown";
	else netinfo.gw	= r.gateway.valueOf();
	netinfo.user 	= dialup_profile.user.valueOf();
	netinfo.pass 	= dialup_profile.password.valueOf();
	netinfo.phone 	= dialup_profile.phone_number.valueOf();
	netinfo.init 	= dialup_profile.modeminit.valueOf();
	
	netinfo.status 	= network.status.valueOf();
	if (netinfo.status == undefined) netinfo.status = "unknown";

	return netinfo;
	
}

function network_dial_ppp() 
{
	var network;
	if ( null == (network = network_binder()) ) return false;
	network.PPP_Up();
	return true;
}

function network_hangup_ppp()
{
	var network;
	if ( null == (network = network_binder()) ) return false;
	network.PPP_Down();
	return true;
}

function network_use_ppp()
{
	var network;
	if ( null == (network = network_binder()) ) return false;
	
	network.dialer.use_ppp = "1";
	
	network.dialer.default_profile = "modem_profile";
	var profile = beos.realTop.network_default_dial_profile();
	if (profile) {
//		profile.autodial = "0";
//		profile.ppp_type = "serial";
	}
	var iface = beos.realTop.network_default_interface();
	if (iface) iface.dhcp = "0";

	network_flush_settings(); // flush to disk
	
	if (!DONT_APPLY_ON_SWITCH) network.Apply();

	return true;
}

function network_use_pppoe()
{
	var network;
	if ( null == (network = network_binder()) ) return false;
	
	network.dialer.use_ppp = "1";
	
	network.dialer.default_profile = "pppoe_profile";
//	var profile = beos.realTop.network_default_dial_profile();
//	if (profile) {
//		profile.ppp_type = "pppoe";
//	}
	var iface = beos.realTop.network_default_interface();
	if (iface) iface.dhcp = "0";

	network_flush_settings(); // flush to disk
	
	if (!DONT_APPLY_ON_SWITCH) network.Apply();

	return true;
}

function network_use_dhcp()
{
	var network;
	if ( null == (network = network_binder()) ) return false;
	
	network.dialer.use_ppp = "0";

	var profile = beos.realTop.network_default_dial_profile();
//	if (profile) profile.autodial = "0";
	var iface = beos.realTop.network_default_interface();
	if (iface) {
		iface.dhcp = "1";
		iface.flags = "up";
	}

	network_flush_settings(); // flush to disk

	if (!DONT_APPLY_ON_SWITCH) network.Apply();

	return true;
}

function network_use_static()
{
	var network;
	if ( null == (network = network_binder()) ) return false;
	
	network.dialer.use_ppp = "0";

	var profile = beos.realTop.network_default_dial_profile();
//	if (profile) profile.autodial = "0";
	var iface = beos.realTop.network_default_interface();
	if (iface) {
		iface.dhcp = "0";
		iface.flags = "up";
	}
	
	if (HACK_STATIC_IP_REMEMBERS_SETTINGS) {
		beos.dprintf("*** HACK ALERT: loading static_ip settings from web state cuz BONE doesn't");
		var staticprofile = network.connection_profiles["ethernet-static"];
		var default_route = beos.realTop.network_default_route();

		iface.address = staticprofile.address.valueOf();
		iface.mask = staticprofile.mask.valueOf();

		default_route.gateway = staticprofile.gateway.valueOf();

		network.DNS.domain = staticprofile.domain.valueOf();
		network.DNS.primary = staticprofile.dns1.valueOf();
		network.DNS.secondary = staticprofile.dns2.valueOf();
	}

	network_flush_settings(); // flush to disk

	if (!DONT_APPLY_ON_SWITCH) network.Apply();

	return true;
}

function network_set_user(user)
{
	var profile = beos.realTop.network_default_dial_profile();
	if (profile) profile.user = user;
	return true;
}

function network_set_pass(pass)
{
	var profile = beos.realTop.network_default_dial_profile();
	if (profile) profile.password = pass;
	return true;
}

function network_set_phone(phone)
{
	var profile = beos.realTop.network_default_dial_profile();
	if (profile) profile.phone_number = phone;
	return true;
}

function network_set_init(str)
{	
	var profile = beos.realTop.network_default_dial_profile();
	if (profile) profile.modeminit = str;
	return true;
}

