aboutsummaryrefslogtreecommitdiffstats
path: root/ui/qt/voip_calls_dialog.cpp
blob: a340602cb5d84e1fe89920c67006d1f3c0a62c00 (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
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
/* voip_calls_dialog.cpp
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "voip_calls_dialog.h"
#include <ui_voip_calls_dialog.h>

#include "file.h"

#include "epan/addr_resolv.h"
#include "epan/dissectors/packet-h225.h"

#include "ui/rtp_stream.h"
#include "ui/rtp_stream_id.h"

#include <ui/qt/utils/qt_ui_utils.h>
#include "rtp_player_dialog.h"
#include "sequence_dialog.h"
#include <ui/qt/utils/stock_icon.h>
#include "wireshark_application.h"
#include <ui/qt/models/voip_calls_info_model.h>

#include <QClipboard>
#include <QContextMenuEvent>
#include <QPushButton>

// To do:
// - More context menu items
//   - Don't select on right click
// - Player
// - Add a screenshot to the user's guide
// - Add filter for quickly searching through list?

// Bugs:
// - Preparing a filter overwrites the existing filter. The GTK+ UI appends.
//   We'll probably have to add an "append" parameter to MainWindow::filterPackets.

enum { voip_calls_type_ = 1000 };

VoipCallsDialog::VoipCallsDialog(QWidget &parent, CaptureFile &cf, bool all_flows) :
    WiresharkDialog(parent, cf),
    ui(new Ui::VoipCallsDialog),
    parent_(parent),
    voip_calls_tap_listeners_removed_(false)
{
    ui->setupUi(this);
    loadGeometry(parent.width() * 4 / 5, parent.height() * 2 / 3);
    ui->callTreeView->installEventFilter(this);

    // Create the model that stores the actual data and the proxy model that is
    // responsible for sorting and filtering data in the display.
    call_infos_model_ = new VoipCallsInfoModel(this);
    cache_model_ = new CacheProxyModel(this);
    cache_model_->setSourceModel(call_infos_model_);
    sorted_model_ = new VoipCallsInfoSortedModel(this);
    sorted_model_->setSourceModel(cache_model_);
    ui->callTreeView->setModel(sorted_model_);

    connect(ui->callTreeView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
            this, SLOT(updateWidgets()));
    ui->callTreeView->sortByColumn(VoipCallsInfoModel::StartTime, Qt::AscendingOrder);
    setWindowSubtitle(all_flows ? tr("SIP Flows") : tr("VoIP Calls"));

    sequence_button_ = ui->buttonBox->addButton(ui->actionFlowSequence->text(), QDialogButtonBox::ActionRole);
    sequence_button_->setToolTip(ui->actionFlowSequence->toolTip());
    prepare_button_ = ui->buttonBox->addButton(ui->actionPrepareFilter->text(), QDialogButtonBox::ActionRole);
    prepare_button_->setToolTip(ui->actionPrepareFilter->toolTip());
    player_button_ = RtpPlayerDialog::addPlayerButton(ui->buttonBox, this);

    connect (ui->todCheckBox, &QAbstractButton::toggled, this, &VoipCallsDialog::switchTimeOfDay);

    copy_button_ = ui->buttonBox->addButton(ui->actionCopyButton->text(), QDialogButtonBox::ActionRole);
    copy_button_->setToolTip(ui->actionCopyButton->toolTip());
    QMenu *copy_menu = new QMenu(copy_button_);
    QAction *ca;
    ca = copy_menu->addAction(tr("as CSV"));
    connect(ca, SIGNAL(triggered()), this, SLOT(copyAsCSV()));
    ca = copy_menu->addAction(tr("as YAML"));
    connect(ca, SIGNAL(triggered()), this, SLOT(copyAsYAML()));
    copy_button_->setMenu(copy_menu);
    connect(&cap_file_, SIGNAL(captureEvent(CaptureEvent)),
            this, SLOT(captureEvent(CaptureEvent)));

    memset (&tapinfo_, 0, sizeof(tapinfo_));
    tapinfo_.tap_packet = tapPacket;
    tapinfo_.tap_reset = tapReset;
    tapinfo_.tap_draw = tapDraw;
    tapinfo_.tap_data = this;
    tapinfo_.callsinfos = g_queue_new();
    tapinfo_.h225_cstype = H225_OTHER;
    tapinfo_.fs_option = all_flows ? FLOW_ALL : FLOW_ONLY_INVITES; /* flow show option */
    tapinfo_.graph_analysis = sequence_analysis_info_new();
    tapinfo_.graph_analysis->name = "voip";
    sequence_info_ = new SequenceInfo(tapinfo_.graph_analysis);
    shown_callsinfos_ = g_queue_new();

    voip_calls_init_all_taps(&tapinfo_);
    if (cap_file_.isValid() && cap_file_.capFile()->dfilter) {
        // Activate display filter checking
        tapinfo_.apply_display_filter = true;
        ui->displayFilterCheckBox->setChecked(true);
    }

    updateWidgets();

    if (cap_file_.isValid()) {
        tapinfo_.session = cap_file_.capFile()->epan;
        cap_file_.delayedRetapPackets();
    }
}

