/**
 * File:	include/dns-server/options.ycp
 * Package:	Configuration of dns-server
 * Summary:	Table options and popups used by DNS server configuration
 * Authors:	Jiri Srain <jsrain@suse.cz>
 *
 * $Id: options.ycp 19875 2004-10-19 12:45:48Z locilka $
 */

{

textdomain "dns-server";

import "Label";
import "CWM";
import "DnsServer";

// global table fallback handlers

/**
 * Fallback initialization function of a table entry / popup
 * @param opt_id any option id
 * @param opt_key any option key
 */
global define void globalPopupInit (any opt_id, string opt_key) ``{
    if (opt_id != nil)
    {
	integer oid = (integer)opt_id;
	UI::ChangeWidget (`id (opt_key), `Value,
	    current_section[oid, "value"]:"");
    }
    UI::SetFocus (`id (opt_key));
}

/**
 * Fallback store function of a table entry / popup
 * @param opt_id any option id
 * @param opt_key any option key
 */
global define void globalPopupStore (any opt_id, string opt_key) ``{
    if (opt_id == nil)
    {
	map<string,any> new_opt = $[
	    "key" : opt_key,
	    "value" : UI::QueryWidget (`id (opt_key), `Value),
	];
	current_section = add (current_section, new_opt);
    }
    else
    {
	integer oid = (integer)opt_id;
	current_section[oid, "value"] = UI::QueryWidget (`id (opt_key), `Value);
    }
    DnsServer::SetModified ();
}

/**
 * Fallback summary function of a table entry / popup
 * @param opt_id any option id
 * @param opt_key any option key
 * @return string table entry summary
 */
global define string globalTableEntrySummary (any opt_id, string opt_key)``{
    integer oid = (integer)opt_id;
    return sformat ("%1", current_section[oid, "value"]:"");
}


// master domain table fallback handlers

/**
 * Fallback initialization function of a table entry / popup
 * @param opt_id any option id
 * @param opt_key any option key
 */
global define void masterPopupInit (any opt_id, string opt_key) ``{
    integer index = (integer)opt_id;
    string key = "";
    string value = "";
    if (index != nil)
    {
	key = current_zone["records", index, "key"]:"";
	value = current_zone["records", index, "value"]:"";
    }
    UI::ChangeWidget (`key, `Value, key);
    UI::ChangeWidget (`value, `Value, value);
    UI::SetFocus (`key);
}

/**
 * Fallback store function of a table entry / popup
 * @param opt_id any option id
 * @param opt_key any option key
 */
global define void masterPopupStore (any opt_id, string opt_key) ``{
    integer index = (integer)opt_id;
    string key = (string)UI::QueryWidget (`key, `Value);
    string value = (string)UI::QueryWidget (`value, `Value);
    if (index != nil)
    {
	current_zone_upd_ops = add (current_zone_upd_ops, $[
	    "operation" : "delete",
	    "type" : opt_key,
	    "key" : current_zone["records", index, "key"]:"",
	    "value" : current_zone["records", index, "value"]:"",
	]);
	current_zone["records", index, "value"] = value;
	current_zone["records", index, "key"] = key;
    }
    else
    {
	map new_rec = $[
	    "key" : key,
	    "value" : value,
	    "type" : opt_key,
	];
	current_zone["records"] = add (current_zone["records"]:[], new_rec);
    }
    current_zone_upd_ops = add (current_zone_upd_ops, $[
	"operation" : "add",
	"type" : opt_key,
	"key" : key,
	"value" : value,
    ]);
}

/**
 * Fallback summary function of a table entry / popup
 * @param opt_id any option id
 * @param opt_key any option key
 * @return string table entry summary
 */
global define string masterTableEntrySummary (any opt_id, string opt_key)``{
    integer index = (integer)opt_id;
    string addr = current_zone["records", index, "value"]:"A";
    // %1 is usually an IP address
    return sformat (_("Unknown Record Type: %1"), addr);
}

/**
 * Fallback function for determining the first column of the table
 * in order not to depend on the option key
 * @param opt_id any option id
 * @param opt_key any option key
 * @return string the table entry
 */
global define string masterTableLabelFunc (any opt_id, string opt_key) ``{
    integer index = (integer)opt_id;
    return current_zone["records", index, "key"]:"";
}

// A popup

global define string ASummary (any opt_id, string key) ``{
    integer index = (integer)opt_id;
    string addr = current_zone["records", index, "value"]:"A";
    // table entry, %1 is IP address
    return sformat (_("Host %1"), addr);
}

define map getAPopup () ``{
    return $[
	"table" : $[
	    "summary" : ASummary,
	    // combo box item, A is more technical description
	    "label" : _("A -- Domain Name Translation"),
	],
	"popup" : $[
	    "widget" : `custom,
	    "custom_widget" :`HBox (`HSpacing (1), `VBox (
		`HSpacing (50),
		`TextEntry (`id (`key), Label::HostName()),
		`VSpacing (2),
		// text entry
		`TextEntry (`id (`value), _("&IP Addresses")),
		`VSpacing (1)
	    ), `HSpacing (1)),
	],
    ];
}


// CNAME popup

global define string CNAMESummary (any opt_id, string key) ``{
    integer index = (integer)opt_id;
    string addr = current_zone["records", index, "value"]:"A";
    // table entry, %1 is host name
    return sformat (_("Alias for %1"), addr);
}

