Commit 23cc5b7f authored by Andrea Scarpino's avatar Andrea Scarpino

Fetch song informations from Media Player

Fixes #2
parent b02e4f9b
......@@ -2,10 +2,15 @@ TARGET = harbour-lyrics
CONFIG += sailfishapp
QT += dbus
DBUS_INTERFACES += org.mpris.MediaPlayer2.Player.xml
SOURCES += \
src/main.cpp \
src/lyricsmanager.cpp \
src/lyric.cpp \
src/mediaplayerscanner.cpp \
src/chartlyricsapi.cpp \
src/geniusapi.cpp \
src/provider.cpp \
......@@ -15,6 +20,7 @@ HEADERS += \
src/lyricsmanager.h \
src/lyric_p.h \
src/lyric.h \
src/mediaplayerscanner.h \
src/chartlyricsapi.h \
src/geniusapi.h \
src/geniusapi_secret.h \
......@@ -30,6 +36,7 @@ OTHER_FILES += \
rpm/harbour-lyrics.changes \
rpm/harbour-lyrics.spec \
rpm/harbour-lyrics.yaml \
org.mpris.MediaPlayer2.Player.xml \
translations/*.ts
CONFIG += sailfishapp_i18n
......
<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node>
<interface name="org.mpris.MediaPlayer2.Player">
<method name="Next"/>
<method name="Previous"/>
<method name="Pause"/>
<method name="PlayPause"/>
<method name="Stop"/>
<method name="Play"/>
<method name="Seek">
<arg direction="in" type="x" name="Offset"/>
</method>
<method name="SetPosition">
<arg direction="in" type="o" name="TrackId"/>
<arg direction="in" type="x" name="Position"/>
</method>
<method name="OpenUri">
<arg direction="in" type="s" name="Uri"/>
<annotation value="QUrl" name="org.qtproject.QtDBus.QtTypeName.In0"/>
</method>
<signal name="Seeked">
<arg direction="out" type="x" name="Position"/>
</signal>
<property access="read" type="s" name="PlaybackStatus"/>
<property access="readwrite" type="s" name="LoopStatus"/>
<property access="readwrite" type="d" name="Rate"/>
<property access="readwrite" type="b" name="Shuffle"/>
<property access="read" type="a{sv}" name="Metadata">
<annotation value="QVariantMap" name="org.qtproject.QtDBus.QtTypeName"/>
</property>
<property access="readwrite" type="d" name="Volume"/>
<property access="read" type="x" name="Position">
<annotation value="false" name="org.freedesktop.DBus.Property.EmitsChangedSignal"/>
</property>
<property access="read" type="d" name="MinimumRate"/>
<property access="read" type="d" name="MaximumRate"/>
<property access="read" type="b" name="CanGoNext"/>
<property access="read" type="b" name="CanGoPrevious"/>
<property access="read" type="b" name="CanPlay"/>
<property access="read" type="b" name="CanPause"/>
<property access="read" type="b" name="CanSeek"/>
<property access="read" type="b" name="CanControl">
<annotation value="false" name="org.freedesktop.DBus.Property.EmitsChangedSignal"/>
</property>
</interface>
</node>
......@@ -29,6 +29,8 @@ Page {
allowedOrientations: Orientation.All
property bool userTyping: false;
Connections {
target: manager
......@@ -47,6 +49,15 @@ Page {
songText.text = "Not found :-("
}
}
onMediaPlayerInfo: {
console.log("Fetched song informations from Media Player");
if (!userTyping) {
artist.text = songArtist;
song.text = songTitle;
searchLyric();
}
}
}
SilicaFlickable {
......@@ -108,6 +119,7 @@ Page {
placeholderText: qsTr("Artist")
onTextChanged: search.enabled = (text.length > 0 && song.text.length > 0)
onActiveFocusChanged: userTyping = artist.activeFocus
EnterKey.enabled: text.length > 0 && song.text.length > 0
EnterKey.onClicked: searchLyric();
......@@ -119,6 +131,7 @@ Page {
placeholderText: qsTr("Song")
onTextChanged: search.enabled = (text.length > 0 && artist.text.length > 0)
onActiveFocusChanged: userTyping = song.activeFocus
EnterKey.enabled: text.length > 0 && artist.text.length > 0
EnterKey.onClicked: searchLyric();
......
......@@ -60,6 +60,18 @@ Page {
}
}
TextSwitch {
id: scanner
width: page.width
text: qsTr("Enable Media Player scanner")
description: qsTr("Checks Media Player to get song info. Do not focus any field to allow text substitution.")
checked: true
onCheckedChanged: {
manager.setMediaPlayerScanner(checked);
}
}
Button {
text: qsTr("Clear cache")
anchors.horizontalCenter: parent.horizontalCenter
......@@ -77,6 +89,8 @@ Page {
} else {
provider.currentIndex = 2;
}
scanner.checked = manager.getMediaPlayerScanner();
}
onStatusChanged: {
......
......@@ -33,21 +33,25 @@
#include "chartlyricsapi.h"
#include "geniusapi.h"
#include "lyricsmaniaapi.h"
#include "mediaplayerscanner.h"
#include "provider.h"
LyricsManager::LyricsManager(QObject *parent) :
QObject(parent)
, api(0)
, mpScanner(0)
{
settings = new QSettings(QCoreApplication::applicationName(), QCoreApplication::applicationName(), this);
setProvider(settings->value("Provider").toString());
setMediaPlayerScanner(settings->value("MediaPlayerScanner", true).toBool());
}
LyricsManager::~LyricsManager()
{
delete settings;
delete api;
delete mpScanner;
}
void LyricsManager::clearCache()
......@@ -140,3 +144,21 @@ void LyricsManager::search(const QString &artist, const QString &song)
connect(api, &Provider::lyricFetched, this, &LyricsManager::storeLyric);
}
}
bool LyricsManager::getMediaPlayerScanner() const
{
return settings->value("MediaPlayerScanner").toBool();
}
void LyricsManager::setMediaPlayerScanner(const bool enabled)
{
if (enabled) {
mpScanner = new MediaPlayerScanner();
connect(mpScanner, &MediaPlayerScanner::mediaPlayerInfo, this, &LyricsManager::mediaPlayerInfo);
} else if (mpScanner) {
delete mpScanner;
mpScanner = 0;
}
settings->setValue("MediaPlayerScanner", enabled);
}
......@@ -32,6 +32,7 @@
class QSettings;
class Provider;
class MediaPlayerScanner;
class LyricsManager : public QObject
{
......@@ -43,10 +44,13 @@ public:
Q_INVOKABLE void clearCache();
Q_INVOKABLE QString getProvider() const;
Q_INVOKABLE void search(const QString &artist, const QString &song);
Q_INVOKABLE bool getMediaPlayerScanner() const;
Q_INVOKABLE void setMediaPlayerScanner(const bool enabled);
Q_INVOKABLE void setProvider(const QString &provider);
Q_SIGNALS:
void searchResult(Lyric *lyric, const bool &found);
void mediaPlayerInfo(const QString &songArtist, const QString &songTitle);
private:
QString getLyricsDir() const;
......@@ -54,6 +58,7 @@ private:
QSettings *settings;
Provider *api;
MediaPlayerScanner *mpScanner;
};
......
/*
The MIT License (MIT)
Copyright (c) 2016 Andrea Scarpino <me@andreascarpino.it>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "mediaplayerscanner.h"
#include <QTimer>
const QString MediaPlayerScanner::MP_DBUS_SERVICE(QLatin1String("org.mpris.MediaPlayer2.jolla-mediaplayer"));
const QString MediaPlayerScanner::MP_DBUS_DAEMON_PATH(QLatin1String("/org/mpris/MediaPlayer2"));
MediaPlayerScanner::MediaPlayerScanner(QObject *parent) :
QObject(parent)
, impface(MediaPlayerScanner::MP_DBUS_SERVICE, MediaPlayerScanner::MP_DBUS_DAEMON_PATH, QDBusConnection::sessionBus())
, m_timer(new QTimer())
{
fetchInfo();
m_timer->start(5 * 1000);
connect(m_timer, &QTimer::timeout, this, &MediaPlayerScanner::fetchInfo);
}
MediaPlayerScanner::~MediaPlayerScanner()
{
delete m_timer;
}
void MediaPlayerScanner::fetchInfo()
{
const QVariantMap map = impface.metadata();
const QStringList artists(map["xesam:artist"].toStringList());
const QString title(map["xesam:title"].toString());
if (!artists.isEmpty() && !title.isEmpty()) {
Q_EMIT mediaPlayerInfo(artists.at(0), title);
}
}
/*
The MIT License (MIT)
Copyright (c) 2016 Andrea Scarpino <me@andreascarpino.it>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef MEDIAPLAYERSCANNER_H
#define MEDIAPLAYERSCANNER_H
#include <QObject>
#include "mediaplayer2_interface.h"
class QTimer;
class MediaPlayerScanner : public QObject
{
Q_OBJECT
public:
explicit MediaPlayerScanner(QObject *parent = 0);
virtual ~MediaPlayerScanner();
Q_SIGNALS:
void mediaPlayerInfo(const QString &songArtist, const QString &songTitle);
private:
static const QString MP_DBUS_SERVICE;
static const QString MP_DBUS_DAEMON_PATH;
void fetchInfo();
OrgMprisMediaPlayer2PlayerInterface impface;
QTimer* m_timer;
};
#endif // MEDIAPLAYERSCANNER_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment