/**
 * File:	include/users/wizards.ycp
 * Package:	Configuration of users and groups
 * Summary:	Wizards definitions
 * Authors:	Johannes Buchhold <jbuch@suse.de>,
 *          Jiri Suchomel <jsuchome@suse.cz>
 *
 * $Id: wizards.ycp 21039 2005-01-28 13:38:59Z jsuchome $
 */

{

textdomain "users";

import "CWM";
import "Label";
import "Ldap";
import "Mode";
import "Popup";
import "Sequencer";
import "Stage";
import "UsersCache";
import "Users";
import "Wizard";

include "users/complex.ycp";
include "users/dialogs.ycp";
include "users/widgets.ycp";

// ------------------ CWM ---------------------------------------------------
// --------------------------------------------------------------------------

define boolean ReallyBack () ``{
    if (!Stage::cont () && Users::Modified())
	return Popup::ReallyAbort (Users::Modified());
    else return true;
}

/**
 * Before showing the table for first time,
 * read LDAP/NIS if they are included in custom set
 */
define symbol InitializeTableItems () ``{

    if (UsersCache::CustomizedUsersView () ||
	UsersCache::CustomizedGroupsView ())
    {
	if ((contains (Users::GetUserCustomSets (), "ldap") ||
	     contains (Users::GetGroupCustomSets (), "ldap")) &&
	    Ldap::bind_pass == nil)
	{
	    Ldap::SetBindPassword (Ldap::GetLDAPPassword (true));
	}
	if (UsersCache::CustomizedUsersView ())
	    Users::ChangeCurrentUsers ("custom");
	if (UsersCache::CustomizedGroupsView ())
	    Users::ChangeCurrentGroups ("custom");
    }
    return `next;
}

/**
 * run the main (Summary) dialog via CWM
 */
define symbol SummaryDialog() {

    term contents = `VBox (
	`HBox (`Left ("rb_users_groups")),
        "table",
	"advanced_buttons"
    );

    any ret = CWM::ShowAndRun ($[
	"widget_names"	: ["rb_users_groups", "table", "advanced_buttons" ],
	"widget_descr"	: widgets,
	"contents"	: contents,
	// dialog caption
	"caption"	: _("User and Group Administration"),
	"back_button"	: Label::BackButton (),
	"next_button"	: Stage::cont () ?
	    Label::NextButton() : Label::FinishButton (),
	"fallback_functions"	: $[
	    `back: ReallyBack
	],
    ]);

    if (ret != nil &&
	is (ret,symbol) && contains ([`new, `edit, `delete], (symbol) ret))
    {
	map update_symbol = $[
	    `new: $[
		"users" : `new_user,
		"groups": `new_group
	    ],
	    `edit: $[
		"users" : `edit_user,
		"groups": `edit_group
	    ],
	    `delete: $[
		"users" : `delete_user,
		"groups": `delete_group
	    ]
	];
	ret = update_symbol [ret, UsersCache::GetCurrentSummary ()]:`back;
    }

    if (Stage::cont () && (ret == `back || ret == `abort))
    {
	Users::SetStartDialog ("user_add");
	Users::SetUseNextTime (true);
	boolean old_gui = Users::GetGUI ();
	Users::SetGUI (false);
	Users::Read ();
	// read also LDAP again: bug #41299
	list<string> current_users	= Users::GetCurrentUsers();
	if (contains (current_users, "ldap"))
	{
	    Users::SetLDAPNotRead (true);
	    Users::ChangeCurrentUsers ("ldap");
	}
	else
	{
	    // update the user item list
	    Users::ChangeCurrentUsers ("custom");
	}
	Users::SetGUI (old_gui);
	ret	= `back;
    }

    return (symbol) ret;
}


// --------------------------------------------------------------------------
// --------------------------------------------------------------------------


/**
 * Main workflow of the users/groups configuration
 * @param start the first dialog
 * @return sequence result
 */
define symbol MainSequence(string start) {

    map aliases = $[
	"init_summary"		: [``(InitializeTableItems ()), true ],
	"summary"		: ``(SummaryDialog ()),
	"user_add"		: ``(EditUserDialog("add_user")),
	"user_add_inst"		: ``(EditUserDialog("add_user")),
	"user_inst_start"	: [``(usersInstStart ()), true],
	"user_edit"		: ``(EditUserDialog("edit_user")),
	"user_save"		: [``(UserSave()), true],

	"group_add"		: ``(EditGroupDialog("add_group")),
	"group_edit"		: ``(EditGroupDialog("edit_group")),
	"group_save"		: [``(GroupSave()), true],

	"defaults"		: ``(DefaultsDialog()),
	"auth"			: ``(AuthentizationDialog()),
	"without_save"		: ``(ReallyAbort()),
    ];

    map main_sequence = $[
	"ws_start"		: "init_summary",
	"init_summary"		: $[
	    `next		: start
	],
	"summary":$[
	    `new_user		: "user_add",
	    `edit_user		: "user_edit",
	    `delete_user	: "user_save",
	    `new_group		: "group_add",
	    `edit_group		: "group_edit",
            `delete_group	: "group_save",
            `defaults		: "defaults",
	    `auth		: "auth",
	    `abort		: "without_save",
	    `cancel		: "without_save",
	    `next		: `next,
            `nosave		: `nosave,
	    `exit		: `abort,
	    `summary		: "summary"
	],
	"user_add": $[
            `nextmodule		: `next,
	    `nosave		: "summary",
	    `additional		: "user_save", // only install
	    `next		: "user_save",  // -> Commit
	    `abort		: `abort,
	    `cancel		: `abort,
	],
	"user_add_inst": $[
            `nextmodule		: `next, // no user and next pressed (install)
            `nosave		: "summary",
            `additional		: "user_save",
            `next		: "user_save",
            `abort		: "without_save",
            `cancel		: "without_save",
	],
	"user_edit": $[
            `next		: "user_save",
	    `abort		: `abort,
	    `cancel		: `abort,
	],
	"user_inst_start": $[
            `next		: "user_add_inst"
	],
	"user_save": $[
            `next		: "summary",
	    `save		: `next//this should be write - during install??
        ],
	"group_add": $[
	    `nosave		: "summary",
            `next		: "group_save",
	    `abort		: `abort,
	    `cancel		: `abort,
	],
        "group_edit": $[
            `next		: "group_save",
	    `abort		: `abort,
	    `cancel		: `abort,
	],
        "group_save"  : $[
            `next		: "summary"
	],
        "defaults": $[
	    `abort		: "without_save",
            `next		: "summary"
	],
        "auth": $[
	    `abort		: "without_save",
            `next		: "summary"
	],
        "without_save": $[
            `next		: `next,
	    `abort		: `abort,
	    `back		: `back
	],
    ];

    return Sequencer::Run (aliases, main_sequence);
}

