コード例 #1
0
ファイル: SettingsWin.py プロジェクト: aregee/network-scanner
class NISettingsBox():
    
    def __init__(self, create_buttons=True):

        self.__create_widgets()

        if(create_buttons):
            self.__create_buttons()

        self._fill_comboboxes()
        self._load_settings()
        self.main_vbox = self.__do_layout(create_buttons)
        
        
    def __create_widgets(self):
        self.timeline = HIGFrame(_("Timeline Settings"))
        self.tl_mode_lbl = gtk.Label(_("Timeline view mode"))
        self.tl_kind_lbl = gtk.Label(_("Timeline view kind"))
        self.tl_mode = gtk.combo_box_new_text()
        self.tl_kind = gtk.combo_box_new_text()

        self.tabs = HIGFrame(_("Devices tab"))
        self.tabs_cbtn = gtk.CheckButton(_("Place close button"))

        self.sbar = HIGFrame(_("Statusbar"))
        self.sbar_tips = gtk.CheckButton(_("Show tips"))
        
        
    def __create_buttons(self):
        # bottom buttons
        self.apply = HIGButton(stock=gtk.STOCK_APPLY)
        self.cancel = HIGButton(stock=gtk.STOCK_CANCEL)
        self.ok = HIGButton(stock=gtk.STOCK_OK)

        self.apply.connect("clicked", self._apply_settings)
        self.ok.connect("clicked", self._apply_settings_exit)
        self.cancel.connect("clicked", self._exit)

    def _fill_comboboxes(self):
        """
        Fill timeline mode and timeline kind combo boxes with available
        options.
        """
        self.tl_kind_opts = { }
        self.tl_mode_opts = { }

        for opt, value in view_kind.items():
            self.tl_kind.append_text(value)
            self.tl_kind_opts[opt] = value

        for opt, value in view_mode.items():
            self.tl_mode.append_text(value)
            self.tl_mode_opts[opt] = value


    def _load_settings(self):
        """
        Load current settings values.
        """
        options = startup_options()
        start_tlkind = self.tl_kind_opts[options['kind']]
        start_tlmode = self.tl_mode_opts[options['mode']]

        store = self.tl_kind.get_model()
        for indx, item in enumerate(store):
            if item[0] == start_tlkind:
                self.tl_kind.set_active(indx)
                break

        store = self.tl_mode.get_model()
        for indx, item in enumerate(store):
            if item[0] == start_tlmode:
                self.tl_mode.set_active(indx)
                break

        self.tabs_cbtn.set_active(options['tabs_close_btn'])
        self.sbar_tips.set_active(options['tips'])


    def _apply_settings(self, event):
        """
        Apply current settings.
        """
        curr_kind = self.tl_kind.get_active_text()
        for kind, value in self.tl_kind_opts.items():
            if value == curr_kind:
                curr_kind = kind
                break

        curr_mode = self.tl_mode.get_active_text()
        for mode, value in self.tl_mode_opts.items():
            if value == curr_mode:
                curr_mode = mode
                break

        write_startup_setting(TL_SECTION, "mode", curr_mode)
        write_startup_setting(TL_SECTION, "kind", curr_kind)
        write_startup_setting(INV_SECTION, "tabs_close_btn",
            self.tabs_cbtn.get_active())
        write_startup_setting(INV_SECTION, "tips", self.sbar_tips.get_active())


    def _apply_settings_exit(self, event):
        """
        Apply current settings and leave.
        """
        self._apply_settings(None)
        self._exit(None)


    def _exit(self, event):
        """
        Close this window.
        """
        self.destroy()


    def __set_props(self):
        """
        Window properties.
        """
        self.set_title(_("Network Inventory Settings"))
        self.set_default_size(305, -1)


    def __do_layout(self, create_buttons):
        main_vbox = HIGVBox()

        # timeline frame
        tl_settings_align = gtk.Alignment(0.5, 0.5, 1, 1)
        tl_settings_align.set_padding(6, 0, 12, 0)
        tl_settings_vbox = HIGVBox()

        mode_hbox = HIGHBox()
        mode_hbox._pack_noexpand_nofill(self.tl_mode_lbl)
        mode_hbox._pack_expand_fill(self.tl_mode)

        kind_hbox = HIGHBox()
        kind_hbox._pack_noexpand_nofill(self.tl_kind_lbl)
        kind_hbox._pack_expand_fill(self.tl_kind)

        tl_settings_vbox._pack_noexpand_nofill(mode_hbox)
        tl_settings_vbox._pack_noexpand_nofill(kind_hbox)
        tl_settings_align.add(tl_settings_vbox)
        self.timeline.add(tl_settings_align)
        main_vbox._pack_noexpand_nofill(self.timeline)
        # end timeline frame

        # statusbar frame
        sbar_settings_align = gtk.Alignment(0.5, 0.5, 1, 1)
        sbar_settings_align.set_padding(6, 0, 12, 0)
        sbar_settings_vbox = HIGVBox()

        sbar_settings_vbox._pack_noexpand_nofill(self.sbar_tips)
        sbar_settings_align.add(sbar_settings_vbox)
        self.sbar.add(sbar_settings_align)
        main_vbox._pack_noexpand_nofill(self.sbar)
        # end statusbar frame

        # tabs frame
        tabs_settings_align = gtk.Alignment(0.5, 0.5, 1, 1)
        tabs_settings_align.set_padding(6, 0, 12, 0)
        tabs_settings_vbox = HIGVBox()

        tabs_settings_vbox._pack_noexpand_nofill(self.tabs_cbtn)
        tabs_settings_align.add(tabs_settings_vbox)
        self.tabs.add(tabs_settings_align)
        main_vbox._pack_noexpand_nofill(self.tabs)
        # end tabs frame

        if create_buttons:
            # buttons box
            btnsbox = HIGHBox()
            btnsbox._pack_noexpand_nofill(self.apply)
            btnsbox._pack_noexpand_nofill(self.cancel)
            btnsbox._pack_noexpand_nofill(self.ok)
            bbox = gtk.HBox()
            bbox.pack_end(btnsbox, False, False, 0)
            
            main_vbox.pack_end(bbox, False, False, 0)
            main_vbox.pack_end(gtk.HSeparator(), False, False, 0)
            # end buttons box

        return main_vbox
    
    
    def get_layout(self):
        return self.main_vbox
