/*
 * view_anymsg.ycp
 *
 * small script for easy /var/log/* and /proc/* viewing
 *
 * Author: Klaus Kaempf <kkaempf@suse.de>
 *
 * $Id: view_anymsg.ycp 20756 2005-01-13 14:22:43Z jsuchome $
 *
 * Reads a \n separated list of filenames from
 * /var/lib/YaST2/filenames
 * Lines starting with "#" are ignored (comments)
 * A line starting with "*" is taken as the default filename, the "*" is stripped
 *
 * All files are listed in an editable combo box, where the user can
 * easily switch between files and even add a new file
 *
 * At finish, the list of filenames is written back to
 * /var/lib/YaST2/filenames
 * adapting the default line (starting with "*") accordingly.
 *
 * The default is either given as WFM::Args(0) or is the file last viewed.
 */

{
    textdomain "repair";

    import "Directory";
    import "FileUtils";
    import "Label";

    string vardir = Directory::vardir;

    // Check if the filename list is present
    if (!FileUtils::Exists (vardir + "/filenames"))
    {
        SCR::Execute(.target.bash, "/bin/cp " + Directory::ydatadir + "/filenames " + vardir + "/filenames");
    }

    // get filename list
    string filenames = (string) SCR::Read(.target.string, vardir + "/filenames");
    if ((filenames == nil)
	|| (size (filenames) <= 0))
    {
	filenames = "/var/log/boot.msg\n/var/log/messages\n";
    }

    // convert \n separated string to ycp list.

    list<string> all_files = splitstring (filenames, "\n");

    // check if default given as argument

    string filename = "";
    if ((size(WFM::Args()) > 0)
	&& is(WFM::Args(0), string))
    {
	filename = (string)WFM::Args(0);
    }


    // build up ComboBox

    boolean set_default = false;

    list<term> combo_files = [];
    foreach (string name, all_files,
    {
	// empty lines or lines starting with "#" are ignored
	if (name != ""
	    && substring (name, 0, 1) != "#")
	{
	    // the default is either given via WFM::Args() -> filename != ""
	    // or by a filename starting with "*"
	    if (!set_default
		&& (filename != ""))
	    {
		combo_files = add (combo_files, `item(`id(filename), filename));
		set_default = true;
	    }
	    else if (!set_default
		     && (substring (name, 0, 1) == "*"))
	    {
		name = substring (name, 1);	// strip leading "*"
		if (filename == "")
		    filename = name;
		combo_files = add (combo_files, `item(`id(name), name, true));
		set_default = true;
	    }
	    else
	    {
		combo_files = add (combo_files, `item(`id(name), name));
	    }
	}
    });

    if (!set_default
	&& (filename != ""))
    {
	all_files = add (all_files, "*" + filename);
	combo_files = add (combo_files, `item(`id(filename), filename));
    }

    // set up dialogue

    UI::OpenDialog( `opt(`decorated, `defaultsize ),
		      `VBox(
			    `HSpacing( 70 ),	// force width
			    `HBox (`HSpacing (1.0), `ComboBox (`id(`custom_file), `opt(`editable, `notify, `hstretch), "", combo_files), `HStretch()),
			    `VSpacing( 0.3 ),
			    `VWeight( 1,
				      `HBox(
					    `VSpacing( 18 ),	// force height
					    `HSpacing( 0.7 ),
					    `LogView( `id(`log ),
						      "",
						      3,	// height
						      0 ),	// number of lines to show
					    `HSpacing( 0.7 )
					    )
				      ),
			    `VSpacing( 0.3 ),
			    `PushButton( `id(`ok), Label::OKButton() ),
			    `VSpacing( 0.3 )
			    )
		      );


    string file_contents = "";
    boolean go_on = true;

    // wait until user clicks "OK"
    // check if ComboBox selected and change view accordingly

    while (go_on)
    {
	// read file contents
	file_contents = (string)SCR::Read (.target.string, filename);

	// Fill the LogView with file contents
	UI::ChangeWidget( `id(`log ), `Value, file_contents );

	string heading = sformat( _("System log (%1)"), filename );
	UI::ChangeWidget( `id(`log ), `Label, heading);

	// wait for user input

	symbol ret = (symbol)UI::UserInput();

	// clicked "OK" -> exit

	if (ret == `ok)
	{
	    go_on = false;
	}
	else if (ret == `cancel)		// close window
	{
	    UI::CloseDialog();
	    return true;
	}
	else if (ret == `custom_file)
	{
	    // adapt to combo box settings

	    string new_file = (string)UI::QueryWidget(`id(`custom_file), `Value);
	    if (new_file != nil)
	    {
		filename = new_file;
	    }
	}
	else
	{
	    y2milestone ("bad UserInput (%1)", ret);
	}
    }

    // write new list of filenames

    list<string> new_files = [];
    set_default = false;

    // re-build list to get new default correct
    foreach (string file, all_files,
    {
	if (substring (file, 0, 1) == "*")
	{
	    string old_default = substring (file, 1);  // strip leading "*"
	    if (old_default == filename)	// default unchanged
	    {
		new_files = add (new_files, file);
		set_default = true;
	    }
	    else				// new default
	    {
		new_files = add (new_files, old_default);
	    }
	}
	else if (file != "")
	{
	    if (file == filename)		// mark new default
	    {
		new_files = add (new_files, "*" + filename);
		set_default = true;
	    }
	    else
	    {
		new_files = add (new_files, file);
	    }
	}
    });

    // if we don't have a default by now, it wasn't in the list before
    // so add it here.

    if (!set_default
	&& (filename != ""))
    {
	new_files = add (new_files, "*" + filename);
    }

    new_files = toset (new_files);

    // convert ycp list back to \n separated string

    filenames = mergestring (new_files, "\n") + "\n";

    SCR::Write(.target.string, vardir + "/filenames", filenames);

    UI::CloseDialog();

    return true;
}
