SVN_SILENT made messages (.desktop file)
[kdeadmin.git] / kpackage / kpTerm.cpp
blob615ff39844ee7d629a987a0f736ea25851b9eb10
1 /*
2 ** Copyright (C) 1999,2000 Toivo Pedaste <toivo@ucs.uwa.edu.au>
3 **
4 */
6 /*
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
27 #include <kpTerm.h>
29 //Added by qt3to4:
30 #include <QtGui/QLabel>
31 #include <QtGui/QKeyEvent>
33 #include <kglobalsettings.h>
34 #include <kdebug.h>
35 #include <kvbox.h>
37 //////////////////////////////////////////////////////////////////////////////
38 //////////////////////////////////////////////////////////////////////////////
40 kpTerm::kpTerm(kpPty *pt, QWidget * parent, const char * name ) :
41 Q3TextEdit(parent,name)
43 pty = pt;
44 setFont(KGlobalSettings::fixedFont());
45 // setMinimumWidth(fontMetrics().maxWidth()*80 +
46 // minimumSizeHint().width());
47 setWordWrap(NoWrap);
48 setReadOnly(true);
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)
73 setReadOnly(false);
74 setFocus();
75 if (pty->startSession(true)) {
76 doConnect();
78 r = pty->run(cmd,false);
79 return true;
80 } else {
81 return false;
85 void kpTerm::cancel() {
86 emit keyOut(QChar::fromLatin1('\03'));
89 void kpTerm::done()
91 clear();
92 doUnconnect();
93 setReadOnly(true);
94 clearFocus();
97 void kpTerm::keyPressEvent ( QKeyEvent * e )
99 // kDebug() << "K=" << e->text() << "\n";
100 if (!e->text().isEmpty()) {
101 emit keyOut(e->text());
102 } else {
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";
114 //` sleep(1);
115 if (stext.indexOf(chrs) < 0) {
116 // kDebug() << "T=[" << stext << "]\n";
117 insert( stext );
118 } else {
119 int p;
120 int op = 0;
122 while ((p = stext.indexOf(chrs,op)) >= 0) {
123 if (p != op) {
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);
138 op = p + 1;
140 if ((signed int)stext.length() > op)
141 insert( stext.right(stext.length()-op));
143 if (bNewLine) {
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) {
153 int x,y;
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);
170 doUnconnect();
175 //////////////////////////////////////////////////////////////////////////////
177 // Dialog window for password prompt
179 //////////////////////////////////////////////////////////////////////////////
180 kpRun::kpRun( QWidget *parent)
181 : KDialog(parent)
183 setModal( true );
184 setButtons( Cancel );
185 KVBox *page = new KVBox(this);
186 setMainWidget( page );
187 title = new QLabel("", page);
188 QFont f( KGlobalSettings::generalFont());
189 f.setBold(true);
190 f.setPointSize(f.pointSize()+4);
191 title->setFont(f);
193 term = new kpTerm(kpty,page);
194 resize(600, 300);
195 connect(term,SIGNAL(result(QStringList &, int)),
196 this,SLOT(slotResult(QStringList &, int)));
197 connect(this,SIGNAL(cancelClicked()),SLOT(slotCancel()));
198 hide();
201 bool kpRun::run(QString cmd, QString msg)
203 QStringList r;
205 title->setText(msg);
206 if (!cmd.isEmpty()) {
207 return term->run(cmd, r);
208 } else {
209 term->doConnect();
210 term->setReadOnly(false);
211 term->setFocus();
212 return true;
216 void kpRun::addText(const QStringList &ret)
218 int last = ret.count()-1;
219 int i = 0;
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) {
229 term->clear();
230 if (ret == 0)
231 accept();
232 else
233 reject();
237 void kpRun::slotCancel()
239 term->clear();
240 term->cancel();
241 accept();
244 #include "kpTerm.moc"