cvsprogressdlg.cpp 6.94 KB
Newer Older
1
/* 
2
 *  Copyright (C) 1999-2002 Bernd Gehrmann
Bernd Gehrmann's avatar
Bernd Gehrmann committed
3
 *                          bernd@mail.berlios.de
4
5
6
7
8
9
10
11
12
13
 *
 * This program may be distributed under the terms of the Q Public
 * License as defined by Trolltech AS of Norway and appearing in the
 * file LICENSE.QPL included in the packaging of this file.
 *
 * This program 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.
 */

14
15
16

#include "cvsprogressdlg.h"

Werner Trobin's avatar
Werner Trobin committed
17
#include <qpushbutton.h>
18
19
20
21
22
#include <qdir.h>
#include <qlayout.h>
#include <qlabel.h>
#include <qtimer.h>
#include <qlistbox.h>
Gioele Barabucci's avatar
Gioele Barabucci committed
23
#include <kapplication.h>
24
25
26
27
28
29
30
31
32
33
#include <kbuttonbox.h>
#include <kprocess.h>
#include <klocale.h>
#include <kconfig.h>
#include <kanimwidget.h>

#include "misc.h"
#include "cervisiapart.h"


34
CvsProgressDialog::CvsProgressDialog(const QString &text, QWidget *parent)
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
    : QSemiModal(parent, "", true), childproc(0)
{
    QBoxLayout *layout = new QVBoxLayout(this, 10);

    QBoxLayout *hbox = new QHBoxLayout();
    layout->addLayout(hbox, 0);
    
    QLabel *textlabel = new QLabel(text, this);
    textlabel->setMinimumWidth(textlabel->sizeHint().width());
    textlabel->setFixedHeight(textlabel->sizeHint().height());
    hbox->addWidget(textlabel);

    gear = new KAnimWidget(QString("kde"), 32, this);
    gear->setFixedSize(32, 32);
    hbox->addStretch();
    hbox->addWidget(gear);

    resultbox = new QListBox(this);
    resultbox->setEnabled(false);
    QFontMetrics rb_fm(resultbox->fontMetrics());
    resultbox->setMinimumSize(rb_fm.width("0")*70,
			      rb_fm.lineSpacing()*8);
    layout->addWidget(resultbox, 5);
    
    QFrame *frame = new QFrame(this);
    frame->setFrameStyle(QFrame::HLine | QFrame::Sunken);
    layout->addWidget(frame, 0);

    KButtonBox *buttonbox = new KButtonBox(this);
    layout->addWidget(buttonbox, 0);
    buttonbox->addStretch();
    cancelbutton = buttonbox->addButton(i18n("Cancel"));
    connect( cancelbutton, SIGNAL(clicked()), SLOT(cancelClicked()) );
    buttonbox->addStretch();
    buttonbox->layout();

    layout->activate();
    resize(sizeHint());

    shown = false;
    cancelled = false;
76
    hasError = false;
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
}


CvsProgressDialog::~CvsProgressDialog()
{
    delete childproc;
}


bool CvsProgressDialog::execCommand(const QString &sandbox, const QString &repository,
                                    const QString &cmdline, const QString &errindicator)
{
    KConfig *config = CervisiaPart::config();
    config->setGroup("General");
    int timeout = (int) config->readUnsignedNumEntry("Timeout", 4000);
    
    indic1 = QString("cvs ") + errindicator + ":";
    indic2 = QString("cvs [") + errindicator + " aborted]:";
    resultbox->insertItem(cmdline);

    config->setGroup("Repository-" + repository);
    QString rsh = config->readEntry("rsh");

    childproc = new KShellProcess("/bin/sh");
    if (!sandbox.isEmpty())
        QDir::setCurrent(sandbox);
    if (!rsh.isEmpty())
104
	*childproc << QString("CVS_RSH=" + KShellProcess::quote(rsh) );
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
    *childproc << cmdline;

    connect( childproc, SIGNAL(processExited(KProcess *)),
	     SLOT(childExited()) );
    // We want to be notified when the child provides error output ...
    connect( childproc, SIGNAL(receivedStdout(KProcess *, char *, int)),
             SLOT(receivedOutputNongui(KProcess *, char *, int)) );
    connect( childproc, SIGNAL(receivedStderr(KProcess *, char *, int)),
             SLOT(receivedOutputNongui(KProcess *, char *, int)) );
    // ... or after 4 seconds (i. e. the timeout set by the user)
    timer = new QTimer(this);
    connect( timer, SIGNAL(timeout()),
             this, SLOT(timeoutOccured()) );
    
    timer->start(timeout, true);
    
    if (!childproc->start(KProcess::NotifyOnExit,
                          KProcess::Communication(KProcess::Stdout|KProcess::Stderr)))
        return false;

    QApplication::setOverrideCursor(waitCursor);
    kapp->enter_loop();
    if (QApplication::overrideCursor())
        QApplication::restoreOverrideCursor();

    return !cancelled;
}


