aboutsummaryrefslogtreecommitdiffstats
path: root/src/control.cpp
blob: d4732b776c0fa8d16994d19df5c22df439299be8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
/****************************************************************************
**
** This file is part of the Qt Extended Opensource Package.
**
** Copyright (C) 2009 Trolltech ASA.
**
** Contact: Qt Extended Information (info@qtextended.org)
**
** This file may be used under the terms of the GNU General Public License
** version 2.0 as published by the Free Software Foundation and appearing
** in the file LICENSE.GPL included in the packaging of this file.
**
** Please review the following information to ensure GNU General Public
** Licensing requirements will be met:
**     http://www.fsf.org/licensing/licenses/info/GPLv2.html.
**
**
****************************************************************************/

#include "control.h"
#include <qpushbutton.h>
#include <qslider.h>
#include <qcheckbox.h>
#include <qcombobox.h>
#include <qmessagebox.h>
#include <qfiledialog.h>
#include <Qt>
#include <qbuffer.h>
#include <qtimer.h>
#include <qevent.h>
#include <QFileInfo>
#include <QFile>
#include <QDir>
#include <QHeaderView>

#define TWO_BYTE_MAX 65535
#define FOUR_CHAR 4
#define FOUR_BYTE_MAX 0x7FFFFFFF
#define EIGHT_CHAR 8
#define HEX_BASE 16

#define SERVICE "org.ofono.phonesim"

ControlWidget::ControlWidget(const QString &ruleFile, Control *parent)
    : QWidget(), p(parent)
{
    QDBusConnection bus = QDBusConnection::sessionBus();
    QFileInfo info( ruleFile );
    QString specFile = info.absolutePath() + "/GSMSpecification.xml";
    if (!QFile::exists(specFile))
        specFile = QDir::currentPath() + "/GSMSpecification.xml";
    translator = new AtTranslator(specFile);

    ui = new Ui_ControlBase;
    ui->setupUi(this);

    script = new Script(this, ui);

    if (!bus.registerService(SERVICE)) {
        qWarning() << bus.lastError().message();
        exit(-1);
    }

    bus.registerObject("/", script, QDBusConnection::ExportAllSlots);

    connect(ui->hsSignalQuality, &QSlider::valueChanged, this, &ControlWidget::sendSQ);
    connect(ui->hsBatteryCharge, &QSlider::valueChanged, this, &ControlWidget::sendBC);
    connect(ui->hsBatteryCharging, &QCheckBox::stateChanged, this, &ControlWidget::chargingChanged);
    connect(ui->pbSelectOperator, &QPushButton::clicked, this, &ControlWidget::sendOPS);
    connect(ui->pbRegistration, &QPushButton::clicked, this, &ControlWidget::sendREG);
    connect(ui->pbSendCellBroadcast, &QPushButton::clicked, this, &ControlWidget::sendCBM);
    connect(ui->pbSendSMSMessage, &QPushButton::clicked, this, &ControlWidget::sendSMSMessage);
    connect(ui->pbFile, &QPushButton::clicked, this, &ControlWidget::selectFile);
    connect(ui->pbSendSMSDatagram, &QPushButton::clicked, this, &ControlWidget::sendSMSDatagram);
    connect(ui->pbIncomingCall, &QPushButton::clicked, this, &ControlWidget::sendCall);
    connect(ui->openSpecButton, &QPushButton::clicked, this, &ControlWidget::resetTranslator);
    connect(ui->atCheckBox, &QCheckBox::clicked, this, &ControlWidget::atChanged);
    connect(ui->pbAddMessage, &QPushButton::clicked, this, &ControlWidget::addVoicemail);
    connect(ui->pbRemoveMessage, &QPushButton::clicked, this, &ControlWidget::delVoicemail);
    connect(ui->pbNotifyUDH, &QPushButton::clicked, this, &ControlWidget::sendVMNotify);
    connect(ui->pbNotifyUDHEnhanced, &QPushButton::clicked, this, &ControlWidget::sendEVMNotify);
    connect(ui->pbSendUSSD, &QPushButton::clicked, this, &ControlWidget::sendUSSD);
    connect(ui->pbCancelUSSD, &QPushButton::clicked, this, &ControlWidget::cancelUSSD);
    connect(ui->cbSimInserted, &QCheckBox::clicked, this, &ControlWidget::simInsertRemove);
    connect(ui->pbStart, &QPushButton::clicked, this, &ControlWidget::simAppStart);
    connect(ui->pbAbort, &QPushButton::clicked, this, &ControlWidget::simAppAbort);
    connect(ui->pbReset, &QPushButton::clicked, this, &ControlWidget::modemSilentReset);
    connect(ui->pbSendGNSSData, &QPushButton::clicked, this, &ControlWidget::sendGNSSData);
    connect(ui->pbGNSSDefault, &QPushButton::clicked, this, &ControlWidget::setDefaultGNSSData);
    connect(ui->pbSendNotif, &QPushButton::clicked, this, &ControlWidget::sendCSSN);
    connect(ui->pbAlerting, &QPushButton::clicked, this, &ControlWidget::setStateAlerting);
    connect(ui->pbActive, &QPushButton::clicked, this, &ControlWidget::setStateConnected);
    connect(ui->pbHangup, &QPushButton::clicked, this, &ControlWidget::setStateHangup);

    QStringList headers;
    headers << "Sender" << "Priority" << "Notification Status";
    ui->twMessageList->setHorizontalHeaderLabels( headers );
    ui->twMessageList->verticalHeader()->hide();

    handleNewApp();
    handleCSSNNotif();

    show();
}

