use http for sending commands
[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 <QCoreApplication>
18 #include <QSettings>
19 #include <QTimer>
20 #include <QTextStream>
21 #include <QNetworkReply>
22
23 #if defined(Q_OS_SYMBIAN) && defined(ORIENTATIONLOCK)
24 #include <eikenv.h>
25 #include <eikappui.h>
26 #include <aknenv.h>
27 #include <aknappui.h>
28 #endif // Q_OS_SYMBIAN && ORIENTATIONLOCK
29
30 /////////////////////////////////////////////////////////////////////
31 //
32 DisconnectedState::DisconnectedState(QAbstractButton *button, QTcpSocket* socket, QState *parent)
33     : QState(parent), m_socket(socket)
34 {
35     assignProperty(button, "text", "Connect");
36     assignProperty(button, "enabled", true);
37 }
38
39 void DisconnectedState::onEntry(QEvent */*event*/)
40 {
41     qDebug("DisconnectedState::onEntry");
42 }
43
44 void DisconnectedState::onExit(QEvent */*event*/)
45 {
46     qDebug("DisconnectedState::onExit");
47 }
48
49 /////////////////////////////////////////////////////////////////////
50 //
51 ConnectingState::ConnectingState(QAbstractButton *button, QTcpSocket* socket, QState *parent)
52     : QState(parent), m_socket(socket)
53 {
54     assignProperty(button, "text", "Connecting");
55     assignProperty(button, "enabled", false);
56 }
57
58 void ConnectingState::onEntry(QEvent */*event*/)
59 {
60     qDebug("ConnectingState::onEntry");
61
62     connect(m_socket, SIGNAL(connected()), this, SIGNAL(connected()));
63     connect(m_socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onSocketError(QAbstractSocket::SocketError)));
64
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
67     QSettings settings;
68
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());
71 }
72
73 void ConnectingState::onExit(QEvent */*event*/)
74 {
75     disconnect(m_socket, 0, this, 0);
76     qDebug("ConnectingState::onExit");
77 }
78
79 void ConnectingState::onSocketError(QAbstractSocket::SocketError)
80 {
81     qDebug("ConnectingState::onSocketError: %s", qPrintable(m_socket->errorString()));
82     emit failed();
83 }
84
85 /////////////////////////////////////////////////////////////////////
86 //
87 ConnectedState::ConnectedState(QAbstractButton *button, QTcpSocket* socket, QState *parent)
88     : QState(parent), m_socket(socket)
89 {
90     assignProperty(button, "text", "Disconnect");
91     assignProperty(button, "enabled", true);
92 }
93
94 void ConnectedState::onEntry(QEvent */*event*/)
95 {
96     qDebug("ConnectedState::onEntry");
97
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)));
101 }
102
103 void ConnectedState::onExit(QEvent */*event*/)
104 {
105     disconnect(m_socket, 0, this, 0);
106     qDebug("ConnectedState::onExit");
107 }
108
109 void ConnectedState::onSocketError(QAbstractSocket::SocketError)
110 {
111     qDebug("ConnectedState::onSocketError: %s", qPrintable(m_socket->errorString()));
112 }
113
114 void ConnectedState::onSocketData()
115 {
116     QTextStream stream(m_socket);
117     QString msg = stream.readAll();
118     qDebug("ConnectedState::onSocketData: %s", qPrintable(msg));
119 }
120
121 /////////////////////////////////////////////////////////////////////
122 //
123 DisconnectingState::DisconnectingState(QAbstractButton *button, QTcpSocket* socket, QState *parent)
124     : QState(parent), m_socket(socket)
125 {
126     assignProperty(button, "text", "Disconnecting");
127     assignProperty(button, "enabled", false);
128 }
129
130 void DisconnectingState::onEntry(QEvent */*event*/)
131 {
132     qDebug("DisconnectingState::onEntry");
133
134     connect(m_socket, SIGNAL(disconnected()), this, SIGNAL(disconnected()));
135     connect(m_socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onSocketError(QAbstractSocket::SocketError)));
136
137     if (m_socket->state() != QTcpSocket::UnconnectedState) {
138         qDebug("DisconnectingState::disconnectFromHost");
139         m_socket->disconnectFromHost();
140     }
141     else {
142         qDebug("DisconnectingState::disconnect");
143         QTimer::singleShot(0, this, SIGNAL(disconnected()));
144     }
145 }
146
147 void DisconnectingState::onExit(QEvent */*event*/)
148 {
149     disconnect(m_socket, 0, this, 0);
150     qDebug("DisconnectingState::onExit");
151 }
152
153 void DisconnectingState::onSocketError(QAbstractSocket::SocketError)
154 {
155     qDebug("DisconnectingState::onSocketError: %s", qPrintable(m_socket->errorString()));
156 }
157
158 /////////////////////////////////////////////////////////////////////
159 //
160 MainWindow::MainWindow(QWidget *parent)
161     : QMainWindow(parent), m_ui(new Ui::MainWindow)
162 {
163     m_ui->setupUi(this);
164     m_manager = new QNetworkAccessManager(this);
165
166     createStates();
167     createTransitions();
168     createConnections();
169
170     m_stateMachine.setInitialState(m_disconnectedState);
171     QTimer::singleShot(0, &m_stateMachine, SLOT(start()));
172 }
173
174 MainWindow::~MainWindow()
175 {
176     delete m_ui;
177     delete m_manager;
178 }
179
180 void MainWindow::setOrientation(ScreenOrientation orientation)
181 {
182 #ifdef Q_OS_SYMBIAN
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());
189         TRAPD(error,
190             if (appUi)
191                 appUi->SetOrientationL(uiOrientation);
192         );
193         Q_UNUSED(error)
194 #else // ORIENTATIONLOCK
195         qWarning("'ORIENTATIONLOCK' needs to be defined on Symbian when locking the orientation.");
196 #endif // ORIENTATIONLOCK
197     }
198 #elif defined(Q_WS_MAEMO_5)
199     Qt::WidgetAttribute attribute;
200     switch (orientation) {
201     case ScreenOrientationLockPortrait:
202         attribute = Qt::WA_Maemo5PortraitOrientation;
203         break;
204     case ScreenOrientationLockLandscape:
205         attribute = Qt::WA_Maemo5LandscapeOrientation;
206         break;
207     case ScreenOrientationAuto:
208     default:
209         attribute = Qt::WA_Maemo5AutoOrientation;
210         break;
211     }
212     setAttribute(attribute, true);
213 #else // Q_OS_SYMBIAN
214     Q_UNUSED(orientation);
215 #endif // Q_OS_SYMBIAN
216 }
217
218 void MainWindow::showExpanded()
219 {
220 #ifdef Q_OS_SYMBIAN
221     showFullScreen();
222 #elif defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6)
223     showMaximized();
224 #else
225     show();
226 #endif
227 }
228
229 void MainWindow::on_actionSetup_triggered()
230 {
231     SetupDialog dialog;
232     dialog.load();
233     if(dialog.exec() == QDialog::Accepted) {
234         dialog.save();
235     }
236 }
237
238 void MainWindow::createStates()
239 {
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);
244 }
245
246 void MainWindow::createTransitions()
247 {
248     m_disconnectedState->addTransition(m_ui->networkButton, SIGNAL(clicked()), m_connectingState);
249
250     m_connectingState->addTransition(m_connectingState, SIGNAL(connected()), m_connectedState);
251     m_connectingState->addTransition(m_connectingState, SIGNAL(failed()), m_disconnectedState);
252
253     m_connectedState->addTransition(m_ui->networkButton, SIGNAL(clicked()), m_disconnectingState);
254     m_connectedState->addTransition(m_connectedState, SIGNAL(disconnected()), m_disconnectedState);
255
256     m_disconnectingState->addTransition(m_disconnectingState, SIGNAL(disconnected()), m_disconnectedState);
257 }
258
259 void MainWindow::createConnections()
260 {
261     connect(m_manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onNetworAccesskManagerReplyFinished(QNetworkReply*)));
262 }
263
264 void MainWindow::on_videoPlayerStateBtn_clicked()
265 {
266     QTextStream stream(&m_serverSocket);
267     stream << JsonEngine().videoPlayerState();
268 }
269
270 void MainWindow::on_videoPlayerPlayPauseBtn_clicked()
271 {
272     QTextStream stream(&m_serverSocket);
273     stream << JsonEngine().videoPlayerPlayPause();
274 }
275
276 void MainWindow::on_videoPlayerStopBtn_clicked()
277 {
278     QTextStream stream(&m_serverSocket);
279     stream << JsonEngine().serialize("VideoPlayer.Stop");
280 }
281
282 void MainWindow::on_videoPlayerSkipPreviousBtn_clicked()
283 {
284     QTextStream stream(&m_serverSocket);
285     stream << JsonEngine().serialize("VideoPlayer.SkipPrevious");
286 }
287
288 void MainWindow::on_videoPlayerSkipNextBtn_clicked()
289 {
290     QTextStream stream(&m_serverSocket);
291     stream << JsonEngine().serialize("VideoPlayer.SkipNext");
292 }
293
294 void MainWindow::on_videoPlayerBigSkipBackwardBtn_clicked()
295 {
296     QTextStream stream(&m_serverSocket);
297     stream << JsonEngine().serialize("VideoPlayer.BigSkipBackward");
298 }
299
300 void MainWindow::on_videoPlayerBigSkipForwardBtn_clicked()
301 {
302     QTextStream stream(&m_serverSocket);
303     stream << JsonEngine().serialize("VideoPlayer.BigSkipForward");
304 }
305
306 void MainWindow::on_videoPlayerSmallSkipBackwardBtn_clicked()
307 {
308     QTextStream stream(&m_serverSocket);
309     stream << JsonEngine().serialize("VideoPlayer.SmallSkipBackward");
310 }
311
312 void MainWindow::on_videoPlayerSmallSkipForwardBtn_clicked()
313 {
314     QTextStream stream(&m_serverSocket);
315     stream << JsonEngine().serialize("VideoPlayer.SmallSkipForward");
316 }
317
318 void MainWindow::on_videoPlayerGetTimeBtn_clicked()
319 {
320     QTextStream stream(&m_serverSocket);
321     stream << JsonEngine().serialize("VideoPlayer.GetTime");
322 }
323
324 void MainWindow::on_xbmcGetVolumeBtn_clicked()
325 {
326     QTextStream stream(&m_serverSocket);
327     stream << JsonEngine().serialize("XBMC.GetVolume");
328 }
329
330 void MainWindow::on_xbmcToggleMuteBtn_clicked()
331 {
332     QTextStream stream(&m_serverSocket);
333     stream << JsonEngine().serialize("XBMC.ToggleMute");
334 }
335
336 void MainWindow::on_xbmcPlayBtn_clicked()
337 {
338     QTextStream stream(&m_serverSocket);
339     stream << JsonEngine().serialize("XBMC.Play");
340 }
341
342 void MainWindow::on_xbmcQuitBtn_clicked()
343 {
344     QTextStream stream(&m_serverSocket);
345     stream << JsonEngine().serialize("XBMC.Quit");
346 }
347
348 void MainWindow::on_playerGetActivePlayers_clicked()
349 {
350     QTextStream stream(&m_serverSocket);
351     stream << JsonEngine().playerGetActivePlayers();
352 }
353
354 void MainWindow::on_actionLeftBtn_clicked()
355 {
356     m_manager->get(QNetworkRequest(QUrl(QString("http://localhost:8080/xbmcCmds/xbmcHttp?command=Action(%1)").arg(ACTION_MOVE_LEFT))));
357 }
358
359 void MainWindow::onNetworAccesskManagerReplyFinished(QNetworkReply *reply)
360 {
361     QTextStream stream(reply);
362     QString msg = stream.readAll();
363     qDebug("MainWindow::onNetworAccesskManagerReplyFinished: %s", qPrintable(msg));
364
365     reply->deleteLater();
366 }
367
368 void MainWindow::on_actionRightBtn_clicked()
369 {
370     m_manager->get(QNetworkRequest(QUrl(QString("http://localhost:8080/xbmcCmds/xbmcHttp?command=Action(%1)").arg(ACTION_MOVE_RIGHT))));
371 }
372
373 void MainWindow::on_actionUpBtn_clicked()
374 {
375     m_manager->get(QNetworkRequest(QUrl(QString("http://localhost:8080/xbmcCmds/xbmcHttp?command=Action(%1)").arg(ACTION_MOVE_UP))));
376 }
377
378 void MainWindow::on_actionDownBtn_clicked()
379 {
380     m_manager->get(QNetworkRequest(QUrl(QString("http://localhost:8080/xbmcCmds/xbmcHttp?command=Action(%1)").arg(ACTION_MOVE_DOWN))));
381 }
382
383 void MainWindow::on_actionEnterBtn_clicked()
384 {
385     m_manager->get(QNetworkRequest(QUrl(QString("http://localhost:8080/xbmcCmds/xbmcHttp?command=Action(%1)").arg(ACTION_ENTER))));
386 }
387
388 void MainWindow::on_actionPlayBtn_clicked()
389 {
390     m_manager->get(QNetworkRequest(QUrl(QString("http://localhost:8080/xbmcCmds/xbmcHttp?command=Action(%1)").arg(ACTION_PLAYER_PLAY))));
391 }
392
393 void MainWindow::on_actionSelectItemBtn_clicked()
394 {
395     m_manager->get(QNetworkRequest(QUrl(QString("http://localhost:8080/xbmcCmds/xbmcHttp?command=Action(%1)").arg(ACTION_SELECT_ITEM))));
396 }
397
398 void MainWindow::on_actionParentDirBtn_clicked()
399 {
400     m_manager->get(QNetworkRequest(QUrl(QString("http://localhost:8080/xbmcCmds/xbmcHttp?command=Action(%1)").arg(ACTION_PARENT_DIR))));
401 }