Development/Tutorials/KIO Slaves/Using KIO Slaves in your Program: Difference between revisions

From KDE TechBase
(New page: In this chapter we write a simple browser to show how easy it is to use kioslaves in KDE 4. The browser will download the page www.kde.org and show the html sourcecode. It will not render ...)
 
(Mark for updating)
 
(11 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{Review|Port to KF5}}
In this chapter we write a simple browser to show how easy it is to use kioslaves in KDE 4. The browser will download the page www.kde.org and show the html sourcecode. It will not render html - remember this is a programming demo.
In this chapter we write a simple browser to show how easy it is to use kioslaves in KDE 4. The browser will download the page www.kde.org and show the html sourcecode. It will not render html - remember this is a programming demo.
The purpose of kioslaves is to have access to a given storage or data representation without the need for blocking calls. This is solved via QT's signal/slot mechanism: Once the data has arrived, e.g. from a web page, it triggers a signal. The signal is connected to the respective slot and can start processing the data.
The purpose of kioslaves is to have access to a given storage or data representation without the need for blocking calls. This is solved via QT's signal/slot mechanism: Once the data has arrived, e.g. from a web page, it triggers a signal. The signal is connected to the respective slot and can start processing the data.
Line 5: Line 7:


== main.cpp ==
== main.cpp ==
<pre>
<syntaxhighlight lang="cpp-qt">
#include <QString>
#include <QString>
#include <kapplication.h>
#include <kapplication.h>
Line 28: Line 30:
   khello.exec();
   khello.exec();
}
}
</pre>
</syntaxhighlight>
The purpose of main is to create a Browser instance and hold it open while the KDE event loop is running (i.e. the KDE program is ready).
The purpose of main is to create a Browser instance and hold it open while the KDE event loop is running (i.e. the KDE program is ready).


== browser.h ==  
== browser.h ==  
<pre>
<syntaxhighlight lang="cpp-qt">
#ifndef KDE4START_H__
#ifndef KDE4START_H__
#define KDE4START_H__
#define KDE4START_H__
Line 48: Line 50:
   public slots:
   public slots:
     void slotButtonClicked();
     void slotButtonClicked();
     void dataishere(KIO::Job *,const QByteArray &);
     void dataIsHere(KIO::Job *,const QByteArray &);
};
};
#endif
#endif
</pre>
</syntaxhighlight>


== browser.cpp ==
== browser.cpp ==
<pre>
<syntaxhighlight lang="cpp-qt">
#include <kio/scheduler.h>
#include <kio/scheduler.h>
#include <kurl.h>
#include <kurl.h>
Line 72: Line 74:
   kDebug() << "entering function";
   kDebug() << "entering function";
   // creating a kioslave
   // creating a kioslave
   KIO::TransferJob *job = KIO::get(KUrl("http://www.kde.org"));
   KIO::TransferJob *job = KIO::get(KUrl("http://emicalculators.in/"));
   connect (job, SIGNAL(  data(KIO::Job *, const QByteArray & )), this, SLOT(dataishere(KIO::Job *,const QByteArray &)));
   connect (job, SIGNAL(  data(KIO::Job *, const QByteArray & )), this, SLOT(dataIsHere(KIO::Job *,const QByteArray &)));
}
}


