|
| 1 | +#include "firstlaunchwizard.h" |
| 2 | +#include "ui_firstlaunchwizard.h" |
| 3 | +#include "misc/common.h" |
| 4 | +#include "misc/autostartmanager.h" |
| 5 | +#include "mainwindow.h" |
| 6 | + |
| 7 | +#include <QTimer> |
| 8 | +#include <QEasingCurve> |
| 9 | +#include <QDesktopServices> |
| 10 | +#include <QUrl> |
| 11 | +#include <QFileInfo> |
| 12 | +#include <QDir> |
| 13 | +#include <QProcess> |
| 14 | + |
| 15 | +FirstLaunchWizard::FirstLaunchWizard(AppConfigWrapper* _appconf, MainWindow* mainwin, QWidget *parent) : |
| 16 | + QWidget(parent), |
| 17 | + ui(new Ui::FirstLaunchWizard) |
| 18 | +{ |
| 19 | + ui->setupUi(this); |
| 20 | + |
| 21 | + appconf = _appconf; |
| 22 | + |
| 23 | + QTimer::singleShot(1000, [&]{ |
| 24 | + ui->p1_icon->startAnimation(); |
| 25 | + }); |
| 26 | + ui->p2_icon->startAnimation(); |
| 27 | + ui->p3_icon->startAnimation(); |
| 28 | + ui->p4_icon->startAnimation(); |
| 29 | + |
| 30 | + |
| 31 | + ui->stackedWidget->setAnimation(QEasingCurve::Type::OutCirc); |
| 32 | + connect(ui->p1_next,&QPushButton::clicked,[&]{ |
| 33 | + ui->stackedWidget->slideInIdx(1); |
| 34 | + }); |
| 35 | + connect(ui->p2_next,&QPushButton::clicked,[&]{ |
| 36 | + ui->stackedWidget->slideInIdx(2); |
| 37 | + }); |
| 38 | + connect(ui->p3_next,&QPushButton::clicked,[&]{ |
| 39 | + ui->stackedWidget->slideInIdx(3); |
| 40 | + }); |
| 41 | + connect(ui->p4_next,&QPushButton::clicked,[&]{ |
| 42 | + emit wizardFinished(); |
| 43 | + }); |
| 44 | + connect(ui->p4_telegram,&QPushButton::clicked,[&]{ |
| 45 | + QDesktopServices::openUrl(QUrl("https://t.me/joinchat/FTKC2A2bolHkFAyO-fuPjw")); |
| 46 | + }); |
| 47 | + |
| 48 | + auto deviceUpdated = [this](){ |
| 49 | + if(lockslot) return; |
| 50 | + QString absolute = |
| 51 | + QFileInfo(appconf->getPath()).absoluteDir().absolutePath(); |
| 52 | + QString devices(pathAppend(absolute,"devices.conf")); |
| 53 | + if(ui->p2_dev_mode_auto->isChecked()){ |
| 54 | + QFile(devices).remove(); |
| 55 | + }else{ |
| 56 | + if(ui->p2_dev_select->currentData() == "---") |
| 57 | + return; |
| 58 | + |
| 59 | + ConfigContainer* devconf = new ConfigContainer(); |
| 60 | + devconf->setConfigMap(ConfigIO::readFile(devices)); |
| 61 | + devconf->setValue("location",ui->p2_dev_select->currentData()); |
| 62 | + ConfigIO::writeFile(devices,devconf->getConfigMap()); |
| 63 | + } |
| 64 | + }; |
| 65 | + refreshDevices(); |
| 66 | + |
| 67 | + |
| 68 | + ui->p3_systray_disable->setChecked(!appconf->getTrayMode()); |
| 69 | + ui->p3_systray_enable->setChecked(appconf->getTrayMode()); |
| 70 | + ui->p3_systray_icon_box->setEnabled(appconf->getTrayMode()); |
| 71 | + |
| 72 | + QString autostart_path = AutostartManager::getAutostartPath("viper-gui.desktop"); |
| 73 | + bool autostart_enabled = AutostartManager::inspectDesktopFile(autostart_path,AutostartManager::Exists); |
| 74 | + bool autostartviper_enabled = AutostartManager::inspectDesktopFile(autostart_path,AutostartManager::UsesViperAutostart); |
| 75 | + |
| 76 | + ui->p3_systray_minOnBoot->setChecked(autostart_enabled); |
| 77 | + ui->p3_systray_autostartViper->setEnabled(autostart_enabled); |
| 78 | + ui->p3_systray_autostartViper->setChecked(autostartviper_enabled); |
| 79 | + |
| 80 | + auto systray_radio = [this,mainwin]{ |
| 81 | + if(lockslot)return; |
| 82 | + int mode = 0; |
| 83 | + if(ui->p3_systray_disable->isChecked())mode=0; |
| 84 | + else if(ui->p3_systray_enable->isChecked())mode=1; |
| 85 | + appconf->setTrayMode(mode); |
| 86 | + mainwin->setTrayVisible(mode); |
| 87 | + ui->p3_systray_icon_box->setEnabled(mode); |
| 88 | + }; |
| 89 | + |
| 90 | + connect(ui->p3_systray_disable,&QRadioButton::clicked,this,systray_radio); |
| 91 | + connect(ui->p3_systray_enable,&QRadioButton::clicked,this,systray_radio); |
| 92 | + |
| 93 | + auto systray_autostart_radio = [this,autostart_path,mainwin]{ |
| 94 | + if(ui->p3_systray_minOnBoot->isChecked()){ |
| 95 | + AutostartManager::saveDesktopFile(autostart_path,mainwin->GetExecutablePath(), |
| 96 | + ui->p3_systray_autostartViper->isChecked()); |
| 97 | + } |
| 98 | + else QFile(autostart_path).remove(); |
| 99 | + ui->p3_systray_autostartViper->setEnabled(ui->p3_systray_minOnBoot->isChecked()); |
| 100 | + }; |
| 101 | + |
| 102 | + connect(ui->p3_systray_minOnBoot,&QPushButton::clicked,this,systray_autostart_radio); |
| 103 | + connect(ui->p3_systray_autostartViper,&QPushButton::clicked,this,systray_autostart_radio); |
| 104 | + |
| 105 | + connect(ui->p2_dev_mode_auto,&QRadioButton::clicked,this,deviceUpdated); |
| 106 | + connect(ui->p2_dev_mode_manual,&QRadioButton::clicked,this,deviceUpdated); |
| 107 | + connect(ui->p2_dev_select,static_cast<void (QComboBox::*)(const QString&)>(&QComboBox::currentIndexChanged), this, deviceUpdated); |
| 108 | + |
| 109 | +} |
| 110 | + |
| 111 | +FirstLaunchWizard::~FirstLaunchWizard() |
| 112 | +{ |
| 113 | + delete ui; |
| 114 | +} |
| 115 | + |
| 116 | +void FirstLaunchWizard::refreshDevices() |
| 117 | +{ |
| 118 | + lockslot = true; |
| 119 | + ui->p2_dev_select->clear(); |
| 120 | + QString absolute = |
| 121 | + QFileInfo(appconf->getPath()).absoluteDir().absolutePath(); |
| 122 | + QFile devices(pathAppend(absolute,"devices.conf")); |
| 123 | + bool devmode_auto = !devices.exists(); |
| 124 | + ui->p2_dev_mode_auto->setChecked(devmode_auto); |
| 125 | + ui->p2_dev_mode_manual->setChecked(!devmode_auto); |
| 126 | + |
| 127 | + QProcess process; |
| 128 | + QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); |
| 129 | + env.insert("LC_ALL", "C"); |
| 130 | + process.setProcessEnvironment(env); |
| 131 | + process.start("sh", QStringList()<<"-c"<<"pactl list sinks | grep \'Name: \' -A1"); |
| 132 | + process.waitForFinished(500); |
| 133 | + |
| 134 | + ConfigContainer* devconf = new ConfigContainer(); |
| 135 | + devconf->setConfigMap(ConfigIO::readFile(pathAppend(absolute,"devices.conf"))); |
| 136 | + QString out = process.readAllStandardOutput(); |
| 137 | + ui->p2_dev_select->addItem("...","---"); |
| 138 | + for(auto item : out.split("Name:")){ |
| 139 | + item.prepend("Name:"); |
| 140 | + QRegularExpression re("(?<=(Name:)\\s)(?<name>.+)[\\s\\S]+(?<=(Description:)\\s)(?<desc>.+)"); |
| 141 | + QRegularExpressionMatch match = re.match(item, 0, QRegularExpression::PartialPreferCompleteMatch); |
| 142 | + if(match.hasMatch()){ |
| 143 | + ui->p2_dev_select->addItem(QString("%1 (%2)").arg(match.captured("desc")).arg(match.captured("name")), |
| 144 | + match.captured("name")); |
| 145 | + } |
| 146 | + } |
| 147 | + QString dev_location = devconf->getString("location"); |
| 148 | + if(dev_location.isEmpty()) |
| 149 | + ui->p2_dev_select->setCurrentIndex(0); |
| 150 | + else{ |
| 151 | + bool notFound = true; |
| 152 | + for(int i = 0; i < ui->p2_dev_select->count(); i++){ |
| 153 | + if(ui->p2_dev_select->itemData(i) == |
| 154 | + dev_location){ |
| 155 | + notFound = false; |
| 156 | + ui->p2_dev_select->setCurrentIndex(i); |
| 157 | + break; |
| 158 | + } |
| 159 | + } |
| 160 | + if(notFound){ |
| 161 | + QString name = QString("Unknown (%1)").arg(dev_location); |
| 162 | + ui->p2_dev_select->addItem(name,dev_location); |
| 163 | + ui->p2_dev_select->setCurrentText(name); |
| 164 | + } |
| 165 | + } |
| 166 | + lockslot = false; |
| 167 | +} |
0 commit comments