/**
 * Whole configuration of users/groups
 * @param start the first dialog
 * @return sequence result
 */
define symbol UsersSequence(string start) ``{

    map aliases = $[
	"read"  : [ ``( ReadDialog(!Stage::cont ())), true ],
	"main"	:   ``( MainSequence( start ) ),
	"write" : [ ``( WriteDialog( true )), true ]// true as parameter ??
    ];

    map sequence = $[
	"ws_start" : "read",
	"read" : $[
	    `abort	: `abort,
	    `next	: "main",
        `nextmodule: `next // this is for skiping users conf during install,
                           // see Users::CheckHomeMounted() or bug #20365
	],
	"main" : $[
	    `abort	: `abort,
	    `next	: "write",
	    `nosave : `next
	],
	"write" : $[
	    `abort	: `abort,
	    `next	: `next
	]
    ];

    string caption = _("User and Group Configuration");
    term contents = `Label(_("Initializing ..."));

    if( ! Stage::cont () )
    {
	Wizard::CreateDialog();
    }
    Wizard::SetDesktopIcon("users");

    any ret = Sequencer::Run (aliases, sequence);

    if( ! Stage::cont () )
	UI::CloseDialog();

    // read LDAP again in the next run
    Users::SetLDAPNotRead (true);

    return (symbol) ret;
}

/**
 * Whole configuration of users/groups but without reading and writing.
 * For use with autoinstallation.
 * @param start the first dialog
 * @return sequence result
 */
define symbol AutoSequence(string start) {
    string caption = _("User and Group Configuration");
    term contents = `Label(_("Initializing ..."));

    Wizard::CreateDialog();
    Wizard::SetDesktopIcon("users");
    Wizard::SetContentsButtons(caption, contents, "",
	    Label::BackButton(), Label::NextButton());

    // initialization: ---------------- (simulate empty Import: bug #44660)
    if (Mode::config () && !Users::Modified ())
    {
	Users::Initialize ();
    }
    // --------------------------------

    symbol ret = MainSequence (start);

    UI::CloseDialog();
    return ret;
}

/* EOF */
}
