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>
22 #if defined(Q_OS_SYMBIAN) && defined(ORIENTATIONLOCK)
27 #endif // Q_OS_SYMBIAN && ORIENTATIONLOCK
29 MainWindow::MainWindow(QWidget *parent)
30 : QMainWindow(parent), m_ui(new Ui::MainWindow)
38 m_stateMachine.setInitialState(m_disconnectedState);
39 QTimer::singleShot(0, &m_stateMachine, SLOT(start()));
42 MainWindow::~MainWindow()
47 void MainWindow::setOrientation(ScreenOrientation orientation)
50 if (orientation != ScreenOrientationAuto) {
51 #if defined(ORIENTATIONLOCK)
52 const CAknAppUiBase::TAppUiOrientation uiOrientation =
53 (orientation == ScreenOrientationLockPortrait) ? CAknAppUi::EAppUiOrientationPortrait
54 : CAknAppUi::EAppUiOrientationLandscape;
55 CAknAppUi* appUi = dynamic_cast<CAknAppUi*> (CEikonEnv::Static()->AppUi());
58 appUi->SetOrientationL(uiOrientation);
61 #else // ORIENTATIONLOCK
62 qWarning("'ORIENTATIONLOCK' needs to be defined on Symbian when locking the orientation.");
63 #endif // ORIENTATIONLOCK
65 #elif defined(Q_WS_MAEMO_5)
66 Qt::WidgetAttribute attribute;
67 switch (orientation) {
68 case ScreenOrientationLockPortrait:
69 attribute = Qt::WA_Maemo5PortraitOrientation;
71 case ScreenOrientationLockLandscape:
72 attribute = Qt::WA_Maemo5LandscapeOrientation;
74 case ScreenOrientationAuto:
76 attribute = Qt::WA_Maemo5AutoOrientation;
79 setAttribute(attribute, true);
81 Q_UNUSED(orientation);
82 #endif // Q_OS_SYMBIAN
85 void MainWindow::showExpanded()
89 #elif defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6)
96 void MainWindow::on_actionSetup_triggered()
100 if(dialog.exec() == QDialog::Accepted) {
105 void MainWindow::createStates()
107 m_disconnectedState = new DisconnectedState(m_ui->networkButton, &m_serverSocket, &m_stateMachine);
108 m_connectingState = new ConnectingState(m_ui->networkButton, &m_serverSocket, &m_stateMachine);
109 m_connectedState = new ConnectedState(m_ui->networkButton, &m_serverSocket, &m_stateMachine);
110 m_disconnectingState = new DisconnectingState(m_ui->networkButton, &m_serverSocket, &m_stateMachine);
113 void MainWindow::createTransitions()
115 m_disconnectedState->addTransition(m_ui->networkButton, SIGNAL(clicked()), m_connectingState);
117 m_connectingState->addTransition(m_connectingState, SIGNAL(connected()), m_connectedState);
118 m_connectingState->addTransition(m_connectingState, SIGNAL(failed()), m_disconnectedState);
120 m_connectedState->addTransition(m_ui->networkButton, SIGNAL(clicked()), m_disconnectingState);
121 m_connectedState->addTransition(m_connectedState, SIGNAL(disconnected()), m_disconnectedState);
123 m_disconnectingState->addTransition(m_disconnectingState, SIGNAL(disconnected()), m_disconnectedState);
126 void MainWindow::createConnections()
130 /////////////////////////////////////////////////////////////////////
132 DisconnectedState::DisconnectedState(QAbstractButton *button, QTcpSocket* socket, QState *parent)
133 : QState(parent), m_socket(socket)
135 assignProperty(button, "text", "Connect");
136 assignProperty(button, "enabled", true);
139 void DisconnectedState::onEntry(QEvent */*event*/)
141 qDebug("DisconnectedState::onEntry");
144 void DisconnectedState::onExit(QEvent */*event*/)
146 qDebug("DisconnectedState::onExit");
149 /////////////////////////////////////////////////////////////////////
151 ConnectingState::ConnectingState(QAbstractButton *button, QTcpSocket* socket, QState *parent)
152 : QState(parent), m_socket(socket)
154 assignProperty(button, "text", "Connecting");
155 assignProperty(button, "enabled", false);
158 void ConnectingState::onEntry(QEvent */*event*/)
160 qDebug("ConnectingState::onEntry");
162 connect(m_socket, SIGNAL(connected()), this, SIGNAL(connected()));
163 connect(m_socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onSocketError(QAbstractSocket::SocketError)));
165 // TODO: we asume the socket is not already connected
166 // 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
169 m_socket->connectToHost(settings.value(SETUP_XBMC_SERVER, SETUP_XBMC_SERVER_DEFAULT).toString(),
170 settings.value(SETUP_XBMC_PORT, SETUP_XBMC_PORT_DEFAULT).toInt());
173 void ConnectingState::onExit(QEvent */*event*/)
175 disconnect(m_socket, 0, this, 0);
176 qDebug("ConnectingState::onExit");
179 void ConnectingState::onSocketError(QAbstractSocket::SocketError)
181 qDebug("ConnectingState::onSocketError: %s", qPrintable(m_socket->errorString()));
185 /////////////////////////////////////////////////////////////////////
187 ConnectedState::ConnectedState(QAbstractButton *button, QTcpSocket* socket, QState *parent)
188 : QState(parent), m_socket(socket)
190 assignProperty(button, "text", "Disconnect");
191 assignProperty(button, "enabled", true);
194 void ConnectedState::onEntry(QEvent */*event*/)
196 qDebug("ConnectedState::onEntry");
198 connect(m_socket, SIGNAL(readyRead()), this, SLOT(onSocketData()));
199 connect(m_socket, SIGNAL(disconnected()), this, SIGNAL(disconnected()));
200 connect(m_socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onSocketError(QAbstractSocket::SocketError)));
202 QTextStream stream(m_socket);
203 stream << JsonEngine().playerGetActivePlayers();
205 qDebug("ConnectedState::onEntry - request sent");
208 void ConnectedState::onExit(QEvent */*event*/)
210 disconnect(m_socket, 0, this, 0);
211 qDebug("ConnectedState::onExit");
214 void ConnectedState::onSocketError(QAbstractSocket::SocketError)
216 qDebug("ConnectedState::onSocketError: %s", qPrintable(m_socket->errorString()));
219 void ConnectedState::onSocketData()
221 QTextStream stream(m_socket);
222 QString msg = stream.readAll();
223 qDebug("ConnectedState::onSocketData: %s", qPrintable(msg));
226 /////////////////////////////////////////////////////////////////////
228 DisconnectingState::DisconnectingState(QAbstractButton *button, QTcpSocket* socket, QState *parent)
229 : QState(parent), m_socket(socket)
231 assignProperty(button, "text", "Disconnecting");
232 assignProperty(button, "enabled", false);
235 void DisconnectingState::onEntry(QEvent */*event*/)
237 qDebug("DisconnectingState::onEntry");
239 connect(m_socket, SIGNAL(disconnected()), this, SIGNAL(disconnected()));
240 connect(m_socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onSocketError(QAbstractSocket::SocketError)));
242 if (m_socket->state() != QTcpSocket::UnconnectedState) {
243 qDebug("DisconnectingState::disconnectFromHost");
244 m_socket->disconnectFromHost();
247 qDebug("DisconnectingState::disconnect");
248 QTimer::singleShot(0, this, SIGNAL(disconnected()));
252 void DisconnectingState::onExit(QEvent */*event*/)
254 disconnect(m_socket, 0, this, 0);
255 qDebug("DisconnectingState::onExit");
258 void DisconnectingState::onSocketError(QAbstractSocket::SocketError)
260 qDebug("DisconnectingState::onSocketError: %s", qPrintable(m_socket->errorString()));