diff src/tools/memory.py @ 420:acd908528ef7

core: profile creation/deletion through database
author Goffi <goffi@goffi.org>
date Wed, 02 Nov 2011 22:49:23 +0100
parents 199cf4ebcc74
children 6c20c76abdcc
line wrap: on
line diff
--- a/src/tools/memory.py	Wed Nov 02 22:47:59 2011 +0100
+++ b/src/tools/memory.py	Wed Nov 02 22:49:23 2011 +0100
@@ -41,6 +41,9 @@
 class ProfileNotInCacheError(Exception):
     pass
 
+class ConnectedProfileError(Exception):
+    pass
+
 class Param():
     """This class manage parameters with xml"""
     ### TODO: add desciption in params
@@ -114,24 +117,50 @@
         host.set_const('savefile_param_xml', SAVEFILE_PARAM_XML)
         host.registerGeneralCB("registerNewAccount", host.registerNewAccountCB)
 
-    def createProfile(self, name):
+    def createProfile(self, profile):
         """Create a new profile
-        @param name: Name of the profile"""
-        if self.storage.hasProfile(name):
-            info (_('The profile name already exists'))
+        @param profile: profile of the profile"""
+        #FIXME: must be asynchronous and call the callback once the profile actually exists
+        if self.storage.hasProfile(profile):
+            info (_('The profile profile already exists'))
             return True
-        if not self.host.trigger.point("ProfileCreation", name):
+        if not self.host.trigger.point("ProfileCreation", profile):
             return False
-        self.params[name]={}
+        self.storage.createProfile(profile)
         return False
 
-    def deleteProfile(self, name):
+    def asyncCreateProfile(self, profile, callback, errback):
+        """Create a new profile
+        @param profile: name of the profile
+        @param callback: called when the profile actually exists in database and memory
+        @param errback: called with a string constant as parameter:
+                        - CONFLICT: the profile already exists
+                        - CANCELED: profile creation canceled
+                        - DATABASE: profile creation in database failed"""
+        #FIXME: must be asynchronous and call the callback once the profile actually exists
+        if self.storage.hasProfile(profile):
+            info (_('The profile name already exists'))
+            errback("CONFLICT")
+            return
+        if not self.host.trigger.point("ProfileCreation", profile):
+            errback("CANCELED")
+            return
+        d = self.storage.createProfile(profile)
+        d.addCallback(lambda ignore: callback())
+        d.addErrback(lambda ignore: errback("DATABASE"))
+
+
+    def deleteProfile(self, profile):
         """Delete an existing profile
-        @param name: Name of the profile"""
-        if not self.storage.hasProfile(name):
-            error (_('Trying to delete an unknown profile'))
+        @param profile: name of the profile"""
+        #TODO: async equivalent, like for createProfile
+        if not self.storage.hasProfile(profile):
+            error(_('Trying to delete an unknown profile'))
             return True
-        del self.params[name]
+        if self.host.isConnected(profile):
+            error(_("Trying to delete a connected profile"))
+            raise ConnectedProfileError
+        self.storage.deleteProfile(profile)
         return False
 
     def getProfileName(self, profile_key):
@@ -596,6 +625,12 @@
         """
         return self.params.createProfile(name)
     
+    def asyncCreateProfile(self, name, callback, errback):
+        """Create a new profile
+        @param name: Profile name
+        """
+        return self.params.asyncCreateProfile(name, callback, errback)
+    
     def deleteProfile(self, name):
         """Delete an existing profile
         @param name: Name of the profile"""