changeset 893:308a96bc7c1b

core, frontends: add method asyncDeleteProfile, remove synchronous methods createProfile and deleteProfile
author souliane <souliane@mailoo.org>
date Mon, 03 Mar 2014 09:59:10 +0100
parents 58107179cd97
children 57c32d8ec847
files frontends/src/bridge/DBus.py frontends/src/jp/cmd_profile.py frontends/src/primitivus/profile_manager.py frontends/src/wix/profile_manager.py src/bridge/DBus.py src/bridge/bridge_constructor/bridge_template.ini src/core/exceptions.py src/core/sat_main.py src/memory/memory.py
diffstat 9 files changed, 47 insertions(+), 82 deletions(-) [+]
line wrap: on
line diff
--- a/frontends/src/bridge/DBus.py	Fri Feb 28 11:19:08 2014 +0100
+++ b/frontends/src/bridge/DBus.py	Mon Mar 03 09:59:10 2014 +0100
@@ -104,6 +104,9 @@
     def asyncCreateProfile(self, profile, callback=None, errback=None):
         return self.db_core_iface.asyncCreateProfile(profile, reply_handler=callback, error_handler=lambda err:errback(err._dbus_error_name[len(const_ERROR_PREFIX)+1:]))
 
+    def asyncDeleteProfile(self, profile, callback=None, errback=None):
+        return self.db_core_iface.asyncDeleteProfile(profile, reply_handler=callback, error_handler=lambda err:errback(err._dbus_error_name[len(const_ERROR_PREFIX)+1:]))
+
     def asyncGetParamA(self, name, category, attribute="value", security_limit=-1, profile_key="@DEFAULT@", callback=None, errback=None):
         return unicode(self.db_core_iface.asyncGetParamA(name, category, attribute, security_limit, profile_key, reply_handler=callback, error_handler=lambda err:errback(err._dbus_error_name[len(const_ERROR_PREFIX)+1:])))
 
@@ -113,15 +116,9 @@
     def connect(self, profile_key="@DEFAULT@"):
         return self.db_core_iface.connect(profile_key)
 
-    def createProfile(self, profile):
-        return self.db_core_iface.createProfile(profile)
-
     def delContact(self, entity_jid, profile_key="@DEFAULT@"):
         return self.db_core_iface.delContact(entity_jid, profile_key)
 
-    def deleteProfile(self, profile):
-        return self.db_core_iface.deleteProfile(profile)
-
     def disconnect(self, profile_key="@DEFAULT@"):
         return self.db_core_iface.disconnect(profile_key)
 
--- a/frontends/src/jp/cmd_profile.py	Fri Feb 28 11:19:08 2014 +0100
+++ b/frontends/src/jp/cmd_profile.py	Mon Mar 03 09:59:10 2014 +0100
@@ -42,7 +42,7 @@
         if self.args.profile not in self.host.bridge.getProfilesList():
             error("Profile %s doesn't exist." % self.args.profile)
             self.host.quit(1)
-        self.host.bridge.deleteProfile(self.args.profile)
+        self.host.bridge.asyncDeleteProfile(self.args.profile, callback=lambda dummy: None)
 
 
 class ProfileInfo(base.CommandBase):
--- a/frontends/src/primitivus/profile_manager.py	Fri Feb 28 11:19:08 2014 +0100
+++ b/frontends/src/primitivus/profile_manager.py	Mon Mar 03 09:59:10 2014 +0100
@@ -92,8 +92,7 @@
     def deleteProfile(self, button):
         profile_name = self.list_profile.getSelectedValue()
         if profile_name:
-            self.host.bridge.deleteProfile(profile_name)
-            self.__refillProfiles()
+            self.host.bridge.asyncDeleteProfile(profile_name, callback=self.__refillProfiles)
         self.host.removePopUp()
 
     def onNewProfile(self, e):
--- a/frontends/src/wix/profile_manager.py	Fri Feb 28 11:19:08 2014 +0100
+++ b/frontends/src/wix/profile_manager.py	Mon Mar 03 09:59:10 2014 +0100
@@ -21,10 +21,8 @@
 
 from sat.core.i18n import _
 import wx
-import pdb
 from logging import debug, info, error
 from sat.tools.jid  import JID
-import pdb
 
 
 class ProfileManager(wx.Panel):
@@ -99,9 +97,10 @@
                 if name[0]=='@':
                     wx.MessageDialog(self, _("A profile name can't start with a @"), _("Bad profile name"), wx.ICON_ERROR).ShowModal()
                 else:
