I recently upgraded to Kubuntu Karmic and since it’s so shiny (I’m a sucker for a bit of eye candy…so long as it doesn’t get in my way) I decided to try my hand at making a widget. Now, I’ve tinkered with this before and made a widget that gets a fortune but that was simple. I want something a bit more complicated. Since I’ve also recently found the python-twitter API it seemed quite a nice little project (yes I’m aware it’s been done before).
KDE TechBase provides some good starting documentation but I had a lot of trouble with just getting a simple data engine set up, installed and a plasmoid that could connect to it. The documentation does a nice job of telling you how to make a data engine, how to make a widget but then gave an over complicated example of how to use to use the two together. So here is the result of some of my trial and error in this simple task…
Let’s start with the data engine.
from PyQt4.QtCore import *
from PyKDE4.kdecore import *
from PyQt4.QtGui import *
from PyKDE4.plasma import Plasma
from PyKDE4 import plasmascript
import twitter
class PyTwitterData(plasmascript.DataEngine):
def __init__(self,parent,args=None):
plasmascript.DataEngine.__init__(self,parent)
# Set up the initial stuff. self.source_dict is just something I was trying out. Read the note
# in updateSourceEvent
def init(self):
self.source_dict = {'username':'password'}
# Minimum polling interval is in milliseconds
self.setMinimumPollingInterval(30000)
# Return the available sources
def sources(self):
sources = ['username']
return sources
# This gets called when the data engine has new data or when polled
def sourceRequestEvent(self, name):
return self.updateSourceEvent(name)
def updateSourceEvent(self, source):
# username and password of the required user. Note that the original idea was to allow
# the use of a dictionary to store the username and password but for some reason it
# wasn't storing it properly so you may have to just replace this to use your normal
# username and password here.
twitter_username = source
twitter_password = self.source_dict.get(str(source), 'default_password')
# Twitter API things. This just makes a twitter API object that gets you own status
api_base = twitter.Api(username=twitter_username,
password=twitter_password)
my_status = api_base.GetUser('username').GetStatus().GetText()
# Set the in the 'Status' key of source (username) to the status string
self.setData(source, 'Status', my_status)
# As far as I know this always returns true so return True!
return True
# Create and return the data engine object
def CreateDataEngine(parent):
return PyTwitterData(parent)
If you’ve read the KDE TechBase documentation you’ll know that this code is meant to go in a file named main.py. The directory structure for the data engine is as follows:
.
| — contents
| `– code
| |– main.py
| — metadata.desktop
` — twitterdataengine.zip
As stated, the code above goes in the main.py file. The metadata.desktop file is important and contains all of the information required to install this data engine. Mine looks like the following:
[Desktop Entry]
Encoding=UTF-8
Name=twitterdataengine
Type=Service
ServiceTypes=Plasma/DataEngine
X-Plasma-API=python
X-Plasma-MainScript=code/main.py
X-KDE-PluginInfo-Author=ScaryClam
X-KDE-PluginInfo-Email=twitterdataengine@scaryclam.co.uk
X-KDE-PluginInfo-Name=twitterdataengine
X-KDE-PluginInfo-Version=1.0
X-KDE-PluginInfo-Website=http://plasma.kde.org/
X-KDE-PluginInfo-Category=Examples
X-KDE-PluginInfo-Depends=
X-KDE-PluginInfo-License=GPL
X-KDE-PluginInfo-EnabledByDefault=true
When the main.py and metadata.desktop file are complete, the contents need zipping. From the base directory for this dataengine run:
$ zip -r twitterdataengine.zip *
Which should zip the entire directory structure into a file named twitterdataengine.zip.
Installing the data engine required the command:
$ plasmapkg -t dataengine -i twitterdataengine.zip
As a side note, removing the data engine also requires the -t dataengine option, obvious when you know but a pain when you’re not thinking and leave it out.
OK, if everything went smoothly the data engine is now installed and ready to be used. A nice little utility for examining data engine is plasmaengine which allows you to have a look at the different engines available and their current sources/values.
Now for the plasmoid to make use of the data engine!
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyKDE4.plasma import Plasma
from PyKDE4 import plasmascript
class TwitterClient(plasmascript.Applet):
def __init__(self,parent,args=None):
plasmascript.Applet.__init__(self,parent)
def init(self):
# Make a widget, 300x300, no configuration (to keep this simple) and
# connect to the data engine
self.setHasConfigurationInterface(False)
self.resize(300, 300)
self.setAspectRatioMode(Plasma.Square)
self.status_text = 'No Status set!'
self.connectToEngine()
def paintInterface(self, painter, option, rect):
# This will just print the status text into an otherwise blank widget
painter.save()
painter.setPen(Qt.white)
painter.drawText(rect, Qt.AlignVCenter | Qt.AlignHCenter,
self.status_text)
painter.restore()
def connectToEngine(self):
# Connect to the source "username" in the twitterdataengine data engine,
# every 30000 milliseconds (30 seconds)
self.twitterEngine = self.dataEngine("twitterdataengine")
self.twitterEngine.connectSource('username', self, 30000)
# This will get called when polled
@pyqtSignature("dataUpdated(const QString &, const Plasma::DataEngine::Data &)")
def dataUpdated(self, sourceName, data):
# data contains all of the data from the source requested. In this case
# it just has one key: "Status"
status = data[QString("Status")]
self.status_text = status
self.update()
def CreateApplet(parent):
# Create applet
return TwitterClient(parent)
Again, this should go into the main.py file and the directory structure is:
.
| — contents
| `– code
| |– main.py
| — metadata.desktop
` — twitterclient.zip
The metadata.desktop file is:
[Desktop Entry]
Encoding=UTF-8
Name=TwitterClient
Type=Service
ServiceTypes=Plasma/Applet
Icon=games-hint
X-Plasma-API=python
X-Plasma-MainScript=code/main.py
X-KDE-PluginInfo-Author=ScaryClam
X-KDE-PluginInfo-Email=twitterclient@scaryclam.co.uk
X-KDE-PluginInfo-Name=twitter_client
X-KDE-PluginInfo-Version=1.0
X-KDE-PluginInfo-Website=http://plasma.kde.org/
X-KDE-PluginInfo-Category=Examples
X-KDE-PluginInfo-Depends=
X-KDE-PluginInfo-License=GPL
X-KDE-PluginInfo-EnabledByDefault=true
Zip up the directory contents (in the same way as the data engine) and then install using:
$ plasmapkg -i twitter_client.zip
If successful, the installed plasmoid can be seen by using the plasmoidviewer utility:
$ plasmoidviewer twitter_client
Hopefully there is now a widget showing the status text of the twitter user entered.