bool VoipCallsDialog::eventFilter(QObject *, QEvent *event)
{
    if (ui->callTreeView->hasFocus() && event->type() == QEvent::KeyPress) {
        QKeyEvent &keyEvent = static_cast<QKeyEvent&>(*event);
        switch(keyEvent.key()) {
            case Qt::Key_I:
                if (keyEvent.modifiers() == Qt::ControlModifier) {
                    // Ctrl+I
                    on_actionSelectInvert_triggered();
                    return true;
                }
                break;
            case Qt::Key_A:
                if (keyEvent.modifiers() == Qt::ControlModifier) {
                    // Ctrl+A
                    on_actionSelectAll_triggered();
                    return true;
                } else if (keyEvent.modifiers() == (Qt::ShiftModifier | Qt::ControlModifier)) {
                    // Ctrl+Shift+A
                    on_actionSelectNone_triggered();
                    return true;
                }
                break;
            default:
                break;
        }
    }
    return false;
}

VoipCallsDialog::~VoipCallsDialog()
{
    delete ui;

    voip_calls_reset_all_taps(&tapinfo_);
    if (!voip_calls_tap_listeners_removed_) {
        voip_calls_remove_all_tap_listeners(&tapinfo_);
        voip_calls_tap_listeners_removed_ = true;
    }
    sequence_info_->unref();
    g_queue_free(tapinfo_.callsinfos);
    // We don't need to clear shown_callsinfos_ data, it was shared
    // with tapinfo_.callsinfos and was cleared
    // during voip_calls_reset_all_taps
    g_queue_free(shown_callsinfos_);
}

void VoipCallsDialog::removeTapListeners()
{
    if (!voip_calls_tap_listeners_removed_) {
        voip_calls_remove_all_tap_listeners(&tapinfo_);
        voip_calls_tap_listeners_removed_ = true;
    }
    WiresharkDialog::removeTapListeners();
}

void VoipCallsDialog::captureFileClosing()
{
    // The time formatting is currently provided by VoipCallsInfoModel, but when
    // the cache is active, the ToD cannot be modified.
    cache_model_->setSourceModel(NULL);
    if (!voip_calls_tap_listeners_removed_) {
        voip_calls_remove_all_tap_listeners(&tapinfo_);
        voip_calls_tap_listeners_removed_ = true;
    }
    tapinfo_.session = NULL;

    WiresharkDialog::captureFileClosing();
}

void VoipCallsDialog::captureFileClosed()
{
    // The time formatting is currently provided by VoipCallsInfoModel, but when
    // the cache is active, the ToD cannot be modified.
    ui->todCheckBox->setEnabled(false);
    ui->displayFilterCheckBox->setEnabled(false);

    WiresharkDialog::captureFileClosed();
}

