server_status_icons.py 4.93 KB
Newer Older
1 2
import os

3 4 5 6 7 8
from gajim.plugins import GajimPlugin
from gajim.plugins.helpers import log_calls
from gajim import gtkgui_helpers
from gajim.common import app
from gajim.common import helpers
from gajim.common import ged
9

10 11 12 13 14
# Since Gajim 1.1.0 _() has to be imported
try:
    from gajim.common.i18n import _
except ImportError:
    pass
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

class ServerStatusIconsPlugin(GajimPlugin):

    @log_calls('ServerStatusIconsPlugin')
    def init(self):
        self.description = _('Replace standard Gajim status icons with server'
            ' specific for known XMPP server accounts (vk.com, ...)')
        self.pos_list = [_('after statusicon'), _('before avatar')]
        self.gui_extension_points = {
            'roster_draw_contact': (self.connect_with_roster_draw_contact,
                                    self.disconnect_from_roster_draw_contact)}
        self.known_servers = {'chat.facebook.com': 'facebook',
                              'gmail.com': 'gtalk',
                              'livejournal.com': 'livejournal',
                              'odnoklassniki.ru': 'odnoklassniki',
                              'vk.com': 'vkontakte',
                              'ya.ru': 'yaonline'}
32
        self.config_dialog = None
33 34 35 36 37

    @log_calls('ServerStatusIconsPlugin')
    def connect_with_roster_draw_contact(self, roster, jid, account, contact):
        if not self.active:
            return
38
        if app.jid_is_transport(jid):
39 40 41 42 43 44 45 46
            return

        child_iters = roster._get_contact_iter(jid, account, contact,
            roster.model)
        if not child_iters:
            return

        icon_name = helpers.get_icon_name_to_show(contact, account)
47
        if app.events.get_events(account, jid) or icon_name == 'requested':
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
            return

        host = jid.split('@')[1]
        server = self.known_servers.get(host, False)
        if not server:
            return

        if server not in roster.transports_state_images['16']:
            # we don't have iconset for this transport loaded yet. Let's do it
            self.make_transport_state_images(roster, server)
        if server in roster.transports_state_images['16'] and \
            icon_name in roster.transports_state_images['16'][server]:
            state_images = roster.transports_state_images['16'][server]
            img = state_images[icon_name]
            for child_iter in child_iters:
                roster.model[child_iter][0] = img

    def make_transport_state_images(self, roster, transport):
        """
        Initialize opened and closed 'transport' iconset dict
        """

        folder = os.path.join(self.local_file_path(transport), '16x16')
        pixo, pixc = gtkgui_helpers.load_icons_meta()
        roster.transports_state_images['opened'][transport] = \
            gtkgui_helpers.load_iconset(folder, pixo, transport=True)
        roster.transports_state_images['closed'][transport] = \
            gtkgui_helpers.load_iconset(folder, pixc, transport=True)
        roster.transports_state_images['16'][transport] = \
            gtkgui_helpers.load_iconset(folder, transport=True)
        folder = os.path.join(self.local_file_path(transport), '32x32')
        roster.transports_state_images['32'][transport] = \
            gtkgui_helpers.load_iconset(folder, transport=True)

    def _nec_our_show(self, obj):
        account = obj.conn.name
84 85
        roster = app.interface.roster
        status = app.connections[account].connected
86

87
        if account not in app.contacts.get_accounts():
88 89 90 91 92
            return
        child_iterA = roster._get_account_iter(account, roster.model)
        if not child_iterA:
            return

93
        hostname = app.config.get_per('accounts', account, 'hostname')
94 95 96 97 98
        server = self.known_servers.get(hostname, False)
        if not server:
            return

        if not roster.regroup:
99
            show = app.SHOW_LIST[status]
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
        else: # accounts merged
            show = helpers.get_global_show()

        if server not in roster.transports_state_images['16']:
            # we don't have iconset for this transport loaded yet. Let's do it
            self.make_transport_state_images(roster, server)
        if server in roster.transports_state_images['16'] and \
            show in roster.transports_state_images['16'][server]:
            roster.model[child_iterA][0] = roster.transports_state_images[
                '16'][server][show]

    @log_calls('ServerStatusIconsPlugin')
    def disconnect_from_roster_draw_contact(self, roster, jid, account,
        contact):
        pass

    @log_calls('ServerStatusIconsPlugin')
    def activate(self):
        self.active = True
119 120
        app.interface.roster.setup_and_draw_roster()
        app.ged.register_event_handler('our-show', ged.GUI2,
121 122 123 124 125
            self._nec_our_show)

    @log_calls('ServerStatusIconsPlugin')
    def deactivate(self):
        self.active = None
126
        app.ged.remove_event_handler('our-show', ged.GUI2,
127
            self._nec_our_show)
128
        app.interface.roster.setup_and_draw_roster()