changeset 420:acd908528ef7

core: profile creation/deletion through database
author Goffi <goffi@goffi.org>
date Wed, 02 Nov 2011 22:49:23 +0100
parents 6c167a2e04b8
children 28e4299d4553
files src/core/sat_main.py src/tools/memory.py src/tools/sqlite.py
diffstat 3 files changed, 60 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/src/core/sat_main.py	Wed Nov 02 22:47:59 2011 +0100
+++ b/src/core/sat_main.py	Wed Nov 02 22:49:23 2011 +0100
@@ -117,6 +117,7 @@
         self.bridge.register("getProfileName", self.memory.getProfileName)
         self.bridge.register("getProfilesList", self.memory.getProfilesList)
         self.bridge.register("createProfile", self.memory.createProfile)
+        self.bridge.register("asyncCreateProfile", self.memory.asyncCreateProfile)
         self.bridge.register("deleteProfile", self.memory.deleteProfile)
         self.bridge.register("registerNewAccount", self.registerNewAccount)
         self.bridge.register("connect", self.connect)
--- 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"""
--- a/src/tools/sqlite.py	Wed Nov 02 22:47:59 2011 +0100
+++ b/src/tools/sqlite.py	Wed Nov 02 22:49:23 2011 +0100
@@ -89,8 +89,20 @@
         d = self.dbpool.runQuery("INSERT INTO profiles(name) VALUES (?)", (name,))
         d.addCallback(getProfileId)
         d.addCallback(profile_created)
-        d.addErrback(lambda ignore: error(_("Can't create profile %(name)s" % {"name":name})))
         return d
+    
+    def deleteProfile(self, name):
+        """Delete profile
+        @param name: name of the profile
+        @return: deferred triggered once profile is actually deleted"""
+        def deletionError(failure):
+            error(_("Can't delete profile [%s]") % name)
+            return failure
+        del self.profiles[name]
+        d = self.dbpool.runQuery("DELETE FROM profiles WHERE name = ?", (name,))
+        d.addCallback(lambda ignore: info(_("Profile [%s] deleted") % name))
+        return d
+
 
     #Params
     def loadGenParams(self, params_gen):