class SqueezeConnectionWorker(Thread):
    """Thread executing tasks from a given tasks queue"""
    def __init__(self, tasks):
        Thread.__init__(self)
        self.tasks = tasks
        self.callbacks = { 'task_done' : {}}
        self.daemon = True
        self.start()
        self.connectionString = None
        self.SocketErrNo = Observable(0)
        self.SocketErrMsg = Observable("")
        self.log = logging.getLogger("JrpcServerthreadPool.SqueezeConnectionWorker")
        self.conn = None
    def cbAddTaskDone(self,funct):
        self.callbacks['task_done'][funct] = 1
            
    def taskDone(self,request):
        self.tasks.task_done()
        self.log.debug( 'taskDone')
        for func in self.callbacks['task_done']:
            func(request)
        # Now process nicely   
        return
    def processRequest(self,request):
        params = request['params']
        if self.connectionString == None:
            self.log.debug('Connection is none')
            return
        if self.conn == None:
            self.log.debug('Connection creating = "%s"' % (self.connectionString))
            self.conn = httplib.HTTPConnection(self.connectionString,timeout=10)
        try:
            #self.log.debug(type(params))
            self.conn.request("POST", "/jsonrpc.js", unicode(params))
        except socket.error, E:
            errorNumber = 99
            try:
                errorNumber = E.errno
            except TypeError:
                pass
            self.SocketErrNo.set(errorNumber)
            self.SocketErrMsg.set(unicode(E.strerror))
            self.conn = None
            self.log.error("Socket error.=%s" % (self.connectionString))
            return
        except httplib.CannotSendRequest, E:
            self.conn = None
            self.log.error("Cannot Send Request, resetting connection.=%s" % (params))
            return
class PopupMenuPresentor(object):
    def __init__(self, Model, View, squeezecmd, interactor):
        self.Model = Model
        self.View = View
        self.squeezeConCtrl = squeezecmd
        interactor.install(self, self.View)
        self.player = Observable(None)
        self._cb_settings = []

        self.callbacks = {
            "on_exit": {},
            "on_settings": {},
        }

    def doCbExit(self):
        results = {}
        for item in self.callbacks["on_exit"]:
            results[item] = item()
        return results

    def doCbSettings(self):
        results = {}
        for item in self.callbacks["on_settings"]:
            results[item] = item()
        return results

    def cbAddOnExit(self, func):
        self.callbacks['on_exit'][func] = 1

    def cbAddOnSettings(self, func):
        self.callbacks['on_settings'][func] = 1

    def GetSqueezeServerPlayer(self):
        return self.player.get()

    def onScPause(self):
        player = self.GetSqueezeServerPlayer()
        #print "player",player
        if player != None:
            self.squeezeConCtrl.Pause(player)
        else:
            self.on_settings()

    def onScPlay(self):
        player = self.GetSqueezeServerPlayer()
        if player != None:
            self.squeezeConCtrl.Play(player)
        else:
            self.on_settings()

    def onScNext(self):
        player = self.GetSqueezeServerPlayer()
        if player != None:
            #self.squeezecmd.squeezecmd_Index(player,1)

            self.squeezeConCtrl.Index(player, 1)
        else:
            self.on_settings()

    def onScPrevious(self):
        player = self.GetSqueezeServerPlayer()
        if player != None:
            #self.squeezecmd.squeezecmd_Index(player,-1)
            self.squeezeConCtrl.Index(player, -1)
        else:
            self.on_settings()

    def onScRandom(self):
        player = self.GetSqueezeServerPlayer()
        if player != None:
            #self.squeezecmd.squeezecmd_randomplay(player)
            self.squeezeConCtrl.PlayRandomSong(player)
        else:
            self.on_settings()

    def on_settings(self):
        self.doCbSettings()

    def ChangePlayer(self, player):
        oldPlayer = self.player.get()
        if oldPlayer != player:
            self.player.set(player)

    def on_exit(self):
        self.doCbExit()
class PopupMenuPresentor(object):
    def __init__(self, Model, View,squeezecmd, interactor):
        self.Model = Model
        self.View = View
        self.squeezeConCtrl = squeezecmd
        interactor.install(self,self.View)
        self.player = Observable(None)
        self._cb_settings = []
        
        self.callbacks = {
            "on_exit" : {},
            "on_settings" : {},
            
        }
        
    def doCbExit(self):
        results = {}
        for item in self.callbacks["on_exit"]:
            results[item] = item()
        return results

    def doCbSettings(self):
        results = {}
        for item in self.callbacks["on_settings"]:
            results[item] = item()
        return results
    
    def cbAddOnExit(self,func):
        self.callbacks['on_exit'][func] = 1   
        
    def cbAddOnSettings(self,func):
        self.callbacks['on_settings'][func] = 1
        
        
        
    def GetSqueezeServerPlayer(self):
        return self.player.get()
    def onScPause(self):
        player = self.GetSqueezeServerPlayer()
        #print "player",player
        if player != None:
            self.squeezeConCtrl.Pause(player)
        else:
            self.on_settings()    
    def onScPlay(self):
        player = self.GetSqueezeServerPlayer()
        if player != None:
            self.squeezeConCtrl.Play(player)
        else:
            self.on_settings()
    

    def onScNext(self):
        player = self.GetSqueezeServerPlayer()
        if player != None:
            #self.squeezecmd.squeezecmd_Index(player,1)
            
            self.squeezeConCtrl.Index(player,1)
        else:
            self.on_settings()
    def onScPrevious(self):
        player = self.GetSqueezeServerPlayer()
        if player != None:
            #self.squeezecmd.squeezecmd_Index(player,-1)
            self.squeezeConCtrl.Index(player,-1)
        else:
            self.on_settings()
    def onScRandom(self):
        player = self.GetSqueezeServerPlayer()
        if player != None:
            #self.squeezecmd.squeezecmd_randomplay(player)
            self.squeezeConCtrl.PlayRandomSong(player)
        else:
            self.on_settings()


    def on_settings(self):
        self.doCbSettings()
    def ChangePlayer(self,player):
        oldPlayer = self.player.get()
        if oldPlayer != player:
            self.player.set(player)
    def on_exit(self):
        self.doCbExit()