diff src/tools/memory.py @ 425:e4e9187e3b5b

backend, bridge: asynchronous history quick_frontend: use of asynchronous history
author Goffi <goffi@goffi.org>
date Tue, 08 Nov 2011 01:08:11 +0100
parents 6c20c76abdcc
children a4a9efadabfc
line wrap: on
line diff
--- a/src/tools/memory.py	Mon Nov 07 22:27:07 2011 +0100
+++ b/src/tools/memory.py	Tue Nov 08 01:08:11 2011 +0100
@@ -34,7 +34,6 @@
 from sat.tools.sqlite import SqliteStorage
 
 SAVEFILE_PARAM_XML="/param" #xml parameters template
-SAVEFILE_HISTORY="/history"
 SAVEFILE_PRIVATE="/private"  #file used to store misc values (mainly for plugins)
 SAVEFILE_DATABASE="/sat.db"
 
@@ -501,12 +500,10 @@
         self.presenceStatus={}
         self.lastResource={} #tmp, will be refactored with bdd integration
         self.subscriptions={}
-        self.history={}  #used to store chat history (key: short jid)
         self.private={}  #used to store private value
         self.server_features={} #used to store discovery's informations
         self.server_identities={}
         self.config = self.parseMainConf()
-        host.set_const('savefile_history', SAVEFILE_HISTORY)
         host.set_const('savefile_private', SAVEFILE_PRIVATE)
         host.set_const('savefile_database', SAVEFILE_DATABASE)
         database_file = os.path.expanduser(self.getConfig('','local_dir')+
@@ -547,8 +544,6 @@
         """Load parameters and all memory things from file/db"""
         param_file_xml = os.path.expanduser(self.getConfig('','local_dir')+
                                         self.host.get_const('savefile_param_xml'))
-        history_file = os.path.expanduser(self.getConfig('','local_dir')+
-                                        self.host.get_const('savefile_history'))
         private_file = os.path.expanduser(self.getConfig('','local_dir')+
                                         self.host.get_const('savefile_private'))
 
@@ -564,16 +559,6 @@
             info (_("No params template, using default template"))
             self.params.load_default_params()
 
-        
-        #history
-        if os.path.exists(history_file):
-            try:
-                with open(history_file, 'r') as history_pickle:
-                    self.history=pickle.load(history_pickle)
-                debug(_("history loaded"))
-            except:
-                error (_("Can't load history !"))
-
         #private
         if os.path.exists(private_file):
             try:
@@ -604,16 +589,11 @@
         #TODO: need to encrypt files (at least passwords !) and set permissions
         param_file_xml = os.path.expanduser(self.getConfig('','local_dir')+
                                         self.host.get_const('savefile_param_xml'))
-        history_file = os.path.expanduser(self.getConfig('','local_dir')+
-                                        self.host.get_const('savefile_history'))
         private_file = os.path.expanduser(self.getConfig('','local_dir')+
                                         self.host.get_const('savefile_private'))
         
         self.params.save_xml(param_file_xml)
         debug(_("params saved"))
-        with open(history_file, 'w') as history_pickle:
-            pickle.dump(self.history, history_pickle)
-        debug(_("history saved"))
         with open(private_file, 'w') as private_pickle:
             pickle.dump(self.private, private_pickle)
         debug(_("private values saved"))
@@ -645,39 +625,12 @@
         @param name: Name of the profile"""
         return self.params.deleteProfile(name)
 
-    def addToHistory(self, me_jid, from_jid, to_jid, type, message):
-        me_short=me_jid.userhost()
-        from_short=from_jid.userhost()
-        to_short=to_jid.userhost()
-
-        if from_jid==me_jid:
-            key=to_short
-        else:
-            key=from_short
-
-        if not self.history.has_key(me_short):
-            self.history[me_short]={}
-        if not self.history[me_short].has_key(key):
-            self.history[me_short][key]={}
+    def addToHistory(self, from_jid, to_jid, message, timestamp=None, profile="@NONE@"):
+        assert(profile!="@NONE@")
+        return self.storage.addToHistory(from_jid, to_jid, message, timestamp, profile)
 
-        self.history[me_short][key][int(time.time())] = (from_jid.full(), message)
-        
-    def getHistory(self, from_jid, to_jid, size):
-        ret={}
-        if not self.history.has_key(from_jid):
-            error(_("source JID not found !"))
-            #TODO: throw an error here
-            return {}
-        if not self.history[from_jid].has_key(to_jid):
-            error(_("dest JID not found !"))
-            #TODO: throw an error here
-            return {}
-        stamps=self.history[from_jid][to_jid].keys()
-        stamps.sort()
-        for stamp in stamps[-size:]:
-            ret[stamp]=self.history[from_jid][to_jid][stamp]
-
-        return ret
+    def getHistory(self, from_jid, to_jid, limit=0, between=True):
+        return self.storage.getHistory(jid.JID(from_jid), jid.JID(to_jid), limit, between)
 
     def setPrivate(self, key, value):
         """Save a misc private value (mainly useful for plugins)"""