Initial commit
[tietoopcom] / include / TocEngine / tocengine.h
1 /** \file TocBaseEngine.h
2  * \brief Declaration of TocBaseEngine class
3  * 
4  * Tieto Open Communicator - Client for the Telepathy communications framework.
5  * Copyright (c) 2010, Tieto Corporation
6  *
7  * All rights reserved.
8  * Redistribution and use in source and binary forms, with or without modification,
9  * are permitted provided that the following conditions are met:
10  *
11  *      Redistributions of source code must retain the above copyright notice,
12  *      this list of conditions and the following disclaimer.
13  *      Redistributions in binary form must reproduce the above copyright notice,
14  *      this list of conditions and the following disclaimer in the documentation
15  *      and/or other materials provided with the distribution.
16  *      Neither the name of the Tieto Corporation nor the names of its contributors 
17  *      may be used to endorse or promote products derived from this software without
18  *      specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  * 
30  */
31
32 #ifndef TOCENGINE_H
33 #define TOCENGINE_H
34
35 #include "TocBaseEngine"
36 #include <QtTapioca/Connection>
37 #include <QtTapioca/TextChannel>
38
39 namespace QtTapioca {
40         class ContactBase;
41 };
42
43 using namespace QtTapioca;
44
45 QT_BEGIN_NAMESPACE
46
47 /** \brief TocEngine class
48  * 
49  * Wrapper around the QtTapioca/QtTelepathy communication framework,
50  * responsible for managing opened channels and connection(s),
51  * and for synchronizaion beetween our contact list and the one we get from the telepathy.
52  */
53 class TocEngine : public TocBaseEngine
54 {
55         Q_OBJECT
56
57 public:
58
59         /**
60          * Public Constructor
61          */
62         TocEngine( QObject * parent = 0 );
63
64         /**
65          * Destructor
66          */
67         ~TocEngine();
68
69 signals:
70         
71 public slots:
72
73         /** \brief Reimplemented from TocBaseEngine
74          * 
75          */     
76         void onPresenceChange(Presence presence, QString description);
77
78         /** \brief Reimplemented from TocBaseEngine
79          * 
80          */     
81         void onAddContactRequest(TocContact* pContact);
82
83         /** \brief Reimplemented from TocBaseEngine
84          * 
85          */     
86         void onEditContactRequest(TocContact* pContact, QString oldUid);
87
88         /** \brief Reimplemented from TocBaseEngine
89          * 
90          */     
91         void onRemoveContactRequest(QString uid);
92
93         /** \brief Reimplemented from TocBaseEngine
94          * 
95          */     
96         void onSubscriptionAccepted(QString /*uid*/, bool /*bAuthorized*/) {}
97         
98         /** \brief Reimplemented from TocBaseEngine
99          * 
100          */
101         void initializeFromSettings();
102
103         /** \brief Called when User wants to send a message to the contact
104          * 
105          * @param uid Contact's ID
106          * @param message Message to be sent
107          */
108         virtual void onNewMessageReady(QString uid, Message message);
109
110         /** \brief Closes the channel(s) created for the given contact
111          * 
112          * When user wants to quit the conversation this slot should be called.
113          * 
114          * @param uid User ID of the contact
115          */
116         virtual void onSessionClosed(QString uid);
117
118 private slots:
119
120         /** \brief Used when connection status was changed
121          * 
122          * @param pConnection Handler to QtTapioca connection
123          * @param status Status of connection
124          * @param reason Reason of status change 
125          */
126         void onStatusChange(QtTapioca::Connection *pConnection, QtTapioca::Connection::Status status, QtTapioca::Connection::Reason reason);
127         
128         /** \brief Used when contact presence was changed
129          *
130          * @param pContact Contact item which changed its presence  
131          * @param presence Actual contact's presence
132          * @param presenceMessage Actual contact's presence message
133          */
134         void onPresenceUpdated(QtTapioca::ContactBase *pContact, QtTapioca::ContactBase::Presence presence, const QString &presenceMessage);
135
136         /** \brief Used when user presence was changed
137          *
138          * @param pContact Contact item which changed its presence
139          * @param presence Actual users's presence
140          * @param presenceMessage Actual users's presence message
141          */
142         void onSelfPresenceUpdated(QtTapioca::ContactBase *pContact, QtTapioca::ContactBase::Presence presence, const QString &presenceMessage);
143
144         /** \brief Used when user receives message
145          *
146          * @param pTextChannel Channel from whom we recieved message
147          * @param message received message
148          */
149         void onMessageReceived(const QtTapioca::TextChannel* pTextChannel, const QtTapioca::TextChannel::Message &message);
150
151         /** \brief Used when message has not been sent
152          * 
153          * When an error occured this slot prepares a Message with a proper 'error' field value
154          * 
155          * @param message Message for which an error occured
156          * @param error Error type
157          */
158         void onMessageDeliveryError(const QtTapioca::TextChannel::Message &message, QtTapioca::TextChannel::Message::DeliveryError error);
159
160         /** \brief Called when someone wants to initiate chat with user
161          * 
162          * To receive message that initiated this channel one should call pendingMessages method
163          * 
164          * @param pConnection Connection on which new channel is created
165          * @param pChannel Newly created channel
166          * @param bSuppresHandler Used to notify other channel handlers that they should not use this channel object
167          */
168         void onChannelCreated(QtTapioca::Connection* pConnection, QtTapioca::Channel* pChannel, bool bSuppresHandler);
169
170
171         /** \brief Authorizes a contact and add it to users list
172          * 
173          * TODO: this should be implemented in a way that authorization
174          * depends on user settings
175          * 
176          * @param pContact Contact to be authorized
177          */
178         void onAuthorizationRequested(QtTapioca::Contact *pContact);
179         
180 private: //Methods
181
182         /** \brief Synchronizes server and local contact lists
183          * 
184          * Lists should be synchronized according to 
185          * the synchronization priority set by user. (NO LONGER TRUE AS THE SYNC PRORITY WILL BE REMOVED)
186          */
187         void synchronizeContactLists();
188
189         /** \brief Adds pending contacts to the list on the server
190          * 
191          */
192         void addPendingContacts();
193
194         /** \brief Removes pending contacts from the list on the server
195          * 
196          */
197         void removePendingContacts();
198
199         /** \brief  Syncronizes contacts from server to the local list
200          * 
201          * When contacts from server are edited in a different client
202          *
203          * Note that this function SHOULD NOT be run alone,
204          * but ONLY AFTER addition and removal of pending contacts from server
205          */
206         void syncContactsFromServer();
207         
208         /** \brief Returns true if a user with the given Uid is already on the list
209          *
210          * @param uid Uid of the searched contact 
211          * @return index of the contact or -1 if not found
212          */
213         int indexForUid( QString uid );
214         
215         /** \brief Removes contact from local contact list
216          * 
217          * @param uid Uid of the contact to be removed
218          */
219         void removeContact( QString uid );
220
221         /** \brief Adds contact to the local list
222          * 
223          * @param contact Pointer to the QtTapioca::Contact instance
224          */
225         void addContact( const Contact* contact );
226
227 private: //Members
228
229         bool                                                            _bNewUserContact;                       /// Indicates that this is the first status change after initializeFromSettings
230         bool                                                            _bContactListSynchronized;      /// Indicates if contact list has been already sent to the UI
231         QtTapioca::Connection*                          _pConnection;                           /// Active connection
232         TocContactList*                                         _pTocContactList;                       /// Current contact list
233         QStringList*                                            _pAddedContacts;                        /// Contacts to be added
234         QStringList*                                            _pRemovedContacts;                      /// Contacts to be removed
235 };
236
237 QT_END_NAMESPACE
238
239 #endif // TOCENGINE_H