void Browser::dataishere(KIO::Job *,const QByteArray & data )
void Browser::dataIsHere(KIO::Job *,const QByteArray & data )
{
{
   kDebug() << "data is here";
   kDebug() << "data is here";
   kDebug() << data;
   kDebug() << data;
}
}
</pre>
</syntaxhighlight>
As discussed, the Browser class does not do a busy wait for the data to arrive from the web. Instead, it uses the http kioslave's signal data by connecting it to the slot dataishere. Dataishere gets the http transmission via its parameter data and just outputs it. That's it - no blocking, no busy wait!
As discussed, the Browser class does not do a busy wait for the data to arrive from the [http://emicalculators.in/ web]. Instead, it uses the http kioslave's signal data by connecting it to the slot dataishere. <tt>dataIsHere</tt> gets the http transmission via its parameter data and just outputs it. That's it - no blocking, no busy wait!


== CMakeLists.txt ==
== CMakeLists.txt ==
<pre>
<syntaxhighlight lang="cmake">
PROJECT( browser )
PROJECT( browser )
FIND_PACKAGE(KDE4 REQUIRED)
FIND_PACKAGE(KDE4 REQUIRED)
Line 99: Line 101:


install(TARGETS browser  DESTINATION ${BIN_INSTALL_DIR} )
install(TARGETS browser  DESTINATION ${BIN_INSTALL_DIR} )
</pre>
</syntaxhighlight>


= Kompile, link and run it =
= Compile, link and run it =
cmake . && make && ./browser
<syntaxhighlight lang="bash">
cmake . && make && ./browser
</syntaxhighlight>

Latest revision as of 12:42, 31 May 2019

Warning
This page needs a review and probably holds information that needs to be fixed.

Parts to be reviewed:

Port to KF5

In this chapter we write a simple browser to show how easy it is to use kioslaves in KDE 4. The browser will download the page www.kde.org and show the html sourcecode. It will not render html - remember this is a programming demo. The purpose of kioslaves is to have access to a given storage or data representation without the need for blocking calls. This is solved via QT's signal/slot mechanism: Once the data has arrived, e.g. from a web page, it triggers a signal. The signal is connected to the respective slot and can start processing the data.

The files

main.cpp

#include <QString>
#include <kapplication.h>
#include <kaboutdata.h>
#include <klocalizedstring.h>
#include <kmessagebox.h>
#include <kcmdlineargs.h>
#include <KMainWindow>
#include <browser.h>

int main (int argc, char *argv[])
{
  // KAboutData (const QByteArray &appName, const QByteArray &catalogName, const KLocalizedString &programName, const QByteArray &version,
  const QByteArray& ba=QByteArray("test");
  const KLocalizedString name=ki18n("myName");
  KAboutData aboutData( ba, ba, name, ba, name);
  KCmdLineArgs::init( argc, argv, &aboutData );
  KApplication khello;

  Browser *mw = new Browser();
  mw->show();
  khello.exec();
}

The purpose of main is to create a Browser instance and hold it open while the KDE event loop is running (i.e. the KDE program is ready).

browser.h

#ifndef KDE4START_H__
#define KDE4START_H__

#include <kmainwindow.h>
#include <kio/scheduler.h>
#include <kurl.h>
#include <kio/jobclasses.h>

class Browser : public QWidget
{
  Q_OBJECT
  public:
    Browser();
  public slots:
    void slotButtonClicked();
    void dataIsHere(KIO::Job *,const QByteArray &);
};
#endif

browser.cpp

#include <kio/scheduler.h>
#include <kurl.h>
#include <kio/jobclasses.h>
#include <kdebug.h>
#include <browser.h>

class TransferJob;

Browser::Browser() : QWidget(NULL)
{
  slotButtonClicked();
}

void Browser::slotButtonClicked()
{
  kDebug() << "entering function";
  // creating a kioslave
  KIO::TransferJob *job = KIO::get(KUrl("http://emicalculators.in/"));
  connect (job, SIGNAL(  data(KIO::Job *, const QByteArray & )), this, SLOT(dataIsHere(KIO::Job *,const QByteArray &)));
}

void Browser::dataIsHere(KIO::Job *,const QByteArray & data )
{
  kDebug() << "data is here";
  kDebug() << data;
}

As discussed, the Browser class does not do a busy wait for the data to arrive from the web. Instead, it uses the http kioslave's signal data by connecting it to the slot dataishere. dataIsHere gets the http transmission via its parameter data and just outputs it. That's it - no blocking, no busy wait!

CMakeLists.txt

PROJECT( browser )
FIND_PACKAGE(KDE4 REQUIRED)
INCLUDE_DIRECTORIES( ${KDE4_INCLUDES} . )


SET(kde4startSources main.cpp browser.cpp )


KDE4_ADD_EXECUTABLE(browser ${kde4startSources} )

TARGET_LINK_LIBRARIES(browser ${KDE4_KDEUI_LIBS} ${KDE4_KPARTS_LIBS} )

install(TARGETS browser  DESTINATION ${BIN_INSTALL_DIR} )

Compile, link and run it

cmake . && make && ./browser