Removed ecore necessity from python-purple.
[python-purple] / conversation.pyx
1 #
2 #  Copyright (c) 2008 INdT - Instituto Nokia de Tecnologia
3 #
4 #  This file is part of python-purple.
5 #
6 #  python-purple is free software: you can redistribute it and/or modify
7 #  it under the terms of the GNU General Public License as published by
8 #  the Free Software Foundation, either version 3 of the License, or
9 #  (at your option) any later version.
10 #
11 #  python-purple is distributed in the hope that it will be useful,
12 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #  GNU General Public License for more details.
15 #
16 #  You should have received a copy of the GNU General Public License
17 #  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19
20 cimport purple
21
22 cdef class Conversation:
23     """
24     Conversation class
25     @param type    UNKNOWN, IM, CHAT, MISC, ANY
26     @param account Your account
27     @param name    Buddy name
28     """
29
30     cdef object __account
31     cdef object __name
32     cdef object __type
33     cdef object __exists
34
35     def __init__(self, type, account, name):
36         self.__type = {
37             "UNKNOWN": conversation.PURPLE_CONV_TYPE_UNKNOWN,
38             "IM": conversation.PURPLE_CONV_TYPE_IM,
39             "CHAT": conversation.PURPLE_CONV_TYPE_CHAT,
40             "MISC": conversation.PURPLE_CONV_TYPE_MISC,
41             "ANY": conversation.PURPLE_CONV_TYPE_ANY }[type]
42         self.__account = account
43         self.__name = name
44
45         if self._get_structure() != NULL:
46             self.__exists = True
47         else:
48             self.__exists = False
49
50     cdef conversation.PurpleConversation *_get_structure(self):
51         return conversation.purple_find_conversation_with_account( \
52             self.__type, self.__name, account.purple_accounts_find( \
53             self.__account.username, self.__account.protocol.id))
54
55     def __get_exists(self):
56         return self.__exists
57     exists = property(__get_exists)
58
59     def __get_account(self):
60         if self.__exists:
61             return self.__account
62         else:
63             return None
64     account = property(__get_account)
65
66     def __get_name(self):
67         if self.__exists:
68             return <char *> conversation.purple_conversation_get_name( \
69                     self._get_structure())
70         else:
71             return None
72     name = property(__get_name)
73
74     def new(self):
75         """
76         Creates a new conversation.
77
78         @return True if successful, False if conversation already exists
79         """
80         if self.__exists:
81             return False
82         else:
83             conversation.purple_conversation_new(self.__type, \
84                     account.purple_accounts_find(self.__account.username, \
85                     self.__account.protocol.id), self.__name)
86             self.__exists = True
87             return True
88
89     def destroy(self):
90         """
91         Destroys a conversation.
92
93         @return True if successful, False if conversation doesn't exists
94         """
95         if self.__exists:
96             conversation.purple_conversation_destroy(self._get_structure())
97             self.__exists = False
98             return True
99         else:
100             return False
101
102     def set_ui_ops(self, cbs):
103         """
104         Sets UI operations for a conversation.
105
106         @return True if sucessful, False otherwise
107         """
108         # FIXME: We may need to create c-functions for each of these?
109         cdef conversation.PurpleConversationUiOps c_conv_ui_ops
110
111         c_conv_ui_ops.create_conversation = NULL
112         c_conv_ui_ops.destroy_conversation = NULL
113         c_conv_ui_ops.write_chat = NULL
114         c_conv_ui_ops.write_im = NULL
115         c_conv_ui_ops.write_conv = NULL
116         c_conv_ui_ops.chat_add_users = NULL
117         c_conv_ui_ops.chat_rename_user = NULL
118         c_conv_ui_ops.chat_remove_users = NULL
119         c_conv_ui_ops.chat_update_user = NULL
120         c_conv_ui_ops.present = NULL
121         c_conv_ui_ops.has_focus = NULL
122         c_conv_ui_ops.custom_smiley_add = NULL
123         c_conv_ui_ops.custom_smiley_write = NULL
124         c_conv_ui_ops.custom_smiley_close = NULL
125         c_conv_ui_ops.send_confirm = NULL
126
127         conversation.purple_conversation_set_ui_ops(self._get_structure(), \
128                 &c_conv_ui_ops)
129         return True
130
131     def im_send(self, message):
132         """
133         Sends a message to this IM conversation.
134
135         @return True if successful, False if conversation is not IM or conversation doesn't exists
136         """
137         if self.__exists and self.__type == conversation.PURPLE_CONV_TYPE_IM:
138             conversation.purple_conv_im_send( \
139                     conversation.purple_conversation_get_im_data( \
140                     self._get_structure()), message)
141             return True
142         else:
143             return False