Updated nullclient.py
[python-purple] / conversation.pyx
index ecb00bc..643d781 100644 (file)
 #  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
 
+cimport purple
+
 cdef class Conversation:
-    """ Conversation class """
-    cdef PurpleConversation *__conv
+    """
+    Conversation class
+    @param type    UNKNOWN, IM, CHAT, MISC, ANY
+    @param account Your account
+    @param name    Buddy name
+    """
+
+    cdef object __account
+    cdef object __name
+    cdef object __type
+    cdef object __exists
+
+    def __init__(self, type, account, name):
+        self.__type = {
+            "UNKNOWN": conversation.PURPLE_CONV_TYPE_UNKNOWN,
+            "IM": conversation.PURPLE_CONV_TYPE_IM,
+            "CHAT": conversation.PURPLE_CONV_TYPE_CHAT,
+            "MISC": conversation.PURPLE_CONV_TYPE_MISC,
+            "ANY": conversation.PURPLE_CONV_TYPE_ANY }[type]
+        self.__account = account
+        self.__name = name
+
+        if self._get_structure() != NULL:
+            self.__exists = True
+        else:
+            self.__exists = False
+
+    cdef conversation.PurpleConversation *_get_structure(self):
+        return conversation.purple_find_conversation_with_account( \
+            self.__type, self.__name, account.purple_accounts_find( \
+            self.__account.username, self.__account.protocol.id))
+
+    def __get_exists(self):
+        return self.__exists
+    exists = property(__get_exists)
+
+    def __get_account(self):
+        if self.__exists:
+            return self.__account
+        else:
+            return None
+    account = property(__get_account)
 
-    def __cinit__(self):
-        purple_conversations_init()
+    def __get_name(self):
+        if self.__exists:
+            return <char *> conversation.purple_conversation_get_name( \
+                    self._get_structure())
+        else:
+            return None
+    name = property(__get_name)
 
-    def conversation_new(self, type, acc, const_char_ptr name):
-        self.__conv = purple_conversation_new(type, <PurpleAccount*>acc.__account, name)
+    def new(self):
+        """
+        Creates a new conversation.
+
+        @return True if successful, False if conversation already exists
+        """
+        if self.__exists:
+            return False
+        else:
+            conversation.purple_conversation_new(self.__type, \
+                    account.purple_accounts_find(self.__account.username, \
+                    self.__account.protocol.id), self.__name)
+            self.__exists = True
+            return True
+
+    def destroy(self):
+        """
+        Destroys a conversation.
+
+        @return True if successful, False if conversation doesn't exists
+        """
+        if self.__exists:
+            conversation.purple_conversation_destroy(self._get_structure())
+            self.__exists = False
+            return True
+        else:
+            return False
+
+    def set_ui_ops(self, cbs):
+        """
+        Sets UI operations for a conversation.
+
+        @return True if sucessful, False otherwise
+        """
+        # FIXME: We may need to create c-functions for each of these?
+        cdef conversation.PurpleConversationUiOps c_conv_ui_ops
 
-    def conversation_set_ui_ops(self):
-        cdef PurpleConversationUiOps c_conv_ui_ops
         c_conv_ui_ops.create_conversation = NULL
         c_conv_ui_ops.destroy_conversation = NULL
         c_conv_ui_ops.write_chat = NULL
@@ -45,18 +124,20 @@ cdef class Conversation:
         c_conv_ui_ops.custom_smiley_close = NULL
         c_conv_ui_ops.send_confirm = NULL
 
-        purple_conversation_set_ui_ops(self.__conv, &c_conv_ui_ops)
-
-    def conversation_write(self, const_char_ptr message):
-        purple_conv_im_send(purple_conversation_get_im_data(self.__conv), message)
-
-    def conversation_destroy(self):
-        purple_conversation_destroy(self.__conv)
+        conversation.purple_conversation_set_ui_ops(self._get_structure(), \
+                &c_conv_ui_ops)
+        return True
 
-    def conversation_get_handle(self):
-        purple_conversations_get_handle()
+    def im_send(self, message):
+        """
+        Sends a message to this IM conversation.
 
-    def send_message(self, buddy, const_char_ptr message):
-        self.conversation_new(1, buddy.account, buddy.name)
-        self.conversation_set_ui_ops()
-        self.conversation_write(message)
+        @return True if successful, False if conversation is not IM or conversation doesn't exists
+        """
+        if self.__exists and self.__type == conversation.PURPLE_CONV_TYPE_IM:
+            conversation.purple_conv_im_send( \
+                    conversation.purple_conversation_get_im_data( \
+                    self._get_structure()), message)
+            return True
+        else:
+            return False