wicd_support.py 2.85 KB
Newer Older
1
import os
2

3
from gajim.common import app
4 5
from gajim.common import dbus_support

6 7
from gajim.plugins import GajimPlugin
from gajim.plugins.helpers import log_calls
Philipp Hörist's avatar
Philipp Hörist committed
8
from gajim.plugins.plugins_i18n import _
9 10 11 12 13


class WicdPlugin(GajimPlugin):
    @log_calls('WicdPlugin')
    def init(self):
14 15 16
        self.description = _(
            'Support for autodetection of network status '
            'for Wicd Network Manager.\nRequires wicd and python-dbus.')
17 18 19 20 21
        self.config_dialog = None
        self.test_activatable()

    def test_activatable(self):
        self.available_text = ''
22 23 24 25
        if os.name == 'nt':
            self.available_text = _('Plugin can\'t be run under Windows.')
            self.activatable = False
            return
26 27 28
        if not dbus_support.supported:
            self.activatable = False
            self.available_text += _('python-dbus is missing! '
29
                                     'Install python-dbus.')
30 31 32 33 34

    @log_calls('WicdPlugin')
    def activate(self):
        try:
            import dbus
35
            from gajim.common.dbus_support import system_bus
36 37 38 39 40 41

            self.bus = system_bus.bus()

            if 'org.wicd.daemon' not in self.bus.list_names():
                return
            wicd_object = self.bus.get_object('org.wicd.daemon',
42
                                              '/org/wicd/daemon')
43
            self.props = dbus.Interface(wicd_object,
44 45 46 47 48 49
                                        'org.freedesktop.DBus.Properties')
            self.bus.add_signal_receiver(self.state_changed,
                                         'StatusChanged',
                                         'org.wicd.daemon',
                                         'org.wicd.daemon',
                                         '/org/wicd/daemon')
50 51 52 53 54
        except dbus.DBusException:
            pass

    @log_calls('WicdPlugin')
    def deactivate(self):
55 56 57 58 59
        self.bus.remove_signal_receiver(self.state_changed,
                                        'StatusChanged',
                                        'org.wicd.daemon',
                                        'org.wicd.daemon',
                                        '/org/wicd/daemon')
60

61
    def state_changed(self, state, _info):
62
        # Connection state constants
63 64 65 66 67 68
        # NOT_CONNECTED = 0
        # CONNECTING = 1
        # WIRELESS = 2
        # WIRED = 3
        # SUSPENDED = 4
        if state in (2, 3):
69 70
            for connection in app.connections.values():
                if app.config.get_per('accounts', connection.name,
71 72 73
                'listen_to_network_manager') and connection.time_to_reconnect:
                    connection._reconnect()
        else:
74 75
            for connection in app.connections.values():
                if app.config.get_per('accounts', connection.name,
76 77
                'listen_to_network_manager') and connection.connected > 1:
                    connection._disconnectedReconnCB()