File indexing completed on 2024-07-21 03:37:38

0001 /*
0002     SPDX-FileCopyrightText: 2007 Frederik Gladhorn <frederik.gladhorn@kdemail.net>
0003     SPDX-FileCopyrightText: 2021 Andreas Cord-Landwehr <cordlandwehr@kde.org>
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "audiowidget.h"
0008 #include <KEduVocDocument>
0009 #include <KEduVocExpression>
0010 #include <QAudioOutput>
0011 #include <QDebug>
0012 #include <QRegularExpression>
0013 
0014 using namespace Editor;
0015 
0016 AudioWidget::AudioWidget(QWidget *parent)
0017     : QWidget(parent)
0018 {
0019     setupUi(this);
0020 
0021     connect(audioUrlRequester, &KUrlRequester::textChanged, this, &AudioWidget::slotAudioFileChanged);
0022     connect(playButton, &QPushButton::clicked, this, &AudioWidget::startPlayback);
0023     connect(recordButton, &QPushButton::clicked, this, &AudioWidget::startRecordAudio);
0024     connect(downloadButton, &QPushButton::clicked, this, &AudioWidget::downloadWebserviceAudio);
0025     connect(&m_player, &QMediaPlayer::playbackStateChanged, this, &AudioWidget::slotPlaybackFinished);
0026     connect(&m_webserviceDownloadWatcher, &QFutureWatcher<bool>::finished, this, &AudioWidget::handleDownloadWebserviceFinished);
0027 
0028     playButton->setEnabled(false);
0029     playButton->setIcon(QIcon::fromTheme(QStringLiteral("media-playback-start")));
0030     recordButton->setVisible(false); // TODO enable when recorder is implemented
0031     recordButton->setIcon(QIcon::fromTheme(QStringLiteral("media-record")));
0032     downloadButton->setEnabled(m_translateShell.isTranslateShellAvailable());
0033 
0034     audioUrlRequester->setEnabled(false);
0035 
0036     m_player.setAudioOutput(new QAudioOutput);
0037 }
0038 
0039 void AudioWidget::setDocument(KEduVocDocument *doc)
0040 {
0041     m_doc = doc;
0042 }
0043 
0044 QString AudioWidget::defaultOutputDirectory() const
0045 {
0046     if (m_doc) {
0047         return m_doc->url().path().remove(".kvtml") + "/media";
0048     } else {
0049         return QString();
0050     }
0051 }
0052 
0053 void AudioWidget::setTranslation(KEduVocExpression *entry, int translation)
0054 {
0055     m_currentTranslation = translation;
0056     m_entry = entry;
0057 
0058     if (m_entry) {
0059         recordButton->setEnabled(true);
0060         audioUrlRequester->setEnabled(true);
0061         audioUrlRequester->setUrl(m_entry->translation(m_currentTranslation)->soundUrl());
0062     } else {
0063         recordButton->setEnabled(false);
0064         audioUrlRequester->setEnabled(false);
0065 
0066         if (m_player.playbackState() == QMediaPlayer::PlayingState) {
0067             playButton->setEnabled(true);
0068         } else {
0069             playButton->setEnabled(false);
0070         }
0071 
0072         audioUrlRequester->clear();
0073     }
0074 }
0075 
0076 void AudioWidget::slotAudioFileChanged(const QString &url)
0077 {
0078     if (m_entry) {
0079         m_entry->translation(m_currentTranslation)->setSoundUrl(QUrl::fromLocalFile(url));
0080     }
0081     playButton->setEnabled(!url.isEmpty());
0082 }
0083 
0084 void AudioWidget::startPlayback()
0085 {
0086     QUrl soundFile = m_entry->translation(m_currentTranslation)->soundUrl();
0087     if (m_player.playbackState() == QMediaPlayer::PlayingState) {
0088         m_player.stop();
0089     }
0090     m_player.setSource(soundFile);
0091     m_player.play();
0092 }
0093 
0094 void AudioWidget::startRecordAudio()
0095 {
0096     /// FIXME: When Phonon gains the ability to record sound, implement me :)
0097 }
0098 
0099 void AudioWidget::slotPlaybackFinished(QMediaPlayer::PlaybackState state)
0100 {
0101     switch (state) {
0102     case QMediaPlayer::StoppedState:
0103         playButton->setIcon(QIcon::fromTheme(QStringLiteral("media-playback-start")));
0104         playButton->setEnabled(!audioUrlRequester->url().isEmpty());
0105         break;
0106 
0107     case QMediaPlayer::PlayingState:
0108         playButton->setIcon(QIcon::fromTheme(QStringLiteral("media-playback-stop")));
0109         playButton->setEnabled(false);
0110         break;
0111 
0112     case QMediaPlayer::PausedState:
0113         playButton->setIcon(QIcon::fromTheme(QStringLiteral("media-playback-start")));
0114         playButton->setEnabled(!audioUrlRequester->url().isEmpty());
0115         break;
0116     }
0117 }
0118 
0119 void AudioWidget::downloadWebserviceAudio()
0120 {
0121     if (!m_entry || !m_doc) {
0122         return;
0123     }
0124 
0125     QDir dir(defaultOutputDirectory());
0126     if (!dir.exists()) {
0127         dir.mkpath(dir.path());
0128     }
0129 
0130     QString soundFileName = m_entry->translation(m_currentTranslation)->soundUrl().toLocalFile();
0131     if (soundFileName.isEmpty()) {
0132         const QString fileName = m_entry->translation(m_currentTranslation)->text().replace(QRegularExpression("[^a-zA-Z\\s]"), QString());
0133         int index{0};
0134         do {
0135             soundFileName = dir.path() + '/' + fileName + '_' + QString::number(index) + ".ts";
0136             ++index;
0137             qDebug() << "test" << soundFileName;
0138         } while (QFile::exists(soundFileName));
0139         m_entry->translation(m_currentTranslation)->setSoundUrl(QUrl::fromLocalFile(soundFileName));
0140     }
0141     QFuture<bool> result = TranslateShellAdapter::downloadSoundFile(m_entry->translation(m_currentTranslation)->text(),
0142                                                                     m_doc->identifier(m_currentTranslation).locale(),
0143                                                                     soundFileName);
0144     m_webserviceDownloadWatcher.setFuture(result);
0145 }
0146 
0147 void AudioWidget::handleDownloadWebserviceFinished()
0148 {
0149     if (!m_entry) {
0150         return;
0151     }
0152     if (QFile::exists(m_entry->translation(m_currentTranslation)->soundUrl().toLocalFile())) {
0153         playButton->setEnabled(true);
0154         playButton->setIcon(QIcon::fromTheme(QStringLiteral("media-playback-start")));
0155         audioUrlRequester->setUrl(m_entry->translation(m_currentTranslation)->soundUrl());
0156     }
0157 }
0158 
0159 #include "moc_audiowidget.cpp"