Switched to using QStateMachine.
[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     createStates();
35     createTransitions();
36     createConnections();
37
38     stateMachine.setInitialState(disconnectedState);
39     QTimer::singleShot(0, &stateMachine, SLOT(start()));
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::createStates()
121 {
122     disconnectedState = new QState(&stateMachine);
123     disconnectedState->assignProperty(ui->networkButton, "text", "Connect");
124     disconnectedState->assignProperty(ui->networkButton, "enabled", true);
125
126     connectedState = new QState(&stateMachine);
127
128     connectingState = new QState(connectedState);
129     connectingState->assignProperty(ui->networkButton, "text", "Connecting");
130     connectingState->assignProperty(ui->networkButton, "enabled", false);
131
132     normalState = new QState(connectedState);
133     normalState->assignProperty(ui->networkButton, "text", "Disconnect");
134     normalState->assignProperty(ui->networkButton, "enabled", true);
135
136     disconnectingState = new QState(connectedState);
137     disconnectingState->assignProperty(ui->networkButton, "text", "Disconnecting");
138     disconnectingState->assignProperty(ui->networkButton, "enabled", false);
139 }
140
141 void MainWindow::createTransitions()
142 {
143     disconnectedState->addTransition(ui->networkButton, SIGNAL(clicked()), connectingState);
144
145     connectedState->addTransition(&serverSocket, SIGNAL(disconnected()), disconnectedState);
146
147     connectingState->addTransition(&serverSocket, SIGNAL(connected()), normalState);
148     connectingState->addTransition(&serverSocket, SIGNAL(error(QAbstractSocket::SocketError)), disconnectedState);
149
150     normalState->addTransition(ui->networkButton, SIGNAL(clicked()), disconnectingState);
151 }
152
153 void MainWindow::createConnections()
154 {
155     connect(connectingState, SIGNAL(entered()), this, SLOT(connectToServer()));
156     connect(disconnectingState, SIGNAL(entered()), this, SLOT(disconnectFromServer()));
157 }