File: portswindow.cpp

package info (click to toggle)
ostinato 1.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 4,840 kB
  • sloc: cpp: 46,226; makefile: 8
file content (635 lines) | stat: -rw-r--r-- 20,223 bytes parent folder | download
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
/*
Copyright (C) 2010 Srivats P.

This file is part of "Ostinato"

This is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>
*/

#include "portswindow.h"

#include "applymsg.h"
#include "deviceswidget.h"
#include "fileformat.pb.h"
#include "portconfigdialog.h"
#include "portgrouplist.h"
#include "portwidget.h"
#include "settings.h"
#include "streamswidget.h"

#include <QInputDialog>
#include <QMainWindow>
#include <QMessageBox>
#include <QProgressDialog>
#include <QSortFilterProxyModel>

extern QMainWindow *mainWindow;

PortsWindow::PortsWindow(PortGroupList *pgl, QWidget *parent)
    : QWidget(parent), proxyPortModel(NULL)
{
    proxyPortModel = new QSortFilterProxyModel(this);

    plm = pgl;

    setupUi(this);
    applyMsg_ = new ApplyMessage();
    portWidget->setPortGroupList(plm);
    streamsWidget->setPortGroupList(plm);
    devicesWidget->setPortGroupList(plm);

    tvPortList->header()->hide();

    // Populate PortList Context Menu Actions
    tvPortList->addAction(actionNew_Port_Group);
    tvPortList->addAction(actionDelete_Port_Group);
    tvPortList->addAction(actionConnect_Port_Group);
    tvPortList->addAction(actionDisconnect_Port_Group);

    tvPortList->addAction(actionExclusive_Control);
    tvPortList->addAction(actionPort_Configuration);

    // PortList, StreamList, DeviceWidget actions combined
    // make this window's actions
    addActions(tvPortList->actions());
    QAction *sep = new QAction(this);
    sep->setSeparator(true);
    addAction(sep);
    addActions(streamsWidget->actions());
    sep = new QAction(this);
    sep->setSeparator(true);
    addAction(sep);
    addActions(devicesWidget->actions());

    // XXX: It would be ideal if we only needed to do the below to 
    // get the proxy model to do its magic. However, the QModelIndex
    // used by the source model and the proxy model are different
    // i.e. the row, column, internalId/internalPtr used by both
    // will be different. Since our domain objects - PortGroupList,
    // PortGroup, Port etc. use these attributes, we need to map the
    // proxy's index to the source's index before invoking any domain
    // object methods
    // TODO: research if we can skip the mapping when the domain 
    // objects' design is reviewed
    if (proxyPortModel) {
        proxyPortModel->setSourceModel(plm->getPortModel());
        tvPortList->setModel(proxyPortModel);
    }
    else
        tvPortList->setModel(plm->getPortModel());
    
    connect( plm->getPortModel(), 
        SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)), 
        this, SLOT(when_portModel_dataChanged(const QModelIndex&, 
            const QModelIndex&)));

    connect(plm->getPortModel(), SIGNAL(modelReset()), 
        SLOT(when_portModel_reset()));

    connect(actionPort_Configuration, SIGNAL(triggered()),
        SLOT(when_actionPort_Configuration_triggered()));
    connect(tvPortList, SIGNAL(activated(const QModelIndex&)),
        SLOT(when_actionPort_Configuration_triggered(const QModelIndex&)));
    connect(tvPortList->selectionModel(),
        SIGNAL(currentChanged(const QModelIndex&, const QModelIndex&)), 
        this, SLOT(when_portView_currentChanged(const QModelIndex&, 
            const QModelIndex&)));

    connect(this,
            SIGNAL(currentPortChanged(const QModelIndex&, const QModelIndex&)),
            portWidget,
            SLOT(setCurrentPortIndex(const QModelIndex&, const QModelIndex&)));
    connect(this,
            SIGNAL(currentPortChanged(const QModelIndex&, const QModelIndex&)),
            streamsWidget, SLOT(setCurrentPortIndex(const QModelIndex&)));
    connect(this,
            SIGNAL(currentPortChanged(const QModelIndex&, const QModelIndex&)),
            devicesWidget, SLOT(setCurrentPortIndex(const QModelIndex&)));

    // Initially we don't have any ports/streams/devices
    //  - so send signal triggers
    when_portView_currentChanged(QModelIndex(), QModelIndex());
}

