summaryrefslogtreecommitdiffstats
path: root/kjsembed/ksimpleprocess.cpp
blob: 0a7ee1cc1106187c01a755db7974332e87b152fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// -*- c++ -*-

/*
 *  Copyright (C) 2004, Ian Reinhart Geiser <[email protected]>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this library; see the file COPYING.LIB.  If not, write to
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 */
#include "ksimpleprocess.h"
#include <tqapplication.h>
#include <klocale.h>
static TQStringList  splitArgList( const TQString &args)
{
	TQStringList returnArgs = TQStringList::split( ' ', args);
	return returnArgs;
}

TQString KSimpleProcess::exec( const TQString &args, bool addStdErr )
{
	KSimpleProcess proc;
	return proc.execInternal( args, addStdErr);
}

TQString KSimpleProcess::execInternal( const TQString &args, bool addStdError)
{
	m_proc->setArguments( splitArgList( args ));
	connect(m_proc, TQT_SIGNAL(processExited()), this, TQT_SLOT(slotProcessExited()));
	connect(m_proc, TQT_SIGNAL(readyReadStdout()), this, TQT_SLOT(slotReceivedStdout()));
	if( addStdError )
		connect(m_proc, TQT_SIGNAL(readyReadStderr()), this, TQT_SLOT(slotReceivedStderr()));

	if ( !m_proc->start()  )
		return i18n("Could not run command '%1'.").arg( args.latin1() );
	enter_loop();
	return m_currBuffer;
}

KSimpleProcess::KSimpleProcess() : TQObject(0,0)
{
	m_currBuffer = "";
	m_stdErrOn = false;
	m_proc = new TQProcess(this);
}

KSimpleProcess::~KSimpleProcess()
{
	delete m_proc;
}


// If a troll sees this, he kills david not me
void tqt_enter_modal( TQWidget *widget );
void tqt_leave_modal( TQWidget *widget );

void KSimpleProcess::enter_loop()
{
  TQWidget dummy(0,0,WType_Dialog | WShowModal);
  dummy.setFocusPolicy( TQWidget::NoFocus );
  tqt_enter_modal(&dummy);
  tqApp->enter_loop();
  tqt_leave_modal(&dummy);
}

void KSimpleProcess::slotProcessExited()
{
	while( m_proc->canReadLineStdout() )
		m_currBuffer += m_proc->readLineStdout() + '\n';
	tqApp->exit_loop();
}

void KSimpleProcess::slotReceivedStdout()
{
	m_currBuffer += m_proc->readLineStdout() + '\n';
}

void KSimpleProcess::slotReceivedStderr()
{
	m_currBuffer += m_proc->readLineStderr() + '\n';
}

#include "ksimpleprocess.moc"