Difference between revisions of "Development/Tutorials/KIO Slaves/Hello World"

Jump to: navigation, search
m
(Compile the stuff: fix typo & use generic way to find Qt include dir)
 
(7 intermediate revisions by 5 users not shown)
Line 1: Line 1:
 
= Understanding =
 
= Understanding =
 
A kioslave allows you to represent any kind of storage in a way you want. As an example, the kio_http kioslave loads data from the network over the http (protocol) and shows it rendered as html. Technically, a kioslave is a shared object plus its description. E.g. the imap4 kioslave consist of the following files:
 
A kioslave allows you to represent any kind of storage in a way you want. As an example, the kio_http kioslave loads data from the network over the http (protocol) and shows it rendered as html. Technically, a kioslave is a shared object plus its description. E.g. the imap4 kioslave consist of the following files:
tweedleburg:/usr/local # find -iname "*imap4*"
+
<syntaxhighlight lang="text">
./lib/kde4/kio_imap4.so
+
tweedleburg:/usr/local # find -iname "*imap4*"
./share/kde4/services/imap4.protocol
+
./lib/kde4/kio_imap4.so
 +
./share/kde4/services/imap4.protocol
 +
</syntaxhighlight>
  
 
= The files =
 
= The files =
Line 9: Line 11:
  
 
== CMakeLists.txt ==
 
== CMakeLists.txt ==
<pre>
+
<syntaxhighlight lang="cmake">
 
PROJECT( tutorial )
 
PROJECT( tutorial )
 
FIND_PACKAGE(KDE4 REQUIRED)
 
FIND_PACKAGE(KDE4 REQUIRED)
Line 27: Line 29:
  
 
install(FILES hello.protocol DESTINATION ${SERVICES_INSTALL_DIR})
 
install(FILES hello.protocol DESTINATION ${SERVICES_INSTALL_DIR})
</pre>
+
</syntaxhighlight>
  
 
== hello.h ==
 
== hello.h ==
<code cppqt>
+
<syntaxhighlight lang="cpp-qt">
 
#ifndef HELLO_H
 
#ifndef HELLO_H
 
#define HELLO_H
 
#define HELLO_H
Line 47: Line 49:
  
 
#endif
 
#endif
</code>
+
</syntaxhighlight>
  
 
== hello.cpp ==
 
== hello.cpp ==
<code cppqt>
+
<syntaxhighlight lang="cpp-qt">
 
#include "hello.h"
 
#include "hello.h"
#include <kdebug.h>  
+
#include <kdebug.h>
 
#include <kcomponentdata.h>
 
#include <kcomponentdata.h>
  
  
 
extern "C" int KDE_EXPORT kdemain( int argc, char **argv )
 