コード例 #2
0
class ProfileManager(HIGWindow):
    """
    Create a Profile Manager 
    """
    def __init__(self, daddy=None):
        HIGWindow.__init__(self, type=gtk.WINDOW_TOPLEVEL)
        self.set_title('Profile Manager')
        self.set_position(gtk.WIN_POS_CENTER)
        self.__create_widgets()
        self.add(self.vbox_main)
        self.__fill_widgets()
        self.__pack_widgets()
        self.__scan_notebook = None
        
        self.daddy = daddy


    def __create_widgets(self):

        self.vbox_main = HIGVBox()

        self.main_frame = HIGFrame("Profiles")
        #self.main_frame.set_shadow_type(gtk.SHADOW_ETCHED_OUT)

        self.align = gtk.Alignment(0.0, 0.0, 1.0, 1.0)
        self.align.set_padding(0,0,12,0)

        self.vbox = HIGVBox()
        self.profiles_sw = HIGScrolledWindow()
        #TreeView
        self.model = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_STRING)
        #self.modelfilter = self.model.filter_new()
        self.profiles_tv = gtk.TreeView(self.model)
        renderer = gtk.CellRendererText()
        column = gtk.TreeViewColumn(_("Name"), renderer, text=0)
        self.profiles_tv.append_column(column)
        renderer_hint = gtk.CellRendererText()
        column_hint = gtk.TreeViewColumn(_("Hint"), renderer_hint, text=1)
        self.profiles_tv.append_column(column_hint)
        #self.profiles_tv.set_model(self.modelfilter)
        #Info 
        self.hbox_info = HIGHBox()
        self.command_label = HIGEntryLabel('Command: ')
        self.command_entry = HIGTextEntry()
        self.command_entry.set_editable(False)

        #Buttons
        self.hbox_buttons = HIGHBox()

        self.wiz_button = HIGButton(title='Wizard', stock='gtk-convert')
        self.wiz_button.connect("clicked", self.new_wiz)

        self.edit_button = HIGButton(stock='gtk-edit')
        self.edit_button.connect("clicked", self.open_peditor)
        self.new_button = HIGButton(stock='gtk-new')
        self.new_button.connect("clicked", self.open_peditor)
        self.copy_button = HIGButton(stock='gtk-copy')
        self.copy_button.connect("clicked", self.copy_profiles)
        self.delete_button = HIGButton(stock=gtk.STOCK_DELETE)
        self.delete_button.connect('clicked', self.delete_profile)	
        #Apply Buttons
        self.cancel_button = HIGButton(stock='gtk-close')
        self.cancel_button.connect("clicked", self.quit)
        
        self.connect("delete-event", self.quit)

    def __fill_widgets(self):


        self.profiles = CommandProfile()
        self._reload_profile_list()

        #selection = self.profiles_tv.get_selection()
        #selection.connect("changed", self.change_nmap_command)
        self.profiles_tv.connect("cursor-changed", self.change_nmap_command)

    def __pack_widgets(self):
        """
        Pack all widgets of windows 
        """
        self.vbox_main.pack_start(self.main_frame, True, True)
        self.main_frame.add(self.align)
        self.align.add(self.vbox)
        self.vbox.set_border_width(6)

        self.vbox.pack_start(self.profiles_sw, True, True, 0)

        self.hbox_info.pack_start(self.command_label, False,False,0)
        self.hbox_info.pack_start(self.command_entry, True, True, 0)
        self.vbox.pack_start(self.hbox_info, False,False,0)



        self.hbox_buttons.pack_end(self.cancel_button)    
        self.hbox_buttons.pack_end(self.copy_button, True, True)
        self.hbox_buttons.pack_end(self.edit_button, True, True)
        self.hbox_buttons.pack_end(self.delete_button, True, True)
        self.hbox_buttons.pack_end(self.new_button, True, True)
        self.hbox_buttons.pack_end(self.wiz_button, True, True)
        self.hbox_buttons.set_spacing(6)
        self.vbox_main.pack_start(self.hbox_buttons, False, False)

        self.profiles_sw.set_size_request(400,170)
        self.profiles_sw.add(self.profiles_tv) 


    def get_selected_profile(self):
        """
        Returns the string with name of selected profile 
        """
        try:
            treeselection = self.profiles_tv.get_selection()
            (model,iter) = treeselection.get_selected()	
            return model.get_value(iter,0)
        except:
            return None

    def change_nmap_command(self,widget_tv):
        """
        Change a nmap command at command entry
        """
        assert widget_tv is not None
        # it call __init__ because when wizard or profile are open,  
        # need update profiles
        self.profiles.__init__()
        # update text entry of command
        self.command_entry.set_text(self.profiles.get_command(
            self.get_selected_profile()))	

    def new_wiz(self,widget):
        w = Wizard()
        w.set_notebook(None)
        w.set_profilemanager(self.model)
        w.show_all()

    def open_peditor(self, widget):
        """
        Open Profile Editor with a Selected or Non-Selected(New) Item
        """
        assert widget is not None

        if widget.get_label() == "gtk-edit":
            # Edit profile selected    
            if self.get_selected_profile() is not None:
                pe = ProfileEditor(self.get_selected_profile())
                pe.set_notebook(self.__scan_notebook)
                pe.set_profilemanager(self.model)
                pe.show_all()
        else:
            # New Profile
            pe = ProfileEditor()
            pe.set_notebook(self.__scan_notebook)
            pe.set_profilemanager(self.model)
            pe.show_all()
            self._reload_profile_list()

    def copy_profiles(self, widget):
        """
        Copy selected Profile
        """
        if self.get_selected_profile() is None:
            return None
        d = ProfileName(_("Insert a profile name"))
        profile_name = d.run()
        if profile_name is None or profile_name=="":
            return None
        #get commands of selected profile
        profile_selected = self.get_selected_profile()
        command = self.profiles.get_command(profile_selected)
        hint = self.profiles.get_hint(profile_selected)
        description = self.profiles.get_description(profile_selected)
        annotation = self.profiles.get_annotation(profile_selected)
        #Options
        prof = self.profiles.get_profile(profile_selected)
        options_used = prof['options']
        options = CommandConstructor(options_used)

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



        myiter = self.model.insert_before(None, None)
        self.model.set_value(myiter, 0, profile_name)
        self.model.set_value(myiter,1, self.profiles.get_hint(profile_name))
        treeselection = self.profiles_tv.get_selection()
        treeselection.select_iter(myiter)
        #(model,iter) = treeselection.get_selected()	
        #model.get_value(iter,0)


    def delete_profile(self, widget=None):
        """
        delete profile 
        """
        if self.get_selected_profile() is None:
            return None
        self.profiles.remove_profile(self.get_selected_profile())
        #Update treeview
        treeselection = self.profiles_tv.get_selection()
        (model,iter) = treeselection.get_selected()
        model.remove(iter)
        # update text entry of command
        self.command_entry.set_text('')	



    def _reload_profile_list(self):
        """
        Reload a list of profiles
        """
        profiles = []
        all_profiles = self.profiles.sections()
        for profile in all_profiles:
            if self.profiles.get(profile, 'tool') == 'nmap':
                profiles.append(profile)
        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.profiles.get_hint(command))
            #self.model.append([command,self.profiles.get_hint(command)])

    def set_notebook(self, notebook):
        self.__scan_notebook = notebook
    def quit(self, widget):
        self.destroy()

    def quit(self, widget, data=None):
        if self.daddy:
            self.daddy.running_pm = False
            self.destroy()
        else:
            gtk.main_quit()
