File: progressdialog.cpp

package info (click to toggle)
cervisia 4:17.08.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 7,548 kB
  • sloc: cpp: 14,364; xml: 393; makefile: 3; sh: 3
file content (337 lines) | stat: -rw-r--r-- 10,219 bytes parent folder | download | duplicates (2)
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
94
95
96
97
98
99
100
101
102
103
104
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
 *  Copyright (c) 1999-2002 Bernd Gehrmann <bernd@mail.berlios.de>
 *  Copyright (c) 2002-2004 Christian Loose <christian.loose@kdemail.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "progressdialog.h"

#include <QString>
#include <QStringList>
#include <QTimer>
#include <QEventLoop>
#include <QProgressBar>
#include <QPlainTextEdit>
#include <QLabel>
#include <QApplication>
#include <QDialogButtonBox>
#include <QVBoxLayout>
#include <QHBoxLayout>

#include <KConfigGroup>

#include <cvsjobinterface.h>
#include "cervisiasettings.h"
#include "debug.h"

//---------------------------------------------------------------------

struct ProgressDialog::Private
{
    bool            isCancelled;
    bool            isShown;
    bool            hasError;
    bool            isDiff;

    OrgKdeCervisia5CvsserviceCvsjobInterface*    cvsJob;
    QString         jobPath;
    QString         buffer;
    QString         errorId1, errorId2;
    QStringList     output;
    QEventLoop      eventLoop;

    QTimer*         timer;
    QProgressBar*   busy;
    QPlainTextEdit* resultbox;
};

//---------------------------------------------------------------------

ProgressDialog::ProgressDialog(QWidget* parent, const QString& heading,const QString &cvsServiceNameService,
                               const QDBusReply<QDBusObjectPath>& jobPath, const QString& errorIndicator,
                               const QString& caption)
    : QDialog(parent)
    , d(new Private)
{
    setWindowTitle(caption);
    setModal(true);

    setupGui(heading);

    d->isCancelled = false;
    d->isShown     = false;
    d->hasError    = false;
    d->isDiff      = heading == QLatin1String("Diff");  // cvs returns error status when there is a difference

    QDBusObjectPath path = jobPath;
    d->jobPath = path.path();
    d->cvsJob = new OrgKdeCervisia5CvsserviceCvsjobInterface(cvsServiceNameService, path.path(), QDBusConnection::sessionBus(), this);

    qCDebug(log_cervisia) << "cvsServiceNameService:" << cvsServiceNameService
                 << "CvsjobInterface" << path.path() << "valid:" << d->cvsJob->isValid();

    d->errorId1 = "cvs " + errorIndicator + ':';
    d->errorId2 = "cvs [" + errorIndicator + " aborted]:";
}

//---------------------------------------------------------------------

ProgressDialog::~ProgressDialog()
{
    delete d->cvsJob;
    delete d;
}

//---------------------------------------------------------------------

void ProgressDialog::setupGui(const QString& heading)
{
    QVBoxLayout *mainLayout = new QVBoxLayout(this);

    QLabel *textLabel = new QLabel(heading);
    mainLayout->addWidget(textLabel);

    d->resultbox = new QPlainTextEdit;
    d->resultbox->setReadOnly(true);
    QFontMetrics fm(d->resultbox->fontMetrics());
    d->resultbox->setMinimumSize(fm.width("0")*70, fm.lineSpacing()*8);
    mainLayout->addWidget(d->resultbox);

    QHBoxLayout *hbox = new QHBoxLayout;

    d->busy = new QProgressBar;
    d->busy->setMinimum(0);
    d->busy->setMaximum(0);  // min == max  =>  busy indicator
    hbox->addWidget(d->busy);
    d->busy->hide();

    QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Cancel);
    connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
    hbox->addWidget(buttonBox);

    mainLayout->addLayout(hbox);
}

//---------------------------------------------------------------------

bool ProgressDialog::execute()
{
    // get command line and display it
    QString cmdLine = d->cvsJob->cvsCommand();
    d->resultbox->insertPlainText(cmdLine);
    qCDebug(log_cervisia) << "cmdLine:" << cmdLine;

    QDBusConnection::sessionBus().connect(QString(), d->jobPath,
                                          "org.kde.cervisia5.cvsservice.cvsjob",
                                          "jobExited", this,
                                          SLOT(slotJobExited(bool,int)));

    QDBusConnection::sessionBus().connect(QString(), d->jobPath,
                                          "org.kde.cervisia5.cvsservice.cvsjob",
                                          "receivedStdout", this,
                                          SLOT(slotReceivedOutputNonGui(QString)));

    QDBusConnection::sessionBus().connect(QString(), d->jobPath,
                                          "org.kde.cervisia5.cvsservice.cvsjob",
                                          "receivedStderr", this,
                                          SLOT(slotReceivedOutputNonGui(QString)));

    // we wait for 4 seconds (or the timeout set by the user) before we
    // force the dialog to show up
    d->timer = new QTimer(this);
    connect(d->timer, SIGNAL(timeout()), this, SLOT(slotTimeoutOccurred()));
    d->timer->setSingleShot(true);
    d->timer->start(CervisiaSettings::timeout());

    bool started = d->cvsJob->execute();
    if( !started )
        return false;

    QApplication::setOverrideCursor(Qt::WaitCursor);

    d->eventLoop.exec();

    if (QApplication::overrideCursor())
        QApplication::restoreOverrideCursor();

    return !d->isCancelled;
}

