And the bindings keep rocking: Writing Ruby KIO slaves

It seems like I got into a hacking frenzy after yesterday’s Ruby plugins for Kate. Today I sat down and took another look at the KRubyPluginFactory to implement KIO slave support.

So here it is, our very first Ruby Hello World KIO slave:

# kate: space-indent on; indent-width 2;

require 'korundum4'
require 'kio'

module Rubytest

  class Main < KIO::SlaveBase

    def initialize protocol, pool_sock, app_sock
      super protocol, pool_sock, app_sock
    end

    def get url
      mimeType 'text/plain'

      data Qt::ByteArray.new('Hello World from our first Ruby KIO slave!')

      finished
    end

  end

end

We’ll also need a rubytest.protocol file that describes our new protocol:

[Protocol]
exec=krubypluginfactory
input=none
output=filesystem
protocol=rubytest
reading=true

To test it, you just have to type rubytest:/ into konqi’s addressbar and you should see nice hello world greeting.

Some more info on the structure of a Ruby kio slave: the Ruby script always has to be named ‘main.rb’ and the SlaveBase derived class always ‘Main’. The module name is the camelized form of the protocol (so a protocol ‘foo_bar’ would map to a module name ‘FooBar’). The script has to reside in <kde prefix>/share/apps/kio_<protocol name>.
The .protocol file itself should be installed to <kde prefix>/share/kde4/services.

As with the Kate plugin, I packaged this example in a tarball. It ships a Makefile and can easily be installed to your home directory with ‘make install’.
I committed this feature to both trunk and the 4.5 branch – so you can soon start coding your own KIO slaves in Ruby! 🙂