void ControlWidget::closeEvent(QCloseEvent *event)
{
    event->ignore();
    hide();
}

ControlWidget::~ControlWidget()
{
    delete script;
    delete ui;
    delete translator;
}

Control::Control(const QString& ruleFile, SimRules *sr, QObject *parent)
        : HardwareManipulator(sr, parent),
        widget(new ControlWidget(ruleFile, this))
{
    connect(widget, &ControlWidget::unsolicitedCommand, this, &Control::unsolicitedCommand);
    connect(widget, &ControlWidget::command, this, &Control::command);
    connect(widget, &ControlWidget::variableChanged, this, &Control::variableChanged);
    connect(widget, &ControlWidget::switchTo, this, &Control::switchTo);
    connect(widget, &ControlWidget::startIncomingCall, this, &Control::startIncomingCall);
    connect(widget, &ControlWidget::stateChangedToAlerting, this, &Control::stateChangedToAlerting);
    connect(widget, &ControlWidget::stateChangedToConnected, this, &Control::stateChangedToConnected);
    connect(widget, &ControlWidget::stateChangedToHangup, this, &Control::stateChangedToHangup);
}

Control::~Control()
{
    delete widget;
}

void Control::callManagement( QList<CallInfo> *list )
{
    int row = 0;
    bool enableCSSU = false;
    bool enableCSSI = false;
    static QString state_table[] = { tr("Active"), tr("Held"),
                                      tr("Dialing"), tr("Alerting"),
                                      tr("Incoming"), tr("Waiting"),
                                      tr("Disconnected"), tr("Swapping") };

    widget->clearCallView();

    foreach( CallInfo i, *list ) {
        QString param[5];

        if ( i.incoming && !enableCSSU )
            enableCSSU = true;
        if ( !i.incoming && !enableCSSI )
            enableCSSI = true;

        param[0].setNum( i.id );
        param[1] = i.number;
        param[2] = state_table[i.state];
        param[3] = i.name;
        param[4] = i.incoming ? tr("Incoming") : tr("Outgoing");

        widget->updateCallView( param, row );
        row++;
    }

    widget->setCssiEnabled( enableCSSI );
    widget->setCssuEnabled( enableCSSU );
}

void ControlWidget::setStateConnected()
{
    emit stateChangedToConnected();
}

void ControlWidget::setStateAlerting()
{
    emit stateChangedToAlerting();
}

void ControlWidget::setStateHangup()
{
    QList <QTableWidgetItem *> items = ui->twCallMgt->selectedItems();

    foreach ( QTableWidgetItem *item, items )
    {
        int row = item->row();
        if ( row >= 0 )
        {
            QTableWidgetItem *itemCallId = ui->twCallMgt->item( row, 0 );
            if ( itemCallId != 0 )
            {
                QString strid = itemCallId->text();
                int id = strid.toInt();
                emit stateChangedToHangup( id );
            }
        }
    }
}

void ControlWidget::setCssiEnabled( bool enableCSSI )
{
    ui->cbCSSI->setEnabled( enableCSSI );
}

