summaryrefslogtreecommitdiffstats
path: root/vcs/cvsservice/cvsprocesswidget.cpp
blob: d4de15c158f352323a9f46725a3b2a29be9a4b10 (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
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
/***************************************************************************
 *   Copyright (C) 2003 by KDevelop Authors                                *
 *   www.kdevelop.org                                                      *
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#include <tqpainter.h>
#include <tqregexp.h>

#include <dcopref.h>
#include <kstatusbar.h>
#include <kdebug.h>
#include <klocale.h>
#include <kmessagebox.h>

#include "kdevpartcontroller.h"
#include "kdevmainwindow.h"
#include "kdevcore.h"

#include "cvspart.h"
#include "cvsprocesswidget.h"
#include "processwidget.h"

#include <cvsjob_stub.h>
#include <cvsservice_stub.h>

// Undef
//#define MYDCOPDEBUG

///////////////////////////////////////////////////////////////////////////////
// class CvsProcessWidget
///////////////////////////////////////////////////////////////////////////////

#ifdef MYDCOPDEBUG
int g_dcopExitCounter = 0;
int g_dcopOutCounter = 0;
int g_dcopErrCounter = 0;
#endif


CvsProcessWidget::CvsProcessWidget( CvsService_stub *service, CvsServicePart *part, TQWidget *parent, const char *name )
    : DCOPObject( "CvsProcessWidgetDCOPIface" ),
    TQTextEdit( parent, name ),
	m_part( part ), m_service( service ), m_job( 0 )
{
    setReadOnly( true );
    setTextFormat( TQt::LogText );

    TQStyleSheetItem *style = 0;
    style = new TQStyleSheetItem( styleSheet(), "goodtag" );
    style->setColor( "black" );

    style = new TQStyleSheetItem( styleSheet(), "errortag" );
    style->setColor( "red" );
    style->setFontWeight( TQFont::Bold );

    style = new TQStyleSheetItem( styleSheet(), "infotag" );
    style->setColor( "blue" );

    style = new TQStyleSheetItem( styleSheet(), "cvs_conflict" );
    style->setColor( "red" );

    style = new TQStyleSheetItem( styleSheet(), "cvs_added" );
    style->setColor( "green" );

    style = new TQStyleSheetItem( styleSheet(), "cvs_removed" );
    style->setColor( "yellow" );

    style = new TQStyleSheetItem( styleSheet(), "cvs_updated" );
    style->setColor( "lightblue" );

    style = new TQStyleSheetItem( styleSheet(), "cvs_modified" );
    style->setColor( "darkgreen" );

    style = new TQStyleSheetItem( styleSheet(), "cvs_unknown" );
    style->setColor( "gray" );
}

///////////////////////////////////////////////////////////////////////////////

CvsProcessWidget::~CvsProcessWidget()
{
    if (m_job)
    {
        delete m_job;
    }
}

///////////////////////////////////////////////////////////////////////////////

bool CvsProcessWidget::isAlreadyWorking() const
{
    if (m_job)
        return m_job->isRunning();
    else
       return false;
}

///////////////////////////////////////////////////////////////////////////////

void CvsProcessWidget::clear()
{
    TQTextEdit::clear();
    this->m_errors = TQString();
    this->m_output = TQString();
}

///////////////////////////////////////////////////////////////////////////////

bool CvsProcessWidget::startJob( const DCOPRef &aJob )
{
    kdDebug(9006) << "CvsProcessWidget::startJob(const DCOPRef &) here!" << endl;

    clear();
    m_part->mainWindow()->raiseView( this );
    m_part->core()->running( m_part, true );

    // create a DCOP stub for the non-concurrent cvs job
    if (m_job)
    {
        delete m_job;
        m_job = 0;
    }
    m_job = new CvsJob_stub( aJob.app(), aJob.obj() );

    // establish connections to the signals of the cvs m_job
    connectDCOPSignal( m_job->app(), m_job->obj(), "jobExited(bool, int)", "slotJobExited(bool, int)", true );
    connectDCOPSignal( m_job->app(), m_job->obj(), "receivedStdout(TQString)", "slotReceivedOutput(TQString)", true );
    connectDCOPSignal( m_job->app(), m_job->obj(), "receivedStderr(TQString)", "slotReceivedErrors(TQString)", true );

    // get command line and add it to output buffer
    TQString cmdLine = m_job->cvsCommand();
    m_part->mainWindow()->statusBar()->message( cmdLine );

    kdDebug(9006) << "Running: " << cmdLine << endl;

    // disconnect 3rd party slots from our signals
    disconnect( TQT_SIGNAL(jobFinished(bool, int)) );

    showInfo( i18n("Started job: %1").arg( cmdLine ) );

#ifdef MYDCOPDEBUG
    g_dcopExitCounter = 0;
    g_dcopOutCounter = 0;
    g_dcopErrCounter = 0;
#endif

    return m_job->execute();
}

///////////////////////////////////////////////////////////////////////////////

void CvsProcessWidget::cancelJob()
{
    kdDebug(9006) << "CvsProcessWidget::cancelJob() here!" << endl;

    if (!m_job || !m_job->isRunning())
        return;
    m_job->cancel();
    delete m_job; m_job = 0;

    showInfo( i18n("*** Job canceled by user request ***") );

    m_part->core()->running( m_part, false );
}

///////////////////////////////////////////////////////////////////////////////

void CvsProcessWidget::slotJobExited( bool normalExit, int exitStatus )
{
    kdDebug(9006) << "CvsProcessWidget::slotJobExited(bool, int) here!" << endl;
#ifdef MYDCOPDEBUG
    g_dcopExitCounter++;
    kdDebug(9006) << "MYDCOPDEBUG: dcopExitCounter == " << g_dcopExitCounter << endl;
#endif
    if (m_job)
    {
        disconnectDCOPSignal( m_job->app(), m_job->obj(), "jobExited(bool, int)", "slotJobExited(bool, int)" );
        disconnectDCOPSignal( m_job->app(), m_job->obj(), "receivedStdout(TQString)", "slotReceivedOutput(TQString)" );
        disconnectDCOPSignal( m_job->app(), m_job->obj(), "receivedStderr(TQString)", "slotReceivedErrors(TQString)" );
        delete m_job;
        m_job = 0;
    }
    TQString exitMsg = i18n("Job finished with exitCode == %1");
    showInfo( exitMsg.arg( exitStatus) );

    m_part->core()->running( m_part, false );
    m_part->mainWindow()->statusBar()->message( i18n("Done CVS command ..."), 2000 );

    emit jobFinished( normalExit, exitStatus );
}

///////////////////////////////////////////////////////////////////////////////

void CvsProcessWidget::slotReceivedOutput( TQString someOutput )
{
    kdDebug(9006) << "CvsProcessWidget::slotReceivedOutput(TQString)  here!" << endl;
#ifdef MYDCOPDEBUG
    g_dcopOutCounter++;
    kdDebug(9006) << "MYDCOPDEBUG: dcopOutCounter == " << g_dcopOutCounter << endl;
#endif

    TQStringList strings = m_outputBuffer.process( someOutput );
    if (strings.count() > 0)
    {
        m_output += strings;
        showOutput( strings );
        scrollToBottom();
    }
}

///////////////////////////////////////////////////////////////////////////////

void CvsProcessWidget::slotReceivedErrors( TQString someErrors )
{
    kdDebug(9006) << "CvsProcessWidget::slotReceivedErrors(TQString)  here!" << endl;
#ifdef MYDCOPDEBUG
    g_dcopErrCounter++;
    kdDebug(9006) << "MYDCOPDEBUG: dcopErrCounter == " << g_dcopErrCounter << endl;
#endif

    TQStringList strings = m_errorBuffer.process( someErrors );
    if (strings.count() > 0)
    {
        m_errors += strings;
        showError( strings );
        scrollToBottom();
    }
}

///////////////////////////////////////////////////////////////////////////////

void CvsProcessWidget::showInfo( const TQStringList &msg )
{
    for (TQStringList::const_iterator it = msg.begin(); it != msg.end(); ++it)
        append( "<infotag>" + (*it) + "</infotag>" );
}

///////////////////////////////////////////////////////////////////////////////

void CvsProcessWidget::showError( const TQStringList &msg )
{
    for (TQStringList::const_iterator it = msg.begin(); it != msg.end(); ++it)
        append( "<errortag>" + (*it) + "</errortag>" );
}

///////////////////////////////////////////////////////////////////////////////

void CvsProcessWidget::showOutput( const TQStringList &msg )
{
    for (TQStringList::const_iterator it = msg.begin(); it != msg.end(); ++it)
    {
        // @todo here we can interpret lines as [C], [M], ...
        const TQString &line = (*it);
        
        //If the line already contains tags we need to replace the 
        //delimiters with the corresponding HTML code so that they are no longer 
        //recognized as tags.
        //This will prevent TQTextEdit from crashing on trying to parse the tags.
        //This should fix BUG:99590
        TQString lineNew(line);
        lineNew.replace("<", "&lt;");
        lineNew.replace(">", "&gt;");
        lineNew.replace("&", "&amp;");

        if (line.startsWith( "C " ))
            append( "<cvs_conflict>" + lineNew + "</cvs_conflict>" );
        else if (line.startsWith( "M " ))
            append( "<cvs_modified>" + lineNew + "</cvs_modified>" );
        else if (line.startsWith( "A " ))
            append( "<cvs_added>" + lineNew + "</cvs_added>" );
        else if (line.startsWith( "R " ))
            append( "<cvs_removed>" + lineNew + "</cvs_removed>" );
        else if (line.startsWith( "U " ))
            append( "<cvs_updated>" + lineNew + "</cvs_updated>" );
        else if (line.startsWith( "? " ))
            append( "<cvs_unknown>" + lineNew + "</cvs_unknown>" );
        else // default
            append( "<goodtag>" + lineNew + "</goodtag>" );
    }
}

#include "cvsprocesswidget.moc"