hamster.py 4.09 KB
Newer Older
1
import os
Dicson's avatar
Dicson committed
2 3
import dbus
import datetime as dt
André's avatar
André committed
4
import logging
5

6
from gi.repository import GObject
7

8 9 10
from gajim.common import app
from gajim.common import ged
from gajim.common import dbus_support
Dicson's avatar
Dicson committed
11

12
from gajim.plugins import GajimPlugin
13
from gajim.plugins.helpers import log_calls
14 15
from gajim.plugins.plugins_i18n import _
from gajim.common.const import ACTIVITIES
Dicson's avatar
Dicson committed
16

André's avatar
André committed
17 18
log = logging.getLogger('gajim.p.hamster')

19

Dicson's avatar
Dicson committed
20 21
HAMSTAER_INTERFACE = 'org.gnome.Hamster'
SUBACTIVITIES = []
22
subactivity_ = [list(ACTIVITIES[x].keys()) for x in list(ACTIVITIES.keys())]
Dicson's avatar
Dicson committed
23 24 25 26 27 28 29 30 31 32
for x in subactivity_ :
    SUBACTIVITIES = SUBACTIVITIES + x
SUBACTIVITIES = set(SUBACTIVITIES)
SUBACTIVITIES.remove('category')
XMPP_ACTIVITIES = set(ACTIVITIES.keys())

class HamsterIntegrationPlugin(GajimPlugin):

    @log_calls('HamsterIntegrationPlugin')
    def init(self):
33
        self.description = _('Integration with project hamster, see '
André's avatar
André committed
34
                             'http://projecthamster.org/')
Dicson's avatar
Dicson committed
35 36
        self.config_dialog = None
        self.events_handlers = {}
37 38 39
        if os.name == 'nt':
            self.available_text = _('Plugin can\'t be run under Windows.')
            self.activatable = False
Dicson's avatar
Dicson committed
40 41 42 43 44 45 46 47 48 49 50

    @log_calls('HamsterIntegrationPlugin')
    def activate(self):
        if not dbus_support.supported:
            return

        self.bus = dbus_support.session_bus.SessionBus()
        try:
            self.session_presence = self.bus.get_object(HAMSTAER_INTERFACE,
                '/org/gnome/Hamster')
        except:
André's avatar
André committed
51
            log.debug('Hamster D-Bus service not found')
Dicson's avatar
Dicson committed
52 53 54 55
            return

        self.bus.add_signal_receiver(self.hamster_facts_changed, 'FactsChanged',
            HAMSTAER_INTERFACE)
56
        app.ged.register_event_handler('signed-in', ged.POSTGUI,
Dicson's avatar
Dicson committed
57
            self.on_signed_in)
Dicson's avatar
Dicson committed
58 59 60 61 62 63

    @log_calls('HamsterIntegrationPlugin')
    def deactivate(self):
        if not dbus_support.supported or not self.active:
            return

Dicson's avatar
Dicson committed
64 65
        self.bus.remove_signal_receiver(self.hamster_facts_changed,
            "FactsChanged", dbus_interface=HAMSTAER_INTERFACE)
66
        app.ged.remove_event_handler('signed-in', ged.POSTGUI,
Dicson's avatar
Dicson committed
67
            self.on_signed_in)
Dicson's avatar
Dicson committed
68 69 70 71 72

    def hamster_facts_changed(self, *args, **kw):
        # get hamster tags
        facts = self.session_presence.GetTodaysFacts(
            dbus_interface=HAMSTAER_INTERFACE)
Dicson's avatar
Dicson committed
73 74
        if not facts:
            return
Dicson's avatar
Dicson committed
75
        if self.from_dbus_fact(facts[-1])['end_time']:
76
            accounts = list(app.connections.keys())
Dicson's avatar
Dicson committed
77
            for account in accounts:
78 79
                if app.account_is_connected(account):
                    connection = app.connections[account]
80
                    connection.get_module('UserActivity').send(None)
Dicson's avatar
Dicson committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
            return

        last_fact = self.from_dbus_fact(facts[-1])
        tags = set(last_fact['tags'])

        activity = "Other"
        activity_candidates = XMPP_ACTIVITIES.intersection(tags)
        if len(activity_candidates) >= 1:
            activity=list(activity_candidates)[0]
        subactivity = 'other'
        subactivity_candidates = SUBACTIVITIES.intersection(tags)
        if len(subactivity_candidates) >= 1:
            subactivity=list(subactivity_candidates)[0]

        # send activity
96 97 98
        for account in app.connections:
            if app.account_is_connected(account):
                connection = app.connections[account]
99 100
                connection.get_module('UserActivity').send(
                    (activity, subactivity, last_fact['fact']))
Dicson's avatar
Dicson committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115

    def from_dbus_fact(self, fact):
        '''unpack the struct into a proper dict'''
        return dict(fact = fact[4],
            start_time  = dt.datetime.utcfromtimestamp(fact[1]),
            end_time = dt.datetime.utcfromtimestamp(fact[2]) if fact[2] else None,
            description = fact[3],
            activity_id = fact[5],
            category = fact[6],
            tags = fact[7],
            date = dt.datetime.utcfromtimestamp(fact[8]).date(),
            delta = dt.timedelta(days = fact[9] // (24 * 60 * 60),
            seconds = fact[9] % (24 * 60 * 60)),
            id = fact[0])

Dicson's avatar
Dicson committed
116
    def on_signed_in(self, network_event):
117
        GObject.timeout_add(5000,self.hamster_facts_changed)