findthisdeviceplugin_config: Check for URL before trying to play sound

Having a clear button and then emitting a warning if an enabled button is clicked doesn't really make sense.
This commit is contained in:
Alexander Lohnau 2023-07-20 16:59:08 +03:00 committed by Albert Vaca Cintora
parent f740a8bda8
commit 41d474b1a5
3 changed files with 20 additions and 22 deletions

View file

@ -32,7 +32,11 @@ FindThisDeviceConfig::FindThisDeviceConfig(QObject *parent, const QVariantList &
m_ui->soundFileRequester->setStartDir(QUrl::fromLocalFile(soundDirs.last()));
}
connect(m_ui->playSoundButton, &QToolButton::clicked, this, &FindThisDeviceConfig::playSound);
connect(m_ui->playSoundButton, &QToolButton::clicked, this, [this]() {
if (const QUrl soundUrl = m_ui->soundFileRequester->url(); soundUrl.isValid()) {
playSound(soundUrl);
}
});
connect(m_ui->soundFileRequester, &KUrlRequester::textChanged, this, &FindThisDeviceConfig::markAsChanged);
}
@ -64,29 +68,23 @@ void FindThisDeviceConfig::save()
KCModule::save();
}
void FindThisDeviceConfig::playSound()
void FindThisDeviceConfig::playSound(const QUrl &soundUrl)
{
const QUrl soundURL = m_ui->soundFileRequester->url();
if (soundURL.isEmpty()) {
qCWarning(KDECONNECT_PLUGIN_FINDTHISDEVICE) << "Not playing sound, no valid ring tone specified.";
} else {
QMediaPlayer *player = new QMediaPlayer;
QMediaPlayer *player = new QMediaPlayer;
#if QT_VERSION_MAJOR < 6
player->setAudioRole(QAudio::Role(QAudio::NotificationRole));
player->setMedia(soundURL);
player->setVolume(100);
player->play();
connect(player, &QMediaPlayer::stateChanged, player, &QObject::deleteLater);
player->setAudioRole(QAudio::Role(QAudio::NotificationRole));
player->setMedia(soundUrl);
player->setVolume(100);
player->play();
connect(player, &QMediaPlayer::stateChanged, player, &QObject::deleteLater);
#else
auto audioOutput = new QAudioOutput();
audioOutput->setVolume(100);
player->setSource(soundURL);
player->setAudioOutput(audioOutput);
player->play();
connect(player, &QMediaPlayer::playingChanged, player, &QObject::deleteLater);
auto audioOutput = new QAudioOutput();
audioOutput->setVolume(100);
player->setSource(soundUrl);
player->setAudioOutput(audioOutput);
player->play();
connect(player, &QMediaPlayer::playingChanged, player, &QObject::deleteLater);
#endif
}
}
#include "findthisdevice_config.moc"

View file

@ -27,7 +27,7 @@ public Q_SLOTS:
void defaults() override;
private Q_SLOTS:
void playSound();
void playSound(const QUrl &soundUrl);
private:
Ui::FindThisDeviceConfigUi *m_ui;

View file

@ -61,7 +61,7 @@ inline QString defaultSound()
}
#endif
if (soundURL.isEmpty()) {
qCWarning(KDECONNECT_PLUGIN_FINDTHISDEVICE) << "Could not find default ring tone.";
qWarning() << "Could not find default ring tone.";
}
return soundURL.toLocalFile();
}