home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / k3bprocess.h < prev    next >
Encoding:
C/C++ Source or Header  |  2008-05-27  |  4.6 KB  |  205 lines

  1. /* 
  2.  *
  3.  * $Id: k3bprocess.h 621644 2007-01-09 12:53:09Z trueg $
  4.  * Copyright (C) 2003 Sebastian Trueg <trueg@k3b.org>
  5.  *
  6.  * This file is part of the K3b project.
  7.  * Copyright (C) 1998-2007 Sebastian Trueg <trueg@k3b.org>
  8.  *
  9.  * This program is free software; you can redistribute it and/or modify
  10.  * it under the terms of the GNU General Public License as published by
  11.  * the Free Software Foundation; either version 2 of the License, or
  12.  * (at your option) any later version.
  13.  * See the file "COPYING" for the exact licensing terms.
  14.  */
  15.  
  16.  
  17. #ifndef K3B_PROCESS_H
  18. #define K3B_PROCESS_H
  19.  
  20.  
  21. #include <kprocess.h>
  22. #include <qstring.h>
  23. #include "k3b_export.h"
  24.  
  25. class K3bExternalBin;
  26.  
  27.  
  28. /**
  29.  * This is an enhanced KProcess.
  30.  * It splits the stderr output to lines making sure the client gets every line as it 
  31.  * was written by the process.
  32.  * Aditionally one may set raw stdout and stdin handling using the stdin() and stdout() methods
  33.  * to get the process' file descriptors.
  34.  * Last but not least K3bProcess is able to duplicate stdout making it possible to connect two 
  35.  * K3bProcesses like used in K3bDataJob to duplicate mkisofs' stdout to the stdin of the writer 
  36.  * (cdrecord or cdrdao)
  37.  */
  38. class LIBK3B_EXPORT K3bProcess : public KProcess
  39. {
  40.   Q_OBJECT
  41.     
  42.  public:
  43.   class OutputCollector;
  44.  
  45.  public:
  46.   K3bProcess();
  47.   ~K3bProcess();
  48.  
  49.   /**
  50.    * In the future this might also set the nice value
  51.    */
  52.   K3bProcess& operator<<( const K3bExternalBin* );
  53.  
  54.   K3bProcess& operator<<( const QString& arg );
  55.   K3bProcess& operator<<( const char* arg );
  56.   K3bProcess& operator<<( const QCString& arg );
  57.   K3bProcess& operator<<( const QStringList& args );
  58.  
  59.   bool start( RunMode run = NotifyOnExit, Communication com = NoCommunication );
  60.  
  61.   /** 
  62.    * get stdin file descriptor
  63.    * Only makes sense while process is running.
  64.    *
  65.    * Only use with setRawStdin
  66.    */
  67.   int stdinFd() const;
  68.  
  69.   /** 
  70.    * get stdout file descriptor
  71.    * Only makes sense while process is running.
  72.    *
  73.    * Only use with setRawStdout
  74.    */
  75.   int stdoutFd() const;
  76.  
  77.   /**
  78.    * @deprecated use writeToFd
  79.    */
  80.   void dupStdout( int fd );
  81.  
  82.   /**
  83.    * @deprecated use readFromFd
  84.    */
  85.   void dupStdin( int fd );
  86.  
  87.   /**
  88.    * Make the process write to @fd instead of Stdout.
  89.    * This means you won't get any stdoutReady() or receivedStdout()
  90.    * signals anymore.
  91.    *
  92.    * Only use this before starting the process.
  93.    */
  94.   void writeToFd( int fd );
  95.  
  96.   /**
  97.    * Make the process read from @fd instead of Stdin.
  98.    * This means you won't get any wroteStdin()
  99.    * signals anymore.
  100.    *
  101.    * Only use this before starting the process.
  102.    */
  103.   void readFromFd( int fd );
  104.  
  105.   /** 
  106.    * If set true the process' stdin fd will be available
  107.    * through @stdinFd.
  108.    * Be aware that you will not get any wroteStdin signals
  109.    * anymore.
  110.    *
  111.    * Only use this before starting the process.
  112.    */
  113.   void setRawStdin(bool b);
  114.  
  115.   /** 
  116.    * If set true the process' stdout fd will be available
  117.    * through @stdoutFd.
  118.    * Be aware that you will not get any stdoutReady or receivedStdout
  119.    * signals anymore.
  120.    *
  121.    * Only use this before starting the process.
  122.    */
  123.   void setRawStdout(bool b);
  124.  
  125.  public slots:
  126.   void setSplitStdout( bool b ) { m_bSplitStdout = b; }
  127.  
  128.   /**
  129.    * default is true
  130.    */
  131.   void setSuppressEmptyLines( bool b );
  132.  
  133.   bool closeStdin();
  134.   bool closeStdout();
  135.  
  136.  private slots:
  137.   void slotSplitStderr( KProcess*, char*, int );
  138.   void slotSplitStdout( KProcess*, char*, int );
  139.  
  140.  signals:
  141.   void stderrLine( const QString& line );
  142.   void stdoutLine( const QString& line );
  143.  
  144.   /** 
  145.    * Gets emitted if raw stdout mode has been requested
  146.    * The data has to be read from @p fd.
  147.    */
  148.   void stdoutReady( int fd );
  149.  
  150.  protected:
  151.   /**
  152.    * reimplemeted from KProcess
  153.    */
  154.   int commSetupDoneP();
  155.  
  156.   /**
  157.    * reimplemeted from KProcess
  158.    */
  159.   int commSetupDoneC();
  160.  
  161.   /**
  162.    * reimplemeted from KProcess
  163.    */
  164.   int setupCommunication( Communication comm );
  165.  
  166.   /**
  167.    * reimplemeted from KProcess
  168.    */
  169.   void commClose();
  170.  
  171.  private:
  172.   static QStringList splitOutput( char*, int, QString&, bool );
  173.  
  174.   class Data;
  175.   Data* d;
  176.  
  177.   bool m_bSplitStdout;
  178. };
  179.  
  180. class LIBK3B_EXPORT K3bProcessOutputCollector: public QObject
  181. {
  182.   Q_OBJECT
  183.     
  184.  public:
  185.   K3bProcessOutputCollector( KProcess* );
  186.   void setProcess( KProcess* );
  187.   
  188.   const QString& output() const { return m_gatheredOutput; }
  189.   const QString& stderrOutput() const { return m_stderrOutput; }
  190.   const QString& stdoutOutput() const { return m_stdoutOutput; }
  191.   
  192.  private slots:
  193.   void slotGatherStderr( KProcess*, char*, int );
  194.   void slotGatherStdout( KProcess*, char*, int );
  195.   
  196.  private:
  197.   QString m_gatheredOutput;
  198.   QString m_stderrOutput;
  199.   QString m_stdoutOutput;
  200.   KProcess* m_process;
  201. };
  202.  
  203.  
  204. #endif
  205.