summaryrefslogtreecommitdiffstats
path: root/kbabel/catalogmanager/libcvs/cvsdialog.cpp
blob: 9568eff3d4f38614d88d62c4994e4a46e94e612e (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
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/* ****************************************************************************
  This file is part of KBabel

  Copyright (C) 2002-2003 by Marco Wegner <mail@marcowegner.de>
  Copyright (C) 2005, 2006 by Nicolas GOUTTE <goutte@kde.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.

  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.

  In addition, as a special exception, the copyright holders give
  permission to link the code of this program with any edition of
  the TQt library by Trolltech AS, Norway (or with modified versions
  of TQt that use the same license as TQt), and distribute linked
  combinations including the two.  You must obey the GNU General
  Public License in all respects for all of the code used other than
  TQt. If you modify this file, you may extend this exception to
  your version of the file, but you are not obligated to do so.  If
  you do not wish to do so, delete this exception statement from
  your version.

**************************************************************************** */


// TQt include files
#include <tqcheckbox.h>
#include <tqcombobox.h>
#include <tqfileinfo.h>
#include <tqframe.h>
#include <tqlabel.h>
#include <tqlayout.h>
#include <tqlistbox.h>
#include <tqpushbutton.h>
#include <tqstring.h>
#include <tqstringlist.h>
#include <tqtextedit.h>
#include <tqtextcodec.h>
// KDE include files
#include <tdeconfig.h>
#include <kdebug.h>
#include <tdeglobal.h>
#include <tdelocale.h>
#include <kprocess.h>
#include <tdetempfile.h>
#include <tdemessagebox.h>
#include <kstringhandler.h>
#include <kcombobox.h>
#include <kcharsets.h>
// Project specific include files
#include "cvsdialog.h"


CVSDialog::CVSDialog( CVS::Command cmd, TQWidget * parent, TDESharedConfig* config )
  : KDialog( parent, "CVSDIALOG", true ), m_config( config )
{
  _cmd = cmd;
  p=0L;
  setCaption( i18n( "CVS Dialog" ) );

  TQString temp;

  TQVBoxLayout * layout = new TQVBoxLayout( this, 6, 6, "MAIN LAYOUT" );

  // Set the label's text depending on the CVS command.
  switch ( cmd ) {
    case CVS::Update:
      temp = i18n( "Update the following files:" );
      break;
    case CVS::Commit:
      temp = i18n( "Commit the following files:" );
      break;
    case CVS::Status:
      temp = i18n( "Get status for the following files:" );
      break;
    case CVS::Diff:
      temp = i18n( "Get diff for the following files:" );
      break;
  }
  layout->addWidget( new TQLabel( temp, this ) );

  // Widget for showing the list of files.
  filebox = new TQListBox( this );
  layout->addWidget( filebox );

  // Add special widgets for 'cvs commit'.
  if ( cmd == CVS::Commit ) {
    TQLabel * label;

    // Combobox for displaying old log messages.
    label = new TQLabel( i18n( "&Old messages:" ), this );
    oldMessages = new TQComboBox( this );
    oldMessages->setDuplicatesEnabled( false );
    label->setBuddy( oldMessages );
    layout->addWidget( label );
    layout->addWidget( oldMessages );

    // Textfield for entering a log message.
    label = new TQLabel( i18n( "&Log message:" ), this );
    logedit = new TQTextEdit( this );
    label->setBuddy( logedit );
    layout->addWidget( label );
    layout->addWidget( logedit );

    label = new TQLabel( i18n( "E&ncoding:" ), this );
    m_encodingComboBox = new KComboBox( this );
    label->setBuddy( m_encodingComboBox );
    layout->addWidget( label );
    layout->addWidget( m_encodingComboBox );
    TQStringList encodingList;
    // The last encoding will be added at the top of the list, when the seetings will be read.
    encodingList << i18n( "Descriptive encoding name", "Recommended ( %1 )" ).arg( "UTF-8" );
    encodingList << i18n( "Descriptive encoding name", "Locale ( %1 )" ).arg( TQTextCodec::codecForLocale()->mimeName() );
    encodingList += TDEGlobal::charsets()->descriptiveEncodingNames();
    m_encodingComboBox->insertStringList( encodingList );
    
    connect( oldMessages, TQT_SIGNAL( activated( int ) ),
      this, TQT_SLOT( slotComboActivated( int ) ) );
  }

  TQHBoxLayout * buttons = new TQHBoxLayout( 0, 0, 6, "BUTTON LAYOUT" );
  // Add special buttons for 'cvs commit'.
  if ( cmd == CVS::Commit ) {
    autoAddBox = new TQCheckBox( i18n( "Auto&matically add files if necessary" ), this );
    buttons->addWidget( autoAddBox );
  }
  buttons->addItem( new TQSpacerItem( 1, 0, TQSizePolicy::Expanding, TQSizePolicy::Minimum ) );

  // Set the main button's text depending on the CVS comand.
  switch ( cmd ) {
    case CVS::Update:
      temp = i18n( "&Update" );
      break;
    case CVS::Commit:
      temp = i18n( "&Commit" );
      break;
    case CVS::Status:
      temp = i18n( "&Get Status" );
      break;
    case CVS::Diff:
      temp = i18n( "&Get Diff" );
      break;
  }
  mainBtn = new TQPushButton( temp, this );
  mainBtn->setDefault( true );
  buttons->addWidget( mainBtn );

  cancelBtn = new TQPushButton( i18n( "C&ancel" ), this );
  buttons->addWidget( cancelBtn );
  layout->addLayout( buttons );

  TQFrame * line = new TQFrame( this );
  line->setFrameStyle( TQFrame::HLine | TQFrame::Sunken );
  layout->addWidget( line );

  layout->addWidget( new TQLabel( i18n( "Command output:" ), this ) );

  output = new TQTextEdit( this );
  output->setReadOnly( true );
  layout->addWidget( output );

  resize( TQSize( 600, 450 ).expandedTo( minimumSizeHint( ) ) );

  if ( cmd == CVS::Commit )
    logedit->setFocus( );

  readSettings( );

  connect( mainBtn, TQT_SIGNAL( clicked( ) ), this, TQT_SLOT( slotExecuteCommand( ) ) );
  connect( cancelBtn, TQT_SIGNAL( clicked( ) ), this, TQT_SLOT( reject( ) ) );
}

void CVSDialog::slotComboActivated( int index )
{
  if ( index < 0 || index >= m_logMessages.count() )
    return;
  logedit->setText( m_logMessages[index] );
}

CVSDialog::~CVSDialog()
{
    delete p;
}

void CVSDialog::accept( )
{
  saveSettings( );
  KDialog::accept( );
}

void CVSDialog::setFiles( const TQStringList& files )
{
  filebox->insertStringList( files );
}

void CVSDialog::setCommandLine( const TQString& command )
{
  _commandLine = command;
}

void CVSDialog::setAddCommand( const TQString& command )
{
  _addCommand = command;
}

void CVSDialog::slotExecuteCommand( )
{
  // Nothing to do here.
  if ( _commandLine.isEmpty( ) ) return;

  kdDebug() << "Preparing TDEProcess" << endl;
  
  // Create a new shell process
  p = new TDEProcess;
  p->setUseShell( true, "/bin/sh" );

  if ( _cmd == CVS::Commit ) {
    // Include command for 'cvs add'.
    if ( autoAddBox->isChecked( ) && !_addCommand.isEmpty( ) )
      _commandLine.prepend( _addCommand );

    const TQString msg( logedit->text() );

    if ( msg.isEmpty() )
    {
        // A good commit should never have an empty comment, so ask the user if he really wants it.
        const int res = KMessageBox::warningContinueCancel( this,
            i18n( "The commit log message is empty. Do you want to continue?" ) );
        if ( res != KMessageBox::Continue )
            return;
    }

    m_encoding = TDEGlobal::charsets()->encodingForName( m_encodingComboBox->currentText() );
    TQTextCodec* codec =  TQTextCodec::codecForName( m_encoding.utf8() );

    if ( !codec )
    {
        KMessageBox::error( this, i18n( "Cannot find encoding: %1" ).arg( m_encoding ) );
        return;
    }
    else if ( !codec->canEncode( msg ) )
    {
        const int res = KMessageBox::warningContinueCancel( this,
            i18n( "The commit log message cannot be encoded in the selected encoding: %1.\n"
            "Do you want to continue?" ).arg( m_encoding ) );
        if ( res != KMessageBox::Continue )
            return;
    }
    
    // Write the commit log message from the input field to a temporary file
    m_tempFile = new KTempFile;
    m_tempFile->setAutoDelete( true );
    TQTextStream* stream = m_tempFile->textStream();
    if ( !stream )
    {
        kdError() << "Could not create TQTextStream for file " << m_tempFile->name();
        delete m_tempFile;
        m_tempFile = 0;
        KMessageBox::error( this, i18n( "Cannot open temporary file for writing. Aborting.") );
        return;
    }
    stream->setCodec( codec );
    *stream << msg;
    m_tempFile->close();
    
    if ( m_tempFile->status() )
    {
        kdError() << "Could not write to file " << m_tempFile->name();
        delete m_tempFile;
        m_tempFile = 0;
        KMessageBox::error( this, i18n( "Cannot write to temporary file. Aborting.") );
        return;
    }
    
    // Change the command line to have the real name of the temporary file
    _commandLine.replace( "@LOG@FILE@", TDEProcess::quote( m_tempFile->name() ) );

    // Update the list of log messages
    if ( !msg.isEmpty() ) {
      const TQString shortLog = KStringHandler::csqueeze( msg, 80 );
      

      // Remove the message from the list if it already exists
      m_logMessages.remove( msg );
      // Prepend the current message to the list
      m_logMessages.prepend( msg );

      // At this time of the process, we do not need the combobox anymore, so we do not squeeze the changed strings.
    }
  }

  // Set the TDEProcess' command line.
  *p << _commandLine;

  connect( p, TQT_SIGNAL( receivedStdout( TDEProcess*, char*, int ) ),
    this, TQT_SLOT ( slotProcessStdout( TDEProcess*, char*, int ) ) );
  connect( p, TQT_SIGNAL( receivedStderr( TDEProcess*, char*, int ) ),
    this, TQT_SLOT ( slotProcessStderr( TDEProcess*, char*, int ) ) );
  connect( p, TQT_SIGNAL( processExited( TDEProcess* ) ),
    this, TQT_SLOT( slotProcessExited( TDEProcess* ) ) );

  output->append( i18n( "[ Starting command ]" ) );

  if ( p->start( TDEProcess::NotifyOnExit, TDEProcess::Communication( TDEProcess::AllOutput ) ) ) {
    // Disable the main button (and the log edit if in commit mode) to
    // indicate activity.
    mainBtn->setEnabled( false );
    if ( _cmd == CVS::Commit )
      logedit->setEnabled( false );
  } else
  {
      kdError() << "Process could not be started." << endl;
      KMessageBox::error( this, i18n( "The process could not be started." ) );
  }
}

void CVSDialog::slotProcessStdout( TDEProcess*, char * buffer, int len )
{
  output->append( TQString::fromLocal8Bit( buffer, len ) );
  // Set the cursor's position at the end of the output.
  output->setCursorPosition( output->lines( ), 0 );

  // If the command is 'cvs status' or 'cvs diff' collect the output of stdout.
  if ( (_cmd == CVS::Status) || (_cmd == CVS::Diff) )
    _statusOutput += TQString::fromLocal8Bit( buffer, len );
}

void CVSDialog::slotProcessStderr( TDEProcess*, char * buffer, int len )
{
  // If an error occurs while executing the command display stderr in
  // another color.
  TQColor oldColor( output->color( ) );
  output->setColor( TQt::red );
  output->append( TQString::fromLocal8Bit( buffer, len ) );
  output->setColor( oldColor );
  output->setCursorPosition( output->lines( ), 0 );
}

void CVSDialog::slotProcessExited( TDEProcess * p )
{
  if ( p->exitStatus( ) )
    output->append( i18n( "[ Exited with status %1 ]" ).arg( p->exitStatus( ) ) );
  else
    output->append( i18n( "[ Finished ]" ) );

  // The command is finished. Now we can reconnect the main button.
  disconnect( mainBtn, 0, 0, 0 );
  if ( _cmd == CVS::Diff )
    mainBtn->setText( i18n( "&Show Diff" ) );
  else
    mainBtn->setText( i18n( "&Close" ) );
  connect( mainBtn, TQT_SIGNAL( clicked( ) ), this, TQT_SLOT( accept( ) ) );

  // Reenable the button and the log edit now that the process is finished.
  mainBtn->setEnabled( true );
  if ( _cmd == CVS::Commit )
    logedit->setEnabled( true );
}

TQString CVSDialog::statusOutput( )
{
  return _statusOutput;
}

void CVSDialog::readSettings( )
{
  TDESharedConfig * config = m_config;
  config->setGroup( "CVSSupport" );

  if ( _cmd == CVS::Commit ) {
    autoAddBox->setChecked( config->readBoolEntry( "AutoAddFiles", true ) );

    // Fill the combobox with old messages.
    m_logMessages.clear();
    m_squeezedLogMessages.clear();
    for ( int cnt = 0; cnt < 10; cnt++ )
      if ( config->hasKey( TQString( "CommitLogMessage%1" ).arg( cnt ) ) )
    {
      const TQString logMessage = config->readEntry( TQString( "CommitLogMessage%1" ).arg( cnt ) );
      if ( !logMessage.isEmpty() )
      {
          // If the message is too long, cut it to 80 characters (or the combo box becomes too wide)
          // ### FIXME: if the string matches the squeezed 80 chars, it might overwrite another entry
        const TQString shortLog = KStringHandler::csqueeze( logMessage );
        m_logMessages.append( logMessage );
        m_squeezedLogMessages.append( shortLog );
        oldMessages->insertItem( shortLog );
      }
    }

    m_encoding = config->readEntry( "CVSEncoding", "UTF-8" );
    m_encodingComboBox->insertItem( i18n( "Descriptive encoding name", "Last choice ( %1 )" ).arg( m_encoding ), 0);
  }
}

void CVSDialog::saveSettings( )
{
  TDESharedConfig * config = m_config;
  config->setGroup( "CVSSupport" );
  if ( _cmd == CVS::Commit ) {
    config->writeEntry( "AutoAddFiles", autoAddBox->isChecked( ) );

    // Write the log messages to the config file.
    int cnt = 0;
    TQStringList::const_iterator it;
    for ( it = m_logMessages.constBegin( ); it != m_logMessages.constEnd( ) && cnt < 10 ; ++it, ++cnt )
      config->writeEntry( TQString( "CommitLogMessage%1" ).arg( cnt ), *it );

    config->writeEntry( "CVSEncoding", m_encoding );
  }
  m_config->sync();
}

#include "cvsdialog.moc"