PortsWindow::~PortsWindow()
{
    delete proxyPortModel;
    delete applyMsg_;
}

int PortsWindow::portGroupCount()
{
    return plm->numPortGroups();
}

int PortsWindow::reservedPortCount()
{
    int count = 0;
    int n = portGroupCount();

    for (int i = 0; i < n; i++)
        count += plm->portGroupByIndex(i).numReservedPorts();

    return count;
}

//! Always return true
bool PortsWindow::openSession(
        const OstProto::SessionContent *session,
        QString & /*error*/)
{
    QProgressDialog progress("Opening Session", NULL,
                             0, session->port_groups_size(), mainWindow);
    progress.show();
    progress.setEnabled(true); // since parent (mainWindow) is disabled

    plm->removeAllPortGroups();

    for (int i = 0; i < session->port_groups_size(); i++) {
        const OstProto::PortGroupContent &pgc = session->port_groups(i);
        PortGroup *pg = new PortGroup(QString::fromStdString(
                                                    pgc.server_name()),
                                      quint16(pgc.server_port()));
        pg->setConfigAtConnect(&pgc);
        plm->addPortGroup(*pg);
        progress.setValue(i+1);
    }

    return true;
}

/*!
 * Prepare content to be saved for a session
 *
 * If port reservation is in use, saves only 'my' reserved ports
 *
 * Returns false, if user cancels op; true, otherwise
 */
bool PortsWindow::saveSession(
        OstProto::SessionContent *session, // OUT param
        QString & /*error*/,
        QProgressDialog *progress)
{
    int n = portGroupCount();
    QString myself;

    if (progress) {
        progress->setLabelText("Preparing Ports and PortGroups ...");
        progress->setRange(0, n);
    }

    if (reservedPortCount())
        myself = appSettings->value(kUserKey, kUserDefaultValue).toString();

    for (int i = 0; i < n; i++)
    {
        PortGroup &pg = plm->portGroupByIndex(i);
        OstProto::PortGroupContent *pgc = session->add_port_groups();

        pgc->set_server_name(pg.serverName().toStdString());
        pgc->set_server_port(pg.serverPort());

        for (int j = 0; j < pg.numPorts(); j++)
        {
            if (myself != pg.mPorts.at(j)->userName())
                continue;

            OstProto::PortContent *pc = pgc->add_ports();
            OstProto::Port *p = pc->mutable_port_config();

            // XXX: We save the entire OstProto::Port even though some
            // fields may be ephemeral; while opening we use only relevant
            // fields
            pg.mPorts.at(j)->protoDataCopyInto(p);

            for (int k = 0; k < pg.mPorts.at(j)->numStreams(); k++)
            {
                OstProto::Stream *s = pc->add_streams();
                pg.mPorts.at(j)->streamByIndex(k)->protoDataCopyInto(*s);
            }

            for (int k = 0; k < pg.mPorts.at(j)->numDeviceGroups(); k++)
            {
                OstProto::DeviceGroup *dg = pc->add_device_groups();
                dg->CopyFrom(*(pg.mPorts.at(j)->deviceGroupByIndex(k)));
            }
        }

        if (progress) {
            if (progress->wasCanceled())
                return false;
            progress->setValue(i);
        }
        if (i % 2 == 0)
            qApp->processEvents();
    }

    return true;
}

QList<QAction*> PortsWindow::portActions()
{
    return tvPortList->actions();
}

QList<QAction*> PortsWindow::streamActions()
{
    return streamsWidget->actions();
}

QList<QAction*> PortsWindow::deviceActions()
{
    return devicesWidget->actions();
}

void PortsWindow::clearCurrentSelection()
{
    tvPortList->selectionModel()->clearCurrentIndex();
    tvPortList->clearSelection();
}

