|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 #This file is part of Beremiz, a Integrated Development Environment for |
|
4 #programming IEC 61131-3 automates supporting plcopen standard and CanFestival. |
|
5 # |
|
6 #Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD |
|
7 # |
|
8 #See COPYING file for copyrights details. |
|
9 # |
|
10 #This library is free software; you can redistribute it and/or |
|
11 #modify it under the terms of the GNU General Public |
|
12 #License as published by the Free Software Foundation; either |
|
13 #version 2.1 of the License, or (at your option) any later version. |
|
14 # |
|
15 #This library is distributed in the hope that it will be useful, |
|
16 #but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
17 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
18 #General Public License for more details. |
|
19 # |
|
20 #You should have received a copy of the GNU General Public |
|
21 #License along with this library; if not, write to the Free Software |
|
22 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
23 |
|
24 import socket |
|
25 import wx |
|
26 import wx.lib.mixins.listctrl as listmix |
|
27 from util.Zeroconf import * |
|
28 |
|
29 import connectors |
|
30 |
|
31 class AutoWidthListCtrl(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin): |
|
32 def __init__(self, parent, id, name, pos=wx.DefaultPosition, |
|
33 size=wx.DefaultSize, style=0): |
|
34 wx.ListCtrl.__init__(self, parent, id, pos, size, style, name=name) |
|
35 listmix.ListCtrlAutoWidthMixin.__init__(self) |
|
36 |
|
37 [ID_DISCOVERYDIALOG, ID_DISCOVERYDIALOGSTATICTEXT1, |
|
38 ID_DISCOVERYDIALOGSERVICESLIST, ID_DISCOVERYDIALOGREFRESHBUTTON, |
|
39 ID_DISCOVERYDIALOGLOCALBUTTON, |
|
40 ] = [wx.NewId() for _init_ctrls in range(5)] |
|
41 |
|
42 class DiscoveryDialog(wx.Dialog, listmix.ColumnSorterMixin): |
|
43 |
|
44 def _init_coll_MainSizer_Items(self, parent): |
|
45 parent.AddWindow(self.staticText1, 0, border=20, flag=wx.TOP|wx.LEFT|wx.RIGHT|wx.GROW) |
|
46 parent.AddWindow(self.ServicesList, 0, border=20, flag=wx.LEFT|wx.RIGHT|wx.GROW) |
|
47 parent.AddSizer(self.ButtonGridSizer, 0, border=20, flag=wx.LEFT|wx.RIGHT|wx.BOTTOM|wx.GROW) |
|
48 |
|
49 def _init_coll_MainSizer_Growables(self, parent): |
|
50 parent.AddGrowableCol(0) |
|
51 parent.AddGrowableRow(1) |
|
52 |
|
53 def _init_coll_ButtonGridSizer_Items(self, parent): |
|
54 parent.AddWindow(self.RefreshButton, 0, border=0, flag=0) |
|
55 parent.AddWindow(self.LocalButton, 0, border=0, flag=0) |
|
56 parent.AddSizer(self.ButtonSizer, 0, border=0, flag=0) |
|
57 |
|
58 def _init_coll_ButtonGridSizer_Growables(self, parent): |
|
59 parent.AddGrowableCol(0) |
|
60 parent.AddGrowableCol(1) |
|
61 parent.AddGrowableRow(1) |
|
62 |
|
63 def _init_sizers(self): |
|
64 self.MainSizer = wx.FlexGridSizer(cols=1, hgap=0, rows=3, vgap=10) |
|
65 self.ButtonGridSizer = wx.FlexGridSizer(cols=3, hgap=5, rows=1, vgap=0) |
|
66 |
|
67 self._init_coll_MainSizer_Items(self.MainSizer) |
|
68 self._init_coll_MainSizer_Growables(self.MainSizer) |
|
69 self._init_coll_ButtonGridSizer_Items(self.ButtonGridSizer) |
|
70 self._init_coll_ButtonGridSizer_Growables(self.ButtonGridSizer) |
|
71 |
|
72 self.SetSizer(self.MainSizer) |
|
73 |
|
74 def _init_ctrls(self, prnt): |
|
75 wx.Dialog.__init__(self, id=ID_DISCOVERYDIALOG, |
|
76 name='DiscoveryDialog', parent=prnt, |
|
77 size=wx.Size(600, 600), style=wx.DEFAULT_DIALOG_STYLE, |
|
78 title='Service Discovery') |
|
79 |
|
80 self.staticText1 = wx.StaticText(id=ID_DISCOVERYDIALOGSTATICTEXT1, |
|
81 label=_('Services available:'), name='staticText1', parent=self, |
|
82 pos=wx.Point(0, 0), size=wx.DefaultSize, style=0) |
|
83 |
|
84 # Set up list control |
|
85 self.ServicesList = AutoWidthListCtrl(id=ID_DISCOVERYDIALOGSERVICESLIST, |
|
86 name='ServicesList', parent=self, pos=wx.Point(0, 0), size=wx.Size(0, 0), |
|
87 style=wx.LC_REPORT|wx.LC_EDIT_LABELS|wx.LC_SORT_ASCENDING|wx.LC_SINGLE_SEL) |
|
88 self.ServicesList.InsertColumn(0, 'NAME') |
|
89 self.ServicesList.InsertColumn(1, 'TYPE') |
|
90 self.ServicesList.InsertColumn(2, 'IP') |
|
91 self.ServicesList.InsertColumn(3, 'PORT') |
|
92 self.ServicesList.SetColumnWidth(0, 150) |
|
93 self.ServicesList.SetColumnWidth(1, 150) |
|
94 self.ServicesList.SetColumnWidth(2, 150) |
|
95 self.ServicesList.SetColumnWidth(3, 150) |
|
96 self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected, id=ID_DISCOVERYDIALOGSERVICESLIST) |
|
97 self.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.OnItemActivated, id=ID_DISCOVERYDIALOGSERVICESLIST) |
|
98 |
|
99 listmix.ColumnSorterMixin.__init__(self, 4) |
|
100 |
|
101 self.RefreshButton = wx.Button(id=ID_DISCOVERYDIALOGREFRESHBUTTON, |
|
102 label=_('Refresh'), name='RefreshButton', parent=self, |
|
103 pos=wx.Point(0, 0), size=wx.DefaultSize, style=0) |
|
104 self.Bind(wx.EVT_BUTTON, self.OnRefreshButton, id=ID_DISCOVERYDIALOGREFRESHBUTTON) |
|
105 |
|
106 self.LocalButton = wx.Button(id=ID_DISCOVERYDIALOGLOCALBUTTON, |
|
107 label=_('Local'), name='LocalButton', parent=self, |
|
108 pos=wx.Point(0, 0), size=wx.DefaultSize, style=0) |
|
109 self.Bind(wx.EVT_BUTTON, self.OnLocalButton, id=ID_DISCOVERYDIALOGLOCALBUTTON) |
|
110 |
|
111 self.ButtonSizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTER) |
|
112 |
|
113 self._init_sizers() |
|
114 |
|
115 def __init__(self, parent): |
|
116 self._init_ctrls(parent) |
|
117 |
|
118 self.itemDataMap = {} |
|
119 self.nextItemId = 0 |
|
120 |
|
121 self.URI = None |
|
122 self.Browsers = [] |
|
123 |
|
124 self.ZeroConfInstance = Zeroconf() |
|
125 self.RefreshList() |
|
126 |
|
127 def __del__(self): |
|
128 for browser in self.Browsers: |
|
129 browser.cancel() |
|
130 self.ZeroConfInstance.close() |
|
131 |
|
132 def RefreshList(self): |
|
133 for browser in self.Browsers: |
|
134 browser.cancel() |
|
135 |
|
136 self.Browsers = [] |
|
137 for t in connectors.dnssd_connectors.keys(): |
|
138 self.Browsers.append(ServiceBrowser(self.ZeroConfInstance, t, self)) |
|
139 |
|
140 def OnRefreshButton(self, event): |
|
141 self.ServicesList.DeleteAllItems() |
|
142 self.RefreshList() |
|
143 |
|
144 def OnLocalButton(self, event): |
|
145 self.URI = "LOCAL://" |
|
146 self.EndModal(wx.ID_OK) |
|
147 event.Skip() |
|
148 |
|
149 # Used by the ColumnSorterMixin, see wx/lib/mixins/listctrl.py |
|
150 def GetListCtrl(self): |
|
151 return self.ServicesList |
|
152 |
|
153 def getColumnText(self, index, col): |
|
154 item = self.ServicesList.GetItem(index, col) |
|
155 return item.GetText() |
|
156 |
|
157 def OnItemSelected(self, event): |
|
158 self.SetURI(event.m_itemIndex) |
|
159 event.Skip() |
|
160 |
|
161 def OnItemActivated(self, event): |
|
162 self.SetURI(event.m_itemIndex) |
|
163 self.EndModal(wx.ID_OK) |
|
164 event.Skip() |
|
165 |
|
166 def SetURI(self, idx): |
|
167 connect_type = self.getColumnText(idx, 1) |
|
168 connect_address = self.getColumnText(idx, 2) |
|
169 connect_port = self.getColumnText(idx, 3) |
|
170 |
|
171 self.URI = "%s://%s:%s"%(connect_type, connect_address, connect_port) |
|
172 |
|
173 def GetURI(self): |
|
174 return self.URI |
|
175 |
|
176 def removeService(self, zeroconf, type, name): |
|
177 wx.CallAfter(self._removeService, name) |
|
178 |
|
179 |
|
180 def _removeService(self, name): |
|
181 ''' |
|
182 called when a service with the desired type goes offline. |
|
183 ''' |
|
184 |
|
185 # loop through the list items looking for the service that went offline |
|
186 for idx in xrange(self.ServicesList.GetItemCount()): |
|
187 # this is the unique identifier assigned to the item |
|
188 item_id = self.ServicesList.GetItemData(idx) |
|
189 |
|
190 # this is the full typename that was received by addService |
|
191 item_name = self.itemDataMap[item_id][4] |
|
192 |
|
193 if item_name == name: |
|
194 self.ServicesList.DeleteItem(idx) |
|
195 break |
|
196 |
|
197 def addService(self, zeroconf, type, name): |
|
198 wx.CallAfter(self._addService, type, name) |
|
199 |
|
200 def _addService(self, type, name): |
|
201 ''' |
|
202 called when a service with the desired type is discovered. |
|
203 ''' |
|
204 info = self.ZeroConfInstance.getServiceInfo(type, name) |
|
205 |
|
206 svcname = name.split(".")[0] |
|
207 typename = type.split(".")[0][1:] |
|
208 ip = str(socket.inet_ntoa(info.getAddress())) |
|
209 port = info.getPort() |
|
210 |
|
211 num_items = self.ServicesList.GetItemCount() |
|
212 |
|
213 # display the new data in the list |
|
214 new_item = self.ServicesList.InsertStringItem(num_items, svcname) |
|
215 self.ServicesList.SetStringItem(new_item, 1, "%s" % typename) |
|
216 self.ServicesList.SetStringItem(new_item, 2, "%s" % ip) |
|
217 self.ServicesList.SetStringItem(new_item, 3, "%s" % port) |
|
218 |
|
219 # record the new data for the ColumnSorterMixin |
|
220 # we assign every list item a unique id (that won't change when items |
|
221 # are added or removed) |
|
222 self.ServicesList.SetItemData(new_item, self.nextItemId) |
|
223 |
|
224 # the value of each column has to be stored in the itemDataMap |
|
225 # so that ColumnSorterMixin knows how to sort the column. |
|
226 |
|
227 # "name" is included at the end so that self.removeService |
|
228 # can access it. |
|
229 self.itemDataMap[self.nextItemId] = [ svcname, typename, ip, port, name ] |
|
230 |
|
231 self.nextItemId += 1 |
|
232 |