-                    profile = self.host.bridge.createProfile(name)
-                    self.__refillProfiles()
-                    self.profile_name.SetValue(name)
+                    def cb():
+                        self.__refillProfiles()
+                        self.profile_name.SetValue(name)
+                    self.host.bridge.asyncCreateProfile(name, callback=cb)
         dlg.Destroy()
 
     def onDeleteProfile(self, event):
@@ -110,9 +109,10 @@
             return
         dlg = wx.MessageDialog(self, _("Are you sure to delete the profile [%s]") % name, _("Confirmation"), wx.ICON_QUESTION | wx.YES_NO)
         if dlg.ShowModal() == wx.ID_YES:
-            self.host.bridge.deleteProfile(name)
-            self.__refillProfiles()
-            self.__setDefault()
+            def cb():
+                self.__refillProfiles()
+                self.__setDefault()
+            self.host.bridge.asyncDeleteProfile(name, callback=cb)
         dlg.Destroy()
 
     def onProfileChange(self, event):
--- a/src/bridge/DBus.py	Fri Feb 28 11:19:08 2014 +0100
+++ b/src/bridge/DBus.py	Mon Mar 03 09:59:10 2014 +0100
@@ -204,6 +204,12 @@
         return self._callback("asyncCreateProfile", unicode(profile), callback=callback, errback=errback)
 
     @dbus.service.method(const_INT_PREFIX+const_CORE_SUFFIX,
+                         in_signature='s', out_signature='',
+                         async_callbacks=('callback', 'errback'))
+    def asyncDeleteProfile(self, profile, callback=None, errback=None):
+        return self._callback("asyncDeleteProfile", unicode(profile), callback=callback, errback=errback)
+
+    @dbus.service.method(const_INT_PREFIX+const_CORE_SUFFIX,
                          in_signature='sssis', out_signature='s',
                          async_callbacks=('callback', 'errback'))
     def asyncGetParamA(self, name, category, attribute="value", security_limit=-1, profile_key="@DEFAULT@", callback=None, errback=None):
@@ -222,24 +228,12 @@
         return self._callback("connect", unicode(profile_key))
 
     @dbus.service.method(const_INT_PREFIX+const_CORE_SUFFIX,
-                         in_signature='s', out_signature='i',
-                         async_callbacks=None)
-    def createProfile(self, profile):
-        return self._callback("createProfile", unicode(profile))
-
-    @dbus.service.method(const_INT_PREFIX+const_CORE_SUFFIX,
                          in_signature='ss', out_signature='',
                          async_callbacks=None)
     def delContact(self, entity_jid, profile_key="@DEFAULT@"):
         return self._callback("delContact", unicode(entity_jid), unicode(profile_key))
 
     @dbus.service.method(const_INT_PREFIX+const_CORE_SUFFIX,
-                         in_signature='s', out_signature='i',
-                         async_callbacks=None)
-    def deleteProfile(self, profile):
-        return self._callback("deleteProfile", unicode(profile))
-
-    @dbus.service.method(const_INT_PREFIX+const_CORE_SUFFIX,
                          in_signature='s', out_signature='',
                          async_callbacks=None)
     def disconnect(self, profile_key="@DEFAULT@"):
--- a/src/bridge/bridge_constructor/bridge_template.ini	Fri Feb 28 11:19:08 2014 +0100
+++ b/src/bridge/bridge_constructor/bridge_template.ini	Mon Mar 03 09:59:10 2014 +0100
@@ -192,18 +192,6 @@
 doc_return=dictionary of asked key,
  if key doesn't exist, the resulting dictionary will neither have the key
 
-[createProfile]
-deprecated=
-type=method
-category=core
-sig_in=s
-sig_out=i
-doc=Create a new profile
-doc_param_0=%(doc_profile)s
-doc_return=status of the creation:
- - 0: Profile created
- - 1: The profile name already exists
-
 [asyncCreateProfile]
 async=
 type=method
@@ -214,19 +202,22 @@
 doc_param_0=%(doc_profile)s
 doc_return=callback is called when profile actually exists in database and memory
 errback is called with error constant as parameter:
- - "CONFLICT": The profile name already exists
- - "CANCELED": profile creation canceled
+ - ConflictError: the profile name already exists
+ - CancelError: profile creation canceled
 
-[deleteProfile]
+[asyncDeleteProfile]
+async=
 type=method
 category=core
 sig_in=s
-sig_out=i
+sig_out=
 doc=Delete a profile
 doc_param_0=%(doc_profile)s
