320f7fdabd8632ea276bad54c22aed702e2e7c52
[simple-xmbc-rem] / src / mainwindow.cpp
1 // checksum 0xfd0b version 0x20001
2 /*
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.
8 */
9
10 #include "mainwindow.h"
11 #include "ui_mainwindow.h"
12
13 #include "constants.h"
14 #include "setupdialog.h"
15 #include "json.h"
16
17 #include <QtCore/QCoreApplication>
18 #include <QtCore/QSettings>
19 #include <QtCore/QTimer>
20 #include <QtCore/QTextStream>
21
22 #if defined(Q_OS_SYMBIAN) && defined(ORIENTATIONLOCK)
23 #include <eikenv.h>
24 #include <eikappui.h>
25 #include <aknenv.h>
26 #include <aknappui.h>
27 #endif // Q_OS_SYMBIAN && ORIENTATIONLOCK
28
29 MainWindow::MainWindow(QWidget *parent)
30     : QMainWindow(parent), m_ui(new Ui::MainWindow)
31 {
32     m_ui->setupUi(this);
33
34     createStates();
35     createTransitions();
36     createConnections();
37
38     m_stateMachine.setInitialState(m_disconnectedState);
39     QTimer::singleShot(0, &m_stateMachine, SLOT(start()));
40 }
41
42 MainWindow::~MainWindow()
43 {
44     delete m_ui;
45 }
46
47 void MainWindow::setOrientation(ScreenOrientation orientation)
48 {
49 #ifdef Q_OS_SYMBIAN
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());
56         TRAPD(error,
57             if (appUi)
58                 appUi->SetOrientationL(uiOrientation);
59         );
60         Q_UNUSED(error)
61 #else // ORIENTATIONLOCK
62         qWarning("'ORIENTATIONLOCK' needs to be defined on Symbian when locking the orientation.");
63 #endif // ORIENTATIONLOCK
64     }
65 #elif defined(Q_WS_MAEMO_5)
66     Qt::WidgetAttribute attribute;
67     switch (orientation) {
68     case ScreenOrientationLockPortrait:
69         attribute = Qt::WA_Maemo5PortraitOrientation;
70         break;
71     case ScreenOrientationLockLandscape:
72         attribute = Qt::WA_Maemo5LandscapeOrientation;
73         break;
74     case ScreenOrientationAuto:
75     default:
76         attribute = Qt::WA_Maemo5AutoOrientation;
77         break;
78     }
79     setAttribute(attribute, true);
80 #else // Q_OS_SYMBIAN
81     Q_UNUSED(orientation);
82 #endif // Q_OS_SYMBIAN
83 }
84
85 void MainWindow::showExpanded()
86 {
87 #ifdef Q_OS_SYMBIAN
88     showFullScreen();
89 #elif defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6)
90     showMaximized();
91 #else
92     show();
93 #endif
94 }
95
96 void MainWindow::on_actionSetup_triggered()
97 {
98     SetupDialog dialog;
99     dialog.load();
100     if(dialog.exec() == QDialog::Accepted) {
101         dialog.save();
102     }
103 }
104
105 void MainWindow::createStates()
106 {
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);
111 }
112
113 void MainWindow::createTransitions()
114 {
115     m_disconnectedState->addTransition(m_ui->networkButton, SIGNAL(clicked()), m_connectingState);
116
117     m_connectingState->addTransition(m_connectingState, SIGNAL(connected()), m_connectedState);
118     m_connectingState->addTransition(m_connectingState, SIGNAL(failed()), m_disconnectedState);
119
120     m_connectedState->addTransition(m_ui->networkButton, SIGNAL(clicked()), m_disconnectingState);
121     m_connectedState->addTransition(m_connectedState, SIGNAL(disconnected()), m_disconnectedState);
122
123     m_disconnectingState->addTransition(m_disconnectingState, SIGNAL(disconnected()), m_disconnectedState);
124 }
125
126 void MainWindow::createConnections()
127 {
128 }
129
130 /////////////////////////////////////////////////////////////////////
131 //
132 DisconnectedState::DisconnectedState(QAbstractButton *button, QTcpSocket* socket, QState *parent)
133     : QState(parent), m_socket(socket)
134 {
135     assignProperty(button, "text", "Connect");
136     assignProperty(button, "enabled", true);
137 }
138
139 void DisconnectedState::onEntry(QEvent */*event*/)
140 {
141     qDebug("DisconnectedState::onEntry");
142 }
143
144 void DisconnectedState::onExit(QEvent */*event*/)
145 {
146     qDebug("DisconnectedState::onExit");
147 }
148
149 /////////////////////////////////////////////////////////////////////
150 //
151 ConnectingState::ConnectingState(QAbstractButton *button, QTcpSocket* socket, QState *parent)
152     : QState(parent), m_socket(socket)
153 {
154     assignProperty(button, "text", "Connecting");
155     assignProperty(button, "enabled", false);
156 }
157
158 void ConnectingState::onEntry(QEvent */*event*/)
159 {
160     qDebug("ConnectingState::onEntry");
161
162     connect(m_socket, SIGNAL(connected()), this, SIGNAL(connected()));
163     connect(m_socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onSocketError(QAbstractSocket::SocketError)));
164
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
167     QSettings settings;
168
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());
171 }
172
173 void ConnectingState::onExit(QEvent */*event*/)
174 {
175     disconnect(m_socket, 0, this, 0);
176     qDebug("ConnectingState::onExit");
177 }
178
179 void ConnectingState::onSocketError(QAbstractSocket::SocketError)
180 {
181     qDebug("ConnectingState::onSocketError: %s", qPrintable(m_socket->errorString()));
182     emit failed();
183 }
184
185 /////////////////////////////////////////////////////////////////////
186 //
187 ConnectedState::ConnectedState(QAbstractButton *button, QTcpSocket* socket, QState *parent)
188     : QState(parent), m_socket(socket)
189 {
190     assignProperty(button, "text", "Disconnect");
191     assignProperty(button, "enabled", true);
192
193 }
194
195 void ConnectedState::onEntry(QEvent */*event*/)
196 {
197     qDebug("ConnectedState::onEntry");
198
199     connect(m_socket, SIGNAL(disconnected()), this, SIGNAL(disconnected()));
200     connect(m_socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onSocketError(QAbstractSocket::SocketError)));
201 }
202
203 void ConnectedState::onExit(QEvent */*event*/)
204 {
205     disconnect(m_socket, 0, this, 0);
206     qDebug("ConnectedState::onExit");
207 }
208
209 void ConnectedState::onSocketError(QAbstractSocket::SocketError)
210 {
211     qDebug("ConnectedState::onSocketError: %s", qPrintable(m_socket->errorString()));
212 }
213
214 /////////////////////////////////////////////////////////////////////
215 //
216 DisconnectingState::DisconnectingState(QAbstractButton *button, QTcpSocket* socket, QState *parent)
217     : QState(parent), m_socket(socket)
218 {
219     assignProperty(button, "text", "Disconnecting");
220     assignProperty(button, "enabled", false);
221 }
222
223 void DisconnectingState::onEntry(QEvent */*event*/)
224 {
225     qDebug("DisconnectingState::onEntry");
226
227     connect(m_socket, SIGNAL(disconnected()), this, SIGNAL(disconnected()));
228     connect(m_socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onSocketError(QAbstractSocket::SocketError)));
229
230     if (m_socket->state() != QTcpSocket::UnconnectedState) {
231         qDebug("DisconnectingState::disconnectFromHost");
232         m_socket->disconnectFromHost();
233     }
234     else {
235         qDebug("DisconnectingState::disconnect");
236         QTimer::singleShot(0, this, SIGNAL(disconnected()));
237     }
238 }
239
240 void DisconnectingState::onExit(QEvent */*event*/)
241 {
242     disconnect(m_socket, 0, this, 0);
243     qDebug("DisconnectingState::onExit");
244 }
245
246 void DisconnectingState::onSocketError(QAbstractSocket::SocketError)
247 {
248     qDebug("DisconnectingState::onSocketError: %s", qPrintable(m_socket->errorString()));
249 }