add color picker and support for whiteboard zooming
[makneto-zunavac1.git] / src / ui-mobile / declarative / SessionController.qml
blob408498b9c5ba3f45ddc5660504c8c7f101b5c2e5
1 /*
2  *   Copyright (C) 2011 Lukáš Karas <lukas.karas@centrum.cz>
3  *
4  *   This program is free software; you can redistribute it and/or modify
5  *   it under the terms of the GNU General Public License as published by
6  *   the Free Software Foundation; either version 2 of the License, or
7  *   (at your option) any later version.
8  *
9  *   This program is distributed in the hope that it will be useful,
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *   GNU General Public License for more details.
13  *
14  *   You should have received a copy of the GNU General Public License
15  *   along with this program; if not, write to the
16  *   Free Software Foundation, Inc.,
17  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  */
20 import Qt 4.7
21 import QtQuick 1.0
22 //import QtMultimediaKit 1.1
23 import org.makneto 0.1 as Makneto
25 Rectangle {
26     id: sessionController
27     color: parent.color
29     property variant scenes: {} // associative array (by session id) of opened sessions
30     property variant topScene: undefined // session id of scene that is on top, or udefined when contact list is on top
31     property variant sessionSceneComponent : Qt.createComponent("SessionScene.qml");
32     property variant _model: ListModel {}
34     function log(msg){
35         console.log("II [SessionController.qml]: "+msg);
36     }
37     function error(msg){
38         console.log("EE [SessionController.qml]: "+msg);
39     }
40     function warn(msg){
41         console.log("WW [SessionController.qml]: "+msg);
42     }
44     function getSessionModel(){
45         return _model;
46     }
48     function onSessionsModelChanged(model){
49         sessionController._model = model;
50         // connect to signals from session model
51         try{
52             sessionController._model.sessionOpened.connect(sessionOpened);
53             sessionController._model.sessionClosed.connect(sessionClosed);
54             sessionController._model.whiteboardMessageReceived.connect(whiteboardMessageReceived);
55             sessionController._model.textMessageReceived.connect(textMessageReceived);
56             sessionController._model.chatStateChanged.connect(chatStateChanged);
57             sessionController._model.sessionTypeChanged.connect(sessionTypeChanged);
58             sessionController._model.messageSent.connect(messageSent);
60             sessionController._model.startBeeping.connect(startBeeping);
61             sessionController._model.stopBeeping.connect(stopBeeping);
62             sessionController._model.startRinging.connect(startRinging);
63             sessionController._model.stopRinging.connect(stopRinging);
65         }catch(e){
66             error("error while connecting to signals "+JSON.stringify(e));
67         }
68     }
70     function startBeeping(){
71         log("startBeeping");
72         //beeper.start();
73     }
75     function stopBeeping(){
76         log("stopBeeping");
77         //beeper.stop();
78     }
80     function startRinging(){
81         log("startRinging");
82         //ringer.start();
83     }
85     function stopRinging(){
86         log("stopRinging");
87         //ringer.stop();
88     }
90     function whiteboardMessageReceived(sessionId, data, contact){
91         try{
92             var scene = scenes[ sessionId ];
93             if (scene===undefined){
94                 error("whiteboard message recived on session "+sessionId+" but this session doesn't exists ("+contact+":"+data+")");
95                 return;
96             }
97             scene.whiteboardMessageReceived(data, contact);
98         }catch(e){
99             error("error "+JSON.stringify(e));
100         }
101     }
102     function messageSent(sessionId, content){
103         try{
104             var scene = scenes[ sessionId ];
105             if (scene===undefined){
106                 error("text message recived on session "+sessionId+" but this session doesn't exists ("+contact+":"+text+")");
107                 return;
108             }
109             scene.messageSent(content);
110         }catch(e){
111             error("error "+JSON.stringify(e));
112         }
113     }
114     function textMessageReceived(sessionId, text, contact){
115         try{
116             var scene = scenes[ sessionId ];
117             if (scene===undefined){
118                 error("text message recived on session "+sessionId+" but this session doesn't exists ("+contact+":"+text+")");
119                 return;
120             }
121             scene.textMessageReceived(text, contact);
122         }catch(e){
123             error("error "+JSON.stringify(e));
124         }
125     }
126     function chatStateChanged(sessionId, chatState, contact){
127         try{
128             var scene = scenes[ sessionId ];
129             if (scene===undefined){
130                 error("chat state change in session "+sessionId+" but this session doesn't exists");
131                 return;
132             }
133             scene.chatStateChanged(chatState, contact);
134         }catch(e){
135             error("error "+JSON.stringify(e));
136         }
137     }
138     function sessionTypeChanged(sessionId, type){
139         try{
140             var scene = scenes[ sessionId ];
141             if (scene===undefined){
142                 error("session type changed in session "+sessionId+" but this session doesn't exists");
143                 return;
144             }
145             scene.sessionTypeChanged(type);
146         }catch(e){
147             error("error "+JSON.stringify(e));
148         }
149     }
151     function showContactList(){
152         log("show contact list");
153         for (var i in scenes){
154             var sc = scenes[i]
155             if (sc.sessionId == topScene){
156                 sc.z = 2;
157             }else{
158                 sc.z = 0;
159             }
160         }
161         contactListScene.z = 1;
162         if (topScene !== undefined){
163             var sc = scenes[topScene]
164             sc.state = "right"
165         }
166         topScene = undefined;
167     }
169     function closeSession(sessionId){
170         log("close session "+sessionId);
171         _model.closeSession(sessionId);
172         //showContactList();
173     }
175     // I don't know how make it better
176     function objRemove(obj, id){
177         var tmp = {}
178         for (var i in obj){
179             if (i !== id)
180                 tmp[i] = obj[i];
181         }
182         return tmp;
183     }
185     function sessionClosed(sessionId){
186         var scene = scenes[ sessionId ];
187         if (scene===undefined){
188             error("scene for session "+sessionId+" doesn't exists");
189             return;
190         }
191         if (topScene == sessionId){
192             showContactList();
193         }
194         //log("scene before delete = "+scenes[ sessionId ]);
195         scenes = objRemove(scenes, sessionId);
196         //log("scene after delete = "+scenes[ sessionId ]);
197         scene.destroyLater();
198     }
200     function sessionOpened(sessionId, extendExisting, requeted){
201         log("create new session for "+sessionId+" ("+extendExisting+", "+requeted+")");
203         //log("we play russian rulete with scenes...");
204         var scene = scenes[ sessionId ];
205         //log("scene is "+scene);
206         if (scene !== undefined){
207             if (requeted){
208                 activateSession(sessionId); // we requested session that already exists, just activate it
209             }else if (!extendExisting){
210                 error("scene with id "+sessionId+" already exists");
211             }
212             return;
213         }
215         if(sessionSceneComponent.status != Component.Ready){
216             error("Component "+sessionSceneComponent.url+" is not ready!");
217             error(sessionSceneComponent.errorString());
218             return false;
219         }
221         var session = sessionSceneComponent.createObject(sessionController);
222         if(session === null){
223             error("error creating session scene");
224             error(sessionSceneComponent.errorString());
225             return false;
226         }
227         var tmp = scenes;
228         tmp[ sessionId ] = session;
229         scenes = tmp;
231         //log("scenes: "+JSON.stringify(scenes));
233         session.state = "right";
234         session.animate = true;
235         session.sessionId = sessionId;
236         session.setSessionsModel(_model);
237         activateSession(sessionId);
238     }
240     function activateSession(id){
241         log("activate scene "+id)
242         //log("scenes: "+JSON.stringify(scenes));
244         var scene = scenes[ id ];
245         if (scene === undefined){
246             error("scene for session "+id+" doesn't exists");
247             showContactList();
248             return;
249         }
250         if (topScene == id){
251             log("top scene already is "+id);
252             return;
253         }
254         for (var i in scenes){
255             var sc = scenes[i]
256             if (sc.sessionId == topScene){
257                 sc.z = 1;
258             }else{
259                 sc.z = 0;
260             }
261         }
262         contactListScene.z = (topScene === undefined)? 1: 0;
263         scene.animate = false;
264         scene.state = "right";
266         scene.z = 2
267         topScene = id;
268         scene.animate = true;
269         scene.state = "center";
270         log("topScene: "+topScene);
271     }
272     function requestSession(managerId, accountId, type){
273         log("request session for "+ managerId+" / "+accountId+" (type "+type+")");
274         _model.requestSession(managerId, accountId, type);
275     }
277     Component.onCompleted: {
278         scenes = {};
279         //main.addSessionsModelListener(sessionController);
280         main.sessionsModelChanged.connect(onSessionsModelChanged);
281     }
283     /*
284     SoundEffect {
285          id: beeper
286          loops: 100
287          source: "/sounds/telephonebeep.mp3"
288      }
289     SoundEffect {
290          id: ringer
291          loops: 100
292          source: "/sounds/telephonering.mp3"
293      }
294      */
296     ContactListScene{
297         id: contactListScene
298         color: parent.color
299         anchors.fill: parent
300         z: 2
301     }