コード例 #3
0
class SchedSchemaEditor(HIGWindow):
    """
    Scheduler Schemas Editor
    """

    def __init__(self, daddy=None):
        HIGWindow.__init__(self)

        self.daddy = daddy
        self.wtitle = _("Scan Scheduler Editor")

        # header
        self.title_markup = "<span size='16500' weight='heavy'>%s</span>"
        self.ttitle = HIGEntryLabel("")
        self.ttitle.set_line_wrap(False)
        self.ttitle.set_markup(self.title_markup % self.wtitle)
        self.umit_logo = gtk.Image()
        self.umit_logo.set_from_file(logo)
        # schemas name
        self.schema_name_lbl = HIGEntryLabel(_("Schema Name"))
        self.schema_name = gtk.combo_box_entry_new_text()
        self.schema_name.connect("changed", self._check_schema)
        # target and scan profiles
        # self.target_lbl = HIGEntryLabel(_("Target"))
        # self.target = gtk.Entry()
        self.scan_name_lbl = HIGEntryLabel(_("Scan Profile"))
        self.scan_name = ProfileCombo()
        self.scan_name.update()
        self.scan_name.set_active(0)
        self.scan_name.connect("changed", self._set_scan_command)
        # scan command
        self.scan_command_lbl = HIGEntryLabel(_("Command"))
        self.scan_command = gtk.Entry()
        # scheduling profile
        self.sched_name_lbl = HIGEntryLabel(_("Scheduling Profile"))
        self.sched_name = gtk.combo_box_new_text()
        self.sched_name_edit = gtk.Button(stock=gtk.STOCK_EDIT)
        blbl = self.sched_name_edit.get_children()[0].get_children()[0].get_children()[1]
        blbl.set_text(_("Edit Profiles"))
        self.sched_name_edit.connect("clicked", self._edit_schedprofiles)
        # schema settings
        self.schema_sett_frame = HIGFrame()
        self.setting_saveto = gtk.CheckButton(_("Save outputs to"))
        self.setting_saveto_entry = gtk.Entry()
        self.setting_saveto_browse = gtk.Button(_("..."))
        self.setting_saveto_browse.connect("clicked", self._select_saveto)
        self.setting_mailto = gtk.CheckButton(_("Send output to email"))
        self.setting_mailto_entry = gtk.Entry()
        self.setting_smtp_lbl = HIGEntryLabel(_("SMTP Schema"))
        self.setting_smtp = gtk.combo_box_new_text()
        self.setting_addtoinv = gtk.CheckButton(_("Add to the Inventory"))
        self.setting_enabled = gtk.CheckButton(_("Enabled"))
        # bottom buttons
        self.help = HIGButton(stock=gtk.STOCK_HELP)
        self.help.connect("clicked", self._show_help)
        self.apply = HIGButton(stock=gtk.STOCK_APPLY)
        self.apply.connect("clicked", self._save_schema)
        self.cancel = HIGButton(stock=gtk.STOCK_CANCEL)
        self.cancel.connect("clicked", self._exit)
        self.ok = HIGButton(stock=gtk.STOCK_OK)
        self.ok.connect("clicked", self._save_schema_and_leave)

        self.load_smtp_schemas()
        self._set_scan_command(None)
        self.profile_running = None  # no SchedProfileEditor instance is running.
        self._load_pscheds()
        self.load_schemas()

        self.__set_props()
        self.__do_layout()

        self.connect("destroy", self._exit)

    def load_smtp_schemas(self):
        """
        Load smtp profiles.
        """
        schemas = ConfigParser()
        schemas.read(Path.smtp_schemas)

        self.smtp_sections = []
        self.setting_smtp.get_model().clear()
        for section in schemas.sections():
            self.smtp_sections.append(section)
            self.setting_smtp.append_text(section)

        self.setting_smtp.set_active(0)

    def load_schemas(self):
        """
        Load schemas profiles.
        """
        schemas = ConfigParser()
        schemas.read(Path.sched_schemas)

        self.sections = []
        self.schema_name.get_model().clear()
        for section in schemas.sections():
            self.sections.append(section)
            self.schema_name.append_text(section)

        self.schema_name.set_active(0)
        self._check_schema(None)

    def _load_schema(self):
        """
        Load current set schedule schema.
        """
        schema = ConfigParser()
        schema.read(Path.sched_schemas)

        values = {
            "command": self.scan_command.set_text,
            "saveto": self.setting_saveto_entry.set_text,
            "mailto": self.setting_mailto_entry.set_text,
        }
        enable = {"saveto": self.setting_saveto.set_active, "mailto": self.setting_mailto.set_active}

        for item in schema.items(self.schema_name.get_active_text()):
            if item[0] == "addtoinv":
                self.setting_addtoinv.set_active(int(item[1]))
                if item[1] == "2":
                    self.apply.set_sensitive(False)
                    self.ok.set_sensitive(False)
                else:
                    self.apply.set_sensitive(True)
                    self.ok.set_sensitive(True)
            elif item[0] == "enabled":
                self.setting_enabled.set_active(int(item[1]))
            elif item[0] == "profile":
                pindx = self.profiles.index(item[1])
                self.sched_name.set_active(pindx)
            elif item[0] == "smtp":
                if item[1]:
                    pindx = self.smtp_sections.index(item[1])
                    self.setting_smtp.set_active(pindx)
            else:
                values[item[0]](item[1])
                if item[0] in ("saveto", "mailto"):
                    if len(item[1]):
                        enable[item[0]](True)
                    else:
                        enable[item[0]](False)

    def _check_schema(self, event):
        """
        Check if current text in schema_name combobox is a schema name.
        """
        if self.schema_name.get_active_text() in self.sections:
            # load schema
            self._load_schema()
        else:
            # reset to default values
            self.apply.set_sensitive(True)
            self.ok.set_sensitive(True)
            self.setting_addtoinv.set_active(False)
            self.setting_enabled.set_active(False)
            self.setting_mailto.set_active(False)
            self.setting_mailto_entry.set_text("")
            self.setting_saveto.set_active(False)
            self.setting_saveto_entry.set_text("")

        self.schema_sett_frame._set_label(self.schema_name.get_active_text() + " - Settings")

    def _set_scan_command(self, event):
        """
        Set scan command based on chosen profile.
        """
        profile = self.scan_name.get_selected_profile()
        cmd_profile = CommandProfile()
        command = cmd_profile.get_command(profile)
        self.scan_command.set_text(command % "<target>")

    def _load_pscheds(self):
        """
        Load scheduling profiles.
        """
        pscheds = ConfigParser()
        pscheds.read(Path.sched_profiles)

        self.profiles = []
        self.sched_name.get_model().clear()
        for section in pscheds.sections():
            self.sched_name.append_text(section)
            self.profiles.append(section)

        self.sched_name.set_active(0)

    def _edit_schedprofiles(self, event):
        """
        Open Scheduling Profiles Editor.
        """
        if self.profile_running:
            return

        win = SchedProfileEditor(self, self.sched_name.get_active_text())
        win.show_all()
        self.profile_running = win

    def _select_saveto(self, event):
        """
        Select directory to save file.
        """
        dir_chooser = DirectoryChooserDialog(_("Select a directory"))

        dir_chooser.run()
        dir_chosen = dir_chooser.get_filename()
        dir_chooser.destroy()
        self.setting_saveto_entry.set_text(dir_chosen)

    def _save_schema(self, event):
        """
        Save current schema.
        """
        schema = self.schema_name.get_active_text()
        command = self.scan_command.get_text()
        schedule = self.sched_name.get_active_text()
        mailto = self.setting_mailto.get_active()

        if not schema or not schedule or not command or "<target>" in command:
            dlg = HIGAlertDialog(
                self,
                message_format=_(
                    "Scheduling Schema - Error\
 while saving."
                ),
                secondary_text=_(
                    'There is some error in at \
least one of the following fields: "Schema name", "Command" or "Scheduling\
 Profile"\n\nCheck if "Schema name" is not empty.\nCheck if "Command" does\
 contain "<target>" on it.\nCheck if there is some "Scheduling Profile" \
selected.'
                ),
            )

            dlg.run()
            dlg.destroy()
            return

        if mailto and not self.setting_smtp.get_active_text():
            dlg = HIGAlertDialog(
                self,
                message_format=_(
                    "Scheduling Schema - Error\
 while saving."
                ),
                secondary_text=_(
                    "You need to create a \
a SMTP Schema for sending email."
                ),
            )

            dlg.run()
            dlg.destroy()
            return

        # check for output existance
        if self.setting_saveto.get_active() and not os.path.isdir(self.setting_saveto_entry.get_text()):

            dlg = HIGAlertDialog(
                self,
                message_format=_(
                    "Scheduling Schema - Error\
 while saving."
                ),
                secondary_text=_(
                    "You especified an invalid \
directory to save scans output."
                ),
            )

            dlg.run()
            dlg.destroy()
            return

        # write schema to file
        s_cfg = ConfigParser()
        s_cfg.read(Path.sched_schemas)

        if not s_cfg.has_section(schema):
            new_sec = True
            s_cfg.add_section(schema)
        else:
            new_sec = False

        s_cfg.set(schema, "profile", schedule)
        s_cfg.set(schema, "command", command)
        if self.setting_enabled.get_active():
            s_cfg.set(schema, "enabled", "1")
        else:
            s_cfg.set(schema, "enabled", "0")
        if self.setting_addtoinv.get_active():
            s_cfg.set(schema, "addtoinv", "1")
        else:
            s_cfg.set(schema, "addtoinv", "0")
        if self.setting_saveto.get_active():
            s_cfg.set(schema, "saveto", self.setting_saveto_entry.get_text())
        else:
            s_cfg.set(schema, "saveto", "")
        if mailto:
            s_cfg.set(schema, "mailto", self.setting_mailto_entry.get_text())
            s_cfg.set(schema, "smtp", self.setting_smtp.get_active_text())
        else:
            s_cfg.set(schema, "mailto", "")
            s_cfg.set(schema, "smtp", "")

        s_cfg.write(open(Path.sched_schemas, "w"))

        if new_sec:
            self.load_schemas()

        if self.daddy:
            self.daddy.load_schemas()

    def _save_schema_and_leave(self, event):
        """
        Save current schema and close editor.
        """
        self._save_schema(None)
        self._exit(None)

    def _show_help(self, event):
        """
        Show help for Scan Scheduler Editor.
        """
        show_help(self, "scheduler.html#setting-up-a-schedule")

    def __set_props(self):
        """
        Set window properties.
        """
        self.set_title(self.wtitle)
        self.set_default_size(440, -1)

    def __do_layout(self):
        """
        Layout widgets in window.
        """
        main_vbox = HIGVBox()
        main_vbox.set_border_width(5)
        main_vbox.set_spacing(12)
        header_hbox = HIGHBox()
        schema_table = HIGTable()
        schedsn_hbox = HIGHBox()
        sett_table = HIGTable()
        btns_hbox = HIGHBox()

        header_hbox._pack_expand_fill(self.ttitle)
        header_hbox._pack_noexpand_nofill(self.umit_logo)

        # schema name
        schema_table.attach_label(self.schema_name_lbl, 0, 1, 0, 1)
        schema_table.attach_entry(self.schema_name, 1, 2, 0, 1)

        # target and scan profile
        schema_table.attach_label(self.scan_name_lbl, 0, 1, 1, 2)
        schema_table.attach_entry(self.scan_name, 1, 2, 1, 2)

        # scan command
        schema_table.attach_label(self.scan_command_lbl, 0, 1, 2, 3)
        schema_table.attach_label(self.scan_command, 1, 2, 2, 3)

        # scheduling profile
        schedsn_hbox._pack_expand_fill(self.sched_name)
        schedsn_hbox._pack_noexpand_nofill(self.sched_name_edit)

        schema_table.attach_label(self.sched_name_lbl, 0, 1, 3, 4)
        schema_table.attach_entry(schedsn_hbox, 1, 2, 3, 4)

        # settings frame
        settings_align = gtk.Alignment(0.5, 0.5, 1, 1)
        settings_align.set_padding(6, 0, 12, 0)
        schemasett_hbox = HIGVBox()

        # saveto
        sett_hbox = HIGHBox()
        sett_hbox._pack_expand_fill(self.setting_saveto_entry)
        sett_hbox._pack_noexpand_nofill(self.setting_saveto_browse)
        sett_table.attach_label(self.setting_saveto, 0, 1, 0, 1)
        sett_table.attach_entry(sett_hbox, 1, 2, 0, 1)

        # mailto, smtp
        sett_hbox = HIGHBox()
        sett_hbox._pack_expand_fill(self.setting_mailto_entry)
        sett_hbox._pack_noexpand_nofill(self.setting_smtp_lbl)
        sett_hbox._pack_expand_fill(self.setting_smtp)
        sett_table.attach_label(self.setting_mailto, 0, 1, 1, 2)
        sett_table.attach_entry(sett_hbox, 1, 2, 1, 2)
        schemasett_hbox._pack_noexpand_nofill(sett_table)

        # add to inventory
        sett_hbox = HIGHBox()
        sett_hbox._pack_noexpand_nofill(self.setting_addtoinv)
        schemasett_hbox._pack_noexpand_nofill(sett_hbox)

        # enabled/disabled
        sett_hbox = HIGHBox()
        sett_hbox._pack_noexpand_nofill(self.setting_enabled)
        schemasett_hbox._pack_noexpand_nofill(sett_hbox)
        settings_align.add(schemasett_hbox)

        self.schema_sett_frame.add(settings_align)

        # bottom buttons
        btns_hbox.set_homogeneous(True)
        btns_hbox._pack_expand_fill(self.help)
        btns_hbox._pack_expand_fill(hig_box_space_holder())
        btns_hbox._pack_expand_fill(self.apply)
        btns_hbox._pack_expand_fill(self.cancel)
        btns_hbox._pack_expand_fill(self.ok)

        main_vbox._pack_noexpand_nofill(header_hbox)
        main_vbox._pack_noexpand_nofill(gtk.HSeparator())
        main_vbox._pack_noexpand_nofill(schema_table)
        main_vbox._pack_noexpand_nofill(gtk.HSeparator())
        main_vbox._pack_noexpand_nofill(self.schema_sett_frame)
        main_vbox.pack_end(btns_hbox, False, False, 0)

        self.add(main_vbox)

    def _exit(self, event):
        """
        Close current and window and profile editor if it is running.
        """
        if self.profile_running:
            self.profile_running._exit(None)

        if self.daddy:
            self.daddy.schemawin = None

        self.destroy()

    def _get_profile_running(self):
        """
        Get profile editor running instance.
        """
        return self.__profilerunning

    def _set_profile_running(self, running):
        """
        Set profile editor instance.
        """
        self.__profilerunning = running

    # Properties
    profile_running = property(_get_profile_running, _set_profile_running)
