root/branch/k0p/umitGUI/ProfileManager.py @ 895

Revision 895, 9.3 kB (checked in by kop-labs, 6 years ago)

copy

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
21
22from higwidgets.higwindows import HIGWindow
23from higwidgets.higboxes import HIGVBox, HIGHBox
24from higwidgets.higscrollers import HIGScrolledWindow
25from higwidgets.higbuttons import HIGButton
26from higwidgets.higlabels import HIGSectionLabel, HIGEntryLabel, HIGDialogLabel
27from higwidgets.higentries import HIGTextEntry
28from higwidgets.higframe import HIGFrame
29from higwidgets.higdialogs import HIGDialog
30
31from umitGUI.Wizard import Wizard
32from umitCore.NmapCommand import CommandConstructor
33# Testing at devel
34from os.path import split, join
35
36from umitCore.Paths import Path
37Path.set_umit_conf(join(split(__file__)[0], 'config', 'umit.conf'))
38#END DEV TEST
39
40from ProfileEditor import ProfileEditor
41from umitCore.UmitConf import CommandProfile
42from umitCore.I18N import _
43
44import gobject
45
46profile_editor = Path.profile_editor
47
48class ProfileManager(HIGWindow):
49    """
50    Create a Profile Manager
51    """
52    def __init__(self):
53        HIGWindow.__init__(self, type=gtk.WINDOW_TOPLEVEL)
54        self.set_title('Profile Manager')
55        self.set_position(gtk.WIN_POS_CENTER)
56        self.__create_widgets()
57        self.add(self.vbox_main)
58        self.__fill_widgets()
59        self.__pack_widgets()
60
61   
62    def __create_widgets(self):
63       
64        self.vbox_main = HIGVBox()
65       
66        self.main_frame = HIGFrame("Profiles")
67        self.main_frame.set_shadow_type(gtk.SHADOW_ETCHED_OUT)
68       
69       
70        self.vbox = HIGVBox()
71        self.profiles_sw = HIGScrolledWindow()
72        #TreeView
73        self.hbox_aux_tv = HIGHBox()
74        self.model = gtk.TreeStore(gobject.TYPE_STRING)
75        self.profiles_tv = gtk.TreeView(self.model)
76        renderer = gtk.CellRendererText()
77        column = gtk.TreeViewColumn("Name", renderer, text=0)
78        self.profiles_tv.append_column(column)
79       
80
81        #Info
82        self.hbox_info = HIGHBox()
83        self.command_label = HIGEntryLabel('Command: ')
84        self.command_entry = HIGTextEntry()
85        self.command_entry.set_editable(False)
86       
87        #Buttons
88        self.hbox_buttons = HIGHBox()
89        self.edit_button = HIGButton(stock='gtk-edit')
90        self.edit_button.connect("clicked", self.open_peditor)
91        self.new_button = HIGButton(stock='gtk-new')
92        self.new_button.connect("clicked", self.new_wiz)
93        self.copy_button = HIGButton(stock='gtk-copy')
94        self.copy_button.connect("clicked", self.copy_profiles)
95        self.delete_button = HIGButton(stock=gtk.STOCK_DELETE)
96        self.delete_button.connect('clicked', self.delete_profile)     
97       
98        #Apply Buttons
99        self.hbox_apply = HIGHBox()
100        self.cancel_button = HIGButton(stock='gtk-cancel')
101        self.cancel_button.connect("clicked", self.quit)
102        self.ok_button = HIGButton(stock='gtk-ok')
103        self.ok_button.connect("clicked", self.apply_modifications)
104
105    def __fill_widgets(self):
106
107               
108        self.profiles = CommandProfile()
109        profiles = self.profiles.sections()
110        profiles.sort()
111       
112       
113        for command in profiles:
114            myiter = self.model.insert_after(None, None)
115            self.model.set_value(myiter, 0, command)
116           
117        #selection = self.profiles_tv.get_selection()
118        #selection.connect("changed", self.change_nmap_command)
119        self.profiles_tv.connect("cursor-changed", self.change_nmap_command)
120
121       
122
123    def __pack_widgets(self):
124        """
125        Pack all widgets of windows
126        """
127        self.vbox_main.pack_start(self.main_frame)
128        self.main_frame.add(self.vbox)
129       
130        self.vbox.pack_start(self.profiles_sw, True, True, 0)
131
132        self.hbox_info.pack_start(self.command_label, False,False,0)
133        self.hbox_info.pack_start(self.command_entry, True, True, 0)
134        self.vbox.pack_start(self.hbox_info, True, True,0)
135       
136        self.vbox.pack_start(self.hbox_buttons, False, True, 0)
137       
138        self.hbox_buttons.pack_end(self.copy_button, False, True)
139        self.hbox_buttons.pack_end(self.edit_button, False, True)
140        self.hbox_buttons.pack_end(self.delete_button, False, True)
141        self.hbox_buttons.pack_end(self.new_button, False, True)
142
143       
144        #self.hbox_aux_tv .pack_start(self.profiles_tv,False, False)
145        self.profiles_sw.set_size_request(400,170)
146        self.profiles_sw.add_with_viewport(self.profiles_tv) 
147
148        self.hbox_apply.pack_end(self.ok_button)
149        self.hbox_apply.pack_end(self.cancel_button)
150        self.vbox_main.pack_start(self.hbox_apply)
151       
152    def get_selected_profile(self):
153        """
154        Returns the string with name of selected profile
155        """
156        treeselection = self.profiles_tv.get_selection()
157        (model,iter) = treeselection.get_selected()
158        return model.get_value(iter,0)
159       
160       
161    def change_nmap_command(self,widget_tv):
162        """
163        Change a nmap command at command entry
164        """
165        assert widget_tv != None
166       
167        self.command_entry.set_text(self.profiles.get_command(self.get_selected_profile()))     
168   
169    def new_wiz(self,widget):
170        w = Wizard()
171        w.set_notebook(None)
172       
173        w.show_all()
174
175    def open_peditor(self, widget):
176        """
177        Open Profile Editor with a Selected or Non-Selected(New) Item
178        """
179        assert widget != None
180       
181        #widget = HIGButton()
182       
183        if widget.get_label() == "gtk-edit":
184            # Edit profile selected   
185           
186            pe = ProfileEditor(self.get_selected_profile())
187            pe.show_all()
188        else:
189            # New Profile
190            pe = ProfileEditor()
191            pe.show_all()
192   
193    def copy_profiles(self, widget):
194        """
195        Copy selected Profile
196        """
197
198
199        d = ProfileName(_("Insert a profile name"))
200        profile_name = d.run()
201        if profile_name == None:
202            return None
203        #get commands of selected profile
204        profile_selected = self.get_selected_profile()
205        command = self.profiles.get_command(profile_selected)
206        hint = self.profiles.get_hint(profile_selected)
207        description = self.profiles.get_description(profile_selected)
208        annotation = self.profiles.get_annotation(profile_selected)
209        #Options???
210        prof = self.profiles.get_profile(profile_selected)
211        options_used = prof['options']
212        options = CommandConstructor(options_used)
213
214        self.profiles.add_profile(profile_name,\
215                                  save = False,\
216                                  command=command,\
217                                  hint=hint,\
218                                  description=description,\
219                                  annotation=annotation,\
220                                  options=options.get_options())
221       
222        myiter = self.model.insert_after(None, None)
223        self.model.set_value(myiter, 0, profile_name)
224       
225
226    def delete_profile(self, widget=None):
227        """
228        delete profile only at treeview
229        """
230        #self.profiles.remove_profile()
231        #Update treeview
232        treeselection = self.profiles_tv.get_selection()
233        (model,iter) = treeselection.get_selected()
234        model.remove(iter)     
235       
236    def _reload_profile_list(self):
237        """
238        Reload a list of profiles
239        """
240       
241       
242   
243    def apply_modifications(self, widget):
244        """
245        Apply changes when clicked ok. Save based on treeview.
246        """
247       
248        self.profiles.save_changes()
249       
250       
251       
252       
253    def quit(self, widget):
254        self.destroy()
255   
256       
257class ProfileName(HIGDialog):
258    def __init__(self, text):
259        HIGDialog.__init__(self, _('Profile\'s Name'))
260
261        dialog_label = HIGDialogLabel(text)
262        dialog_label.show()
263        self.vbox.pack_start(dialog_label)
264        self.entry_text = HIGTextEntry()
265        self.entry_text.show()
266        self.vbox.pack_start(self.entry_text)
267        self.add_button(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)
268        self.add_button(gtk.STOCK_OK, gtk.RESPONSE_OK)
269   
270    def run(self):
271        """
272        Returns text of entry
273        and None if someone clicked Cancel.
274        """
275        text = None
276        response = HIGDialog.run(self)
277        text = self.entry_text.get_text()
278        if response==gtk.RESPONSE_CANCEL:
279            text = None
280        self.destroy()
281        return text
282
283       
284class ProfileChosse(HIGHBox):
285    def __init__(self):
286        HIGHBox.__init__(self)       
287        self._create_profile()
288       
289    #def _create_profile(self):
290    def __init__(self):
291        HIGHBox.__init__(self)
292
293        self._create_profile()
294       
295        self.change_button = gtk.Button(_("Change"))
296       
297        self._pack_noexpand_nofill(self.profile_label)
298        self._pack_expand_fill(self.profile_entry)
299       
300        self._pack_noexpand_nofill(self.change_button)
301
302        self.profile_entry.set_focus_child(self.profile_entry.child)
303
304        # Events
305        self.profile_entry.child.connect('activate',
306                        lambda x: self.change_button.clicked())
307
308    def _create_profile(self):
309        self.profile_label = HIGEntryLabel(_('Profile:'))
310        self.profile_entry = ProfileCombo()
311        self.update()
312    def update(self):
313        self.update_profile()
314    def update_profile(self):
315        self.profile_entry.update()
316       
317       
318       
319       
320if __name__=="__main__":
321    pm = ProfileManager()
322    pm.show_all()
323    gtk.main()
324   
325       
326       
Note: See TracBrowser for help on using the browser.