prepare for whiteboard pinch zoom
[makneto-zunavac1.git] / src / ui-mobile / makneto.cpp
blob48d9f4fa11bc21503f3ac499ca2f04f5831c2ac8
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.
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.
20 #include <QDebug>
21 #include <QSound>
22 #include <QMainWindow>
23 #include <QtOpenGL/QGLWidget>
25 // declarative
26 #include <QDeclarativeView>
27 #include <QDeclarativeContext>
28 #include <QDeclarativeError>
29 #include <QDeclarativeItem>
30 #include <QtDeclarative/QDeclarativeEngine>
31 #include <QtDeclarative/QDeclarativeContext>
33 // Gstreamer and QtGstQmlSink
34 #include <gst/gst.h>
35 #include <QtGstQmlSink/qmlvideosurfacegstsink.h>
36 #include <QtGstQmlSink/qmlgstvideoitem.h>
38 // telepathy
39 #include <TelepathyQt4/Types>
40 #include <TelepathyQt4/Account>
42 // backend
43 #include "../backend/telepathy-initializer.h"
44 #include "../backend/telepathy-client.h"
45 #include "../backend/session.h"
46 #include "../backend/voip/element-factory/defaultelementfactory.h"
47 #include "../backend/voip/element-factory/deviceelementfactory.h"
48 #include "../backend/accounts-model-item.h"
49 #include "../backend/accounts-model.h"
51 // makneto-mobile
52 #include "main-window.h"
53 #include "makneto.h"
54 #include "telepathytypes.h"
55 #include "contacts-model-proxy.h"
56 #include "contacts-sort-filter-model-proxy.h"
57 #include "mobile-gst-element-factory.h"
58 #include "sound-notificator.h"
60 Makneto::Makneto(QApplication *app) : _app(app),
61 _qmlView(0),
62 _window(0),
63 _contactsModel(0),
64 _contactsProxy(0),
65 _sessionModel(0),
66 _notificationsModel(0),
67 _trayIcon(0),
68 _ownDeviceFactory(0) {
70 // register data types to make its enums accessible from qml
71 qmlRegisterUncreatableType<MaknetoBackend::AccountsModel > ("org.makneto", 0, 1, "AccountsModel", QLatin1String("This type is only exported for its enums"));
72 qmlRegisterUncreatableType<MaknetoBackend::Session > ("org.makneto", 0, 1, "Session", QLatin1String("This type is only exported for its enums"));
73 qmlRegisterUncreatableType<SessionModel > ("org.makneto", 0, 1, "SessionModel", QLatin1String("This type is only exported for its enums"));
74 qmlRegisterUncreatableType<NotificationsModel > ("org.makneto", 0, 1, "NotificationsModel", QLatin1String("This type is only exported for its enums"));
75 qmlRegisterUncreatableType<NotificationItem > ("org.makneto", 0, 1, "NotificationItem", QLatin1String("This type is only exported for its enums"));
76 qmlRegisterUncreatableType<TelepathyTypes > ("org.makneto", 0, 1, "TelepathyTypes", QLatin1String("This type is only exported for its enums"));
78 /* register QtGstQmlSink items for playing videos */
79 qmlRegisterType<QmlGstVideoItem > ("Gst", 1, 0, "VideoItem");
80 qmlRegisterType<QmlPainterVideoSurface > ("Gst", 1, 0, "VideoSurface");
82 _qmlView = new QDeclarativeView();
83 _window = new MainWindow(_app, this);
85 connect(_qmlView, SIGNAL(statusChanged(QDeclarativeView::Status)),
86 this, SLOT(onQmlStatusChanged(QDeclarativeView::Status)));
88 _qmlView->setResizeMode(QDeclarativeView::SizeRootObjectToView);
89 _window->setCentralWidget(_qmlView);
93 Makneto::~Makneto() {
94 MaknetoBackend::DeviceElementFactory::destroyAllFactories();
96 if (_qmlView)
97 _qmlView->deleteLater();
98 if (_window)
99 _window->deleteLater();
100 if (_notificationsModel)
101 _notificationsModel->deleteLater();
102 if (_contactsProxy)
103 _contactsProxy->deleteLater();
104 if (_contactsModel)
105 _contactsModel->deleteLater();
106 if (_sessionModel)
107 _sessionModel->deleteLater();
108 if (_soundNotificator)
109 _soundNotificator->deleteLater();
110 if (_presenceManager)
111 _presenceManager->deleteLater();
114 bool Makneto::init() {
115 QDeclarativeContext* rootContext = _qmlView->rootContext();
116 rootContext->setContextProperty("makneto", this);
118 _qmlView->setSource(QUrl("qrc:/declarative/mainview.qml"));
119 if (_qmlView->status() == QDeclarativeView::Error)
120 return false;
122 return true;
125 void Makneto::onQmlStatusChanged(QDeclarativeView::Status status) {
126 switch (status) {
127 case QDeclarativeView::Null:
128 break;
129 case QDeclarativeView::Ready:
130 qDebug() << "Makneto: QML ready...";
131 onViewReady();
132 break;
133 case QDeclarativeView::Loading:
134 qDebug() << "Makneto: Loading QML...";
135 break;
136 case QDeclarativeView::Error:
137 qWarning() << "Makneto: Error occurs while loading qml (ui) file. Exiting";
138 qWarning() << _qmlView->errors();
139 _app->exit(-1);
140 break;
141 default:
142 qWarning() << "Makneto: udefind QML status" << status;
146 void Makneto::onViewReady() {
147 showWindow();
149 _ownDeviceFactory = new MobileGstElementFactory(_qmlView);
150 MaknetoBackend::DeviceElementFactory::installFactory(_ownDeviceFactory);
152 MaknetoBackend::TelepathyInitializer *tpInit = new MaknetoBackend::TelepathyInitializer();
154 connect(tpInit,
155 SIGNAL(finished(MaknetoBackend::TelepathyClient *)),
156 SLOT(onTelepathyInitializerFinished(MaknetoBackend::TelepathyClient *)));
158 tpInit->createClient(_app->applicationName());
161 void Makneto::showWindow() {
162 #ifdef Q_OS_SYMBIAN
163 qDebug() << "running on Symbian, show fullscreen";
164 _window->showFullScreen();
165 #elif defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6)
166 qDebug() << "running on Maemo, show maximized";
167 _window->showMaximized();
168 #else
169 qDebug() << "Makneto: running environment is not Symbian nor Maemo - show in window";
170 _window->show();
171 #endif
174 void Makneto::onTelepathyInitializerFinished(MaknetoBackend::TelepathyClient* client) {
176 QDeclarativeContext* rootContext = _qmlView->rootContext();
177 //QObject *rootObject = dynamic_cast<QObject*> (_qmlView->rootObject());
179 // initialize models
180 // - account model direct from backend
181 rootContext->setContextProperty("accountsModel", client->accountsModel());
183 _presenceManager = new PresenceManager(client->accountsModel(), this);
184 rootContext->setContextProperty("presenceManager", _presenceManager);
186 // - contacts model
187 _contactsModel = new ContactsModelProxy(this, client->accountsModel());
188 _contactsProxy = new ContactsSortFilterModelProxy(_contactsModel, this);
189 rootContext->setContextProperty("contactsModel", _contactsProxy);
191 // - notifications model
192 _notificationsModel = new NotificationsModel(client, _window, this);
193 rootContext->setContextProperty("notificationsModel", _notificationsModel);
195 // - session model
196 _sessionModel = new SessionModel(client, _notificationsModel, this);
197 rootContext->setContextProperty("sessionsModel", _sessionModel);
199 connect(_sessionModel, SIGNAL(videoPreviewAvailable(const QString &, QGst::ElementPtr)), SLOT(onVideoPreviewAvailable(const QString &, QGst::ElementPtr)));
200 connect(_sessionModel, SIGNAL(videoAvailable(const QString &, QGst::ElementPtr)), SLOT(onVideoAvailable(const QString &, QGst::ElementPtr)));
201 connect(_sessionModel, SIGNAL(videoPreviewRemoved(const QString &, QGst::ElementPtr)), SLOT(onVideoPreviewRemoved(const QString &, QGst::ElementPtr)));
202 connect(_sessionModel, SIGNAL(videoRemoved(const QString &, QGst::ElementPtr)), SLOT(onVideoRemoved(const QString &, QGst::ElementPtr)));
204 _soundNotificator = new SoundNotificator(_app, this);
205 connect(_sessionModel, SIGNAL(startBeeping()), _soundNotificator, SLOT(onStartBeeping()));
206 connect(_sessionModel, SIGNAL(stopBeeping()), _soundNotificator, SLOT(onStopBeeping()));
207 connect(_sessionModel, SIGNAL(startRinging()), _soundNotificator, SLOT(onStartRinging()));
208 connect(_sessionModel, SIGNAL(stopRinging()), _soundNotificator, SLOT(onStopRinging()));
210 emit modelsReady();
211 qDebug() << "Makneto: Telepathy has been initialised";
214 void Makneto::onVideoPreviewAvailable(const QString &sessionId, QGst::ElementPtr videoOutElemPtr) {
215 //QDeclarativeContext* rootContext = _qmlView->rootContext();
217 QmlPainterVideoSurface *surface = _ownDeviceFactory->mapElementToSurface(videoOutElemPtr);
218 if (!surface) {
219 qWarning() << "Makneto: element for id" << sessionId << " is not created by own factory";
220 return;
222 //rootContext->setContextProperty(sessionId, surface);
223 qDebug() << "Makneto: videoSurfaceReady preview," << sessionId << "," << surface;
224 emit videoSurfaceReady("preview", sessionId, surface);
227 void Makneto::onVideoAvailable(const QString &sessionId, QGst::ElementPtr videoOutElemPtr) {
228 //QDeclarativeContext* rootContext = _qmlView->rootContext();
230 QmlPainterVideoSurface *surface = _ownDeviceFactory->mapElementToSurface(videoOutElemPtr);
231 if (!surface) {
232 qWarning() << "Makneto: element for id" << sessionId << " is not created by own factory";
233 return;
235 //rootContext->setContextProperty(sessionId, surface);
236 qDebug() << "Makneto: videoSurfaceReady video," << sessionId << "," << surface;
237 emit videoSurfaceReady("video", sessionId, surface);
240 void Makneto::onVideoPreviewRemoved(const QString &sessionId, QGst::ElementPtr videoOutElemPtr) {
241 _ownDeviceFactory->forgetElement(videoOutElemPtr);
242 emit videoSurfaceRemoved("preview", sessionId);
245 void Makneto::onVideoRemoved(const QString &sessionId, QGst::ElementPtr videoOutElemPtr) {
246 _ownDeviceFactory->forgetElement(videoOutElemPtr);
247 emit videoSurfaceRemoved("video", sessionId);
251 #include "makneto.moc"