void CvsProgressDialog::stopNonguiPart()
{
    timer->stop();
    disconnect( childproc, SIGNAL(receivedStdout(KProcess *, char *, int)),
                this, SLOT(receivedOutputNongui(KProcess *, char *, int)) );
    disconnect( childproc, SIGNAL(receivedStderr(KProcess *, char *, int)),
                this, SLOT(receivedOutputNongui(KProcess *, char *, int)) );

    kapp->exit_loop();
}


void CvsProgressDialog::startGuiPart()
{
    shown = true;
    connect( childproc, SIGNAL(receivedStdout(KProcess *, char *, int)),
	     SLOT(receivedOutput(KProcess *, char *, int)) );
    connect( childproc, SIGNAL(receivedStderr(KProcess *, char *, int)),
	     SLOT(receivedOutput(KProcess *, char *, int)) );
    show();
    gear->start();
    QApplication::restoreOverrideCursor();
    kapp->enter_loop();
}


void CvsProgressDialog::timeoutOccured()
{
    stopNonguiPart();
    startGuiPart();
}



168
bool CvsProgressDialog::getOneLine(QString *str)
169
170
171
172
173
{
    if (output.isEmpty())
        return false;

    *str = output.first();
174
    output.remove(output.begin());
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191

    return true;
}


bool CvsProgressDialog::processOutput()
{
    bool err = false;
    int pos;
    while ( (pos = buf.find('\n')) != -1)
	{
	    QString item = buf.left(pos);
            if (item.left(indic1.length()) == indic1 ||
                item.left(indic2.length()) == indic2 ||
                item.left(21) == "cvs [server aborted]:")
                {
                    err = true;
192
                    hasError = true;
193
194
195
196
197
                    resultbox->insertItem(item);
                }
            else if (item.left(11) == "cvs server:")
                resultbox->insertItem(item);
            else
198
                output.append(item);
199
200
201
202
203
204
205
206
207
	    buf = buf.right(buf.length()-pos-1);
        }
    
    return err;
}


void CvsProgressDialog::receivedOutputNongui(KProcess *, char *buffer, int buflen)
{
208
    buf += QString::fromLocal8Bit(buffer, buflen);
209
210
211
212
213
214
215
216
217
218
    if (processOutput())
        {
            stopNonguiPart();
            startGuiPart();
        }
}


void CvsProgressDialog::receivedOutput(KProcess *, char *buffer, int buflen)
{
219
    buf += QString::fromLocal8Bit(buffer, buflen);
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
    (void) processOutput();
}
 

void CvsProgressDialog::childExited()
{
    if (!shown)
        stopNonguiPart();

    gear->stop();
    if (!buf.isEmpty())
      {
        buf += '\n';
        (void) processOutput();
      }

    // Close the dialog automatically if there are no
    // error messages or the process has been aborted
    // 'by hand' (e.g.  by clicking the cancel button)
239
    if (!hasError || !childproc->normalExit())
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
        kapp->exit_loop();
}


void CvsProgressDialog::cancelClicked()
{
    cancelled = true;

    if (childproc->isRunning())
        childproc->kill();
    else
        kapp->exit_loop();
}


void CvsProgressDialog::closeEvent(QCloseEvent *e)
{
    cancelClicked();
    QSemiModal::closeEvent(e);
}

261
262
#include "cvsprogressdlg.moc"

263
264
265
// Local Variables:
// c-basic-offset: 4
// End: