Skip to content
mprisinterface.cpp 5.48 KiB
Newer Older
/*
 * Copyright (C) 2019  Kevin Whitaker <eyecreate@eyecreate.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "mprisinterface.h"
#include <qcryptographichash.h>

MPRISInterface::MPRISInterface(QObject* parent) : QDBusAbstractAdaptor(parent)
{
}

bool MPRISInterface::CanQuit()
{
    return false;
}

bool MPRISInterface::CanRaise()
{
    return false;
}

bool MPRISInterface::HasTrackList()
{
    return false; //TODO:this might have value implementing.
}

QString MPRISInterface::Identity()
{
    return "Simple Cast";
}

QStringList MPRISInterface::SupportedMimeTypes()
{
    return {"audio/ogg","audio/mpeg","audio/webm"};
}

QStringList MPRISInterface::SupportedUriSchemes()
{
    return {"https","http"};
}



MPRISPlayerInterface::MPRISPlayerInterface(MiniMediaPlayer *player,QObject* parent) : QDBusAbstractAdaptor(parent),player(player)
{
    //Connected Changed values from media player.
    connect(player,&MiniMediaPlayer::playStateChanged,this,&MPRISPlayerInterface::mediaStateChanged);
    connect(player,&MiniMediaPlayer::playlistChanged,this,&MPRISPlayerInterface::mediaPlaylistChanged);
    connect(player,&MiniMediaPlayer::mediaVolumeChanged,this,&MPRISPlayerInterface::mediaVolumeChanged);
    connect(player,&MiniMediaPlayer::trackPositionChanged,this,&MPRISPlayerInterface::mediaPositionChanged);
}

void MPRISPlayerInterface::dbusUpdate(QVariantMap changed)
{
    //Workaround for qt dbus not sending this signal
    QDBusMessage msg = QDBusMessage::createSignal("/org/mpris/MediaPlayer2","org.freedesktop.DBus.Properties","PropertiesChanged");
    msg << "org.mpris.MediaPlayer2.Player";
    msg << changed;
    msg << QStringList();
    QDBusConnection::sessionBus().send(msg);
}

void MPRISPlayerInterface::mediaPositionChanged(qint64 position)
{
    emit positionChanged(position * 1000); //milliseconds to microseconds
    map.insert("Position",position * 1000);
void MPRISPlayerInterface::mediaStateChanged(bool isPlaying)
{
    emit playbackStatusChanged(PlaybackStatus());
    QVariantMap map;
    map.insert("PlaybackStatus",PlaybackStatus());
    dbusUpdate(map);
}

void MPRISPlayerInterface::mediaVolumeChanged(int volume)
{
    emit volumeChanged(volumeFromMediaPlayer(volume));
    map.insert("Volume",volumeFromMediaPlayer(volume));
}

void MPRISPlayerInterface::mediaPlaylistChanged(QStringList playlist)
{
    if(playlist.size() > 0)
    {
        emit metadataChanged(Metadata());
        QVariantMap map;
        map.insert("Metadata",Metadata());
        dbusUpdate(map);
    }
    else
    {
        mediaStateChanged(false);
    }
}

bool MPRISPlayerInterface::CanControl()
{
    return true;
}

bool MPRISPlayerInterface::CanGoNext()
{
    return true;
}

bool MPRISPlayerInterface::CanGoPrevious()
{
    return false;
}

bool MPRISPlayerInterface::CanSeek()
{
    return false;
}

bool MPRISPlayerInterface::CanPause()
{
    return true;
}

bool MPRISPlayerInterface::CanPlay()
{
    return true;
}

double MPRISPlayerInterface::MaximumRate()
{
    return 1.0;
}

double MPRISPlayerInterface::MinimumRate()
{
    return 1.0;
}

void MPRISPlayerInterface::OpenUri(QString uri)
{
    //TODO:this might be another way to add tracks
}

void MPRISPlayerInterface::Next()
{
    player->nextTrack();
}

void MPRISPlayerInterface::Pause()
{
    player->playPauseTrack(false);
}

void MPRISPlayerInterface::Play()
{
    player->playPauseTrack(true);
}

void MPRISPlayerInterface::PlayPause()
{
    player->playPauseTrack(!player->isPlaying());
}

void MPRISPlayerInterface::Stop()
{
    //TODO: think if worth implementing.
}

QString MPRISPlayerInterface::PlaybackStatus()
{
    if(player->getTrackNames().size() == 0)
    {
        return "Stopped";
    }
    else if(player->isPlaying()) {
        return "Playing";
    }
    else
    {
        return "Paused";
    }
}

qlonglong MPRISPlayerInterface::Position()
{
    return player->getCurrentPosition() * 1000; //milliseconds to microseconds
}

double MPRISPlayerInterface::Rate()
{
    return 1.0;
}

double MPRISPlayerInterface::Volume()
{
    return volumeFromMediaPlayer(player->getVolume());
}

double MPRISPlayerInterface::volumeFromMediaPlayer(int volume)
{
    if(volume== 0) return 0;
    else return volume/100;
}

void MPRISPlayerInterface::setVolume(double volume)
{
    if(volume < 0) player->setVolume(0);
    player->setVolume(volume*100); //QMediaPlayer uses 0 to 100
}

QVariantMap MPRISPlayerInterface::Metadata()
    QVariantMap map;
    if(player->getTrackNames().size() > 0)
    {
        map.insert("mpris:trackid",QVariant("org.mpris.MediaPlayer2.simplecast.Player."+QString(QCryptographicHash::hash(player->getTrackNames()[0].toUtf8(),QCryptographicHash::Algorithm::Md5))));
        map.insert("mpris:length",QVariant(player->getTrackDuration()*1000));
        map.insert("xesam:title",QVariant(player->getTrackNames()[0]));
    }