void PortsWindow::showMyReservedPortsOnly(bool enabled)
{
    if (!proxyPortModel)
        return;

    if (enabled) {
        QString rx = "Port Group|\\["
                    + QRegExp::escape(appSettings->value(kUserKey, 
                                            kUserDefaultValue).toString())
                    + "\\]";
        qDebug("%s: regexp: <%s>", __FUNCTION__, qPrintable(rx));
        proxyPortModel->setFilterRegExp(QRegExp(rx));
    }
    else
        proxyPortModel->setFilterRegExp(QRegExp(""));
}

void PortsWindow::when_portView_currentChanged(const QModelIndex& currentIndex,
    const QModelIndex& previousIndex)
{
    QModelIndex current = currentIndex;
    QModelIndex previous = previousIndex;

    if (proxyPortModel) {
        current = proxyPortModel->mapToSource(current);
        previous = proxyPortModel->mapToSource(previous);
    }

    updatePortViewActions(currentIndex);

    qDebug("In %s", __FUNCTION__);

    if (previous.isValid() && plm->isPort(previous))
    {
        disconnect(&(plm->port(previous)),
                   SIGNAL(localConfigChanged(int, int, bool)),
                   this,
                   SLOT(updateApplyHint(int, int, bool)));
    }

    if (!current.isValid())
    {    
        qDebug("setting stacked widget to welcome page");
        swDetail->setCurrentIndex(0); // welcome page
    }
    else
    {
        if (plm->isPortGroup(current))
        {
            swDetail->setCurrentIndex(1);    // portGroup detail page    
        }
        else if (plm->isPort(current))
        {
            swDetail->setCurrentIndex(2);    // port detail page
            connect(&(plm->port(current)),
                    SIGNAL(localConfigChanged(int, int, bool)),
                    SLOT(updateApplyHint(int, int, bool)));
            if (plm->port(current).isDirty())
                updateApplyHint(plm->port(current).portGroupId(),
                                plm->port(current).id(), true);
            else if (plm->port(current).numStreams())
                applyHint->setText("Click <img src=':/icons/control_play'/> "
                                   "to transmit packets");
            else
                applyHint->setText("");
        }
    }

    emit currentPortChanged(current, previous);
}

void PortsWindow::when_portModel_dataChanged(const QModelIndex& topLeft,
    const QModelIndex& bottomRight)
{
    qDebug("In %s %d:(%d, %d) - %d:(%d, %d)", __FUNCTION__,
            topLeft.parent().isValid(), topLeft.row(), topLeft.column(),
            bottomRight.parent().isValid(), bottomRight.row(), bottomRight.column());

    if (!topLeft.isValid() || !bottomRight.isValid())
        return;

    if (topLeft.parent() != bottomRight.parent())
        return;

    // If a port has changed, expand the port group
    if (topLeft.parent().isValid())
        tvPortList->expand(proxyPortModel ? 
                proxyPortModel->mapFromSource(topLeft.parent()) : 
                topLeft.parent());

#if 0 // not sure why the >= <= operators are not overloaded in QModelIndex
    if ((tvPortList->currentIndex() >= topLeft) &&
        (tvPortList->currentIndex() <= bottomRight))
#endif
    if (((topLeft < tvPortList->currentIndex()) || 
            (topLeft == tvPortList->currentIndex())) &&
        (((tvPortList->currentIndex() < bottomRight)) || 
            (tvPortList->currentIndex() == bottomRight)))
    {
        // Update UI to reflect potential change in exclusive mode,
        // transmit mode et al
        when_portView_currentChanged(tvPortList->currentIndex(),
                tvPortList->currentIndex());
    }
}

void PortsWindow::when_portModel_reset()
{
    when_portView_currentChanged(QModelIndex(), tvPortList->currentIndex());
}

