File indexing completed on 2024-11-10 04:50:09

0001 /*
0002   SPDX-FileCopyrightText: 2001 Marc Mutz <mutz@kde.org>
0003 
0004   SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "soundtestwidget.h"
0008 
0009 #include <KLineEdit>
0010 #include <KLocalizedString>
0011 #include <KUrlRequester>
0012 #include <QIcon>
0013 #include <QUrl>
0014 
0015 #include <QHBoxLayout>
0016 #include <QPushButton>
0017 
0018 #include <QStandardPaths>
0019 #include <phonon/backendcapabilities.h>
0020 #include <phonon/mediaobject.h>
0021 
0022 using namespace MailCommon;
0023 
0024 SoundTestWidget::SoundTestWidget(QWidget *parent)
0025     : QWidget(parent)
0026     , m_urlRequester(new KUrlRequester(this))
0027     , m_playButton(new QPushButton(this))
0028 {
0029     auto layout = new QHBoxLayout(this);
0030     layout->setContentsMargins({});
0031 
0032     m_playButton->setIcon(QIcon::fromTheme(QStringLiteral("media-playback-start")));
0033     m_playButton->setToolTip(i18n("Play"));
0034     layout->addWidget(m_playButton);
0035 
0036     layout->addWidget(m_urlRequester);
0037 
0038     connect(m_playButton, &QPushButton::clicked, this, &SoundTestWidget::playSound);
0039     connect(m_urlRequester, &KUrlRequester::openFileDialog, this, &SoundTestWidget::openSoundDialog);
0040     connect(m_urlRequester->lineEdit(), &QLineEdit::textChanged, this, &SoundTestWidget::slotUrlChanged);
0041 
0042     slotUrlChanged(m_urlRequester->lineEdit()->text());
0043 }
0044 
0045 SoundTestWidget::~SoundTestWidget() = default;
0046 
0047 void SoundTestWidget::slotUrlChanged(const QString &url)
0048 {
0049     m_playButton->setEnabled(!url.trimmed().isEmpty());
0050     Q_EMIT textChanged(url);
0051 }
0052 
0053 void SoundTestWidget::openSoundDialog(KUrlRequester *)
0054 {
0055     static bool init = true;
0056     if (!init) {
0057         return;
0058     }
0059 
0060     init = false;
0061     QFileDialog *fileDialog = m_urlRequester->fileDialog();
0062     fileDialog->setWindowTitle(i18nc("@title:window", "Select Sound File"));
0063 
0064     m_urlRequester->setMimeTypeFilters(Phonon::BackendCapabilities::availableMimeTypes());
0065 
0066     const QStringList soundDirs = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("sound/"), QStandardPaths::LocateDirectory);
0067 
0068     if (!soundDirs.isEmpty()) {
0069         QUrl soundURL;
0070         QDir dir;
0071         dir.setFilter(QDir::Files | QDir::Readable);
0072 
0073         for (const QString &soundDir : soundDirs) {
0074             dir = soundDir;
0075             if (dir.isReadable() && dir.count() > 2) {
0076                 soundURL.setPath(soundDir);
0077                 m_urlRequester->setUrl(soundURL);
0078                 break;
0079             }
0080         }
0081     }
0082 }
0083 
0084 void SoundTestWidget::playSound()
0085 {
0086     const QString parameter = m_urlRequester->lineEdit()->text();
0087     if (parameter.isEmpty()) {
0088         return;
0089     }
0090 
0091     const QString file = QStringLiteral("file:");
0092     const QString play = (parameter.startsWith(file) ? parameter.mid(file.length()) : parameter);
0093     if (m_player) {
0094         if (m_player->state() == Phonon::PlayingState) {
0095             m_player->pause();
0096         } else {
0097             m_player->setCurrentSource(QUrl::fromLocalFile(play));
0098             m_player->play();
0099         }
0100     } else {
0101         m_player = Phonon::createPlayer(Phonon::NotificationCategory, QUrl::fromLocalFile(play));
0102         m_player->setParent(this);
0103         m_player->play();
0104         connect(m_player, &Phonon::MediaObject::stateChanged, this, [this](Phonon::State newState, Phonon::State) {
0105             if (newState == Phonon::PlayingState) {
0106                 m_playButton->setIcon(QIcon::fromTheme(QStringLiteral("media-playback-pause")));
0107                 m_playButton->setToolTip(i18n("Pause"));
0108             } else {
0109                 m_playButton->setIcon(QIcon::fromTheme(QStringLiteral("media-playback-start")));
0110                 m_playButton->setToolTip(i18n("Play"));
0111             }
0112         });
0113     }
0114 }
0115 
0116 QString SoundTestWidget::url() const
0117 {
0118     return m_urlRequester->lineEdit()->text();
0119 }
0120 
0121 void SoundTestWidget::setUrl(const QString &url)
0122 {
0123     m_urlRequester->lineEdit()->setText(url);
0124 }
0125 
0126 void SoundTestWidget::clear()
0127 {
0128     m_urlRequester->lineEdit()->clear();
0129 }
0130 
0131 #include "moc_soundtestwidget.cpp"