void ControlWidget::setCssuEnabled( bool enableCSSU )
{
    ui->cbCSSU->setEnabled( enableCSSU );
}

void ControlWidget::clearCallView()
{
    ui->twCallMgt->setRowCount(0);
    ui->twCallMgt->clearContents();
}

void ControlWidget::updateCallView( QString callParameters [5], int row )
{
    if ( row > ui->twCallMgt->rowCount() - 1 )
        ui->twCallMgt->insertRow( row );

    for ( int i = 0; i < 5; i++ )
    {
        QTableWidgetItem *item = new QTableWidgetItem( callParameters[i] );
        item->setFlags( item->flags() & ~Qt::ItemIsEditable );
        ui->twCallMgt->setItem( row, i, item );
    }
}

void Control::setPhoneNumber( const QString &number )
{
    widget->setWindowTitle("Phonesim - Number: " + number);
}

void Control::warning( const QString &title, const QString &message )
{
    QMessageBox::warning(widget, title, message, "OK");
}

void ControlWidget::handleCSSNNotif()
{
    ui->cbCSSU->setEnabled( false );
    ui->cbCSSI->setEnabled( false );

    ui->cbCSSU->insertItem( 0, "" );
    ui->cbCSSU->insertItem( 1, "0 - forwarded", 0 );
    ui->cbCSSU->insertItem( 3, "2 - on hold", 2 );
    ui->cbCSSU->insertItem( 4, "3 - retrieved", 3 );
    ui->cbCSSU->insertItem( 5, "4 - multiparty", 4 );

    ui->cbCSSI->insertItem( 0, "" );
    ui->cbCSSI->insertItem( 3, "2 - forwarded", 2 );
    ui->cbCSSI->insertItem( 6, "5 - outgoing barred", 5 );
    ui->cbCSSI->insertItem( 7, "6 - incoming barred", 6 );
}

void ControlWidget::sendCSSN()
{
    QVariant v = ui->cbCSSU->itemData( ui->cbCSSU->currentIndex() );

    if ( v.canConvert<int>() && ui->cbCSSU->isEnabled() )
        emit unsolicitedCommand( "+CSSU: "+QString::number( v.toInt() ) );

    v = ui->cbCSSI->itemData( ui->cbCSSI->currentIndex() );

    if ( v.canConvert<int>() && ui->cbCSSI->isEnabled() )
        emit unsolicitedCommand( "+CSSI: "+QString::number( v.toInt() ) );
}

void ControlWidget::sendSQ()
{
    emit variableChanged("SQ",QString::number(ui->hsSignalQuality->value())+",99");
    emit unsolicitedCommand("+CSQ: "+QString::number(ui->hsSignalQuality->value())+",99");
}

void ControlWidget::sendBC()
{
    bool charging = ui->hsBatteryCharging->checkState() == Qt::Checked;
    emit variableChanged("BC",QString::number(charging)+","+QString::number(ui->hsBatteryCharge->value()));
    emit unsolicitedCommand("+CBC: "+QString::number(charging)+","+QString::number(ui->hsBatteryCharge->value()));
}

void ControlWidget::chargingChanged(int state)
{
    bool charging = state  == Qt::Checked;
    ui->hsBatteryCharge->setEnabled(!charging);
    emit variableChanged("BC",QString::number(charging)+","+QString::number(ui->hsBatteryCharge->value()));
    emit unsolicitedCommand("+CBC: "+QString::number(charging)+","+QString::number(ui->hsBatteryCharge->value()));
}

void ControlWidget::sendOPS()
{
    emit variableChanged("OP", ui->leOperatorName->text());
    emit unsolicitedCommand("+CREG: 5");
}

void ControlWidget::sendREG()
{
    QString commandString = "+CREG: "+QString::number(ui->cbRegistrationStatus->currentIndex());

    if ( ui->chkLocationInfo->checkState() == Qt::Checked ) {
        bool ok;

        p->convertString(ui->leLAC->text(),TWO_BYTE_MAX,FOUR_CHAR,HEX_BASE, &ok);
        if (!ok) {
            p->warning(tr("Invalid LAC"),
                    tr("Location Area Code must be 4 hex digits long"));
            return;
        }

        p->convertString(ui->leCellID->text(),FOUR_BYTE_MAX,EIGHT_CHAR,HEX_BASE, &ok);
        if (!ok) {
            p->warning(tr("Invalid Cell ID"),
                    tr("Cell ID must be 8 hex digits long"));
            return;
        }

        commandString.append(",\"" + ui->leLAC->text() + "\",\"" + ui->leCellID->text() + "\"");
    }

    emit unsolicitedCommand(commandString);
}