void VoipCallsDialog::contextMenuEvent(QContextMenuEvent *event)
{
    bool selected = ui->callTreeView->selectionModel()->hasSelection();

    if (! selected)
        return;

    QMenu popupMenu;
    QAction *action;

    popupMenu.addMenu(ui->menuSelect);
    action = popupMenu.addAction(tr("Display time as time of day"), this, SLOT(switchTimeOfDay()));
    action->setCheckable(true);
    action->setChecked(call_infos_model_->timeOfDay());
    action->setEnabled(!file_closed_);
    popupMenu.addSeparator();
    action = popupMenu.addAction(tr("Copy as CSV"), this, SLOT(copyAsCSV()));
    action->setToolTip(tr("Copy stream list as CSV."));
    action = popupMenu.addAction(tr("Copy as YAML"), this, SLOT(copyAsYAML()));
    action->setToolTip(tr("Copy stream list as YAML."));

    popupMenu.exec(event->globalPos());
}

void VoipCallsDialog::changeEvent(QEvent *event)
{
    if (0 != event)
    {
        switch (event->type())
        {
        case QEvent::LanguageChange:
            ui->retranslateUi(this);
            break;
        default:
            break;
        }
    }
    QDialog::changeEvent(event);
}

void VoipCallsDialog::captureEvent(CaptureEvent e)
{
    if (e.captureContext() == CaptureEvent::Retap)
    {
        switch (e.eventType())
        {
        case CaptureEvent::Started:
            ui->displayFilterCheckBox->setEnabled(false);
            break;
        case CaptureEvent::Finished:
            ui->displayFilterCheckBox->setEnabled(true);
            break;
        default:
            break;
        }
    }

}

void VoipCallsDialog::tapReset(void *tapinfo_ptr)
{
    voip_calls_tapinfo_t *tapinfo = static_cast<voip_calls_tapinfo_t *>(tapinfo_ptr);
    VoipCallsDialog *voip_calls_dialog = static_cast<VoipCallsDialog *>(tapinfo->tap_data);

    // Create new callsinfos queue in tapinfo. Current callsinfos are
    // in shown_callsinfos_.
    voip_calls_dialog->tapinfo_.callsinfos = g_queue_new();
    voip_calls_reset_all_taps(tapinfo);

    // Leave old graph_analysis as is and allocate new one
    voip_calls_dialog->sequence_info_->unref();
    voip_calls_dialog->tapinfo_.graph_analysis = sequence_analysis_info_new();
    voip_calls_dialog->tapinfo_.graph_analysis->name = "voip";
    voip_calls_dialog->sequence_info_ = new SequenceInfo(voip_calls_dialog->tapinfo_.graph_analysis);
}

tap_packet_status VoipCallsDialog::tapPacket(void *, packet_info *, epan_dissect_t *, const void *)
{
#ifdef QT_MULTIMEDIA_LIB
//    voip_calls_tapinfo_t *tapinfo = (voip_calls_tapinfo_t *) tapinfo_ptr;
    // add_rtp_packet for voip player.
//    return TAP_PACKET_REDRAW;
#endif
    return TAP_PACKET_DONT_REDRAW;
}

void VoipCallsDialog::tapDraw(void *tapinfo_ptr)
{
    voip_calls_tapinfo_t *tapinfo = static_cast<voip_calls_tapinfo_t *>(tapinfo_ptr);

    if (!tapinfo || !tapinfo->redraw) {
        return;
    }

    GList *graph_item = g_queue_peek_nth_link(tapinfo->graph_analysis->items, 0);
    for (; graph_item; graph_item = gxx_list_next(graph_item)) {
        for (GList *rsi_entry = g_list_first(tapinfo->rtpstream_list); rsi_entry; rsi_entry = gxx_list_next(rsi_entry)) {
            seq_analysis_item_t * sai = gxx_list_data(seq_analysis_item_t *, graph_item);
            rtpstream_info_t *rsi = gxx_list_data(rtpstream_info_t *, rsi_entry);

            if (rsi->start_fd->num == sai->frame_number) {
                rsi->call_num = sai->conv_num;
                // VOIP_CALLS_DEBUG("setting conv num %u for frame %u", sai->conv_num, sai->frame_number);
            }
        }
    }

    VoipCallsDialog *voip_calls_dialog = static_cast<VoipCallsDialog *>(tapinfo->tap_data);
    if (voip_calls_dialog) {
        voip_calls_dialog->updateCalls();
    }
}

gint VoipCallsDialog::compareCallNums(gconstpointer a, gconstpointer b)
{
    voip_calls_info_t *call_a = (voip_calls_info_t *)a;
    voip_calls_info_t *call_b = (voip_calls_info_t *)b;

    return (call_a->call_num != call_b->call_num);
}

