X-Git-Url: http://git.maemo.org/git/?a=blobdiff_plain;f=src%2Fgvoice%2Fconversations.py;h=7fbe1ca637146aed4ddd3216d825b76761b00463;hb=dc9f6a05edd1d1d94f3687f4344ae45c9101b1b3;hp=6e0811205f153bcfd7adcc80c1296149c7660d56;hpb=54fb3d0f7f39e6446e1771cc29483a4d6138f8dd;p=theonering diff --git a/src/gvoice/conversations.py b/src/gvoice/conversations.py index 6e08112..7fbe1ca 100644 --- a/src/gvoice/conversations.py +++ b/src/gvoice/conversations.py @@ -1,12 +1,16 @@ #!/usr/bin/python -# @bug False positives on startup. Luckily the object path for the channel is -# unique, so can use that to cache some of the data out to file - from __future__ import with_statement import logging +try: + import cPickle + pickle = cPickle +except ImportError: + import pickle + +import constants import util.coroutines as coroutines import util.misc as util_misc @@ -22,6 +26,34 @@ class Conversations(object): self.updateSignalHandler = coroutines.CoTee() + @property + def _name(self): + return repr(self._get_raw_conversations.__name__) + + def load(self, path): + assert not self._conversations + try: + with open(path, "rb") as f: + fileVersion, fileBuild, convs = pickle.load(f) + except (pickle.PickleError, IOError): + _moduleLogger.exception("While loading for %s" % self._name) + return + + if fileVersion == constants.__version__ and fileBuild == constants.__build__: + self._conversations = convs + else: + _moduleLogger.debug( + "%s Skipping cache due to version mismatch (%s-%s)" % (self._name, fileVersion, fileBuild) + ) + + def save(self, path): + try: + dataToDump = (constants.__version__, constants.__build__, self._conversations) + with open(path, "wb") as f: + pickle.dump(dataToDump, f, pickle.HIGHEST_PROTOCOL) + except (pickle.PickleError, IOError): + _moduleLogger.exception("While saving for %s" % self._name) + def update(self, force=False): if not force and self._conversations: return @@ -32,7 +64,7 @@ class Conversations(object): conversations = list(self._get_raw_conversations()) conversations.sort() for conversation in conversations: - key = conversation.contactId, util_misc.normalize_number(conversation.number) + key = util_misc.normalize_number(conversation.number) try: mergedConversations = self._conversations[key] except KeyError: @@ -44,7 +76,7 @@ class Conversations(object): isConversationUpdated = True except RuntimeError, e: if False: - _moduleLogger.info("Skipping conversation for %r because '%s'" % (key, e)) + _moduleLogger.debug("%s Skipping conversation for %r because '%s'" % (self._name, key, e)) isConversationUpdated = False if isConversationUpdated: @@ -64,7 +96,7 @@ class Conversations(object): try: del self._conversations[key] except KeyError: - _moduleLogger.info("Conversation never existed for %r" % (key,)) + _moduleLogger.info("%s Conversation never existed for %r" % (self._name, key, )) def clear_all(self): self._conversations.clear() @@ -77,11 +109,28 @@ class MergedConversations(object): def append_conversation(self, newConversation): self._validate(newConversation) + similarExist = False for similarConversation in self._find_related_conversation(newConversation.id): self._update_previous_related_conversation(similarConversation, newConversation) self._remove_repeats(similarConversation, newConversation) + similarExist = True + if similarExist: + # Hack to reduce a race window with GV marking messages as read + # because it thinks we replied when really we replied to the + # previous message. Clients of this code are expected to handle + # this gracefully. Other race conditions may exist but clients are + # responsible for them + if newConversation.messages: + newConversation.isRead = False + else: + newConversation.isRead = True self._conversations.append(newConversation) + def to_dict(self): + selfDict = {} + selfDict["conversations"] = [conv.to_dict() for conv in self._conversations] + return selfDict + @property def conversations(self): return self._conversations @@ -90,7 +139,7 @@ class MergedConversations(object): if not self._conversations: return - for constantField in ("contactId", "number"): + for constantField in ("number", ): assert getattr(self._conversations[0], constantField) == getattr(newConversation, constantField), "Constant field changed, soemthing is seriously messed up: %r v %r" % ( getattr(self._conversations[0], constantField), getattr(newConversation, constantField), @@ -108,7 +157,7 @@ class MergedConversations(object): return similarConversations def _update_previous_related_conversation(self, relatedConversation, newConversation): - for commonField in ("isRead", "isSpam", "isTrash", "isArchived"): + for commonField in ("isSpam", "isTrash", "isArchived"): newValue = getattr(newConversation, commonField) setattr(relatedConversation, commonField, newValue)