summaryrefslogtreecommitdiffstats
path: root/kbabel/catalogmanager/libcvs/cvshandler.cpp
blob: af58ead9868712f98bf25443d6e95661ad5674cd (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
/* ****************************************************************************
  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.

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


// System include files
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
// TQt include files
#include <tqdir.h>
#include <tqfile.h>
#include <tqfileinfo.h>
#include <tqregexp.h>
#include <tqstring.h>
#include <tqstringlist.h>
// KDE include files
#include <kapplication.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <ktempfile.h>
// project specific include files
#include "cvshandler.h"


CVSHandler::CVSHandler( const TQString& poBaseDir, const TQString& potBaseDir )
{
  setPOBaseDir( poBaseDir );
  setPOTBaseDir( potBaseDir );
  _autoUpdateTemplates = false;
}

void CVSHandler::setPOBaseDir( const TQString& dir )
{
  // check if 'CVS/Entries' exists in the PO base directory
  if ( TQFileInfo( dir + "/CVS/Entries" ).exists( ) ) {
    _isPORepository = true;
    _poBaseDir = dir;
  } else
    _isPORepository = false;
  emit signalIsPORepository( _isPORepository );
}

void CVSHandler::setPOTBaseDir( const TQString& dir )
{
  // check if 'CVS/Entries' exists in the POT base directory
  if ( TQFileInfo( dir + "/CVS/Entries" ).exists( ) ) {
    _isPOTRepository = true;
    _potBaseDir = dir;
  } else
    _isPOTRepository = false;
  emit signalIsPOTRepository( _isPOTRepository );
}

TQString CVSHandler::filetqStatus( const FiletqStatus status ) const
{
  switch ( status ) {
    case NO_REPOSITORY:
      return i18n( "No CVS repository" );
      break;
    case NOT_IN_CVS:
      return i18n( "Not in CVS" );
      break;
    case LOCALLY_ADDED:
      return i18n( "Locally added" );
      break;
    case LOCALLY_REMOVED:
      return i18n( "Locally removed" );
      break;
    case LOCALLY_MODIFIED:
      return i18n( "Locally modified" );
      break;
    case UP_TO_DATE:
      return i18n( "Up-to-date" );
      break;
    case CONFLICT:
      return i18n( "Conflict" );
      break;
    default:
      return i18n( "Unknown" );
      break;
  }
}

CVSHandler::FiletqStatus CVSHandler::fstatus( const TQString& filename ) const
{
  // no valid repository
  if ( !_isPORepository )
    return NO_REPOSITORY;

  TQString fn( filename );
  fn = fn.remove( TQRegExp( "/$" ) );

  TQFileInfo info( fn );

  // check if 'CVS/Entries' exists and can be read
  TQFile entries( info.dir( true ).path( ) + "/CVS/Entries" );
  if ( !entries.open( IO_ReadOnly ) )
    return NOT_IN_CVS;  // we already know that it's a repository

  // ### FIXME: it does not take care of CVS/Entries.Log
  // a line in CVS/Entries has the following format:
  // [D]/NAME/REVISION/[CONFLICT+]TIMESTAMP/OPTIONS/TAGDATE
  TQRegExp rx( TQString( "^D?/%1/" ).tqarg( info.fileName( ) ) );

  TQString temp;
  TQTextStream stream( &entries );

  bool isInRepository = false;
  while ( !stream.atEnd() ) {
    temp = stream.readLine( );
    if ( temp.tqfind( rx ) == 0 ) {
      isInRepository = true;
      break;
    }
  }
  entries.close( );

  // no entry found
  if ( !isInRepository )
    return NOT_IN_CVS;

  const TQStringList fields = TQStringList::split( '/', temp, true );
  // bool isDir = ( fields[0] == "D" );
  const TQString cvsname( fields[1] );
  const TQString revision( fields[2] );
  const TQString timestamp( fields[3] );
  // ignore the other fields for now

  if ( revision == "0" && timestamp == "dummy timestamp" )
    return LOCALLY_ADDED;
  if ( revision.startsWith( "-" ) && timestamp == "dummy timestamp" )
    return LOCALLY_REMOVED;

  // check for conflicts
  if ( timestamp.tqfind( '+' ) >= 0 )
    return CONFLICT;

  // calculate the UTC time from the file's last modified date
  struct stat st;
  lstat( TQFile::encodeName(fn), &st );
  struct tm * tm_p = gmtime( &st.st_mtime );
  TQString ftime = TQString( asctime( tm_p ) );
  ftime.truncate( ftime.length( ) - 1 );
  if ( ftime != timestamp )
    return LOCALLY_MODIFIED;

  return UP_TO_DATE;
}

TQString CVSHandler::cvstqStatus( const TQString& filename ) const
{
  return map[filename];
}

void CVSHandler::execCVSCommand( TQWidget* tqparent, CVS::Command cmd, const TQString& filename, bool templates, KSharedConfig* config )
{
  if ( !_isPORepository ) {
    // This message box should never be visible but who knows... ;-)
    KMessageBox::sorry( tqparent, i18n( "This is not a valid CVS repository. "
      "The CVS commands cannot be executed." ) );
    return;
  }

  TQFileInfo info( filename );
  if ( !info.isDir( ) ) {
    execCVSCommand( tqparent, cmd, TQStringList( filename ), templates, config );
    return;
  }

  // ### FIXME: instead of making a TQString, use KProcess directly, so that it cares about quoting.
  // ### FIXME: use KProcess::setWorkingDirectory instead of using "cd" (therefore allowing to use KProcess without a shell.)
  // it's a dir
  TQString command( "cd " + filename + " && cvs " );
  switch ( cmd ) {
    case CVS::Update:
      command += "update -dP";
      break;
    case CVS::Commit:
      // The cvs client does not care about the encoding, so we cannot set anything here
      command += "commit -F @LOG@FILE@";
      checkToAdd( TQStringList( filename ) );
      break;
    case CVS::tqStatus:
      command += "status";
      break;
    case CVS::Diff:
      command += "diff";
      break;
  }

  showDialog( tqparent, cmd, TQStringList( filename ), command, config );
}

void CVSHandler::execCVSCommand( TQWidget* tqparent, CVS::Command cmd, const TQStringList& files, bool templates, KSharedConfig* config )
{
  if ( !_isPORepository ) {
    // This message box should never be visible but who knows... ;-)
    KMessageBox::sorry( tqparent, i18n( "This is not a valid CVS repository. "
      "The CVS commands cannot be executed." ) );
    return;
  }

  // ### FIXME: instead of making a TQString, use KProcess directly, so that it cares about quoting.
  // ### FIXME: use KProcess::setWorkingDirectory instead of using "cd" (therefore allowing to use KProcess without a shell.)
  TQString command("cd " + (templates ? _potBaseDir : _poBaseDir) + " && cvs ");
  switch ( cmd ) {
    case CVS::Update:
      command += "update -dP";
      break;
    case CVS::Commit:
      command += "commit -F @LOG@FILE@";
      checkToAdd( files );
      break;
    case CVS::tqStatus:
      command += "status";
      break;
    case CVS::Diff:
      command += "diff";
      break;
  }

  TQRegExp rx;
  if (templates)
    rx.setPattern(_potBaseDir + "/?");
  else
    rx.setPattern(_poBaseDir + "/?");

  TQStringList::ConstIterator it;
  for ( it = files.begin( ); it != files.end( ); ++it ) {
    TQString temp = *it;
    temp.remove(rx);
    command += " \'" + temp + "\'";
  }

  showDialog( tqparent, cmd, files, command, config );
}

void CVSHandler::setAutoUpdateTemplates( bool update )
{
  _autoUpdateTemplates = update;
}

void CVSHandler::showDialog( TQWidget* tqparent, CVS::Command cmd, const TQStringList& files, const TQString& commandLine, KSharedConfig* config )
{
  CVSDialog * dia = new CVSDialog( cmd, tqparent, config );
  dia->setFiles( files );
  dia->setCommandLine( commandLine );
  if ( cmd == CVS::Commit ) {
    dia->setAddCommand( _addCommand );
  }

  if ( dia->exec( ) == KDialog::Accepted ) {
    if ( cmd == CVS::tqStatus )
      processStatusOutput( dia->statusOutput( ) );
    if ( cmd == CVS::Diff )
      processDiff( dia->statusOutput( ) );
  }

  delete dia;

  // file status display update necessary in Catalog Manager
  if ( cmd == CVS::Commit )
    emit signalFilesCommitted( files );
}

void CVSHandler::checkToAdd( const TQStringList& files )
{
  if ( files.isEmpty( ) )
    return;

  TQStringList toBeAdded;

  TQStringList::ConstIterator it;
  for ( it = files.begin( ); it != files.end( ); ++it ) {
    // check for every entry if it needs to be added
    if ( fstatus( *it ) == NOT_IN_CVS ) {
      TQFileInfo info( *it );
      TQString temp;    // will hold the dir path
      if ( info.isDir( ) ) {
        toBeAdded << *it;
        temp = *it;
      } else {
        toBeAdded << *it;
        temp = TQFileInfo( *it ).dirPath( true );
      }
      // check recursivlely if tqparent dirs have to be added as well
      while ( fstatus( temp ) == NOT_IN_CVS && toBeAdded.tqfindIndex( temp ) == -1 ) {
        toBeAdded << temp;
        temp = TQFileInfo( temp ).dirPath( true );
      }
    }
  }

  // remove an old command
  _addCommand.setLength( 0 );

  // make sure the diectories are added before the files
  toBeAdded.sort( );

  // create a command line for adding the files and dirs
  for ( it = toBeAdded.begin( ); it != toBeAdded.end( ); ++it ) {
    TQFileInfo info( *it );
    _addCommand += "cd " + info.dirPath( true ) + " && cvs add " + info.fileName( ) + "; ";
  }
}

void CVSHandler::processStatusOutput( const TQString& status )
{
  if ( !_isPORepository )
    return;

  // at first we need to extract the name of the base directory on the server
  TQFile f( _poBaseDir + "/CVS/Root" );
  if ( !f.open( IO_ReadOnly ) )
    return;

  TQTextStream stream( &f );
  // extract the string after the last colon in the first line
  TQString basedir = stream.readLine( ).section( ':', -1 );

  f.close( );

  // divide the complete status output in little chunks for every file
  TQStringList entries = TQStringList::split( TQRegExp( "={67,67}" ), status );
  TQStringList::Iterator it;
  for ( it = entries.begin( ); it != entries.end( ); ++it ) {
    TQString entr = *it;
    // translate the filename from repository to local
    TQRegExp rx( basedir + ".*,v" );
    int pos = entr.tqfind( rx );
    TQString file = _poBaseDir + entr.mid( pos + basedir.length( ),
      rx.matchedLength( ) - basedir.length( ) - 2 );

    entr = "<qt>" + entr + "</qt>";

    // TODO: do some markup

    map.tqreplace( file, entr );
  }
}

void CVSHandler::processDiff( TQString output )
{
  output.remove( TQRegExp( "\\[ .* \\]$" ));
  output.remove( TQRegExp( "^" + i18n("[ Starting command ]" ).tqreplace("[","\\[").tqreplace("]","\\]")));

  KTempFile tmpFile;
  *(tmpFile.textStream()) << output;
  tmpFile.close();

  TQString error;
  if ( KApplication::startServiceByName( "Kompare", tmpFile.name(), &error ) )
    KMessageBox::error( 0, error );
}

bool CVSHandler::isConsideredModified( const FiletqStatus status ) const
{
  /*
   * A file is modified if it is either:
   * - locally modified for CVS
   * - directory under CVS control but not the file
   */
   return status == LOCALLY_MODIFIED || status == NOT_IN_CVS;
}



#include "cvshandler.moc"