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

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

- Align XML files
- Remove useless files
- Remove useless prints
- Remove useless comment code

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