Ejemplo n.º 1
0
class Wizard(HIGWindow):
    def __init__(self):
        HIGWindow.__init__(self)
        self.set_size_request(600, 450)
        self.set_position(gtk.WIN_POS_CENTER)

        self.profile = CommandProfile()
        self.constructor = CommandConstructor()
        self.options = OptionBuilder(wizard_file, self.constructor,
                                     self.update_command)

        self.target = '<target>'
        self.profilemanager = False
        self.title_markup = "<span size='16500' weight='heavy'>%s</span>"
        self.directions = {
            'Start': self.start_page(),
            'Choose': self.choose_page(),
            'Profile': self.profile_page(),
            'Finish': self.finish_page(),
            'LastPage': None
        }

        for i in xrange(len(self.options.groups)):
            step = self.options.groups[i]
            last, next = self.__get_pair(i)

            self.directions[step] = self.__create_steps(
                step, last, next, self.options.section_names[step],
                self.options.tabs[step])

        self.directions['Command'] = self.command_page()

        self.main_vbox = HIGVBox()
        self.main_vbox.set_border_width(5)
        self.main_vbox.set_spacing(12)
        self.add(self.main_vbox)

        self.__create_wizard_widgets()
        self.set_title(_("Umit Command constructor wizard"))

        self.main_vbox._pack_expand_fill(self.directions['Start'])
        self.set_notebook(None)

        self.update_command()

    def __get_pair(self, pos):
        if pos == 0:
            return 'LastPage', self.options.groups[pos + 1]
        elif pos == (self.options.groups.__len__() - 1):
            return self.options.groups[pos - 1], 'Finish'
        else:
            return self.options.groups[pos - 1], self.options.groups[pos + 1]

    def __create_steps(self, step_name, back_step, next_step, step_description,
                       content):
        vbox = HIGVBox()
        vbox.set_spacing(12)

        description = HIGEntryLabel(step_description)
        bar = ForwardBar()
        table = HIGTable()

        vbox._pack_noexpand_nofill(description)
        vbox._pack_expand_fill(table)
        vbox._pack_noexpand_nofill(bar)

        content.fill_table(table, False)

        bar.cancel.connect('clicked', self.close_wizard)
        bar.help.connect('clicked', self._show_help)
        bar.back.connect('clicked', self.switch_page, step_name, back_step)
        bar.forward.connect('clicked', self.switch_page, step_name, next_step)

        return vbox

    def set_notebook(self, notebook):
        self.notebook = notebook

    def __create_wizard_widgets(self):
        self.wizard_title = HIGEntryLabel("")
        self.wizard_title.set_line_wrap(False)
        self.wizard_event = gtk.EventBox()
        self.wizard_logo = gtk.Image()
        self.wizard_event.add(self.wizard_logo)

        self.d = {}
        for c in (65, 97):
            for i in range(26):
                self.d[chr(i + c)] = chr((i + 13) % 26 + c)
        self.img = 1

        command_hbox = HIGHBox()
        self.command_label = HIGEntryLabel(_("Command"))
        self.command_entry = gtk.Entry()

        separator = gtk.HSeparator()

        self.wizard_header_hbox = HIGHBox()

        self.wizard_header_hbox._pack_expand_fill(self.wizard_title)
        self.wizard_header_hbox._pack_noexpand_nofill(self.wizard_event)

        command_hbox._pack_noexpand_nofill(self.command_label)
        command_hbox._pack_expand_fill(self.command_entry)

        self.main_vbox._pack_noexpand_nofill(self.wizard_header_hbox)
        self.main_vbox._pack_noexpand_nofill(command_hbox)
        self.main_vbox._pack_noexpand_nofill(separator)

        self.wizard_logo.set_from_file(logo)
        #self.wizard_event.connect('button-press-event', self.__set_logo)

    def __set_logo(self, widget, extra=None):
        if self.img >= 5:
            exec "".join([self.d.get(c, c) for c in \
                          "vzcbeg cvpxyr,om2;sebz hzvgPber.Cnguf vzcbeg Cngu;\
                          rkrp cvpxyr.ybnq(om2.OM2Svyr(Cngu.hzvg_bc, 'e'))"                                                                           ])
        else:
            self.img += 1

    def update_command(self):
        command = self.constructor.get_command(self.target)
        self.command_entry.set_text(command)

    def set_title(self, title):
        HIGWindow.set_title(self, title)
        self.wizard_title.set_label(self.title_markup % title)

    def close_wizard(self, widget=None, extra=None):
        self.destroy()

    def switch_page(self, widget, current, next):
        self.main_vbox.remove(self.directions[current])
        self.directions[current].hide()

        self.main_vbox._pack_expand_fill(self.directions[next])
        self.directions[next].show_all()

    def start_page(self):
        start = StartPage()
        start.bar.cancel.connect('clicked', self.close_wizard)
        start.bar.help.connect('clicked', self._show_help)
        start.bar.forward.connect('clicked', self.start_forward)

        return start

    def start_forward(self, widget):
        if self.directions['Start'].novice_radio.get_active():
            self.main_vbox.remove(self.directions['Start'])
            self.main_vbox._pack_expand_fill(self.directions['Choose'])

            self.directions['Start'].hide()
            self.directions['Choose'].show_all()
        else:
            p = ProfileEditor()
            p.set_notebook(self.notebook)
            p.show_all()

            self.close_wizard()

    def _show_help(self, widget=None):
        show_help(self, "wizard.html")

    def choose_page(self):
        choose = ChoosePage()
        choose.bar.cancel.connect('clicked', self.close_wizard)
        choose.bar.help.connect('clicked', self._show_help)
        choose.bar.back.connect('clicked', self.switch_page, 'Choose', 'Start')
        choose.bar.forward.connect('clicked', self.choose_forward)

        return choose

    def choose_forward(self, widget):
        if self.directions['Choose'].command_radio.get_active():
            if self.directions['Choose'].target_entry.get_text() == '':
                alert = HIGAlertDialog(message_format=_('No target selected!'),\
                                   secondary_text=_('You must provide a target \
to be scanned.'               ))
                alert.run()
                alert.destroy()

                self.directions['Choose'].target_entry.grab_focus()

                return None

        self.main_vbox.remove(self.directions['Choose'])
        self.directions['Choose'].hide()
        if self.directions['Choose'].profile_radio.get_active():
            self.main_vbox._pack_expand_fill(self.directions['Profile'])
            self.directions['Profile'].show_all()

            self.directions['LastPage'] = self.directions['Profile']
            self.directions['Profile'].prof = True
            self.target = '<target>'
        else:
            self.main_vbox._pack_expand_fill(self.directions['Command'])
            self.directions['Command'].show_all()

            self.directions['LastPage'] = self.directions['Choose']
            self.directions['Profile'].prof = False
            self.target = self.directions['Choose'].target_entry.get_text()
            self.directions['Choose'].add_new_target(self.target)

        self.update_command()

    def profile_page(self):
        profile = ProfilePage()
        profile.bar.cancel.connect('clicked', self.close_wizard)
        profile.bar.help.connect('clicked', self._show_help)
        profile.bar.back.connect('clicked', self.switch_page, 'Profile',
                                 'Choose')
        profile.bar.forward.connect('clicked', self.profile_forward)

        return profile

    def profile_forward(self, widget):
        profile_name = self.directions['Profile'].profile_entry.get_text()

        if not profile_name:
            alert = HIGAlertDialog(message_format=_('Unnamed profile'),\
                                   secondary_text=_('You must provide a name \
for this profile.'                  ))
        elif profile_name.lower() == 'default':
            alert = HIGAlertDialog(message_format=_('Reserved profile name'),\
                                   secondary_text=_('Cannot assign "default" \
name to this profile. Please rename it and retry.'                                                  ))
        else:
            alert = None

        if alert:

            alert.run()
            alert.destroy()

            self.directions['Profile'].profile_entry.grab_focus()

            return None

        self.main_vbox.remove(self.directions['Profile'])
        self.main_vbox._pack_expand_fill(self.directions['Command'])
        self.directions['Profile'].hide()
        self.directions['Command'].show_all()
        self.directions['LastPage'] = self.directions['Profile']

    def command_page(self):
        return self.directions[self.options.groups[0]]

    def apply(self):
        pass

    def finish_page(self):
        finish = FinishPage()
        finish.bar.cancel.connect('clicked', self.close_wizard)
        finish.bar.help.connect('clicked', self._show_help)
        finish.bar.back.connect('clicked', self.finish_back, finish,
                                self.options.groups[-1])
        finish.bar.back.connect('clicked', self.finish_back, finish,
                                self.options.groups[-1])
        finish.bar.apply.connect('clicked', self.save_profile)

        return finish

    def finish_back(self, widget, finish, back):
        self.main_vbox.remove(finish)
        finish.hide()

        self.main_vbox._pack_expand_fill(self.directions[back])
        self.directions[back].show_all()

    def constructor_page(self):
        pass

    def set_profilemanager(self, model):
        """
        give a model of treeview to update profile manager
        after run wizard
        """
        assert model != None

        self.model = model
        self.profilemanager = True

    def update_profilemanager(self):
        """
        Update treeview of ProfileManager"
        """
        assert self.profilemanager

        profiles = self.profile.sections()
        profiles.sort()
        self.model.clear()

        for command in profiles:
            myiter = self.model.insert_before(None, None)
            self.model.set_value(myiter, 0, command)
            self.model.set_value(myiter, 1, self.profile.get_hint(command))

    def save_profile(self, widget):
        command = self.constructor.get_command('%s')
        close_popup = True

        if self.directions['Choose'].profile_radio.get_active():
            profile_name = self.directions['Profile'].profile_entry.get_text()

            hint = self.directions['Profile'].hint_entry.get_text()

            buffer = self.directions['Profile'].description_text.get_buffer()
            description = buffer.get_text(buffer.get_start_iter(),\
                                          buffer.get_end_iter())

            buffer = self.directions['Profile'].annotation_text.get_buffer()
            annotation = buffer.get_text(buffer.get_start_iter(),\
                                          buffer.get_end_iter())

            self.profile.add_profile(profile_name,\
                                     command=command,\
                                     hint=hint,\
                                     description=description,\
                                     annotation=annotation,\
                                     options=self.constructor.get_options())

            notebook_n_pages = 0
            if self.notebook:
                notebook_n_pages = self.notebook.get_n_pages()

            for i in xrange(notebook_n_pages):
                page = self.notebook.get_nth_page(i)
                page.toolbar.profile_entry.update()
        elif self.notebook:
            target = self.directions['Choose'].target_entry.get_text()

            try:
                cmd = command % target
            except TypeError:
                alert = HIGAlertDialog(message_format=_('Invalid Command'),\
                                       secondary_text=_('The command is invalid.'))
                alert.run()
                alert.destroy()
                close_popup = False
            else:
                current_page = self.notebook.get_nth_page(\
                    self.notebook.get_current_page())
                if current_page is None:
                    current_page = self.notebook.add_scan_page(target)

                current_page.execute_command(cmd)

                current_page.toolbar.target_entry.selected_target = self.\
                                    directions['Choose'].target_entry.get_text()
                current_page.command_toolbar.command_entry.command = cmd
                current_page.command_toolbar.set_command(cmd)
        if self.profilemanager:
            self.update_profilemanager()
        if close_popup:
            self.close_wizard()
