etisserant@203: # -*- coding: utf-8 -*- etisserant@203: etisserant@203: #This file is part of Beremiz, a Integrated Development Environment for etisserant@203: #programming IEC 61131-3 automates supporting plcopen standard and CanFestival. etisserant@203: # etisserant@203: #Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD etisserant@203: # etisserant@203: #See COPYING file for copyrights details. etisserant@203: # etisserant@203: #This library is free software; you can redistribute it and/or etisserant@203: #modify it under the terms of the GNU General Public etisserant@203: #License as published by the Free Software Foundation; either etisserant@203: #version 2.1 of the License, or (at your option) any later version. etisserant@203: # etisserant@203: #This library is distributed in the hope that it will be useful, etisserant@203: #but WITHOUT ANY WARRANTY; without even the implied warranty of etisserant@203: #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU etisserant@203: #General Public License for more details. etisserant@203: # etisserant@203: #You should have received a copy of the GNU General Public etisserant@203: #License along with this library; if not, write to the Free Software etisserant@203: #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA etisserant@203: laurent@399: import socket etisserant@203: import wx laurent@399: import wx.lib.mixins.listctrl as listmix btaylor@357: from Zeroconf import * laurent@399: laurent@399: import connectors etisserant@203: b@375: class AutoWidthListCtrl(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin): laurent@392: def __init__(self, parent, id, name, pos=wx.DefaultPosition, etisserant@203: size=wx.DefaultSize, style=0): laurent@392: wx.ListCtrl.__init__(self, parent, id, pos, size, style, name=name) etisserant@203: listmix.ListCtrlAutoWidthMixin.__init__(self) etisserant@203: laurent@392: [ID_DISCOVERYDIALOG, ID_DISCOVERYDIALOGSTATICTEXT1, laurent@392: ID_DISCOVERYDIALOGSERVICESLIST, ID_DISCOVERYDIALOGREFRESHBUTTON, laurent@392: ID_DISCOVERYDIALOGLOCALBUTTON, laurent@392: ] = [wx.NewId() for _init_ctrls in range(5)] laurent@392: etisserant@203: class DiscoveryDialog(wx.Dialog, listmix.ColumnSorterMixin): laurent@392: laurent@392: def _init_coll_MainSizer_Items(self, parent): laurent@392: parent.AddWindow(self.staticText1, 0, border=20, flag=wx.TOP|wx.LEFT|wx.RIGHT|wx.GROW) laurent@392: parent.AddWindow(self.ServicesList, 0, border=20, flag=wx.LEFT|wx.RIGHT|wx.GROW) laurent@392: parent.AddSizer(self.ButtonGridSizer, 0, border=20, flag=wx.LEFT|wx.RIGHT|wx.BOTTOM|wx.GROW) laurent@392: laurent@392: def _init_coll_MainSizer_Growables(self, parent): laurent@392: parent.AddGrowableCol(0) laurent@392: parent.AddGrowableRow(1) laurent@392: laurent@392: def _init_coll_ButtonGridSizer_Items(self, parent): laurent@392: parent.AddWindow(self.RefreshButton, 0, border=0, flag=0) laurent@392: parent.AddWindow(self.LocalButton, 0, border=0, flag=0) laurent@392: parent.AddSizer(self.ButtonSizer, 0, border=0, flag=0) laurent@392: laurent@392: def _init_coll_ButtonGridSizer_Growables(self, parent): laurent@392: parent.AddGrowableCol(0) laurent@392: parent.AddGrowableCol(1) laurent@392: parent.AddGrowableRow(1) laurent@392: laurent@392: def _init_sizers(self): laurent@392: self.MainSizer = wx.FlexGridSizer(cols=1, hgap=0, rows=3, vgap=10) laurent@392: self.ButtonGridSizer = wx.FlexGridSizer(cols=3, hgap=5, rows=1, vgap=0) laurent@392: laurent@392: self._init_coll_MainSizer_Items(self.MainSizer) laurent@392: self._init_coll_MainSizer_Growables(self.MainSizer) laurent@392: self._init_coll_ButtonGridSizer_Items(self.ButtonGridSizer) laurent@392: self._init_coll_ButtonGridSizer_Growables(self.ButtonGridSizer) laurent@392: laurent@392: self.SetSizer(self.MainSizer) laurent@392: laurent@392: def _init_ctrls(self, prnt): laurent@392: wx.Dialog.__init__(self, id=ID_DISCOVERYDIALOG, laurent@392: name='DiscoveryDialog', parent=prnt, laurent@392: size=wx.Size(600, 600), style=wx.DEFAULT_DIALOG_STYLE, laurent@392: title='Service Discovery') laurent@392: laurent@392: self.staticText1 = wx.StaticText(id=ID_DISCOVERYDIALOGSTATICTEXT1, laurent@392: label=_('Services available:'), name='staticText1', parent=self, laurent@392: pos=wx.Point(0, 0), size=wx.DefaultSize, style=0) laurent@392: laurent@392: # Set up list control laurent@392: self.ServicesList = AutoWidthListCtrl(id=ID_DISCOVERYDIALOGSERVICESLIST, laurent@392: name='ServicesList', parent=self, pos=wx.Point(0, 0), size=wx.Size(0, 0), laurent@392: style=wx.LC_REPORT|wx.LC_EDIT_LABELS|wx.LC_SORT_ASCENDING|wx.LC_SINGLE_SEL) laurent@392: self.ServicesList.InsertColumn(0, 'NAME') laurent@392: self.ServicesList.InsertColumn(1, 'TYPE') laurent@392: self.ServicesList.InsertColumn(2, 'IP') laurent@392: self.ServicesList.InsertColumn(3, 'PORT') laurent@392: self.ServicesList.SetColumnWidth(0, 150) laurent@392: self.ServicesList.SetColumnWidth(1, 150) laurent@392: self.ServicesList.SetColumnWidth(2, 150) laurent@392: self.ServicesList.SetColumnWidth(3, 150) laurent@392: self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected, id=ID_DISCOVERYDIALOGSERVICESLIST) laurent@392: self.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.OnItemActivated, id=ID_DISCOVERYDIALOGSERVICESLIST) laurent@392: etisserant@203: listmix.ColumnSorterMixin.__init__(self, 4) laurent@392: laurent@392: self.RefreshButton = wx.Button(id=ID_DISCOVERYDIALOGREFRESHBUTTON, laurent@392: label=_('Refresh'), name='RefreshButton', parent=self, laurent@392: pos=wx.Point(0, 0), size=wx.DefaultSize, style=0) laurent@392: self.Bind(wx.EVT_BUTTON, self.OnRefreshButton, id=ID_DISCOVERYDIALOGREFRESHBUTTON) laurent@392: laurent@392: self.LocalButton = wx.Button(id=ID_DISCOVERYDIALOGLOCALBUTTON, laurent@392: label=_('Local'), name='LocalButton', parent=self, laurent@392: pos=wx.Point(0, 0), size=wx.DefaultSize, style=0) laurent@392: self.Bind(wx.EVT_BUTTON, self.OnLocalButton, id=ID_DISCOVERYDIALOGLOCALBUTTON) laurent@392: laurent@392: self.ButtonSizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTER) laurent@392: laurent@392: self._init_sizers() laurent@392: laurent@392: def __init__(self, parent): laurent@392: self._init_ctrls(parent) laurent@392: b@375: self.itemDataMap = {} b@375: self.nextItemId = 0 laurent@392: laurent@392: self.URI = None laurent@399: self.Browsers = [] laurent@392: laurent@392: self.ZeroConfInstance = Zeroconf() etisserant@221: self.RefreshList() laurent@392: laurent@392: def __del__(self): laurent@399: for browser in self.Browsers: laurent@399: browser.cancel() laurent@392: self.ZeroConfInstance.close() laurent@392: etisserant@221: def RefreshList(self): laurent@399: for browser in self.Browsers: laurent@399: browser.cancel() laurent@399: laurent@399: self.Browsers = [] laurent@399: for t in connectors.dnssd_connectors.keys(): laurent@399: self.Browsers.append(ServiceBrowser(self.ZeroConfInstance, t, self)) etisserant@221: etisserant@221: def OnRefreshButton(self, event): laurent@392: self.ServicesList.DeleteAllItems() etisserant@221: self.RefreshList() etisserant@221: laurent@392: def OnLocalButton(self, event): laurent@392: self.URI = "LOCAL://" laurent@392: self.EndModal(wx.ID_OK) btaylor@357: event.Skip() btaylor@357: etisserant@203: # Used by the ColumnSorterMixin, see wx/lib/mixins/listctrl.py etisserant@203: def GetListCtrl(self): laurent@392: return self.ServicesList etisserant@203: etisserant@203: def getColumnText(self, index, col): laurent@392: item = self.ServicesList.GetItem(index, col) etisserant@203: return item.GetText() etisserant@203: etisserant@203: def OnItemSelected(self, event): laurent@392: self.SetURI(event.m_itemIndex) etisserant@203: event.Skip() etisserant@203: etisserant@203: def OnItemActivated(self, event): laurent@392: self.SetURI(event.m_itemIndex) laurent@392: self.EndModal(wx.ID_OK) etisserant@203: event.Skip() etisserant@203: laurent@392: def SetURI(self, idx): laurent@392: connect_type = self.getColumnText(idx, 1) laurent@392: connect_address = self.getColumnText(idx, 2) laurent@392: connect_port = self.getColumnText(idx, 3) laurent@392: laurent@392: self.URI = "%s://%s:%s"%(connect_type, connect_address, connect_port) laurent@392: laurent@392: def GetURI(self): laurent@392: return self.URI etisserant@203: btaylor@357: def removeService(self, zeroconf, type, name): b@375: ''' b@375: called when a service with the desired type goes offline. b@375: ''' laurent@392: b@375: # loop through the list items looking for the service that went offline laurent@392: for idx in xrange(self.ServicesList.GetItemCount()): b@375: # this is the unique identifier assigned to the item laurent@392: item_id = self.ServicesList.GetItemData(idx) b@375: b@375: # this is the full typename that was received by addService b@375: item_name = self.itemDataMap[item_id][4] b@375: b@375: if item_name == name: laurent@392: self.ServicesList.DeleteItem(idx) b@375: break laurent@392: greg@262: def addService(self, zeroconf, type, name): b@375: ''' b@375: called when a service with the desired type is discovered. b@375: ''' laurent@392: info = self.ZeroConfInstance.getServiceInfo(type, name) b@374: b@374: svcname = name.split(".")[0] etisserant@203: typename = type.split(".")[0][1:] b@374: ip = str(socket.inet_ntoa(info.getAddress())) b@374: port = info.getPort() b@374: laurent@392: num_items = self.ServicesList.GetItemCount() b@375: b@375: # display the new data in the list laurent@392: new_item = self.ServicesList.InsertStringItem(num_items, svcname) laurent@392: self.ServicesList.SetStringItem(new_item, 1, "%s" % typename) laurent@392: self.ServicesList.SetStringItem(new_item, 2, "%s" % ip) laurent@392: self.ServicesList.SetStringItem(new_item, 3, "%s" % port) b@375: b@375: # record the new data for the ColumnSorterMixin b@375: # we assign every list item a unique id (that won't change when items b@375: # are added or removed) laurent@392: self.ServicesList.SetItemData(new_item, self.nextItemId) b@375: b@375: # the value of each column has to be stored in the itemDataMap b@375: # so that ColumnSorterMixin knows how to sort the column. b@375: b@375: # "name" is included at the end so that self.removeService b@375: # can access it. b@375: self.itemDataMap[self.nextItemId] = [ svcname, typename, ip, port, name ] b@375: b@375: self.nextItemId += 1 laurent@392: