phoneuis/bubblemanager2/tsrc/bubbletest2/bubbletester/mainwindow.cpp
branchGCC_SURGE
changeset 51 f39ed5e045e0
parent 40 bab96b7ed1a4
parent 46 bc5a64e5bc3c
equal deleted inserted replaced
40:bab96b7ed1a4 51:f39ed5e045e0
     1 /*!
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Animated icon.
       
    15 *
       
    16 */
       
    17 
       
    18 #include <QLocalServer>
       
    19 #include <QLocalSocket>
       
    20 #include <QMessageBox>
       
    21 #include <QLabel>
       
    22 #include <QStatusBar>
       
    23 #include <QDomDocument>
       
    24 
       
    25 #include "mainwindow.h"
       
    26 #include "ui_mainwindow.h"
       
    27 #include "bubbledata.h"
       
    28 
       
    29 
       
    30 MainWindow::MainWindow(QWidget *parent)
       
    31     : QMainWindow(parent),
       
    32       ui(new Ui::MainWindow),
       
    33       mServer(new QLocalServer(this)),
       
    34       mClientConnection(0),
       
    35       mStatusLabel(new QLabel(this)),
       
    36       mBubbleId("1")
       
    37 {
       
    38     ui->setupUi(this);
       
    39     statusBar()->addWidget(mStatusLabel,1);
       
    40 
       
    41     connect( ui->comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(bubbleChanged()) );
       
    42     connect( ui->lineEdit, SIGNAL(editingFinished()), this, SLOT(updateName()) );
       
    43     connect( ui->lineEdit_2, SIGNAL(editingFinished()), this, SLOT(updateNumber()) );
       
    44     connect( ui->comboBox_2, SIGNAL(currentIndexChanged(int)), this, SLOT(updateState()) );
       
    45     connect( ui->checkBox, SIGNAL(stateChanged(int)) , this, SLOT(updateDivert(int)) );
       
    46     connect( ui->checkBox_2, SIGNAL(stateChanged(int)) , this, SLOT(updateMute(int)) );
       
    47 
       
    48     mDataModel = new BubbleData(this);
       
    49 
       
    50     initUi();
       
    51 }
       
    52 
       
    53 MainWindow::~MainWindow()
       
    54 {
       
    55     delete ui;
       
    56     mServer->close();
       
    57     delete mServer;
       
    58 }
       
    59 
       
    60 
       
    61 bool MainWindow::start()
       
    62 {
       
    63     bool success = false;
       
    64     if (!mServer->listen("bubbletestserver")) {
       
    65         QMessageBox::critical( this,
       
    66                                tr("Bubble Tester"),
       
    67                                tr("Unable to start the server: %1.").arg(mServer->errorString()));
       
    68         return success;
       
    69     }
       
    70 
       
    71     mStatusLabel->setText(tr("Server running"));
       
    72 
       
    73     connect( mServer, SIGNAL(newConnection()), this, SLOT(clientConnected()));
       
    74     success = true;
       
    75     return success;
       
    76 }
       
    77 
       
    78 
       
    79 void MainWindow::clientConnected()
       
    80 {
       
    81     if (mClientConnection) {
       
    82         return;
       
    83     }
       
    84 
       
    85     // get the next pending connection
       
    86     mClientConnection = mServer->nextPendingConnection();
       
    87 
       
    88     mStatusLabel->setText(tr("Connected"));
       
    89 
       
    90     connect(mClientConnection, SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
       
    91 }
       
    92 
       
    93 void MainWindow::clientDisconnected()
       
    94 {
       
    95     mClientConnection = 0;
       
    96     connect((QLocalSocket*)sender(), SIGNAL(disconnected()), (QLocalSocket*)sender(), SLOT(deleteLater()));
       
    97     mStatusLabel->setText(tr("Disconnected"));
       
    98 }
       
    99 
       
   100 void MainWindow::sendData()
       
   101 {
       
   102     QByteArray block;
       
   103     QDataStream out(&block, QIODevice::WriteOnly);
       
   104 
       
   105     out << mDataModel->document().toString();
       
   106 
       
   107     if (mClientConnection) {
       
   108         mClientConnection->write(block);
       
   109     }
       
   110 }
       
   111 
       
   112 void MainWindow::initUi()
       
   113 {
       
   114     ui->lineEdit->setText(mDataModel->dataField(mBubbleId,"name"));
       
   115     ui->lineEdit_2->setText(mDataModel->dataField(mBubbleId,"number"));
       
   116 
       
   117     QString state = mDataModel->dataField(mBubbleId,"state");
       
   118     int index = ui->comboBox_2->findText(state);
       
   119     ui->comboBox_2->setCurrentIndex(index);
       
   120 
       
   121     QString divert = mDataModel->dataField(mBubbleId,"divert");
       
   122     ui->checkBox->setChecked((divert=="On"));
       
   123 
       
   124     QString mute = mDataModel->dataField("mute");
       
   125     ui->checkBox_2->setChecked((mute=="On"));
       
   126 }
       
   127 
       
   128 void MainWindow::bubbleChanged()
       
   129 {
       
   130     mBubbleId = ui->comboBox->currentText();
       
   131     initUi();
       
   132 }
       
   133 
       
   134 void MainWindow::updateName()
       
   135 {
       
   136     mDataModel->setDataField(mBubbleId,"name",ui->lineEdit->text());
       
   137     sendData();
       
   138 }
       
   139 
       
   140 void MainWindow::updateNumber()
       
   141 {
       
   142     mDataModel->setDataField(mBubbleId,"number",ui->lineEdit_2->text());
       
   143     sendData();
       
   144 }
       
   145 
       
   146 void MainWindow::updateState()
       
   147 {
       
   148     mDataModel->setDataField(mBubbleId,"state",ui->comboBox_2->currentText());
       
   149     sendData();
       
   150 }
       
   151 
       
   152 void MainWindow::updateDivert(int state)
       
   153 {
       
   154     if (!state) {
       
   155         mDataModel->setDataField(mBubbleId,"divert","Off");
       
   156     } else {
       
   157         mDataModel->setDataField(mBubbleId,"divert","On");
       
   158     }
       
   159 
       
   160     sendData();
       
   161 }
       
   162 
       
   163 void MainWindow::updateMute(int state)
       
   164 {
       
   165     if (!state) {
       
   166         mDataModel->setDataField("mute","Off");
       
   167     } else {
       
   168         mDataModel->setDataField("mute","On");
       
   169     }
       
   170 
       
   171     sendData();
       
   172 }
       
   173 
       
   174 
       
   175 
       
   176 
       
   177 
       
   178 
       
   179 
       
   180 
       
   181 
       
   182 
       
   183 
       
   184