[rkward-cvs] SF.net SVN: rkward: [2214] branches/KDE4_port/rkward

tfry at users.sourceforge.net tfry at users.sourceforge.net
Tue Nov 13 01:32:06 UTC 2007


Revision: 2214
          http://rkward.svn.sourceforge.net/rkward/?rev=2214&view=rev
Author:   tfry
Date:     2007-11-12 17:32:05 -0800 (Mon, 12 Nov 2007)

Log Message:
-----------
Qt3 / KDE3 support code removals in PHPBackend

Modified Paths:
--------------
    branches/KDE4_port/rkward/plugin/rkstandardcomponent.cpp
    branches/KDE4_port/rkward/scriptbackends/phpbackend.cpp
    branches/KDE4_port/rkward/scriptbackends/phpbackend.h
    branches/KDE4_port/rkward/scriptbackends/scriptbackend.cpp
    branches/KDE4_port/rkward/scriptbackends/scriptbackend.h

Modified: branches/KDE4_port/rkward/plugin/rkstandardcomponent.cpp
===================================================================
--- branches/KDE4_port/rkward/plugin/rkstandardcomponent.cpp	2007-11-13 00:06:16 UTC (rev 2213)
+++ branches/KDE4_port/rkward/plugin/rkstandardcomponent.cpp	2007-11-13 01:32:05 UTC (rev 2214)
@@ -156,7 +156,7 @@
 	RK_TRACE (PLUGIN);
 
 	if (gui) gui->deleteLater ();
-	delete backend;
+	backend->destroy ();	// it will self-destruct, when it has closed the process.
 }
 
 void RKStandardComponent::hide () {

Modified: branches/KDE4_port/rkward/scriptbackends/phpbackend.cpp
===================================================================
--- branches/KDE4_port/rkward/scriptbackends/phpbackend.cpp	2007-11-13 00:06:16 UTC (rev 2213)
+++ branches/KDE4_port/rkward/scriptbackends/phpbackend.cpp	2007-11-13 01:32:05 UTC (rev 2214)
@@ -21,6 +21,7 @@
 #include <qfile.h>
 #include <qfileinfo.h>
 #include <qdir.h>
+#include <QTimer>
 
 #include <kmessagebox.h>
 #include <klocale.h>
@@ -34,54 +35,47 @@
 	RK_TRACE (PHP);
 
 	php_process = 0;
+	dead = false;
 	eot_string="#RKEND#\n";
 	eoq_string="#RKQEND#\n";
-	busy_writing = false;
 
 	PHPBackend::filename = filename;
 }
 
 
