class poller: def __init__(self, model): self.log = logging.getLogger("poller") self.model = model self.Pollfrequancy = Observable(1) self.PollNext = Observable(datetime.datetime.now()) def isDue(self): lastPolled = self.PollNext.get() now = datetime.datetime.now() if now > lastPolled: return True return False def handleResponce(self, responce, ding, dong, foo): self.log.error('Programing error this methd shoudl be overridern') def wrapOutput(self, listcommands): now = datetime.datetime.now() pollfrequancy = self.Pollfrequancy.get() next = now + datetime.timedelta(seconds=pollfrequancy) self.PollNext.update(next) output = [] for command in listcommands: seconds, message = command msg = json.dumps(message, sort_keys=True, indent=4) #print 'seconds, msg' ,seconds, msg next = now + datetime.timedelta(seconds=seconds) diction = {'dueDate': next, 'msg': msg} output.append(diction) return output
class squeezePlayerMdl: def __init__(self, index, identifier=None, Name=None): self.index = Observable(index) self.identifier = Observable(identifier) self.name = Observable(Name) self.discovered = Observable(False) self.index.addCallback(self.OnAtribChange) self.identifier.addCallback(self.OnAtribChange) self.name.addCallback(self.OnAtribChange) self.operationMode = Observable(None) self.CurrentTrackTitle = Observable(None) self.CurrentTrackArtist = Observable(None) self.CurrentTrackEnds = Observable(None) self.CurrentTrackId = Observable(None) def OnAtribChange(self, value): discovered = True if self.name.get() == None: discovered = False if self.identifier.get() == None: discovered = False previously = self.discovered.get() if not previously == discovered: self.discovered.set(discovered)
class poller: def __init__(self, model): self.log = logging.getLogger("poller") self.model = model self.Pollfrequancy = Observable(1) self.PollNext = Observable(datetime.datetime.now()) def isDue(self): lastPolled = self.PollNext.get() now = datetime.datetime.now() if now > lastPolled: return True return False def handleResponce(self, responce, ding, dong, foo): self.log.error('Programing error this methd shoudl be overridern') def wrapOutput(self, listcommands): now = datetime.datetime.now() pollfrequancy = self.Pollfrequancy.get() next = now + datetime.timedelta(seconds=pollfrequancy) self.PollNext.update(next) output = [] for command in listcommands: seconds, message = command msg = json.dumps(message, sort_keys=True, indent=4) #print 'seconds, msg' ,seconds, msg next = now + datetime.timedelta(seconds=seconds) diction = { 'dueDate' : next, 'msg' : msg } output.append(diction) return output
class squeezePlayerMdl: def __init__(self, index, identifier=None, Name=None): self.index = Observable(index) self.identifier = Observable(identifier) self.name = Observable(Name) self.discovered = Observable(False) self.index.addCallback(self.OnAtribChange) self.identifier.addCallback(self.OnAtribChange) self.name.addCallback(self.OnAtribChange) self.operationMode = Observable(None) self.CurrentTrackTitle = Observable(None) self.CurrentTrackArtist = Observable(None) self.CurrentTrackEnds = Observable(None) self.CurrentTrackId = Observable(None) def OnAtribChange(self, value): discovered = True if (self.name.get() == None): discovered = False if (self.identifier.get() == None): discovered = False previously = self.discovered.get() if not previously == discovered: self.discovered.set(discovered)
class squeezeConMdle: def __init__(self): self.host = Observable("localhost") self.port = Observable(9000) # connectionStr : a simple to observe break of connection settings obj self.connectionStr = Observable("localhost:9000") self.connected = Observable(False) # Number of players on the server that can be used self.playersCount = Observable(0) # Socket Computer diagnostic error. Will be 0 when no error self.SocketErrNo = Observable(0) # Socket Human diagnostic error. Will be "" with no error, self.SocketErrMsg = Observable("") self.playerList = [] self.Players = ObservableDict() self.CbPlayersAvailable= [] self.CbChurrentTrack = [] self.host.addCallback(self.OnHostChange) self.port.addCallback(self.OnPortChange) self.connectionStr.addCallback(self.OnConnectedChange) self.connected.addCallback(self.OnConnectedChange) self.playersCount.addCallback(self.OnPlayersCountChange) self.SongCache = ObservableDict() def OnHostChange(self, value): newHost = self.host.get() newPort = self.port.get() newConnectionStr = "%s:%s" % (self.host.get(), self.port.get()) self.connectionStr.update(newConnectionStr) def OnPortChange(self, value): newHost = self.host.get() newPort = self.port.get() newConnectionStr = "%s:%s" % (self.host.get(), self.port.get()) self.connectionStr.update(newConnectionStr) def OnConnectedChange(self, value): if not self.connected.get(): if 0 != self.playersCount.get(): self.playersCount.set(0) def OnPlayersCountChange(self, value): self.playerList = [] for index in range(value): self.playerList.append(squeezePlayerMdl(index)) self.playerList[index].discovered.addCallback(self.OnPlayersAvailableChange) self.playerList[index].CurrentTrackId.addCallback(self.OnCurrentTrack) self.OnPlayersAvailableChange(value) def OnPlayersAvailableChange(self, value): #print "OnPlayersAvailableChange" AvailablePlayersList = [] for index in range(len(self.playerList)): if True != self.playerList[index].discovered.get(): continue PlayerName = self.playerList[index].name.get() AvailablePlayersList.append(PlayerName) if PlayerName in self.Players: continue self.Players[PlayerName] = self.playerList[index] AvailablePlayersSet = set(AvailablePlayersList) for item in AvailablePlayersSet.symmetric_difference(self.Players): del self.Players[item] for func, args, kargs in self.CbPlayersAvailable: func(*args, **kargs) def CbPlayersAvailableAdd(self, func, *args, **kargs): self.CbPlayersAvailable.append((func, args, kargs)) def CbChurrentTrackAdd(self, func, *args, **kargs): self.CbChurrentTrack.append((func, args, kargs)) def OnCurrentTrack(self, value): #print "OnCurrentTrack (%s)" % value for func, args, kargs in self.CbChurrentTrack: func(*args, **kargs) def playerListClear(self): self.playerList = [] if 0 != self.playersCount.get(): self.playersCount.set(0)
class TaskBarIconPresentor(object): def __init__(self, Model, View, interactor): """Takes as a model a taskBarMdle""" self.log = logging.getLogger("TaskBarIconPresentor") self.Model = Model self.View = View interactor.install(self, self.View) #self.tbupdator = TaskBarIconUpdateInteractor() #self.tbupdator.install(self.Model, self.View) self.currentPlayer = Observable(None) self.callbacks = { "on_settings": {}, "on_modelUpdate": {}, "on_popupMenu": {}, "on_left_up": {}, } self.TaskBarIconName = None self.Model.currentIconName.addCallback(self._OnIconChange) self.Model.tooltip.addCallback(self._OnToolTipChange) def doCbAbstract(self, indexer): results = {} if not indexer in self.callbacks.keys(): return results for item in self.callbacks[indexer]: results[item] = item() return results def doCbModelUpdate(self): for item in self.callbacks["on_modelUpdate"]: item(self) def doCbPopupMenu(self): return self.doCbAbstract("on_popupMenu") def doCbExit(self): return self.doCbAbstract("on_exit") def _OnIconChange(self, IconName): self.View.set_icon(IconName, (16, 16)) #self.doCbModelUpdate() def _OnToolTipChange(self, ToolTip): self.log.debug("_OnToolTipChange") self.View.set_toolTip(ToolTip) def cbAddReqMdlUpdate(self, func): self.callbacks['on_modelUpdate'][func] = 1 def cbAddRequestPopUpMenu(self, func): self.callbacks['on_popupMenu'][func] = 1 def cbAddOnSettings(self, func): self.callbacks['on_settings'].append(func) def cbAddOnToolTipUpdate(self, func): self.callbacks['on_settings'].append(func) def cbAddOnExit(self, func): self.callbacks['on_exit'].append(func) def OnTrack(self): self.UpdateToolTip() def GetSqueezeServerPlayer(self): return self.currentPlayer.get() def UpdateToolTip(self): newToolTip = self.Model.tooltip.get() #self.View.set_toolTip(newToolTip) return newToolTip def OnPlayers(self): #print "OnPlayers(=%s)" % (Event) self.UpdateToolTip() #self.View.set_icon("ART_APPLICATION_STATUS_DISCONECTED",(16,16)) self.View.set_icon("ART_APPLICATION_STATUS_CONNECTED", (16, 16)) def SelectPopupMenu(self): self.log.debug("CreatePopupMenu") resultset = self.doCbPopupMenu() #print "resultset",len(resultset) for item in resultset: #self.log.debug( "CreatePopupMenu=%s" % (item)) rc = item() if rc == None: continue return rc self.log.debug("CreatePopupMenu=None") return None def on_move(self): #print 'on_move' pass self.UpdateToolTip() def on_settings(self): for item in self.callbacks["on_settings"]: item() def playerChanged1(self, value): if value != self.Model.GuiPlayer.get(): self.Model.GuiPlayer.set(value) #self.View.set_icon("ART_APPLICATION_STATUS_CONNECTED",(16,16)) def on_exit(self): #self.on_settings_close(event) #wx.CallAfter(self.Destroy) #self.View.Exit() self.doCbExit() def on_left_up(self, ): #self.log.debug( 'on_left_up=%s' % self.GetSqueezeServerPlayer()) return self.doCbAbstract("on_left_up") def on_right_down(self): #print 'on_right_down' pass def on_right_up(self): self.log.debug('on_right_up') #menu = self.CreatePopupMenu() #print dir (menu) def on_right_dclick(self): self.log.debug('on_right_dclick') #self.CreatePopUp( event.GetPoint() ) def on_click(self): pass def on_left_down(self): #print 'Tray icon was left-clicked.' pass def on_left_dclick(self): #print 'Tray icon was on_left_dclick-clicked.' pass def setIcon(self, IconName): #self.View.set_icon("ART_APPLICATION_STATUS_CONNECTED",(16,16)) self.View.set_icon(IconName, (16, 16))
class interactorNowPlaying: def __init__(self): self.installGui(None) def installGui(self, gui): self.view = gui if self.view == None: return self.view.cbPlayer.Bind(wx.EVT_COMBOBOX, self.OnCbSelect) self.view.BtnPause.Bind(wx.EVT_BUTTON, self.OnCbPause) self.view.BtnNext.Bind(wx.EVT_BUTTON, self.OnCbTrackSeekForward) self.view.BtnLast.Bind(wx.EVT_BUTTON, self.OnCbTrackSeekBackward) self.view.BtnPlay.Bind(wx.EVT_BUTTON, self.OnCbPlay) self.view.BtnStop.Bind(wx.EVT_BUTTON, self.OnCbStop) self.view.Bind(wx.EVT_MENU, self.OnShowSettings, id=self.view.MenuItemSettings.GetId()) self.view.Bind(wx.EVT_MENU, self.OnCbPause, id=self.view.MenuItemPause.GetId()) self.view.Bind(wx.EVT_MENU, self.OnCbTrackSeekForward, id=self.view.MenuItemNext.GetId()) self.view.Bind(wx.EVT_MENU, self.OnCbTrackSeekBackward, id=self.view.MenuItemLast.GetId()) self.view.Bind(wx.EVT_MENU, self.OnCbPlay, id=self.view.MenuItemPlay.GetId()) self.view.Bind(wx.EVT_MENU, self.OnCbStop, id=self.view.MenuItemStop.GetId()) self.view.Bind(wx.EVT_MENU, self.OnCbRandomSong, id=self.view.MenuItemRndSong.GetId()) self.updateView() self.OnUpdateFrmIcon() def install(self, mdlNowPlaying, mainInteractor): self.mdlNowPlaying = mdlNowPlaying self.watchers = {} self.watchersSong = {} self.mainInteractor = mainInteractor self.mdlNowPlaying.availablePlayer.addCallback(self.onAvailablePlayer) self.mdlNowPlaying.nowPlayPlayerId.addCallback(self.updatePlayerId) self.mdlNowPlaying.CurrentTrackId.addCallback(self.updateTrackId) self.mdlNowPlaying.CurrentTrackName.addCallback(self.updateTrackName) self.mdlNowPlaying.CurrentTrackArtist.addCallback( self.updateTrackArtist) self.mdlNowPlaying.CurrentTrackAlbum.addCallback(self.updateTrackAlbum) self.mdlNowPlaying.CurrentTrackEnds.addCallback(self.updateTrackEnds) self.mdlNowPlaying.CurrentTrackDuration.addCallback( self.updateTrackEnds) self.mdlNowPlaying.CurrentPlayerStatus.addCallback( self.updatePlayerStatus) self.mdlNowPlaying.frmCurrentIconName.addCallback(self.OnUpdateFrmIcon) self.mdlNowPlaying.serverConnected.addCallback(self.OnConnected) self.watchingPlayer = Observable(None) self.watchingSong = Observable(None) for player in self.mdlNowPlaying.availablePlayer.keys(): self.onAvailablePlayer(player) self.updateView() def UpdateIcon(self, event=None): if self.view == None: return connected = self.mdlNowPlaying.serverConnected.get() playerId = self.mdlNowPlaying.CurrentTrackId.get() playerStatus = self.mdlNowPlaying.CurrentPlayerStatus.get() iconName = 'ART_APPLICATION_STATUS_DISCONECTED' if connected: iconName = 'ART_APPLICATION_STATUS_CONNECTED' if playerId != None: iconName = 'ART_PLAYER_PLAY' if playerStatus != None: mapping = { 'playing': 'ART_PLAYER_PLAY', 'paused': 'ART_PLAYER_PAUSE', 'stop': 'ART_PLAYER_STOP' } if playerStatus in mapping.keys(): iconName = mapping[playerStatus] self.mdlNowPlaying.frmCurrentIconName.update(iconName) def OnConnected(self, event=None): self.UpdateIcon() def OnUpdateFrmIcon(self, event=None): if self.view == None: return iconName = self.mdlNowPlaying.frmCurrentIconName.get() if iconName == None: return icon = wx.ArtProvider.GetIcon(iconName, wx.ART_TOOLBAR, (16, 16)) self.view.SetIcon(icon) def OnShowSettings(self, event): self.mainInteractor.doCbOnSettings(event) def OnCbPlay(self, event): currentPlayer = self.mdlNowPlaying.nowPlayPlayerId.get() if currentPlayer != None: self.mainInteractor.doCbOnPlay(event, currentPlayer) def OnCbStop(self, event): currentPlayer = self.mdlNowPlaying.nowPlayPlayerId.get() if currentPlayer != None: self.mainInteractor.doCbOnStop(event, currentPlayer) def OnCbSelect(self, event): value = event.GetString() if not value in self.mdlNowPlaying.availablePlayer.keys(): return self.mdlNowPlaying.nowPlayPlayerId.update(value) self.updatePlayerStatus() def OnCbPause(self, event): currentPlayer = self.mdlNowPlaying.nowPlayPlayerId.get() if currentPlayer != None: self.mainInteractor.doCbOnPause(event, currentPlayer) def OnCbTrackSeekForward(self, event): currentPlayer = self.mdlNowPlaying.nowPlayPlayerId.get() if currentPlayer != None: self.mainInteractor.doCbOnSeekForward(event, currentPlayer) def OnCbTrackSeekBackward(self, event): currentPlayer = self.mdlNowPlaying.nowPlayPlayerId.get() if currentPlayer != None: self.mainInteractor.doCbOnSeekBackwards(event, currentPlayer) def OnCbRandomSong(self, event): currentPlayer = self.mdlNowPlaying.nowPlayPlayerId.get() if currentPlayer != None: self.mainInteractor.doCbOnRandomSongs(event, currentPlayer) def updateView(self): availablePlayers = set(self.mdlNowPlaying.availablePlayer.keys()) #playerslist = set(self.view.Players.keys()) watcherlist = set(self.watchers.keys()) watchers2delnew = watcherlist.difference(availablePlayers) watchers2new = availablePlayers.difference(watcherlist) for watch in watchers2new: newWatcher = watcherPlayer() self.watchers[watch] = watcherPlayer() self.watchers[watch].install( self.mdlNowPlaying.availablePlayer[watch], self.mdlNowPlaying) for watch in watchers2delnew: del self.watchers[watch] self.updateCombo() self.updateTrackName() self.updateTrackArtist() self.updateTrackAlbum() self.updateTrackEnds() #self.OnConnected() def onOperatingMode(self, key): self.log.error("onOperatingMode=" % (self.watchingPlayer.get())) def onCurrentTrackEnds(self, key): self.log.error("onCurrentTrackEnds=" % (self.watchingPlayer.get())) def onOperatingMode(self, operationMode): self.log.error("onTrackId=" % (self.watchingPlayer.get())) playerId = self.mdlNowPlaying.nowPlayPlayerId.get() self.mdlNowPlaying.operationMode.update(operationMode) def onAvailablePlayer(self, key): added = False if not key in self.mdlNowPlaying.availablePlayer.keys(): added = True self.updateView() currentKey = self.mdlNowPlaying.nowPlayPlayerId.get() if None == currentKey: defaultPlayer = self.mdlNowPlaying.CurrentPlayerIdDefault.get() if None == defaultPlayer: defaultPlayer = key self.mdlNowPlaying.nowPlayPlayerId.update(defaultPlayer) self.updateCombo() def updateCombo(self, key=None): if self.view == None: return availableKeys = self.mdlNowPlaying.availablePlayer.keys() availableKeysLen = len(availableKeys) combokeysDesitered = set(['']) if availableKeysLen > 0: combokeysDesitered = set(availableKeys) combokeysFound = set(self.view.cbPlayer.Items) todelete = combokeysFound.difference(combokeysDesitered) newkeys = combokeysDesitered.difference(combokeysFound) # No delete option in wxpython so this work around if len(todelete) > 0: self.view.cbPlayer.Clear() newkeys = availableKeys for thiskey in newkeys: self.view.cbPlayer.Append(thiskey) selectedplayer = self.mdlNowPlaying.nowPlayPlayerId.get() if selectedplayer != None: if selectedplayer in self.view.cbPlayer.Items: self.view.cbPlayer.SetValue(selectedplayer) def updatePlayerId(self, value): self.UpdateIcon() self.updateView() currentKey = self.mdlNowPlaying.nowPlayPlayerId.get() if None == currentKey: return watcherlist = set(self.watchers.keys()) if value == None: availableKeys = self.mdlNowPlaying.availablePlayer.keys() availableKeysLen = len(availableKeys) if availableKeysLen > 0: value = availableKeys[0] self.mdlNowPlaying.nowPlayPlayerId.update(availableKeys[0]) self.view.cbPlayer.SetValue(availableKeys[0]) for player in watcherlist: if currentKey == player: self.watchers[player].enable() self.watchers[player].update() else: self.watchers[player].disable() #self.watchPlayer(value) playerId = self.mdlNowPlaying.nowPlayPlayerId.get() if playerId == None: return if not playerId in self.mdlNowPlaying.availablePlayer.keys(): return def updateTrackId(self, TrackId): currentWatching = self.mdlNowPlaying.CurrentTrackId.get() if not currentWatching in self.mdlNowPlaying.availableSong.keys(): return self.songWatch = SongWatcher() self.songWatch.install(self.mdlNowPlaying.availableSong[TrackId], self.mdlNowPlaying) def updateTrackName(self, event=None): if self.view == None: return displayText = '' TrackName = self.mdlNowPlaying.CurrentTrackName.get() if TrackName == None: self.view.tcHost.SetValue('') return for track in TrackName: displayText += track self.view.tcHost.SetValue(displayText) def updateTrackArtist(self, event=None): if self.view == None: return displayText = '' TrackArtist = self.mdlNowPlaying.CurrentTrackArtist.get() if TrackArtist == None: self.view.tbArtist.SetValue('') return for track in TrackArtist: displayText += track oldValue = self.view.tbArtist.GetValue() if oldValue != displayText: self.view.tbArtist.SetValue(displayText) def updateTrackAlbum(self, event=None): if self.view == None: return displayText = '' TrackAlbum = self.mdlNowPlaying.CurrentTrackAlbum.get() if TrackAlbum == None: self.view.tbArtist.SetValue('') return for track in TrackAlbum: displayText += track self.view.tbAlbum.SetValue(displayText) def updateTrackEnds(self, event=None): if self.view == None: return displayText = '' TrackEnds = self.mdlNowPlaying.CurrentTrackEnds.get() if TrackEnds == None: self.view.slider.SetValue(10000) return Tracklength = self.mdlNowPlaying.CurrentTrackDuration.get() if Tracklength == None: self.view.slider.SetValue(10000) return now = datetime.datetime.now() timediff = TrackEnds - now length = Tracklength[0] remaining = Tracklength[0] - timediff.seconds sliderpos = remaining * 10000 / Tracklength[0] if sliderpos > 10000: sliderpos = 10000 self.view.slider.SetValue(sliderpos) def updatePlayerStatus(self, event=None): self.UpdateIcon() if self.view == None: return status = self.mdlNowPlaying.CurrentPlayerStatus.get() disablePause = False disableplay = False disableStop = False if status == None: disablePause = True disableplay = True disableStop = True if status == 'paused': disablePause = True if status == 'playing': disableplay = True if status == 'stop': disablePause = True disableStop = True if disableplay == self.view.BtnPlay.Enabled: if disableplay: self.view.BtnPlay.Disable() else: self.view.BtnPlay.Enable() if disablePause == self.view.BtnPause.Enabled: if disablePause: self.view.BtnPause.Disable() else: self.view.BtnPause.Enable() if disableStop == self.view.BtnStop.Enabled: if disableStop: self.view.BtnStop.Disable() else: self.view.BtnStop.Enable()
class squeezeConMdle: def __init__(self): self.host = Observable("localhost") self.port = Observable(9000) # connectionStr : a simple to observe break of connection settings obj self.connectionStr = Observable("localhost:9000") self.connected = Observable(False) # Number of players on the server that can be used self.playersCount = Observable(0) # Socket Computer diagnostic error. Will be 0 when no error self.SocketErrNo = Observable(0) # Socket Human diagnostic error. Will be "" with no error, self.SocketErrMsg = Observable("") self.playerList = [] self.Players = ObservableDict() self.CbPlayersAvailable = [] self.CbChurrentTrack = [] self.host.addCallback(self.OnHostChange) self.port.addCallback(self.OnPortChange) self.connectionStr.addCallback(self.OnConnectedChange) self.connected.addCallback(self.OnConnectedChange) self.playersCount.addCallback(self.OnPlayersCountChange) self.SongCache = ObservableDict() def OnHostChange(self, value): newHost = self.host.get() newPort = self.port.get() newConnectionStr = "%s:%s" % (self.host.get(), self.port.get()) self.connectionStr.update(newConnectionStr) def OnPortChange(self, value): newHost = self.host.get() newPort = self.port.get() newConnectionStr = "%s:%s" % (self.host.get(), self.port.get()) self.connectionStr.update(newConnectionStr) def OnConnectedChange(self, value): if not self.connected.get(): if 0 != self.playersCount.get(): self.playersCount.set(0) def OnPlayersCountChange(self, value): self.playerList = [] for index in range(value): self.playerList.append(squeezePlayerMdl(index)) self.playerList[index].discovered.addCallback( self.OnPlayersAvailableChange) self.playerList[index].CurrentTrackId.addCallback( self.OnCurrentTrack) self.OnPlayersAvailableChange(value) def OnPlayersAvailableChange(self, value): #print "OnPlayersAvailableChange" AvailablePlayersList = [] for index in range(len(self.playerList)): if True != self.playerList[index].discovered.get(): continue PlayerName = self.playerList[index].name.get() AvailablePlayersList.append(PlayerName) if PlayerName in self.Players: continue self.Players[PlayerName] = self.playerList[index] AvailablePlayersSet = set(AvailablePlayersList) for item in AvailablePlayersSet.symmetric_difference(self.Players): del self.Players[item] for func, args, kargs in self.CbPlayersAvailable: func(*args, **kargs) def CbPlayersAvailableAdd(self, func, *args, **kargs): self.CbPlayersAvailable.append((func, args, kargs)) def CbChurrentTrackAdd(self, func, *args, **kargs): self.CbChurrentTrack.append((func, args, kargs)) def OnCurrentTrack(self, value): #print "OnCurrentTrack (%s)" % value for func, args, kargs in self.CbChurrentTrack: func(*args, **kargs) def playerListClear(self): self.playerList = [] if 0 != self.playersCount.get(): self.playersCount.set(0)
class interactorNowPlaying: def __init__(self): self.installGui(None) def installGui(self,gui): self.view = gui if self.view == None: return self.view.cbPlayer.Bind(wx.EVT_COMBOBOX, self.OnCbSelect) self.view.BtnPause.Bind(wx.EVT_BUTTON, self.OnCbPause) self.view.BtnNext.Bind(wx.EVT_BUTTON, self.OnCbTrackSeekForward) self.view.BtnLast.Bind(wx.EVT_BUTTON, self.OnCbTrackSeekBackward) self.view.BtnPlay.Bind(wx.EVT_BUTTON, self.OnCbPlay) self.view.BtnStop.Bind(wx.EVT_BUTTON, self.OnCbStop) self.view.Bind(wx.EVT_MENU, self.OnShowSettings,id=self.view.MenuItemSettings.GetId() ) self.view.Bind(wx.EVT_MENU, self.OnCbPause,id=self.view.MenuItemPause.GetId() ) self.view.Bind(wx.EVT_MENU, self.OnCbTrackSeekForward,id=self.view.MenuItemNext.GetId() ) self.view.Bind(wx.EVT_MENU, self.OnCbTrackSeekBackward,id=self.view.MenuItemLast.GetId() ) self.view.Bind(wx.EVT_MENU, self.OnCbPlay,id=self.view.MenuItemPlay.GetId() ) self.view.Bind(wx.EVT_MENU, self.OnCbStop,id=self.view.MenuItemStop.GetId() ) self.view.Bind(wx.EVT_MENU, self.OnCbRandomSong,id=self.view.MenuItemRndSong.GetId() ) self.updateView() self.OnUpdateFrmIcon() def install(self,mdlNowPlaying,mainInteractor): self.mdlNowPlaying = mdlNowPlaying self.watchers = {} self.watchersSong = {} self.mainInteractor = mainInteractor self.mdlNowPlaying.availablePlayer.addCallback(self.onAvailablePlayer) self.mdlNowPlaying.nowPlayPlayerId.addCallback(self.updatePlayerId) self.mdlNowPlaying.CurrentTrackId.addCallback(self.updateTrackId) self.mdlNowPlaying.CurrentTrackName.addCallback(self.updateTrackName) self.mdlNowPlaying.CurrentTrackArtist.addCallback(self.updateTrackArtist) self.mdlNowPlaying.CurrentTrackAlbum.addCallback(self.updateTrackAlbum) self.mdlNowPlaying.CurrentTrackEnds.addCallback(self.updateTrackEnds) self.mdlNowPlaying.CurrentTrackDuration.addCallback(self.updateTrackEnds) self.mdlNowPlaying.CurrentPlayerStatus.addCallback(self.updatePlayerStatus) self.mdlNowPlaying.frmCurrentIconName.addCallback(self.OnUpdateFrmIcon) self.mdlNowPlaying.serverConnected.addCallback(self.OnConnected) self.watchingPlayer = Observable(None) self.watchingSong = Observable(None) for player in self.mdlNowPlaying.availablePlayer.keys(): self.onAvailablePlayer(player) self.updateView() def UpdateIcon(self,event = None): if self.view == None: return connected = self.mdlNowPlaying.serverConnected.get() playerId = self.mdlNowPlaying.CurrentTrackId.get() playerStatus = self.mdlNowPlaying.CurrentPlayerStatus.get() iconName = 'ART_APPLICATION_STATUS_DISCONECTED' if connected: iconName = 'ART_APPLICATION_STATUS_CONNECTED' if playerId != None: iconName = 'ART_PLAYER_PLAY' if playerStatus != None: mapping = {'playing' :'ART_PLAYER_PLAY', 'paused' : 'ART_PLAYER_PAUSE', 'stop' : 'ART_PLAYER_STOP' } if playerStatus in mapping.keys(): iconName = mapping[playerStatus] self.mdlNowPlaying.frmCurrentIconName.update(iconName) def OnConnected(self,event = None): self.UpdateIcon() def OnUpdateFrmIcon(self,event = None): if self.view == None: return iconName = self.mdlNowPlaying.frmCurrentIconName.get() if iconName == None: return icon = wx.ArtProvider.GetIcon(iconName, wx.ART_TOOLBAR, (16,16)) self.view.SetIcon(icon) def OnShowSettings(self,event): self.mainInteractor.doCbOnSettings(event) def OnCbPlay(self,event): currentPlayer = self.mdlNowPlaying.nowPlayPlayerId.get() if currentPlayer != None: self.mainInteractor.doCbOnPlay(event,currentPlayer) def OnCbStop(self,event): currentPlayer = self.mdlNowPlaying.nowPlayPlayerId.get() if currentPlayer != None: self.mainInteractor.doCbOnStop(event,currentPlayer) def OnCbSelect(self,event): value = event.GetString() if not value in self.mdlNowPlaying.availablePlayer.keys(): return self.mdlNowPlaying.nowPlayPlayerId.update(value) self.updatePlayerStatus() def OnCbPause(self,event): currentPlayer = self.mdlNowPlaying.nowPlayPlayerId.get() if currentPlayer != None: self.mainInteractor.doCbOnPause(event,currentPlayer) def OnCbTrackSeekForward(self,event): currentPlayer = self.mdlNowPlaying.nowPlayPlayerId.get() if currentPlayer != None: self.mainInteractor.doCbOnSeekForward(event,currentPlayer) def OnCbTrackSeekBackward(self,event): currentPlayer = self.mdlNowPlaying.nowPlayPlayerId.get() if currentPlayer != None: self.mainInteractor.doCbOnSeekBackwards(event,currentPlayer) def OnCbRandomSong (self,event): currentPlayer = self.mdlNowPlaying.nowPlayPlayerId.get() if currentPlayer != None: self.mainInteractor.doCbOnRandomSongs(event,currentPlayer) def updateView(self): availablePlayers = set(self.mdlNowPlaying.availablePlayer.keys()) #playerslist = set(self.view.Players.keys()) watcherlist = set(self.watchers.keys()) watchers2delnew = watcherlist.difference(availablePlayers) watchers2new = availablePlayers.difference(watcherlist) for watch in watchers2new: newWatcher = watcherPlayer() self.watchers[watch] = watcherPlayer() self.watchers[watch].install( self.mdlNowPlaying.availablePlayer[watch], self.mdlNowPlaying) for watch in watchers2delnew: del self.watchers[watch] self.updateCombo() self.updateTrackName() self.updateTrackArtist() self.updateTrackAlbum() self.updateTrackEnds() #self.OnConnected() def onOperatingMode(self,key): self.log.error("onOperatingMode=" % (self.watchingPlayer.get())) def onCurrentTrackEnds(self,key): self.log.error("onCurrentTrackEnds=" % (self.watchingPlayer.get())) def onOperatingMode(self,operationMode): self.log.error("onTrackId=" % (self.watchingPlayer.get())) playerId = self.mdlNowPlaying.nowPlayPlayerId.get() self.mdlNowPlaying.operationMode.update(operationMode) def onAvailablePlayer(self,key): added = False if not key in self.mdlNowPlaying.availablePlayer.keys(): added = True self.updateView() currentKey = self.mdlNowPlaying.nowPlayPlayerId.get() if None == currentKey: defaultPlayer = self.mdlNowPlaying.CurrentPlayerIdDefault.get() if None == defaultPlayer: defaultPlayer = key self.mdlNowPlaying.nowPlayPlayerId.update(defaultPlayer) self.updateCombo() def updateCombo(self,key = None): if self.view == None: return availableKeys = self.mdlNowPlaying.availablePlayer.keys() availableKeysLen = len(availableKeys) combokeysDesitered = set(['']) if availableKeysLen > 0: combokeysDesitered = set(availableKeys) combokeysFound = set(self.view.cbPlayer.Items) todelete = combokeysFound.difference(combokeysDesitered) newkeys = combokeysDesitered.difference(combokeysFound) # No delete option in wxpython so this work around if len(todelete) > 0: self.view.cbPlayer.Clear() newkeys = availableKeys for thiskey in newkeys: self.view.cbPlayer.Append(thiskey) selectedplayer = self.mdlNowPlaying.nowPlayPlayerId.get() if selectedplayer != None: if selectedplayer in self.view.cbPlayer.Items: self.view.cbPlayer.SetValue(selectedplayer) def updatePlayerId(self,value): self.UpdateIcon() self.updateView() currentKey = self.mdlNowPlaying.nowPlayPlayerId.get() if None == currentKey: return watcherlist = set(self.watchers.keys()) if value == None: availableKeys = self.mdlNowPlaying.availablePlayer.keys() availableKeysLen = len (availableKeys) if availableKeysLen > 0: value = availableKeys[0] self.mdlNowPlaying.nowPlayPlayerId.update(availableKeys[0]) self.view.cbPlayer.SetValue(availableKeys[0]) for player in watcherlist: if currentKey == player: self.watchers[player].enable() self.watchers[player].update() else: self.watchers[player].disable() #self.watchPlayer(value) playerId = self.mdlNowPlaying.nowPlayPlayerId.get() if playerId == None: return if not playerId in self.mdlNowPlaying.availablePlayer.keys() : return def updateTrackId(self,TrackId): currentWatching = self.mdlNowPlaying.CurrentTrackId.get() if not currentWatching in self.mdlNowPlaying.availableSong.keys(): return self.songWatch = SongWatcher() self.songWatch.install(self.mdlNowPlaying.availableSong[TrackId],self.mdlNowPlaying) def updateTrackName(self,event = None): if self.view == None: return displayText = '' TrackName = self.mdlNowPlaying.CurrentTrackName.get() if TrackName == None: self.view.tcHost.SetValue('') return for track in TrackName: displayText += track self.view.tcHost.SetValue(displayText) def updateTrackArtist(self,event= None): if self.view == None: return displayText = '' TrackArtist = self.mdlNowPlaying.CurrentTrackArtist.get() if TrackArtist == None: self.view.tbArtist.SetValue('') return for track in TrackArtist: displayText += track oldValue = self.view.tbArtist.GetValue() if oldValue != displayText: self.view.tbArtist.SetValue(displayText) def updateTrackAlbum(self,event= None): if self.view == None: return displayText = '' TrackAlbum = self.mdlNowPlaying.CurrentTrackAlbum.get() if TrackAlbum == None: self.view.tbArtist.SetValue('') return for track in TrackAlbum: displayText += track self.view.tbAlbum.SetValue(displayText) def updateTrackEnds(self,event= None): if self.view == None: return displayText = '' TrackEnds = self.mdlNowPlaying.CurrentTrackEnds.get() if TrackEnds == None: self.view.slider.SetValue(10000) return Tracklength = self.mdlNowPlaying.CurrentTrackDuration.get() if Tracklength == None: self.view.slider.SetValue(10000) return now = datetime.datetime.now() timediff = TrackEnds - now length = Tracklength[0] remaining = Tracklength[0] - timediff.seconds sliderpos = remaining * 10000 / Tracklength[0] if sliderpos > 10000: sliderpos = 10000 self.view.slider.SetValue(sliderpos) def updatePlayerStatus(self,event= None): self.UpdateIcon() if self.view == None: return status = self.mdlNowPlaying.CurrentPlayerStatus.get() disablePause = False disableplay = False disableStop = False if status == None: disablePause = True disableplay = True disableStop = True if status == 'paused': disablePause = True if status == 'playing': disableplay = True if status == 'stop': disablePause = True disableStop = True if disableplay == self.view.BtnPlay.Enabled: if disableplay: self.view.BtnPlay.Disable() else: self.view.BtnPlay.Enable() if disablePause == self.view.BtnPause.Enabled: if disablePause: self.view.BtnPause.Disable() else: self.view.BtnPause.Enable() if disableStop == self.view.BtnStop.Enabled: if disableStop: self.view.BtnStop.Disable() else: self.view.BtnStop.Enable()
class TaskBarIconPresentor(object): def __init__(self, Model, View, interactor): """Takes as a model a taskBarMdle""" self.log = logging.getLogger("TaskBarIconPresentor") self.Model = Model self.View = View interactor.install(self,self.View) #self.tbupdator = TaskBarIconUpdateInteractor() #self.tbupdator.install(self.Model, self.View) self.currentPlayer = Observable(None) self.callbacks = { "on_settings" : {}, "on_modelUpdate" : {}, "on_popupMenu" : {}, "on_left_up" : {}, } self.TaskBarIconName = None self.Model.currentIconName.addCallback(self._OnIconChange) self.Model.tooltip.addCallback(self._OnToolTipChange) def doCbAbstract(self,indexer): results = {} if not indexer in self.callbacks.keys(): return results for item in self.callbacks[indexer]: results[item] = item() return results def doCbModelUpdate(self): for item in self.callbacks["on_modelUpdate"]: item(self) def doCbPopupMenu(self): return self.doCbAbstract("on_popupMenu") def doCbExit(self): return self.doCbAbstract("on_exit") def _OnIconChange(self,IconName): self.View.set_icon(IconName,(16,16)) #self.doCbModelUpdate() def _OnToolTipChange(self,ToolTip): self.log.debug("_OnToolTipChange") self.View.set_toolTip(ToolTip) def cbAddReqMdlUpdate(self,func): self.callbacks['on_modelUpdate'][func] = 1 def cbAddRequestPopUpMenu(self,func): self.callbacks['on_popupMenu'][func] = 1 def cbAddOnSettings(self,func): self.callbacks['on_settings'].append(func) def cbAddOnToolTipUpdate(self,func): self.callbacks['on_settings'].append(func) def cbAddOnExit(self,func): self.callbacks['on_exit'].append(func) def OnTrack(self): self.UpdateToolTip() def GetSqueezeServerPlayer(self): return self.currentPlayer.get() def UpdateToolTip(self): newToolTip = self.Model.tooltip.get() #self.View.set_toolTip(newToolTip) return newToolTip def OnPlayers(self): #print "OnPlayers(=%s)" % (Event) self.UpdateToolTip() #self.View.set_icon("ART_APPLICATION_STATUS_DISCONECTED",(16,16)) self.View.set_icon("ART_APPLICATION_STATUS_CONNECTED",(16,16)) def SelectPopupMenu(self): self.log.debug( "CreatePopupMenu") resultset = self.doCbPopupMenu() #print "resultset",len(resultset) for item in resultset: #self.log.debug( "CreatePopupMenu=%s" % (item)) rc = item() if rc == None: continue return rc self.log.debug( "CreatePopupMenu=None") return None def on_move(self): #print 'on_move' pass self.UpdateToolTip() def on_settings(self): for item in self.callbacks["on_settings"]: item() def playerChanged1 (self,value): if value != self.Model.GuiPlayer.get(): self.Model.GuiPlayer.set(value) #self.View.set_icon("ART_APPLICATION_STATUS_CONNECTED",(16,16)) def on_exit(self): #self.on_settings_close(event) #wx.CallAfter(self.Destroy) #self.View.Exit() self.doCbExit() def on_left_up(self,): #self.log.debug( 'on_left_up=%s' % self.GetSqueezeServerPlayer()) return self.doCbAbstract("on_left_up") def on_right_down(self): #print 'on_right_down' pass def on_right_up(self): self.log.debug( 'on_right_up') #menu = self.CreatePopupMenu() #print dir (menu) def on_right_dclick(self): self.log.debug( 'on_right_dclick') #self.CreatePopUp( event.GetPoint() ) def on_click(self): pass def on_left_down(self): #print 'Tray icon was left-clicked.' pass def on_left_dclick(self): #print 'Tray icon was on_left_dclick-clicked.' pass def setIcon(self,IconName): #self.View.set_icon("ART_APPLICATION_STATUS_CONNECTED",(16,16)) self.View.set_icon(IconName,(16,16))