Warning, file /multimedia/dragon/src/mpris2/mediaplayer2player.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     SPDX-FileCopyrightText: 2012 Eike Hein <hein@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 */
0006 
0007 #include "mediaplayer2player.h"
0008 #include "mainWindow.h"
0009 #include "mpris2.h"
0010 #include "videoWindow.h"
0011 
0012 #include <QCryptographicHash>
0013 
0014 static QByteArray makeTrackId(const QString &source)
0015 {
0016     return QByteArray("/org/kde/") + APP_NAME + "/tid_" + QCryptographicHash::hash(source.toLocal8Bit(), QCryptographicHash::Sha1).toHex();
0017 }
0018 
0019 MediaPlayer2Player::MediaPlayer2Player(QObject *parent)
0020     : QDBusAbstractAdaptor(parent)
0021     , oldPos(0)
0022 {
0023     connect(Dragon::engine(), &Dragon::VideoWindow::tick, this, &MediaPlayer2Player::tick);
0024     connect(Dragon::engine(), &Dragon::VideoWindow::currentSourceChanged, this, &MediaPlayer2Player::currentSourceChanged);
0025     connect(Dragon::engine(), &Dragon::VideoWindow::metaDataChanged, this, &MediaPlayer2Player::emitMetadataChange);
0026     connect(Dragon::engine(), &Dragon::VideoWindow::stateUpdated, this, &MediaPlayer2Player::stateUpdated);
0027     connect(Dragon::engine(), &Dragon::VideoWindow::totalTimeChanged, this, &MediaPlayer2Player::emitMetadataChange);
0028     connect(Dragon::engine(), &Dragon::VideoWindow::seekableChanged, this, &MediaPlayer2Player::seekableChanged);
0029     connect(Dragon::engine(), &Dragon::VideoWindow::volumeChanged, this, &MediaPlayer2Player::volumeChanged);
0030 }
0031 
0032 MediaPlayer2Player::~MediaPlayer2Player()
0033 {
0034 }
0035 
0036 bool MediaPlayer2Player::CanGoNext() const
0037 {
0038     return Dragon::engine()->canGoNext();
0039 }
0040 
0041 void MediaPlayer2Player::Next() const
0042 {
0043     Dragon::engine()->nextChapter();
0044 }
0045 
0046 bool MediaPlayer2Player::CanGoPrevious() const
0047 {
0048     return Dragon::engine()->canGoPrev();
0049 }
0050 
0051 void MediaPlayer2Player::Previous() const
0052 {
0053     Dragon::engine()->prevChapter();
0054 }
0055 
0056 bool MediaPlayer2Player::CanPause() const
0057 {
0058     return Dragon::engine()->state() != Phonon::ErrorState;
0059 }
0060 
0061 void MediaPlayer2Player::Pause() const
0062 {
0063     Dragon::engine()->pause();
0064 }
0065 
0066 void MediaPlayer2Player::PlayPause() const
0067 {
0068     Dragon::engine()->playPause();
0069 }
0070 
0071 void MediaPlayer2Player::Stop() const
0072 {
0073     Dragon::engine()->stop();
0074 }
0075 
0076 bool MediaPlayer2Player::CanPlay() const
0077 {
0078     return true;
0079 }
0080 
0081 void MediaPlayer2Player::Play() const
0082 {
0083     Dragon::engine()->play();
0084 }
0085 
0086 void MediaPlayer2Player::SetPosition(const QDBusObjectPath &TrackId, qlonglong Position) const
0087 {
0088     if (TrackId.path().toLocal8Bit() == makeTrackId(Dragon::engine()->urlOrDisc()))
0089         Dragon::engine()->seek(Position / 1000);
0090 }
0091 
0092 void MediaPlayer2Player::OpenUri(QString Uri) const
0093 {
0094     static_cast<Dragon::MainWindow *>(Dragon::mainWindow())->open(QUrl(Uri));
0095 }
0096 
0097 QString MediaPlayer2Player::PlaybackStatus() const
0098 {
0099     switch (Dragon::engine()->state()) {
0100     case (Phonon::PlayingState):
0101         return QStringLiteral("Playing");
0102         break;
0103     case (Phonon::PausedState):
0104     case (Phonon::BufferingState):
0105         return QStringLiteral("Paused");
0106         break;
0107     case (Phonon::StoppedState):
0108         return QStringLiteral("Stopped");
0109         break;
0110     default:
0111         return QStringLiteral("Stopped");
0112         break;
0113     }
0114 }
0115 
0116 QString MediaPlayer2Player::LoopStatus() const
0117 {
0118     return QStringLiteral("None");
0119 }
0120 
0121 void MediaPlayer2Player::setLoopStatus(const QString &loopStatus) const
0122 {
0123     Q_UNUSED(loopStatus)
0124 }
0125 
0126 double MediaPlayer2Player::Rate() const
0127 {
0128     return 1.0;
0129 }
0130 
0131 void MediaPlayer2Player::setRate(double rate) const
0132 {
0133     Q_UNUSED(rate)
0134 }
0135 
0136 bool MediaPlayer2Player::Shuffle() const
0137 {
0138     return false;
0139 }
0140 
0141 void MediaPlayer2Player::setShuffle(bool shuffle) const
0142 {
0143     Q_UNUSED(shuffle)
0144 }
0145 
0146 QVariantMap MediaPlayer2Player::Metadata() const
0147 {
0148     QVariantMap metaData;
0149 
0150     switch (Dragon::engine()->mediaSourceType()) {
0151     case Phonon::MediaSource::Invalid:
0152     case Phonon::MediaSource::Empty:
0153         break;
0154     default:
0155         metaData = {
0156             {QStringLiteral("mpris:trackid"), QVariant::fromValue<QDBusObjectPath>(QDBusObjectPath(makeTrackId(Dragon::engine()->urlOrDisc()).constData()))},
0157             {QStringLiteral("mpris:length"), Dragon::engine()->length() * 1000},
0158             {QStringLiteral("xesam:url"), Dragon::engine()->urlOrDisc()},
0159         };
0160     }
0161 
0162     QMultiMap<QString, QString> phononMetaData = Dragon::engine()->metaData();
0163     QMultiMap<QString, QString>::const_iterator i = phononMetaData.constBegin();
0164 
0165     while (i != phononMetaData.constEnd()) {
0166         if (i.key() == QLatin1String("ALBUM") || i.key() == QLatin1String("TITLE"))
0167             metaData[QLatin1String("xesam:") + i.key().toLower()] = i.value();
0168         else if (i.key() == QLatin1String("ARTIST") || i.key() == QLatin1String("GENRE"))
0169             metaData[QLatin1String("xesam:") + i.key().toLower()] = QStringList(i.value());
0170 
0171         ++i;
0172     }
0173 
0174     return metaData;
0175 }
0176 
0177 double MediaPlayer2Player::Volume() const
0178 {
0179     return Dragon::engine()->volume();
0180 }
0181 
0182 void MediaPlayer2Player::setVolume(double volume) const
0183 {
0184     Dragon::engine()->setVolume(qBound(qreal(0.0), qreal(volume), qreal(1.0)));
0185 }
0186 
0187 qlonglong MediaPlayer2Player::Position() const
0188 {
0189     return Dragon::engine()->currentTime() * 1000;
0190 }
0191 
0192 double MediaPlayer2Player::MinimumRate() const
0193 {
0194     return 1.0;
0195 }
0196 
0197 double MediaPlayer2Player::MaximumRate() const
0198 {
0199     return 1.0;
0200 }
0201 
0202 bool MediaPlayer2Player::CanSeek() const
0203 {
0204     return Dragon::engine()->isSeekable();
0205 }
0206 
0207 void MediaPlayer2Player::Seek(qlonglong Offset) const
0208 {
0209     Dragon::engine()->seek(((Dragon::engine()->currentTime() * 1000) + Offset) / 1000);
0210 }
0211 
0212 bool MediaPlayer2Player::CanControl() const
0213 {
0214     return true;
0215 }
0216 
0217 void MediaPlayer2Player::tick(qint64 newPos)
0218 {
0219     if (newPos - oldPos > Dragon::engine()->tickInterval() + 250 || newPos < oldPos)
0220         Q_EMIT Seeked(newPos * 1000);
0221 
0222     oldPos = newPos;
0223 }
0224 
0225 void MediaPlayer2Player::emitMetadataChange() const
0226 {
0227     const QVariantMap properties{{QStringLiteral("Metadata"), Metadata()}};
0228     Mpris2::signalPropertiesChange(this, properties);
0229 }
0230 
0231 void MediaPlayer2Player::currentSourceChanged() const
0232 {
0233     const QVariantMap properties{
0234         {QStringLiteral("Metadata"), Metadata()},
0235         {QStringLiteral("CanSeek"), CanSeek()},
0236     };
0237     Mpris2::signalPropertiesChange(this, properties);
0238 }
0239 
0240 void MediaPlayer2Player::stateUpdated() const
0241 {
0242     const QVariantMap properties{
0243         {QStringLiteral("PlaybackStatus"), PlaybackStatus()},
0244         {QStringLiteral("CanPause"), CanPause()},
0245     };
0246     Mpris2::signalPropertiesChange(this, properties);
0247 }
0248 
0249 void MediaPlayer2Player::totalTimeChanged() const
0250 {
0251     const QVariantMap properties{{QStringLiteral("Metadata"), Metadata()}};
0252     Mpris2::signalPropertiesChange(this, properties);
0253 }
0254 
0255 void MediaPlayer2Player::seekableChanged(bool seekable) const
0256 {
0257     const QVariantMap properties{{QStringLiteral("CanSeek"), seekable}};
0258     Mpris2::signalPropertiesChange(this, properties);
0259 }
0260 
0261 void MediaPlayer2Player::volumeChanged() const
0262 {
0263     const QVariantMap properties{{QStringLiteral("Volume"), Volume()}};
0264     Mpris2::signalPropertiesChange(this, properties);
0265 }
0266 
0267 #include "moc_mediaplayer2player.cpp"