//---------------------------------------------------------------------

bool ProgressDialog::getLine(QString& line)
{
    if( d->output.isEmpty() )
        return false;

    line = d->output.first();
    d->output.removeFirst();

    return true;
}

//---------------------------------------------------------------------

QStringList ProgressDialog::getOutput() const
{
    return d->output;
}

//---------------------------------------------------------------------

void ProgressDialog::slotReceivedOutputNonGui(QString buffer)
{
    qCDebug(log_cervisia) << buffer;

    d->buffer += buffer;

    processOutput();
    if( d->hasError )
    {
        stopNonGuiPart();
        startGuiPart();
    }
}

//---------------------------------------------------------------------

void ProgressDialog::slotReceivedOutput(QString buffer)
{
    qCDebug(log_cervisia) << buffer;
    d->buffer += buffer;
    processOutput();
}

//---------------------------------------------------------------------

void ProgressDialog::slotJobExited(bool normalExit, int status)
{
    Q_UNUSED(normalExit)

    if( !d->isShown )
        stopNonGuiPart();

    d->busy->hide();

    if( !d->buffer.isEmpty() )
    {
        d->buffer += '\n';
        processOutput();
    }

    if ( (status != 0) && !d->isDiff )  // cvs command exited with error -> show error text
    {
        QString line;
        while ( getLine(line) )
        {
            d->resultbox->insertPlainText(QLatin1String("\n"));
            d->resultbox->insertPlainText(line);
        }
        startGuiPart();
        d->busy->hide();
        return;
    }

    // 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)
    if( !d->hasError || d->isCancelled )
        d->eventLoop.exit();
}

//---------------------------------------------------------------------

void ProgressDialog::reject()
{
    d->isCancelled = true;

    bool isRunning = d->cvsJob->isRunning();
    if( isRunning )
        d->cvsJob->cancel();
    else
        d->eventLoop.exit();

    QDialog::reject();
}

//---------------------------------------------------------------------

void ProgressDialog::slotTimeoutOccurred()
{
    stopNonGuiPart();
    startGuiPart();
}

//---------------------------------------------------------------------

void ProgressDialog::stopNonGuiPart()
{
    d->timer->stop();

    QDBusConnection::sessionBus().disconnect(QString(), d->jobPath, "org.kde.cervisia5.cvsservice.cvsjob",
                                             "receivedStdout", this, SLOT(slotReceivedOutputNonGui(QString)));

    QDBusConnection::sessionBus().disconnect(QString(), d->jobPath, "org.kde.cervisia5.cvsservice.cvsjob",
                                             "receivedStderr", this, SLOT(slotReceivedOutputNonGui(QString)));
}

//---------------------------------------------------------------------

void ProgressDialog::startGuiPart()
{
    QDBusConnection::sessionBus().connect(QString(), d->jobPath, "org.kde.cervisia5.cvsservice.cvsjob",
                                          "receivedStdout", this, SLOT(slotReceivedOutput(QString)));

    QDBusConnection::sessionBus().connect(QString(), d->jobPath, "org.kde.cervisia5.cvsservice.cvsjob",
                                          "receivedStderr", this, SLOT(slotReceivedOutput(QString)));

    show();
    d->isShown = true;

    d->busy->show();
    QApplication::restoreOverrideCursor();
}

//---------------------------------------------------------------------

void ProgressDialog::processOutput()
{
    int pos;
    while( (pos = d->buffer.indexOf('\n')) != -1 )
    {
        QString item = d->buffer.left(pos);
        if( item.startsWith(d->errorId1) ||
            item.startsWith(d->errorId2) ||
            item.startsWith(QLatin1String("cvs [server aborted]:")) )
        {
            d->hasError = true;
            d->resultbox->insertPlainText(QLatin1String("\n"));
            d->resultbox->insertPlainText(item);
        }
        else if( item.startsWith(QLatin1String("cvs server:")) )
        {
            d->resultbox->insertPlainText(QLatin1String("\n"));
            d->resultbox->insertPlainText(item);
        }
        else
            d->output.append(item);

        // remove item from buffer
        d->buffer.remove(0, pos+1);
    }
}

//---------------------------------------------------------------------