class SchedSchemaEditor(HIGWindow):
    """
    Scheduler Schemas Editor
    """
    def __init__(self, daddy=None):
        HIGWindow.__init__(self)

        self.daddy = daddy
        self.wtitle = _("Scan Scheduler Editor")

        # header
        self.title_markup = "<span size='16500' weight='heavy'>%s</span>"
        self.ttitle = HIGEntryLabel("")
        self.ttitle.set_line_wrap(False)
        self.ttitle.set_markup(self.title_markup % self.wtitle)
        self.umit_logo = gtk.Image()
        self.umit_logo.set_from_file(logo)
        # schemas name
        self.schema_name_lbl = HIGEntryLabel(_("Schema Name"))
        self.schema_name = gtk.combo_box_entry_new_text()
        self.schema_name.connect('changed', self._check_schema)
        # target and scan profiles
        #self.target_lbl = HIGEntryLabel(_("Target"))
        #self.target = gtk.Entry()
        self.scan_name_lbl = HIGEntryLabel(_("Scan Profile"))
        self.scan_name = ProfileCombo()
        self.scan_name.update()
        self.scan_name.set_active(0)
        self.scan_name.connect('changed', self._set_scan_command)
        # scan command
        self.scan_command_lbl = HIGEntryLabel(_("Command"))
        self.scan_command = gtk.Entry()
        # scheduling profile
        self.sched_name_lbl = HIGEntryLabel(_("Scheduling Profile"))
        self.sched_name = gtk.combo_box_new_text()
        self.sched_name_edit = gtk.Button(stock=gtk.STOCK_EDIT)
        blbl = self.sched_name_edit.get_children()[0].get_children(
        )[0].get_children()[1]
        blbl.set_text(_("Edit Profiles"))
        self.sched_name_edit.connect('clicked', self._edit_schedprofiles)
        # schema settings
        self.schema_sett_frame = HIGFrame()
        self.setting_saveto = gtk.CheckButton(_("Save outputs to"))
        self.setting_saveto_entry = gtk.Entry()
        self.setting_saveto_browse = gtk.Button(_("..."))
        self.setting_saveto_browse.connect('clicked', self._select_saveto)
        self.setting_mailto = gtk.CheckButton(_("Send output to email"))
        self.setting_mailto_entry = gtk.Entry()
        self.setting_smtp_lbl = HIGEntryLabel(_("SMTP Schema"))
        self.setting_smtp = gtk.combo_box_new_text()
        self.setting_addtoinv = gtk.CheckButton(_("Add to the Inventory"))
        self.setting_enabled = gtk.CheckButton(_("Enabled"))
        # bottom buttons
        self.help = HIGButton(stock=gtk.STOCK_HELP)
        self.help.connect('clicked', self._show_help)
        self.apply = HIGButton(stock=gtk.STOCK_APPLY)
        self.apply.connect('clicked', self._save_schema)
        self.cancel = HIGButton(stock=gtk.STOCK_CANCEL)
        self.cancel.connect('clicked', self._exit)
        self.ok = HIGButton(stock=gtk.STOCK_OK)
        self.ok.connect('clicked', self._save_schema_and_leave)

        self.load_smtp_schemas()
        self._set_scan_command(None)
        self.profile_running = None  # no SchedProfileEditor instance is running.
        self._load_pscheds()
        self.load_schemas()

        self.__set_props()
        self.__do_layout()

        self.connect('destroy', self._exit)

    def load_smtp_schemas(self):
        """
        Load smtp profiles.
        """
        schemas = ConfigParser()
        schemas.read(Path.smtp_schemas)

        self.smtp_sections = []
        self.setting_smtp.get_model().clear()
        for section in schemas.sections():
            self.smtp_sections.append(section)
            self.setting_smtp.append_text(section)

        self.setting_smtp.set_active(0)

    def load_schemas(self):
        """
        Load schemas profiles.
        """
        schemas = ConfigParser()
        schemas.read(Path.sched_schemas)

        self.sections = []
        self.schema_name.get_model().clear()
        for section in schemas.sections():
            self.sections.append(section)
            self.schema_name.append_text(section)

        self.schema_name.set_active(0)
        self._check_schema(None)

    def _load_schema(self):
        """
        Load current set schedule schema.
        """
        schema = ConfigParser()
        schema.read(Path.sched_schemas)

        values = {
            'command': self.scan_command.set_text,
            'saveto': self.setting_saveto_entry.set_text,
            'mailto': self.setting_mailto_entry.set_text
        }
        enable = {
            'saveto': self.setting_saveto.set_active,
            'mailto': self.setting_mailto.set_active
        }

        for item in schema.items(self.schema_name.get_active_text()):
            if item[0] == 'addtoinv':
                self.setting_addtoinv.set_active(int(item[1]))
                if item[1] == '2':
                    self.apply.set_sensitive(False)
                    self.ok.set_sensitive(False)
                else:
                    self.apply.set_sensitive(True)
                    self.ok.set_sensitive(True)
            elif item[0] == 'enabled':
                self.setting_enabled.set_active(int(item[1]))
            elif item[0] == 'profile':
                pindx = self.profiles.index(item[1])
                self.sched_name.set_active(pindx)
            elif item[0] == 'smtp':
                if item[1]:
                    pindx = self.smtp_sections.index(item[1])
                    self.setting_smtp.set_active(pindx)
            else:
                values[item[0]](item[1])
                if item[0] in ('saveto', 'mailto'):
                    if len(item[1]):
                        enable[item[0]](True)
                    else:
                        enable[item[0]](False)

    def _check_schema(self, event):
        """
        Check if current text in schema_name combobox is a schema name.
        """
        if self.schema_name.get_active_text() in self.sections:
            # load schema
            self._load_schema()
        else:
            # reset to default values
            self.apply.set_sensitive(True)
            self.ok.set_sensitive(True)
            self.setting_addtoinv.set_active(False)
            self.setting_enabled.set_active(False)
            self.setting_mailto.set_active(False)
            self.setting_mailto_entry.set_text('')
            self.setting_saveto.set_active(False)
            self.setting_saveto_entry.set_text('')

        self.schema_sett_frame._set_label(self.schema_name.get_active_text() \
+ " - Settings")

    def _set_scan_command(self, event):
        """
        Set scan command based on chosen profile.
        """
        profile = self.scan_name.get_selected_profile()
        cmd_profile = CommandProfile()
        command = cmd_profile.get_command(profile)
        self.scan_command.set_text(command % '<target>')

    def _load_pscheds(self):
        """
        Load scheduling profiles.
        """
        pscheds = ConfigParser()
        pscheds.read(Path.sched_profiles)

        self.profiles = []
        self.sched_name.get_model().clear()
        for section in pscheds.sections():
            self.sched_name.append_text(section)
            self.profiles.append(section)

        self.sched_name.set_active(0)

    def _edit_schedprofiles(self, event):
        """
        Open Scheduling Profiles Editor.
        """
        if self.profile_running:
            return

        win = SchedProfileEditor(self, self.sched_name.get_active_text())
        win.show_all()
        self.profile_running = win

    def _select_saveto(self, event):
        """
        Select directory to save file.
        """
        dir_chooser = DirectoryChooserDialog(_("Select a directory"))

        dir_chooser.run()
        dir_chosen = dir_chooser.get_filename()
        dir_chooser.destroy()
        self.setting_saveto_entry.set_text(dir_chosen)

    def _save_schema(self, event):
        """
        Save current schema.
        """
        schema = self.schema_name.get_active_text()
        command = self.scan_command.get_text()
        schedule = self.sched_name.get_active_text()
        mailto = self.setting_mailto.get_active()

        if not schema or not schedule or not command or '<target>' in command:
            dlg = HIGAlertDialog(self,
                                 message_format=_('Scheduling Schema - Error\
 while saving.'),
                                 secondary_text=_("There is some error in at \
least one of the following fields: \"Schema name\", \"Command\" or \"Scheduling\
 Profile\"\n\nCheck if \"Schema name\" is not empty.\nCheck if \"Command\" does\
 contain \"<target>\" on it.\nCheck if there is some \"Scheduling Profile\" \
selected."))

            dlg.run()
            dlg.destroy()
            return

        if mailto and not self.setting_smtp.get_active_text():
            dlg = HIGAlertDialog(self,
                                 message_format=_('Scheduling Schema - Error\
 while saving.'),
                                 secondary_text=_("You need to create a \
a SMTP Schema for sending email."))

            dlg.run()
            dlg.destroy()
            return

        # check for output existance
        if self.setting_saveto.get_active() and \
           not os.path.isdir(self.setting_saveto_entry.get_text()):

            dlg = HIGAlertDialog(self,
                                 message_format=_('Scheduling Schema - Error\
 while saving.'),
                                 secondary_text=_("You especified an invalid \
directory to save scans output."))

            dlg.run()
            dlg.destroy()
            return

        # write schema to file
        s_cfg = ConfigParser()
        s_cfg.read(Path.sched_schemas)

        if not s_cfg.has_section(schema):
            new_sec = True
            s_cfg.add_section(schema)
        else:
            new_sec = False

        s_cfg.set(schema, 'profile', schedule)
        s_cfg.set(schema, 'command', command)
        if self.setting_enabled.get_active():
            s_cfg.set(schema, 'enabled', '1')
        else:
            s_cfg.set(schema, 'enabled', '0')
        if self.setting_addtoinv.get_active():
            s_cfg.set(schema, 'addtoinv', '1')
        else:
            s_cfg.set(schema, 'addtoinv', '0')
        if self.setting_saveto.get_active():
            s_cfg.set(schema, 'saveto', self.setting_saveto_entry.get_text())
        else:
            s_cfg.set(schema, 'saveto', '')
        if mailto:
            s_cfg.set(schema, 'mailto', self.setting_mailto_entry.get_text())
            s_cfg.set(schema, 'smtp', self.setting_smtp.get_active_text())
        else:
            s_cfg.set(schema, 'mailto', '')
            s_cfg.set(schema, 'smtp', '')

        s_cfg.write(open(Path.sched_schemas, 'w'))

        if new_sec:
            self.load_schemas()

        if self.daddy:
            self.daddy.load_schemas()

    def _save_schema_and_leave(self, event):
        """
        Save current schema and close editor.
        """
        self._save_schema(None)
        self._exit(None)

    def _show_help(self, event):
        """
        Show help for Scan Scheduler Editor.
        """
        show_help(self, "scheduler.html#setting-up-a-schedule")

    def __set_props(self):
        """
        Set window properties.
        """
        self.set_title(self.wtitle)
        self.set_default_size(440, -1)

    def __do_layout(self):
        """
        Layout widgets in window.
        """
        main_vbox = HIGVBox()
        main_vbox.set_border_width(5)
        main_vbox.set_spacing(12)
        header_hbox = HIGHBox()
        schema_table = HIGTable()
        schedsn_hbox = HIGHBox()
        sett_table = HIGTable()
        btns_hbox = HIGHBox()

        header_hbox._pack_expand_fill(self.ttitle)
        header_hbox._pack_noexpand_nofill(self.umit_logo)

        # schema name
        schema_table.attach_label(self.schema_name_lbl, 0, 1, 0, 1)
        schema_table.attach_entry(self.schema_name, 1, 2, 0, 1)

        # target and scan profile
        schema_table.attach_label(self.scan_name_lbl, 0, 1, 1, 2)
        schema_table.attach_entry(self.scan_name, 1, 2, 1, 2)

        # scan command
        schema_table.attach_label(self.scan_command_lbl, 0, 1, 2, 3)
        schema_table.attach_label(self.scan_command, 1, 2, 2, 3)

        # scheduling profile
        schedsn_hbox._pack_expand_fill(self.sched_name)
        schedsn_hbox._pack_noexpand_nofill(self.sched_name_edit)

        schema_table.attach_label(self.sched_name_lbl, 0, 1, 3, 4)
        schema_table.attach_entry(schedsn_hbox, 1, 2, 3, 4)

        # settings frame
        settings_align = gtk.Alignment(0.5, 0.5, 1, 1)
        settings_align.set_padding(6, 0, 12, 0)
        schemasett_hbox = HIGVBox()

        # saveto
        sett_hbox = HIGHBox()
        sett_hbox._pack_expand_fill(self.setting_saveto_entry)
        sett_hbox._pack_noexpand_nofill(self.setting_saveto_browse)
        sett_table.attach_label(self.setting_saveto, 0, 1, 0, 1)
        sett_table.attach_entry(sett_hbox, 1, 2, 0, 1)

        # mailto, smtp
        sett_hbox = HIGHBox()
        sett_hbox._pack_expand_fill(self.setting_mailto_entry)
        sett_hbox._pack_noexpand_nofill(self.setting_smtp_lbl)
        sett_hbox._pack_expand_fill(self.setting_smtp)
        sett_table.attach_label(self.setting_mailto, 0, 1, 1, 2)
        sett_table.attach_entry(sett_hbox, 1, 2, 1, 2)
        schemasett_hbox._pack_noexpand_nofill(sett_table)

        # add to inventory
        sett_hbox = HIGHBox()
        sett_hbox._pack_noexpand_nofill(self.setting_addtoinv)
        schemasett_hbox._pack_noexpand_nofill(sett_hbox)

        # enabled/disabled
        sett_hbox = HIGHBox()
        sett_hbox._pack_noexpand_nofill(self.setting_enabled)
        schemasett_hbox._pack_noexpand_nofill(sett_hbox)
        settings_align.add(schemasett_hbox)

        self.schema_sett_frame.add(settings_align)

        # bottom buttons
        btns_hbox.set_homogeneous(True)
        btns_hbox._pack_expand_fill(self.help)
        btns_hbox._pack_expand_fill(hig_box_space_holder())
        btns_hbox._pack_expand_fill(self.apply)
        btns_hbox._pack_expand_fill(self.cancel)
        btns_hbox._pack_expand_fill(self.ok)

        main_vbox._pack_noexpand_nofill(header_hbox)
        main_vbox._pack_noexpand_nofill(gtk.HSeparator())
        main_vbox._pack_noexpand_nofill(schema_table)
        main_vbox._pack_noexpand_nofill(gtk.HSeparator())
        main_vbox._pack_noexpand_nofill(self.schema_sett_frame)
        main_vbox.pack_end(btns_hbox, False, False, 0)

        self.add(main_vbox)

    def _exit(self, event):
        """
        Close current and window and profile editor if it is running.
        """
        if self.profile_running:
            self.profile_running._exit(None)

        if self.daddy:
            self.daddy.schemawin = None

        self.destroy()

    def _get_profile_running(self):
        """
        Get profile editor running instance.
        """
        return self.__profilerunning

    def _set_profile_running(self, running):
        """
        Set profile editor instance.
        """
        self.__profilerunning = running

    # Properties
    profile_running = property(_get_profile_running, _set_profile_running)
    def __layout(self):
        """
        Layout widgets
        """
        main_vbox = HIGVBox()
        main_vbox.set_border_width(5)
        main_vbox.set_spacing(12)
        main_hpaned = gtk.HPaned()
        btns_hbox = HIGHBox()
        left_box = HIGVBox()
        right_box = gtk.VBox()

        header_hbox = HIGHBox()
        hostdetect_hbox = HIGHBox()
        targetl_hbox = HIGHBox()
        targetv_hbox = HIGHBox()
        targetr_hbox = HIGHBox()

        # header
        header_hbox._pack_expand_fill(self.ttitle)
        header_hbox._pack_noexpand_nofill(self.umit_logo)
        # network list
        netframe = HIGFrame(_("Network list"))
        settings_align = gtk.Alignment(0.5, 0.5, 1, 1)
        settings_align.set_padding(6, 0, 12, 0)
        nbox = HIGVBox()
        entry = gtk.Entry()
        entry.set_text(_("Sample 192.168.254.0/24"))
        nbox._pack_noexpand_nofill(entry)
        addnw_hbox = HIGHBox()
        addnw_hbox._pack_noexpand_nofill(self.addnetworks)
        nbox.pack_end(addnw_hbox, False, False, 0)
        self.networks_box = nbox
        settings_align.add(nbox)
        netframe.add(settings_align)
        # detection
        hostdetect_hbox._pack_noexpand_nofill(self.netdetect_btn)
        hostdetect_hbox._pack_noexpand_nofill(self.hostdetect_btn)

        # target list
        targetl_hbox._pack_noexpand_nofill(self.target_lbl)
        targetv_hbox._pack_expand_fill(self.tview)
        targetr_hbox.pack_end(self.target_remove, False, False, 0)

        # bottom buttons
        btns_hbox.set_homogeneous(True)
        btns_hbox._pack_expand_fill(self.help)
        btns_hbox._pack_expand_fill(hig_box_space_holder())
        btns_hbox._pack_expand_fill(self.cancel)
        btns_hbox._pack_expand_fill(self.apply)
        # change apply button stock text
        lbl = self.apply.get_children()[0].get_children()[0].get_children()[1]
        lbl.set_text(_("Use target list"))

        left_box._pack_noexpand_nofill(netframe)
        left_box.pack_end(hostdetect_hbox, False, False, 0)
        right_box.pack_start(targetl_hbox, False, False, 0)
        right_box.pack_start(targetv_hbox, True, True, 6)
        right_box.pack_start(targetr_hbox, False, False, 0)

        left_align = gtk.Alignment(0.5, 0.5, 1, 1)
        left_align.set_padding(0, 0, 0, 6)
        left_align.add(left_box)
        right_align = gtk.Alignment(0.5, 0.5, 1, 1)
        right_align.set_padding(0, 0, 6, 0)
        right_align.add(right_box)

        main_hpaned.add1(left_align)
        main_hpaned.add2(right_align)

        main_vbox._pack_noexpand_nofill(header_hbox)
        main_vbox._pack_noexpand_nofill(gtk.HSeparator())
        main_vbox._pack_expand_fill(main_hpaned)
        main_vbox.pack_end(btns_hbox, False, False, 0)

        self.add(main_vbox)
