# -*- coding: iso-8859-1 -*-
# -----------------------------------------------------------------------
# config.py - a configuration plugin
# -----------------------------------------------------------------------
# Freevo - A Home Theater PC framework
# Copyright (C) 2003 Krister Lagerstrom, et al. 
# Please see the file freevo/Docs/CREDITS for a complete list of authors.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MER-
# CHANTABILITY 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, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# ----------------------------------------------------------------------- */


#python modules
import os, time, stat, re, copy

# rdf modules
from xml.dom.ext.reader import Sax2
import urllib

#freevo modules
import config, menu, plugin, util
import gui
from gui import InputBox
from item import Item
from config import rtXML

from mainmenu import MainMenuItem
from application import MenuApplication

import logging
log = logging.getLogger()


class PluginInterface(plugin.MainMenuPlugin):
    """
    A plugin to change the runtime configuration of Freevo

    To activate, put the following line in local_conf.py:

    plugin.activate('configuration') 

    """
    # make an init func that creates the cache dir if it don't exist
    def __init__(self):
        plugin.MainMenuPlugin.__init__(self)


    def items(self, parent):
        return [ ConfigMainMenuItem(parent) ]


class ConfigurationItem(Item):
    """
    Item for the menu for one configuration item
    """
    def __init__(self, parent):
        self.full_path = []
        self.leaf_name = ''
        self.is_var = False
        self.config_value = ''
        self.var_name = ''
        self.var_type = ''
        self.options = {}

        Item.__init__(self, parent)
            

    def set_path(self, base, ext):
        self.base_path = base[:]
        self.full_path = base[:]
        self.full_path.append(ext)
        
        
    def mod_string(self, arg=None, menuw=None):
        self.menuw = menuw
        InputBox(text=_('Change value:'), 
            start_value=rtXML.get_value(self.base_path, self.var_name), 
            handler=self.alter_string).show()


    def mod_integer(self, arg=None, menuw=None):
        self.menuw = menuw
        InputBox(text=_('Change value:'), type='integer', 
            start_value=rtXML.get_value(self.base_path, self.var_name), 
            increment=self.options['increment'],
            min_int=self.options['min_int'],
            max_int=self.options['max_int'],
            handler=self.alter_string).show()


    def alter_string(self, string=None):
        if string:
            if rtXML.set_value(self.base_path, self.var_name, string):
                return None
            else:
                return "Cannot set variable"


    def edit_var(self, arg=None, menuw=None):
        if self.var_type == 'string':
            self.mod_string(arg, menuw)
        elif self.var_type == 'integer':
            self.mod_integer(arg, menuw)


    def actions(self):
        """
        follow this option, show sub-options or edit the value
        """
        if not self.is_var:
            self.parent.path = self.full_path
            return [ ( self.parent.create_config_menu , _('Show options' )) ]
        else:
            return [ ( self.edit_var, self.name ) ]
            
            


class ConfigMainMenuItem(MainMenuItem):
    """
    This is the item for the main menu and creates the list
    of Headlines Sites in a submenu.
    """
    def __init__(self, parent):
        self.path = []
        MainMenuItem.__init__( self, _('Configuration'), type='main',
                               parent=parent)


    def actions(self):
        """
        return a list of actions for this item
        """
        items = [ ( self.create_config_menu , _('Show options' )) ]
        return items
 
 
    def create_config_menu(self, arg=None, menuw=None):
        configuration_values = []

        items = rtXML.get_items( self.path )
        
        if items != False and len(items) > 0:
            for item in items:
                configuration_item = ConfigurationItem(self)
                
                configuration_item.name = item[0]
                configuration_item.leaf_name = item[1]
                configuration_item.is_var = item[1] == 'var'
                configuration_item.var_name = item[2]
                configuration_item.config_value = item[3]
                configuration_item.var_type = item[4]
                configuration_item.options = item[5]
                
                configuration_item.set_path(self.path, item[1])
                
                configuration_values += [ configuration_item ]
        
        if (len(configuration_values) == 0):
            configuration_values += [menu.MenuItem(_('No configuration values'),
                                              menuw.back_one_menu, 0)]

        configuration_menu = menu.Menu(_('Configuration'), configuration_values)
        menuw.pushmenu(configuration_menu)
        
        menuw.refresh()

        

    