summaryrefslogtreecommitdiffstats
path: root/src/mainwindow.cpp
blob: b35a8f98d1c99ae53a83b9d9a9c3fb476b0ecae3 (plain) (blame)
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
#include "mainwindow.h"
#include "slxoutput.h"

#include <PulseAudioQt/Sink>
#include <PulseAudioQt/Port>
#include <PulseAudioQt/Card>
#include <PulseAudioQt/Profile>

MainWindow::MainWindow() : QDialog()
{
    setupUi(this);
    setWindowFlags(Qt::WindowStaysOnTopHint | windowFlags());
}

MainWindow::~MainWindow()
{

}

SlxOutput* MainWindow::getDevice(const PulseAudioQt::Card *card, const PulseAudioQt::Sink *sink, const PulseAudioQt::Port *port)
{
    PulseAudioQt::Profile *profile = nullptr;
    QString cardId;
    QString id = sink->name() + port->name();
    SlxOutput* w = _widgets.value(id, nullptr);
    QString heading = port->description();

	if (card != nullptr) {
		cardId = card->name();
		if (card->activeProfileIndex() < card->profiles().size()) {
			profile = card->profiles().at(card->activeProfileIndex());
			heading.append(QLatin1String(" (")).append(profile->description()).append(QLatin1String(")"));
		}
	}
	if (w != nullptr) {
		w->headingLabel->setText(heading);
		return w;
	}

	w = new SlxOutput(SlxOutput::SinkPortItem, id, cardId, QLatin1String(), sink->name(), port->name(),
					  heading, sink->description());
	_widgets.insert(id, w);

    insertItemWidget(w, true);
    return w;
}

SlxOutput* MainWindow::getCardProfile(const PulseAudioQt::Card *card, const PulseAudioQt::Profile *profile)
{

    QString id = card->name() + profile->name();
    SlxOutput* w = _widgets.value(id, nullptr);
    if (w != nullptr)
        return w;

    w = new SlxOutput(SlxOutput::CardProfileItem, id, card->name(), profile->name(), QLatin1String(), QLatin1String(),
                      profile->description(), card->description());
    _widgets.insert(id, w);

    insertItemWidget(w, false);
    return w;
}

void MainWindow::insertItemWidget(SlxOutput* w, bool isDevice)
{
    bool done = false;
    int idx;

    for (idx = 0; idx < container->count(); ++idx) {
        auto *l = container->itemAt(idx);
        SlxOutput *other = qobject_cast<SlxOutput*>(l->widget());
        if (other == nullptr)
            continue;
        printf("    Comparing %s - %s\n", other->isDevice() ? "SINK" : "PROF", other->nameLabel->text().toLocal8Bit().constData());
        if (isDevice != other->isDevice()) {
            if (isDevice)
                break;
            continue;
        }
        if (w->compareTo(other) < 0) {
            done = true;
            printf("Inserting before that (%d)!\n", idx);
            container->insertWidget(idx, w);
            break;
        }
    }
    if (!done) {
        if (idx == -1) {
            idx = 0;
        }
        printf("Inserting before that (%d) 2!\n", idx);
        container->insertWidget(idx, w);
    }
}

/**
 * Mark all entries as unused.
 */
void MainWindow::mark()
{
    for (auto *w : _widgets) {
        w->unused = true;
    }
}

/**
 * Remove all entries marked as unused.
 */
void MainWindow::sweep()
{
    for (QMutableMapIterator<QString, SlxOutput*> it(_widgets); it.hasNext(); ) {
        it.next();
        auto *w = it.value();
        if (w->unused) {
            printf("Killing %p\n", w);
            it.remove();
            w->deleteLater();
        }
    }
}