root/branch/PacketManipulator/Tabs/MainTab.py @ 3323

Revision 3323, 7.4 kB (checked in by nopper, 5 years ago)

Icons

Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3# Copyright (C) 2008 Adriano Monteiro Marques
4#
5# Author: Francesco Piccinno <stack.box@gmail.com>
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program; if not, write to the Free Software
19# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
21import gtk
22import Backend
23
24from widgets.HexView import HexView
25from widgets.Expander import AnimatedExpander
26
27from views import UmitView
28from Icons import get_pixbuf
29
30class ProtocolHierarchy(gtk.ScrolledWindow):
31    def __init__(self, packet):
32        gtk.ScrolledWindow.__init__(self)
33
34        self.__create_widgets()
35        self.__pack_widgets()
36        self.__connect_signals()
37
38        self.proto_icon = get_pixbuf('protocol_small')
39
40        root = None
41
42        # We pray to be ordered :(
43        for proto in Backend.get_packet_protos(packet):
44            root = self.store.append(root, [self.proto_icon, Backend.get_proto_name(proto), proto])
45
46    def __create_widgets(self):
47        # Icon / string (like TCP packet with some info?) / hidden
48        self.store = gtk.TreeStore(gtk.gdk.Pixbuf, str, object)
49        self.view = gtk.TreeView(self.store)
50
51        pix = gtk.CellRendererPixbuf()
52        txt = gtk.CellRendererText()
53
54        col = gtk.TreeViewColumn('Name')
55
56        col.pack_start(pix, False)
57        col.pack_start(txt, True)
58
59        col.set_attributes(pix, pixbuf=0)
60        col.set_attributes(txt, text=1)
61
62        self.view.append_column(col)
63
64        self.view.set_grid_lines(gtk.TREE_VIEW_GRID_LINES_BOTH)
65        self.view.set_enable_tree_lines(True)
66        self.view.set_rules_hint(True)
67
68    def __pack_widgets(self):
69        self.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
70        self.set_shadow_type(gtk.SHADOW_ETCHED_IN)
71
72        self.add(self.view)
73
74    def __connect_signals(self):
75        self.view.enable_model_drag_dest([('text/plain', 0, 0)], gtk.gdk.ACTION_COPY)
76        self.view.connect('drag-data-received', self.__on_drag_data)
77
78    def __on_drag_data(self, widget, ctx, x, y, data, info, time):
79        if data and data.format == 8:
80            ret = self.view.get_dest_row_at_pos(x, y)
81
82            if not ret:
83                self.store.append(None, [None, data.data, data.data, None])
84            else:
85                path, pos = ret
86                print path, pos
87
88            ctx.finish(True, False, time)
89        else:
90            ctx.finish(False, False, time)
91
92    def get_active_protocol(self):
93        """
94        Return the selected protocol or the most
95        important protocol if no selection.
96
97        @return an instance of Protocol or None
98        """
99
100        model, iter = self.view.get_selection().get_selected()
101
102        if not iter:
103            iter = model.get_iter_first()
104
105            if not iter:
106                return None
107
108        obj = model.get_value(iter, 2)
109       
110        assert (isinstance(obj, Backend.Protocol), "Should be a Protocol instance.")
111
112        return obj
113
114
115class SessionPage(gtk.VBox):
116    def __init__(self, proto_name):
117        gtk.VBox.__init__(self)
118
119        self.__create_widgets(Backend.get_proto(proto_name))
120        self.__pack_widgets()
121        self.__connect_signals()
122
123    def __create_widgets(self, proto):
124        self._label = gtk.Label("*" + proto.__name__)
125
126        self.packet = Backend.Packet(proto())
127
128        self.vpaned = gtk.VPaned()
129        self.proto_hierarchy = ProtocolHierarchy(self.packet)
130        self.hexview = HexView()
131
132        self.redraw_hexview()
133
134    def __pack_widgets(self):
135        self.vpaned.pack1(self.proto_hierarchy)
136        self.vpaned.pack2(self.hexview)
137        self.pack_start(self.vpaned)
138
139        self.show_all()
140
141    def __connect_signals(self):
142        pass
143
144    def redraw_hexview(self):
145        """
146        Redraws the hexview
147        """
148        if self.packet:
149            self.hexview.payload = self.packet.get_raw()
150        else:
151            print "redraw_hexview(): no packet!!!"
152            self.hexview.payload = ""
153
154    def get_label(self):
155        return self._label
156
157    label = property(get_label)
158
159class SessionNotebook(gtk.Notebook):
160    def __init__(self):
161        gtk.Notebook.__init__(self)
162
163        self.set_show_border(False)
164        self.set_scrollable(True)
165
166    def create_session(self, proto_name):
167        session = SessionPage(proto_name)
168        self.append_page(session, session.label)
169        self.set_tab_reorderable(session, True)
170
171    def get_current_session(self):
172        """
173        Get the current SessionPage
174
175        @return a SessionPage instance or None
176        """
177
178        idx = self.get_current_page()
179        obj = self.get_nth_page(idx)
180
181        if obj and isinstance(obj, SessionPage):
182            return obj
183
184        return None
185
186class MainTab(UmitView):
187    tab_position = None
188    label_text = "MainTab"
189
190    def __create_widgets(self):
191        "Create the widgets"
192        self.vbox = gtk.VBox(False, 2)
193
194        self.sniff_expander = AnimatedExpander("<b>Sniff perspective</b>", 'sniff_small')
195        self.packet_expander = AnimatedExpander("<b>Packet perspective</b>", 'packet_small')
196
197        self.session_notebook = SessionNotebook()
198
199    def __pack_widgets(self):
200        "Pack the widgets"
201
202        # In the main window we have a perspective like
203        # + Sniff (expander)
204        # |- Protocol Hierarchy (like wireshark)
205        # |_ Hex View (containing the dump of the packet)
206       
207        self.vbox.pack_start(self.sniff_expander)
208        self.vbox.pack_start(self.packet_expander)
209
210        self.packet_expander.add(self.session_notebook)
211        self.sniff_expander.add(gtk.Button("Miao"))
212        #self.vbox.pack_start(self.session_notebook)
213
214        self.session_notebook.drag_dest_set(
215            gtk.DEST_DEFAULT_ALL,
216            [('text/plain', 0, 0)],
217            gtk.gdk.ACTION_COPY
218        )
219
220        self._main_widget.add(self.vbox)
221        self._main_widget.show_all()
222
223    def __connect_signals(self):
224        self.session_notebook.connect('drag-data-received', self.__on_drag_data)
225
226    def create_ui(self):
227        "Create the ui"
228        self.__create_widgets()
229        self.__pack_widgets()
230        self.__connect_signals()
231
232    def get_current_session(self):
233        "@returns the current SessionPage or None"
234        page = self.get_current_page()
235
236        if page and isinstance(page, SessionPage):
237            return page
238        return None
239
240    def get_current_page(self):
241        "@return the current page in notebook or None"
242
243        idx = self.session_notebook.get_current_page()
244        return self.session_notebook.get_nth_page(idx)
245
246    #===========================================================================
247
248    def __on_drag_data(self, widget, ctx, x, y, data, info, time):
249        "drag-data-received callback"
250
251        if data and data.format == 8:
252            proto = data.data
253
254            if Backend.get_proto(proto):
255                self.session_notebook.create_session(data.data)
256                ctx.finish(True, False, time)
257                return True
258
259        ctx.finish(False, False, time)
Note: See TracBrowser for help on using the browser.