void VoipCallsDialog::updateCalls()
{
    voip_calls_info_t *new_callsinfo;
    voip_calls_info_t *old_callsinfo;
    GList *found;

    ui->callTreeView->setSortingEnabled(false);

    // Merge new callsinfos with old ones
    // It keeps list of calls visible including selected items
    GList *list = g_queue_peek_nth_link(tapinfo_.callsinfos, 0);
    while (list) {
        // Find new callsinfo
        new_callsinfo = gxx_list_data(voip_calls_info_t*, list);
        found = g_queue_find_custom(shown_callsinfos_, new_callsinfo, VoipCallsDialog::compareCallNums);
        if (!found) {
            // New call, add it to list for show
            g_queue_push_tail(shown_callsinfos_, new_callsinfo);
        } else {
            // Existing call
            old_callsinfo = (voip_calls_info_t *)found->data;
            if (new_callsinfo != old_callsinfo) {
                // Replace it
                voip_calls_free_callsinfo(old_callsinfo);
                found->data = new_callsinfo;
            }
        }

        list = gxx_list_next(list);
    }

    // Update model
    call_infos_model_->updateCalls(shown_callsinfos_);

    // Resize columns
    for (int i = 0; i < call_infos_model_->columnCount(); i++) {
        ui->callTreeView->resizeColumnToContents(i);
    }

    ui->callTreeView->setSortingEnabled(true);

    updateWidgets();
}

void VoipCallsDialog::updateWidgets()
{
    bool selected = ui->callTreeView->selectionModel()->hasSelection();
    bool have_ga_items = false;

    if (tapinfo_.graph_analysis && tapinfo_.graph_analysis->items) {
        have_ga_items = true;
    }

    bool enable = selected && have_ga_items && !file_closed_;

    prepare_button_->setEnabled(enable);
    sequence_button_->setEnabled(enable);
#if defined(QT_MULTIMEDIA_LIB)
    player_button_->setEnabled(enable);
#endif

    WiresharkDialog::updateWidgets();
}