define map getCNAMEPopup () {
    return $[
	"table" : $[
	    "summary" : CNAMESummary,
	    // combo box item, CNAME is more technical description
	    "label" : _("CNAME -- Alias for Domain Name"),
	],
	"popup" : $[
	    "widget" : `custom,
	    "custom_widget" : `HBox (`HSpacing (1), `VBox (
		`HSpacing (30),
		// text entry
		`TextEntry (`id (`key), _("&Alias")),
		`VSpacing (1),
		// text entry
		`TextEntry (`id (`value), _("&Base Host Name")),
		`VSpacing (1)
	    ), `HSpacing (1)),
	],
    ];
}


// PTR popup

global define string PTRSummary (any opt_id, string key) ``{
    integer index = (integer)opt_id;
    string addr = current_zone["records", index, "value"]:"A";
    // table entry, %1 is host name
    return sformat (_("Pointer to %1"), addr);
}

define map getPTRPopup () ``{
    return $[
	"table" : $[
	    "summary" : PTRSummary,
	    // combo box item, PTR is more technical description
	    "label" : _("PTR -- Reverse Translation"),
	],
	"popup" : $[
	    "widget" : `custom,
	    "custom_widget" : `HBox (`HSpacing (1), `VBox (
		`HSpacing (30),
		// text entry
		`TextEntry (`id (`key), _("&IP Address")),
		`VSpacing (1),
		// text entry
		`TextEntry (`id (`value), Label::HostName()),
		`VSpacing (1)
	    ), `HSpacing (1)),
	],
    ];
}



global define string NSSummary (any opt_id, string key) ``{
    integer index = (integer)opt_id;
    string addr = current_zone["records", index, "value"]:"A";
    // table entry, %1 is host name
    return sformat (_("Name Server %1"), addr);
}

define map getNSPopup () ``{
    return $[
	"table" : $[
	    "summary" : NSSummary,
	    // combo box item, NS is more technical description
	    "label" : _("NS -- Name Server"),
	],
	"popup" : $[
	    "widget" : `custom,
	    "custom_widget" : `HBox (`HSpacing (1), `VBox (
		`HSpacing (30),
		// text entry
		`TextEntry (`id (`key), _("&Domain")),
		`VSpacing (1),
		// text entry
		`TextEntry (`id (`value), _("&Name Server")),
		`VSpacing (1)
	    ), `HSpacing (1)),
	],
    ];
}

global define string MXSummary (any opt_id, string key) ``{
    integer index = (integer)opt_id;
    string addr = current_zone["records", index, "value"]:"A";
    list<string> l = splitstring (addr, " ");
    l = filter (string s, l, ``(s != ""));
    string prio = l[0]:"";
    l[0] = "";
    l = filter (string s, l, ``(s != ""));
    addr = mergestring (l, " ");
    // table entry, %1 is host name, %2 is integer
    return sformat (_("Mail Relay %1, Priority %2"), addr, prio);
}

global define void MXInit (any opt_id, string key) ``{
    integer index = (integer)opt_id;
    string key = "";
    string value = "";
    if (index != nil)
    {
	key = current_zone["records", index, "key"]:"";
	value = current_zone["records", index, "value"]:"";
    }
    list<string> l = splitstring (value, " ");
    l = filter (string s, l, ``(s != ""));
    string prio = l[0]:"";
    l[0] = "";
    l = filter (string s, l, ``(s != ""));
    value = mergestring (l, " ");
    UI::ChangeWidget (`key, `Value, key);
    UI::ChangeWidget (`value, `Value, value);
    UI::ChangeWidget (`prio, `Value, tointeger (prio));
    UI::SetFocus (`key);
}

global define void MXStore (any opt_id, string opt_key) ``{
    integer index = (integer)opt_id;
    string key = (string)UI::QueryWidget (`key, `Value);
    string value = (string)UI::QueryWidget (`value, `Value);
    integer prio = (integer)UI::QueryWidget (`prio, `Value);
    value = sformat ("%1 %2", prio, value);
    if (index != nil)
    {
	current_zone_upd_ops = add (current_zone_upd_ops, $[
	    "operation" : "delete",
	    "type" : opt_key,
	    "key" : current_zone["records", index, "key"]:"",
	    "value" : current_zone["records", index, "value"]:"",
	]);
	current_zone["records", index, "value"] = value;
	current_zone["records", index, "key"] = key;
    }
    else
    {
	map new_rec = $[
	    "key" : key,
	    "value" : value,
	    "type" : opt_key,
	];
	current_zone["records"] = add (current_zone["records"]:[], new_rec);
    }
    current_zone_upd_ops = add (current_zone_upd_ops, $[
	"operation" : "add",
	"type" : opt_key,
	"key" : key,
	"value" : value,
    ]);
}

define map getMXPopup () ``{
    return $[
	"table" : $[
	    "summary" : MXSummary,
	    // combo box item, MX is more technical description
	    "label" : _("MX -- Mail Relay"),
	],
	"popup" : $[
	    "init" : MXInit,
	    "store" : MXStore,
	    "widget" : `custom,
	    "custom_widget" : `HBox (`HSpacing (1), `VBox (
		`HSpacing (30),
		// text entry
		`TextEntry (`id (`key), _("&Domain Name")),
		`VSpacing (1),
		// text entry
		`TextEntry (`id (`value), _("&Mail Relay")),
		`VSpacing (1),
		// int field
		`IntField (`id (`prio), _("&Priority"), 0, 100, 0),
		`VSpacing (1)
	    ), `HSpacing (1)),
	],
    ];
}

/**
 * Initialize all poups
 * @return map description of all popups/options
 */
global define map InitPopups () ``{
    return $[
	"A" : getAPopup (),
	"CNAME" : getCNAMEPopup (),
	"PTR" : getPTRPopup (),
	"NS" : getNSPopup (),
	"MX" : getMXPopup (),

    ];
}

// EOF

}
