root/branch/InterfaceEditor/umitGUI/ProfileEditor.py @ 4001

Revision 4001, 17.3 kB (checked in by luis, 4 years ago)

Fixed #196 - wizard expert mode

Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3#
4# Copyright (C) 2005-2006 Insecure.Com LLC.
5# Copyright (C) 2007-2008 Adriano Monteiro Marques
6#
7# Authors:
8#  Adriano Monteiro Marques <py.adriano@gmail.com>
9#  Luis Antonio Bastiao Silva <luis.kop@gmail.com>
10#
11#
12# This program is free software; you can redistribute it and/or modify
13# it under the terms of the GNU General Public License as published by
14# the Free Software Foundation; either version 2 of the License, or
15# (at your option) any later version.
16#
17# This program is distributed in the hope that it will be useful,
18# but WITHOUT ANY WARRANTY; without even the implied warranty of
19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20# GNU General Public License for more details.
21#
22# You should have received a copy of the GNU General Public License
23# along with this program; if not, write to the Free Software
24# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25
26
27import gtk
28
29from higwidgets.higwindows import HIGWindow
30from higwidgets.higboxes import HIGVBox, HIGHBox
31from higwidgets.higboxes import HIGSpacer, hig_box_space_holder
32from higwidgets.higexpanders import HIGExpander
33from higwidgets.higlabels import HIGSectionLabel, HIGEntryLabel
34from higwidgets.higscrollers import HIGScrolledWindow
35from higwidgets.higtextviewers import HIGTextView
36from higwidgets.higbuttons import HIGButton
37from higwidgets.higtables import HIGTable
38from higwidgets.higdialogs import HIGAlertDialog, HIGDialog
39from umitGUI.ScanToolbar import ScanCommandToolbar, ScanToolbar
40from umitGUI.ProfileCombo import ProfileCombo
41
42
43
44
45from umitGUI.OptionBuilder import *
46
47from umitCore.ProfileEditorConf import profile_editor_file
48from umitCore.NmapCommand import CommandConstructor
49from umitCore.UmitConf import Profile, CommandProfile
50from umitCore.UmitLogging import log
51from umitCore.I18N import _
52
53
54class ProfileEditor(HIGWindow):
55    def __init__(self, profile_name=None):
56        HIGWindow.__init__(self)
57        self.set_title(_('Profile Editor'))
58        self.set_position(gtk.WIN_POS_CENTER)
59        self.__create_widgets()
60        self.__pack_widgets()
61       
62        self.scan_notebook = None
63        self.profilemanager = None
64       
65
66        self.original_profile = None
67        self.profile_name = profile_name
68        self.profile = CommandProfile()
69
70        self.deleted = False
71        options_used = {}
72
73        if profile_name:
74            log.debug("Showing profile %s" % profile_name)
75            self.original_profile = self.profile.get_profile(profile_name)
76            options_used = self.original_profile['options']
77
78            # Interface settings
79            self.profile_name_entry.set_text(profile_name)
80            self.profile_hint_entry.set_text(self.original_profile['hint'])
81            self.profile_description_text.get_buffer().set_text(\
82                self.original_profile['description'])
83            self.profile_annotation_text.get_buffer().set_text(\
84                self.original_profile['annotation'])
85            ## Integration -- commented
86            #if delete:
87            #    # Removing profile. It must be saved again
88            #    self.profile.remove_profile(profile_name)
89            #
90            #    self.deleted = True
91
92        self.constructor = CommandConstructor(options_used)
93        self.options = OptionBuilder(profile_editor_file,
94                                     self.constructor,
95                                     self.update_command)
96        log.debug("Option groups: %s" % str(self.options.groups))
97        log.debug("Option section names: %s" % str(self.options.section_names))
98        #log.debug("Option tabs: %s" % str(self.options.tabs))
99
100        self.constructor = CommandConstructor(options_used)
101        self.options = OptionBuilder(profile_editor_file, self.constructor, self.update_command)
102        log.debug("Option groups: %s" % str(self.options.groups))
103        log.debug("Option section names: %s" % str(self.options.section_names))
104        #log.debug("Option tabs: %s" % str(self.options.tabs))
105       
106        for tab in self.options.groups:
107            self.__create_tab(tab,
108                              self.options.section_names[tab],
109                              self.options.tabs[tab])
110
111        self.update_command()
112
113    def update_command(self):
114        """Regenerate command with target '<target>' and set the value for
115        the command entry"""
116        self.command_entry.set_text(self.constructor.get_command('<target>'))
117
118    def help(self, widget):
119        d = HIGAlertDialog(parent=self,
120                        message_format=_("Help not implemented"),
121                        secondary_text=_("Umit help is not implemented yet."))
122        d.run()
123        d.destroy()
124
125    def __create_widgets(self):
126        self.main_vbox = HIGVBox()
127
128       
129       
130        self.command_expander = HIGExpander('<b>'+_('Command')+'</b>')
131        self.command_expander.set_expanded(True)
132        self.command_entry = gtk.Entry()
133        self.notebook = gtk.Notebook()
134
135        # Profile info page
136        self.profile_info_vbox = HIGVBox()
137        self.profile_info_label = HIGSectionLabel(_('Profile Information'))
138        self.profile_name_label = HIGEntryLabel(_('Profile name'))
139        self.profile_name_entry = gtk.Entry()
140        self.profile_hint_label = HIGEntryLabel(_('Hint'))
141        self.profile_hint_entry = gtk.Entry()
142        self.profile_description_label = HIGEntryLabel(_('Description'))
143        #self.profile_description_label = HIGHBox()
144        self.profile_description_scroll = HIGScrolledWindow()
145        self.profile_description_text = HIGTextView()
146        self.profile_annotation_label = HIGEntryLabel(_('Annotation'))
147        #self.profile_annotation_label = HIGHBox()
148        self.profile_annotation_scroll = HIGScrolledWindow()
149        self.profile_annotation_text = HIGTextView()
150
151        # Buttons
152        self.buttons_hbox = HIGHBox()
153
154        self.help_button = HIGButton(stock=gtk.STOCK_HELP)
155        self.help_button.connect('clicked', self.help)
156
157        self.delete_button = HIGButton(stock=gtk.STOCK_DELETE)
158        self.delete_button.connect('clicked', self.on_delete)
159
160        #self.delete_button = HIGButton(stock=gtk.STOCK_DELETE)
161        #self.delete_button.connect('clicked', self.delete_profile)
162       
163        self.cancel_button = HIGButton(stock=gtk.STOCK_CANCEL)
164       
165        # Integration
166        self.cancel_button.connect('clicked', self.on_cancel)
167
168        self.ok_button = HIGButton(stock=gtk.STOCK_OK)
169        self.ok_button.connect('clicked', self.save_profile)
170
171        self.profile_name_entry.connect('activate', self.save_profile)
172
173        self.connect('delete-event', self.restore_profile)
174        self.connect('show', self.on_show)
175
176    def __pack_widgets(self):
177        self.add(self.main_vbox)
178
179        # Packing widgets to main_vbox
180
181        self.main_vbox._pack_noexpand_nofill(self.command_expander)
182        self.main_vbox._pack_expand_fill(self.notebook)
183        self.main_vbox._pack_noexpand_nofill(self.buttons_hbox)
184
185        # Packing command_entry on command_expander
186        self.command_expander.hbox.pack_start(self.command_entry)
187
188        # Packing profile information tab on notebook
189        self.notebook.append_page(self.profile_info_vbox,
190                                  gtk.Label(_('Profile')))
191        self.profile_info_vbox.set_border_width(5)
192        table = HIGTable()
193        self.profile_info_vbox._pack_noexpand_nofill(self.profile_info_label)
194        self.profile_info_vbox._pack_noexpand_nofill(HIGSpacer(table))
195
196        self.profile_annotation_scroll.add(self.profile_annotation_text)
197        self.profile_description_scroll.add(self.profile_description_text)
198
199        vbox_desc = HIGVBox()
200        vbox_desc._pack_noexpand_nofill(self.profile_description_label)
201        vbox_desc._pack_expand_fill(hig_box_space_holder())
202
203        vbox_ann = HIGVBox()
204        vbox_ann._pack_noexpand_nofill(self.profile_annotation_label)
205        vbox_ann._pack_expand_fill(hig_box_space_holder())
206
207        table.attach(self.profile_name_label,0,1,0,1,xoptions=0)
208        table.attach(self.profile_name_entry,1,2,0,1)
209        #table.attach(self.profile_hint_label,0,1,1,2,xoptions=0)
210        table.attach(self.profile_hint_label,0,1,1,2)
211        table.attach(self.profile_hint_entry,1,2,1,2)
212        table.attach(vbox_desc,0,1,2,3)
213        table.attach(self.profile_description_scroll,1,2,2,3)
214        table.attach(vbox_ann,0,1,3,4)
215        table.attach(self.profile_annotation_scroll,1,2,3,4)
216
217        # Packing buttons on button_hbox
218        self.buttons_hbox.pack_start(self.help_button)
219        self.buttons_hbox.pack_start(self.delete_button)
220        self.buttons_hbox.pack_start(self.cancel_button)
221        self.buttons_hbox.pack_start(self.ok_button)
222
223        self.buttons_hbox.set_border_width(5)
224        self.buttons_hbox.set_spacing(6)
225
226    def __create_tab(self, tab_name, section_name, tab):
227        log.debug(">>> Tab name: %s" % tab_name)
228        log.debug(">>>Creating profile editor section: %s" % section_name)
229
230        vbox = HIGVBox()
231        table = HIGTable()
232        section = HIGSectionLabel(section_name)
233
234        vbox._pack_noexpand_nofill(section)
235        vbox._pack_noexpand_nofill(HIGSpacer(table))
236        # Add a scrollbar in Profiles Frames
237        vbox.set_border_width(6)
238
239        tab.fill_table(table, True)
240        self.scrollwindow = HIGScrolledWindow()
241        self.scrollwindow.set_size_request(600,300)
242        vp = gtk.Viewport()
243        vp.add(vbox)
244        vp.set_shadow_type(gtk.SHADOW_NONE)
245        self.scrollwindow.add(vp)
246
247        vbox_tmp = HIGVBox()
248        vbox_tmp.set_border_width(6)
249        vbox_tmp.set_spacing(12)
250        vbox_tmp.pack_start(self.scrollwindow)
251
252       
253        self.notebook.append_page(vbox_tmp, gtk.Label(tab_name))
254    def set_profilemanager(self, model):
255        """
256        give a model of treeview to update profile manager
257        after run wizard
258        """
259        assert model != None
260       
261        self.model = model
262        self.profilemanager = True 
263        vbox.set_border_width(5)
264
265        tab.fill_table(table, True)
266
267        self.notebook.append_page(vbox, gtk.Label(tab_name))
268   
269    def set_profilemanager(self, model):
270        """
271        give a model of treeview to update profile manager
272        after run wizard
273        """
274        assert model is not None
275
276        self.model = model
277        self.profilemanager = True
278
279    def update_profilemanager(self):
280        """
281        Update treeview of ProfileManager"
282        """
283        assert self.profilemanager
284
285        profiles = self.profile.sections()
286        profiles.sort()
287        self.model.clear()
288
289        for command in profiles:
290            myiter = self.model.insert_before(None, None)
291            self.model.set_value(myiter, 0, command)
292            self.model.set_value(myiter,1, self.profile.get_hint(command))
293
294
295    def save_profile(self, widget=None):
296        if self.profile_name:
297            self.profile.remove_profile(self.profile_name)
298
299        profile_name = self.profile_name_entry.get_text()
300        if profile_name == '':
301            alert = HIGAlertDialog(message_format=_('Unnamed profile'),\
302                                   secondary_text=_('You must provide a name \
303for this profile.'))
304            alert.run()
305            alert.destroy()
306
307            self.notebook.set_current_page(0)
308            self.profile_name_entry.grab_focus()
309
310            return None
311        command = self.constructor.get_command('%s')
312        hint = self.profile_hint_entry.get_text()
313
314        buf = self.profile_description_text.get_buffer()
315        description = buf.get_text(buf.get_start_iter(),\
316                                   buf.get_end_iter())
317
318        buf = self.profile_annotation_text.get_buffer()
319        annotation = buf.get_text(buf.get_start_iter(),\
320                                  buf.get_end_iter())
321
322        self.profile.add_profile(profile_name,\
323                                 command=command,\
324                                 hint=hint,\
325                                 description=description,\
326                                 annotation=annotation,\
327                                 options=self.constructor.get_options())
328        self.deleted = False
329
330        if self.profilemanager:
331            self.update_profilemanager()
332
333        self.on_cancel()
334
335    def on_show(self, widget=None):
336        # Focus on profile name input
337        self.profile_name_entry.select_region(0, -1)
338        self.profile_name_entry.grab_focus()
339
340    def clean_profile_info(self):
341        self.profile_name_entry.set_text('')
342        self.profile_hint_entry.set_text('')
343        self.profile_description_text.get_buffer().set_text('')
344        self.profile_annotation_text.get_buffer().set_text('')
345        self.profile_name = None
346
347    def set_notebook(self, notebook):
348        self.scan_notebook = notebook
349    #<<<<<<< .working
350   
351    def quit(self, widget=None, extra=None):
352        if self.deleted:
353            dialog = HIGDialog(buttons=(gtk.STOCK_OK, gtk.RESPONSE_OK,
354                                        gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL))
355            alert = HIGEntryLabel('<b>'+_("Deleting Profile")+'</b>')
356            text = HIGEntryLabel(_('Your profile is going to be deleted! Click\
357 Ok to continue, or Cancel to go back to Profile Editor.'))
358            hbox = HIGHBox()
359            hbox.set_border_width(5)
360            hbox.set_spacing(12)
361           
362            vbox = HIGVBox()
363            vbox.set_border_width(5)
364            vbox.set_spacing(12)
365           
366            image = gtk.Image()
367            image.set_from_stock(gtk.STOCK_DIALOG_WARNING, gtk.ICON_SIZE_DIALOG)
368           
369            vbox.pack_start(alert)
370            vbox.pack_start(text)
371            hbox.pack_start(image)
372            hbox.pack_start(vbox)
373           
374            dialog.vbox.pack_start(hbox)
375            dialog.vbox.show_all()
376           
377            response = dialog.run()
378            dialog.destroy()
379           
380            if response == gtk.RESPONSE_CANCEL:
381                return None
382        self.destroy()       
383        if self.scan_notebook != None:
384           
385            for i in xrange(self.scan_notebook.get_n_pages()):
386                page = self.scan_notebook.get_nth_page(i)
387                page.toolbar.profile_entry.update()
388       
389    #=======
390
391    def on_delete(self, widget=None):
392        if not self.profile_name:
393            return self.on_cancel()
394
395        dialog = HIGDialog(buttons=(gtk.STOCK_OK, gtk.RESPONSE_OK,
396                                    gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL))
397        alert = HIGEntryLabel('<b>'+_("Deleting Profile")+'</b>')
398        text = HIGEntryLabel(_('Your profile is going to be deleted! Click \
399Ok to continue, or Cancel to go back to Profile Editor.'))
400        hbox = HIGHBox()
401        hbox.set_border_width(5)
402        hbox.set_spacing(12)
403
404        vbox = HIGVBox()
405        vbox.set_border_width(5)
406        vbox.set_spacing(12)
407
408        image = gtk.Image()
409        image.set_from_stock(gtk.STOCK_DIALOG_WARNING, gtk.ICON_SIZE_DIALOG)
410
411        vbox.pack_start(alert)
412        vbox.pack_start(text)
413        hbox.pack_start(image)
414        hbox.pack_start(vbox)
415
416        dialog.vbox.pack_start(hbox)
417        dialog.vbox.show_all()
418
419        response = dialog.run()
420        dialog.destroy()
421
422        if response == gtk.RESPONSE_CANCEL:
423            return None
424
425        self.deleted = True
426        self.profile.remove_profile(self.profile_name)
427        self.on_cancel()
428
429    def restore_profile(self, widget=None, user_param=None):
430        if self.original_profile:
431            p = self.original_profile
432
433            profile_name = p['profile']
434            command = p['command']
435            hint = p['hint']
436            description = p['description']
437            annotation = p['annotation']
438            options = p['options']
439
440            self.profile.add_profile(profile_name,\
441                                     command=command,\
442                                     hint=hint,\
443                                     description=description,\
444                                     annotation=annotation,\
445                                     options=options)
446
447        # No need to update the profile entry here
448        self.destroy()
449
450    def on_cancel(self, widget=None):
451        self.destroy()
452        self.update_profile_entry()
453
454    def update_profile_entry(self):
455        page = None
456        for i in xrange(self.scan_notebook.get_n_pages()):
457            page = self.scan_notebook.get_nth_page(i)
458
459            page.toolbar.profile_entry.update(\
460                self.profile_name_entry.get_text())
461
462            list = page.toolbar.profile_entry.get_model()
463            length = len(list)
464            if self.deleted and length > 0 :
465                page.toolbar.profile_entry.set_active(0)
466            elif self.deleted and length == 0:
467                page.toolbar.profile_entry.child.set_text("")
468
469        if page is not None:
470            page.toolbar.profile_entry.update()
471
472        #page.toolbar.scan_profile.profile_entry.child.\
473        #    set_text(self.profile_name_entry.get_text())
474    def quit_without_saving(self, widget=None):
475        self.deleted=False
476        self.quit()
477    def remove_profile(self,profile_name=None):
478        '''
479        Remove current profile
480        '''
481        if not profile_name:
482            profile_name = self.profile_name
483        self.profile.remove_profile(profile_name)
484        self.deleted = True       
485   
486    def delete_profile(self, widget=None):
487        """
488        delete profile
489        """
490        self.remove_profile()
491        self.deleted=False       
492        self.quit()
493
494
495
496if __name__ == '__main__':
497    p = ProfileEditor()
498    p.show_all()
499
500    gtk.main()
501
Note: See TracBrowser for help on using the browser.