Adding voicemail support to the session and making it possible to save it
[gc-dialer] / src / backends / gv_backend.py
index e820ffb..a4fadde 100644 (file)
@@ -32,28 +32,31 @@ import logging
 
 from gvoice import gvoice
 
+from util import io as io_utils
+
 
 _moduleLogger = logging.getLogger(__name__)
 
 
 class GVDialer(object):
 
+       MESSAGE_TEXTS = "Text"
+       MESSAGE_VOICEMAILS = "Voicemail"
+       MESSAGE_ALL = "All"
+
        def __init__(self, cookieFile = None):
                self._gvoice = gvoice.GVoiceBackend(cookieFile)
+               self._texts = []
+               self._voicemails = []
 
        def is_quick_login_possible(self):
                """
-               @returns True then is_authed might be enough to login, else full login is required
+               @returns True then refresh_account_info might be enough to login, else full login is required
                """
                return self._gvoice.is_quick_login_possible()
 
-       def is_authed(self, force = False):
-               """
-               Attempts to detect a current session
-               @note Once logged in try not to reauth more than once a minute.
-               @returns If authenticated
-               """
-               return self._gvoice.is_authed(force)
+       def refresh_account_info(self):
+               return self._gvoice.refresh_account_info()
 
        def login(self, username, password):
                """
@@ -100,15 +103,14 @@ class GVDialer(object):
        def get_feed(self, feed):
                return self._gvoice.get_feed(feed)
 
-       def download(self, messageId, adir):
+       def download(self, messageId, targetPath):
                """
                Download a voicemail or recorded call MP3 matching the given ``msg``
                which can either be a ``Message`` instance, or a SHA1 identifier. 
-               Saves files to ``adir`` (defaults to current directory). 
                Message hashes can be found in ``self.voicemail().messages`` for example. 
                Returns location of saved file.
                """
-               return self._gvoice.download(messageId, adir)
+               self._gvoice.download(messageId, targetPath)
 
        def is_valid_syntax(self, number):
                """
@@ -148,18 +150,19 @@ class GVDialer(object):
                """
                return list(self._gvoice.get_recent())
 
-       def get_contacts(self):
-               """
-               @returns Fresh dictionary of items
-               """
-               return dict(self._gvoice.get_contacts())
+       def get_messages(self, messageType):
+               messages = list(self._get_messages(messageType))
+               messages.sort(key=lambda message: message["time"])
+               return messages
 
-       def get_messages(self):
-               return list(self._get_messages())
+       def _get_messages(self, messageType):
+               if messageType in [self.MESSAGE_VOICEMAILS, self.MESSAGE_ALL] or not self._voicemails:
+                       self._voicemails = list(self._gvoice.get_voicemails())
+               voicemails = self._voicemails
+               if messageType in [self.MESSAGE_TEXTS, self.MESSAGE_ALL] or not self._texts:
+                       self._texts = list(self._gvoice.get_texts())
+               smss = self._texts
 
-       def _get_messages(self):
-               voicemails = self._gvoice.get_voicemails()
-               smss = self._gvoice.get_texts()
                conversations = itertools.chain(voicemails, smss)
                for conversation in conversations:
                        messages = conversation.messages
@@ -213,7 +216,7 @@ class GVDialer(object):
                        "high": "<b>%s</b>",
                }
                return " ".join(
-                       messagePartFormat[text.accuracy] % text.text
+                       messagePartFormat[text.accuracy] % io_utils.escape(text.text)
                        for text in message.body
                )