/* ------------------------------------------------------------------------------
 * Copyright (c) 2006-2012 Novell, Inc. All Rights Reserved.
 *
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of version 2 of the GNU General Public License as published by the
 * Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, contact Novell, Inc.
 *
 * To contact Novell about this file by physical or electronic mail, you may find
 * current contact information at www.novell.com.
 * ------------------------------------------------------------------------------
 */

/**
 * File:	installation/general/inst_relase_notes.ycp
 * Module:	Installation
 * Summary:	Display release notes
 * Authors:	Arvin Schnell <arvin@suse.de>
 *
 * Display release notes.
 *
 * $Id$
 */

{
    textdomain "installation";

    import "Language";
    import "Report";
    import "Label";
    import "Stage";
    import "Packages";
    import "Mode";

    /* filename of release notes */
    string file = "";

    /* release notes */
    string text = "";


// FIXME similar function in packager/include/load_release_notes.ycp

    /* function to load release notes */
    define boolean load_release_notes ()
    {
        string path_to_relnotes = "/docu";
	integer source_id = 0;
	if (Stage::initial ())
	{
	    source_id = Packages::theSources[0]:0;
	}
	else
	{
	    list<integer> sources = Pkg::SourceStartCache (true);
	    source_id = sources[0]:0;
	}
	string path_templ = path_to_relnotes + "/RELEASE-NOTES.%1.rtf";
	y2debug ("Path template: %1", path_templ);
	string tmp = sformat (path_templ, Language::language);
	y2debug ("Trying to get %1", tmp);
	tmp = Pkg::SourceProvideDigestedFile (source_id, 1, tmp, false);
	if (tmp == nil)
	{
	    tmp = sformat (path_templ, splitstring (Language::language, "_")[0]:"");
	    y2debug ("Trying to get %1", tmp);
	    tmp = Pkg::SourceProvideDigestedFile (source_id, 1, tmp, false);
	}
	if (tmp == nil)
	{
	    tmp = sformat (path_templ, "en");
	    y2debug ("Trying to get %1", tmp);
	    tmp = Pkg::SourceProvideDigestedFile (source_id, 1, tmp, false);
	}
	if (tmp == nil)
	    return false;

	text = (string)SCR::Read (.target.string, [tmp, ""]);
	if (text != "" && text != nil)
	    return true;
	return false;
    };

    // in live installation, the release notes are in the /usr/doc directory, find right file there (bug 332862)
    define boolean find_release_notes ()
    {
	y2milestone ("Finding release notes in local filesystem");
	// FIXME hardcoded product name
        string path_to_relnotes = "/usr/share/doc/release-notes/openSUSE/";
	string path_templ = path_to_relnotes + "/RELEASE-NOTES.%1.rtf";
	y2debug ("Path template: %1", path_templ);
	string tmp = sformat (path_templ, Language::language);
	y2debug ("Trying to get %1", tmp);
	if (0 >= (integer)SCR::Read (.target.size, tmp))
	{
	    tmp = sformat (path_templ, splitstring (Language::language, "_")[0]:"");
	    y2debug ("Trying to get %1", tmp);
	}
	if (0 >= (integer)SCR::Read (.target.size, tmp))
	{
	    tmp = sformat (path_templ, "en");
	    y2debug ("Trying to get %1", tmp);
	}
	if (0 >= (integer)SCR::Read (.target.size, tmp))
	    return false;

	y2milestone ("Reading file %1", tmp);
	text = (string)SCR::Read (.target.string, [tmp, ""]);
	if (text != "" && text != nil)
	    return true;
	return false;
    }

    y2milestone ("Calling: Release Notes Popup");

    if (! (Mode::live_installation ()
	? find_release_notes ()
	: load_release_notes ()
    ))
    {
	// error report
	Report::Error (_("Cannot load release notes."));
	return nil;
    }

    text =
// beginning of the rich text with the release notes
_("<p><b>These are the release notes for the initial release. They are
part of the installation media. During installation, if a connection
to the Internet is available, you can download updated release notes
from the SUSE Linux Web server.</b></p>
") + text;

    // bugzilla #221222, #213239
    map display_info = UI::GetDisplayInfo();
    integer min_size_x = 76;
    integer min_size_y = 22;
    
    // textmode
    if (display_info["TextMode"]:true) {
	min_size_x = tointeger(display_info["Width"]:80)  * 3 / 4;
	min_size_y = tointeger(display_info["Height"]:25) * 2 / 3;
	if (min_size_x < 76) min_size_x = 76;
	if (min_size_y < 22) min_size_y = 22;
	y2milestone("X/x Y/y %1/%2 %3/%4",
	    display_info["Width"]:80, min_size_x,
	    display_info["Height"]:25, min_size_y);
    // GUI
    } else {
	min_size_x = 100;
	min_size_y = 30;
    }

    term contents = `MinSize (
	min_size_x, min_size_y,
	`VBox (
	    `VSpacing (0.5),
	    `Left (`Heading (_("Release Notes"))),
	    `RichText (`id (`text), text),
	    `VSpacing (0.5),
	    `ButtonBox (
		`PushButton (`id (`close), `opt (`okButton, `key_F9), Label::CloseButton ())
	    ),
	    `VSpacing (0.5)
	)
    );

    UI::OpenDialog (contents);
    contents = nil;
    UI::SetFocus (`close);

    // FIXME: richtext eats return key, but only in NCurses and we want to
    // make users read release notes (and make PgDn work). For Next, F10 is
    // availbale
    UI::SetFocus (`id (`text));

    any ret = nil;
    repeat {
	ret = UI::UserInput();
    } until ( ret == `close || ret == `back );
    UI::CloseDialog ();

    y2milestone ("Finishing: Release Notes Popup");

    return ret;
}