コード例 #6
0
    def __layout(self):
        """
        Layout widgets
        """
        main_vbox = HIGVBox()
        main_vbox.set_border_width(5)
        main_vbox.set_spacing(12)
        main_hpaned = gtk.HPaned()
        btns_hbox = HIGHBox()
        left_box = HIGVBox()
        right_box = gtk.VBox()

        header_hbox = HIGHBox()
        hostdetect_hbox = HIGHBox()
        targetl_hbox = HIGHBox()
        targetv_hbox = HIGHBox()
        targetr_hbox = HIGHBox()

        # header
        header_hbox._pack_expand_fill(self.ttitle)
        header_hbox._pack_noexpand_nofill(self.umit_logo)
        # network list
        netframe = HIGFrame(_("Network list"))
        settings_align = gtk.Alignment(0.5, 0.5, 1, 1)
        settings_align.set_padding(6, 0, 12, 0)
        nbox = HIGVBox()
        entry = gtk.Entry()
        entry.set_text(_("Sample 192.168.254.0/24"))
        nbox._pack_noexpand_nofill(entry)
        addnw_hbox = HIGHBox()
        addnw_hbox._pack_noexpand_nofill(self.addnetworks)
        nbox.pack_end(addnw_hbox, False, False, 0)
        self.networks_box = nbox
        settings_align.add(nbox)
        netframe.add(settings_align)
        # detection
        hostdetect_hbox._pack_noexpand_nofill(self.netdetect_btn)
        hostdetect_hbox._pack_noexpand_nofill(self.hostdetect_btn)

        # target list
        targetl_hbox._pack_noexpand_nofill(self.target_lbl)
        targetv_hbox._pack_expand_fill(self.tview)
        targetr_hbox.pack_end(self.target_remove, False, False, 0)

        # bottom buttons
        btns_hbox.set_homogeneous(True)
        btns_hbox._pack_expand_fill(self.help)
        btns_hbox._pack_expand_fill(hig_box_space_holder())
        btns_hbox._pack_expand_fill(self.cancel)
        btns_hbox._pack_expand_fill(self.apply)
        # change apply button stock text
        lbl = self.apply.get_children()[0].get_children()[0].get_children()[1]
        lbl.set_text(_("Use target list"))


        left_box._pack_noexpand_nofill(netframe)
        left_box.pack_end(hostdetect_hbox, False, False, 0)
        right_box.pack_start(targetl_hbox, False, False, 0)
        right_box.pack_start(targetv_hbox, True, True, 6)
        right_box.pack_start(targetr_hbox, False, False, 0)

        left_align = gtk.Alignment(0.5, 0.5, 1, 1)
        left_align.set_padding(0, 0, 0, 6)
        left_align.add(left_box)
        right_align = gtk.Alignment(0.5, 0.5, 1, 1)
        right_align.set_padding(0, 0, 6, 0)
        right_align.add(right_box)

        main_hpaned.add1(left_align)
        main_hpaned.add2(right_align)

        main_vbox._pack_noexpand_nofill(header_hbox)
        main_vbox._pack_noexpand_nofill(gtk.HSeparator())
        main_vbox._pack_expand_fill(main_hpaned)
        main_vbox.pack_end(btns_hbox, False, False, 0)

        self.add(main_vbox)
