What’s Playing, Doc?

So, I was round at a friend’s, and he was playing with Amarok and the Plasma widgets screensaver, and wanted a way to see what was currently playing when his computer was locked.  Of course, I pointed him towards the Now Playing applet, but it was overkill for what he wanted – he didn’t want the buttons, or the sliders – just a display of the current information.  Well, I was in the mood for playing, and it took me about 15 minutes to construct a javascript plasmoid, most of which was spent researching how to use data engines from them.

The code I left him with was:

layout = new LinearLayout(plasmoid);
layout.setOrientation(QtVertical);

label = new Label();
layout.addItem(label);
label.text = 'No player'

plasmoid.dataUpdate = function(name, data) {
    label.text = 'Info:\n';
    for (var key in data) {
        label.text += key + ': ' + data[key] + '\n';
    }
}

plasmoid.dataEngine("nowplaying").connectSource("org.mpris.amarok", plasmoid, 500);

This was in a file called “main.js” in a subfolder called “contents”. In the plasmoid folder (the folder containing the contents folder), I also put in a metadata.desktop file:


[Desktop Entry]
Name=Simple Now Playing
Comment=Now Playing as Matt likes it
Icon=applications-multimedia
Type=Service
X-Plasma-API=javascript
X-Plasma-MainScript=main.js
X-Plasma-DefaultSize=200,100
X-KDE-ServiceTypes=Plasma/Applet
X-KDE-PluginInfo-Author=Alex Merry
X-KDE-PluginInfo-Email=alex.merry [SPAMNO]@[SPAMNO] kdemail.org
X-KDE-PluginInfo-Name=simplenowplaying
X-KDE-PluginInfo-Version=0.1
X-KDE-PluginInfo-Website=http://plasma.kde.org/
X-KDE-PluginInfo-Category=Multimedia
X-KDE-PluginInfo-Depends=
X-KDE-PluginInfo-License=GPL
X-KDE-PluginInfo-EnabledByDefault=true

Now it was just a case of calling plasmapkg -i . from within the plasmoid directory, and the simple widget was installed. This produces the following:

simple-now-playing

Of course, this will only work with Amarok, and doesn’t respond to Amarok being started or quit. That’s OK, it’s not much more work to deal with that:


layout = new LinearLayout(plasmoid);
layout.setOrientation(QtVertical);

label = new Label();
layout.addItem(label);
label.text = "No player found";

function firstSource() {
	var sources = plasmoid.dataEngine("nowplaying").sources;
	if (sources.length) {
		return sources[0];
	} else {
		label.text = "No player found";
		return '';
	}
}

plasmoid.dataUpdate = function(name, data) {
	if (source == name) {
		label.text = 'Info:\n';
		for (var key in data) {
			label.text += key + ': ' + data[key] + '\n';	
		}
	}
}

source = firstSource();

npDataEngine = plasmoid.dataEngine("nowplaying");

npDataEngine.sourceRemoved.connect(function(name) {
	if (name == source) {
		source = firstSource();
		if (source) {
			npDataEngine.connect(source, plasmoid, 500);
		}
	}
});

npDataEngine.sourceAdded.connect(function(name) {
	if (!source) {
		source = name;
		npDataEngine.connect(source, plasmoid, 500);
	}
});

if (source) {
	npDataEngine.connectSource(source, plasmoid, 500);
}

Of course, it’s not much work to only show the things you’re interested in, but this covers the interesting parts of the plasmoid.

Interesting note: it appears that if you replace the line label.text = 'Info:\n'; with label.text = '';, the subsequent calls to label.text += ... don’t work – you just end up with a blank label.

About these ads

Tags: , , , ,

4 Responses to “What’s Playing, Doc?”

  1. luis Says:

    Very nice. As a KDE+XBMC user, I wonder if the Now playing plasmoid/widget could be extended to support XBMC. Sorry if it’s not the right place to ask.

    Thanks

  2. randomguy3 Says:

    It’s something I’ll look into.

  3. luis Says:

    Well, thanks a lot. I cannot code, just a Kde user I appreciate and thank all the hard work you people do.

  4. Tony Murray Says:

    Nice job, Alex. Now we just need an applet that does control only ;)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: