1 // checksum 0xfd0b version 0x20001
3 This file was generated by the Mobile Qt Application wizard of Qt Creator.
4 MainWindow is a convenience class containing mobile device specific code
5 such as screen orientation handling.
6 It is recommended not to modify this file, since newer versions of Qt Creator
7 may offer an updated version of it.
10 #include "mainwindow.h"
11 #include "ui_mainwindow.h"
13 #include "constants.h"
14 #include "setupdialog.h"
17 #include <QCoreApplication>
20 #include <QTextStream>
21 #include <QNetworkReply>
23 #if defined(Q_OS_SYMBIAN) && defined(ORIENTATIONLOCK)
28 #endif // Q_OS_SYMBIAN && ORIENTATIONLOCK
30 /////////////////////////////////////////////////////////////////////
32 DisconnectedState::DisconnectedState(QAbstractButton *button, QTcpSocket* socket, QState *parent)
33 : QState(parent), m_socket(socket)
35 assignProperty(button, "text", "Connect");
36 assignProperty(button, "enabled", true);
39 void DisconnectedState::onEntry(QEvent */*event*/)
41 qDebug("DisconnectedState::onEntry");
44 void DisconnectedState::onExit(QEvent */*event*/)
46 qDebug("DisconnectedState::onExit");
49 /////////////////////////////////////////////////////////////////////
51 ConnectingState::ConnectingState(QAbstractButton *button, QTcpSocket* socket, QState *parent)
52 : QState(parent), m_socket(socket)
54 assignProperty(button, "text", "Connecting");
55 assignProperty(button, "enabled", false);
58 void ConnectingState::onEntry(QEvent */*event*/)
60 qDebug("ConnectingState::onEntry");
62 connect(m_socket, SIGNAL(connected()), this, SIGNAL(connected()));
63 connect(m_socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onSocketError(QAbstractSocket::SocketError)));
65 // TODO: we asume the socket is not already connected
66 // TODO: we should add code to do nothing if the connection is ok, or close and open a new one if the server or port changed
69 m_socket->connectToHost(settings.value(SETUP_XBMC_SERVER, SETUP_XBMC_SERVER_DEFAULT).toString(),
70 settings.value(SETUP_XBMC_PORT, SETUP_XBMC_PORT_DEFAULT).toInt());
73 void ConnectingState::onExit(QEvent */*event*/)
75 disconnect(m_socket, 0, this, 0);
76 qDebug("ConnectingState::onExit");
79 void ConnectingState::onSocketError(QAbstractSocket::SocketError)
81 qDebug("ConnectingState::onSocketError: %s", qPrintable(m_socket->errorString()));
85 /////////////////////////////////////////////////////////////////////
87 ConnectedState::ConnectedState(QAbstractButton *button, QTcpSocket* socket, QState *parent)
88 : QState(parent), m_socket(socket)
90 assignProperty(button, "text", "Disconnect");
91 assignProperty(button, "enabled", true);
94 void ConnectedState::onEntry(QEvent */*event*/)
96 qDebug("ConnectedState::onEntry");
98 connect(m_socket, SIGNAL(readyRead()), this, SLOT(onSocketData()));
99 connect(m_socket, SIGNAL(disconnected()), this, SIGNAL(disconnected()));
100 connect(m_socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onSocketError(QAbstractSocket::SocketError)));
103 void ConnectedState::onExit(QEvent */*event*/)
105 disconnect(m_socket, 0, this, 0);
106 qDebug("ConnectedState::onExit");
109 void ConnectedState::onSocketError(QAbstractSocket::SocketError)
111 qDebug("ConnectedState::onSocketError: %s", qPrintable(m_socket->errorString()));
114 void ConnectedState::onSocketData()
116 QTextStream stream(m_socket);
117 QString msg = stream.readAll();
118 qDebug("ConnectedState::onSocketData: %s", qPrintable(msg));
121 /////////////////////////////////////////////////////////////////////
123 DisconnectingState::DisconnectingState(QAbstractButton *button, QTcpSocket* socket, QState *parent)
124 : QState(parent), m_socket(socket)
126 assignProperty(button, "text", "Disconnecting");
127 assignProperty(button, "enabled", false);
130 void DisconnectingState::onEntry(QEvent */*event*/)
132 qDebug("DisconnectingState::onEntry");
134 connect(m_socket, SIGNAL(disconnected()), this, SIGNAL(disconnected()));
135 connect(m_socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onSocketError(QAbstractSocket::SocketError)));
137 if (m_socket->state() != QTcpSocket::UnconnectedState) {
138 qDebug("DisconnectingState::disconnectFromHost");
139 m_socket->disconnectFromHost();
142 qDebug("DisconnectingState::disconnect");
143 QTimer::singleShot(0, this, SIGNAL(disconnected()));
147 void DisconnectingState::onExit(QEvent */*event*/)
149 disconnect(m_socket, 0, this, 0);
150 qDebug("DisconnectingState::onExit");
153 void DisconnectingState::onSocketError(QAbstractSocket::SocketError)
155 qDebug("DisconnectingState::onSocketError: %s", qPrintable(m_socket->errorString()));
158 /////////////////////////////////////////////////////////////////////
160 MainWindow::MainWindow(QWidget *parent)
161 : QMainWindow(parent), m_ui(new Ui::MainWindow)
164 m_manager = new QNetworkAccessManager(this);
170 m_stateMachine.setInitialState(m_disconnectedState);
171 QTimer::singleShot(0, &m_stateMachine, SLOT(start()));
174 MainWindow::~MainWindow()
180 void MainWindow::setOrientation(ScreenOrientation orientation)
183 if (orientation != ScreenOrientationAuto) {
184 #if defined(ORIENTATIONLOCK)
185 const CAknAppUiBase::TAppUiOrientation uiOrientation =
186 (orientation == ScreenOrientationLockPortrait) ? CAknAppUi::EAppUiOrientationPortrait
187 : CAknAppUi::EAppUiOrientationLandscape;
188 CAknAppUi* appUi = dynamic_cast<CAknAppUi*> (CEikonEnv::Static()->AppUi());
191 appUi->SetOrientationL(uiOrientation);
194 #else // ORIENTATIONLOCK
195 qWarning("'ORIENTATIONLOCK' needs to be defined on Symbian when locking the orientation.");
196 #endif // ORIENTATIONLOCK
198 #elif defined(Q_WS_MAEMO_5)
199 Qt::WidgetAttribute attribute;
200 switch (orientation) {
201 case ScreenOrientationLockPortrait:
202 attribute = Qt::WA_Maemo5PortraitOrientation;
204 case ScreenOrientationLockLandscape:
205 attribute = Qt::WA_Maemo5LandscapeOrientation;
207 case ScreenOrientationAuto:
209 attribute = Qt::WA_Maemo5AutoOrientation;
212 setAttribute(attribute, true);
213 #else // Q_OS_SYMBIAN
214 Q_UNUSED(orientation);
215 #endif // Q_OS_SYMBIAN
218 void MainWindow::showExpanded()
222 #elif defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6)
229 void MainWindow::on_actionSetup_triggered()
233 if(dialog.exec() == QDialog::Accepted) {
238 void MainWindow::createStates()
240 m_disconnectedState = new DisconnectedState(m_ui->networkButton, &m_serverSocket, &m_stateMachine);
241 m_connectingState = new ConnectingState(m_ui->networkButton, &m_serverSocket, &m_stateMachine);
242 m_connectedState = new ConnectedState(m_ui->networkButton, &m_serverSocket, &m_stateMachine);
243 m_disconnectingState = new DisconnectingState(m_ui->networkButton, &m_serverSocket, &m_stateMachine);
246 void MainWindow::createTransitions()
248 m_disconnectedState->addTransition(m_ui->networkButton, SIGNAL(clicked()), m_connectingState);
250 m_connectingState->addTransition(m_connectingState, SIGNAL(connected()), m_connectedState);
251 m_connectingState->addTransition(m_connectingState, SIGNAL(failed()), m_disconnectedState);
253 m_connectedState->addTransition(m_ui->networkButton, SIGNAL(clicked()), m_disconnectingState);
254 m_connectedState->addTransition(m_connectedState, SIGNAL(disconnected()), m_disconnectedState);
256 m_disconnectingState->addTransition(m_disconnectingState, SIGNAL(disconnected()), m_disconnectedState);
259 void MainWindow::createConnections()
261 connect(m_manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onNetworAccesskManagerReplyFinished(QNetworkReply*)));
264 void MainWindow::on_videoPlayerStateBtn_clicked()
266 QTextStream stream(&m_serverSocket);
267 stream << JsonEngine().videoPlayerState();
270 void MainWindow::on_videoPlayerPlayPauseBtn_clicked()
272 QTextStream stream(&m_serverSocket);
273 stream << JsonEngine().videoPlayerPlayPause();
276 void MainWindow::on_videoPlayerStopBtn_clicked()
278 QTextStream stream(&m_serverSocket);
279 stream << JsonEngine().serialize("VideoPlayer.Stop");
282 void MainWindow::on_videoPlayerSkipPreviousBtn_clicked()
284 QTextStream stream(&m_serverSocket);
285 stream << JsonEngine().serialize("VideoPlayer.SkipPrevious");
288 void MainWindow::on_videoPlayerSkipNextBtn_clicked()
290 QTextStream stream(&m_serverSocket);
291 stream << JsonEngine().serialize("VideoPlayer.SkipNext");
294 void MainWindow::on_videoPlayerBigSkipBackwardBtn_clicked()
296 QTextStream stream(&m_serverSocket);
297 stream << JsonEngine().serialize("VideoPlayer.BigSkipBackward");
300 void MainWindow::on_videoPlayerBigSkipForwardBtn_clicked()
302 QTextStream stream(&m_serverSocket);
303 stream << JsonEngine().serialize("VideoPlayer.BigSkipForward");
306 void MainWindow::on_videoPlayerSmallSkipBackwardBtn_clicked()
308 QTextStream stream(&m_serverSocket);
309 stream << JsonEngine().serialize("VideoPlayer.SmallSkipBackward");
312 void MainWindow::on_videoPlayerSmallSkipForwardBtn_clicked()
314 QTextStream stream(&m_serverSocket);
315 stream << JsonEngine().serialize("VideoPlayer.SmallSkipForward");
318 void MainWindow::on_videoPlayerGetTimeBtn_clicked()
320 QTextStream stream(&m_serverSocket);
321 stream << JsonEngine().serialize("VideoPlayer.GetTime");
324 void MainWindow::on_xbmcGetVolumeBtn_clicked()
326 QTextStream stream(&m_serverSocket);
327 stream << JsonEngine().serialize("XBMC.GetVolume");
330 void MainWindow::on_xbmcToggleMuteBtn_clicked()
332 QTextStream stream(&m_serverSocket);
333 stream << JsonEngine().serialize("XBMC.ToggleMute");
336 void MainWindow::on_xbmcPlayBtn_clicked()
338 QTextStream stream(&m_serverSocket);
339 stream << JsonEngine().serialize("XBMC.Play");
342 void MainWindow::on_xbmcQuitBtn_clicked()
344 QTextStream stream(&m_serverSocket);
345 stream << JsonEngine().serialize("XBMC.Quit");
348 void MainWindow::on_playerGetActivePlayers_clicked()
350 QTextStream stream(&m_serverSocket);
351 stream << JsonEngine().playerGetActivePlayers();
354 void MainWindow::on_actionLeftBtn_clicked()
356 m_manager->get(QNetworkRequest(QUrl(QString("http://localhost:8080/xbmcCmds/xbmcHttp?command=Action(%1)").arg(ACTION_MOVE_LEFT))));
359 void MainWindow::onNetworAccesskManagerReplyFinished(QNetworkReply *reply)
361 QTextStream stream(reply);
362 QString msg = stream.readAll();
363 qDebug("MainWindow::onNetworAccesskManagerReplyFinished: %s", qPrintable(msg));
365 reply->deleteLater();
368 void MainWindow::on_actionRightBtn_clicked()
370 m_manager->get(QNetworkRequest(QUrl(QString("http://localhost:8080/xbmcCmds/xbmcHttp?command=Action(%1)").arg(ACTION_MOVE_RIGHT))));
373 void MainWindow::on_actionUpBtn_clicked()
375 m_manager->get(QNetworkRequest(QUrl(QString("http://localhost:8080/xbmcCmds/xbmcHttp?command=Action(%1)").arg(ACTION_MOVE_UP))));
378 void MainWindow::on_actionDownBtn_clicked()
380 m_manager->get(QNetworkRequest(QUrl(QString("http://localhost:8080/xbmcCmds/xbmcHttp?command=Action(%1)").arg(ACTION_MOVE_DOWN))));
383 void MainWindow::on_actionEnterBtn_clicked()
385 m_manager->get(QNetworkRequest(QUrl(QString("http://localhost:8080/xbmcCmds/xbmcHttp?command=Action(%1)").arg(ACTION_ENTER))));
388 void MainWindow::on_actionPlayBtn_clicked()
390 m_manager->get(QNetworkRequest(QUrl(QString("http://localhost:8080/xbmcCmds/xbmcHttp?command=Action(%1)").arg(ACTION_PLAYER_PLAY))));
393 void MainWindow::on_actionSelectItemBtn_clicked()
395 m_manager->get(QNetworkRequest(QUrl(QString("http://localhost:8080/xbmcCmds/xbmcHttp?command=Action(%1)").arg(ACTION_SELECT_ITEM))));
398 void MainWindow::on_actionParentDirBtn_clicked()
400 m_manager->get(QNetworkRequest(QUrl(QString("http://localhost:8080/xbmcCmds/xbmcHttp?command=Action(%1)").arg(ACTION_PARENT_DIR))));