Esempio n. 1
0
class Notifications:
    USER_ONLINE = 'user_online'
    NEW_GAME = 'new_game'
    GAME_FULL = 'game_full'

    def __init__(self, client, gameset, playerset, me):
        self.client = client
        self.me = me

        self.settings = NsSettingsDialog(self.client)
        self.dialog = NotificationDialog(self.client, self.settings)
        self.events = []
        self.disabledStartup = True
        self.game_running = False

        client.gameEnter.connect(self.gameEnter)
        client.gameExit.connect(self.gameExit)
        client.gameFull.connect(self._gamefull)
        gameset.newLobby.connect(self._newLobby)
        playerset.playerAdded.connect(self._newPlayer)

        self.user = util.THEME.icon("client/user.png", pix=True)

    def _newPlayer(self, player):
        if self.isDisabled() or not self.settings.popupEnabled(self.USER_ONLINE):
            return

        if self.me.player is not None and self.me.player == player:
            return

        notify_mode = self.settings.getCustomSetting(self.USER_ONLINE, 'mode')
        if notify_mode != 'all' and not self.me.isFriend(player.id):
            return

        self.events.append((self.USER_ONLINE, player.copy()))
        self.checkEvent()

    def _newLobby(self, game):
        if self.isDisabled() or not self.settings.popupEnabled(self.NEW_GAME):
            return

        host = game.host_player
        notify_mode = self.settings.getCustomSetting(self.NEW_GAME, 'mode')
        if notify_mode != 'all':
            if host is None or not self.me.isFriend(host):
                return

        self.events.append((self.NEW_GAME, game.copy()))
        self.checkEvent()

    def _gamefull(self):
        if self.isDisabled() or not self.settings.popupEnabled(self.GAME_FULL):
            return
        self.events.append((self.GAME_FULL, None))
        self.checkEvent()

    def gameEnter(self):
        self.game_running = True

    def gameExit(self):
        self.game_running = False
        # kick the queue
        if self.settings.ingame_notifications == IngameNotification.QUEUE:
            self.checkEvent()

    def isDisabled(self):
        return (
            self.disabledStartup
            or self.game_running and self.settings.ingame_notifications == IngameNotification.DISABLE
            or not self.settings.enabled
        )

    def setNotificationEnabled(self, enabled):
        self.settings.enabled = enabled
        self.settings.saveSettings()

    @QtCore.pyqtSlot()
    def on_showSettings(self):
        """ Shows a Settings Dialg with all registered notifications modules  """
        self.settings.show()

    def showEvent(self):
        """
        Display the next event in the queue as popup

        Pops event from queue and checks if it is showable as per settings
        If event is showable, process event data and then feed it into notification dialog

        Returns True if showable event found, False otherwise
        """

        event = self.events.pop(0)

        eventType = event[0]
        data = event[1]
        pixmap = None
        text = str(data)
        if eventType == self.USER_ONLINE:
            player = data
            pixmap = self.user
            text = '<html>%s<br><font color="silver" size="-2">is online</font></html>' % \
                   (player.login)
        elif eventType == self.NEW_GAME:
            game = data
            preview = maps.preview(game.mapname, pixmap=True)
            if preview:
                pixmap = preview.scaled(80, 80)

            # TODO: outsource as function?
            mod = game.featured_mod
            mods = game.sim_mods

            modstr = ''
            if mod != 'faf' or mods:
                modstr = mod
                if mods:
                    if mod == 'faf':
                        modstr = ", ".join(list(mods.values()))
                    else:
                        modstr = mod + " & " + ", ".join(list(mods.values()))
                    if len(modstr) > 20:
                        modstr = modstr[:15] + "..."

            modhtml = '' if (modstr == '') else '<br><font size="-4"><font color="red">mods</font> %s</font>' % modstr
            text = '<html>%s<br><font color="silver" size="-2">on</font> %s%s</html>' % \
                   (game.title, maps.getDisplayName(game.mapname), modhtml)
        elif eventType == self.GAME_FULL:
            pixmap = self.user
            text = '<html><br><font color="silver" size="-2">Game is full.</font></html>'
        self.dialog.newEvent(pixmap, text, self.settings.popup_lifetime, self.settings.soundEnabled(eventType))

    def checkEvent(self):
        """
        Checks that we are in correct state to show next notification popup

        This means:
            * There need to be events pending
            * There must be no notification showing right now (i.e. notification dialog hidden)
            * Game isn't running, or ingame notifications are enabled

        """
        if (len(self.events) > 0 and self.dialog.isHidden() and
                (not self.game_running or self.settings.ingame_notifications == IngameNotification.ENABLE)):
            self.showEvent()