void PortsWindow::updateApplyHint(int /*portGroupId*/, int /*portId*/,
        bool configChanged)
{
    if (configChanged)
        applyHint->setText("Configuration has changed - "
                           "<font color='red'><b>click Apply</b></font> "
                           "to activate the changes");
    else if (plm->getStreamModel()->rowCount() > 0)
        applyHint->setText("Configuration activated - "
                           "click <img src=':/icons/control_play'/> "
                           "to transmit packets");
    else
        applyHint->setText("Configuration activated");
}

void PortsWindow::updatePortViewActions(const QModelIndex& currentIndex)
{
    QModelIndex current = currentIndex;

    if (proxyPortModel)
        current = proxyPortModel->mapToSource(current);

    if (!current.isValid())
    {
        qDebug("current is now invalid");
        actionDelete_Port_Group->setDisabled(true);
        actionConnect_Port_Group->setDisabled(true);
        actionDisconnect_Port_Group->setDisabled(true);

        actionExclusive_Control->setDisabled(true);
        actionPort_Configuration->setDisabled(true);
    
        goto _EXIT;
    }

    qDebug("currentChanged %p", (void*)current.internalId());

    if (plm->isPortGroup(current))
    {
        actionDelete_Port_Group->setEnabled(true);

        actionExclusive_Control->setDisabled(true);
        actionPort_Configuration->setDisabled(true);

        switch(plm->portGroup(current).state())
        {
            case QAbstractSocket::UnconnectedState:
            case QAbstractSocket::ClosingState:
                qDebug("state = unconnected|closing");
                actionConnect_Port_Group->setEnabled(true);
                actionDisconnect_Port_Group->setDisabled(true);
                break;

            case QAbstractSocket::HostLookupState:
            case QAbstractSocket::ConnectingState:
            case QAbstractSocket::ConnectedState:
                qDebug("state = lookup|connecting|connected");
                actionConnect_Port_Group->setDisabled(true);
                actionDisconnect_Port_Group->setEnabled(true);
                break;


            case QAbstractSocket::BoundState:
            case QAbstractSocket::ListeningState:
            default:
                // FIXME(LOW): indicate error
                qDebug("unexpected state");
                break;
        }
    }
    else if (plm->isPort(current))
    {
        actionDelete_Port_Group->setDisabled(true);
        actionConnect_Port_Group->setDisabled(true);
        actionDisconnect_Port_Group->setDisabled(true);

        actionExclusive_Control->setEnabled(true);
        if (plm->port(current).hasExclusiveControl())
            actionExclusive_Control->setChecked(true);
        else
            actionExclusive_Control->setChecked(false);
        actionPort_Configuration->setEnabled(true);
    }

_EXIT:
    return;
}

void PortsWindow::on_pbApply_clicked()
{
    QModelIndex    curPort;
    QModelIndex curPortGroup;

    curPort = tvPortList->selectionModel()->currentIndex();
    if (proxyPortModel)
        curPort = proxyPortModel->mapToSource(curPort);
    if (!curPort.isValid())
    {
        qDebug("%s: curPort is invalid", __FUNCTION__);
        goto _exit;
    }

    if (!plm->isPort(curPort))
    {
        qDebug("%s: curPort is not a port", __FUNCTION__);
        goto _exit;
    }

    if (plm->port(curPort).getStats().state().is_transmit_on())
    {
        QMessageBox::information(0, "Configuration Change",
                "Please stop transmit on the port before applying any changes");
        goto _exit;
    }

    curPortGroup = plm->getPortModel()->parent(curPort);
    if (!curPortGroup.isValid())
    {
        qDebug("%s: curPortGroup is invalid", __FUNCTION__);
        goto _exit;
    }
    if (!plm->isPortGroup(curPortGroup))
    {
        qDebug("%s: curPortGroup is not a portGroup", __FUNCTION__);
        goto _exit;
    }

    disconnect(applyMsg_);
    connect(&(plm->portGroup(curPortGroup)), SIGNAL(applyFinished()),
            applyMsg_, SLOT(hide()));
    applyMsg_->show();

    // FIXME(HI): shd this be a signal?
    //portGroup.when_configApply(port);
    // FIXME(MED): mixing port id and index!!!
    plm->portGroup(curPortGroup).when_configApply(plm->port(curPort).id());

_exit:
    return;

#if 0
    // TODO (LOW): This block is for testing only
    QModelIndex    current = tvPortList->selectionModel()->currentIndex();

    if (current.isValid())
        qDebug("current =  %llx", current.internalId());
    else
        qDebug("current is invalid");
#endif
}

