make zooming more smooth (zooming on background)
[makneto-zunavac1.git] / src / ui-mobile / makneto.cpp
blobf8d1d387d2eb390fcf12e425777fd8322e327454
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"
59 #include "wheelarea.h"
60 #include "view-graber.h"
62 Makneto::Makneto(QApplication *app) : _app(app),
63 _qmlView(0),
64 _window(0),
65 _contactsModel(0),
66 _contactsProxy(0),
67 _sessionModel(0),
68 _notificationsModel(0),
69 _trayIcon(0),
70 _ownDeviceFactory(0) {
72 // register data types to make its enums accessible from qml
73 qmlRegisterUncreatableType<MaknetoBackend::AccountsModel > ("org.makneto", 0, 1, "AccountsModel", QLatin1String("This type is only exported for its enums"));
74 qmlRegisterUncreatableType<MaknetoBackend::Session > ("org.makneto", 0, 1, "Session", QLatin1String("This type is only exported for its enums"));
75 qmlRegisterUncreatableType<SessionModel > ("org.makneto", 0, 1, "SessionModel", QLatin1String("This type is only exported for its enums"));
76 qmlRegisterUncreatableType<NotificationsModel > ("org.makneto", 0, 1, "NotificationsModel", QLatin1String("This type is only exported for its enums"));
77 qmlRegisterUncreatableType<NotificationItem > ("org.makneto", 0, 1, "NotificationItem", QLatin1String("This type is only exported for its enums"));
78 qmlRegisterUncreatableType<TelepathyTypes > ("org.makneto", 0, 1, "TelepathyTypes", QLatin1String("This type is only exported for its enums"));
80 /* register QtGstQmlSink items for playing videos */
81 qmlRegisterType<QmlGstVideoItem > ("Gst", 1, 0, "VideoItem");
82 qmlRegisterType<QmlPainterVideoSurface > ("Gst", 1, 0, "VideoSurface");
84 qmlRegisterType<WheelArea>("MyTools", 1, 0, "WheelArea");
85 qmlRegisterType<ViewGraber>("MyTools", 1, 0, "ViewGraber");
87 _qmlView = new QDeclarativeView();
88 _window = new MainWindow(_app, this);
90 connect(_qmlView, SIGNAL(statusChanged(QDeclarativeView::Status)),
91 this, SLOT(onQmlStatusChanged(QDeclarativeView::Status)));
93 _qmlView->setResizeMode(QDeclarativeView::SizeRootObjectToView);
94 _window->setCentralWidget(_qmlView);
98 Makneto::~Makneto() {
99 MaknetoBackend::DeviceElementFactory::destroyAllFactories();
101 if (_qmlView)
102 _qmlView->deleteLater();
103 if (_window)
104 _window->deleteLater();
105 if (_notificationsModel)
106 _notificationsModel->deleteLater();
107 if (_contactsProxy)
108 _contactsProxy->deleteLater();
109 if (_contactsModel)
110 _contactsModel->deleteLater();
111 if (_sessionModel)
112 _sessionModel->deleteLater();
113 if (_soundNotificator)
114 _soundNotificator->deleteLater();
115 if (_presenceManager)
116 _presenceManager->deleteLater();
119 bool Makneto::init() {
120 QDeclarativeContext* rootContext = _qmlView->rootContext();
121 rootContext->setContextProperty("makneto", this);
123 _qmlView->setSource(QUrl("qrc:/declarative/mainview.qml"));
124 if (_qmlView->status() == QDeclarativeView::Error)
125 return false;
127 return true;
130 void Makneto::onQmlStatusChanged(QDeclarativeView::Status status) {
131 switch (status) {
132 case QDeclarativeView::Null:
133 break;
134 case QDeclarativeView::Ready:
135 qDebug() << "Makneto: QML ready...";
136 onViewReady();
137 break;
138 case QDeclarativeView::Loading:
139 qDebug() << "Makneto: Loading QML...";
140 break;
141 case QDeclarativeView::Error:
142 qWarning() << "Makneto: Error occurs while loading qml (ui) file. Exiting";
143 qWarning() << _qmlView->errors();
144 _app->exit(-1);
145 break;
146 default:
147 qWarning() << "Makneto: udefind QML status" << status;
151 void Makneto::onViewReady() {
152 showWindow();
154 _ownDeviceFactory = new MobileGstElementFactory(_qmlView);
155 MaknetoBackend::DeviceElementFactory::installFactory(_ownDeviceFactory);
157 MaknetoBackend::TelepathyInitializer *tpInit = new MaknetoBackend::TelepathyInitializer();
159 connect(tpInit,
160 SIGNAL(finished(MaknetoBackend::TelepathyClient *)),
161 SLOT(onTelepathyInitializerFinished(MaknetoBackend::TelepathyClient *)));
163 tpInit->createClient(_app->applicationName());
166 void Makneto::showWindow() {
167 #ifdef Q_OS_SYMBIAN
168 qDebug() << "running on Symbian, show fullscreen";
169 _window->showFullScreen();
170 #elif defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6)
171 qDebug() << "running on Maemo, show maximized";
172 _window->showMaximized();
173 #else
174 qDebug() << "Makneto: running environment is not Symbian nor Maemo - show in window";
175 _window->show();
176 #endif
179 void Makneto::onTelepathyInitializerFinished(MaknetoBackend::TelepathyClient* client) {
181 QDeclarativeContext* rootContext = _qmlView->rootContext();
182 //QObject *rootObject = dynamic_cast<QObject*> (_qmlView->rootObject());
184 // initialize models
185 // - account model direct from backend
186 rootContext->setContextProperty("accountsModel", client->accountsModel());
188 _presenceManager = new PresenceManager(client->accountsModel(), this);
189 rootContext->setContextProperty("presenceManager", _presenceManager);
191 // - contacts model
192 _contactsModel = new ContactsModelProxy(this, client->accountsModel());
193 _contactsProxy = new ContactsSortFilterModelProxy(_contactsModel, this);
194 rootContext->setContextProperty("contactsModel", _contactsProxy);
196 // - notifications model
197 _notificationsModel = new NotificationsModel(client, _window, this);
198 rootContext->setContextProperty("notificationsModel", _notificationsModel);
200 // - session model
201 _sessionModel = new SessionModel(client, _notificationsModel, this);
202 rootContext->setContextProperty("sessionsModel", _sessionModel);
204 connect(_sessionModel, SIGNAL(videoPreviewAvailable(const QString &, QGst::ElementPtr)), SLOT(onVideoPreviewAvailable(const QString &, QGst::ElementPtr)));
205 connect(_sessionModel, SIGNAL(videoAvailable(const QString &, QGst::ElementPtr)), SLOT(onVideoAvailable(const QString &, QGst::ElementPtr)));
206 connect(_sessionModel, SIGNAL(videoPreviewRemoved(const QString &, QGst::ElementPtr)), SLOT(onVideoPreviewRemoved(const QString &, QGst::ElementPtr)));
207 connect(_sessionModel, SIGNAL(videoRemoved(const QString &, QGst::ElementPtr)), SLOT(onVideoRemoved(const QString &, QGst::ElementPtr)));
209 _soundNotificator = new SoundNotificator(_app, this);
210 connect(_sessionModel, SIGNAL(startBeeping()), _soundNotificator, SLOT(onStartBeeping()));
211 connect(_sessionModel, SIGNAL(stopBeeping()), _soundNotificator, SLOT(onStopBeeping()));
212 connect(_sessionModel, SIGNAL(startRinging()), _soundNotificator, SLOT(onStartRinging()));
213 connect(_sessionModel, SIGNAL(stopRinging()), _soundNotificator, SLOT(onStopRinging()));
215 emit modelsReady();
216 qDebug() << "Makneto: Telepathy has been initialised";
219 void Makneto::onVideoPreviewAvailable(const QString &sessionId, QGst::ElementPtr videoOutElemPtr) {
220 //QDeclarativeContext* rootContext = _qmlView->rootContext();
222 QmlPainterVideoSurface *surface = _ownDeviceFactory->mapElementToSurface(videoOutElemPtr);
223 if (!surface) {
224 qWarning() << "Makneto: element for id" << sessionId << " is not created by own factory";
225 return;
227 //rootContext->setContextProperty(sessionId, surface);
228 qDebug() << "Makneto: videoSurfaceReady preview," << sessionId << "," << surface;
229 emit videoSurfaceReady("preview", sessionId, surface);
232 void Makneto::onVideoAvailable(const QString &sessionId, QGst::ElementPtr videoOutElemPtr) {
233 //QDeclarativeContext* rootContext = _qmlView->rootContext();
235 QmlPainterVideoSurface *surface = _ownDeviceFactory->mapElementToSurface(videoOutElemPtr);
236 if (!surface) {
237 qWarning() << "Makneto: element for id" << sessionId << " is not created by own factory";
238 return;
240 //rootContext->setContextProperty(sessionId, surface);
241 qDebug() << "Makneto: videoSurfaceReady video," << sessionId << "," << surface;
242 emit videoSurfaceReady("video", sessionId, surface);
245 void Makneto::onVideoPreviewRemoved(const QString &sessionId, QGst::ElementPtr videoOutElemPtr) {
246 _ownDeviceFactory->forgetElement(videoOutElemPtr);
247 emit videoSurfaceRemoved("preview", sessionId);
250 void Makneto::onVideoRemoved(const QString &sessionId, QGst::ElementPtr videoOutElemPtr) {
251 _ownDeviceFactory->forgetElement(videoOutElemPtr);
252 emit videoSurfaceRemoved("video", sessionId);
256 #include "makneto.moc"