-doc_return=status of the deletion:
- - 0: Profile deleted
- - 1: The profile doesn't exists
+doc_return=callback is called when profile has been deleted from database and memory
+errback is called with error constant as parameter:
+ - ProfileUnknownError: the profile name is unknown
+ - ConnectedProfileError: a connected profile would not be deleted
+
 
 [registerNewAccount]
 deprecated=
--- a/src/core/exceptions.py	Fri Feb 28 11:19:08 2014 +0100
+++ b/src/core/exceptions.py	Mon Mar 03 09:59:10 2014 +0100
@@ -33,6 +33,11 @@
     pass
 
 
+class ConnectedProfileError(Exception):
+    """This error is raised when trying to delete a connected profile."""
+    pass
+
+
 class NotConnectedProfileError(Exception):
     pass
 
--- a/src/core/sat_main.py	Fri Feb 28 11:19:08 2014 +0100
+++ b/src/core/sat_main.py	Mon Mar 03 09:59:10 2014 +0100
@@ -134,9 +134,8 @@
         self.bridge.register("getProfileName", self.memory.getProfileName)
         self.bridge.register("getProfilesList", self.memory.getProfilesList)
         self.bridge.register("getEntityData", lambda _jid, keys, profile: self.memory.getEntityData(jid.JID(_jid), keys, profile))
-        self.bridge.register("createProfile", self.memory.createProfile)
         self.bridge.register("asyncCreateProfile", self.memory.asyncCreateProfile)
-        self.bridge.register("deleteProfile", self.memory.deleteProfile)
+        self.bridge.register("asyncDeleteProfile", self.memory.asyncDeleteProfile)
         self.bridge.register("registerNewAccount", self.registerNewAccount)
         self.bridge.register("connect", self.connect)
         self.bridge.register("asyncConnect", self.asyncConnect)
--- a/src/memory/memory.py	Fri Feb 28 11:19:08 2014 +0100
+++ b/src/memory/memory.py	Mon Mar 03 09:59:10 2014 +0100
@@ -215,25 +215,11 @@
         self.params_gen = {}
         host.registerCallback(host.registerNewAccountCB, with_data=True, force_id="registerNewAccount")
 
-    def createProfile(self, profile):
-        """Create a new profile
-        @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 [%s] already exists') % (profile, ))
-            return True
-        if not self.host.trigger.point("ProfileCreation", profile):
-            return False
-        self.storage.createProfile(profile)
-        return False
-
     def asyncCreateProfile(self, profile):
         """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
+        @return: a Deferred instance
         """
         if self.storage.hasProfile(profile):
             info(_('The profile name already exists'))
@@ -242,18 +228,18 @@
             return defer.fail(Failure(exceptions.CancelError))
         return self.storage.createProfile(profile)
 
-    def deleteProfile(self, profile):
+    def asyncDeleteProfile(self, profile):
         """Delete an existing profile
-        @param profile: name of the profile"""
-        #TODO: async equivalent, like for createProfile
+        @param profile: name of the profile
+        @return: a Deferred instance
+        """
         if not self.storage.hasProfile(profile):
-            error(_('Trying to delete an unknown profile'))
-            return True
+            info(_('Trying to delete an unknown profile'))
+            return defer.fail(Failure(exceptions.ProfileUnknownError))
         if self.host.isConnected(profile):
-            error(_("Trying to delete a connected profile"))
-            raise exceptions.NotConnectedProfileError
-        self.storage.deleteProfile(profile)
-        return False
+            info(_("Trying to delete a connected profile"))
+            return defer.fail(Failure(exceptions.ConnectedProfileError))
+        return self.storage.deleteProfile(profile)
 
     def getProfileName(self, profile_key, return_profile_keys = False):
         """return profile according to profile_key
@@ -890,22 +876,16 @@
         @return: profile name or None if it doesn't exist"""
         return self.params.getProfileName(profile_key, return_profile_keys)
 
-    def createProfile(self, name):
-        """Create a new profile
-        @param name: Profile name
-        """
-        return self.params.createProfile(name)
-
     def asyncCreateProfile(self, name):
         """Create a new profile
         @param name: Profile name
         """
         return self.params.asyncCreateProfile(name)
 
-    def deleteProfile(self, name):
+    def asyncDeleteProfile(self, name):
         """Delete an existing profile
         @param name: Name of the profile"""
-        return self.params.deleteProfile(name)
+        return self.params.asyncDeleteProfile(name)
 
     def addToHistory(self, from_jid, to_jid, message, type_='chat', extra=None, timestamp=None, profile="@NONE@"):
         assert profile != "@NONE@"