void PortsWindow::on_actionNew_Port_Group_triggered()
{
    bool ok;
    QString text = QInputDialog::getText(this, 
        "Add Port Group", "Port Group Address (HostName[:Port])",
        QLineEdit::Normal, lastNewPortGroup, &ok);
    
    if (ok)
    {
        QStringList addr = text.split(":");
        quint16 port = DEFAULT_SERVER_PORT;

        if (addr.size() > 2) { // IPv6 Address
            // IPv6 addresses with port number SHOULD be specified as
            // [2001:db8::1]:80 (RFC5952 Sec6) to avoid ambiguity due to ':'
            addr = text.split("]:");
            if (addr.size() > 1)
                port = addr[1].toUShort();
        }
        else if (addr.size() == 2) // Hostname/IPv4 + Port specified
            port = addr[1].toUShort();

        // Play nice and remove square brackets irrespective of addr type
        addr[0].remove(QChar('['));
        addr[0].remove(QChar(']'));

        PortGroup *pg = new PortGroup(addr[0], port);
        plm->addPortGroup(*pg);
        lastNewPortGroup = text;
    }
}

void PortsWindow::on_actionDelete_Port_Group_triggered()
{
    QModelIndex    current = tvPortList->selectionModel()->currentIndex();

    if (proxyPortModel)
        current = proxyPortModel->mapToSource(current);

    if (current.isValid())
        plm->removePortGroup(plm->portGroup(current));
}

void PortsWindow::on_actionConnect_Port_Group_triggered()
{
    QModelIndex    current = tvPortList->selectionModel()->currentIndex();

    if (proxyPortModel)
        current = proxyPortModel->mapToSource(current);

    if (current.isValid())
        plm->portGroup(current).connectToHost();
}

void PortsWindow::on_actionDisconnect_Port_Group_triggered()
{
    QModelIndex    current = tvPortList->selectionModel()->currentIndex();

    if (proxyPortModel)
        current = proxyPortModel->mapToSource(current);

    if (current.isValid())
        plm->portGroup(current).disconnectFromHost();
}

void PortsWindow::on_actionExclusive_Control_triggered(bool checked)
{
    QModelIndex    current = tvPortList->selectionModel()->currentIndex();

    if (proxyPortModel)
        current = proxyPortModel->mapToSource(current);

    if (plm->isPort(current))
    {
        OstProto::Port config;
        
        config.set_is_exclusive_control(checked);
        plm->portGroup(current.parent()).modifyPort(current.row(), config);
    }
}

void PortsWindow::when_actionPort_Configuration_triggered(
        const QModelIndex &portIndex)
{
    QModelIndex current = portIndex.isValid() ?
        portIndex : tvPortList->selectionModel()->currentIndex();

    if (proxyPortModel)
        current = proxyPortModel->mapToSource(current);

    if (!plm->isPort(current))
        return;

    Port &port = plm->port(current);
    OstProto::Port config;
    // XXX: we don't call Port::protoDataCopyInto() to get config b'coz
    // we want only the modifiable fields populated to send to Drone
    // TODO: extend Port::protoDataCopyInto() to accept an optional param
    // which says copy only modifiable fields
    //plm->port(current).protoDataCopyInto(&config);
    config.set_description(port.systemDescription().toStdString());
    config.set_user_description(port.userDescription().toStdString());
    config.set_transmit_mode(port.transmitMode());
    config.set_is_tracking_stream_stats(port.trackStreamStats());
    config.set_is_exclusive_control(port.hasExclusiveControl());
    config.set_user_name(port.userName().toStdString());

    PortConfigDialog dialog(config, port.getStats().state(), this);

    if (dialog.exec() == QDialog::Accepted)
        plm->portGroup(current.parent()).modifyPort(current.row(), config);
}