void ControlWidget::sendCBM()
{
    p->constructCBMessage(ui->leMessageCode->text(),ui->cbGeographicalScope->currentIndex(),
                       ui->leUpdateNumber->text(),ui->leChannel->text(),
                       ui->cbLanguage->currentIndex(),ui->teContent->toPlainText());
}

void ControlWidget::sendSMSMessage()
{
    if (ui->leMessageSender->text().isEmpty()) {
        p->warning(tr("Invalid Sender"),
                tr("Sender must not be empty"));
        return;
    }

    if (ui->leSMSServiceCenter->text().isEmpty() || ui->leSMSServiceCenter->text().contains(QRegExp("\\D"))) {
        p->warning(tr("Invalid Service Center"),
                tr("Service Center must not be empty and contain "
                   "only digits"));
        return;
    }
    p->constructSMSMessage(ui->leSMSClass->text().toInt(), ui->leMessageSender->text(), ui->leSMSServiceCenter->text(), ui->teSMSText->toPlainText());
}

void ControlWidget::setDefaultGNSSData()
{
    ui->teGNSStext->clear();

    ui->teGNSStext->append("<?xml version=\"1.0\" ?>");
    ui->teGNSStext->append("<pos>");
    ui->teGNSStext->append("  <pos_err>");
    ui->teGNSStext->append("    <err_reason literal=\"not_enough_gps_satellites\" />");
    ui->teGNSStext->append("  </pos_err>");
    ui->teGNSStext->append("</pos>");
}

void ControlWidget::sendGNSSData()
{
    QStringList xml = ui->teGNSStext->toPlainText().split("\n");

    foreach ( QString line, xml )
    {
        if (!line.isEmpty()) {
            QString cposr =  "+CPOSR: " + line;

            emit unsolicitedCommand(cposr);
            handleFromData( cposr );
        }
    }
}

void ControlWidget::sendMGD()
{
    emit command("AT+CMGD=1");
}

void ControlWidget::selectFile()
{
    ui->leFile->setText(QFileDialog::getOpenFileName(this, "Select File", "/home", "Files (*.*)"));
}

void ControlWidget::sendSMSDatagram()
{
    QString dstPortStr = ui->leDstPort->text();
    if ( dstPortStr.contains(QRegExp("\\D")) ) {
        p->warning(tr("Invalid Port"), tr("Port number can contain only digits" ));
        return;
    }
    int dst = dstPortStr.toInt();

    QString srcPortStr = ui->leSrcPort->text();
    if ( srcPortStr.contains(QRegExp("\\D")) ) {
        p->warning(tr("Invalid Port"), tr("Port number can contain only digits" ));
        return;
    }
    int src = srcPortStr.toInt();

    QString sender = ui->leDatagramSender->text();

    //obtain data from either file or text edit
    QFile file(ui->leFile->text());
    QByteArray data;
    if ( ui->chkAppData->checkState() ==Qt::Checked ) {
        if ( !file.open(QIODevice::ReadOnly) ) {
            p->warning(tr("Invalid File"), tr("File could not be opened"));
            return;
        }
        data = file.readAll();
    } else {
       data = ui->teAppData->toPlainText().toUtf8();
    }

    //obtain Content-Type if required
    QByteArray contentType;
    if ( ui->chkContentType->checkState() == Qt::Checked ) {
        QString contentTypeStr = ui->leContentType->text();
        if ( contentTypeStr.length() == 0 ) {
            p->warning( tr("Invalid ContentType"),
                     tr("Please check the Content Type") );
            return;
        }
        contentType = contentTypeStr.toUtf8();
    }

    //construct and place SMS datagram in SMSList
    p->constructSMSDatagram(src, dst, sender, data, contentType);
}

void ControlWidget::sendCall()
{
    QString number;
    QString calledNumber;
    QString name;

    if (ui->cbCaller->isChecked())
        number = ui->leCaller->text();

    if (ui->cbCalledLine->isChecked())
        calledNumber = ui->leCalledLine->text();

    if (ui->cbCallerName->isChecked())
        name = ui->leCallerName->text();

    emit startIncomingCall( number, calledNumber, name );
}

