#!/usr/bin/env python
# Copyright (C) 2005 Insecure.Com LLC.
#
# Author: Luis A. Bastiao Silva <luis.kop@gmail.com>
#
# 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
# 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, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

import gtk


from higwidgets.higtables import HIGTable
from higwidgets.higboxes import HIGVBox, HIGHBox
from higwidgets.higscrollers import HIGScrolledWindow 
from higwidgets.higlabels import HIGSectionLabel, HIGEntryLabel
from higwidgets.higentries import HIGTextEntry
from higwidgets.hignotebooks import HIGNotebook
from higwidgets.higbuttons import HIGButton

from umitCore.I18N import _
# Testing at devel 
from os.path import split, join

from umitCore.Paths import Path
Path.set_umit_conf(join(split(__file__)[0], 'config', 'umit.conf'))
#END DEV TEST
options = Path.options

import gobject

from umitGUI.OptionBuilder import OptionBuilder
from umitCore.NmapOptions import  NmapOptions


class OptionDisplay(HIGTable):
    def __init__(self, option=None):
        HIGTable.__init__(self)
        self.create_and_attach_widgets()
        self.set_border_width(5)
    def create_and_attach_widgets(self):
        self.option_label = HIGSectionLabel('New Option')
	self.option_label.set_width_chars(10)
	self.attach(self.option_label, 0, 1, 0, 1)

	self.name_label = HIGEntryLabel(_('Name:'))
        self.name_entry = HIGTextEntry()
        self.attach(self.name_label, 0,1,1,2)
        self.attach(self.name_entry, 1,2,1,2)
        
        self.hint_label = HIGEntryLabel(_('Hint:'))
        self.hint_entry = HIGTextEntry()
        self.attach(self.hint_label, 0,1,2,3)
        self.attach(self.hint_entry,1,2,2,3)
        
        self.aguments_label = HIGEntryLabel(_('Arguments:'))
        self.arguments_entry = HIGTextEntry()
        self.attach(self.aguments_label, 0,1, 3,4)
        self.attach(self.arguments_entry, 1,2,3,4)
        
        self.need_root = gtk.CheckButton(_('Need root'))
        self.attach(self.need_root, 0,1,4,5)
        
        self.options_label = HIGEntryLabel(_('Options:'))
        self.options_entry = HIGTextEntry()
        self.attach(self.options_label,0,1,5,6)
        self.attach(self.options_entry, 1,2,5,6)
        
            
        
    def clear(self):
        """ 
        Clear Option Display
        """
        self.options_label.set_label('')
        self.name_entry.set_text('')
        self.hint_entry.set_text('')
        self.arguments_entry.set_text('')
        self.need_root.set_active(False)
        self.options_entry.set_text('')
        
        
    def set_option_list(self, list):
	"""
	set option list from a dictionarie 
	
	@param list: Elements of a option
	@type list: Dictionarie with elements
	"""
	self.clear()
	self.option_label.set_new_text(list['name'])
        self.name_entry.set_text(list['name'])
        self.hint_entry.set_text(list['hint'])
    
	for i in list['arguments']:
	    i = self.arguments_entry.get_text() + ", " + i
	    self.arguments_entry.set_text(i)
	
	self.options_entry.set_text(list['option'])
        self.need_root.set_active(list['need_root'])
	
	
    def set_option(self,name, hint,
                   arguments, need_root, 
                   options):        
        """
        fill fields
	buggy arguments.
        """
	self.clear()
        self.options_label.set_label(name)
        self.name_entry.set_text(name)
        self.hint_entry.set_text(hint)
        self.arguments_entry.set_text(arguments)
        self.need_root.set_active(need_root)

class OptionDisplayMainFrame(OptionDisplay):
    def __init__(self, option=None):
	OptionDisplay.__init__(self, option)
	hbox = HIGHBox()
	hbox.set_border_width(12)
	self.delete_button = HIGButton(stock='gtk-delete')
	self.delete_button.connect('clicked', self.delete_option)
	self.new_button = HIGButton(stock='gtk-new')
	self.new_button.connect('clicked', self.new_option)
	self.update_button = HIGButton(stock='gtk-refresh')
	self.update_button.connect('clicked', self.update_option)
	self.add_button = HIGButton(stock='gtk-add')
	self.add_button.connect('clicked', self.add_option)
	hbox.pack_end(self.delete_button,False,False)
	hbox.pack_end(self.update_button, False, False)
	hbox.pack_end(self.add_button, False, False)
	hbox.pack_end(self.new_button, False,False)
        self.attach(hbox, 1,2,6,7)

    def delete_option(self, widget):
	"""
	Delete option
	@param widget: widget from connect
	@type widget: HIGButton
	"""
    def update_option(self, widget):
	"""
	Update option
	@param widget: widget from connect
	@type widget: HIGButton
	"""
    def new_option(self, widget):
	"""
	Clean Option Display
	@param widget: widget from connect
	@type widget: HIGButton
	"""                        
    def add_option(self, widget):
	"""
	Add option
	@param widget: widget from connect
	@type widget: HIGButton
	"""
	
class OptionList(HIGVBox):
    """
    A treeview with a list of actual options
    """
    
    def __init__(self, optiondisplay=None):
        HIGVBox.__init__(self)
        self.__model =  gtk.TreeStore(gobject.TYPE_STRING)
        self.__treeview = gtk.TreeView(self.__model)
	self.__treeview.set_headers_visible(False)
        renderer = gtk.CellRendererText() 
        column = gtk.TreeViewColumn("Option List", renderer, text=0)
        self.__treeview.append_column(column)
        self.options = NmapOptions(options)
        self.__scrolledwindow = HIGScrolledWindow()
        hbox = HIGHBox()
        hbox.pack_start(self.__treeview, True, True)
	self.__scrolledwindow.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
	self.__scrolledwindow.add_with_viewport(hbox)
	self.notebook = HIGNotebook()
	self.notebook.append_page(self.__scrolledwindow, HIGEntryLabel(_('Option List')))


        self.pack_start(self.notebook, True, True)
        self.optiondisplay = optiondisplay
        
        self.exists_display = (self.optiondisplay != None ) #True or False
	self.set_option_display(optiondisplay)
	
            
    def set_option_display(self, optiondisplay):
	"""
	Set a option display to change fields when cursor change
	
	@param optiondisplay: it's a mainframe that contains fields to set 
	@type optiondisplay: OptionDisplay
	"""
	
	self.optiondisplay = optiondisplay
	self.exists_display = (self.optiondisplay != None ) #True or False
        if self.exists_display:
	    print "cursor-changed"
            self.__treeview.connect("cursor-changed",self.update_option_display)
	    
            

    def get_selected(self):
	"""
	Returns the string with name of selected option
	"""
	try:
	    treeselection = self.__treeview.get_selection()
	    (model,iter) = treeselection.get_selected()	
	    return model.get_value(iter,0)
	except:
	    return None            
            
    def update_option_display(self, widget):
        """
        Update option display contents 
        """
        option = self.options.get_option(self.get_selected())
        self.optiondisplay.set_option_list(option)
	self.optiondisplay.add_button.set_sensitive(False)
    
    
    
    def reload(self):
        """
        Reload items of treeview
        """
        
        list = self.options.get_options_list()
        for i in list:
            myiter = self.__model.insert_before(None, None)
            self.__model.set_value(myiter, 0, i)

    def add(self, option):
        """
        Add a new option
        """
    def remove(self, option):
        """
        Remove current option
        """
    def save(self, option):
        """
        Save from option treeview to xml file 
        """


if __name__ == "__main__":
    o = OptionList()
    
    