class ProfileManager(HIGWindow):
    """
    Create a Profile Manager 
    """
    def __init__(self, daddy=None):
        HIGWindow.__init__(self, type=gtk.WINDOW_TOPLEVEL)
        self.set_title('Profile Manager')
        self.set_position(gtk.WIN_POS_CENTER)
        self.__create_widgets()
        self.add(self.vbox_main)
        self.__fill_widgets()
        self.__pack_widgets()
        self.__scan_notebook = None

        self.daddy = daddy

    def __create_widgets(self):

        self.vbox_main = HIGVBox()

        self.main_frame = HIGFrame("Profiles")
        #self.main_frame.set_shadow_type(gtk.SHADOW_ETCHED_OUT)

        self.align = gtk.Alignment(0.0, 0.0, 1.0, 1.0)
        self.align.set_padding(0, 0, 12, 0)

        self.vbox = HIGVBox()
        self.profiles_sw = HIGScrolledWindow()
        #TreeView
        self.model = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_STRING)
        #self.modelfilter = self.model.filter_new()
        self.profiles_tv = gtk.TreeView(self.model)
        renderer = gtk.CellRendererText()
        column = gtk.TreeViewColumn(_("Name"), renderer, text=0)
        self.profiles_tv.append_column(column)
        renderer_hint = gtk.CellRendererText()
        column_hint = gtk.TreeViewColumn(_("Hint"), renderer_hint, text=1)
        self.profiles_tv.append_column(column_hint)
        #self.profiles_tv.set_model(self.modelfilter)
        #Info
        self.hbox_info = HIGHBox()
        self.command_label = HIGEntryLabel('Command: ')
        self.command_entry = HIGTextEntry()
        self.command_entry.set_editable(False)

        #Buttons
        self.hbox_buttons = HIGHBox()

        self.wiz_button = HIGButton(title='Wizard', stock='gtk-convert')
        self.wiz_button.connect("clicked", self.new_wiz)

        self.edit_button = HIGButton(stock='gtk-edit')
        self.edit_button.connect("clicked", self.open_peditor)
        self.new_button = HIGButton(stock='gtk-new')
        self.new_button.connect("clicked", self.open_peditor)
        self.copy_button = HIGButton(stock='gtk-copy')
        self.copy_button.connect("clicked", self.copy_profiles)
        self.delete_button = HIGButton(stock=gtk.STOCK_DELETE)
        self.delete_button.connect('clicked', self.delete_profile)
        #Apply Buttons
        self.cancel_button = HIGButton(stock='gtk-close')
        self.cancel_button.connect("clicked", self.quit)

        self.connect("delete-event", self.quit)

    def __fill_widgets(self):

        self.profiles = CommandProfile()
        self._reload_profile_list()

        #selection = self.profiles_tv.get_selection()
        #selection.connect("changed", self.change_nmap_command)
        self.profiles_tv.connect("cursor-changed", self.change_nmap_command)

    def __pack_widgets(self):
        """
        Pack all widgets of windows 
        """
        self.vbox_main.pack_start(self.main_frame, True, True)
        self.main_frame.add(self.align)
        self.align.add(self.vbox)
        self.vbox.set_border_width(6)

        self.vbox.pack_start(self.profiles_sw, True, True, 0)

        self.hbox_info.pack_start(self.command_label, False, False, 0)
        self.hbox_info.pack_start(self.command_entry, True, True, 0)
        self.vbox.pack_start(self.hbox_info, False, False, 0)

        self.hbox_buttons.pack_end(self.cancel_button)
        self.hbox_buttons.pack_end(self.copy_button, True, True)
        self.hbox_buttons.pack_end(self.edit_button, True, True)
        self.hbox_buttons.pack_end(self.delete_button, True, True)
        self.hbox_buttons.pack_end(self.new_button, True, True)
        self.hbox_buttons.pack_end(self.wiz_button, True, True)
        self.hbox_buttons.set_spacing(6)
        self.vbox_main.pack_start(self.hbox_buttons, False, False)

        self.profiles_sw.set_size_request(400, 170)
        self.profiles_sw.add(self.profiles_tv)

    def get_selected_profile(self):
        """
        Returns the string with name of selected profile 
        """
        try:
            treeselection = self.profiles_tv.get_selection()
            (model, iter) = treeselection.get_selected()
            return model.get_value(iter, 0)
        except:
            return None

    def change_nmap_command(self, widget_tv):
        """
        Change a nmap command at command entry
        """
        assert widget_tv is not None
        # it call __init__ because when wizard or profile are open,
        # need update profiles
        self.profiles.__init__()
        # update text entry of command
        self.command_entry.set_text(
            self.profiles.get_command(self.get_selected_profile()))

    def new_wiz(self, widget):
        w = Wizard()
        w.set_notebook(None)
        w.set_profilemanager(self.model)
        w.show_all()

    def open_peditor(self, widget):
        """
        Open Profile Editor with a Selected or Non-Selected(New) Item
        """
        assert widget is not None

        if widget.get_label() == "gtk-edit":
            # Edit profile selected
            if self.get_selected_profile() is not None:
                pe = ProfileEditor(self.get_selected_profile())
                pe.set_notebook(self.__scan_notebook)
                pe.set_profilemanager(self.model)
                pe.show_all()
        else:
            # New Profile
            pe = ProfileEditor()
            pe.set_notebook(self.__scan_notebook)
            pe.set_profilemanager(self.model)
            pe.show_all()
            self._reload_profile_list()

    def copy_profiles(self, widget):
        """
        Copy selected Profile
        """
        if self.get_selected_profile() is None:
            return None
        d = ProfileName(_("Insert a profile name"))
        profile_name = d.run()
        if profile_name is None or profile_name == "":
            return None
        #get commands of selected profile
        profile_selected = self.get_selected_profile()
        command = self.profiles.get_command(profile_selected)
        hint = self.profiles.get_hint(profile_selected)
        description = self.profiles.get_description(profile_selected)
        annotation = self.profiles.get_annotation(profile_selected)
        #Options
        prof = self.profiles.get_profile(profile_selected)
        options_used = prof['options']
        options = CommandConstructor(options_used)

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

        myiter = self.model.insert_before(None, None)
        self.model.set_value(myiter, 0, profile_name)
        self.model.set_value(myiter, 1, self.profiles.get_hint(profile_name))
        treeselection = self.profiles_tv.get_selection()
        treeselection.select_iter(myiter)
        #(model,iter) = treeselection.get_selected()
        #model.get_value(iter,0)

    def delete_profile(self, widget=None):
        """
        delete profile 
        """
        if self.get_selected_profile() is None:
            return None
        self.profiles.remove_profile(self.get_selected_profile())
        #Update treeview
        treeselection = self.profiles_tv.get_selection()
        (model, iter) = treeselection.get_selected()
        model.remove(iter)
        # update text entry of command
        self.command_entry.set_text('')

    def _reload_profile_list(self):
        """
        Reload a list of profiles
        """
        profiles = []
        all_profiles = self.profiles.sections()
        for profile in all_profiles:
            if self.profiles.get(profile, 'tool') == 'nmap':
                profiles.append(profile)
        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.profiles.get_hint(command))
            #self.model.append([command,self.profiles.get_hint(command)])

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

    def quit(self, widget):
        self.destroy()

    def quit(self, widget, data=None):
        if self.daddy:
            self.daddy.running_pm = False
            self.destroy()
        else:
            gtk.main_quit()
