Auto-enabled system contacts for all contacts
[theonering] / src / autogv.py
index 1955e8b..9a0f61e 100644 (file)
@@ -1,6 +1,6 @@
 import logging
 
-import gobject
+import dbus
 import telepathy
 
 try:
@@ -20,9 +20,10 @@ import util.coroutines as coroutines
 import util.go_utils as gobject_utils
 import util.tp_utils as telepathy_utils
 import util.misc as misc_utils
+import gvoice
 
 
-_moduleLogger = logging.getLogger("autogv")
+_moduleLogger = logging.getLogger(__name__)
 
 
 class NewGVConversations(object):
@@ -69,12 +70,11 @@ class NewGVConversations(object):
                        # Maemo 4.1's RTComm opens a window for a chat regardless if a
                        # message is received or not, so we need to do some filtering here
                        mergedConv = conv.get_conversation(phoneNumber)
-                       unreadConvs = [
-                               conversation
-                               for conversation in mergedConv.conversations
-                               if not conversation.isRead and not conversation.isArchived
-                       ]
-                       if not unreadConvs:
+                       newConversations = mergedConv.conversations
+                       newConversations = gvoice.conversations.filter_out_read(newConversations)
+                       newConversations = gvoice.conversations.filter_out_self(newConversations)
+                       newConversations = list(newConversations)
+                       if not newConversations:
                                continue
 
                        chan = self._connRef()._channel_manager.channel_for_props(props, signal=True)
@@ -119,7 +119,11 @@ class RefreshVoicemail(object):
                        return
 
                conn = telepathy.client.Connection(serviceName, connObjectPath)
-               chan = telepathy.client.Channel(serviceName, channelObjectPath)
+               try:
+                       chan = telepathy.client.Channel(serviceName, channelObjectPath)
+               except dbus.exceptions.UnknownMethodException:
+                       _moduleLogger.exception("Client might not have implemented a deprecated method")
+                       return
                missDetection = telepathy_utils.WasMissedCall(
                        bus, conn, chan, self._on_missed_call, self._on_error_for_missed
                )
@@ -137,6 +141,24 @@ class RefreshVoicemail(object):
                self._outstandingRequests.remove(missDetection)
 
 
+class TimedDisconnect(object):
+
+       def __init__(self, connRef):
+               self._connRef = connRef
+               self.__delayedDisconnect = gobject_utils.Timeout(self._on_delayed_disconnect)
+
+       def start(self):
+               self.__delayedDisconnect.start(seconds=20)
+
+       def stop(self):
+               self.__delayedDisconnect.cancel()
+
+       @misc_utils.log_exception(_moduleLogger)
+       def _on_delayed_disconnect(self):
+               _moduleLogger.info("Timed disconnect occurred")
+               self._connRef().disconnect(telepathy.CONNECTION_STATUS_REASON_NETWORK_ERROR)
+
+
 class AutoDisconnect(object):
 
        def __init__(self, connRef):
@@ -175,13 +197,14 @@ class AutoDisconnect(object):
                else:
                        _moduleLogger.info("Other status: %r" % (status, ))
 
+       @misc_utils.log_exception(_moduleLogger)
        def _cancel_delayed_disconnect(self):
                _moduleLogger.info("Cancelling auto-log off")
                self.__delayedDisconnect.cancel()
 
        @misc_utils.log_exception(_moduleLogger)
        def _on_delayed_disconnect(self):
-               if not self.session.is_logged_in():
+               if not self._connRef().session.is_logged_in():
                        _moduleLogger.info("Received connection change event when not logged in")
                        return
                try:
@@ -228,6 +251,25 @@ class DisconnectOnShutdown(object):
                @note Hildon specific
                """
                try:
-                       self._connRef().disconnect(telepathy.CONNECTION_STATUS_REASON_REQUEST)
+                       self._connRef().disconnect(telepathy.CONNECTION_STATUS_REASON_REQUESTED)
                except Exception:
                        _moduleLogger.exception("Error durring disconnect")
+
+
+class DelayEnableContactIntegration(object):
+
+       def __init__(self, protocolName):
+               self.__enableSystemContactSupport = telepathy_utils.EnableSystemContactIntegration(
+                       protocolName
+               )
+               self.__delayedEnable = gobject_utils.Async(self._on_delayed_enable)
+
+       def start(self):
+               self.__delayedEnable.start()
+
+       def stop(self):
+               self.__delayedEnable.cancel()
+
+       @misc_utils.log_exception(_moduleLogger)
+       def _on_delayed_enable(self):
+               self.__enableSystemContactSupport.start()