Added code to connect to the XBMC server.
[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), ui(new Ui::MainWindow)
31 {
32     ui->setupUi(this);
33
34     connect(&serverSocket, SIGNAL(connected()), this, SLOT(serverConnected()));
35     connect(&serverSocket, SIGNAL(disconnected()), this, SLOT(serverDisconnected()));
36     connect(&serverSocket, SIGNAL(readyRead()), this, SLOT(serverDataAvailable()));
37     connect(&serverSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(serverError()));
38
39     connectToServer();
40 }
41
42 MainWindow::~MainWindow()
43 {
44     delete 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::connectToServer()
106 {
107     // TODO: we asume the socket is not already connected
108     // 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
109     QSettings settings;
110
111     serverSocket.connectToHost(settings.value(SETUP_XBMC_SERVER, SETUP_XBMC_SERVER_DEFAULT).toString(),
112                                settings.value(SETUP_XBMC_PORT, SETUP_XBMC_PORT_DEFAULT).toInt());
113 }
114
115 void MainWindow::disconnectFromServer()
116 {
117     serverSocket.disconnectFromHost();
118 }
119
120 void MainWindow::serverConnected()
121 {
122     ui->testButton->setEnabled(true);
123     ui->actionConnect->setChecked(true);
124 }
125
126 void MainWindow::serverDisconnected()
127 {
128     ui->testButton->setEnabled(false);
129     ui->actionConnect->setChecked(false);
130 }
131
132 void MainWindow::serverDataAvailable()
133 {
134     QTextStream stream(&serverSocket);
135     ui->textEdit->setText(stream.readAll());
136 }
137
138 void MainWindow::serverError()
139 {
140     ui->textEdit->setText(serverSocket.errorString());
141     ui->actionConnect->setChecked(serverSocket.state() == QTcpSocket::ConnectedState);
142     // clean the error window after 2 secs
143     QTimer::singleShot(2000, this, SLOT(cleanError()));
144 }
145
146 void MainWindow::cleanError()
147 {
148     ui->textEdit->clear();
149 }
150
151 void MainWindow::on_testButton_clicked()
152 {
153     QTextStream stream(&serverSocket);
154     stream << JsonEngine().playerGetActivePlayers();
155 }
156
157 void MainWindow::on_actionConnect_triggered(bool checked)
158 {
159     qDebug("menu state: %d", checked);
160     if (checked) {
161         connectToServer();
162     } else {
163         disconnectFromServer();
164     }
165 }