Development/Tutorials/Programming Tutorial KDE 4/KDirWatch

From KDE TechBase
Revision as of 20:30, 29 June 2011 by Neverendingo (talk | contribs) (Text replace - "<code cppqt>" to "<syntaxhighlight lang="cpp-qt">")
Warning
If this is going to be made into a proper tutorial, it should be on the main tutorial page. Not a part of this small tutorial series.


Using KDirWatch

If you want your program to be notified upon file changes, use KDirWatch. Here is an example.

khello.h

<syntaxhighlight lang="cpp-qt">

  1. ifndef _KHELLO_H_
  2. define _KHELLO_H_
  1. include <kmainwindow.h>
  2. include <kdirwatch.h>

/**

* @short Application Main Window
* @author Thorsten Staerk <[email protected]>
* @version 0.1
*/

class khello : public KMainWindow {

   Q_OBJECT

public:

   KDirWatch dw;
   /**
    * Default Constructor
    */
   khello(char* args);

public slots:

   void slotdirty();

};

  1. endif // _KHELLO_H_

This subclasses KMainWindow.

khello.cpp

<syntaxhighlight lang="cpp-qt">

  1. include "khello.h"
  1. include <qlabel.h>
  1. include <kdebug.h>
  2. include <kmainwindow.h>
  3. include <klocale.h>
  4. include <kmessagebox.h>

khello::khello(char* args)

   : KMainWindow()

{

   kdDebug() << "args = " << args << endl;
   dw.addFile(args);
   kdDebug() << "Watching file " << args << endl;
   connect(&dw, SIGNAL(dirty(const QString & ) ),
          SLOT( slotdirty() ) );

}

void khello::slotdirty() {

 kdDebug() << "File has changed" << endl;

}

  1. include "khello.moc"

When instanciated, the class creates a KDirWatcher object and tells it to watch the file passed in by the arguments (dw.addFile(args)). The DirWatcher will now emit a SIGNAL dirty each time this file gets modified. This signal is connected to khello's slot slotdirty(). So, every time the file gets modified, slotdirty is called.

main.cpp

<syntaxhighlight lang="cpp-qt"> /* This program is a demo how to react on file changes.

  • /
  1. include "khello.h"
  2. include <kapplication.h>
  3. include <kaboutdata.h>
  4. include <kcmdlineargs.h>
  5. include <kdebug.h>
  6. include <klocale.h>

static const char description[] =

   I18N_NOOP("An ");

static const char version[] = "0.1";

static KCmdLineOptions options[] = {

   { "+[URL]", I18N_NOOP( "Document to open" ), 0 },
   KCmdLineLastOption

};

int main(int argc, char **argv) {

   KAboutData about("khello", I18N_NOOP("khello"), version, 
     description, KAboutData::License_GPL, 
     "(C) %{YEAR} Thorsten Staerk", 0, 0, "[email protected]");
   about.addAuthor( "Thorsten Staerk", 0, "[email protected]" );
   KCmdLineArgs::init(argc, argv, &about);
   KCmdLineArgs::addCmdLineOptions( options );
   KApplication app;
   KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
   kdDebug() << argv[1] << endl;
   khello *mainWin = new khello(argv[1]);
   mainWin->show();
   return app.exec();

}

CMake

The best way to build the program is to use CMake.

CMakeLists.txt

project (dirwatch) find_package(KDE4 REQUIRED KIO KDIRWATCH) include_directories( ${KDE4_INCLUDES} ) set(dirwatch_SRCS

       main.cpp
       khello.cpp

) kde4_add_executable(dirwatch ${dirwatch_SRCS}) target_link_libraries( dirwatch ${KDE4_KDEUI_LIBS}

 ${KDE4_KIO_LIBS} )

Note that we added ${KDE4_KIO_LIBS} to the target_link_libraries. If we do not do it, the linker complains about undefined symbols. In the kio-libs, KDirWatch is defined.

Compile it

To compile, link and run it, use:

cmake . && make && ./dirwatch /var/log/messages

If /var/log/messages changes, the program will notify you about it.