2 * Copyright (C) 2011 Lukáš Karas <lukas.karas@centrum.cz>
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.
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.
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.
22 //import QtMultimediaKit 1.1
23 import org.makneto 0.1 as Makneto
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 {}
35 console.log("II [SessionController.qml]: "+msg);
38 console.log("EE [SessionController.qml]: "+msg);
41 console.log("WW [SessionController.qml]: "+msg);
44 function getSessionModel(){
48 function onSessionsModelChanged(model){
49 sessionController._model = model;
50 // connect to signals from session model
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);
66 error("error while connecting to signals "+JSON.stringify(e));
70 function startBeeping(){
75 function stopBeeping(){
80 function startRinging(){
85 function stopRinging(){
90 function whiteboardMessageReceived(sessionId, data, contact){
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+")");
97 scene.whiteboardMessageReceived(data, contact);
99 error("error "+JSON.stringify(e));
102 function messageSent(sessionId, content){
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+")");
109 scene.messageSent(content);
111 error("error "+JSON.stringify(e));
114 function textMessageReceived(sessionId, text, contact){
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+")");
121 scene.textMessageReceived(text, contact);
123 error("error "+JSON.stringify(e));
126 function chatStateChanged(sessionId, chatState, contact){
128 var scene = scenes[ sessionId ];
129 if (scene===undefined){
130 error("chat state change in session "+sessionId+" but this session doesn't exists");
133 scene.chatStateChanged(chatState, contact);
135 error("error "+JSON.stringify(e));
138 function sessionTypeChanged(sessionId, type){
140 var scene = scenes[ sessionId ];
141 if (scene===undefined){
142 error("session type changed in session "+sessionId+" but this session doesn't exists");
145 scene.sessionTypeChanged(type);
147 error("error "+JSON.stringify(e));
151 function showContactList(){
152 log("show contact list");
153 for (var i in scenes){
155 if (sc.sessionId == topScene){
161 contactListScene.z = 1;
162 if (topScene !== undefined){
163 var sc = scenes[topScene]
166 topScene = undefined;
169 function closeSession(sessionId){
170 log("close session "+sessionId);
171 _model.closeSession(sessionId);
175 // I don't know how make it better
176 function objRemove(obj, id){
185 function sessionClosed(sessionId){
186 var scene = scenes[ sessionId ];
187 if (scene===undefined){
188 error("scene for session "+sessionId+" doesn't exists");
191 if (topScene == sessionId){
194 //log("scene before delete = "+scenes[ sessionId ]);
195 scenes = objRemove(scenes, sessionId);
196 //log("scene after delete = "+scenes[ sessionId ]);
197 scene.destroyLater();
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){
208 activateSession(sessionId); // we requested session that already exists, just activate it
209 }else if (!extendExisting){
210 error("scene with id "+sessionId+" already exists");
215 if(sessionSceneComponent.status != Component.Ready){
216 error("Component "+sessionSceneComponent.url+" is not ready!");
217 error(sessionSceneComponent.errorString());
221 var session = sessionSceneComponent.createObject(sessionController);
222 if(session === null){
223 error("error creating session scene");
224 error(sessionSceneComponent.errorString());
228 tmp[ sessionId ] = session;
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);
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");
251 log("top scene already is "+id);
254 for (var i in scenes){
256 if (sc.sessionId == topScene){
262 contactListScene.z = (topScene === undefined)? 1: 0;
263 scene.animate = false;
264 scene.state = "right";
268 scene.animate = true;
269 scene.state = "center";
270 log("topScene: "+topScene);
272 function requestSession(managerId, accountId, type){
273 log("request session for "+ managerId+" / "+accountId+" (type "+type+")");
274 _model.requestSession(managerId, accountId, type);
277 Component.onCompleted: {
279 //main.addSessionsModelListener(sessionController);
280 main.sessionsModelChanged.connect(onSessionsModelChanged);
287 source: "/sounds/telephonebeep.mp3"
292 source: "/sounds/telephonering.mp3"