コード例 #8
0
class NISettingsBox():
    def __init__(self, create_buttons=True):

        self.__create_widgets()

        if (create_buttons):
            self.__create_buttons()

        self._fill_comboboxes()
        self._load_settings()
        self.main_vbox = self.__do_layout(create_buttons)

    def __create_widgets(self):
        self.timeline = HIGFrame(_("Timeline Settings"))
        self.tl_mode_lbl = gtk.Label(_("Timeline view mode"))
        self.tl_kind_lbl = gtk.Label(_("Timeline view kind"))
        self.tl_mode = gtk.combo_box_new_text()
        self.tl_kind = gtk.combo_box_new_text()

        self.tabs = HIGFrame(_("Devices tab"))
        self.tabs_cbtn = gtk.CheckButton(_("Place close button"))

        self.sbar = HIGFrame(_("Statusbar"))
        self.sbar_tips = gtk.CheckButton(_("Show tips"))

    def __create_buttons(self):
        # bottom buttons
        self.apply = HIGButton(stock=gtk.STOCK_APPLY)
        self.cancel = HIGButton(stock=gtk.STOCK_CANCEL)
        self.ok = HIGButton(stock=gtk.STOCK_OK)

        self.apply.connect("clicked", self._apply_settings)
        self.ok.connect("clicked", self._apply_settings_exit)
        self.cancel.connect("clicked", self._exit)

    def _fill_comboboxes(self):
        """
        Fill timeline mode and timeline kind combo boxes with available
        options.
        """
        self.tl_kind_opts = {}
        self.tl_mode_opts = {}

        for opt, value in view_kind.items():
            self.tl_kind.append_text(value)
            self.tl_kind_opts[opt] = value

        for opt, value in view_mode.items():
            self.tl_mode.append_text(value)
            self.tl_mode_opts[opt] = value

    def _load_settings(self):
        """
        Load current settings values.
        """
        options = startup_options()
        start_tlkind = self.tl_kind_opts[options['kind']]
        start_tlmode = self.tl_mode_opts[options['mode']]

        store = self.tl_kind.get_model()
        for indx, item in enumerate(store):
            if item[0] == start_tlkind:
                self.tl_kind.set_active(indx)
                break

        store = self.tl_mode.get_model()
        for indx, item in enumerate(store):
            if item[0] == start_tlmode:
                self.tl_mode.set_active(indx)
                break

        self.tabs_cbtn.set_active(options['tabs_close_btn'])
        self.sbar_tips.set_active(options['tips'])

    def _apply_settings(self, event):
        """
        Apply current settings.
        """
        curr_kind = self.tl_kind.get_active_text()
        for kind, value in self.tl_kind_opts.items():
            if value == curr_kind:
                curr_kind = kind
                break

        curr_mode = self.tl_mode.get_active_text()
        for mode, value in self.tl_mode_opts.items():
            if value == curr_mode:
                curr_mode = mode
                break

        write_startup_setting(TL_SECTION, "mode", curr_mode)
        write_startup_setting(TL_SECTION, "kind", curr_kind)
        write_startup_setting(INV_SECTION, "tabs_close_btn",
                              self.tabs_cbtn.get_active())
        write_startup_setting(INV_SECTION, "tips", self.sbar_tips.get_active())

    def _apply_settings_exit(self, event):
        """
        Apply current settings and leave.
        """
        self._apply_settings(None)
        self._exit(None)

    def _exit(self, event):
        """
        Close this window.
        """
        self.destroy()

    def __set_props(self):
        """
        Window properties.
        """
        self.set_title(_("Network Inventory Settings"))
        self.set_default_size(305, -1)

    def __do_layout(self, create_buttons):
        main_vbox = HIGVBox()

        # timeline frame
        tl_settings_align = gtk.Alignment(0.5, 0.5, 1, 1)
        tl_settings_align.set_padding(6, 0, 12, 0)
        tl_settings_vbox = HIGVBox()

        mode_hbox = HIGHBox()
        mode_hbox._pack_noexpand_nofill(self.tl_mode_lbl)
        mode_hbox._pack_expand_fill(self.tl_mode)

        kind_hbox = HIGHBox()
        kind_hbox._pack_noexpand_nofill(self.tl_kind_lbl)
        kind_hbox._pack_expand_fill(self.tl_kind)

        tl_settings_vbox._pack_noexpand_nofill(mode_hbox)
        tl_settings_vbox._pack_noexpand_nofill(kind_hbox)
        tl_settings_align.add(tl_settings_vbox)
        self.timeline.add(tl_settings_align)
        main_vbox._pack_noexpand_nofill(self.timeline)
        # end timeline frame

        # statusbar frame
        sbar_settings_align = gtk.Alignment(0.5, 0.5, 1, 1)
        sbar_settings_align.set_padding(6, 0, 12, 0)
        sbar_settings_vbox = HIGVBox()

        sbar_settings_vbox._pack_noexpand_nofill(self.sbar_tips)
        sbar_settings_align.add(sbar_settings_vbox)
        self.sbar.add(sbar_settings_align)
        main_vbox._pack_noexpand_nofill(self.sbar)
        # end statusbar frame

        # tabs frame
        tabs_settings_align = gtk.Alignment(0.5, 0.5, 1, 1)
        tabs_settings_align.set_padding(6, 0, 12, 0)
        tabs_settings_vbox = HIGVBox()

        tabs_settings_vbox._pack_noexpand_nofill(self.tabs_cbtn)
        tabs_settings_align.add(tabs_settings_vbox)
        self.tabs.add(tabs_settings_align)
        main_vbox._pack_noexpand_nofill(self.tabs)
        # end tabs frame

        if create_buttons:
            # buttons box
            btnsbox = HIGHBox()
            btnsbox._pack_noexpand_nofill(self.apply)
            btnsbox._pack_noexpand_nofill(self.cancel)
            btnsbox._pack_noexpand_nofill(self.ok)
            bbox = gtk.HBox()
            bbox.pack_end(btnsbox, False, False, 0)

            main_vbox.pack_end(bbox, False, False, 0)
            main_vbox.pack_end(gtk.HSeparator(), False, False, 0)
            # end buttons box

        return main_vbox

    def get_layout(self):
        return self.main_vbox