File indexing completed on 2022-11-29 13:00:34

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