Archive for May, 2009

Akademy rooms

25th May 2009

I figured I’d follow Peter Zhou’s lead and ask if anyone is still looking for a roommate at Akademy. I don’t bite, I promise…

What’s Playing, Doc?

17th May 2009

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.


Follow

Get every new post delivered to your Inbox.