def create_ui(self):
        self.hbox = gtk.HBox(False, 2)

        # Create the toolbar for sending selected packet
        self.toolbar = gtk.Toolbar()
        self.toolbar.set_style(gtk.TOOLBAR_ICONS)
        self.toolbar.set_orientation(gtk.ORIENTATION_VERTICAL)

        stocks = (
            gtk.STOCK_EDIT,
            gtk.STOCK_DELETE,
            gtk.STOCK_CLEAR,
            gtk.STOCK_SELECT_COLOR
        )

        tooltips = (
            _('Complete layers'),
            _('Remove selected layer'),
            _('Reset layer to default'),
            _('Graph packet')
        )

        callbacks = (
            self.__on_complete,
            self.__on_remove,
            self.__on_reset,
            self.__on_graph
        )

        for tooltip, stock, callback in zip(tooltips, stocks, callbacks):
            action = gtk.Action(None, None, tooltip, stock)
            action.connect('activate', callback)
            self.toolbar.insert(action.create_tool_item(), -1)


        self.proto_hierarchy = ProtocolHierarchy(self.session)
        self.hexview = HexView()

        try:
            # Only the read write hexview provide this
            self.hexview.set_insert_mode(True)
            self.hexview.set_read_only_mode(False)
            self.hexview.changed_callback = self.__on_hexview_changed
            self.hexview.connect('focus-out-event', self.__on_hexview_focus_out)

            self._packet_changed = False
        except:
            pass

        self.hbox.pack_start(self.proto_hierarchy, False, False, 0)
        self.hbox.pack_start(self.toolbar, False, False)
        self.hbox.pack_start(self.hexview)#, False, False)

        Prefs()['gui.maintab.hexview.font'].connect(self.hexview.modify_font)
        Prefs()['gui.maintab.hexview.bpl'].connect(self.hexview.set_bpl)

        self.pack_start(self.hbox)
class PacketPage(Perspective):
    icon = 'packet_small'
    title = _('Packet perspective')

    def create_ui(self):
        self.hbox = gtk.HBox(False, 2)

        # Create the toolbar for sending selected packet
        self.toolbar = gtk.Toolbar()
        self.toolbar.set_style(gtk.TOOLBAR_ICONS)
        self.toolbar.set_orientation(gtk.ORIENTATION_VERTICAL)

        stocks = (
            gtk.STOCK_EDIT,
            gtk.STOCK_DELETE,
            gtk.STOCK_CLEAR,
            gtk.STOCK_SELECT_COLOR
        )

        tooltips = (
            _('Complete layers'),
            _('Remove selected layer'),
            _('Reset layer to default'),
            _('Graph packet')
        )

        callbacks = (
            self.__on_complete,
            self.__on_remove,
            self.__on_reset,
            self.__on_graph
        )

        for tooltip, stock, callback in zip(tooltips, stocks, callbacks):
            action = gtk.Action(None, None, tooltip, stock)
            action.connect('activate', callback)
            self.toolbar.insert(action.create_tool_item(), -1)


        self.proto_hierarchy = ProtocolHierarchy(self.session)
        self.hexview = HexView()

        try:
            # Only the read write hexview provide this
            self.hexview.set_insert_mode(True)
            self.hexview.set_read_only_mode(False)
            self.hexview.changed_callback = self.__on_hexview_changed
            self.hexview.connect('focus-out-event', self.__on_hexview_focus_out)

            self._packet_changed = False
        except:
            pass

        self.hbox.pack_start(self.proto_hierarchy, False, False, 0)
        self.hbox.pack_start(self.toolbar, False, False)
        self.hbox.pack_start(self.hexview)#, False, False)

        Prefs()['gui.maintab.hexview.font'].connect(self.hexview.modify_font)
        Prefs()['gui.maintab.hexview.bpl'].connect(self.hexview.set_bpl)

        self.pack_start(self.hbox)

    def redraw_hexview(self):
        """
        Redraws the hexview
        """
        if self.session.packet:
            self.hexview.payload = self.session.packet.get_raw()
        else:
            self.hexview.payload = ''

    def __on_hexview_focus_out(self, widget, evt):
        if self._packet_changed and \
           self.session.packet.rebuild_from_raw_payload(self.hexview.payload):
            # Ok we've created a correct packet. Strange? Yes it is
            # At this point we have to repopulate protocol hierarchy widget at
            # first and the follow with property tab

            self.session.reload()
            #self.reload()
        else:
            self.redraw_hexview()

        self._packet_changed = False

    def __on_hexview_changed(self, hexdoc, cdata, push_undo):
        # Dummy function. Let's the focus-out-event make the rest
        self._packet_changed = True

    def reload(self):
        # Hide the toolbar while merging fields

        # FIXME: cyclic
        #if isinstance(self.session, SequenceSession) and \
        if getattr(self.session, 'sequence_page', None) and \
           self.session.sequence_page.merging:
            self.toolbar.hide()
            self.proto_hierarchy.hide()
        else:
            self.toolbar.show()
            self.proto_hierarchy.show()

        self.redraw_hexview()
        self.proto_hierarchy.reload()

    def __on_remove(self, action):
        packet, protocol = self.proto_hierarchy.get_active_protocol()

        if not packet:
            return

        if packet.remove(protocol):
            self.session.reload_container(packet)
            self.reload()

    def __on_reset(self, action):
        packet, protocol = self.proto_hierarchy.get_active_protocol()

        if not packet:
            return

        if packet.reset(protocol):
            self.session.reload_container(packet)
            self.reload()

    def __on_complete(self, action):
        packet, protocol = self.proto_hierarchy.get_active_protocol()

        if not packet:
            return

        if packet.complete():
            self.session.reload_container(packet)
            self.reload()

    def __on_graph(self, action):
        if not self.session.packet:
            return

        dialog = gtk.Dialog(
                _('Graph for %s') % self.session.packet.get_protocol_str(),
                self.get_toplevel(), 0, (gtk.STOCK_CLOSE, gtk.RESPONSE_REJECT,
                                         gtk.STOCK_SAVE, gtk.RESPONSE_ACCEPT))

        dialog.plotter = Plotter(self.session.packet)
        dialog.vbox.pack_start(dialog.plotter)
        dialog.show_all()

        dialog.connect('response', self.__on_graph_response)

    def __on_graph_response(self, dialog, id):
        if id == gtk.RESPONSE_REJECT:
            dialog.hide()
            dialog.destroy()
        elif id == gtk.RESPONSE_ACCEPT:
            chooser = gtk.FileChooserDialog(_('Save graph to'), dialog,
                         gtk.FILE_CHOOSER_ACTION_SAVE,
                         (gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT,
                          gtk.STOCK_SAVE, gtk.RESPONSE_ACCEPT))

            if chooser.run() == gtk.RESPONSE_ACCEPT:
                fname = chooser.get_filename()
                dialog.plotter.export_to(fname)

            chooser.hide()
            chooser.destroy()