Esempio n. 2
0
class Notifications:
    USER_ONLINE = 'user_online'
    NEW_GAME = 'new_game'

    def __init__(self, client):
        self.client = client

        self.dialog = NotificationDialog(self.client)
        self.events = []
        self.disabledStartup = True
        self.lock = Lock()

        self.settings = NsSettingsDialog(self.client)

        self.user = util.icon("client/user.png", pix=True)

    def isDisabled(self):
        return self.disabledStartup or not self.settings.enabled

    def setNotificationEnabled(self, enabled):
        self.settings.enabled = enabled
        self.settings.saveSettings()

    @QtCore.pyqtSlot()
    def on_event(self, eventType, data):
        """
        Puts an event in a queue, can trigger a popup.
        Keyword arguments:
        eventType -- Type of the event
        data -- Custom data that is used by the system to show a detailed popup
        """
        if self.isDisabled() or not self.settings.popupEnabled(eventType):
            return
        self.events.append((eventType, data))
        if self.dialog.isHidden():
            self.showEvent()

    @QtCore.pyqtSlot()
    def on_showSettings(self):
        """ Shows a Settings Dialg with all registered notifications modules  """
        self.settings.show()

    def showEvent(self):
        """ Display the next event in the queue as popup  """
        self.lock.acquire()
        event = self.events[0]
        del self.events[0]
        self.lock.release()

        eventType = event[0]
        data = event[1]
        pixmap = None
        text = str(data)
        if eventType == self.USER_ONLINE:
            username = data['user']
            if self.settings.getCustomSetting(
                    eventType, 'mode'
            ) == 'friends' and not self.client.players.isFriend(username):
                self.dialogClosed()
                return
            pixmap = self.user
            text = '<html>%s<br><font color="silver" size="-2">joined</font> %s</html>' % (
                username, data['channel'])
        elif eventType == self.NEW_GAME:
            if self.settings.getCustomSetting(
                    eventType, 'mode') == 'friends' and (
                        'host' not in data
                        or not self.client.players.isFriend(data['host'])):
                self.dialogClosed()
                return

            preview = maps.preview(data['mapname'], pixmap=True)
            if preview:
                pixmap = preview.scaled(80, 80)

            #TODO: outsource as function?
            mod = None if 'featured_mod' not in data else data['featured_mod']
            mods = None if 'sim_mods' not in data else data['sim_mods']

            modstr = ''
            if (mod != 'faf' or mods):
                modstr = mod
                if mods:
                    if mod == 'faf': modstr = ", ".join(mods.values())
                    else: modstr = mod + " & " + ", ".join(mods.values())
                    if len(modstr) > 20: modstr = modstr[:15] + "..."

            modhtml = '' if (
                modstr == ''
            ) else '<br><font size="-4"><font color="red">mods</font> %s</font>' % modstr
            text = '<html>%s<br><font color="silver" size="-2">on</font> %s%s</html>' % (
                data['title'], maps.getDisplayName(data['mapname']), modhtml)

        self.dialog.newEvent(pixmap, text, self.settings.popup_lifetime,
                             self.settings.soundEnabled(eventType))

    def dialogClosed(self):
        if self.events:
            self.showEvent()
Esempio n. 3
0
class Notifications:
    USER_ONLINE = 'user_online'
    NEW_GAME = 'new_game'

    def __init__(self, client):
        self.client = client

        self.dialog = NotificationDialog(self.client)
        self.events = []
        self.disabledStartup = True
        self.lock = Lock()

        self.settings = NsSettingsDialog(self.client)

        self.user = util.icon("client/user.png", pix=True)

    def isDisabled(self):
        return self.disabledStartup or not self.settings.enabled

    def setNotificationEnabled(self, enabled):
        self.settings.enabled = enabled
        self.settings.saveSettings()

    @QtCore.pyqtSlot()
    def on_event(self, eventType, data):
        """
        Puts an event in a queue, can trigger a popup.
        Keyword arguments:
        eventType -- Type of the event
        data -- Custom data that is used by the system to show a detailed popup
        """
        if self.isDisabled() or not self.settings.popupEnabled(eventType):
            return
        self.events.append((eventType, data))
        if self.dialog.isHidden():
            self.showEvent()

    @QtCore.pyqtSlot()
    def on_showSettings(self):
        """ Shows a Settings Dialg with all registered notifications modules  """
        self.settings.show()

    def showEvent(self):
        """ Display the next event in the queue as popup  """
        self.lock.acquire()
        event = self.events[0]
        del self.events[0]
        self.lock.release()

        eventType = event[0]
        data = event[1]
        pixmap = None
        text = str(data)
        if eventType == self.USER_ONLINE:
            userid = data['user']
            if self.settings.getCustomSetting(eventType, 'mode') == 'friends' and not self.client.players.isFriend(userid):
                self.dialogClosed()
                return
            pixmap = self.user
            text = '<html>%s<br><font color="silver" size="-2">joined</font> %s</html>' % (self.client.players[userid].login, data['channel'])
        elif eventType == self.NEW_GAME:
            if self.settings.getCustomSetting(eventType, 'mode') == 'friends' and ('host' not in data or not self.client.players.isFriend(data['host'])):
                self.dialogClosed()
                return

            preview = maps.preview(data['mapname'], pixmap=True)
            if preview:
                pixmap = preview.scaled(80, 80)

            #TODO: outsource as function?
            mod = None if 'featured_mod' not in data else data['featured_mod']
            mods = None if 'sim_mods' not in data else data['sim_mods']

            modstr = ''
            if (mod != 'faf' or mods):
                modstr = mod
                if mods:
                    if mod == 'faf':modstr = ", ".join(mods.values())
                    else: modstr = mod + " & " + ", ".join(mods.values())
                    if len(modstr) > 20: modstr = modstr[:15] + "..."

            modhtml = '' if (modstr == '') else '<br><font size="-4"><font color="red">mods</font> %s</font>' % modstr
            text = '<html>%s<br><font color="silver" size="-2">on</font> %s%s</html>' % (data['title'], maps.getDisplayName(data['mapname']), modhtml)

        self.dialog.newEvent(pixmap, text, self.settings.popup_lifetime, self.settings.soundEnabled(eventType))

    def dialogClosed(self):
        if self.events:
            self.showEvent()