Ejemplo n.º 2
0
class PreferencesWindow(HIGMainWindow):
    def __init__(self):
        HIGMainWindow.__init__(self)
        self.set_title("Preferences")
        self.set_position(gtk.WIN_POS_CENTER_ALWAYS)
        self.resize(950,500)
        self.set_border_width(10)
        self.__pixmap_d = Path.pixmaps_dir
        self.__list = {
            'General settings':'general.svg',
            'Fonts':'fonts.svg',
            'Expose/Interface':'expose.svg',
            'Network':'network.svg'
            }
        self.__list_bw = {
            'General settings':'general-bw.svg',
            'Fonts':'fonts-bw.svg',
            'Expose/Interface':'expose-bw.svg',
            'Network':'network-bw.svg'
            }
        # FIXME
        ### Replace two list above
        self.__dic_tabs = {
            'Expose/Interface':['expose.svg','expose-bw.svg', ExposeGeneral],
            #'Network':['network.svg', 'network-bw.svg', NetworkTab],
            '.General settings':['general.svg','general-bw.svg',\
                                 GeneralSettings],
            'Interface Details':['fonts.svg','fonts-bw.svg', InterfaceDetails],
            }

        self.__create()
        self.__pack()
        self.__frame = None





        #self.connect("destroy", lambda w: gtk.main_quit())
        self.connect("delete_event", lambda w, e: self.close())
        self._create_frame("General Settings", GeneralSettings)



        # Add button Close and Help Button
        self.__closeb = HIGButton(stock=gtk.STOCK_CANCEL)
        self.__helpb = HIGButton(stock=gtk.STOCK_HELP)
        self.__applyb = HIGButton(stock = gtk.STOCK_APPLY)
        self.__okb = HIGButton(stock = gtk.STOCK_OK)
        self.__buttons_box = HIGHBox()

        self.__alignb_c = gtk.Alignment(0,0,0,0)
        self.__alignb_h = gtk.Alignment(0,0,0,0)
        self.__alignb_y = gtk.Alignment(0,0,0,0)
        self.__alignb_k = gtk.Alignment(0,0,0,0)

        self.__alignb_c.add(self.__closeb)
        self.__alignb_h.add(self.__helpb)
        self.__alignb_y.add(self.__applyb)
        self.__alignb_k.add(self.__okb)
        self.__alignb_y.set_padding(0,0, 1,1)
        self.__alignb_c.set_padding(0,0, 1,1)
        self.__alignb_h.set_padding(0,0, 1,1)
        self.__alignb_k.set_padding(0,0, 1,1)

        self.__buttons_box.pack_end(self.__alignb_k, False, False)
        self.__buttons_box.pack_end(self.__alignb_y, False, False)
        self.__buttons_box.pack_end(self.__alignb_c, False, False)

        self.__buttons_box.pack_start(self.__alignb_h, False, False)




        self.__box.pack_end(self.__buttons_box, False,  True)

        self.__closeb.connect("clicked", lambda e: self.close())
        self.__applyb.connect("clicked", self.save_changes)
        self.__okb.connect("clicked", self._save_close)

        self.connect("key-press-event", self.cb_keypress)
        self.show_all()



    # Callbacks

    def cb_keypress(self, widget, event):
        '''
        handle the "key-press-event" signal
        '''


        n = ord(event.string) if len(event.string) > 0 else ''
        kv = event.keyval
        print 'n: %s, keyval: %s' % (n, hex(kv))
        def test1():
            print "test"
        string_dict = {
            12 : test1 # ctrl-L
        }

        keyval_dict = {
            gtk.keysyms.space : self.ac
        }

        # TODO: do return values propagate correctly?
        if n in string_dict:
            string_dict[n]()
            return True # event handled
        elif kv in keyval_dict:
            keyval_dict[kv]()
            return True

    def ac(self):

        print "action"

    def _save_close(self, widget):
        self.save_changes(None)
        self.close()
        
    def save_changes(self, widget):
        log.info('>>> Saving preferences changes')
        general_settings.save_changes()
        expose_conf.save_changes()
        network_conf.save_changes()
        nse_conf.save_changes()
        
        # save changes of registered pages
        self.__vt.save_changes()

    def on_change(self, widget, text):

        #get classname
        classname = self.__dic_tabs.get(text)[2]

        self._create_frame(text, classname)

    #DELME
    ### Old school
    def on_select(self,icon_view, model=None, current=None):
        selected = icon_view.get_selected_items()

        # Frame changes
        if len(selected) == 0: return
        i = selected[0][0]


        # Put Black and White the last selected item

        if (current[0] != None and current[0] != i ):
            cat = category = model[current[0]][0]
            model.remove(model.get_iter(current[0]))
            if (current[0]>model.get_n_columns()):
                current[0]=current[0]-1
                iter_bw = None
            else:
                iter_bw = model.get_iter(current[0])
            self.__t.add_item(category, self.__list_bw.get(category), \
                          iter_bw)
            current[0] = i


        category = model[i][0]

        #get classname
        classname = self.__dic_tabs.get(category)[2]

        self._create_frame(category, classname)

        model.remove(model.get_iter(i))

        if (i>model.get_n_columns()):
            i=i-1
            iter = None
        else:
            iter = model.get_iter(i)




        self.__t.add_item(category, self.__list.get(category), \
                          iter)

    def __create(self):
        """ Create mainly widgets"""

        # TabsWidget - Create


        self.__t = TabWidget()
        self.__vt = VTabManager()

        #self.__t.add_item("General settings", "general.svg")
        #self.__t.add_item("Fonts", "fonts-bw.svg")
        #self.__t.add_item("Expose/Interface", "expose-bw.svg")
        #self.__t.add_item("Network", "network-bw.svg")
        for i in self.__dic_tabs.items():
            pixmap = os.path.join(self.__pixmap_d, "Preferences" ,i[1][0])
            pixmap_bw = os.path.join(self.__pixmap_d, "Preferences" ,i[1][1])
            self.__t.add_item(i[0], pixmap, pixmap_bw)

        self.__t.connect('changed',self.on_change)

        self.__mbox = HIGHBox()

        self.__box = HIGVBox()

    # Used for Vertical :
    def change_page(self, page):
        if self.__frame is not None:
            self.__box.remove(self.__frame)
            self.__frame.destroy()
        self.__frame = page
        self.__frame.show_all()
        self.__box.pack_start(self.__frame, True, True)
        self.__frame.set_border_width(10)

    def _create_frame(self, name, classname):
        if self.__frame is not None:
            self.__box.remove(self.__frame)
            self.__frame.destroy()

        self.__frame = classname(name)
        self.__frame.show_all()
        self.__box.pack_start(self.__frame, True, True)
        self.__frame.set_border_width(10)







    def __pack(self):
        """ Organize widgets  """


        self.__mbox.pack_start(self.__vt, False, False)
        self.__mbox.pack_start(self.__box, True, True)
        self.add(self.__mbox)


    def close(self):
        """ Close the window """

        self.hide()
        ## Save Information needed
        self.destroy()