void VoipCallsDialog::prepareFilter()
{
    if (!ui->callTreeView->selectionModel()->hasSelection() || !tapinfo_.graph_analysis) {
        return;
    }

    QString filter_str;
    QSet<guint16> selected_calls;
    QString frame_numbers;
    QList<int> rows;

    /* Build a new filter based on frame numbers */
    foreach (QModelIndex index, ui->callTreeView->selectionModel()->selectedIndexes()) {
        if (index.isValid() && ! rows.contains(index.row()))
        {
            voip_calls_info_t *call_info = VoipCallsInfoModel::indexToCallInfo(index);
            if (!call_info) {
                return;
            }

            selected_calls << call_info->call_num;
            rows << index.row();
        }
    }

    GList *cur_ga_item = g_queue_peek_nth_link(tapinfo_.graph_analysis->items, 0);
    while (cur_ga_item && cur_ga_item->data) {
        seq_analysis_item_t *ga_item = gxx_list_data(seq_analysis_item_t*, cur_ga_item);
        if (selected_calls.contains(ga_item->conv_num)) {
            frame_numbers += QString("%1 ").arg(ga_item->frame_number);
        }
        cur_ga_item = gxx_list_next(cur_ga_item);
    }

    if (!frame_numbers.isEmpty()) {
        frame_numbers.chop(1);
        filter_str = QString("frame.number in {%1} or rtp.setup-frame in {%1}").arg(frame_numbers);
    }

#if 0
    // XXX The GTK+ UI falls back to building a filter based on protocols if the filter
    // length is too long. Leaving this here for the time being in case we need to do
    // the same in the Qt UI.
    const sip_calls_info_t *sipinfo;
    const isup_calls_info_t *isupinfo;
    const h323_calls_info_t *h323info;
    const h245_address_t *h245_add = NULL;
    const gcp_ctx_t* ctx;
    char *guid_str;

    if (filter_length < max_filter_length) {
        gtk_editable_insert_text(GTK_EDITABLE(main_display_filter_widget), filter_string_fwd->str, -1, &pos);
    } else {
        g_string_free(filter_string_fwd, TRUE);
        filter_string_fwd = g_string_new(filter_prepend);

        g_string_append_printf(filter_string_fwd, "(");
        is_first = TRUE;
        /* Build a new filter based on protocol fields */
        lista = g_queue_peek_nth_link(voip_calls_get_info()->callsinfos, 0);
        while (lista) {
            listinfo = gxx_list_data(voip_calls_info_t *, lista);
            if (listinfo->selected) {
                if (!is_first)
                    g_string_append_printf(filter_string_fwd, " or ");
                switch (listinfo->protocol) {
                case VOIP_SIP:
                    sipinfo = (sip_calls_info_t *)listinfo->prot_info;
                    g_string_append_printf(filter_string_fwd,
                        "(sip.Call-ID == \"%s\")",
                        sipinfo->call_identifier
                    );
                    break;
                case VOIP_ISUP:
                    isupinfo = (isup_calls_info_t *)listinfo->prot_info;
                    g_string_append_printf(filter_string_fwd,
                        "(isup.cic == %i and frame.number >= %i and frame.number <= %i and mtp3.network_indicator == %i and ((mtp3.dpc == %i) and (mtp3.opc == %i)) or ((mtp3.dpc == %i) and (mtp3.opc == %i)))",
                        isupinfo->cic, listinfo->start_fd->num,
                        listinfo->stop_fd->num,
                        isupinfo->ni, isupinfo->dpc, isupinfo->opc,
                        isupinfo->opc, isupinfo->dpc
                    );
                    break;
                case VOIP_H323:
                {
                    h323info = (h323_calls_info_t *)listinfo->prot_info;
					guid_str = guid_to_str(NULL, &h323info->guid[0]);
                    g_string_append_printf(filter_string_fwd,
                        "((h225.guid == %s || q931.call_ref == %x:%x || q931.call_ref == %x:%x)",
                        guid_str,
                        (guint8) (h323info->q931_crv & 0x00ff),
                        (guint8)((h323info->q931_crv & 0xff00)>>8),
                        (guint8) (h323info->q931_crv2 & 0x00ff),
                        (guint8)((h323info->q931_crv2 & 0xff00)>>8));
                    listb = g_list_first(h323info->h245_list);
					wmem_free(NULL, guid_str);
                    while (listb) {
                        h245_add = gxx_list_data(h245_address_t *, listb);
                        g_string_append_printf(filter_string_fwd,
                            " || (ip.addr == %s && tcp.port == %d && h245)",
                            address_to_qstring(&h245_add->h245_address), h245_add->h245_port);
                        listb = gxx_list_next(listb);
                    }
                    g_string_append_printf(filter_string_fwd, ")");
                }
                    break;
                case TEL_H248:
                    ctx = (gcp_ctx_t *)listinfo->prot_info;
                    g_string_append_printf(filter_string_fwd,
                        "(h248.ctx == 0x%x)", ctx->id);
                    break;
                default:
                    /* placeholder to assure valid display filter expression */
                    g_string_append_printf(filter_string_fwd,
                        "(frame)");
                    break;
                }
                is_first = FALSE;
            }
            lista = gxx_list_next(lista);
        }

        g_string_append_printf(filter_string_fwd, ")");
        gtk_editable_insert_text(GTK_EDITABLE(main_display_filter_widget), filter_string_fwd->str, -1, &pos);
    }
#endif

    emit updateFilter(filter_str);
}

