From 47936f60c34be8db9a199ccd261e0338ce0f1be7 Mon Sep 17 00:00:00 2001 From: Thomas McGuire Date: Tue, 14 Aug 2007 18:27:39 +0000 Subject: [PATCH] =?UTF-8?q?Port=20to=20KProcess.=20Additionally,=20get=20r?= =?UTF-8?q?id=20of=20the=20=C3=BCber-verbose=20debug=20output=20in=20case?= =?UTF-8?q?=20the=20pipe=20command=20fails.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit svn path=/trunk/KDE/kdepim/; revision=700077 --- templateparser.cpp | 152 ++++++++++++--------------------------------- templateparser.h | 10 --- 2 files changed, 38 insertions(+), 124 deletions(-) diff --git a/templateparser.cpp b/templateparser.cpp index d32ab7457..6faa5b47b 100644 --- a/templateparser.cpp +++ b/templateparser.cpp @@ -24,7 +24,7 @@ #include #include #include -#include +#include #include #include #include @@ -276,7 +276,7 @@ void TemplateParser::processWithTemplate( const QString &tmpl ) } } else if ( cmd.startsWith( "QUOTEPIPE=" ) ) { - // pipe message body throw command and insert it as quotation + // pipe message body through command and insert it as quotation kDebug(5006) <<"Command: QUOTEPIPE="; QString q; int len = parseQuotes( "QUOTEPIPE=", cmd, q ); @@ -317,7 +317,7 @@ void TemplateParser::processWithTemplate( const QString &tmpl ) } } else if ( cmd.startsWith( "TEXTPIPE=" ) ) { - // pipe message body throw command and insert it as is + // pipe message body through command and insert it as is kDebug(5006) <<"Command: TEXTPIPE="; QString q; int len = parseQuotes( "TEXTPIPE=", cmd, q ); @@ -329,7 +329,7 @@ void TemplateParser::processWithTemplate( const QString &tmpl ) } } else if ( cmd.startsWith( "MSGPIPE=" ) ) { - // pipe full message throw command and insert result as is + // pipe full message through command and insert result as is kDebug(5006) <<"Command: MSGPIPE="; QString q; int len = parseQuotes( "MSGPIPE=", cmd, q ); @@ -341,7 +341,7 @@ void TemplateParser::processWithTemplate( const QString &tmpl ) } } else if ( cmd.startsWith( "BODYPIPE=" ) ) { - // pipe message body generated so far throw command and insert result as is + // pipe message body generated so far through command and insert result as is kDebug(5006) <<"Command: BODYPIPE="; QString q; int len = parseQuotes( "BODYPIPE=", cmd, q ); @@ -351,7 +351,7 @@ void TemplateParser::processWithTemplate( const QString &tmpl ) body.append( str ); } else if ( cmd.startsWith( "CLEARPIPE=" ) ) { - // pipe message body generated so far throw command and + // pipe message body generated so far through command and // insert result as is replacing current body kDebug(5006) <<"Command: CLEARPIPE="; QString q; @@ -978,121 +978,45 @@ QString TemplateParser::findTemplate() QString TemplateParser::pipe( const QString &cmd, const QString &buf ) { - mPipeOut = ""; - mPipeErr = ""; - mPipeRc = 0; - - K3Process proc; - QByteArray data = buf.toLocal8Bit(); - - // kDebug(5006) <<"Command data:" << data; - - proc << KShell::splitArgs( cmd, KShell::TildeExpand ); - proc.setUseShell( true ); - connect( &proc, SIGNAL( receivedStdout( K3Process *, char *, int ) ), - this, SLOT( onReceivedStdout( K3Process *, char *, int ) ) ); - connect( &proc, SIGNAL( receivedStderr( K3Process *, char *, int ) ), - this, SLOT( onReceivedStderr( K3Process *, char *, int ) ) ); - connect( &proc, SIGNAL( wroteStdin( K3Process * ) ), - this, SLOT( onWroteStdin( K3Process * ) ) ); - - if ( proc.start( K3Process::NotifyOnExit, K3Process::All ) ) { - - bool pipe_filled = proc.writeStdin( data, data.length() ); - if ( pipe_filled ) { - proc.closeStdin(); - - bool exited = proc.wait( PipeTimeout ); - if ( exited ) { - - if ( proc.normalExit() ) { - - mPipeRc = proc.exitStatus(); - if ( mPipeRc != 0 && mDebug ) { - if ( mPipeErr.isEmpty() ) { - KMessageBox::error( 0, - i18n( "Pipe command exit with status %1: %2", - mPipeRc, cmd ) ); - } else { - KMessageBox::detailedError( 0, - i18n( "Pipe command exit with status %1: %2", - mPipeRc, cmd ), mPipeErr ); - } - } - - } else { - - mPipeRc = -( proc.exitSignal() ); - if ( mPipeRc != 0 && mDebug ) { - if ( mPipeErr.isEmpty() ) { - KMessageBox::error( 0, - i18n( "Pipe command killed by signal %1: %2", - -(mPipeRc), cmd ) ); - } else { - KMessageBox::detailedError( 0, - i18n( "Pipe command killed by signal %1: %2", - -(mPipeRc), cmd ), mPipeErr ); - } - } - } - - } else { - // process does not exited after TemplateParser::PipeTimeout seconds, kill it - proc.kill(); - proc.detach(); - if ( mDebug ) { - KMessageBox::error( 0, - i18n( "Pipe command did not finish within %1 seconds: %2", - PipeTimeout, cmd ) ); - } + KProcess process; + bool success, finished; + + process.setOutputChannelMode( KProcess::SeparateChannels ); + process.setShellCommand( cmd ); + process.start(); + if ( process.waitForStarted( PipeTimeout ) ) { + process.write( buf.toAscii() ); + if ( process.waitForBytesWritten( PipeTimeout ) ) { + process.closeWriteChannel(); + if ( process.waitForFinished( PipeTimeout ) ) { + success = ( process.exitStatus() == QProcess::NormalExit ); + finished = true; } - - } else { - // can`t write to stdin of process - proc.kill(); - proc.detach(); - if ( mDebug ) { - if ( mPipeErr.isEmpty() ) { - KMessageBox::error( 0, - i18n( "Cannot write to process stdin: %1", cmd ) ); - } else { - KMessageBox::detailedError( 0, - i18n( "Cannot write to process stdin: %1", - cmd ), mPipeErr ); - } + else { + finished = false; + success = false; } } + else { + success = false; + finished = false; + } - } else if ( mDebug ) { - KMessageBox::error( 0, - i18n( "Cannot start pipe command from template: %1", - cmd ) ); + // The process has started, but did not finish in time. Kill it. + if ( !finished ) + process.kill(); } + else + success = false; - return mPipeOut; -} - -void TemplateParser::onProcessExited( K3Process *proc ) -{ - Q_UNUSED( proc ); - // do nothing for now -} - -void TemplateParser::onReceivedStdout( K3Process *proc, char *buffer, int buflen ) -{ - Q_UNUSED( proc ); - mPipeOut += QString::fromLocal8Bit( buffer, buflen ); -} - -void TemplateParser::onReceivedStderr( K3Process *proc, char *buffer, int buflen ) -{ - Q_UNUSED( proc ); - mPipeErr += QString::fromLocal8Bit( buffer, buflen ); -} + if ( !success && mDebug ) + KMessageBox::error( 0, i18n( "Pipe command %1 failed.", + cmd ) ); -void TemplateParser::onWroteStdin( K3Process *proc ) -{ - proc->closeStdin(); + if ( success ) + return process.readAllStandardOutput(); + else + return QString(); } } // namespace KMail diff --git a/templateparser.h b/templateparser.h index 8283ffc7f..581934e1e 100644 --- a/templateparser.h +++ b/templateparser.h @@ -27,7 +27,6 @@ class KMMessage; class QString; class KMFolder; class QObject; -class K3Process; namespace KMail { @@ -70,21 +69,12 @@ class TemplateParser : public QObject bool mNoQuote; bool mAllowDecryption; bool mSelectionIsBody; - int mPipeRc; - QString mPipeOut; - QString mPipeErr; bool mDebug; QString mQuoteString; bool mAppend; int parseQuotes( const QString &prefix, const QString &str, QString "e ) const; - - protected slots: - void onProcessExited( K3Process *proc ); - void onReceivedStdout( K3Process *proc, char *buffer, int buflen ); - void onReceivedStderr( K3Process *proc, char *buffer, int buflen ); - void onWroteStdin( K3Process *proc ); }; } // namespace KMail