root/trunk/umitGUI/ProfileEditor.py @ 1104

Revision 1104, 10.9 kB (checked in by boltrix, 6 years ago)

Changed encoding of files to utf-8

Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4# Copyright (C) 2005 Insecure.Com LLC.
5#
6# Author: Adriano Monteiro Marques <py.adriano@gmail.com>
7#
8# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program; if not, write to the Free Software
20# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22import gtk
23
24from higwidgets.higwindows import HIGWindow
25from higwidgets.higboxes import HIGVBox, HIGHBox, HIGSpacer, hig_box_space_holder
26from higwidgets.higexpanders import HIGExpander
27from higwidgets.higlabels import HIGSectionLabel, HIGEntryLabel
28from higwidgets.higscrollers import HIGScrolledWindow
29from higwidgets.higtextviewers import HIGTextView
30from higwidgets.higbuttons import HIGButton
31from higwidgets.higtables import HIGTable
32from higwidgets.higdialogs import HIGAlertDialog, HIGDialog
33
34from umitGUI.OptionBuilder import *
35
36from umitCore.Paths import Path
37from umitCore.NmapCommand import CommandConstructor
38from umitCore.UmitConf import Profile, CommandProfile
39from umitCore.Logging import log
40from umitCore.I18N import _
41
42profile_editor = Path.profile_editor
43
44
45class ProfileEditor(HIGWindow):
46    def __init__(self, profile_name=None, delete=True):
47        HIGWindow.__init__(self)
48        self.set_title(_('Profile Editor'))
49        self.set_position(gtk.WIN_POS_CENTER)
50       
51        self.__create_widgets()
52        self.__pack_widgets()
53       
54        self.profile = CommandProfile()
55       
56        self.deleted = False
57        options_used = {}
58       
59        if profile_name:
60            log.debug("Showing profile %s" % profile_name)
61            prof = self.profile.get_profile(profile_name)
62            options_used = prof['options']
63           
64            # Interface settings
65            self.profile_name_entry.set_text(profile_name)
66            self.profile_hint_entry.set_text(prof['hint'])
67            self.profile_description_text.get_buffer().set_text(prof['description'])
68            self.profile_annotation_text.get_buffer().set_text(prof['annotation'])
69
70            if delete:
71                # Removing profile. It must be saved again
72                self.profile.remove_profile(profile_name)
73                self.deleted = True
74       
75        self.constructor = CommandConstructor(options_used)
76        self.options = OptionBuilder(profile_editor, self.constructor, self.update_command)
77        log.debug("Option groups: %s" % str(self.options.groups))
78        log.debug("Option section names: %s" % str(self.options.section_names))
79        #log.debug("Option tabs: %s" % str(self.options.tabs))
80       
81        for tab in self.options.groups:
82            self.__create_tab(tab, self.options.section_names[tab], self.options.tabs[tab])
83       
84        self.update_command()
85   
86    def update_command(self):
87        """Regenerate command with target '<target>' and set the value for the command entry"""
88        self.command_entry.set_text(self.constructor.get_command('<target>'))
89
90    def help(self, widget):
91        d = HIGAlertDialog(parent=self,
92                           message_format=_("Help not implemented"),
93                           secondary_text=_("Umit help is not implemented yet."))
94        d.run()
95        d.destroy()
96   
97    def __create_widgets(self):
98        self.main_vbox = HIGVBox()
99        self.command_expander = HIGExpander('<b>'+_('Command')+'</b>')
100        self.command_expander.set_expanded(True)
101        self.command_entry = gtk.Entry()
102       
103        self.notebook = gtk.Notebook()
104       
105        # Profile info page
106        self.profile_info_vbox = HIGVBox()
107        self.profile_info_label = HIGSectionLabel(_('Profile Information'))
108        self.profile_name_label = HIGEntryLabel(_('Profile name'))
109        self.profile_name_entry = gtk.Entry()
110        self.profile_hint_label = HIGEntryLabel(_('Hint'))
111        self.profile_hint_entry = gtk.Entry()
112        self.profile_description_label = HIGEntryLabel(_('Description'))
113        self.profile_description_scroll = HIGScrolledWindow()
114        self.profile_description_text = HIGTextView()
115        self.profile_annotation_label = HIGEntryLabel(_('Annotation'))
116        self.profile_annotation_scroll = HIGScrolledWindow()
117        self.profile_annotation_text = HIGTextView()
118       
119        # Buttons
120        self.buttons_hbox = HIGHBox()
121       
122        self.help_button = HIGButton(stock=gtk.STOCK_HELP)
123        self.help_button.connect('clicked', self.help)
124       
125        self.cancel_button = HIGButton(stock=gtk.STOCK_CANCEL)
126        self.cancel_button.connect('clicked', self.quit)
127       
128        self.ok_button = HIGButton(stock=gtk.STOCK_OK)
129        self.ok_button.connect('clicked', self.save_profile)
130   
131    def __pack_widgets(self):
132        self.add(self.main_vbox)
133       
134        # Packing widgets to main_vbox
135        self.main_vbox._pack_noexpand_nofill(self.command_expander)
136        self.main_vbox._pack_expand_fill(self.notebook)
137        self.main_vbox._pack_noexpand_nofill(self.buttons_hbox)
138       
139        # Packing command_entry on command_expander
140        self.command_expander.hbox.pack_start(self.command_entry)
141       
142        # Packing profile information tab on notebook
143        self.notebook.append_page(self.profile_info_vbox, gtk.Label(_('Profile')))
144        self.profile_info_vbox.set_border_width(5)
145        table = HIGTable()
146        self.profile_info_vbox._pack_noexpand_nofill(self.profile_info_label)
147        self.profile_info_vbox._pack_noexpand_nofill(HIGSpacer(table))
148       
149        self.profile_annotation_scroll.add(self.profile_annotation_text)
150        self.profile_description_scroll.add(self.profile_description_text)
151       
152        vbox_desc = HIGVBox()
153        vbox_desc._pack_noexpand_nofill(self.profile_description_label)
154        vbox_desc._pack_expand_fill(hig_box_space_holder())
155       
156        vbox_ann = HIGVBox()
157        vbox_ann._pack_noexpand_nofill(self.profile_annotation_label)
158        vbox_ann._pack_expand_fill(hig_box_space_holder())
159       
160        table.attach(self.profile_name_label,0,1,0,1,xoptions=0)
161        table.attach(self.profile_name_entry,1,2,0,1)
162        table.attach(self.profile_hint_label,0,1,1,2,xoptions=0)
163        table.attach(self.profile_hint_entry,1,2,1,2)
164        table.attach(vbox_desc,0,1,2,3,xoptions=0)
165        table.attach(self.profile_description_scroll,1,2,2,3)
166        table.attach(vbox_ann,0,1,3,4,xoptions=0)
167        table.attach(self.profile_annotation_scroll,1,2,3,4)
168       
169        # Packing buttons on button_hbox
170        self.buttons_hbox.pack_start(self.help_button)
171        self.buttons_hbox.pack_start(self.cancel_button)
172        self.buttons_hbox.pack_start(self.ok_button)
173       
174        self.buttons_hbox.set_border_width(5)
175        self.buttons_hbox.set_spacing(6)
176
177    def __create_tab(self, tab_name, section_name, tab):
178        log.debug(">>> Tab name: %s" % tab_name)
179        log.debug(">>>Creating profile editor section: %s" % section_name)
180
181        vbox = HIGVBox()
182        table = HIGTable()
183        section = HIGSectionLabel(section_name)
184       
185        vbox._pack_noexpand_nofill(section)
186        vbox._pack_noexpand_nofill(HIGSpacer(table))
187        vbox.set_border_width(5)
188
189        tab.fill_table(table, True)
190       
191        self.notebook.append_page(vbox, gtk.Label(tab_name))
192   
193    def save_profile(self, widget):
194        profile_name = self.profile_name_entry.get_text()
195        if profile_name == '':
196            alert = HIGAlertDialog(message_format=_('Unnamed profile'),\
197                                   secondary_text=_('You must provide a name \
198for this profile.'))
199            alert.run()
200            alert.destroy()
201           
202            self.notebook.set_current_page(0)
203            self.profile_name_entry.grab_focus()
204           
205            return None
206       
207        command = self.constructor.get_command('%s')
208        hint = self.profile_hint_entry.get_text()
209       
210        buf = self.profile_description_text.get_buffer()
211        description = buf.get_text(buf.get_start_iter(),\
212                                      buf.get_end_iter())
213       
214        buf = self.profile_annotation_text.get_buffer()
215        annotation = buf.get_text(buf.get_start_iter(),\
216                                      buf.get_end_iter())
217
218        self.profile.add_profile(profile_name,\
219                                 command=command,\
220                                 hint=hint,\
221                                 description=description,\
222                                 annotation=annotation,\
223                                 options=self.constructor.get_options())
224       
225        self.deleted = False
226        self.quit()
227   
228    def clean_profile_info(self):
229        self.profile_name_entry.set_text('')
230        self.profile_hint_entry.set_text('')
231        self.profile_description_text.get_buffer().set_text('')
232        self.profile_annotation_text.get_buffer().set_text('')
233   
234    def set_notebook(self, notebook):
235        self.scan_notebook = notebook
236   
237    def quit(self, widget=None, extra=None):
238        if self.deleted:
239            dialog = HIGDialog(buttons=(gtk.STOCK_OK, gtk.RESPONSE_OK,
240                                        gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL))
241            alert = HIGEntryLabel('<b>'+_("Deleting Profile")+'</b>')
242            text = HIGEntryLabel(_('Your profile is going to be deleted! Click\
243 Ok to continue, or Cancel to go back to Profile Editor.'))
244            hbox = HIGHBox()
245            hbox.set_border_width(5)
246            hbox.set_spacing(12)
247           
248            vbox = HIGVBox()
249            vbox.set_border_width(5)
250            vbox.set_spacing(12)
251           
252            image = gtk.Image()
253            image.set_from_stock(gtk.STOCK_DIALOG_WARNING, gtk.ICON_SIZE_DIALOG)
254           
255            vbox.pack_start(alert)
256            vbox.pack_start(text)
257            hbox.pack_start(image)
258            hbox.pack_start(vbox)
259           
260            dialog.vbox.pack_start(hbox)
261            dialog.vbox.show_all()
262           
263            response = dialog.run()
264            dialog.destroy()
265           
266            if response == gtk.RESPONSE_CANCEL:
267                return None
268       
269        self.destroy()
270       
271        for i in xrange(self.scan_notebook.get_n_pages()):
272            page = self.scan_notebook.get_nth_page(i)
273            page.toolbar.profile_entry.update()
274       
275        #page.toolbar.scan_profile.profile_entry.child.\
276        #    set_text(self.profile_name_entry.get_text())
277
278
279if __name__ == '__main__':
280    p = ProfileEditor()
281    p.show_all()
282   
283    gtk.main()
Note: See TracBrowser for help on using the browser.