root/branch/k0p/umitInterfaceEditor/OptionManager.py @ 1410

Revision 1410, 13.9 kB (checked in by kop-labs, 6 years ago)

ToolbarInterface? - remove option added

Line 
1#!/usr/bin/env python
2# Copyright (C) 2005 Insecure.Com LLC.
3#
4# Author: Luis A. Bastiao Silva <luis.kop@gmail.com>
5#
6# This program is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 2 of the License, or
9# (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program; if not, write to the Free Software
18# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
20import gtk
21import os
22
23from higwidgets.higtables import HIGTable
24from higwidgets.higboxes import HIGVBox, HIGHBox
25from higwidgets.higscrollers import HIGScrolledWindow
26from higwidgets.higlabels import HIGSectionLabel, HIGEntryLabel
27from higwidgets.higentries import HIGTextEntry
28from higwidgets.hignotebooks import HIGNotebook
29from higwidgets.higbuttons import HIGButton
30from higwidgets.higdialogs import HIGDialog, HIGAlertDialog
31from umitCore.Logging import log
32from umitCore.I18N import _
33## Testing at devel
34from os.path import split, join
35
36from umitCore.Paths import Path
37#Path.set_umit_conf(join(split(__file__)[0], 'config', 'umit.conf'))
38##END DEV TEST
39options = Path.options
40pixmaps_dir = Path.pixmaps_dir
41
42import gobject
43from umitInterfaceEditor.ProfileCore import ProfileOption
44from umitGUI.OptionBuilder import OptionBuilder
45from umitCore.NmapOptions import  NmapOptions
46from umitInterfaceEditor.OptionsCore import ListOptions, Option, ARG_TYPES
47
48'''
49Contains a Display, and the List of Options
50to use in the GUI UIE
51It's a sub-file of UIE
52'''
53
54
55
56
57class OptionDisplay(HIGTable):
58    def __init__(self, option=None):
59        HIGTable.__init__(self)
60        self.create_and_attach_widgets()
61        self.set_border_width(5)
62        self.arg_type=None
63   
64    def create_and_attach_widgets(self):
65        self.option_label = HIGSectionLabel('New Option')
66        self.option_label.set_width_chars(10)
67        self.attach(self.option_label, 0, 1, 0, 1)
68
69        self.name_label = HIGEntryLabel(_('Name:'))
70        self.name_entry = HIGTextEntry()
71        self.attach(self.name_label, 0,1,1,2)
72        self.attach(self.name_entry, 1,3,1,2)
73       
74        self.hint_label = HIGEntryLabel(_('Hint:'))
75        self.hint_entry = HIGTextEntry()
76        self.attach(self.hint_label, 0,1,2,3)
77        self.attach(self.hint_entry,1,3,2,3)
78
79        self.need_root = gtk.CheckButton(_('Need root'))
80        self.attach(self.need_root, 0,1,3,4)   
81       
82     
83       
84        self.options_label = HIGEntryLabel(_('Options:'))
85        hbox = HIGHBox()
86        self.options_entry = HIGTextEntry()
87        self.insert_arg_button = HIGButton(title='Args', stock='gtk-add')
88        self.insert_arg_button.connect('clicked', self.update_args)
89        self.attach(self.options_label,0,1,4,5)
90        self.attach(self.options_entry, 1,2,4,5)
91        self.attach(self.insert_arg_button, 2,3, 4, 5)
92       
93        self.aguments_label = HIGEntryLabel(_('Arguments:'))
94        self.arguments_entry = HIGTextEntry()
95        self.arguments_entry.set_editable(False)
96        self.attach(self.aguments_label, 0,1, 5,6)
97        self.attach(self.arguments_entry, 1,3,5,6)     
98       
99   
100    def update_args(self, widget):
101        '''
102        Update aguments entry and option entry
103        '''     
104       
105        cursor_index = self.options_entry.get_position()
106        text_entry = self.options_entry.get_text()
107        arg_description, arg_key = self.dialog_args()
108        if arg_key != None :
109            #Update arguments
110           
111            self.arg_type = arg_key
112            if text_entry.find('%s') == -1: 
113                left = text_entry[0:cursor_index]
114                right = text_entry[cursor_index:len(text_entry)]
115                final = left + "%s" + right
116                self.options_entry.set_text(final)
117            self.arguments_entry.set_text(arg_description)     
118           
119       
120    def dialog_args(self):
121        '''
122        Create a dialog
123        '''
124       
125        d = HIGDialog(_('Arguments'))
126        description_label = HIGEntryLabel(
127            _('Insert the description to argument:'))
128        description_label.show()
129        description_entry = HIGTextEntry()
130        text = self.arguments_entry.get_text()
131        description_entry.set_text(text)
132        description_entry.show()
133       
134        combo_box = gtk.combo_box_new_text()
135        combo_box.set_wrap_width(1)
136        index = -1 
137        j = 0 
138        for i in ARG_TYPES:
139            combo_box.append_text(ARG_TYPES[i])
140            if i == self.arg_type:
141                index = j
142            j = j + 1
143        if index > -1 : 
144            combo_box.set_active(index)
145        combo_box.show()
146        d.vbox.pack_start(description_label, False, False)
147        d.vbox.pack_start(description_entry, False, False)
148        d.vbox.pack_start(combo_box, False, False)
149        d.add_button(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)
150        d.add_button(gtk.STOCK_OK, gtk.RESPONSE_OK)
151        resp = d.run()
152        result  = None, None 
153        if resp == gtk.RESPONSE_OK:
154            model = combo_box.get_model()
155            active = combo_box.get_active()
156            if active < 0:
157                return None, None 
158            combo_selected =  model[active][0]
159            result = description_entry.get_text(), combo_selected
160            #self.insert_arg_button.set_label('Edit args')
161            #self.insert_arg_button.
162        d.destroy()
163        return result
164   
165    def clear(self):
166        """
167        Clear Option Display
168        """
169        self.option_label.set_new_text('New Option')
170        self.name_entry.set_text('')
171        self.hint_entry.set_text('')
172        self.arguments_entry.set_text('')
173        self.need_root.set_active(False)
174        self.options_entry.set_text('')
175       
176       
177    def set_option_list(self, list):
178        """
179        set option list from a dictionarie
180       
181        @param list: Elements of a option
182        @type list: Dictionarie with elements
183        """
184        self.clear()
185        self.option_label.set_new_text(list['name'])
186        self.name_entry.set_text(list['name'])
187        self.hint_entry.set_text(list['hint'])
188   
189        for i in list['arguments']:
190            i = self.arguments_entry.get_text() + i
191            self.arguments_entry.set_text(i)
192       
193        self.options_entry.set_text(list['option'])
194        self.need_root.set_active(list['need_root'])
195        self.arg_type = list['arg_type']
196       
197       
198    def set_option(self,name, hint,
199                   arguments, need_root, 
200                   options, arg_type):       
201        """
202        fill fields
203        buggy arguments.
204        """
205        self.clear()
206        self.options_entry.set_label(name)
207        self.name_entry.set_text(name)
208        self.hint_entry.set_text(hint)
209        self.arguments_entry.set_text(arguments)
210        self.need_root.set_active(need_root)
211        self.arg_type = arg_type
212
213class OptionDisplayMainFrame(OptionDisplay):
214    def __init__(self, option=None):
215        OptionDisplay.__init__(self)
216        hbox = HIGHBox()
217        hbox.set_border_width(12)
218        self.delete_button = HIGButton(stock='gtk-delete')
219        self.delete_button.connect('clicked', self.delete_option)
220        self.new_button = HIGButton(stock='gtk-new')
221        self.new_button.connect('clicked', self.new_option)
222        self.update_button = HIGButton(stock='gtk-refresh')
223        self.update_button.connect('clicked', self.update_option)
224        self.add_button = HIGButton(stock='gtk-add')
225        self.add_button.connect('clicked', self.add_option)
226        hbox.pack_end(self.delete_button,False,False)
227        hbox.pack_end(self.update_button, False, False)
228        hbox.pack_end(self.add_button, False, False)
229        hbox.pack_end(self.new_button, False,False)
230        self.attach(hbox, 1,2,6,7)
231
232        self.optionlist = option
233   
234    def set_options_list(self, options):
235        '''
236        set a OptionList to manipulate when add remove and edit
237        @param options: Options List
238        @type options: class OptionList
239        '''
240       
241        self.optionlist = options
242
243
244    def get_option(self):
245        '''
246        Get a option filled
247        @return: a option
248        @rtype: class Option
249        '''
250
251        name = self.name_entry.get_text()
252        hint = self.hint_entry.get_text()
253        #Args is only to one yet
254        arguments = [self.arguments_entry.get_text()]
255        need_root = self.need_root.get_active()
256        arg_type = self.arg_type
257        options = self.options_entry.get_text()
258        opt = Option(name, options, hint, arguments, need_root, arg_type)
259        return opt
260
261    def delete_option(self, widget):
262        """
263        Delete option
264        @param widget: widget from connect
265        @type widget: HIGButton
266        """
267        name = self.name_entry.get_text()
268        if name == '':
269            d = HIGAlertDialog(type=gtk.MESSAGE_ERROR, 
270                               message_format=_('Invalid option '), 
271                           secondary_text='Fill fields of option')
272            d.run()
273            d.destroy()
274        else:       
275            if self.optionlist.options.exists(name): 
276                self.optionlist.options.remove_option(name)
277
278                #Update treeview
279                (model,iter) = self.optionlist.get_selected_option()
280                model.remove(iter)     
281                self.clear()
282               
283            else: 
284                d = HIGAlertDialog(type= gtk.MESSAGE_ERROR, 
285                                   message_format=_('ERROR'),
286                                   secondary_text=_('Option do not exists')
287                               )
288                d.run()
289                d.destroy()
290       
291    def update_option(self, widget):
292        """
293        Update option
294        @param widget: widget from connect
295        @type widget: HIGButton
296        """
297        name = self.name_entry.get_text()
298        if name == '':
299            d = HIGAlertDialog(type=gtk.MESSAGE_ERROR, 
300                               message_format=_('Invalid name option '), 
301                           secondary_text='Fill fields of option')
302            d.run()
303            d.destroy()
304        else:   
305            if self.optionlist.options.exists(name): 
306                opt = self.get_option()
307                self.optionlist.options.remove_option(name)
308                self.optionlist.add(opt)
309           
310       
311       
312    def new_option(self, widget):
313        """
314        Clean Option Display
315        @param widget: widget from connect
316        @type widget: HIGButton
317        """   
318        self.clear()
319        self.add_button.set_sensitive(True)
320        self.update_button.set_sensitive(False)
321        self.delete_button.set_sensitive(False)
322       
323    def add_option(self, widget):
324        """
325        Add option
326        @param widget: widget from connect
327        @type widget: HIGButton
328        """
329       
330
331        opt = self.get_option()
332        self.optionlist.add(opt)
333        self.add_button.set_sensitive(False)
334       
335
336TARGET_STRING = 0
337TARGET_ROOTWIN = 1
338
339target = [
340    ('STRING', 0, TARGET_STRING),
341    ('text/plain', 0, TARGET_STRING),
342    ('application/x-rootwin-drop', 0, TARGET_ROOTWIN)
343]
344
345class OptionList(HIGVBox):
346    """
347    A treeview with a list of actual options
348    """
349   
350    def __init__(self, optiondisplay=None):
351        HIGVBox.__init__(self)
352        self.__model =  gtk.TreeStore(gtk.gdk.Pixbuf,gobject.TYPE_STRING)
353        self.__treeview = gtk.TreeView(self.__model)
354        self.__treeview.set_headers_visible(True)
355        self.__treeview.drag_source_set(gtk.gdk.BUTTON1_MASK | 
356                                        gtk.gdk.BUTTON3_MASK,
357                                        target, gtk.gdk.ACTION_COPY | 
358                                        gtk.gdk.ACTION_MOVE)
359        self.__treeview.connect('drag_data_get', self.source_drag_data_get)
360        column = gtk.TreeViewColumn()
361        column.set_title('Name')
362        render_pixbuf = gtk.CellRendererPixbuf()
363        column.pack_start(render_pixbuf, expand=False)
364        column.add_attribute(render_pixbuf, 'pixbuf', 0)
365        render_text = gtk.CellRendererText()
366        column.pack_start(render_text, expand=True)
367        column.add_attribute(render_text, 'text', 1)
368        self.__treeview.append_column(column)
369        self.options = ListOptions(options)
370        self.__scrolledwindow = HIGScrolledWindow()
371        self.__scrolledwindow.set_policy(gtk.POLICY_AUTOMATIC,
372                                         gtk.POLICY_AUTOMATIC)
373        self.pack_start(self.__scrolledwindow, True, True )
374        self.__scrolledwindow.add(self.__treeview)
375        self.optiondisplay = optiondisplay
376       
377        self.exists_display = (self.optiondisplay != None ) #True or False
378        self.set_option_display(optiondisplay)
379    def get_list_option(self):
380        return self.options
381    def source_drag_data_get(self, btn, context, selection_data, info, time):
382
383        param_send = self.get_selected()
384        #param_send = opt
385        selection_data.set(selection_data.target, 8, param_send)
386    def get_selected_option(self):
387        '''
388        @return: iter and model of option treeview selected
389        '''
390        treeselection = self.__treeview.get_selection()
391        (model,iter) = treeselection.get_selected()
392        return model, iter
393
394    def set_option_display(self, optiondisplay):
395        """
396        Set a option display to change fields when cursor change
397       
398        @param optiondisplay: it's a mainframe that contains fields to set
399        @type optiondisplay: OptionDisplay
400        """
401       
402        self.optiondisplay = optiondisplay
403        self.exists_display = (self.optiondisplay != None ) #True or False
404        if self.exists_display:
405            log.debug('<<< Cursor changed')
406            self.__treeview.connect("cursor-changed",self.update_option_display)
407           
408           
409    def get_selected(self):
410        """
411        Returns the string with name of selected option
412        """
413        try:
414            treeselection = self.__treeview.get_selection()
415            (model,iter) = treeselection.get_selected() 
416            return model.get_value(iter,1)
417        except:
418            return None           
419           
420    def update_option_display(self, widget):
421        """
422        Update option display contents
423        """
424        if self.get_selected()==None :
425            return
426        option = self.options.get_option(self.get_selected())
427        self.optiondisplay.set_option_list(option)
428        self.optiondisplay.add_button.set_sensitive(False)
429        self.optiondisplay.update_button.set_sensitive(True)   
430        self.optiondisplay.delete_button.set_sensitive(True)
431   
432    def _arg_img(self, arg):
433        if arg == 'str':
434            return 'entry'
435        elif arg == 'int':
436            return 'spinbutton'
437        elif arg == '' :
438            return 'checkbutton'
439        return 'label'
440    def reload(self):
441        """
442        Reload items of treeview
443        """
444       
445        list = self.options.get_options_list()
446        for i in list:
447            arg = self.options.get_arg_type(i)
448            myiter = self.__model.insert_before(None, None)
449            icon = gtk.Image()
450            s = self._arg_img(arg)
451            img_dir =  os.path.join(pixmaps_dir, '%s.png' % s)
452            icon.set_from_file(img_dir)
453            icon = icon.get_pixbuf()
454            self.__model.set_value(myiter, 0, icon)
455            self.__model.set_value(myiter, 1, i)
456
457
458    def add(self, option):
459        """
460        Add a new option
461        """
462       
463        self.options.add_option_from_dic(option.get_option_dic())
464
465        myiter = self.__model.insert_before(None, None)
466        #XXX : 1 ? I don't have sure about it
467        self.__model.set_value(myiter, 1, option.get_name())
468        self.options.reload_opt()
469
470
471
472    def save(self):
473        """
474        Save from option treeview to xml file
475        """
476        self.options.write_file(options)
477
478
479if __name__ == "__main__":
480    o = OptionList()
481   
482   
Note: See TracBrowser for help on using the browser.