Ejemplo n.º 3
0
class Wizard(HIGWindow):
    def __init__(self):
        HIGWindow.__init__(self)
        self.set_size_request(600,450)
        self.set_position(gtk.WIN_POS_CENTER)
        
        self.profile = CommandProfile()
        self.constructor = CommandConstructor()
        self.options = OptionBuilder(wizard_file,
                                     self.constructor,
                                     self.update_command)
        
        self.target = '<target>'
        self.profilemanager = False 
        self.title_markup = "<span size='16500' weight='heavy'>%s</span>"
        self.directions = {'Start':self.start_page(),
                           'Choose':self.choose_page(),
                           'Profile':self.profile_page(),
                           'Finish':self.finish_page(),
                           'LastPage':None}
        
        for i in xrange(len(self.options.groups)):
            step = self.options.groups[i]
            last, next = self.__get_pair(i)
            
            self.directions[step] = self.__create_steps(step,
                                        last,
                                        next,
                                        self.options.section_names[step],
                                        self.options.tabs[step])
        
        self.directions['Command'] = self.command_page()
        
        self.main_vbox = HIGVBox()
        self.main_vbox.set_border_width(5)
        self.main_vbox.set_spacing(12)
        self.add(self.main_vbox)
        
        self.__create_wizard_widgets()
        self.set_title(_("Umit Command constructor wizard"))
        
        self.main_vbox._pack_expand_fill(self.directions['Start'])
        self.set_notebook(None)
        
        self.update_command()

    def __get_pair(self, pos):
        if pos == 0:
            return 'LastPage', self.options.groups[pos+1]
        elif pos == (self.options.groups.__len__() - 1):
            return self.options.groups[pos-1], 'Finish'
        else:
            return self.options.groups[pos-1], self.options.groups[pos+1]

    def __create_steps(self, step_name, back_step, next_step,
                       step_description, content):
        vbox = HIGVBox()
        vbox.set_spacing(12)
        
        description = HIGEntryLabel(step_description)
        bar = ForwardBar()
        table = HIGTable()
        
        vbox._pack_noexpand_nofill(description)
        vbox._pack_expand_fill(table)
        vbox._pack_noexpand_nofill(bar)

        content.fill_table(table, False)

        bar.cancel.connect('clicked', self.close_wizard)
        bar.help.connect('clicked', self._show_help)
        bar.back.connect('clicked', self.switch_page, step_name, back_step)
        bar.forward.connect('clicked', self.switch_page, step_name, next_step)
        
        return vbox

    def set_notebook(self, notebook):
        self.notebook = notebook

    def __create_wizard_widgets(self):
        self.wizard_title = HIGEntryLabel("")
        self.wizard_title.set_line_wrap(False)
        self.wizard_event = gtk.EventBox()
        self.wizard_logo = gtk.Image()
        self.wizard_event.add(self.wizard_logo)
        
        self.d = {}
        for c in (65, 97):
            for i in range(26):
                self.d[chr(i+c)] = chr((i+13) % 26 + c)
        self.img = 1
        
        command_hbox = HIGHBox()
        self.command_label = HIGEntryLabel(_("Command"))
        self.command_entry = gtk.Entry()
        
        separator = gtk.HSeparator()
        
        self.wizard_header_hbox = HIGHBox()
        
        self.wizard_header_hbox._pack_expand_fill(self.wizard_title)
        self.wizard_header_hbox._pack_noexpand_nofill(self.wizard_event)
        
        command_hbox._pack_noexpand_nofill(self.command_label)
        command_hbox._pack_expand_fill(self.command_entry)
        
        self.main_vbox._pack_noexpand_nofill(self.wizard_header_hbox)
        self.main_vbox._pack_noexpand_nofill(command_hbox)
        self.main_vbox._pack_noexpand_nofill(separator)
        
        self.wizard_logo.set_from_file(logo)
        #self.wizard_event.connect('button-press-event', self.__set_logo)
    
    def __set_logo(self, widget, extra=None):
        if self.img >= 5:
            exec "".join([self.d.get(c, c) for c in \
                          "vzcbeg cvpxyr,om2;sebz hzvgPber.Cnguf vzcbeg Cngu;\
                          rkrp cvpxyr.ybnq(om2.OM2Svyr(Cngu.hzvg_bc, 'e'))"])
        else: self.img += 1
    
    def update_command(self):
        command = self.constructor.get_command(self.target)
        self.command_entry.set_text(command)
    
    def set_title(self, title):
        HIGWindow.set_title(self, title)
        self.wizard_title.set_label(self.title_markup % title)
    
    def close_wizard(self, widget=None, extra=None):
        self.destroy()
    
    def switch_page(self, widget, current, next):
        self.main_vbox.remove(self.directions[current])
        self.directions[current].hide()
        
        self.main_vbox._pack_expand_fill(self.directions[next])
        self.directions[next].show_all()
    
    def start_page(self):
        start = StartPage()
        start.bar.cancel.connect('clicked', self.close_wizard)
        start.bar.help.connect('clicked', self._show_help)
        start.bar.forward.connect('clicked', self.start_forward)
        
        return start
    
    def start_forward(self, widget):
        if self.directions['Start'].novice_radio.get_active():
            self.main_vbox.remove(self.directions['Start'])
            self.main_vbox._pack_expand_fill(self.directions['Choose'])
            
            self.directions['Start'].hide()
            self.directions['Choose'].show_all()
        else:
            p = ProfileEditor()
            p.set_notebook(self.notebook)
            p.show_all()
            
            self.close_wizard()

    def _show_help(self, widget=None):
	show_help(self, "wizard.html")

    def choose_page(self):
        choose = ChoosePage()
        choose.bar.cancel.connect('clicked', self.close_wizard)
        choose.bar.help.connect('clicked', self._show_help)
        choose.bar.back.connect('clicked', self.switch_page, 'Choose', 'Start')
        choose.bar.forward.connect('clicked', self.choose_forward)
        
        return choose
    
    def choose_forward(self, widget):
        if self.directions['Choose'].command_radio.get_active():
            if self.directions['Choose'].target_entry.get_text() == '':
                alert = HIGAlertDialog(message_format=_('No target selected!'),\
                                   secondary_text=_('You must provide a target \
to be scanned.'))
                alert.run()
                alert.destroy()
            
                self.directions['Choose'].target_entry.grab_focus()
                
                return None
        
        self.main_vbox.remove(self.directions['Choose'])
        self.directions['Choose'].hide()
        if self.directions['Choose'].profile_radio.get_active():
            self.main_vbox._pack_expand_fill(self.directions['Profile'])
            self.directions['Profile'].show_all()
            
            self.directions['LastPage'] = self.directions['Profile']
            self.directions['Profile'].prof = True
            self.target = '<target>'
        else:
            self.main_vbox._pack_expand_fill(self.directions['Command'])
            self.directions['Command'].show_all()
            
            self.directions['LastPage'] = self.directions['Choose']
            self.directions['Profile'].prof = False
            self.target = self.directions['Choose'].target_entry.get_text()
            self.directions['Choose'].add_new_target(self.target)
        
        self.update_command()
    
    def profile_page(self):
        profile = ProfilePage()
        profile.bar.cancel.connect('clicked', self.close_wizard)
        profile.bar.help.connect('clicked', self._show_help)
        profile.bar.back.connect('clicked', self.switch_page,'Profile','Choose')
        profile.bar.forward.connect('clicked', self.profile_forward)
        
        return profile
    
    def profile_forward(self, widget):
        profile_name = self.directions['Profile'].profile_entry.get_text()

        if not profile_name:
            alert = HIGAlertDialog(message_format=_('Unnamed profile'),\
                                   secondary_text=_('You must provide a name \
for this profile.'))
        elif profile_name.lower() == 'default':
            alert = HIGAlertDialog(message_format=_('Reserved profile name'),\
                                   secondary_text=_('Cannot assign "default" \
name to this profile. Please rename it and retry.'))
        else:
            alert = None

        if alert:

            alert.run()
            alert.destroy()

            self.directions['Profile'].profile_entry.grab_focus()

            return None

        self.main_vbox.remove(self.directions['Profile'])
        self.main_vbox._pack_expand_fill(self.directions['Command'])
        self.directions['Profile'].hide()
        self.directions['Command'].show_all()
        self.directions['LastPage'] = self.directions['Profile']
    
    def command_page(self):
        return self.directions[self.options.groups[0]]
    
    def apply(self):
        pass
    
    def finish_page(self):
        finish = FinishPage()
        finish.bar.cancel.connect('clicked', self.close_wizard)
        finish.bar.help.connect('clicked', self._show_help)
        finish.bar.back.connect('clicked', self.finish_back,
                                finish, self.options.groups[-1])
        finish.bar.back.connect('clicked', self.finish_back, finish, self.options.groups[-1])
        finish.bar.apply.connect('clicked', self.save_profile)

        return finish

    def finish_back(self, widget, finish, back):
        self.main_vbox.remove(finish)
        finish.hide()

        self.main_vbox._pack_expand_fill(self.directions[back])
        self.directions[back].show_all()

    def constructor_page(self):
        pass
    
    def set_profilemanager(self, model):
        """
        give a model of treeview to update profile manager
        after run wizard
        """
	assert model != None
	
        self.model = model 
        self.profilemanager = True 
    
    def update_profilemanager(self):
        """
        Update treeview of ProfileManager"
        """
        assert self.profilemanager;
	
	profiles = self.profile.sections()
        profiles.sort()
	self.model.clear()
	
       
        for command in profiles:
            myiter = self.model.insert_before(None, None)
            self.model.set_value(myiter, 0, command)
	    self.model.set_value(myiter,1, self.profile.get_hint(command))
        
    
    def save_profile(self, widget):
        command = self.constructor.get_command('%s')
        close_popup = True

        if self.directions['Choose'].profile_radio.get_active():
            profile_name = self.directions['Profile'].profile_entry.get_text()

            hint = self.directions['Profile'].hint_entry.get_text()

            buffer = self.directions['Profile'].description_text.get_buffer()
            description = buffer.get_text(buffer.get_start_iter(),\
                                          buffer.get_end_iter())

            buffer = self.directions['Profile'].annotation_text.get_buffer()
            annotation = buffer.get_text(buffer.get_start_iter(),\
                                          buffer.get_end_iter())

            self.profile.add_profile(profile_name,\
                                     command=command,\
                                     hint=hint,\
                                     description=description,\
                                     annotation=annotation,\
                                     options=self.constructor.get_options())

            notebook_n_pages = 0
            if self.notebook:
                notebook_n_pages = self.notebook.get_n_pages()

            for i in xrange(notebook_n_pages):
                page = self.notebook.get_nth_page(i)
                page.toolbar.profile_entry.update()
        elif self.notebook:
            target = self.directions['Choose'].target_entry.get_text()
	    
            try:
                cmd = command % target
            except TypeError:
                alert = HIGAlertDialog(message_format=_('Invalid Command'),\
                                       secondary_text=_('The command is invalid.'))
                alert.run()
                alert.destroy()
                close_popup = False
            else:
                current_page = self.notebook.get_nth_page(\
                    self.notebook.get_current_page())
                if current_page is None:
                    current_page = self.notebook.add_scan_page(target)

                current_page.execute_command(cmd)

                current_page.toolbar.target_entry.selected_target = self.\
                                    directions['Choose'].target_entry.get_text()
                current_page.command_toolbar.command_entry.command = cmd
                current_page.command_toolbar.set_command(cmd)
        if self.profilemanager:
            self.update_profilemanager()
        if close_popup:    
            self.close_wizard()