-PHPBackend::~PHPBackend() {
+PHPBackend::~PHPBackend () {
 	RK_TRACE (PHP);
-	destroy ();
+
+	if (php_process) {
+		RK_DO (qDebug ("PHP process has not exited after being killed for ten seconds. We destroy it now, hoping for the best"), PHP, DL_ERROR);
+	}
 }
 
 bool PHPBackend::initialize (RKComponentPropertyCode *code_property, bool add_headings) {
 	RK_TRACE (PHP);
 
-	if (php_process && php_process->isRunning ()) {
+	if (php_process) {
 		RK_DO (qDebug ("another template is already openend in this backend"), PHP, DL_ERROR);
 		return false;
 	}
 
 	QDir files_path (RKCommonFunctions::getRKWardDataDir () + "phpfiles/");
 	QString common_php = files_path.absoluteFilePath ("common.php");
-	QString php_ini = files_path.absoluteFilePath ("php.ini");
+	QString php_ini = files_path.absoluteFilePath ("php.ini");	// make sure to set good options
 	if (!QFileInfo (common_php).isReadable ()) {
 		KMessageBox::error (0, i18n ("The support file \"%1\" could not be found or is not readable. Please check your installation.", common_php), i18n ("PHP-Error"));
 		emit (haveError ());
 		return false;
 	}
 
-	php_process = new K3Process ();
-	*php_process << RKSettingsModulePHP::phpBin();
-	*php_process << "-c" << php_ini;	// set correct options
-	*php_process << common_php;
-	
-	// we have to be connected at all times! Otherwise the connection will be gone for good.
-	//connect (php_process, SIGNAL (receivedStderr (K3Process *, char*, int)), this, SLOT (gotError (K3Process *, char*, int)));
-	connect (php_process, SIGNAL (wroteStdin (K3Process *)), this, SLOT (doneWriting (K3Process* )));
-	connect (php_process, SIGNAL (receivedStdout (K3Process *, char*, int)), this, SLOT (gotOutput (K3Process *, char*, int)));
-	connect (php_process, SIGNAL (processExited (K3Process *)), this, SLOT (processDied (K3Process*)));
-	
-	if (!php_process->start (K3Process::NotifyOnExit, K3Process::All)) {
-		KMessageBox::error (0, i18n ("The PHP backend could not be started. Check whether you have correctly configured the location of the PHP-binary (Settings->Configure Settings->PHP backend)"), i18n ("PHP-Error"));
-		emit (haveError ());
-		return false;
-	}
+	php_process = new QProcess (this);
+	connect (php_process, SIGNAL (readyRead()), this, SLOT (gotOutput()));
+	connect (php_process, SIGNAL (error(QProcess::ProcessError)), this, SLOT (processError(QProcess::ProcessError)));
+	connect (php_process, SIGNAL (finished(int,QProcess::ExitStatus)), this, SLOT (processDead(int,QProcess::ExitStatus)));
 
-	busy_writing = doing_command = startup_done = false;
+	php_process->start (RKSettingsModulePHP::phpBin() + " -c " + php_ini + " " + common_php);
+
+	doing_command = startup_done = false;
 	busy = true;
 
 	// start the real template
@@ -94,40 +88,33 @@
 
 void PHPBackend::destroy () {
 	RK_TRACE (PHP);
-
-	if (php_process) {
-		php_process->detach ();
-		php_process->deleteLater ();
-		php_process = 0;
+	if (!dead) {
+		dead = true;
+		php_process->kill ();
+		QTimer::singleShot (10000, this, SLOT (deleteLater()));	// don't wait for ever for the process to die, even if it's somewhat dangerous
 	}
-	
-	busy_writing = false;
+
 	busy = false;
 	
 	while (command_stack.count ()) {
-		delete command_stack.first ();
-		command_stack.pop_front ();
+		delete command_stack.takeFirst ();
 	}
-	
-	data_stack.clear ();
 }
 
 void PHPBackend::tryNextFunction () {
 	RK_TRACE (PHP);
 
-	if ((!busy_writing) && php_process && php_process->isRunning () && (!busy) && (!command_stack.isEmpty ())) {
+	if (php_process && (php_process->state () == QProcess::Running) && (!busy) && (!command_stack.isEmpty ())) {
 	/// clean up previous command if applicable
 		if (command_stack.first ()->complete) {
-			delete command_stack.first ();
-			command_stack.pop_front ();
+			delete command_stack.takeFirst ();
 			
 			if (!command_stack.count ()) return;
 		}
 		
 		RK_DO (qDebug ("submitting PHP code: %s", command_stack.first ()->command.toLatin1 ().data ()), PHP, DL_DEBUG);
-		current_command = command_stack.first ()->command + eot_string;
-		php_process->writeStdin (current_command.toLatin1 (), current_command.length ());
-		busy_writing = doing_command = busy = true;
+		php_process->write (QString (command_stack.first ()->command + eot_string).toLatin1 ());
+		doing_command = busy = true;
 		command_stack.first ()->complete = true;
 		current_flags = command_stack.first ()->flags;
 		current_type = command_stack.first ()->type;
@@ -136,36 +123,22 @@
 
 void PHPBackend::writeData (const QString &data) {
 	RK_TRACE (PHP);
-	data_stack.append (data + eot_string);
-	tryWriteData ();
-}
 
-void PHPBackend::tryWriteData () {
-	RK_TRACE (PHP);
-
-	if ((!busy_writing) && php_process && php_process->isRunning () && busy && (!data_stack.isEmpty ())) {
-		RK_DO (qDebug ("submitting data: %s", data_stack.first ().toLatin1 ().data ()), PHP, DL_DEBUG);
-		php_process->writeStdin (data_stack.first ().toLatin1 (), data_stack.first ().length ());
-		busy_writing = true;
-		doing_command = false;
-	}
-}
-
-void PHPBackend::doneWriting (K3Process *) {
-	RK_TRACE (PHP);
-
-	busy_writing = false;
-	if (!doing_command) data_stack.pop_front ();
-	tryWriteData ();
+	RK_DO (qDebug ("submitting data: %s", data.toLatin1 ().data ()), PHP, DL_DEBUG);
+	php_process->write (QString (data + eot_string).toLatin1 ());
 	tryNextFunction ();
 }
 
-void PHPBackend::gotOutput (K3Process *, char* buf, int) {
+void PHPBackend::gotOutput () {
 	RK_TRACE (PHP);
 
-	RK_DO (qDebug ("PHP transmission:\n%s", buf), PHP, DL_DEBUG);
+	qint64 len = php_process->bytesAvailable ();
+	if (!len) return;	// false alarm
 
-	output_raw_buffer += buf;
+	QByteArray buf = php_process->readAll ();
+	RK_DO (qDebug ("PHP transmission:\n%s", buf.data ()), PHP, DL_DEBUG);
+
+	output_raw_buffer.append (QString::fromLatin1 (buf));
 	QString request;
 	QString data;
 	int i, j;
@@ -193,7 +166,7 @@
 	// pending data is always first in a stream, so process it first, too
 	if (have_data) {
 		if (!startup_done) {
-			php_process->detach ();
+			php_process->close ();
 			KMessageBox::error (0, i18n ("There has been an error\n(\"%1\")\nwhile starting up the PHP backend. Most likely this is due to either a bug in RKWard or a problem with your PHP installation. Check the settings (Settings->Configure Settings->PHP backend) and try again.", output_raw_buffer.trimmed ()), i18n ("PHP-Error"));
 			emit (haveError ());
 			destroy ();
@@ -223,7 +196,7 @@
 //			writeData (res + eot_string);
 		} else if (request.startsWith ("PHP-Error")) {
 			QString error = request.remove ("PHP-Error");
-			php_process->detach ();
+			php_process->close ();
 			KMessageBox::error (0, i18n ("The PHP-backend has reported an error\n(\"%1\")\nand has been shut down. This is most likely due to a bug in the plugin. But of course you may want to try to close and restart the plugin to see whether it works with different settings.", error.trimmed ()), i18n ("PHP-Error"));
 			emit (haveError ());
 			destroy ();
@@ -234,16 +207,30 @@
 	}
 }
 
-void PHPBackend::processDied (K3Process *) {
+void PHPBackend::processError (QProcess::ProcessError error) {
 	RK_TRACE (PHP);
 
-	if (php_process) {		// if the php_process is already 0, this means, we have caught an error message before the process died, have already shown a message, emitted haveError(), and called destroy()
-		php_process->detach ();
+	if (dead) return;	// we are already dead, so we've shown an error before.
+
+	php_process->close ();
+
+	if (error == QProcess::FailedToStart) {
+		KMessageBox::error (0, i18n ("The PHP backend could not be started. Check whether you have correctly configured the location of the PHP-binary (Settings->Configure Settings->PHP backend)"), i18n ("PHP-Error"));
+	} else {
 		KMessageBox::error (0, i18n ("The PHP-backend has died unexpectedly. The current output buffer is shown below:\n%1", output_raw_buffer), i18n ("PHP Process exited"));
-		emit (haveError ());
-		destroy ();
 	}
+
+	emit (haveError ());
+	destroy ();
 }
 
+void PHPBackend::processDead (int, QProcess::ExitStatus) {
+	RK_TRACE (PHP);
 
+	if (dead) {
+		php_process = 0;
+		deleteLater ();
+	} else destroy ();
+}
+
 #include "phpbackend.moc"

Modified: branches/KDE4_port/rkward/scriptbackends/phpbackend.h
===================================================================
--- branches/KDE4_port/rkward/scriptbackends/phpbackend.h	2007-11-13 00:06:16 UTC (rev 2213)
+++ branches/KDE4_port/rkward/scriptbackends/phpbackend.h	2007-11-13 01:32:05 UTC (rev 2214)
@@ -21,9 +21,8 @@
 
 #include <qstring.h>
 #include <qobject.h>
-#include <k3process.h>
+#include <QProcess>
 #include <qstringlist.h>
-#include <q3valuelist.h>
 
 class RKPlugin;
 
@@ -48,25 +47,20 @@
 	void preview (int flags) { callFunction ("getPreview ();", flags, Preview); };
 	void writeData (const QString &data);
 public slots:
-	void gotOutput (K3Process *proc, char* buf, int len);
-	//void gotError (K3Process *proc, char* buf, int len);
-	void processDied (K3Process *proc);
-	void doneWriting (K3Process *proc);
+	void gotOutput ();
+	void processError (QProcess::ProcessError error);
+	void processDead (int exitCode, QProcess::ExitStatus exitStatus);
 private:
 	void tryNextFunction ();
-	void tryWriteData ();
-	K3Process *php_process;
+	QProcess *php_process;
 /// The string singalling the end of transmission to/from PHP. TODO: make static
 	QString eot_string;
 /// The string singalling the end of request from PHP. TODO: make static
 	QString eoq_string;
-/** actually only one piece of data gets requested at a time. However, sometimes it takes a while until we realize (via doneWriting ()) that the last piece was transmitted ok (we have to keep a write buffer until then). So it may happen, that a new piece of information was requested, before we have completed the process for the previous one. Hence we use a FIFO stack just like for the commands (though it's handled slightly differently). */
-	QStringList data_stack;
-/// write buffer for the current command
-	QString current_command;
-	bool busy_writing;
+
 	bool doing_command;
 	bool startup_done;
+	bool dead;
 	QString output_raw_buffer;
 
 	QString _output;

Modified: branches/KDE4_port/rkward/scriptbackends/scriptbackend.cpp
===================================================================
--- branches/KDE4_port/rkward/scriptbackends/scriptbackend.cpp	2007-11-13 00:06:16 UTC (rev 2213)
+++ branches/KDE4_port/rkward/scriptbackends/scriptbackend.cpp	2007-11-13 01:32:05 UTC (rev 2214)
@@ -21,8 +21,6 @@
 #include "../plugin/rkcomponentproperties.h"
 
 #include "../debug.h"
-//Added by qt3to4:
-#include <Q3ValueList>
 
 ScriptBackend::ScriptBackend () : QObject() {
 	busy = false;
@@ -65,7 +63,7 @@
 		current_type = Ignore;
 	}
 
-	Q3ValueList<ScriptCommand *>::iterator it = command_stack.begin ();
+	QLinkedList<ScriptCommand *>::iterator it = command_stack.begin ();
 	while (it != command_stack.end ()) {
 		if ((*it)->type == type) {
 			delete (*it);

Modified: branches/KDE4_port/rkward/scriptbackends/scriptbackend.h
===================================================================
--- branches/KDE4_port/rkward/scriptbackends/scriptbackend.h	2007-11-13 00:06:16 UTC (rev 2213)
+++ branches/KDE4_port/rkward/scriptbackends/scriptbackend.h	2007-11-13 01:32:05 UTC (rev 2214)
@@ -20,8 +20,7 @@
 #include <qobject.h>
 
 #include <qstring.h>
-//Added by qt3to4:
-#include <Q3ValueList>
+#include <QLinkedList>
 
 class RKComponentPropertyCode;
 
@@ -81,7 +80,7 @@
 	/// whether command has finished
 		bool complete;
 	};
-	Q3ValueList<ScriptCommand *> command_stack;
+	QLinkedList<ScriptCommand *> command_stack;
 
 	int current_flags;
 	int current_type;


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the rkward-tracker mailing list