void ControlWidget::handleFromData( const QString& cmd )
{
    ui->atViewer->append(cmd);
    /*QStringList dataList = cmd.split("\n");
    QString dataItem;
    foreach( dataItem, dataList ){
        if( dataItem != "" ){
            ui->atViewer->append(translator->translate(dataItem));
            ui->atViewer->append(dataItem);
        }
    }*/
}

void Control::handleFromData( const QString& cmd )
{
    widget->handleFromData(cmd);
}

void ControlWidget::handleToData( const QString& cmd )
{
    QStringList dataList = cmd.split("\n");
    QString dataItem;
    foreach( dataItem, dataList ){
        if( dataItem != "" ){
            ui->atViewer->append( dataItem + " : " + translator->translateCommand(dataItem) );
        }
    }

    if ( cmd.startsWith("AT+CUSD=") ) {
        int comma = cmd.indexOf( QChar(',') );
        if ( comma >= 0 ) {
            QString content = cmd.mid(comma + 1);
            content.remove( QChar('"') );
            ui->lblResponse->setText( content );
        }

        if ( cmd.startsWith("AT+CUSD=2") )
            ui->lblResponse->setText( "" );
    }
}

void Control::handleToData( const QString& cmd )
{
    widget->handleToData(cmd);
}

void ControlWidget::resetTranslator()
{
    QString fileName = QFileDialog::getOpenFileName(this,
     tr("Open Specification File"), QDir::homePath(), tr("Specification files (*.xml)"));
    if(fileName != nullptr)
        translator->resetSpecification(fileName);
}

void ControlWidget::atChanged()
{
    ui->atGroupBox->setVisible( ui->atCheckBox->isChecked() );
}

void ControlWidget::sendVMNotify( int type )
{
    QList<QVMMessage> received;
    QList<QVMMessage> deleted;

    for ( QList<VoicemailItem>::iterator i = mailbox.begin();
            i != mailbox.end(); ++i ) {
        QVMMessage msg( i->id, i->sender.text(), i->priority.text() == "Urgent" );

        if ( i->notifyReceived ) {
            received.append( msg );

            i->notifyReceived = false;
            i->pending.setText( "" );
        }

        if ( i->notifyDeleted ) {
            int position = i - mailbox.begin();

            deleted.append( msg );

            ui->twMessageList->takeItem( position, 0 );
            ui->twMessageList->takeItem( position, 1 );
            ui->twMessageList->takeItem( position, 2 );
            ui->twMessageList->model()->removeRow( position );

            mailbox.erase( i );
        }
    }

    return p->sendVMNotify( type, mailbox.size(), received, deleted,
            ui->leServiceDiallingNumber->text() );
}

void ControlWidget::sendEVMNotify()
{
    return sendVMNotify( 1 );
}

void ControlWidget::addVoicemail()
{
    int position = ui->twMessageList->rowCount();

    mailbox.append( VoicemailItem(
            ui->leSenderNumber->text(),
            ui->chkPriority->isChecked() ) );
    ui->leSenderNumber->setText( "" );
    ui->chkPriority->setChecked( false );

    ui->twMessageList->model()->insertRow( position );
    ui->twMessageList->setItem( position, 0, &mailbox.last().sender );
    ui->twMessageList->setItem( position, 1, &mailbox.last().priority );
    ui->twMessageList->setItem( position, 2, &mailbox.last().pending );
}

void ControlWidget::delVoicemail()
{
    foreach ( QTableWidgetSelectionRange range,
            ui->twMessageList->selectedRanges() ) {
        for ( int i = range.topRow(); i <= range.bottomRow(); i ++ ) {
            VoicemailItem &item = mailbox[i];

            item.notifyDeleted = true;
            item.pending.setText( "DELETE" );
        }
    }
}

int ControlWidget::VoicemailItem::nextId;

ControlWidget::VoicemailItem::VoicemailItem( const QString &from, bool urgent )
    : sender( from ), priority( urgent ? "Urgent" : "" ), pending( "NEW" )
{
    notifyReceived = true;
    notifyDeleted = false;

    id = nextId++;
}