void VoipCallsDialog::showSequence()
{
    if (file_closed_) return;

    QSet<guint16> selected_calls;
    foreach (QModelIndex index, ui->callTreeView->selectionModel()->selectedIndexes()) {
        voip_calls_info_t *call_info = VoipCallsInfoModel::indexToCallInfo(index);
        if (!call_info) {
            return;
        }
        selected_calls << call_info->call_num;
    }

    sequence_analysis_list_sort(tapinfo_.graph_analysis);
    GList *cur_ga_item = g_queue_peek_nth_link(tapinfo_.graph_analysis->items, 0);
    while (cur_ga_item && cur_ga_item->data) {
        seq_analysis_item_t *ga_item = gxx_list_data(seq_analysis_item_t*, cur_ga_item);
        ga_item->display = selected_calls.contains(ga_item->conv_num);
        cur_ga_item = gxx_list_next(cur_ga_item);
    }

    SequenceDialog *sequence_dialog = new SequenceDialog(parent_, cap_file_, sequence_info_);
    // Bypass this dialog and forward signals to parent
    connect(sequence_dialog, SIGNAL(rtpStreamsDialogSelectRtpStream(rtpstream_id_t *)), &parent_, SLOT(rtpStreamsDialogSelectRtpStream(rtpstream_id_t *)));
    connect(sequence_dialog, SIGNAL(rtpStreamsDialogDeselectRtpStream(rtpstream_id_t *)), &parent_, SLOT(rtpStreamsDialogDeselectRtpStream(rtpstream_id_t *)));
    connect(sequence_dialog, SIGNAL(rtpPlayerDialogReplaceRtpStreams(QVector<rtpstream_info_t *>)), &parent_, SLOT(rtpPlayerDialogReplaceRtpStreams(QVector<rtpstream_info_t *>)));
    connect(sequence_dialog, SIGNAL(rtpPlayerDialogAddRtpStreams(QVector<rtpstream_info_t *>)), &parent_, SLOT(rtpPlayerDialogAddRtpStreams(QVector<rtpstream_info_t *>)));
    connect(sequence_dialog, SIGNAL(rtpPlayerDialogRemoveRtpStreams(QVector<rtpstream_info_t *>)), &parent_, SLOT(rtpPlayerDialogRemoveRtpStreams(QVector<rtpstream_info_t *>)));
    sequence_dialog->setAttribute(Qt::WA_DeleteOnClose);
    sequence_dialog->enableVoIPFeatures();
    sequence_dialog->show();
}

QVector<rtpstream_info_t *>VoipCallsDialog::getSelectedRtpStreams()
{
    QVector<rtpstream_info_t *> stream_infos;
    foreach (QModelIndex index, ui->callTreeView->selectionModel()->selectedIndexes()) {
        voip_calls_info_t *vci = VoipCallsInfoModel::indexToCallInfo(index);
        if (!vci) continue;

        for (GList *rsi_entry = g_list_first(tapinfo_.rtpstream_list); rsi_entry; rsi_entry = gxx_list_next(rsi_entry)) {
            rtpstream_info_t *rsi = gxx_list_data(rtpstream_info_t *, rsi_entry);
            if (!rsi) continue;

            //VOIP_CALLS_DEBUG("checking call %u, start frame %u == stream call %u, start frame %u, setup frame %u",
            //                vci->call_num, vci->start_fd->num,
            //                rsi->call_num, rsi->start_fd->num, rsi->setup_frame_number);
            if (vci->call_num == static_cast<guint>(rsi->call_num)) {
                //VOIP_CALLS_DEBUG("adding call number %u", vci->call_num);
                stream_infos << rsi;
            }
        }
    }

    return stream_infos;
}

void VoipCallsDialog::rtpPlayerReplace()
{
    if (ui->callTreeView->selectionModel()->selectedIndexes().count() < 1) return;

    emit rtpPlayerDialogReplaceRtpStreams(getSelectedRtpStreams());
}

void VoipCallsDialog::rtpPlayerAdd()
{
    if (ui->callTreeView->selectionModel()->selectedIndexes().count() < 1) return;

    emit rtpPlayerDialogAddRtpStreams(getSelectedRtpStreams());
}

void VoipCallsDialog::rtpPlayerRemove()
{
    if (ui->callTreeView->selectionModel()->selectedIndexes().count() < 1) return;

    emit rtpPlayerDialogRemoveRtpStreams(getSelectedRtpStreams());
}

QList<QVariant> VoipCallsDialog::streamRowData(int row) const
{
    QList<QVariant> row_data;

    if (row >= sorted_model_->rowCount()) {
        return row_data;
    }

    for (int col = 0; col < sorted_model_->columnCount(); col++) {
        if (row < 0) {
            row_data << sorted_model_->headerData(col, Qt::Horizontal);
        } else {
            row_data << sorted_model_->index(row, col).data();
        }
    }
    return row_data;
}

