@@ -50,6 +50,7 @@ class Listeners(QWidget):
5050 idListener = 0
5151 listListenerObject = []
5252
53+
5354 def __init__ (self , parent , grpcClient ):
5455 super (QWidget , self ).__init__ (parent )
5556
@@ -65,20 +66,22 @@ def __init__(self, parent, grpcClient):
6566
6667 # List of sessions
6768 self .listListener = QTableWidget ()
68- self .listListener .installEventFilter (self )
6969 self .listListener .setShowGrid (False )
7070 self .listListener .setSelectionBehavior (QTableView .SelectRows )
71+
7172 self .listListener .setRowCount (0 )
72- self .listListener .setColumnCount (5 )
73- self .listListener .cellPressed .connect (self .listListenerClicked )
73+ self .listListener .setColumnCount (4 )
74+
75+ # self.listListener.cellPressed.connect(self.listListenerClicked)
76+ self .listListener .setContextMenuPolicy (Qt .CustomContextMenu )
77+ self .listListener .customContextMenuRequested .connect (self .showContextMenu )
78+
7479 self .listListener .verticalHeader ().setVisible (False )
7580 header = self .listListener .horizontalHeader ()
76- for i in range (5 ):
81+ for i in range (header . count ()):
7782 header .setSectionResizeMode (i , QHeaderView .Stretch )
78- header .setSectionResizeMode (0 , QHeaderView .ResizeToContents )
7983 self .layout .addWidget (self .listListener )
8084
81-
8285 # Thread to get listeners every second
8386 # https://realpython.com/python-pyqt-qthread/
8487 self .thread = QThread ()
@@ -90,48 +93,49 @@ def __init__(self, parent, grpcClient):
9093
9194 self .setLayout (self .layout )
9295
96+
9397 def __del__ (self ):
9498 self .getListenerWorker .quit ()
9599 self .thread .quit ()
96100 self .thread .wait ()
97101
98- # interact with listener list
99- def listListenerClicked (self , row , column ):
100- self .item = str (self .listListener .item (row , 0 ).data (0 ))
101- menu = QMenu ()
102- menu .addAction ('Stop' )
103- menu .triggered .connect (self .actionClicked )
104- menu .exec_ (QCursor .pos ())
105-
106- # catch right click on Listener panel
107- def eventFilter (self , source , event ):
108- if (event .type () == QEvent .ContextMenu and source is self .listListener ):
109- self .item = source .itemAt (event .pos ())
110- if self .item == None :
111- menu = QMenu ()
112- menu .addAction ('Add' )
113- menu .triggered .connect (self .actionClicked )
114- menu .exec_ (event .globalPos ())
115-
116- return super (Listeners , self ).eventFilter (source , event )
102+
103+ def showContextMenu (self , position ):
104+ index = self .listListener .indexAt (position )
105+ if not index .isValid ():
106+ menu = QMenu ()
107+ menu .addAction ('Add' )
108+ menu .triggered .connect (self .actionClicked )
109+ menu .exec_ (self .listListener .viewport ().mapToGlobal (position ))
110+ else :
111+ row = index .row ()
112+ self .item = str (self .listListener .item (row , 0 ).data (0 ))
113+
114+ menu = QMenu ()
115+ menu .addAction ('Stop' )
116+ menu .triggered .connect (self .actionClicked )
117+ menu .exec_ (self .listListener .viewport ().mapToGlobal (position ))
118+
117119
118120 # catch stopListener menu click
119121 def actionClicked (self , action ):
120122 if action .text () == "Add" :
121123 self .listenerForm ()
122124 elif action .text () == "Stop" :
123- id = self .item
125+ hash = self .item
124126 for listenerStore in self .listListenerObject :
125- if listenerStore .id == int ( id ) :
127+ if listenerStore .listenerHash [ 0 : 8 ] == hash :
126128 self .stopListener (listenerStore .listenerHash )
127129
130+
128131 # form for adding a listener
129132 def listenerForm (self ):
130133 if self .createListenerWindow is None :
131134 self .createListenerWindow = CreateListner ()
132135 self .createListenerWindow .procDone .connect (self .addListener )
133136 self .createListenerWindow .show ()
134137
138+
135139 # send message for adding a listener
136140 def addListener (self , message ):
137141 if message [0 ]== "github" :
@@ -151,12 +155,14 @@ def addListener(self, message):
151155 port = int (message [2 ]))
152156 self .grpcClient .addListener (listener )
153157
158+
154159 # send message for stoping a listener
155160 def stopListener (self , listenerHash ):
156161 listener = TeamServerApi_pb2 .Listener (
157162 listenerHash = listenerHash )
158163 self .grpcClient .stopListener (listener )
159164
165+
160166 # query the server to get the list of listeners
161167 def getListeners (self ):
162168 responses = self .grpcClient .getListeners ()
@@ -194,27 +200,30 @@ def getListeners(self):
194200 elif listener .type == DnsType :
195201 self .listListenerObject .append (Listener (self .idListener , listener .listenerHash , listener .type , listener .domain , listener .port , listener .numberOfSession ))
196202 elif listener .type == SmbType :
197- self .listListenerObject .append (Listener (self .idListener , listener .listenerHash , listener .type , listener .domain , "" , listener .numberOfSession ))
203+ self .listListenerObject .append (Listener (self .idListener , listener .listenerHash , listener .type , listener .ip , listener . domain , listener .numberOfSession ))
198204 else :
199205 self .listListenerObject .append (Listener (self .idListener , listener .listenerHash , listener .type , listener .ip , listener .port , listener .numberOfSession ))
200206 self .idListener = self .idListener + 1
201207
202208 self .printListeners ()
203209
210+
204211 def printListeners (self ):
205212 self .listListener .setRowCount (len (self .listListenerObject ))
206- self .listListener .setHorizontalHeaderLabels (["ID" , " Listener ID" , "Type" , "Host" , "Port" ])
213+ self .listListener .setHorizontalHeaderLabels (["Listener ID" , "Type" , "Host" , "Port" ])
207214 for ix , listenerStore in enumerate (self .listListenerObject ):
208- id = QTableWidgetItem (str (listenerStore .id ))
209- self .listListener .setItem (ix , 0 , id )
215+
210216 listenerHash = QTableWidgetItem (listenerStore .listenerHash [0 :8 ])
211- self .listListener .setItem (ix , 1 , listenerHash )
217+ self .listListener .setItem (ix , 0 , listenerHash )
218+
212219 type = QTableWidgetItem (listenerStore .type )
213- self .listListener .setItem (ix , 2 , type )
220+ self .listListener .setItem (ix , 1 , type )
221+
214222 host = QTableWidgetItem (listenerStore .host )
215- self .listListener .setItem (ix , 3 , host )
223+ self .listListener .setItem (ix , 2 , host )
224+
216225 port = QTableWidgetItem (str (listenerStore .port ))
217- self .listListener .setItem (ix , 4 , port )
226+ self .listListener .setItem (ix , 3 , port )
218227
219228
220229class CreateListner (QWidget ):
0 commit comments