I just found out today that KTorrent ships with a DataEngine and Plasmoid. (Yes, sometimes I'm a bit slow. ;) Now I can watch my torrents progress without having to keep popping up the KTorrent window. Nice!
This inspired me to do a little bit of hacking on the applet this evening. When the icon is clicked, it now pops up the KTorrent window and if you drop an entry from KTorrent to the desktop KTorrent plasmoid is created.
Since KTorrent already has drag and drop support internally, all I had to do to get plasmoid creation working was add one line to the plasma-applet-ktorrent.desktop file: X-Plasma-DropMimeTypes=application/x-ktorrent-drag-object. Extra nice!
It took 17 lines of code, however, to have the plasmoid set itself to the dropped plasmoid. There's obviously room for improvement there in the Plasma API. Possible improvements might include a way to get at the drop data as text (would have eliminated 4 lines) and a way to query an engine for a sorce based on some data (QStringList DataEngine::sourcesMatching(const QString &key, const QVariant &value) would have eliminated another 6 lines). Those two additions would take those 17 lines of code and turn them into just 7.
It remains to be seen if these methods would have general purpose use outside of this one plasmoid, though. There's no point in expanding the Plasma API just for the KTorrent plasmoid ;) so it's an important question.
Finding and activating the window for KTorrent took only 8 lines of code, thanks to libtaskmanager and KWindowSystem. If this becomes a common thing for plasmoids to do we will probably want to provide a convenience function in libplasma for this to make it easy for plasmoids and to avoid having to bind all of a libtaskmanager for scripted plasmoids.
It also occurred to me that it would be cool to support dropping a torrent file to the desktop and having a KTorrent download plasmoid offered as an option. I haven't implemented this, but it would be easy enough to do with a Plasma::Service that encapsulated aspects of the KTorrent D-Bus service (which is rather comprehensive and well designed in my opinion). That way the plasmoid could just do engine("ktorrent")->serviceForSource("core") and use that to load the file. In fact, this approach could be used to provide interaction such as start/stop/pause as well.
And all of this because I found a cute new toy to enjoy but wanted it to be drag and drop aware. ;)