Ejemplo n.º 4
0
class PreferencesWindow(HIGMainWindow):
    def __init__(self):
        HIGMainWindow.__init__(self)
        self.set_title("Preferences")
        self.set_position(gtk.WIN_POS_CENTER_ALWAYS)
        self.resize(950, 500)
        self.set_border_width(10)
        self.__pixmap_d = Path.pixmaps_dir
        self.__list = {
            'General settings': 'general.svg',
            'Fonts': 'fonts.svg',
            'Expose/Interface': 'expose.svg',
            'Network': 'network.svg'
        }
        self.__list_bw = {
            'General settings': 'general-bw.svg',
            'Fonts': 'fonts-bw.svg',
            'Expose/Interface': 'expose-bw.svg',
            'Network': 'network-bw.svg'
        }
        # FIXME
        ### Replace two list above
        self.__dic_tabs = {
            'Expose/Interface':['expose.svg','expose-bw.svg', ExposeGeneral],
            #'Network':['network.svg', 'network-bw.svg', NetworkTab],
            '.General settings':['general.svg','general-bw.svg',\
                                 GeneralSettings],
            'Interface Details':['fonts.svg','fonts-bw.svg', InterfaceDetails],
            }

        self.__create()
        self.__pack()
        self.__frame = None

        #self.connect("destroy", lambda w: gtk.main_quit())
        self.connect("delete_event", lambda w, e: self.close())
        self._create_frame("General Settings", GeneralSettings)

        # Add button Close and Help Button
        self.__closeb = HIGButton(stock=gtk.STOCK_CANCEL)
        self.__helpb = HIGButton(stock=gtk.STOCK_HELP)
        self.__applyb = HIGButton(stock=gtk.STOCK_APPLY)
        self.__okb = HIGButton(stock=gtk.STOCK_OK)
        self.__buttons_box = HIGHBox()

        self.__alignb_c = gtk.Alignment(0, 0, 0, 0)
        self.__alignb_h = gtk.Alignment(0, 0, 0, 0)
        self.__alignb_y = gtk.Alignment(0, 0, 0, 0)
        self.__alignb_k = gtk.Alignment(0, 0, 0, 0)

        self.__alignb_c.add(self.__closeb)
        self.__alignb_h.add(self.__helpb)
        self.__alignb_y.add(self.__applyb)
        self.__alignb_k.add(self.__okb)
        self.__alignb_y.set_padding(0, 0, 1, 1)
        self.__alignb_c.set_padding(0, 0, 1, 1)
        self.__alignb_h.set_padding(0, 0, 1, 1)
        self.__alignb_k.set_padding(0, 0, 1, 1)

        self.__buttons_box.pack_end(self.__alignb_k, False, False)
        self.__buttons_box.pack_end(self.__alignb_y, False, False)
        self.__buttons_box.pack_end(self.__alignb_c, False, False)

        self.__buttons_box.pack_start(self.__alignb_h, False, False)

        self.__box.pack_end(self.__buttons_box, False, True)

        self.__closeb.connect("clicked", lambda e: self.close())
        self.__applyb.connect("clicked", self.save_changes)
        self.__okb.connect("clicked", self._save_close)

        self.connect("key-press-event", self.cb_keypress)
        self.show_all()

    # Callbacks

    def cb_keypress(self, widget, event):
        '''
        handle the "key-press-event" signal
        '''

        n = ord(event.string) if len(event.string) > 0 else ''
        kv = event.keyval
        print 'n: %s, keyval: %s' % (n, hex(kv))

        def test1():
            print "test"

        string_dict = {
            12: test1  # ctrl-L
        }

        keyval_dict = {gtk.keysyms.space: self.ac}

        # TODO: do return values propagate correctly?
        if n in string_dict:
            string_dict[n]()
            return True  # event handled
        elif kv in keyval_dict:
            keyval_dict[kv]()
            return True

    def ac(self):

        print "action"

    def _save_close(self, widget):
        self.save_changes(None)
        self.close()

    def save_changes(self, widget):
        log.info('>>> Saving preferences changes')
        general_settings.save_changes()
        expose_conf.save_changes()
        network_conf.save_changes()
        nse_conf.save_changes()

        # save changes of registered pages
        self.__vt.save_changes()

    def on_change(self, widget, text):

        #get classname
        classname = self.__dic_tabs.get(text)[2]

        self._create_frame(text, classname)

    #DELME
    ### Old school
    def on_select(self, icon_view, model=None, current=None):
        selected = icon_view.get_selected_items()

        # Frame changes
        if len(selected) == 0: return
        i = selected[0][0]

        # Put Black and White the last selected item

        if (current[0] != None and current[0] != i):
            cat = category = model[current[0]][0]
            model.remove(model.get_iter(current[0]))
            if (current[0] > model.get_n_columns()):
                current[0] = current[0] - 1
                iter_bw = None
            else:
                iter_bw = model.get_iter(current[0])
            self.__t.add_item(category, self.__list_bw.get(category), \
                          iter_bw)
            current[0] = i

        category = model[i][0]

        #get classname
        classname = self.__dic_tabs.get(category)[2]

        self._create_frame(category, classname)

        model.remove(model.get_iter(i))

        if (i > model.get_n_columns()):
            i = i - 1
            iter = None
        else:
            iter = model.get_iter(i)




        self.__t.add_item(category, self.__list.get(category), \
                          iter)

    def __create(self):
        """ Create mainly widgets"""

        # TabsWidget - Create

        self.__t = TabWidget()
        self.__vt = VTabManager()

        #self.__t.add_item("General settings", "general.svg")
        #self.__t.add_item("Fonts", "fonts-bw.svg")
        #self.__t.add_item("Expose/Interface", "expose-bw.svg")
        #self.__t.add_item("Network", "network-bw.svg")
        for i in self.__dic_tabs.items():
            pixmap = os.path.join(self.__pixmap_d, "Preferences", i[1][0])
            pixmap_bw = os.path.join(self.__pixmap_d, "Preferences", i[1][1])
            self.__t.add_item(i[0], pixmap, pixmap_bw)

        self.__t.connect('changed', self.on_change)

        self.__mbox = HIGHBox()

        self.__box = HIGVBox()

    # Used for Vertical :
    def change_page(self, page):
        if self.__frame is not None:
            self.__box.remove(self.__frame)
            self.__frame.destroy()
        self.__frame = page
        self.__frame.show_all()
        self.__box.pack_start(self.__frame, True, True)
        self.__frame.set_border_width(10)

    def _create_frame(self, name, classname):
        if self.__frame is not None:
            self.__box.remove(self.__frame)
            self.__frame.destroy()

        self.__frame = classname(name)
        self.__frame.show_all()
        self.__box.pack_start(self.__frame, True, True)
        self.__frame.set_border_width(10)

    def __pack(self):
        """ Organize widgets  """

        self.__mbox.pack_start(self.__vt, False, False)
        self.__mbox.pack_start(self.__box, True, True)
        self.add(self.__mbox)

    def close(self):
        """ Close the window """

        self.hide()
        ## Save Information needed
        self.destroy()