void ControlWidget::sendUSSD()
{
    bool ask = ui->chkAskResponse->isChecked();
    QString text = ui->leUSSDString->text();

    p->sendUSSD( false, ask, text );

    ui->lblResponse->setText( "" );
    ui->leUSSDString->setText( "" );
}

void ControlWidget::cancelUSSD()
{
    p->sendUSSD( true, false, "" );

    ui->lblResponse->setText( "" );
    ui->leUSSDString->setText( "" );
}

void ControlWidget::simInsertRemove()
{
    bool isChecked = ui->cbSimInserted->isChecked();
    p->setSimPresent( isChecked );
    emit unsolicitedCommand( "+USIMSTATE: "+QString::number( isChecked ) );
}

void ControlWidget::handleNewApp()
{
    ui->cbSimApps->insertItems( 0, p->getSimAppsNameList() );
}

void Control::handleNewApp()
{
    widget->handleNewApp();
}

void ControlWidget::simAppStart()
{
    if ( ui->cbSimApps->currentIndex() >= 0 )
        p->simAppStart( ui->cbSimApps->currentIndex() );
}

void ControlWidget::simAppAbort()
{
    p->simAppAbort();
}

void ControlWidget::modemSilentReset()
{
    emit unsolicitedCommand("+CRST:");
}

Script::Script(QObject *obj, Ui_ControlBase *ui) : QDBusAbstractAdaptor(obj)
{
    /* Export tabs to be accessed by script */
    QScriptValue qsTab = engine.newQObject(ui->tab);
    engine.globalObject().setProperty("tabRegistration", qsTab);

    QScriptValue qsTab2 = engine.newQObject(ui->tab_2);
    engine.globalObject().setProperty("tabCBM", qsTab2);

    QScriptValue qsTab3 = engine.newQObject(ui->tab_3);
    engine.globalObject().setProperty("tabSMS", qsTab3);

    QScriptValue qsTab4 = engine.newQObject(ui->tab_4);
    engine.globalObject().setProperty("tabVoiceMail", qsTab4);

    QScriptValue qsTab5 = engine.newQObject(ui->tab_5);
    engine.globalObject().setProperty("tabUSSD", qsTab5);

    QScriptValue qsTab6 = engine.newQObject(ui->tab_6);
    engine.globalObject().setProperty("tabSIM", qsTab6);

    QScriptValue qsTab8 = engine.newQObject(ui->tab_8);
    engine.globalObject().setProperty("tabPosition", qsTab8);

    QScriptValue qsTab9 = engine.newQObject(ui->tab_9);
    engine.globalObject().setProperty("tabCall", qsTab9);
}

void Script::SetPath(const QString &path, const QDBusMessage &msg)
{
    QDir dir(path);
    if (!dir.exists()) {
        QDBusMessage reply = msg.createErrorReply(SERVICE "Error.PathNotFound", "Path doesn't exist");
        QDBusConnection::sessionBus().send(reply);
        return;
    }

    dirPath = path;
}

QString Script::GetPath()
{
    return dirPath;
}

QString Script::Run(const QString &name, const QDBusMessage &msg)
{
    QString fileName;

    if (dirPath.endsWith('/'))
        fileName = dirPath + name;
    else
        fileName = dirPath + "/" + name;

    QFile scriptFile(fileName);

    if (!scriptFile.open(QIODevice::ReadOnly)) {
        QDBusMessage reply = msg.createErrorReply(SERVICE ".Error.FileNotFound", "Script file doesn't exist");
        QDBusConnection::sessionBus().send(reply);
        return QString();
    }

    QTextStream stream(&scriptFile);
    stream.setCodec("UTF-8");
    QString contents = stream.readAll();
    scriptFile.close();

    QScriptValue qsScript = engine.evaluate(contents);
    if (qsScript.isError()) {
        QString info = fileName + ", line " + qsScript.property("lineNumber").toString() + ", " + qsScript.toString();
        QDBusMessage reply = msg.createErrorReply(SERVICE ".Error.ScriptExecError", info);
        QDBusConnection::sessionBus().send(reply);
        return QString();
    }

    /*
     * If this QScriptValue is an object, calling this function has side effects on the script engine,
     * since the engine will call the object's toString() function (and possibly valueOf()) in an attempt
     * to convert the object to a primitive value (possibly resulting in an uncaught script exception).
     */
    if (qsScript.isObject() || qsScript.isUndefined())
        return QString();

    return qsScript.toString();
}