2 ** Copyright (C) 1999,2000 Toivo Pedaste <toivo@ucs.uwa.edu.au>
7 ** This program is free software; you can redistribute it and/or modify
8 ** it under the terms of the GNU General Public License as published by
9 ** the Free Software Foundation; either version 2 of the License, or
10 ** (at your option) any later version.
12 ** This program is distributed in the hope that it will be useful,
13 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ** GNU General Public License for more details.
17 ** You should have received a copy of the GNU General Public License
18 ** along with this program in a file called COPYING; if not, write to
19 ** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20 ** MA 02110-1301, USA.
24 ** Bug reports and questions can be sent to kde-devel@kde.org
30 #include <QtGui/QLabel>
31 #include <QtGui/QKeyEvent>
33 #include <kglobalsettings.h>
37 //////////////////////////////////////////////////////////////////////////////
38 //////////////////////////////////////////////////////////////////////////////
40 kpTerm::kpTerm(kpPty
*pt
, QWidget
* parent
, const char * name
) :
41 Q3TextEdit(parent
,name
)
44 setFont(KGlobalSettings::fixedFont());
45 // setMinimumWidth(fontMetrics().maxWidth()*80 +
46 // minimumSizeHint().width());
51 void kpTerm::doConnect()
53 connect(pty
, SIGNAL(textIn(const QString
&, bool)), this,
54 SLOT(textIn(const QString
&, bool)));
55 connect(pty
,SIGNAL(result(QStringList
&, int)),
56 this,SLOT(slotResult(QStringList
&, int)));
57 connect(this, SIGNAL(keyOut(QString
)), pty
,
58 SLOT(keyOut(QString
)));
61 void kpTerm::doUnconnect()
63 disconnect(pty
, SIGNAL(textIn(const QString
&, bool)), this,
64 SLOT(textIn(const QString
&, bool)));
65 disconnect(pty
,SIGNAL(result(QStringList
&, int)),
66 this,SLOT(slotResult(QStringList
&, int)));
67 disconnect(this, SIGNAL(keyOut(QString
)), pty
,
68 SLOT(keyOut(QString
)));
71 bool kpTerm::run(const QString
&cmd
, QStringList
&r
)
75 if (pty
->startSession(true)) {
78 r
= pty
->run(cmd
,false);
85 void kpTerm::cancel() {
86 emit
keyOut(QChar::fromLatin1('\03'));
97 void kpTerm::keyPressEvent ( QKeyEvent
* e
)
99 // kDebug() << "K=" << e->text() << "\n";
100 if (!e
->text().isEmpty()) {
101 emit
keyOut(e
->text());
103 Q3TextEdit::keyPressEvent (e
);
105 setCursorPosition(9999,9999);
108 void kpTerm::textIn(const QString
&stext
, bool bNewLine
)
110 // kDebug() << "=" << bNewLine << " [" << stext << "]\n";
111 QRegExp
chrs("[\\010\\012\\015]");
112 QString del
= "\010";
113 // kDebug() << "Tin=[" << stext << "]\n";
115 if (stext
.indexOf(chrs
) < 0) {
116 // kDebug() << "T=[" << stext << "]\n";
122 while ((p
= stext
.indexOf(chrs
,op
)) >= 0) {
124 insert( stext
.mid(op
, p
-op
));
125 // kDebug() << "B=[" << stext.mid(op, p-op) << "]\n";
127 if (stext
[p
] == '\b') {
128 doKeyboardAction(ActionBackspace
);
129 // kDebug() << "Back\n";
130 } else if (stext
[p
] == '\r') {
131 // kDebug() << "Start\n";
132 moveCursor(MoveLineStart
, false);
133 } else if (stext
[p
] == '\n') {
134 // kDebug() << "New1\n";
135 moveCursor(MoveEnd
, false);
136 doKeyboardAction(ActionReturn
);
140 if ((signed int)stext
.length() > op
)
141 insert( stext
.right(stext
.length()-op
));
144 // kDebug() << "End2\n";
145 moveCursor(MoveEnd
, false);
146 doKeyboardAction(ActionReturn
);
148 // kDebug() << "End3\n";
149 // moveCursor(MoveEnd, false);
152 void kpTerm::insert ( const QString
& str
, bool) {
154 getCursorPosition(&y
,&x
);
156 if (str
.length() > 0) {
157 // kDebug() << "ins:" << y << "," << x << str <<":" << str.length() << "\n";
158 if (x
== 0 && str
!= "\n") {
159 doKeyboardAction(ActionKill
);
160 getCursorPosition(&y
,&x
);
161 // kDebug() << "k=" << y << "," << x <<"\n";
163 Q3TextEdit::insert(str
,(bool)false);
167 void kpTerm::slotResult(QStringList
&rlist
, int ret
)
169 emit
result(rlist
, ret
);
175 //////////////////////////////////////////////////////////////////////////////
177 // Dialog window for password prompt
179 //////////////////////////////////////////////////////////////////////////////
180 kpRun::kpRun( QWidget
*parent
)
184 setButtons( Cancel
);
185 KVBox
*page
= new KVBox(this);
186 setMainWidget( page
);
187 title
= new QLabel("", page
);
188 QFont
f( KGlobalSettings::generalFont());
190 f
.setPointSize(f
.pointSize()+4);
193 term
= new kpTerm(kpty
,page
);
195 connect(term
,SIGNAL(result(QStringList
&, int)),
196 this,SLOT(slotResult(QStringList
&, int)));
197 connect(this,SIGNAL(cancelClicked()),SLOT(slotCancel()));
201 bool kpRun::run(QString cmd
, QString msg
)
206 if (!cmd
.isEmpty()) {
207 return term
->run(cmd
, r
);
210 term
->setReadOnly(false);
216 void kpRun::addText(const QStringList
&ret
)
218 int last
= ret
.count()-1;
220 for ( QStringList::ConstIterator it
= ret
.begin(); it
!= ret
.end(); ++it
, ++i
) {
221 // kDebug() << "ks=" << *it << "\n";
222 term
->textIn(*it
, (i
!= last
));
226 void kpRun::slotResult(QStringList
&, int ret
)
228 if (ret
== 0 || ret
== 666) {
237 void kpRun::slotCancel()
244 #include "kpTerm.moc"