Kio-imap4 programming

From ThorstensHome
Revision as of 17:19, 11 August 2008 by WikiSysop (Talk)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

This page is to develop your know-how about KDE kio imap4 programming.

Contents

Work with kioslaves

CMakeLists.txt

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


SET(kde4startSources main.cpp Kde4Start.cpp )

kde4_add_ui_files( kde4startSources Kde4StartUi.ui)

KDE4_ADD_EXECUTABLE(kde4start ${kde4startSources} )

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

install(TARGETS kde4start  DESTINATION ${BIN_INSTALL_DIR} )

main.cpp

#include <QString>
#include <kapplication.h>
#include <kaboutdata.h>
#include <klocalizedstring.h>
#include <kmessagebox.h>
#include <kcmdlineargs.h>
#include <KMainWindow>
#include "Kde4Start.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;

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

Kde4Start.h

#ifndef KDE4START_H__
#define KDE4START_H__

#include <kmainwindow.h>
#include <kio/scheduler.h>
#include <kurl.h>
#include <kio/jobclasses.h>
class Ui_Kde4StartUi;
class Kde4Start : public QWidget
{
  Q_OBJECT
  public:
    Kde4Start();
  public slots:
    void slotButtonClicked();
    void dataishere(KIO::Job *,const QByteArray &);
  private:
    Ui_Kde4StartUi *mKde4StartUi;
};
#endif

Kde4Start.cpp

#include "Kde4Start.h"
#include "Kde4Start.moc"
#include "ui_Kde4StartUi.h"
#include <kio/scheduler.h>
#include <kurl.h>
#include <kio/jobclasses.h>
#include <kdebug.h>

class TransferJob;

Kde4Start::Kde4Start() : QWidget(NULL)
{
  mKde4StartUi = new Ui_Kde4StartUi();
  mKde4StartUi->setupUi(this);
  connect(mKde4StartUi->pushButton, SIGNAL(clicked()),SLOT(slotButtonClicked()));
}

void Kde4Start::slotButtonClicked()
{
  mKde4StartUi->label->setText("Hello");
  // creating a kioslave
  KIO::TransferJob *job = KIO::get(KUrl("http://www.kde.org"));
  connect (job, SIGNAL(  data(KIO::Job *, const QByteArray & )), this, SLOT(dataishere(KIO::Job *,const QByteArray &)));
}

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

Compile, link and run

cmake . && make && ./kde4start

kio_imap4

test.cpp

#include <kio/scheduler.h>
#include <kio/slave.h>
#include <kdebug.h>
#include <kcomponentdata.h>
int main()
{
  KComponentData instance( "kcmkmail_config_misc" );
  const KUrl url("imap://dummy:password@localhost/inbox");
  KIO::Slave* a=KIO::Scheduler::getConnectedSlave(url,KIO::MetaData());
  if (a!=0) kDebug() << "got a slave" << endl;
  kDebug() << "slave pid is " << a->slave_pid() << endl;
}

Slave::send is implemented in kdelibs/kio/kio/slave.h. It calls kdelibs/kio/kio/connection.h's send.

Compile, link and run it

g++ -o kimap -I/home/kde-devel/kde/include -L/home/kde-devel/kde/lib -lkio test.cpp && ./kimap