Example #3
0
    def __on_edit(self, widget):
        if not HexView:
            try:
                text = unicode(self.value)
            except UnicodeDecodeError:
                dialog = gtk.MessageDialog(None, gtk.DIALOG_MODAL,
                                           gtk.MESSAGE_WARNING, gtk.BUTTONS_OK,
                _("Cannot edit this field because I can't convert it to utf8.\n"
                  "Try to edit the field with python shell or simply install "
                  "pygtkhex that provides a nice HexView to edit raw fields"))
                dialog.run()
                dialog.hide()
                dialog.destroy()

                return


        dialog = gtk.Dialog(_('Editing string field'),
                            None, gtk.DIALOG_MODAL,
                            (gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT,
                             gtk.STOCK_OK, gtk.RESPONSE_ACCEPT))

        if not HexView:
            buffer = gtk.TextBuffer()
            buffer.set_text(text)

            view = gtk.TextView(buffer)
            view.modify_font(pango.FontDescription(
                Prefs()['gui.maintab.hexview.font'].value))

            sw = gtk.ScrolledWindow()
            sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
            sw.set_shadow_type(gtk.SHADOW_ETCHED_IN)
            sw.add(view)
            sw.show_all()

            dialog.vbox.pack_start(sw)
        else:
            hex = HexView()
            hex.set_insert_mode(True)
            hex.set_read_only_mode(False)

            hex.font = Prefs()['gui.maintab.hexview.font'].value
            hex.payload = self.value

            hex.show()
            dialog.vbox.pack_start(hex)

        dialog.set_size_request(400, 300)

        if dialog.run() == gtk.RESPONSE_ACCEPT:
            if not HexView:
                self.entry.set_text(buffer.get_text(buffer.get_start_iter(),
                                                buffer.get_end_iter(), True))
            else:
                self.value = hex.payload
                self._ignore_changed = True

                try:
                    self.entry.set_text(unicode(self.value))
                except UnicodeDecodeError:
                    self.entry.set_text('')

        dialog.hide()
        dialog.destroy()