void VoipCallsDialog::on_callTreeView_activated(const QModelIndex &index)
{
    voip_calls_info_t *call_info = VoipCallsInfoModel::indexToCallInfo(index);
    if (!call_info) {
        return;
    }
    emit goToPacket(call_info->start_fd->num);
}

void VoipCallsDialog::selectAll()
{
    ui->callTreeView->selectAll();
}

void VoipCallsDialog::selectNone()
{
    ui->callTreeView->clearSelection();
}

void VoipCallsDialog::copyAsCSV()
{
    QString csv;
    QTextStream stream(&csv, QIODevice::Text);
    for (int row = -1; row < sorted_model_->rowCount(); row++) {
        QStringList rdsl;
        foreach (QVariant v, streamRowData(row)) {
            QString strval = v.toString();
            // XXX should quotes (") in strval be stripped/sanitized?
            rdsl << QString("\"%1\"").arg(strval);
        }
        stream << rdsl.join(",") << '\n';
    }
    wsApp->clipboard()->setText(stream.readAll());
}

void VoipCallsDialog::copyAsYAML()
{
    QString yaml;
    QTextStream stream(&yaml, QIODevice::Text);
    stream << "---" << '\n';
    for (int row = -1; row < sorted_model_->rowCount(); row++) {
        stream << "-" << '\n';
        foreach (QVariant v, streamRowData(row)) {
            stream << " - " << v.toString() << '\n';
        }
    }
    wsApp->clipboard()->setText(stream.readAll());
}

void VoipCallsDialog::on_buttonBox_clicked(QAbstractButton *button)
{
    if (button == prepare_button_) {
        prepareFilter();
    } else if (button == sequence_button_) {
        showSequence();
    }
}

void VoipCallsDialog::removeAllCalls()
{
    voip_calls_info_t *callsinfo;
    GList *list = NULL;

    call_infos_model_->removeAllCalls();

    /* Free shown callsinfos */
    list = g_queue_peek_nth_link(shown_callsinfos_, 0);
    while (list)
    {
        callsinfo = (voip_calls_info_t *)list->data;
        voip_calls_free_callsinfo(callsinfo);
        list = g_list_next(list);
    }
    g_queue_clear(shown_callsinfos_);
}

void VoipCallsDialog::on_displayFilterCheckBox_toggled(bool checked)
{
    if (!cap_file_.isValid()) {
        return;
    }

    tapinfo_.apply_display_filter = checked;
    removeAllCalls();

    cap_file_.retapPackets();
}

void VoipCallsDialog::on_buttonBox_helpRequested()
{
    wsApp->helpTopicAction(HELP_TELEPHONY_VOIP_CALLS_DIALOG);
}

void VoipCallsDialog::switchTimeOfDay()
{
    bool checked = ! call_infos_model_->timeOfDay();

    ui->todCheckBox->setChecked(checked);
    call_infos_model_->setTimeOfDay(checked);
    ui->callTreeView->resizeColumnToContents(VoipCallsInfoModel::StartTime);
    ui->callTreeView->resizeColumnToContents(VoipCallsInfoModel::StopTime);
}

void VoipCallsDialog::displayFilterSuccess(bool success)
{
    if (success && ui->displayFilterCheckBox->isChecked()) {
        removeAllCalls();
        cap_file_.retapPackets();
    }
}

void VoipCallsDialog::invertSelection()
{
    QModelIndex rootIndex = ui->callTreeView->rootIndex();
    QModelIndex first = sorted_model_->index(0, 0, QModelIndex());
    int numOfItems = sorted_model_->rowCount(rootIndex);
    int numOfCols = sorted_model_->columnCount(rootIndex);
    QModelIndex last = sorted_model_->index(numOfItems - 1, numOfCols - 1, QModelIndex());

    QItemSelection selection(first, last);
    ui->callTreeView->selectionModel()->select(selection, QItemSelectionModel::Toggle);
}

void VoipCallsDialog::on_actionSelectAll_triggered()
{
    ui->callTreeView->selectAll();
}

void VoipCallsDialog::on_actionSelectInvert_triggered()
{
    invertSelection();
}

void VoipCallsDialog::on_actionSelectNone_triggered()
{
    ui->callTreeView->clearSelection();
}