implemented screen inhibit.
[simple-xmbc-rem] / src / mainwindow.cpp
index 8fcae21..5ef8368 100644 (file)
 #include "mainwindow.h"
 #include "ui_mainwindow.h"
 
-#include "constants.h"
 #include "setupdialog.h"
-#include "json.h"
+#include "xbmc.h"
+#include "constants.h"
 
-#include <QtCore/QCoreApplication>
-#include <QtCore/QSettings>
-#include <QtCore/QTimer>
-#include <QtCore/QTextStream>
+#include <QSettings>
+#include <QCoreApplication>
 
 #if defined(Q_OS_SYMBIAN) && defined(ORIENTATIONLOCK)
 #include <eikenv.h>
 #endif // Q_OS_SYMBIAN && ORIENTATIONLOCK
 
 MainWindow::MainWindow(QWidget *parent)
-    : QMainWindow(parent), ui(new Ui::MainWindow)
+    : QMainWindow(parent), m_ui(new Ui::MainWindow)
 {
-    ui->setupUi(this);
+    m_ui->setupUi(this);
+    m_xbmc = new Xbmc(this);
 
-    createStates();
-    createTransitions();
-    createConnections();
+#ifdef Q_WS_MAEMO_5
+    m_screenSaver = 0;
+#endif
 
-    stateMachine.setInitialState(disconnectedState);
-    QTimer::singleShot(0, &stateMachine, SLOT(start()));
+    setupScreen();
 }
 
 MainWindow::~MainWindow()
 {
-    delete ui;
+#ifdef Q_WS_MAEMO_5
+    delete m_screenSaver;
+#endif
+
+    delete m_xbmc;
+    delete m_ui;
+}
+
+void MainWindow::setupScreen()
+{
+#ifdef Q_WS_MAEMO_5
+    QSettings settings;
+
+    bool disable_screensaver = settings.value(SETUP_SCREEN_DISABLE_SCREENSAVER, SETUP_SCREEN_DISABLE_SCREENSAVER_DEFAULT).toBool();
+
+    if (disable_screensaver) {
+        if (m_screenSaver == 0) {
+            m_screenSaver = new QSystemScreenSaver(this);
+            bool result = m_screenSaver->setScreenSaverInhibit();
+            qDebug("disabling the screensaver: %s", (result?"ok":"failed"));
+        }
+    } else {
+        if (m_screenSaver) {
+            delete m_screenSaver;
+            m_screenSaver = 0;
+            qDebug("screensaver enabled");
+        }
+    }
+#endif
 }
 
 void MainWindow::setOrientation(ScreenOrientation orientation)
@@ -100,58 +126,81 @@ void MainWindow::on_actionSetup_triggered()
     if(dialog.exec() == QDialog::Accepted) {
         dialog.save();
     }
+
+    setupScreen();
 }
 
-void MainWindow::connectToServer()
+void MainWindow::on_rightBtn_clicked()
 {
-    // TODO: we asume the socket is not already connected
-    // 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
-    QSettings settings;
+    m_xbmc->actionRight();
+}
+
+void MainWindow::on_leftBtn_clicked()
+{
+    m_xbmc->actionLeft();
+}
 
-    serverSocket.connectToHost(settings.value(SETUP_XBMC_SERVER, SETUP_XBMC_SERVER_DEFAULT).toString(),
-                               settings.value(SETUP_XBMC_PORT, SETUP_XBMC_PORT_DEFAULT).toInt());
+void MainWindow::on_upBtn_clicked()
+{
+    m_xbmc->actionUp();
 }
 
-void MainWindow::disconnectFromServer()
+void MainWindow::on_downBtn_clicked()
 {
-    serverSocket.disconnectFromHost();
+    m_xbmc->actionDown();
 }
 
-void MainWindow::createStates()
+void MainWindow::on_playBtn_clicked()
 {
-    disconnectedState = new QState(&stateMachine);
-    disconnectedState->assignProperty(ui->networkButton, "text", "Connect");
-    disconnectedState->assignProperty(ui->networkButton, "enabled", true);
+    m_xbmc->actionPlay();
+}
 
-    connectedState = new QState(&stateMachine);
+void MainWindow::on_muteBtn_clicked()
+{
+    m_xbmc->actionMute();
+}
 
-    connectingState = new QState(connectedState);
-    connectingState->assignProperty(ui->networkButton, "text", "Connecting");
-    connectingState->assignProperty(ui->networkButton, "enabled", false);
+void MainWindow::on_volumeUpBtn_clicked()
+{
+    m_xbmc->actionVolumeUp();
+}
+
+void MainWindow::on_volumeDownBtn_clicked()
+{
+    m_xbmc->actionVolumeDown();
+}
 
-    normalState = new QState(connectedState);
-    normalState->assignProperty(ui->networkButton, "text", "Disconnect");
-    normalState->assignProperty(ui->networkButton, "enabled", true);
+void MainWindow::on_nextSubtitleBtn_clicked()
+{
+    m_xbmc->actionNextSubtitle();
+}
 
-    disconnectingState = new QState(connectedState);
-    disconnectingState->assignProperty(ui->networkButton, "text", "Disconnecting");
-    disconnectingState->assignProperty(ui->networkButton, "enabled", false);
+void MainWindow::on_nextLanguageBtn_clicked()
+{
+    m_xbmc->actionNextLanguage();
 }
 
-void MainWindow::createTransitions()
+void MainWindow::on_selectButton_clicked()
 {
-    disconnectedState->addTransition(ui->networkButton, SIGNAL(clicked()), connectingState);
+    m_xbmc->actionSelect();
+}
 
-    connectedState->addTransition(&serverSocket, SIGNAL(disconnected()), disconnectedState);
+void MainWindow::on_stopBtn_clicked()
+{
+    m_xbmc->actionStop();
+}
 
-    connectingState->addTransition(&serverSocket, SIGNAL(connected()), normalState);
-    connectingState->addTransition(&serverSocket, SIGNAL(error(QAbstractSocket::SocketError)), disconnectedState);
+void MainWindow::on_showGuiBtn_clicked()
+{
+    m_xbmc->actionShowGui();
+}
 
-    normalState->addTransition(ui->networkButton, SIGNAL(clicked()), disconnectingState);
+void MainWindow::on_contextMenuBtn_clicked()
+{
+    m_xbmc->actionContextMenu();
 }
 
-void MainWindow::createConnections()
+void MainWindow::on_sendEscKeyBtn_clicked()
 {
-    connect(connectingState, SIGNAL(entered()), this, SLOT(connectToServer()));
-    connect(disconnectingState, SIGNAL(entered()), this, SLOT(disconnectFromServer()));
+    m_xbmc->actionSendKeyEsc();
 }