#
# language_text.py: text mode language selection dialog
#
# Copyright 2001-2002 Red Hat, Inc.
#
# This software may be freely redistributed under the terms of the GNU
# library public license.
#
# You should have received a copy of the GNU Library Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

import os
import isys
import iutil
import time
from snack import *
from constants_text import *
from flags import flags

from rhpl.log import *
from rhpl.translate import _

class LanguageWindow:
    def __call__(self, screen, textInterface, instLanguage):
        languages = instLanguage.available ()

        current = instLanguage.getCurrent()

        height = min((8, len(languages)))
	buttons = [TEXT_OK_BUTTON, TEXT_BACK_BUTTON]

        translated = []
        for lang in languages:
            translated.append (_(lang))
        (button, choice) = \
            ListboxChoiceWindow(screen, _("Language Selection"),
			_("What language would you like to use during the "
			  "installation process?"), translated, 
			buttons, width = 30, default = _(current), scroll = 1,
                                height = height, help = "lang")

        if button == TEXT_BACK_CHECK:
            return INSTALL_BACK

        choice = languages[choice]
        
        if ((instLanguage.getFontFile(choice) == "None")):
            ButtonChoiceWindow(screen, "Language Unavailable",
                               "%s display is unavailable in text mode.  The "
                               "installation will continue in English." % (choice,),
                               buttons=[TEXT_OK_BUTTON])
            instLanguage.setRuntimeDefaults(choice)
            return INSTALL_OK
            
        if (flags.setupFilesystems and
            instLanguage.getFontFile(choice) == "bterm"
            and not flags.serial and not flags.virtpconsole
            and not isys.isPsudoTTY(0)
            and not isys.isVioConsole()):
            # bterm to the rescue...  have to shut down the screen and
            # create a new one, though (and do a sleep)
            log("starting bterm")
            try:
                screen.finish()
                rc = isys.startBterm()
                time.sleep(1)
            except Exception, e:
                log("got an exception starting bterm: %s" %(e,))
                rc = 1
            newscreen = SnackScreen()
            textInterface.setScreen(newscreen)
            screen = newscreen

            if rc == 1:
                ButtonChoiceWindow(screen, "Language Unavailable",
                                   "%s display is unavailable in text mode.  "
                                   "The installation will continue in "
                                   "English." % (choice,),
                                   buttons=[TEXT_OK_BUTTON])
                instLanguage.setRuntimeDefaults(choice)
                return INSTALL_OK
                

	instLanguage.setRuntimeLanguage(choice)
                
	textInterface.drawFrame()
	    
        return INSTALL_OK

class LanguageSupportWindow:
    def __call__(self, screen, language):
        langs = []
        default = language.getDefault()
        langs.append(default)
        language.setSupported(langs)
        language.setDefault(default)
        return INSTALL_OK

class LanguageDefaultWindow:
    def __call__(self,screen, language):
        langs = language.getSupported ()
	current = language.getDefault()

        if not langs or len(langs) <= 1:
	    language.setDefault(current)
            return INSTALL_NOOP

	langs.sort()

        height = min((screen.height - 16, len(langs)))
        
        buttons = [TEXT_OK_BUTTON, TEXT_BACK_BUTTON]

        (button, choice) = ListboxChoiceWindow(screen, _("Default Language"),
			_("Choose the default language for this system: "), langs, 
			buttons, width = 30, default = current, scroll = 1,
                                               height = height, help = "langdefault")

	if (button == TEXT_BACK_CHECK):
            return INSTALL_BACK

        language.setDefault (langs[choice])
        return INSTALL_OK

