Skip to content

Commit

Permalink
Merge pull request #50 from wdehoog/master
Browse files Browse the repository at this point in the history
stuff to check if librespot is in the known devices list
  • Loading branch information
wdehoog authored Oct 12, 2018
2 parents 9cd1c48 + d3c1062 commit 0ecb260
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 11 deletions.
60 changes: 51 additions & 9 deletions qml/components/Librespot.qml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import org.nemomobile.dbus 2.0
Item {

property alias serviceEnabled: manager.librespotServiceEnabled
property alias serviceRunning: librespotService.serviceRunning
property alias serviceRunning: librespotUnit.serviceRunning

/*onLibrespotServiceEnabled: {
console.log("onLibrespotServiceEnabled: " + librespotServiceEnabled)
Expand All @@ -21,7 +21,7 @@ Item {
}*/

DBusInterface {
id: librespotService
id: librespotUnit

property bool serviceRunning: false

Expand All @@ -32,9 +32,9 @@ Item {
signalsEnabled: true

function updateState() {
console.log("librespotService.updateState: path=" + path)
console.log("librespotUnit.updateState: path=" + path)
if (path !== "") {
var activeState = librespotService.getProperty("ActiveState")
var activeState = librespotUnit.getProperty("ActiveState")
if (activeState === "active") {
serviceRunning = true
} else if(activeState === "inactive") {
Expand All @@ -52,6 +52,46 @@ Item {
}
}

function getName() {
return getEnvironmentVariable("DEVICE_NAME")
}

function getEnvironmentVariable(name) {
var i
if(!librespotService.environment)
return ""
for(i=0;i<librespotService.environment.length;i++) {
var s = librespotService.environment[i].split("=")
if(s[0] === name)
return s[1]
}
return ""
}

DBusInterface {
id: librespotService

property var environment: null

bus: DBus.SessionBus
service: "org.freedesktop.systemd1"
iface: "org.freedesktop.systemd1.Service"

function readServiceProperty(property) {
if (path !== "") {
environment = librespotService.getProperty(property)
console.log(JSON.stringify(environment))
}
}

onPathChanged: {
if(path !== "") {
//readServiceProperty("ExecStart")
readServiceProperty("Environment")
}
}
}

DBusInterface {
id: manager

Expand Down Expand Up @@ -116,7 +156,7 @@ Item {
}

function updateEnabled() {
typedCall("GetUnitFileState", [{"type": "s", "value": librespotService.serviceName}],
typedCall("GetUnitFileState", [{"type": "s", "value": librespotUnit.serviceName}],
function(state) {
// seems to be 'static'
if (state !== "disabled" && state !== "invalid") {
Expand All @@ -131,11 +171,13 @@ Item {
}

function updatePath() {
typedCall("GetUnit", [{ "type": "s", "value": librespotService.serviceName}],
typedCall("GetUnit", [{ "type": "s", "value": librespotUnit.serviceName}],
function(unit) {
librespotUnit.path = unit
librespotService.path = unit
},
function() {
librespotUnit.path = ""
librespotService.path = ""
})
}
Expand All @@ -158,18 +200,18 @@ Item {
signal jobRemoved(int id, string path, string unit, string result)
onJobRemoved: {
if(systemdJob === path)
librespotService.updateState()
librespotUnit.updateState()
console.log("onJobRemoved id:" + id + ", path:" + path + ", unit:" + unit + ", result:" + result)
}
}

function start() {
// when already running we still restart so Librespot will reregister at the Spotify servers
// so it hopefully appears in the list of available devices
manager.restartUnit(librespotService.serviceName)
manager.restartUnit(librespotUnit.serviceName)
}

function stop() {
manager.stopUnit(librespotService.serviceName)
manager.stopUnit(librespotUnit.serviceName)
}
}
8 changes: 6 additions & 2 deletions qml/components/SpotifyController.qml
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ Item {
}
}

Connections {
/*Connections {
target: app
onStateChanged: {
if (app.state === Qt.ApplicationActive) {
app.controller.reloadDevices();
}
}
}
}*/

Connections {
target: spotify
Expand Down Expand Up @@ -100,17 +100,21 @@ Item {
})
}

signal devicesReloaded()

function reloadDevices() {
Spotify.getMyDevices(function(error, data) {
if (data) {
try {
console.log("reloadDevices() #devices: " + data.devices.length)
devicesModel.clear();
for (var i=0; i < data.devices.length; i++) {
//console.log(JSON.stringify(data.devices[i]))
devicesModel.append(data.devices[i]);
if (data.devices[i].is_active)
playbackState.device = data.devices[i]
}
devicesReloaded()
} catch (err) {
console.log("reloadDevices() error: " + err)
}
Expand Down
30 changes: 30 additions & 0 deletions qml/hutspot.qml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ ApplicationWindow {

property alias queue: queue
property alias playingPage: playingPage
property alias librespot: librespot

allowedOrientations: defaultAllowedOrientations

Expand Down Expand Up @@ -814,6 +815,35 @@ ApplicationWindow {
id: librespot
}

// check if the Librespot service is known to Spotify
function isLibrespotInDevicesList() {
var i
// we cannot determine the name if it is not running
if(!librespot.serviceRunning)
return false
var devName = librespot.getName()
if(devName.length === 0) // failed to determine the name
return false
for(i=0;i<spotifyController.devices.count;i++) {
var device = spotifyController.devices.get(i)
if(device.name === devName)
return true
}
return false
}

Connections {
target: spotifyController
onDevicesReloaded: {
// check if Librespot is known to Spotify and if not restart it
if(app.start_stop_librespot.value
&& !isLibrespotInDevicesList()) {
console.log("Librespot is not in the devices list so restart it")
librespot.start()
}
}
}

function getAppIconSource() {
return getAppIconSource2(Theme.iconSizeExtraLarge)
}
Expand Down

0 comments on commit 0ecb260

Please sign in to comment.