extern "C" int KDE_EXPORT kdemain( int argc, char **argv )
{                                  
+
{
 
   kDebug(7000) << "Entering function";
 
   kDebug(7000) << "Entering function";
 
   KComponentData instance( "kio_hello" );
 
   KComponentData instance( "kio_hello" );
  
   if (argc != 4)  
+
   if (argc != 4)
 
   {
 
   {
 
     fprintf( stderr, "Usage: kio_hello protocol domain-socket1 domain-socket2\n");
 
     fprintf( stderr, "Usage: kio_hello protocol domain-socket1 domain-socket2\n");
Line 83: Line 85:
 
hello::hello( const QByteArray &pool, const QByteArray &app )
 
hello::hello( const QByteArray &pool, const QByteArray &app )
 
: SlaveBase( "hello", pool, app ) {}
 
: SlaveBase( "hello", pool, app ) {}
</code>
+
</syntaxhighlight>
  
 
== hello.protocol ==  
 
== hello.protocol ==  
[Protocol]
+
<syntaxhighlight lang="text">
DocPath=kioslave/kio_hello.html  
+
[Protocol]
exec=kio_hello
+
DocPath=kioslave/kio_hello.html
input=none
+
exec=kio_hello
output=filesystem
+
input=none
protocol=hello
+
output=filesystem
reading=true
+
protocol=hello
 +
reading=true
 +
</syntaxhighlight>
  
 
== Compile the stuff ==
 
== Compile the stuff ==
g++ -shared -lkdeui -lkio -lkdecore -fPIC -I/usr/local/include hello.cpp -o kio_hello.so
+
Create a new Folder "build":
 +
<syntaxhighlight lang="bash">
 +
mkdir build
 +
cd build
 +
</syntaxhighlight>
 +
Run cmake and make
 +
<syntaxhighlight lang="bash">
 +
cmake ..
 +
make
 +
</syntaxhighlight>
  
''this does not complile if the qt headers are not in /usr/local/include''
+
now you can install it (maybe you should use an experimental setup?)
 +
<syntaxhighlight lang="bash">
 +
make install
 +
</syntaxhighlight>
 +
 
 +
If you want to do this by hand:
 +
 
 +
<syntaxhighlight lang="bash">
 +
g++ -shared -lkdeui -lkio -lkdecore -fPIC -I$(qmake -query QT_INSTALL_HEADERS) hello.cpp -o kio_hello.so
 +
</syntaxhighlight>
  
''On my system I could fix it by changing -I/usr/local/include to -I/usr/include/qt4''
 
 
== Install the stuff ==
 
== Install the stuff ==
 +
 +
Now you can install it (maybe you should use an experimental setup?):
 +
<syntaxhighlight lang="bash">
 +
make install
 +
</syntaxhighlight>
 +
 +
or if you want to install it to your system:
 +
<syntaxhighlight lang="bash">
 +
sudo make install
 +
</syntaxhighlight>
 +
 +
Of course you can also do this by hand.
 +
 
Find out where your protocols are lying:
 
Find out where your protocols are lying:
kde4-config --path services
+
<syntaxhighlight lang="bash">
/usr/share/kde4/services/
+
kde4-config --path services
 +
  /usr/share/kde4/services/
 +
 
 +
kde4-config --path module
 +
  /usr/lib64/kde4/
  
cp kio_hello.so /usr/local/lib/kde4/
+
cp kio_hello.so /usr/local/lib/kde4/
cp kio_hello.so /usr/lib64/kde4/
+
cp kio_hello.so /usr/lib64/kde4/
cp kio_hello.protocol /usr/share/kde4/services/
+
cp kio_hello.protocol /usr/share/kde4/services/
 +
</syntaxhighlight>
  
 
= Test it =
 
= Test it =
 +
== In Konqueror ==
 
Start kinfocenter, choose hello as protocol. If this is possible, start konqueror, type hello:/// into the URL bar.
 
Start kinfocenter, choose hello as protocol. If this is possible, start konqueror, type hello:/// into the URL bar.
 +
== On the command line ==
 +
<syntaxhighlight lang="bash">
 +
kioclient 'cat' 'hello:///'
 +
</syntaxhighlight>

Latest revision as of 13:34, 20 November 2012

Contents

[edit] Understanding

A kioslave allows you to represent any kind of storage in a way you want. As an example, the kio_http kioslave loads data from the network over the http (protocol) and shows it rendered as html. Technically, a kioslave is a shared object plus its description. E.g. the imap4 kioslave consist of the following files:

tweedleburg:/usr/local # find -iname "*imap4*"
./lib/kde4/kio_imap4.so
./share/kde4/services/imap4.protocol

[edit] The files

We want to write a "hello world" kioslave here. This can be seen as a learning exercise and as a template for future programming projects.

[edit] CMakeLists.txt

PROJECT( tutorial )
FIND_PACKAGE(KDE4 REQUIRED)
INCLUDE_DIRECTORIES( ${KDE4_INCLUDES} . )
 
set(kio_hello_PART_SRCS
   hello.cpp)
 
kde4_add_plugin(kio_hello ${kio_hello_PART_SRCS})
 
target_link_libraries(kio_hello ${KDE4_KIO_LIBS})
 
install(TARGETS kio_hello  DESTINATION ${PLUGIN_INSTALL_DIR})
 
 
########### install files ###############
 
install(FILES hello.protocol DESTINATION ${SERVICES_INSTALL_DIR})

[edit] hello.h

#ifndef HELLO_H
#define HELLO_H
 
#include <kio/slavebase.h>
 
/**
  This class implements a hello-world kioslave
 */
class hello : public KIO::SlaveBase
{
  public:
    hello( const QByteArray &pool, const QByteArray &app );
    void get( const KUrl &url );
};
 
#endif

[edit] hello.cpp

#include "hello.h"
#include <kdebug.h>
#include <kcomponentdata.h>
 
 
extern "C" int KDE_EXPORT kdemain( int argc, char **argv )
{
  kDebug(7000) << "Entering function";
  KComponentData instance( "kio_hello" );
 
  if (argc != 4)
  {
    fprintf( stderr, "Usage: kio_hello protocol domain-socket1 domain-socket2\n");
    exit( -1 );
  }
  hello slave( argv[2], argv[3] );
  slave.dispatchLoop();
  return 0;
}
 
void hello::get( const KUrl &url )
{
  kDebug(7000) << "Entering function";
  mimeType( "text/plain" );
  QByteArray str( "Hello_world" );
  data( str );
  finished();
  kDebug(7000) << "Leaving function";
}
 
hello::hello( const QByteArray &pool, const QByteArray &app )
: SlaveBase( "hello", pool, app ) {}

[edit] hello.protocol

[Protocol]
DocPath=kioslave/kio_hello.html
exec=kio_hello
input=none
output=filesystem
protocol=hello
reading=true

[edit] Compile the stuff

Create a new Folder "build":

mkdir build
cd build

Run cmake and make

cmake ..
make

now you can install it (maybe you should use an experimental setup?)

make install

If you want to do this by hand:

g++ -shared -lkdeui -lkio -lkdecore -fPIC -I$(qmake -query QT_INSTALL_HEADERS) hello.cpp -o kio_hello.so

[edit] Install the stuff

Now you can install it (maybe you should use an experimental setup?):

make install

or if you want to install it to your system:

sudo make install

Of course you can also do this by hand.

Find out where your protocols are lying:

kde4-config --path services
  /usr/share/kde4/services/
 
kde4-config --path module
  /usr/lib64/kde4/
 
cp kio_hello.so /usr/local/lib/kde4/
cp kio_hello.so /usr/lib64/kde4/
cp kio_hello.protocol /usr/share/kde4/services/

[edit] Test it

[edit] In Konqueror

Start kinfocenter, choose hello as protocol. If this is possible, start konqueror, type hello:/// into the URL bar.

[edit] On the command line

kioclient 'cat' 'hello:///'

This page was last modified on 20 November 2012, at 13:34. This page has been accessed 11,375 times. Content is available under Creative Commons License SA 3.0 as well as the GNU Free Documentation License 1.2.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V.Legal