/**
 * File:	include/phone-services/fax.ycp
 * Package:	Communication
 * Summary:     Fax machine dialogs
 * Authors:	Karsten Keil <kkeil@suse.de>
 *
 * $Id: fax.ycp 20711 2005-01-11 15:34:25Z jsuchome $
 *
 *
 */

{

textdomain "phone-services";

import "Fax";
import "Wizard";
import "Label";
import "Popup";

define map Fax_UserEditDialog(string uname, map uconf);

/**
 * Main Fax machine dialog
 * @param standalone true if not run from another ycp client
 * @return dialog result
 */
define any FaxMainDialog() ``{

    /* dialog caption */
    string caption = _("Fax Machine Configuration");

    /* some variables */

    term theader = `header(
	/* Table header 1/6 */
	_("User"),
	/* Table header 2/6 */
	_("Fax Numbers"),
	/* Table header 3/6 */
	_("MSN"),
	/* Table header 4/6 */
	_("Action"),
	/* Table header 5/6 */
	_("StationID"),
	/* Table header 6/6 */
	_("Headline")
    );

    /* Fax dialog general help 1/3 */
    string help = _("<p>The fax system for one or more users can be
set up in this dialog. Each user must have at least one unique fax
number configured. Refer to the telecommunication chapter in the
manuals for further details.</p>
") +

    /* Fax dialog general help 2/3 */
_("<p><b>Prefix</b>: Only for users behind a PBX. Enter the
prefix number for getting a public line. This number will be dialed
before any destination number.</p>
") +

    /* Fax dialog general help 3/3 */
_("<p>When adding or editing a user, a dialog will be shown with
the following fields:</p>") +

    /* Fax dialog help for options 1/6 */
_("<p><b>User</b>: The system user to which this fax account belongs.</p> \n") +

    /* Fax dialog help for options 2/6 */
_("<p><b>Fax Numbers</b>: The numbers (separated by commas)
on which faxes should be received for this user. If you enter
<tt>*</tt>, the user gets <b>any</b> call. Leave
this empty for a send-only account.</p>
") +

    /* Fax dialog help for options 3/6 */
_("<p><b>Outgoing MSN</b>: The number to use for outgoing calls. If
empty, the first number of <b>Fax Numbers</b> is used.</p>
") +

    /* Fax dialog help for options 4/6 */
_("<p><b>StationID</b>: The fax station ID. Set it to the external
number in international format, such as <tt>+49 89 12345</tt>.</p>
") +

    /* Fax dialog help for options 5/6 */
_("<p><b>Headline</b>: The headline used for sending faxes -- normally
a string containing some name.</p>
") +

    /* Fax dialog help for options 6/6 */
_("<p><b>Action</b>: By using the default <tt>MailAndSave</tt>,
received faxes are sent to the user as mail and saved to disk.
To disable the mails, set this to <tt>SaveOnly</tt>.</p>
");

    integer max = 0;
    integer items = 0;
    list<term> table_items = [];
    map<string,map> userconf = Fax::aconfig;
    string prefix = Fax::gconfig["dial_prefix"]:"";

    /* make ui items from config map */
    term item = nil;
    foreach(string k, map m, userconf, ``{
	item = `item(`id(k),
	    k,
	    m["fax_numbers"]:"",
	    m["outgoing_MSN"]:"",
	    m["fax_action"]:"",
	    m["fax_stationID"]:"",
	    m["fax_headline"]:"");
	table_items = add(table_items, item);
	items = items + 1;
    });

    y2debug("table_items=%1", table_items);
    y2debug("items=%1", items);
    y2debug("userconf=%1", userconf);
    max = items;

    /* main dialog contents */
    term contents = `VBox(
	`VSpacing(2),
	`HBox(
	    `HSpacing(1),
	    `VBox(
		`VSpacing(1),
//		`VStretch(),
		/* Frame title */
		`Frame(_("&User Table"), `VBox(
		    `Table(`id(`table), theader, []),
		    /* PushButton label */
		    `HBox(`PushButton(`id(`add), _("Ad&d")),
			/* PushButton label */
			`PushButton(`id(`edit), `opt(`disabled), _("&Edit")),
			/* PushButton label */
			`PushButton(`id(`delete), `opt(`disabled), _("De&lete"))
		    )
		)),
		`VSpacing(1)
//		`VStretch()
	    )
//	    `HSpacing(1)
	),
	`HBox(
	    `HSpacing(1),
	    /* TextEntry label */
	    `TextEntry(`id(`prefix), `opt(`shrinkable), _("&Prefix"), prefix),
	    `HSpacing(10),
	    `HSpacing(10),
	    `HSpacing(10),
	    `HSpacing(10)
	),
	`VSpacing(2)
    );

    Wizard::SetContentsButtons(caption, contents, help,
	Label::BackButton(), Label::FinishButton());

    UI::ChangeWidget(`id(`table), `Items, table_items);
    UI::SetFocus(`id(`table));

    any ret = nil;
    while(true) {
	boolean new_table = false;
	map cur_item = $[];

	UI::ChangeWidget(`id(`edit), `Enabled, (items > 0));
	UI::ChangeWidget(`id(`delete), `Enabled, (items > 0));

	ret = UI::UserInput();

	/* abort? */
	if(ret == `abort || ret == `cancel) {
	    /* TODO: handle the changed dialog values */
	    if(Popup::ReallyAbort(Fax::modified)) break;
	    else continue;
	}
	/* edit user settings */
	else if(ret == `edit) {
	    string cur = (string) UI::QueryWidget(`id(`table), `CurrentItem);

	    y2debug("cur=%1", cur);

	    cur_item = Fax_UserEditDialog(cur, userconf);
	    y2debug("cur_item=%1", cur_item);
	    if(cur_item == nil)
		continue;
	    else {
		cur = cur_item["ID"]:"";
		if (cur == "")
		    continue;
		cur_item = filter(`k, `v, cur_item, ``(k != "ID"));
		new_table = true;
		userconf = add(userconf, cur, cur_item);
		y2debug("userconf=%1", userconf);
	    }
	}
	/* add user */
	else if(ret == `add) {
	    cur_item = Fax_UserEditDialog("", userconf);
	    if(cur_item == nil)
		continue;
	    else {
		string cur = cur_item["ID"]:"";
		if (cur == "")
		    continue;
		cur_item = filter(`k, `v, cur_item, ``(k != "ID"));
		new_table = true;
		userconf = add(userconf, cur, cur_item);
	    }
	}
	/* delete user */
	else if(ret == `delete) {
	    items = items - 1;
	    string cur = (string) UI::QueryWidget(`id(`table), `CurrentItem);
	    table_items = filter(term e, table_items, ``(cur != e[0,0]:""));
	    userconf = (map<string,map>) filter(`k, `v, userconf, ``(k != cur));
	    new_table = true;
	}
	else if(ret == `back) {
	    break;
	}
	else if(ret == `next) {
	    break;
	}
	else {
	    y2debug("Unexpected retcode: %1", ret);
	    continue;
	}
	if (new_table) {
	    items = 0;
	    table_items = [];
	    foreach(string k, map m, userconf, ``{
		item = `item(`id(k),
		    k,
		    m["fax_numbers"]:"",
		    m["outgoing_MSN"]:"",
		    m["fax_action"]:"",
		    m["fax_stationID"]:"",
		    m["fax_headline"]:"");
		table_items = add(table_items, item);
		items = items + 1;
	    });
	    max = items;
	    UI::ChangeWidget(`id(`table), `Items, table_items);
	    UI::ChangeWidget(`id(`table), `CurrentItem, max);
	    Fax::modified = true;
	}
    };

    /* update settings from widgets */
    if(ret == `next) {
	prefix = (string) UI::QueryWidget(`id(`prefix), `Value);
	if (Fax::gconfig["dial_prefix"]:"" != prefix) {
		Fax::gconfig = add(Fax::gconfig, "dial_prefix", prefix);
		Fax::modified = true;
	}
	if (Fax::aconfig != userconf) {
	    Fax::aconfig = userconf;
	    Fax::modified = true;
	}
    }
    return ret;
}

/**
 * Fax user edit dialog
 * @param uname name of the edited user settings
 * @param uconf map of all userentries
 * @return user settings or nil, if canceled
 */
define map Fax_UserEditDialog(string uname, map uconf) ``{

    map uc = uconf[uname]:$[];

    if (uc == $[]) {
	/* set globals as defaults */
	uc["fax_stationID"] = Fax::gconfig["fax_stationID"]:"";
	uc["fax_headline"] = Fax::gconfig["fax_headline"]:"";
	uc["fax_action"] = Fax::gconfig["fax_action"]:"MailAndSave";
	uc["outgoing_MSN"] = Fax::gconfig["outgoing_MSN"]:"";
    }
    /* add at least the user */
    if (uname != "" && !contains(Fax::users, uname)) {
	Fax::users = add(Fax::users, uname);
	Fax::users = sort(Fax::users);
    }

    y2debug("uc=%1", uc);
    list actions = [ "MailAndSave", "SaveOnly" ];

    UI::OpenDialog(`opt(`decorated),`HBox(
	`HSpacing(2),
	`VBox(
	    /* ComboBox label */
	    `ComboBox(`id(`user), `opt(`editable, `hstretch), _("&User"), Fax::users),
	    `VSpacing(0.5),
	    /* TextEntry label */
	    `TextEntry(`id(`phone), _("&Fax Numbers"), uc["fax_numbers"]:""),
	    `VSpacing(0.5),
	    /* TextEntry label  */
	    `TextEntry(`id(`outgoing), _("Outgoing &MSN"), uc["outgoing_MSN"]:""),
	    `VSpacing(0.5),
	    /* TextEntry label */
	    `TextEntry(`id(`station), _("&StationID"), uc["fax_stationID"]:""),
	    `VSpacing(0.5),
	    /* TextEntry label */
	    `TextEntry(`id(`headline), _("&Headline"), uc["fax_headline"]:""),
	    `VSpacing(0.5),
	    /* ComboBox label */
	    `ComboBox(`id(`action), `opt(`hstretch), _("&Action"), actions),
	    `VSpacing(1),
	    `HBox(
		`PushButton(`id(`ok), `opt(`default), Label::OKButton()),
		`HSpacing(5),
		`PushButton(`id(`cancel), Label::CancelButton())
	    )
	),
	`HSpacing(2)
    ));

    if(uname != "")
	UI::ChangeWidget(`id(`user), `Value, uname);
    UI::ChangeWidget(`id(`phone), `ValidChars, "0123456789#*,");
    UI::ChangeWidget(`id(`station), `ValidChars, "0123456789+ ");
    UI::ChangeWidget(`id(`action), `Value, uc["fax_action"]:"MailAndSave");
    UI::SetFocus(`id(`user));

    any ret = nil;
    string user = "";
    string sval = "";
    string oval = "";

    while(true) {
	ret = UI::UserInput();
	if(ret != `ok) break;

	user = (string) UI::QueryWidget(`id(`user), `Value);
	if ((user == nil) || (user == "")) {
	    /* Popup::Error text */
	    Popup::Error(_("User must be set."));
	    UI::SetFocus(`id(`user));
	    continue;
	}
	uc = add(uc, "ID", user);

	sval = (string) UI::QueryWidget(`id(`phone), `Value);
	oval = (string) UI::QueryWidget(`id(`outgoing), `Value);
        if((sval == nil || sval == "") && (oval == nil || oval == "")) {
            /* Popup::Error text */
            Popup::Error(_("Fax Numbers and Outgoing MSN must not both be empty"));
            UI::SetFocus(`id(`phone));
            continue;
        }
	if(sval != nil)
		uc = add(uc, "fax_numbers", sval);
	if(oval != nil)
		uc = add(uc, "outgoing_MSN", oval);

	sval = (string) UI::QueryWidget(`id(`station), `Value);
	if(sval == "") {
	    /* Popup::Error text */
	    Popup::Error(_("StationID is invalid."));
	    UI::SetFocus(`id(`station));
	    continue;
	} else if(20 < size(sval)) {
	    /* Popup::Error text */
	    Popup::Error(_("The maximum length for a StationID is twenty."));
	    UI::SetFocus(`id(`station));
	    continue;
	}
	uc = add(uc, "fax_stationID", sval);

	sval = (string) UI::QueryWidget(`id(`headline), `Value);
	/* fax_headline "" is OK, default value  */
	if(50 < size(sval)) {
	    /* Popup::Error text */
	    Popup::Error(_("The maximum length for a headline is fifty."));
	    UI::SetFocus(`id(`headline));
	    continue;
	}
	uc = add(uc, "fax_headline", sval);

	sval = (string) UI::QueryWidget(`id(`action), `Value);
	if(sval == nil || sval == "") {
	    /* Popup::Error text */
	    Popup::Error(_("Action is invalid."));
	    UI::SetFocus(`id(`action));
	    continue;
	}
	uc = add(uc, "fax_action", sval);

	break;
    }
    y2debug("ret=%1", ret);
    UI::CloseDialog();
    if(ret != `ok) return nil;
    return uc;
}

/* EOF */
}
