1"""
2    Slixmpp: The Slick XMPP Library
3    Copyright (C) 2013 Nathanael C. Fritz, Lance J.T. Stout
4    This file is part of slixmpp.
5
6    See the file LICENSE for copying permission.
7"""
8
9from slixmpp.stanza import Iq
10from slixmpp.xmlstream.handler import Callback
11from slixmpp.xmlstream.matcher import StanzaPath
12from slixmpp.xmlstream import register_stanza_plugin
13from slixmpp.plugins import BasePlugin
14from slixmpp.plugins.google.settings import stanza
15
16
17class GoogleSettings(BasePlugin):
18
19    """
20    Google: Gmail Notifications
21
22    Also see <https://developers.google.com/talk/jep_extensions/usersettings>.
23    """
24
25    name = 'google_settings'
26    description = 'Google: User Settings'
27    dependencies = set()
28    stanza = stanza
29
30    def plugin_init(self):
31        register_stanza_plugin(Iq, stanza.UserSettings)
32
33        self.xmpp.register_handler(
34                Callback('Google Settings',
35                    StanzaPath('iq@type=set/google_settings'),
36                    self._handle_settings_change))
37
38    def plugin_end(self):
39        self.xmpp.remove_handler('Google Settings')
40
41    def get(self, timeout=None, callback=None):
42        iq = self.xmpp.Iq()
43        iq['type'] = 'get'
44        iq.enable('google_settings')
45        return iq.send(timeout=timeout, callback=callback)
46
47    def update(self, settings, timeout=None, callback=None):
48        iq = self.xmpp.Iq()
49        iq['type'] = 'set'
50        iq.enable('google_settings')
51
52        for setting, value in settings.items():
53            iq['google_settings'][setting] = value
54
55        return iq.send(timeout=timeout, callback=callback)
56
57    def _handle_settings_change(self, iq):
58        reply = self.xmpp.Iq()
59        reply['type'] = 'result'
60        reply['id'] = iq['id']
61        reply['to'] = iq['from']
62        reply.send()
63        self.xmpp.event('google_settings_change', iq)
64