-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmainwindow.cpp
165 lines (147 loc) · 5.63 KB
/
mainwindow.cpp
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
/**********************************************************************
* mainwindow.cpp
**********************************************************************
* Copyright (C) 2015 MX Authors
*
* Authors: Adrian
* MX Linux <http://mxlinux.org>
*
* This file is part of mx-select-sound.
*
* mx-select-sound 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.
*
* mx-select-sound 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 mx-select-sound. If not, see <http://www.gnu.org/licenses/>.
**********************************************************************/
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QDir>
#include <QFile>
#include <QRadioButton>
#include <QTextEdit>
#include <QTimer>
#include "about.h"
#include "version.h"
MainWindow::MainWindow(QWidget *parent)
: QDialog(parent),
ui(new Ui::MainWindow)
{
qDebug().noquote() << QCoreApplication::applicationName() << "version:" << VERSION;
ui->setupUi(this);
setConnections();
setWindowFlags(Qt::Window); // for the close, min and max buttons
if (ui->pushApply->icon().isNull()) {
ui->pushApply->setIcon(QIcon(":/icons/dialog-ok.svg"));
}
setWindowTitle(tr("MX Select Sound"));
adjustSize();
}
MainWindow::~MainWindow()
{
delete ui;
}
// Util function for getting bash command output and error code
Result MainWindow::runCmd(const QString &cmd)
{
QEventLoop loop;
proc = new QProcess(this);
proc->setProcessChannelMode(QProcess::MergedChannels);
connect(proc, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), &loop, &QEventLoop::quit);
proc->start(QStringLiteral("/bin/bash"), {"-c", cmd});
loop.exec();
disconnect(proc, SIGNAL(finished(int)), nullptr, nullptr);
Result result = {proc->exitCode(), proc->readAll().trimmed()};
delete proc;
return result;
}
// Get the list of sound cards
QStringList MainWindow::listCards()
{
QStringList card_list;
const QString cards
= runCmd(R"(cat /proc/asound/cards 2>/dev/null | sed -n -r 's/[0-9 ]+\[//p' | sed 's/\s*\]:/:/')").output;
if (cards.size() == 0) {
QMessageBox::critical(this, tr("MX Select Sound"), tr("No sound cards/devices were found."));
} else {
card_list = cards.split(QStringLiteral("\n"));
ui->comboBox->addItems(card_list);
}
return card_list;
}
// Get default card
QString MainWindow::getDefault()
{
QString prev_card;
QString default_card = tr("none");
Result cmd = runCmd(R"(set -o pipefail; sed -n -r 's/^\s*defaults.pcm.!card\s+(.*)/\1/p' ~/.asoundrc | head -n 1)");
if (cmd.exitCode == 0) {
prev_card = cmd.output.toUtf8();
for (int i = 0; i < ui->comboBox->count(); i++) {
if (prev_card == ui->comboBox->itemText(i).section(QStringLiteral(":"), 0, 0).toUtf8()) {
default_card = prev_card;
}
}
}
qDebug() << "Default sound card:" << default_card;
ui->pushTest->setDisabled(default_card == tr("none"));
ui->labelCurrent->setText(default_card);
return default_card;
}
void MainWindow::setConnections()
{
connect(ui->pushApply, &QPushButton::clicked, this, &MainWindow::pushApply_clicked);
connect(ui->pushAbout, &QPushButton::clicked, this, &MainWindow::pushAbout_clicked);
connect(ui->pushHelp, &QPushButton::clicked, this, &MainWindow::pushHelp_clicked);
connect(ui->pushTest, &QPushButton::clicked, this, &MainWindow::pushTest_clicked);
}
void MainWindow::pushApply_clicked()
{
QString selected = ui->comboBox->currentText().section(QStringLiteral(":"), 0, 0);
QFile asoundrc;
asoundrc.setFileName(QDir::homePath() + "/.asoundrc");
if (asoundrc.open(QFile::WriteOnly | QFile::Text)) {
asoundrc.write(QStringLiteral("defaults.pcm.!card %1\ndefaults.ctl.!card %1").arg(selected).toUtf8());
asoundrc.close();
getDefault();
}
}
void MainWindow::pushAbout_clicked()
{
hide();
displayAboutMsgBox(
tr("About MX Select Sound"),
"<p align=\"center\"><b><h2>" + tr("MX Select Sound") + "</h2></b></p><p align=\"center\">" + tr("Version: ")
+ VERSION + "</p><p align=\"center\"><h3>" + tr("Program for selecting the default sound card in MX Linux")
+ "</h3></p><p align=\"center\"><a href=\"http://mxlinux.org\">http://mxlinux.org</a><br /></p>"
"<p align=\"center\">"
+ tr("Copyright (c) MX Linux") + "<br /><br /></p>",
QStringLiteral("/usr/share/doc/mx-select-sound/license.html"), tr("%1 License").arg(windowTitle()));
show();
}
void MainWindow::pushHelp_clicked()
{
QLocale locale;
const QString lang = locale.bcp47Name();
QString url = QStringLiteral("/usr/share/doc/mx-select-sound/mx-select-sound.html");
if (lang.startsWith(QLatin1String("fr"))) {
url = QStringLiteral("https://mxlinux.org/wiki/help-files/help-mx-carte-son");
}
displayDoc(url, tr("%1 Help").arg(tr("MX Select Sound")));
}
// Test default sound card
void MainWindow::pushTest_clicked()
{
const int exitCode = runCmd(QStringLiteral("speaker-test -c 2 -t wav -l 2")).exitCode;
if (exitCode != 0) {
QMessageBox::critical(this, tr("MX Select Sound"), tr("Could not play test sound."));
}
}