annotate kdiff3/src/fileaccess.cpp @ 53:32d5cbf9db71

Corrections for 0.9.81: - Fix for configure --enable-final - Bugfixes - First steps towards internationalisation
author joachim99
date Tue, 20 Jan 2004 20:19:59 +0000
parents c59d5a3a8ff3
children 8af4bb9d9a5a
rev   line source
joachim99@8 1 /***************************************************************************
joachim99@8 2 * Copyright (C) 2003 by Joachim Eibl *
joachim99@8 3 * joachim.eibl@gmx.de *
joachim99@8 4 * *
joachim99@8 5 * This program is free software; you can redistribute it and/or modify *
joachim99@8 6 * it under the terms of the GNU General Public License as published by *
joachim99@8 7 * the Free Software Foundation; either version 2 of the License, or *
joachim99@8 8 * (at your option) any later version. *
joachim99@8 9 ***************************************************************************/
joachim99@8 10
joachim99@8 11 #include "fileaccess.h"
joachim99@8 12 #include <iostream>
joachim99@8 13 #include <kio/global.h>
joachim99@8 14 #include <kmessagebox.h>
joachim99@8 15 #include "optiondialog.h"
joachim99@8 16 #include <qlayout.h>
joachim99@8 17 #include <qlabel.h>
joachim99@8 18 #include <qapplication.h>
joachim99@51 19 #include <qpushbutton.h>
joachim99@8 20 #if QT_VERSION==230
joachim99@8 21 #else
joachim99@8 22 #include <qeventloop.h>
joachim99@8 23 #endif
joachim99@8 24 #include "common.h"
joachim99@8 25 #include <ktempfile.h>
joachim99@8 26 #include <qdir.h>
joachim99@8 27 #include <qregexp.h>
joachim99@8 28 #include <qtextstream.h>
joachim99@8 29 #include <vector>
joachim99@8 30 #include <klocale.h>
joachim99@8 31
joachim99@8 32 #include <sys/types.h>
joachim99@8 33 #include <sys/stat.h>
joachim99@8 34
joachim99@8 35 #ifdef _WIN32
joachim99@8 36 #include <sys/utime.h>
joachim99@8 37 #include <io.h>
joachim99@8 38 #else
joachim99@8 39 #include <unistd.h> // Needed for creating symbolic links via symlink().
joachim99@8 40 #include <utime.h>
joachim99@8 41 #endif
joachim99@8 42
joachim99@8 43
joachim99@8 44 ProgressDialog* g_pProgressDialog;
joachim99@8 45
joachim99@8 46
joachim99@8 47 FileAccess::FileAccess( const QString& name, bool bWantToWrite )
joachim99@8 48 {
joachim99@8 49 setFile( name, bWantToWrite );
joachim99@8 50 }
joachim99@8 51
joachim99@8 52 FileAccess::FileAccess()
joachim99@8 53 {
joachim99@8 54 m_bValidData = false;
joachim99@8 55 m_size = 0;
joachim99@8 56 m_creationTime = QDateTime();
joachim99@8 57 m_accessTime = QDateTime();
joachim99@8 58 m_modificationTime = QDateTime();
joachim99@8 59 m_bReadable = false;
joachim99@8 60 m_bWritable = false;
joachim99@8 61 m_bExecutable = false;
joachim99@8 62 m_bLocal = false;
joachim99@8 63 m_bHidden = false;
joachim99@8 64 m_bExists = false;
joachim99@8 65 m_bFile = false;
joachim99@8 66 m_bDir = false;
joachim99@8 67 m_bSymLink = false;
joachim99@8 68 }
joachim99@8 69
joachim99@8 70 FileAccess::~FileAccess()
joachim99@8 71 {
joachim99@8 72 if( !m_localCopy.isEmpty() )
joachim99@8 73 {
joachim99@8 74 removeFile( m_localCopy );
joachim99@8 75 }
joachim99@8 76 }
joachim99@8 77
joachim99@8 78 void FileAccess::setFile( const QString& name, bool bWantToWrite )
joachim99@8 79 {
joachim99@8 80 m_url = KURL::fromPathOrURL( name );
joachim99@8 81 m_bValidData = false;
joachim99@8 82
joachim99@8 83 m_size = 0;
joachim99@8 84 m_creationTime = QDateTime();
joachim99@8 85 m_accessTime = QDateTime();
joachim99@8 86 m_modificationTime = QDateTime();
joachim99@8 87 m_bReadable = false;
joachim99@8 88 m_bWritable = false;
joachim99@8 89 m_bExecutable = false;
joachim99@8 90 m_bHidden = false;
joachim99@8 91 m_bExists = false;
joachim99@8 92 m_bFile = false;
joachim99@8 93 m_bDir = false;
joachim99@8 94 m_bSymLink = false;
joachim99@8 95 m_linkTarget = "";
joachim99@8 96 m_fileType = -1;
joachim99@8 97
joachim99@8 98 // Note: Checking if the filename-string is empty is necessary for Win95/98/ME.
joachim99@8 99 // The isFile() / isDir() queries would cause the program to crash.
joachim99@8 100 // (This is a Win95-bug which has been corrected only in WinNT/2000/XP.)
joachim99@8 101 if ( !name.isEmpty() )
joachim99@8 102 {
joachim99@51 103 // FileAccess tries to detect if the given name is an URL or a local file.
joachim99@51 104 // This is a problem if the filename looks like an URL (i.e. contains a colon ':').
joachim99@51 105 // e.g. "file:f.txt" is a valid filename.
joachim99@51 106 // Most of the time it is sufficient to check if the file exists locally.
joachim99@51 107 // 2 Problems remain:
joachim99@51 108 // 1. When the local file exists and the remote location is wanted nevertheless. (unlikely)
joachim99@51 109 // 2. When the local file doesn't exist and should be written to.
joachim99@51 110
joachim99@51 111 bool bExistsLocal = QDir().exists(name);
joachim99@51 112 if ( m_url.isLocalFile() || !m_url.isValid() || bExistsLocal ) // assuming that invalid means relative
joachim99@8 113 {
joachim99@8 114 QString localName = name;
joachim99@51 115 if ( !bExistsLocal && m_url.isLocalFile() && name.left(5).lower()=="file:" )
joachim99@8 116 {
joachim99@8 117 localName = m_url.path(); // I want the path without preceding "file:"
joachim99@8 118 }
joachim99@8 119 QFileInfo fi( localName );
joachim99@8 120 m_bReadable = fi.isReadable();
joachim99@8 121 m_bWritable = fi.isWritable();
joachim99@8 122 m_bExecutable = fi.isExecutable();
joachim99@8 123 #if QT_VERSION==230
joachim99@8 124 m_creationTime = fi.lastModified();
joachim99@8 125 m_bHidden = false;
joachim99@8 126 #else
joachim99@8 127 m_creationTime = fi.created();
joachim99@8 128 m_bHidden = fi.isHidden();
joachim99@8 129 #endif
joachim99@8 130 m_modificationTime = fi.lastModified();
joachim99@8 131 m_accessTime = fi.lastRead();
joachim99@8 132 m_size = fi.size();
joachim99@8 133 m_bSymLink = fi.isSymLink();
joachim99@8 134 m_bFile = fi.isFile();
joachim99@8 135 m_bDir = fi.isDir();
joachim99@8 136 m_bExists = fi.exists();
joachim99@8 137 m_name = fi.fileName();
joachim99@8 138 m_path = fi.filePath();
joachim99@8 139 m_absFilePath= fi.absFilePath();
joachim99@8 140 if ( m_bSymLink ) m_linkTarget = fi.readLink();
joachim99@8 141 m_bLocal = true;
joachim99@8 142 m_bValidData = true;
joachim99@8 143 if ( ! m_url.isValid() )
joachim99@8 144 {
joachim99@8 145 m_url.setPath( m_absFilePath );
joachim99@8 146 }
joachim99@8 147 }
joachim99@8 148 else
joachim99@8 149 {
joachim99@8 150 m_absFilePath = name;
joachim99@8 151 m_name = m_url.fileName();
joachim99@8 152 m_bLocal = false;
joachim99@8 153
joachim99@8 154 FileAccessJobHandler jh( this ); // A friend, which writes to the parameters of this class!
joachim99@8 155 jh.stat(2/*all details*/, bWantToWrite); // returns bSuccess, ignored
joachim99@8 156
joachim99@8 157 m_path = name;
joachim99@51 158 m_bValidData = true; // After running stat() the variables are initialised
joachim99@51 159 // and valid even if the file doesn't exist and the stat
joachim99@51 160 // query failed.
joachim99@8 161 }
joachim99@8 162 }
joachim99@8 163 }
joachim99@8 164
joachim99@8 165 void FileAccess::addPath( const QString& txt )
joachim99@8 166 {
joachim99@51 167 if ( m_url.isValid() )
joachim99@51 168 {
joachim99@51 169 m_url.addPath( txt );
joachim99@51 170 setFile( m_url.url() ); // reinitialise
joachim99@51 171 }
joachim99@51 172 else
joachim99@51 173 {
joachim99@51 174 QString slash = (txt.isEmpty() || txt[0]=='/') ? "" : "/";
joachim99@51 175 setFile( absFilePath() + slash + txt );
joachim99@51 176 }
joachim99@8 177 }
joachim99@8 178
joachim99@8 179 /* Filetype:
joachim99@8 180 S_IFMT 0170000 bitmask for the file type bitfields
joachim99@8 181 S_IFSOCK 0140000 socket
joachim99@8 182 S_IFLNK 0120000 symbolic link
joachim99@8 183 S_IFREG 0100000 regular file
joachim99@8 184 S_IFBLK 0060000 block device
joachim99@8 185 S_IFDIR 0040000 directory
joachim99@8 186 S_IFCHR 0020000 character device
joachim99@8 187 S_IFIFO 0010000 fifo
joachim99@8 188 S_ISUID 0004000 set UID bit
joachim99@8 189 S_ISGID 0002000 set GID bit (see below)
joachim99@8 190 S_ISVTX 0001000 sticky bit (see below)
joachim99@8 191
joachim99@8 192 Access:
joachim99@8 193 S_IRWXU 00700 mask for file owner permissions
joachim99@8 194 S_IRUSR 00400 owner has read permission
joachim99@8 195 S_IWUSR 00200 owner has write permission
joachim99@8 196 S_IXUSR 00100 owner has execute permission
joachim99@8 197 S_IRWXG 00070 mask for group permissions
joachim99@8 198 S_IRGRP 00040 group has read permission
joachim99@8 199 S_IWGRP 00020 group has write permission
joachim99@8 200 S_IXGRP 00010 group has execute permission
joachim99@8 201 S_IRWXO 00007 mask for permissions for others (not in group)
joachim99@8 202 S_IROTH 00004 others have read permission
joachim99@8 203 S_IWOTH 00002 others have write permisson
joachim99@8 204 S_IXOTH 00001 others have execute permission
joachim99@8 205 */
joachim99@8 206
joachim99@8 207 void FileAccess::setUdsEntry( const KIO::UDSEntry& e )
joachim99@8 208 {
joachim99@8 209 #ifndef KREPLACEMENTS_H
joachim99@8 210 KIO::UDSEntry::const_iterator ei;
joachim99@8 211 long acc = 0;
joachim99@8 212 long fileType = 0;
joachim99@8 213 for( ei=e.begin(); ei!=e.end(); ++ei )
joachim99@8 214 {
joachim99@8 215 const KIO::UDSAtom& a = *ei;
joachim99@8 216 switch( a.m_uds )
joachim99@8 217 {
joachim99@8 218 case KIO::UDS_SIZE : m_size = a.m_long; break;
joachim99@8 219 case KIO::UDS_USER : m_user = a.m_str; break;
joachim99@8 220 case KIO::UDS_GROUP : m_group = a.m_str; break;
joachim99@8 221 case KIO::UDS_NAME : m_path = a.m_str; break; // During listDir the relative path is given here.
joachim99@8 222 case KIO::UDS_MODIFICATION_TIME : m_modificationTime.setTime_t( a.m_long ); break;
joachim99@8 223 case KIO::UDS_ACCESS_TIME : m_accessTime.setTime_t( a.m_long ); break;
joachim99@8 224 case KIO::UDS_CREATION_TIME : m_creationTime.setTime_t( a.m_long ); break;
joachim99@8 225 case KIO::UDS_LINK_DEST : m_linkTarget = a.m_str; break;
joachim99@8 226 case KIO::UDS_ACCESS :
joachim99@8 227 {
joachim99@8 228 acc = a.m_long;
joachim99@8 229 m_bReadable = (acc & S_IRUSR)!=0;
joachim99@8 230 m_bWritable = (acc & S_IWUSR)!=0;
joachim99@8 231 m_bExecutable = (acc & S_IXUSR)!=0;
joachim99@8 232 break;
joachim99@8 233 }
joachim99@8 234 case KIO::UDS_FILE_TYPE :
joachim99@8 235 {
joachim99@8 236 fileType = a.m_long;
joachim99@8 237 m_bDir = ( fileType & S_IFMT ) == S_IFDIR;
joachim99@8 238 m_bFile = ( fileType & S_IFMT ) == S_IFREG;
joachim99@8 239 m_bSymLink = ( fileType & S_IFMT ) == S_IFLNK;
joachim99@8 240 m_bExists = fileType != 0;
joachim99@8 241 m_fileType = fileType;
joachim99@8 242 break;
joachim99@8 243 }
joachim99@8 244
joachim99@8 245 case KIO::UDS_URL : // m_url = KURL( a.str );
joachim99@8 246 break;
joachim99@8 247 case KIO::UDS_MIME_TYPE : break;
joachim99@8 248 case KIO::UDS_GUESSED_MIME_TYPE : break;
joachim99@8 249 case KIO::UDS_XML_PROPERTIES : break;
joachim99@8 250 default: break;
joachim99@8 251 }
joachim99@8 252 }
joachim99@8 253
joachim99@8 254 m_bExists = acc!=0 || fileType!=0;
joachim99@8 255
joachim99@8 256 m_bLocal = false;
joachim99@8 257 m_bValidData = true;
joachim99@8 258 m_bSymLink = !m_linkTarget.isEmpty();
joachim99@8 259 if ( m_name.isEmpty() )
joachim99@8 260 {
joachim99@8 261 int pos = m_path.findRev('/') + 1;
joachim99@8 262 m_name = m_path.mid( pos );
joachim99@8 263 }
joachim99@8 264 m_bHidden = m_name[0]=='.';
joachim99@8 265 #endif
joachim99@8 266 }
joachim99@8 267
joachim99@8 268
joachim99@8 269 bool FileAccess::isValid() const { return m_bValidData; }
joachim99@8 270 bool FileAccess::isFile() const { return m_bFile; }
joachim99@8 271 bool FileAccess::isDir() const { return m_bDir; }
joachim99@8 272 bool FileAccess::isSymLink() const { return m_bSymLink; }
joachim99@8 273 bool FileAccess::exists() const { return m_bExists; }
joachim99@8 274 long FileAccess::size() const { return m_size; }
joachim99@8 275 KURL FileAccess::url() const { return m_url; }
joachim99@8 276 bool FileAccess::isLocal() const { return m_bLocal; }
joachim99@8 277 bool FileAccess::isReadable() const { return m_bReadable; }
joachim99@8 278 bool FileAccess::isWritable() const { return m_bWritable; }
joachim99@8 279 bool FileAccess::isExecutable() const { return m_bExecutable; }
joachim99@8 280 bool FileAccess::isHidden() const { return m_bHidden; }
joachim99@8 281 QString FileAccess::readLink() const { return m_linkTarget; }
joachim99@8 282 QString FileAccess::absFilePath() const{ return m_absFilePath; } // Full abs path
joachim99@8 283 QString FileAccess::fileName() const { return m_name; } // Just the name-part of the path, without parent directories
joachim99@8 284 QString FileAccess::filePath() const { return m_path; } // The path-string that was used during construction
joachim99@8 285 QString FileAccess::prettyAbsPath() const { return isLocal() ? m_absFilePath : m_url.prettyURL(); }
joachim99@8 286
joachim99@8 287 QDateTime FileAccess::created() const
joachim99@8 288 {
joachim99@8 289 return ( m_creationTime.isValid() ? m_creationTime : m_modificationTime );
joachim99@8 290 }
joachim99@8 291
joachim99@8 292 QDateTime FileAccess::lastModified() const
joachim99@8 293 {
joachim99@8 294 return m_modificationTime;
joachim99@8 295 }
joachim99@8 296
joachim99@8 297 QDateTime FileAccess::lastRead() const
joachim99@8 298 {
joachim99@8 299 return ( m_accessTime.isValid() ? m_accessTime : m_modificationTime );
joachim99@8 300 }
joachim99@8 301
joachim99@8 302 static bool interruptableReadFile( QFile& f, void* pDestBuffer, unsigned long maxLength )
joachim99@8 303 {
joachim99@8 304 const unsigned long maxChunkSize = 100000;
joachim99@8 305 unsigned long i=0;
joachim99@8 306 while( i<maxLength )
joachim99@8 307 {
joachim99@8 308 unsigned long nextLength = min2( maxLength-i, maxChunkSize );
joachim99@8 309 unsigned long reallyRead = f.readBlock( (char*)pDestBuffer+i, nextLength );
joachim99@8 310 if ( reallyRead != nextLength )
joachim99@8 311 {
joachim99@8 312 return false;
joachim99@8 313 }
joachim99@8 314 i+=reallyRead;
joachim99@8 315
joachim99@8 316 g_pProgressDialog->setSubCurrent( double(i)/maxLength );
joachim99@8 317 if ( g_pProgressDialog->wasCancelled() ) return false;
joachim99@8 318 }
joachim99@8 319 return true;
joachim99@8 320 }
joachim99@8 321
joachim99@8 322 bool FileAccess::readFile( void* pDestBuffer, unsigned long maxLength )
joachim99@8 323 {
joachim99@8 324 if ( !m_localCopy.isEmpty() )
joachim99@8 325 {
joachim99@8 326 QFile f( m_localCopy );
joachim99@8 327 if ( f.open( IO_ReadOnly ) )
joachim99@8 328 return interruptableReadFile(f, pDestBuffer, maxLength);// maxLength == f.readBlock( (char*)pDestBuffer, maxLength );
joachim99@8 329 }
joachim99@8 330 else if (m_bLocal)
joachim99@8 331 {
joachim99@8 332 QFile f( filePath() );
joachim99@8 333
joachim99@8 334 if ( f.open( IO_ReadOnly ) )
joachim99@8 335 return interruptableReadFile(f, pDestBuffer, maxLength); //maxLength == f.readBlock( (char*)pDestBuffer, maxLength );
joachim99@8 336 }
joachim99@8 337 else
joachim99@8 338 {
joachim99@8 339 FileAccessJobHandler jh( this );
joachim99@8 340 return jh.get( pDestBuffer, maxLength );
joachim99@8 341 }
joachim99@8 342 return false;
joachim99@8 343 }
joachim99@8 344
joachim99@8 345 bool FileAccess::writeFile( void* pSrcBuffer, unsigned long length )
joachim99@8 346 {
joachim99@8 347 if (m_bLocal)
joachim99@8 348 {
joachim99@8 349 QFile f( filePath() );
joachim99@8 350 if ( f.open( IO_WriteOnly ) )
joachim99@8 351 {
joachim99@8 352 const unsigned long maxChunkSize = 100000;
joachim99@8 353 unsigned long i=0;
joachim99@8 354 while( i<length )
joachim99@8 355 {
joachim99@8 356 unsigned long nextLength = min2( length-i, maxChunkSize );
joachim99@8 357 unsigned long reallyWritten = f.writeBlock( (char*)pSrcBuffer+i, nextLength );
joachim99@8 358 if ( reallyWritten != nextLength )
joachim99@8 359 {
joachim99@8 360 return false;
joachim99@8 361 }
joachim99@8 362 i+=reallyWritten;
joachim99@8 363
joachim99@8 364 g_pProgressDialog->setSubCurrent( double(i)/length );
joachim99@8 365 if ( g_pProgressDialog->wasCancelled() ) return false;
joachim99@8 366 }
joachim99@8 367 return true;
joachim99@8 368 }
joachim99@8 369 }
joachim99@8 370 else
joachim99@8 371 {
joachim99@8 372 FileAccessJobHandler jh( this );
joachim99@8 373 return jh.put( pSrcBuffer, length, true /*overwrite*/ );
joachim99@8 374 }
joachim99@8 375 return false;
joachim99@8 376 }
joachim99@8 377
joachim99@8 378 bool FileAccess::copyFile( const QString& dest )
joachim99@8 379 {
joachim99@8 380 FileAccessJobHandler jh( this );
joachim99@8 381 return jh.copyFile( dest ); // Handles local and remote copying.
joachim99@8 382 }
joachim99@8 383
joachim99@8 384 bool FileAccess::rename( const QString& dest )
joachim99@8 385 {
joachim99@8 386 FileAccessJobHandler jh( this );
joachim99@8 387 return jh.rename( dest );
joachim99@8 388 }
joachim99@8 389
joachim99@8 390 bool FileAccess::removeFile()
joachim99@8 391 {
joachim99@8 392 if ( isLocal() )
joachim99@8 393 {
joachim99@8 394 return QDir().remove( absFilePath() );
joachim99@8 395 }
joachim99@8 396 else
joachim99@8 397 {
joachim99@8 398 FileAccessJobHandler jh( this );
joachim99@8 399 return jh.removeFile( absFilePath() );
joachim99@8 400 }
joachim99@8 401 }
joachim99@8 402
joachim99@8 403 bool FileAccess::removeFile( const QString& name ) // static
joachim99@8 404 {
joachim99@8 405 return FileAccess(name).removeFile();
joachim99@8 406 }
joachim99@8 407
joachim99@8 408 bool FileAccess::listDir( t_DirectoryList* pDirList, bool bRecursive, bool bFindHidden,
joachim99@8 409 const QString& filePattern, const QString& fileAntiPattern, const QString& dirAntiPattern,
joachim99@8 410 bool bFollowDirLinks, bool bUseCvsIgnore )
joachim99@8 411 {
joachim99@8 412 FileAccessJobHandler jh( this );
joachim99@8 413 return jh.listDir( pDirList, bRecursive, bFindHidden, filePattern, fileAntiPattern,
joachim99@8 414 dirAntiPattern, bFollowDirLinks, bUseCvsIgnore );
joachim99@8 415 }
joachim99@8 416
joachim99@8 417 QString FileAccess::tempFileName()
joachim99@8 418 {
joachim99@8 419 #ifdef KREPLACEMENTS_H
joachim99@8 420
joachim99@8 421 QString fileName;
joachim99@8 422 #ifdef _WIN32
joachim99@8 423 QString tmpDir = getenv("TEMP");
joachim99@8 424 for(int i=0; ;++i)
joachim99@8 425 {
joachim99@8 426 // short filenames for WIN98 because for system() the command must not exceed 120 characters.
joachim99@8 427 fileName = tmpDir + "/" + QString::number(i);
joachim99@8 428 if ( ! FileAccess::exists(fileName) )
joachim99@8 429 break;
joachim99@8 430 }
joachim99@8 431 #else
joachim99@8 432 QString tmpDir = "/tmp";
joachim99@8 433 for(int i=0; ;++i)
joachim99@8 434 {
joachim99@8 435 fileName = tmpDir + "/kdiff3_" + QString::number(i) +".tmp";
joachim99@8 436 if ( ! FileAccess::exists(fileName) )
joachim99@8 437 break;
joachim99@8 438 }
joachim99@8 439 #endif
joachim99@8 440
joachim99@8 441 return QDir::convertSeparators(fileName);
joachim99@8 442
joachim99@8 443 #else // using KDE
joachim99@8 444
joachim99@8 445 KTempFile tmpFile;
joachim99@8 446 tmpFile.setAutoDelete( true ); // We only want the name. Delete the precreated file immediately.
joachim99@8 447 return tmpFile.name();
joachim99@8 448
joachim99@8 449 #endif
joachim99@8 450 }
joachim99@8 451
joachim99@8 452 bool FileAccess::makeDir( const QString& dirName )
joachim99@8 453 {
joachim99@8 454 FileAccessJobHandler fh(0);
joachim99@8 455 return fh.mkDir( dirName );
joachim99@8 456 }
joachim99@8 457
joachim99@8 458 bool FileAccess::removeDir( const QString& dirName )
joachim99@8 459 {
joachim99@8 460 FileAccessJobHandler fh(0);
joachim99@8 461 return fh.rmDir( dirName );
joachim99@8 462 }
joachim99@8 463
joachim99@8 464 bool FileAccess::symLink( const QString& linkTarget, const QString& linkLocation )
joachim99@8 465 {
friseb123@19 466 #ifdef _WIN32
joachim99@24 467 return false;
friseb123@19 468 #else
joachim99@8 469 return 0==::symlink( linkTarget.ascii(), linkLocation.ascii() );
joachim99@8 470 //FileAccessJobHandler fh(0);
joachim99@8 471 //return fh.symLink( linkTarget, linkLocation );
friseb123@19 472 #endif
joachim99@8 473 }
joachim99@8 474
joachim99@8 475 bool FileAccess::exists( const QString& name )
joachim99@8 476 {
joachim99@8 477 FileAccess fa( name );
joachim99@8 478 return fa.exists();
joachim99@8 479 }
joachim99@8 480
joachim99@8 481 // If the size couldn't be determined by stat() then the file is copied to a local temp file.
joachim99@8 482 long FileAccess::sizeForReading()
joachim99@8 483 {
joachim99@8 484 if ( m_size == 0 && !isLocal() )
joachim99@8 485 {
joachim99@8 486 // Size couldn't be determined. Copy the file to a local temp place.
joachim99@8 487 QString localCopy = tempFileName();
joachim99@8 488 bool bSuccess = copyFile( localCopy );
joachim99@8 489 if ( bSuccess )
joachim99@8 490 {
joachim99@8 491 QFileInfo fi( localCopy );
joachim99@8 492 m_size = fi.size();
joachim99@8 493 m_localCopy = localCopy;
joachim99@8 494 return m_size;
joachim99@8 495 }
joachim99@8 496 else
joachim99@8 497 {
joachim99@8 498 return 0;
joachim99@8 499 }
joachim99@8 500 }
joachim99@8 501 else
joachim99@8 502 return m_size;
joachim99@8 503 }
joachim99@8 504
joachim99@8 505 QString FileAccess::getStatusText()
joachim99@8 506 {
joachim99@8 507 return m_statusText;
joachim99@8 508 }
joachim99@8 509
joachim99@8 510 QString FileAccess::cleanDirPath( const QString& path ) // static
joachim99@8 511 {
joachim99@8 512 KURL url(path);
joachim99@8 513 if ( url.isLocalFile() || ! url.isValid() )
joachim99@8 514 {
joachim99@8 515 return QDir().cleanDirPath( path );
joachim99@8 516 }
joachim99@8 517 else
joachim99@8 518 {
joachim99@8 519 return path;
joachim99@8 520 }
joachim99@8 521 }
joachim99@8 522
joachim99@8 523 bool FileAccess::createBackup( const QString& bakExtension )
joachim99@8 524 {
joachim99@8 525 if ( exists() )
joachim99@8 526 {
joachim99@8 527 // First rename the existing file to the bak-file. If a bak-file file exists, delete that.
joachim99@8 528 QString bakName = absFilePath() + bakExtension;
joachim99@8 529 FileAccess bakFile( bakName, true /*bWantToWrite*/ );
joachim99@8 530 if ( bakFile.exists() )
joachim99@8 531 {
joachim99@8 532 bool bSuccess = bakFile.removeFile();
joachim99@8 533 if ( !bSuccess )
joachim99@8 534 {
joachim99@8 535 m_statusText = i18n("While trying to make a backup, deleting an older backup failed. \nFilename: ") + bakName;
joachim99@8 536 return false;
joachim99@8 537 }
joachim99@8 538 }
joachim99@8 539 bool bSuccess = rename( bakName );
joachim99@8 540 if (!bSuccess)
joachim99@8 541 {
joachim99@8 542 m_statusText = i18n("While trying to make a backup, renaming failed. \nFilenames: ") +
joachim99@8 543 absFilePath() + " -> " + bakName;
joachim99@8 544 return false;
joachim99@8 545 }
joachim99@8 546 }
joachim99@8 547 return true;
joachim99@8 548 }
joachim99@8 549
joachim99@8 550 FileAccessJobHandler::FileAccessJobHandler( FileAccess* pFileAccess )
joachim99@8 551 {
joachim99@8 552 m_pFileAccess = pFileAccess;
joachim99@8 553 m_bSuccess = false;
joachim99@8 554 }
joachim99@8 555
joachim99@8 556 bool FileAccessJobHandler::stat( int detail, bool bWantToWrite )
joachim99@8 557 {
joachim99@8 558 m_bSuccess = false;
joachim99@8 559 m_pFileAccess->m_statusText = QString();
joachim99@8 560 KIO::StatJob* pStatJob = KIO::stat( m_pFileAccess->m_url, ! bWantToWrite, detail, false );
joachim99@8 561
joachim99@8 562 connect( pStatJob, SIGNAL(result(KIO::Job*)), this, SLOT(slotStatResult(KIO::Job*)));
joachim99@8 563
joachim99@51 564 g_pProgressDialog->enterEventLoop( pStatJob, i18n("Getting file status: %1").arg(m_pFileAccess->prettyAbsPath()) );
joachim99@8 565
joachim99@8 566 return m_bSuccess;
joachim99@8 567 }
joachim99@8 568
joachim99@8 569 void FileAccessJobHandler::slotStatResult(KIO::Job* pJob)
joachim99@8 570 {
joachim99@8 571 if ( pJob->error() )
joachim99@8 572 {
joachim99@8 573 //pJob->showErrorDialog(g_pProgressDialog);
joachim99@8 574 m_pFileAccess->m_bExists = false;
joachim99@8 575 m_bSuccess = true;
joachim99@8 576 }
joachim99@8 577 else
joachim99@8 578 {
joachim99@8 579 m_bSuccess = true;
joachim99@8 580
joachim99@8 581 m_pFileAccess->m_bValidData = true;
joachim99@8 582 const KIO::UDSEntry e = static_cast<KIO::StatJob*>(pJob)->statResult();
joachim99@8 583
joachim99@8 584 m_pFileAccess->setUdsEntry( e );
joachim99@8 585 }
joachim99@8 586
joachim99@8 587 g_pProgressDialog->exitEventLoop();
joachim99@8 588 }
joachim99@8 589
joachim99@8 590
joachim99@8 591 bool FileAccessJobHandler::get(void* pDestBuffer, long maxLength )
joachim99@8 592 {
joachim99@8 593 if ( maxLength>0 )
joachim99@8 594 {
joachim99@8 595 KIO::TransferJob* pJob = KIO::get( m_pFileAccess->m_url, false /*reload*/, false );
joachim99@8 596 m_transferredBytes = 0;
joachim99@8 597 m_pTransferBuffer = (char*)pDestBuffer;
joachim99@8 598 m_maxLength = maxLength;
joachim99@8 599 m_bSuccess = false;
joachim99@8 600 m_pFileAccess->m_statusText = QString();
joachim99@8 601
joachim99@8 602 connect( pJob, SIGNAL(result(KIO::Job*)), this, SLOT(slotSimpleJobResult(KIO::Job*)));
joachim99@8 603 connect( pJob, SIGNAL(data(KIO::Job*,const QByteArray &)), this, SLOT(slotGetData(KIO::Job*, const QByteArray&)));
joachim99@8 604 connect( pJob, SIGNAL(percent(KIO::Job*,unsigned long)), this, SLOT(slotPercent(KIO::Job*, unsigned long)));
joachim99@8 605
joachim99@51 606 g_pProgressDialog->enterEventLoop( pJob, i18n("Reading file: %1").arg(m_pFileAccess->prettyAbsPath()) );
joachim99@8 607 return m_bSuccess;
joachim99@8 608 }
joachim99@8 609 else
joachim99@8 610 return true;
joachim99@8 611 }
joachim99@8 612
joachim99@8 613 void FileAccessJobHandler::slotGetData( KIO::Job* pJob, const QByteArray& newData )
joachim99@8 614 {
joachim99@8 615 if ( pJob->error() )
joachim99@8 616 {
joachim99@8 617 pJob->showErrorDialog(g_pProgressDialog);
joachim99@8 618 }
joachim99@8 619 else
joachim99@8 620 {
joachim99@8 621 long length = min2( long(newData.size()), m_maxLength - m_transferredBytes );
joachim99@8 622 ::memcpy( m_pTransferBuffer + m_transferredBytes, newData.data(), newData.size() );
joachim99@8 623 m_transferredBytes += length;
joachim99@8 624 }
joachim99@8 625 }
joachim99@8 626
joachim99@8 627 bool FileAccessJobHandler::put(void* pSrcBuffer, long maxLength, bool bOverwrite, bool bResume, int permissions )
joachim99@8 628 {
joachim99@8 629 if ( maxLength>0 )
joachim99@8 630 {
joachim99@8 631 KIO::TransferJob* pJob = KIO::put( m_pFileAccess->m_url, permissions, bOverwrite, bResume, false );
joachim99@8 632 m_transferredBytes = 0;
joachim99@8 633 m_pTransferBuffer = (char*)pSrcBuffer;
joachim99@8 634 m_maxLength = maxLength;
joachim99@8 635 m_bSuccess = false;
joachim99@8 636 m_pFileAccess->m_statusText = QString();
joachim99@8 637
joachim99@8 638 connect( pJob, SIGNAL(result(KIO::Job*)), this, SLOT(slotPutJobResult(KIO::Job*)));
joachim99@8 639 connect( pJob, SIGNAL(dataReq(KIO::Job*, QByteArray&)), this, SLOT(slotPutData(KIO::Job*, QByteArray&)));
joachim99@8 640 connect( pJob, SIGNAL(percent(KIO::Job*,unsigned long)), this, SLOT(slotPercent(KIO::Job*, unsigned long)));
joachim99@8 641
joachim99@51 642 g_pProgressDialog->enterEventLoop( pJob, i18n("Writing file: %1").arg(m_pFileAccess->prettyAbsPath()) );
joachim99@8 643 return m_bSuccess;
joachim99@8 644 }
joachim99@8 645 else
joachim99@8 646 return true;
joachim99@8 647 }
joachim99@8 648
joachim99@8 649 void FileAccessJobHandler::slotPutData( KIO::Job* pJob, QByteArray& data )
joachim99@8 650 {
joachim99@8 651 if ( pJob->error() )
joachim99@8 652 {
joachim99@8 653 pJob->showErrorDialog(g_pProgressDialog);
joachim99@8 654 }
joachim99@8 655 else
joachim99@8 656 {
joachim99@8 657 long maxChunkSize = 100000;
joachim99@8 658 long length = min2( maxChunkSize, m_maxLength - m_transferredBytes );
joachim99@8 659 bool bSuccess = data.resize( length );
joachim99@8 660 if ( bSuccess )
joachim99@8 661 {
joachim99@8 662 if ( length>0 )
joachim99@8 663 {
joachim99@8 664 ::memcpy( data.data(), m_pTransferBuffer + m_transferredBytes, data.size() );
joachim99@8 665 m_transferredBytes += length;
joachim99@8 666 }
joachim99@8 667 }
joachim99@8 668 else
joachim99@8 669 {
joachim99@8 670 KMessageBox::error( g_pProgressDialog, i18n("Out of memory") );
joachim99@8 671 data.resize(0);
joachim99@8 672 m_bSuccess = false;
joachim99@8 673 }
joachim99@8 674 }
joachim99@8 675 }
joachim99@8 676
joachim99@8 677 void FileAccessJobHandler::slotPutJobResult(KIO::Job* pJob)
joachim99@8 678 {
joachim99@8 679 if ( pJob->error() )
joachim99@8 680 {
joachim99@8 681 pJob->showErrorDialog(g_pProgressDialog);
joachim99@8 682 }
joachim99@8 683 else
joachim99@8 684 {
joachim99@8 685 m_bSuccess = (m_transferredBytes == m_maxLength); // Special success condition
joachim99@8 686 }
joachim99@8 687 g_pProgressDialog->exitEventLoop(); // Close the dialog, return from exec()
joachim99@8 688 }
joachim99@8 689
joachim99@8 690 bool FileAccessJobHandler::mkDir( const QString& dirName )
joachim99@8 691 {
joachim99@8 692 KURL dirURL = KURL::fromPathOrURL( dirName );
joachim99@8 693 if ( dirName.isEmpty() )
joachim99@8 694 return false;
joachim99@8 695 else if ( dirURL.isLocalFile() )
joachim99@8 696 {
joachim99@8 697 return QDir().mkdir( dirURL.path() );
joachim99@8 698 }
joachim99@8 699 else
joachim99@8 700 {
joachim99@8 701 m_bSuccess = false;
joachim99@8 702 KIO::SimpleJob* pJob = KIO::mkdir( dirURL );
joachim99@8 703 connect( pJob, SIGNAL(result(KIO::Job*)), this, SLOT(slotSimpleJobResult(KIO::Job*)));
joachim99@8 704
joachim99@51 705 g_pProgressDialog->enterEventLoop( pJob, i18n("Making directory: %1").arg(dirName) );
joachim99@8 706 return m_bSuccess;
joachim99@8 707 }
joachim99@8 708 }
joachim99@8 709
joachim99@8 710 bool FileAccessJobHandler::rmDir( const QString& dirName )
joachim99@8 711 {
joachim99@8 712 KURL dirURL = KURL::fromPathOrURL( dirName );
joachim99@8 713 if ( dirName.isEmpty() )
joachim99@8 714 return false;
joachim99@8 715 else if ( dirURL.isLocalFile() )
joachim99@8 716 {
joachim99@8 717 return QDir().rmdir( dirURL.path() );
joachim99@8 718 }
joachim99@8 719 else
joachim99@8 720 {
joachim99@8 721 m_bSuccess = false;
joachim99@8 722 KIO::SimpleJob* pJob = KIO::rmdir( dirURL );
joachim99@8 723 connect( pJob, SIGNAL(result(KIO::Job*)), this, SLOT(slotSimpleJobResult(KIO::Job*)));
joachim99@8 724
joachim99@51 725 g_pProgressDialog->enterEventLoop(pJob, i18n("Removing directory: %1").arg(dirName));
joachim99@8 726 return m_bSuccess;
joachim99@8 727 }
joachim99@8 728 }
joachim99@8 729
joachim99@8 730 bool FileAccessJobHandler::removeFile( const QString& fileName )
joachim99@8 731 {
joachim99@8 732 if ( fileName.isEmpty() )
joachim99@8 733 return false;
joachim99@8 734 else
joachim99@8 735 {
joachim99@8 736 m_bSuccess = false;
joachim99@8 737 KIO::SimpleJob* pJob = KIO::file_delete( fileName, false );
joachim99@8 738 connect( pJob, SIGNAL(result(KIO::Job*)), this, SLOT(slotSimpleJobResult(KIO::Job*)));
joachim99@8 739
joachim99@51 740 g_pProgressDialog->enterEventLoop( pJob, i18n("Removing file: %1").arg(fileName) );
joachim99@8 741 return m_bSuccess;
joachim99@8 742 }
joachim99@8 743 }
joachim99@8 744
joachim99@8 745 bool FileAccessJobHandler::symLink( const QString& linkTarget, const QString& linkLocation )
joachim99@8 746 {
joachim99@8 747 if ( linkTarget.isEmpty() || linkLocation.isEmpty() )
joachim99@8 748 return false;
joachim99@8 749 else
joachim99@8 750 {
joachim99@8 751 m_bSuccess = false;
joachim99@8 752 KIO::CopyJob* pJob = KIO::link( linkTarget, linkLocation, false );
joachim99@8 753 connect( pJob, SIGNAL(result(KIO::Job*)), this, SLOT(slotSimpleJobResult(KIO::Job*)));
joachim99@8 754
joachim99@51 755 g_pProgressDialog->enterEventLoop( pJob,
joachim99@51 756 i18n("Creating symbolic link: %1 -> %2").arg(linkLocation).arg(linkTarget) );
joachim99@8 757 return m_bSuccess;
joachim99@8 758 }
joachim99@8 759 }
joachim99@8 760
joachim99@8 761 bool FileAccessJobHandler::rename( const QString& dest )
joachim99@8 762 {
joachim99@8 763 KURL kurl = KURL::fromPathOrURL( dest );
joachim99@8 764 if ( dest.isEmpty() )
joachim99@8 765 return false;
joachim99@8 766 else if ( m_pFileAccess->isLocal() && kurl.isLocalFile() )
joachim99@8 767 {
joachim99@8 768 return QDir().rename( m_pFileAccess->absFilePath(), kurl.path() );
joachim99@8 769 }
joachim99@8 770 else
joachim99@8 771 {
joachim99@8 772 bool bOverwrite = false;
joachim99@8 773 bool bResume = false;
joachim99@8 774 bool bShowProgress = false;
joachim99@8 775 int permissions=-1;
joachim99@8 776 m_bSuccess = false;
joachim99@8 777 KIO::FileCopyJob* pJob = KIO::file_move( m_pFileAccess->m_url, kurl.url(), permissions, bOverwrite, bResume, bShowProgress );
joachim99@8 778 connect( pJob, SIGNAL(result(KIO::Job*)), this, SLOT(slotSimpleJobResult(KIO::Job*)));
joachim99@8 779 connect( pJob, SIGNAL(percent(KIO::Job*,unsigned long)), this, SLOT(slotPercent(KIO::Job*, unsigned long)));
joachim99@8 780
joachim99@51 781 g_pProgressDialog->enterEventLoop( pJob,
joachim99@51 782 i18n("Renaming file: %1 -> %2").arg(m_pFileAccess->prettyAbsPath()).arg(dest) );
joachim99@8 783 return m_bSuccess;
joachim99@8 784 }
joachim99@8 785 }
joachim99@8 786
joachim99@8 787 void FileAccessJobHandler::slotSimpleJobResult(KIO::Job* pJob)
joachim99@8 788 {
joachim99@8 789 if ( pJob->error() )
joachim99@8 790 {
joachim99@8 791 pJob->showErrorDialog(g_pProgressDialog);
joachim99@8 792 }
joachim99@8 793 else
joachim99@8 794 {
joachim99@8 795 m_bSuccess = true;
joachim99@8 796 }
joachim99@8 797 g_pProgressDialog->exitEventLoop(); // Close the dialog, return from exec()
joachim99@8 798 }
joachim99@8 799
joachim99@8 800
joachim99@8 801 // Copy local or remote files.
joachim99@8 802 bool FileAccessJobHandler::copyFile( const QString& dest )
joachim99@8 803 {
joachim99@8 804 KURL destUrl = KURL::fromPathOrURL( dest );
joachim99@8 805 m_pFileAccess->m_statusText = QString();
joachim99@8 806 if ( ! m_pFileAccess->isLocal() || ! destUrl.isLocalFile() ) // if either url is nonlocal
joachim99@8 807 {
joachim99@8 808 bool bOverwrite = false;
joachim99@8 809 bool bResume = false;
joachim99@8 810 bool bShowProgress = false;
joachim99@8 811 int permissions = (m_pFileAccess->isExecutable()?0111:0)+(m_pFileAccess->isWritable()?0222:0)+(m_pFileAccess->isReadable()?0444:0);
joachim99@8 812 m_bSuccess = false;
joachim99@8 813 KIO::FileCopyJob* pJob = KIO::file_copy ( m_pFileAccess->m_url, destUrl.url(), permissions, bOverwrite, bResume, bShowProgress );
joachim99@8 814 connect( pJob, SIGNAL(result(KIO::Job*)), this, SLOT(slotSimpleJobResult(KIO::Job*)));
joachim99@8 815 connect( pJob, SIGNAL(percent(KIO::Job*,unsigned long)), this, SLOT(slotPercent(KIO::Job*, unsigned long)));
joachim99@51 816 g_pProgressDialog->enterEventLoop( pJob,
joachim99@51 817 i18n("Copying file: %1 -> %2").arg(m_pFileAccess->prettyAbsPath()).arg(dest) );
joachim99@8 818
joachim99@8 819 return m_bSuccess;
joachim99@8 820 // Note that the KIO-slave preserves the original date, if this is supported.
joachim99@8 821 }
joachim99@8 822
joachim99@8 823 // Both files are local:
joachim99@8 824 QString srcName = m_pFileAccess->absFilePath();
joachim99@8 825 QString destName = dest;
joachim99@8 826 QFile srcFile( srcName );
joachim99@8 827 QFile destFile( destName );
joachim99@8 828 bool bReadSuccess = srcFile.open( IO_ReadOnly );
joachim99@8 829 if ( bReadSuccess == false )
joachim99@8 830 {
joachim99@51 831 m_pFileAccess->m_statusText = i18n("Error during file copy operation: Opening file for reading failed. Filename: %1").arg(srcName);
joachim99@8 832 return false;
joachim99@8 833 }
joachim99@8 834 bool bWriteSuccess = destFile.open( IO_WriteOnly );
joachim99@8 835 if ( bWriteSuccess == false )
joachim99@8 836 {
joachim99@51 837 m_pFileAccess->m_statusText = i18n("Error during file copy operation: Opening file for writing failed. Filename: %1").arg(destName);
joachim99@8 838 return false;
joachim99@8 839 }
joachim99@8 840
joachim99@8 841 #if QT_VERSION==230
joachim99@8 842 typedef long Q_LONG;
joachim99@8 843 #endif
joachim99@8 844 std::vector<char> buffer(100000);
joachim99@8 845 Q_LONG bufSize = buffer.size();
joachim99@8 846 Q_LONG srcSize = srcFile.size();
joachim99@53 847 while ( srcSize > 0 && !g_pProgressDialog->wasCancelled() )
joachim99@8 848 {
joachim99@8 849 Q_LONG readSize = srcFile.readBlock( &buffer[0], min2( srcSize, bufSize ) );
joachim99@53 850 if ( readSize==-1 || readSize==0 )
joachim99@8 851 {
joachim99@51 852 m_pFileAccess->m_statusText = i18n("Error during file copy operation: Reading failed. Filename: %1").arg(srcName);
joachim99@8 853 return false;
joachim99@8 854 }
joachim99@8 855 srcSize -= readSize;
joachim99@8 856 while ( readSize > 0 )
joachim99@8 857 {
joachim99@8 858 Q_LONG writeSize = destFile.writeBlock( &buffer[0], readSize );
joachim99@53 859 if ( writeSize==-1 || writeSize==0 )
joachim99@8 860 {
joachim99@51 861 m_pFileAccess->m_statusText = i18n("Error during file copy operation: Writing failed. Filename: %1").arg(destName);
joachim99@8 862 return false;
joachim99@8 863 }
joachim99@8 864 readSize -= writeSize;
joachim99@8 865 }
joachim99@8 866 destFile.flush();
joachim99@53 867 g_pProgressDialog->setSubCurrent( (double)(srcFile.size()-srcSize)/srcFile.size(), false );
joachim99@8 868 }
joachim99@8 869 srcFile.close();
joachim99@8 870 destFile.close();
joachim99@8 871
joachim99@8 872 // Update the times of the destFile
joachim99@8 873 #ifdef _WIN32
joachim99@8 874 struct _stat srcFileStatus;
joachim99@8 875 int statResult = ::_stat( srcName.ascii(), &srcFileStatus );
joachim99@8 876 if (statResult==0)
joachim99@8 877 {
joachim99@8 878 _utimbuf destTimes;
joachim99@8 879 destTimes.actime = srcFileStatus.st_atime;/* time of last access */
joachim99@8 880 destTimes.modtime = srcFileStatus.st_mtime;/* time of last modification */
joachim99@8 881
joachim99@8 882 _utime ( destName.ascii(), &destTimes );
joachim99@8 883 _chmod ( destName.ascii(), srcFileStatus.st_mode );
joachim99@8 884 }
joachim99@8 885 #else
joachim99@8 886 struct stat srcFileStatus;
joachim99@8 887 int statResult = ::stat( srcName.ascii(), &srcFileStatus );
joachim99@8 888 if (statResult==0)
joachim99@8 889 {
joachim99@8 890 utimbuf destTimes;
joachim99@8 891 destTimes.actime = srcFileStatus.st_atime;/* time of last access */
joachim99@8 892 destTimes.modtime = srcFileStatus.st_mtime;/* time of last modification */
joachim99@8 893
joachim99@8 894 utime ( destName.ascii(), &destTimes );
joachim99@8 895 chmod ( destName.ascii(), srcFileStatus.st_mode );
joachim99@8 896 }
joachim99@8 897 #endif
joachim99@8 898 return true;
joachim99@8 899 }
joachim99@8 900
joachim99@8 901 static bool wildcardMultiMatch( const QString& wildcard, const QString& testString, bool bCaseSensitive )
joachim99@8 902 {
joachim99@8 903 QStringList sl = QStringList::split( ";", wildcard );
joachim99@8 904
joachim99@8 905 for ( QStringList::Iterator it = sl.begin(); it != sl.end(); ++it )
joachim99@8 906 {
joachim99@8 907 QRegExp pattern( *it, bCaseSensitive, true /*wildcard mode*/);
joachim99@8 908 #if QT_VERSION==230
joachim99@8 909 int len=0;
joachim99@8 910 if ( pattern.match( testString, 0, &len )!=-1 && len==testString.length())
joachim99@8 911 return true;
joachim99@8 912 #else
joachim99@8 913 if ( pattern.exactMatch( testString ) )
joachim99@8 914 return true;
joachim99@8 915 #endif
joachim99@8 916 }
joachim99@8 917
joachim99@8 918 return false;
joachim99@8 919 }
joachim99@8 920
joachim99@8 921
joachim99@8 922 // class CvsIgnoreList from Cervisia cvsdir.cpp
joachim99@8 923 // Copyright (C) 1999-2002 Bernd Gehrmann <bernd@mail.berlios.de>
joachim99@8 924 // with elements from class StringMatcher
joachim99@8 925 // Copyright (c) 2003 André Wöbbeking <Woebbeking@web.de>
joachim99@8 926 // Modifications for KDiff3 by Joachim Eibl
joachim99@8 927 class CvsIgnoreList
joachim99@8 928 {
joachim99@8 929 public:
joachim99@8 930 CvsIgnoreList(){}
joachim99@8 931 void init(FileAccess& dir, bool bUseLocalCvsIgnore );
joachim99@8 932 bool matches(const QString& fileName) const;
joachim99@8 933
joachim99@8 934 private:
joachim99@8 935 void addEntriesFromString(const QString& str);
joachim99@8 936 void addEntriesFromFile(const QString& name);
joachim99@8 937 void addEntry(const QString& entry);
joachim99@8 938
joachim99@8 939 QStringList m_exactPatterns;
joachim99@8 940 QStringList m_startPatterns;
joachim99@8 941 QStringList m_endPatterns;
joachim99@8 942 QStringList m_generalPatterns;
joachim99@8 943 };
joachim99@8 944
joachim99@8 945
joachim99@8 946 void CvsIgnoreList::init( FileAccess& dir, bool bUseLocalCvsIgnore )
joachim99@8 947 {
joachim99@8 948 static const char *ignorestr = ". .. core RCSLOG tags TAGS RCS SCCS .make.state "
joachim99@8 949 ".nse_depinfo #* .#* cvslog.* ,* CVS CVS.adm .del-* *.a *.olb *.o *.obj "
joachim99@8 950 "*.so *.Z *~ *.old *.elc *.ln *.bak *.BAK *.orig *.rej *.exe _$* *$";
joachim99@8 951
joachim99@8 952 addEntriesFromString(QString::fromLatin1(ignorestr));
joachim99@8 953 addEntriesFromFile(QDir::homeDirPath() + "/.cvsignore");
joachim99@8 954 addEntriesFromString(QString::fromLocal8Bit(::getenv("CVSIGNORE")));
joachim99@8 955
joachim99@8 956 if (bUseLocalCvsIgnore)
joachim99@8 957 {
joachim99@8 958 FileAccess file(dir);
joachim99@8 959 file.addPath( ".cvsignore" );
joachim99@8 960 int size = file.exists() ? file.sizeForReading() : 0;
joachim99@8 961 if ( size>0 )
joachim99@8 962 {
joachim99@8 963 char* buf=new char[size];
joachim99@8 964 if (buf!=0)
joachim99@8 965 {
joachim99@8 966 file.readFile( buf, size );
joachim99@8 967 int pos1 = 0;
joachim99@8 968 for ( int pos = 0; pos<=size; ++pos )
joachim99@8 969 {
joachim99@8 970 if( pos==size || buf[pos]==' ' || buf[pos]=='\t' || buf[pos]=='\n' || buf[pos]=='\r' )
joachim99@8 971 {
joachim99@8 972 if (pos>pos1)
joachim99@8 973 {
joachim99@8 974 QCString entry( &buf[pos1], pos-pos1+1 );
joachim99@8 975 addEntry( entry );
joachim99@8 976 }
joachim99@8 977 pos1=pos+1;
joachim99@8 978 }
joachim99@8 979 }
joachim99@8 980 delete buf;
joachim99@8 981 }
joachim99@8 982 }
joachim99@8 983 }
joachim99@8 984 }
joachim99@8 985
joachim99@8 986
joachim99@8 987 void CvsIgnoreList::addEntriesFromString(const QString& str)
joachim99@8 988 {
joachim99@8 989 int posLast(0);
joachim99@8 990 int pos;
joachim99@8 991 while ((pos = str.find(' ', posLast)) >= 0)
joachim99@8 992 {
joachim99@8 993 if (pos > posLast)
joachim99@8 994 addEntry(str.mid(posLast, pos - posLast));
joachim99@8 995 posLast = pos + 1;
joachim99@8 996 }
joachim99@8 997
joachim99@8 998 if (posLast < static_cast<int>(str.length()))
joachim99@8 999 addEntry(str.mid(posLast));
joachim99@8 1000 }
joachim99@8 1001
joachim99@8 1002
joachim99@8 1003 void CvsIgnoreList::addEntriesFromFile(const QString &name)
joachim99@8 1004 {
joachim99@8 1005 QFile file(name);
joachim99@8 1006
joachim99@8 1007 if( file.open(IO_ReadOnly) )
joachim99@8 1008 {
joachim99@8 1009 QTextStream stream(&file);
joachim99@8 1010 while( !stream.eof() )
joachim99@8 1011 {
joachim99@8 1012 addEntriesFromString(stream.readLine());
joachim99@8 1013 }
joachim99@8 1014 }
joachim99@8 1015 }
joachim99@8 1016
joachim99@8 1017 void CvsIgnoreList::addEntry(const QString& pattern)
joachim99@8 1018 {
joachim99@8 1019 if (pattern != QChar('!'))
joachim99@8 1020 {
joachim99@8 1021 if (pattern.isEmpty()) return;
joachim99@8 1022
joachim99@8 1023 // The general match is general but slow.
joachim99@8 1024 // Special tests for '*' and '?' at the beginning or end of a pattern
joachim99@8 1025 // allow fast checks.
joachim99@8 1026
joachim99@8 1027 // Count number of '*' and '?'
joachim99@8 1028 unsigned int nofMetaCharacters = 0;
joachim99@8 1029
joachim99@8 1030 const QChar* pos;
joachim99@8 1031 pos = pattern.unicode();
joachim99@8 1032 const QChar* posEnd;
joachim99@8 1033 posEnd=pos + pattern.length();
joachim99@8 1034 while (pos < posEnd)
joachim99@8 1035 {
joachim99@8 1036 if( *pos==QChar('*') || *pos==QChar('?') ) ++nofMetaCharacters;
joachim99@8 1037 ++pos;
joachim99@8 1038 }
joachim99@8 1039
joachim99@8 1040 if ( nofMetaCharacters==0 )
joachim99@8 1041 {
joachim99@8 1042 m_exactPatterns.append(pattern);
joachim99@8 1043 }
joachim99@8 1044 else if ( nofMetaCharacters==1 )
joachim99@8 1045 {
joachim99@8 1046 if ( pattern.constref(0) == QChar('*') )
joachim99@8 1047 {
joachim99@8 1048 m_endPatterns.append( pattern.right( pattern.length() - 1) );
joachim99@8 1049 }
joachim99@8 1050 else if (pattern.constref(pattern.length() - 1) == QChar('*'))
joachim99@8 1051 {
joachim99@8 1052 m_startPatterns.append( pattern.left( pattern.length() - 1) );
joachim99@8 1053 }
joachim99@8 1054 else
joachim99@8 1055 {
joachim99@8 1056 m_generalPatterns.append(pattern.local8Bit());
joachim99@8 1057 }
joachim99@8 1058 }
joachim99@8 1059 else
joachim99@8 1060 {
joachim99@8 1061 m_generalPatterns.append(pattern.local8Bit());
joachim99@8 1062 }
joachim99@8 1063 }
joachim99@8 1064 else
joachim99@8 1065 {
joachim99@8 1066 m_exactPatterns.clear();
joachim99@8 1067 m_startPatterns.clear();
joachim99@8 1068 m_endPatterns.clear();
joachim99@8 1069 m_generalPatterns.clear();
joachim99@8 1070 }
joachim99@8 1071 }
joachim99@8 1072
joachim99@8 1073 bool CvsIgnoreList::matches(const QString& text) const
joachim99@8 1074 {
joachim99@8 1075 if (m_exactPatterns.find(text) != m_exactPatterns.end())
joachim99@8 1076 {
joachim99@8 1077 return true;
joachim99@8 1078 }
joachim99@8 1079
joachim99@8 1080 QStringList::ConstIterator it;
joachim99@8 1081 QStringList::ConstIterator itEnd;
joachim99@8 1082 for ( it=m_startPatterns.begin(), itEnd=m_startPatterns.end(); it != itEnd; ++it)
joachim99@8 1083 {
joachim99@8 1084 if (text.startsWith(*it))
joachim99@8 1085 {
joachim99@8 1086 return true;
joachim99@8 1087 }
joachim99@8 1088 }
joachim99@8 1089
joachim99@8 1090 for ( it = m_endPatterns.begin(), itEnd=m_endPatterns.end(); it != itEnd; ++it)
joachim99@8 1091 {
joachim99@8 1092 if (text.mid( text.length() - (*it).length() )==*it) //(text.endsWith(*it))
joachim99@8 1093 {
joachim99@8 1094 return true;
joachim99@8 1095 }
joachim99@8 1096 }
joachim99@8 1097
joachim99@8 1098 /*
joachim99@8 1099 for (QValueList<QCString>::const_iterator it(m_generalPatterns.begin()),
joachim99@8 1100 itEnd(m_generalPatterns.end());
joachim99@8 1101 it != itEnd; ++it)
joachim99@8 1102 {
joachim99@8 1103 if (::fnmatch(*it, text.local8Bit(), FNM_PATHNAME) == 0)
joachim99@8 1104 {
joachim99@8 1105 return true;
joachim99@8 1106 }
joachim99@8 1107 }
joachim99@8 1108 */
joachim99@8 1109
joachim99@8 1110
joachim99@8 1111 for ( it = m_generalPatterns.begin(); it != m_generalPatterns.end(); ++it )
joachim99@8 1112 {
joachim99@8 1113 QRegExp pattern( *it, true /*CaseSensitive*/, true /*wildcard mode*/);
joachim99@8 1114 #if QT_VERSION==230
joachim99@8 1115 int len=0;
joachim99@8 1116 if ( pattern.match( text, 0, &len )!=-1 && len==text.length())
joachim99@8 1117 return true;
joachim99@8 1118 #else
joachim99@8 1119 if ( pattern.exactMatch( text ) )
joachim99@8 1120 return true;
joachim99@8 1121 #endif
joachim99@8 1122 }
joachim99@8 1123
joachim99@8 1124 return false;
joachim99@8 1125 }
joachim99@8 1126
joachim99@8 1127 static QString nicePath( const QFileInfo& fi )
joachim99@8 1128 {
joachim99@8 1129 QString fp = fi.filePath();
joachim99@8 1130 if ( fp.length()>2 && fp[0] == '.' && fp[1] == '/' )
joachim99@8 1131 {
joachim99@8 1132 return fp.mid(2);
joachim99@8 1133 }
joachim99@8 1134 return fp;
joachim99@8 1135 }
joachim99@8 1136
joachim99@8 1137 static bool cvsIgnoreExists( t_DirectoryList* pDirList )
joachim99@8 1138 {
joachim99@8 1139 t_DirectoryList::iterator i;
joachim99@8 1140 for( i = pDirList->begin(); i!=pDirList->end(); ++i )
joachim99@8 1141 {
joachim99@8 1142 if ( i->fileName()==".cvsignore" )
joachim99@8 1143 return true;
joachim99@8 1144 }
joachim99@8 1145 return false;
joachim99@8 1146 }
joachim99@8 1147
joachim99@8 1148 bool FileAccessJobHandler::listDir( t_DirectoryList* pDirList, bool bRecursive, bool bFindHidden, const QString& filePattern,
joachim99@8 1149 const QString& fileAntiPattern, const QString& dirAntiPattern, bool bFollowDirLinks, bool bUseCvsIgnore )
joachim99@8 1150 {
joachim99@8 1151 m_pDirList = pDirList;
joachim99@8 1152 m_pDirList->clear();
joachim99@8 1153 m_bFindHidden = bFindHidden;
joachim99@8 1154 m_bRecursive = bRecursive;
joachim99@8 1155 m_bFollowDirLinks = bFollowDirLinks; // Only relevant if bRecursive==true.
joachim99@8 1156 m_fileAntiPattern = fileAntiPattern;
joachim99@8 1157 m_filePattern = filePattern;
joachim99@8 1158 m_dirAntiPattern = dirAntiPattern;
joachim99@8 1159
joachim99@8 1160 if ( g_pProgressDialog->wasCancelled() )
joachim99@8 1161 return true; // Cancelled is not an error.
joachim99@8 1162
joachim99@8 1163 g_pProgressDialog->setSubInformation( i18n("Reading directory: ") + m_pFileAccess->absFilePath(), 0, false );
joachim99@8 1164
joachim99@8 1165 if( m_pFileAccess->isLocal() )
joachim99@8 1166 {
joachim99@8 1167 m_bSuccess = QDir::setCurrent( m_pFileAccess->absFilePath() );
joachim99@8 1168 if ( m_bSuccess )
joachim99@8 1169 {
joachim99@8 1170 m_bSuccess = true;
joachim99@8 1171 QDir dir( "." );
joachim99@8 1172
joachim99@8 1173 dir.setSorting( QDir::Name | QDir::DirsFirst );
joachim99@8 1174 dir.setFilter( QDir::Files | QDir::Dirs | QDir::Hidden );
joachim99@8 1175 dir.setMatchAllDirs( true );
joachim99@8 1176
joachim99@8 1177 const QFileInfoList *fiList = dir.entryInfoList();
joachim99@8 1178 if ( fiList == 0 )
joachim99@8 1179 {
joachim99@8 1180 // No Permission to read directory or other error.
joachim99@8 1181 m_bSuccess = false;
joachim99@8 1182 }
joachim99@8 1183 else
joachim99@8 1184 {
joachim99@8 1185 QFileInfoListIterator it( *fiList ); // create list iterator
joachim99@8 1186 for ( ; it.current() != 0; ++it ) // for each file...
joachim99@8 1187 {
joachim99@8 1188 QFileInfo* fi = it.current();
joachim99@8 1189 if ( fi->fileName() == "." || fi->fileName()==".." )
joachim99@8 1190 continue;
joachim99@8 1191
joachim99@8 1192 pDirList->push_back( FileAccess( nicePath(*fi) ) );
joachim99@8 1193 }
joachim99@8 1194 }
joachim99@8 1195 }
joachim99@8 1196 }
joachim99@8 1197 else
joachim99@8 1198 {
joachim99@8 1199 bool bShowProgress = false;
joachim99@8 1200
joachim99@8 1201 KIO::ListJob* pListJob=0;
joachim99@8 1202 pListJob = KIO::listDir( m_pFileAccess->m_url, bShowProgress, true /*bFindHidden*/ );
joachim99@8 1203
joachim99@8 1204 m_bSuccess = false;
joachim99@8 1205 if ( pListJob!=0 )
joachim99@8 1206 {
joachim99@8 1207 connect( pListJob, SIGNAL( entries( KIO::Job *, const KIO::UDSEntryList& ) ),
joachim99@8 1208 this, SLOT( slotListDirProcessNewEntries( KIO::Job *, const KIO::UDSEntryList& )) );
joachim99@8 1209 connect( pListJob, SIGNAL( result( KIO::Job* )),
joachim99@8 1210 this, SLOT( slotSimpleJobResult(KIO::Job*) ) );
joachim99@8 1211
joachim99@8 1212 connect( pListJob, SIGNAL( infoMessage(KIO::Job*, const QString&)),
joachim99@8 1213 this, SLOT( slotListDirInfoMessage(KIO::Job*, const QString&) ));
joachim99@8 1214
joachim99@8 1215 // This line makes the transfer via fish unreliable.:-(
joachim99@8 1216 //connect( pListJob, SIGNAL(percent(KIO::Job*,unsigned long)), this, SLOT(slotPercent(KIO::Job*, unsigned long)));
joachim99@8 1217
joachim99@51 1218 g_pProgressDialog->enterEventLoop( pListJob,
joachim99@51 1219 i18n("Listing directory: %1").arg(m_pFileAccess->prettyAbsPath()) );
joachim99@8 1220 }
joachim99@8 1221 }
joachim99@8 1222
joachim99@8 1223 CvsIgnoreList cvsIgnoreList;
joachim99@8 1224 if ( bUseCvsIgnore )
joachim99@8 1225 {
joachim99@8 1226 cvsIgnoreList.init( *m_pFileAccess, cvsIgnoreExists(pDirList) );
joachim99@8 1227 }
joachim99@8 1228
joachim99@8 1229 // Now remove all entries that don't match:
joachim99@8 1230 t_DirectoryList::iterator i;
joachim99@8 1231 for( i = pDirList->begin(); i!=pDirList->end(); )
joachim99@8 1232 {
joachim99@8 1233 t_DirectoryList::iterator i2=i;
joachim99@8 1234 ++i2;
joachim99@8 1235 QString fn = i->fileName();
joachim99@8 1236 if ( (!bFindHidden && i->isHidden() )
joachim99@8 1237 ||
joachim99@8 1238 (i->isFile() &&
joachim99@8 1239 ( !wildcardMultiMatch( filePattern, i->fileName(), true ) ||
joachim99@8 1240 wildcardMultiMatch( fileAntiPattern, i->fileName(), true ) ) )
joachim99@8 1241 ||
joachim99@8 1242 (i->isDir() && wildcardMultiMatch( dirAntiPattern, i->fileName(), true ) )
joachim99@8 1243 ||
joachim99@8 1244 cvsIgnoreList.matches(i->fileName())
joachim99@8 1245 )
joachim99@8 1246 {
joachim99@8 1247 // Remove it
joachim99@8 1248 pDirList->erase( i );
joachim99@8 1249 i = i2;
joachim99@8 1250 }
joachim99@8 1251 else
joachim99@8 1252 {
joachim99@8 1253 ++i;
joachim99@8 1254 }
joachim99@8 1255 }
joachim99@8 1256
joachim99@8 1257 if ( bRecursive )
joachim99@8 1258 {
joachim99@8 1259 t_DirectoryList subDirsList;
joachim99@8 1260
joachim99@8 1261 t_DirectoryList::iterator i;
joachim99@8 1262 for( i = m_pDirList->begin(); i!=m_pDirList->end(); ++i )
joachim99@8 1263 {
joachim99@8 1264 if ( i->isDir() && (!i->isSymLink() || m_bFollowDirLinks))
joachim99@8 1265 {
joachim99@8 1266 t_DirectoryList dirList;
joachim99@8 1267 i->listDir( &dirList, bRecursive, bFindHidden,
joachim99@8 1268 filePattern, fileAntiPattern, dirAntiPattern, bFollowDirLinks, bUseCvsIgnore );
joachim99@8 1269
joachim99@8 1270 t_DirectoryList::iterator j;
joachim99@8 1271 for( j = dirList.begin(); j!=dirList.end(); ++j )
joachim99@8 1272 {
joachim99@8 1273 j->m_path = i->fileName() + "/" + j->m_path;
joachim99@8 1274 }
joachim99@8 1275
joachim99@8 1276 // append data onto the main list
joachim99@8 1277 subDirsList.splice( subDirsList.end(), dirList );
joachim99@8 1278 }
joachim99@8 1279 }
joachim99@8 1280
joachim99@8 1281 m_pDirList->splice( m_pDirList->end(), subDirsList );
joachim99@8 1282 }
joachim99@8 1283
joachim99@8 1284 return m_bSuccess;
joachim99@8 1285 }
joachim99@8 1286
joachim99@8 1287
joachim99@8 1288 void FileAccessJobHandler::slotListDirProcessNewEntries( KIO::Job *, const KIO::UDSEntryList& l )
joachim99@8 1289 {
joachim99@8 1290 KURL parentUrl( m_pFileAccess->m_absFilePath );
joachim99@8 1291
joachim99@8 1292 KIO::UDSEntryList::ConstIterator i;
joachim99@8 1293 for ( i=l.begin(); i!=l.end(); ++i )
joachim99@8 1294 {
joachim99@8 1295 const KIO::UDSEntry& e = *i;
joachim99@8 1296 FileAccess fa;
joachim99@8 1297 fa.setUdsEntry( e );
joachim99@8 1298
joachim99@8 1299 if ( fa.filePath() != "." && fa.filePath() != ".." )
joachim99@8 1300 {
joachim99@8 1301 fa.m_url = parentUrl;
joachim99@8 1302 fa.m_url.addPath( fa.filePath() );
joachim99@8 1303 fa.m_absFilePath = fa.m_url.url();
joachim99@8 1304 m_pDirList->push_back( fa );
joachim99@8 1305 }
joachim99@8 1306 }
joachim99@8 1307 }
joachim99@8 1308
joachim99@8 1309 void FileAccessJobHandler::slotListDirInfoMessage( KIO::Job*, const QString& msg )
joachim99@8 1310 {
joachim99@8 1311 g_pProgressDialog->setSubInformation( msg, 0 );
joachim99@8 1312 }
joachim99@8 1313
joachim99@8 1314 void FileAccessJobHandler::slotPercent( KIO::Job*, unsigned long percent )
joachim99@8 1315 {
joachim99@8 1316 g_pProgressDialog->setSubCurrent( percent/100.0 );
joachim99@8 1317 }
joachim99@8 1318
joachim99@8 1319
joachim99@8 1320 ProgressDialog::ProgressDialog( QWidget* pParent )
joachim99@8 1321 : QDialog( pParent, 0, true )
joachim99@8 1322 {
joachim99@8 1323 QVBoxLayout* layout = new QVBoxLayout(this);
joachim99@8 1324
joachim99@8 1325 m_pInformation = new QLabel( " ", this );
joachim99@8 1326 layout->addWidget( m_pInformation );
joachim99@8 1327
joachim99@8 1328 m_pProgressBar = new KProgress(1000, this);
joachim99@8 1329 layout->addWidget( m_pProgressBar );
joachim99@8 1330
joachim99@8 1331 m_pSubInformation = new QLabel( " ", this);
joachim99@8 1332 layout->addWidget( m_pSubInformation );
joachim99@8 1333
joachim99@8 1334 m_pSubProgressBar = new KProgress(1000, this);
joachim99@8 1335 layout->addWidget( m_pSubProgressBar );
joachim99@8 1336
joachim99@51 1337 QHBoxLayout* hlayout = new QHBoxLayout( layout );
joachim99@51 1338 m_pSlowJobInfo = new QLabel( " ", this);
joachim99@51 1339 hlayout->addWidget( m_pSlowJobInfo );
joachim99@51 1340
joachim99@51 1341 m_pAbortButton = new QPushButton( i18n("Cancel"), this);
joachim99@51 1342 hlayout->addWidget( m_pAbortButton );
joachim99@51 1343 connect( m_pAbortButton, SIGNAL(clicked()), this, SLOT(slotAbort()) );
joachim99@51 1344
joachim99@8 1345 m_dCurrent = 0.0;
joachim99@8 1346 m_dSubMax = 1.0;
joachim99@8 1347 m_dSubMin = 0.0;
joachim99@8 1348 m_dSubCurrent = 0.0;
joachim99@8 1349 resize( 400, 100 );
joachim99@8 1350 m_t1.start();
joachim99@8 1351 m_t2.start();
joachim99@8 1352 m_bWasCancelled = false;
joachim99@51 1353 m_pJob = 0;
joachim99@8 1354 }
joachim99@8 1355
joachim99@8 1356
joachim99@8 1357 void ProgressDialog::setInformation(const QString& info, double dCurrent, bool bRedrawUpdate )
joachim99@8 1358 {
joachim99@8 1359 m_pInformation->setText( info );
joachim99@8 1360 m_dCurrent = dCurrent;
joachim99@8 1361 m_dSubCurrent=0;
joachim99@8 1362 m_dSubMin = 0;
joachim99@8 1363 m_dSubMax = 1;
joachim99@8 1364 m_pSubInformation->setText("");
joachim99@8 1365 recalc(bRedrawUpdate);
joachim99@8 1366 }
joachim99@8 1367
joachim99@8 1368 void ProgressDialog::setInformation(const QString& info, bool bRedrawUpdate )
joachim99@8 1369 {
joachim99@8 1370 m_pInformation->setText( info );
joachim99@8 1371 m_dSubCurrent = 0;
joachim99@8 1372 m_dSubMin = 0;
joachim99@8 1373 m_dSubMax = 1;
joachim99@8 1374 m_pSubInformation->setText("");
joachim99@8 1375 recalc(bRedrawUpdate);
joachim99@8 1376 }
joachim99@8 1377
joachim99@8 1378 void ProgressDialog::setMaximum( int maximum )
joachim99@8 1379 {
joachim99@8 1380 m_maximum = maximum;
joachim99@8 1381 m_dCurrent = 0;
joachim99@8 1382 }
joachim99@8 1383
joachim99@8 1384 void ProgressDialog::step( bool bRedrawUpdate )
joachim99@8 1385 {
joachim99@8 1386 m_dCurrent += 1.0/m_maximum;
joachim99@8 1387 m_dSubCurrent=0;
joachim99@8 1388 recalc(bRedrawUpdate);
joachim99@8 1389 }
joachim99@8 1390
joachim99@8 1391 void ProgressDialog::setSubInformation(const QString& info, double dSubCurrent, bool bRedrawUpdate )
joachim99@8 1392 {
joachim99@8 1393 m_pSubInformation->setText(info);
joachim99@8 1394 m_dSubCurrent = dSubCurrent;
joachim99@8 1395 recalc(bRedrawUpdate);
joachim99@8 1396 }
joachim99@8 1397
joachim99@8 1398 void ProgressDialog::setSubCurrent( double dSubCurrent, bool bRedrawUpdate )
joachim99@8 1399 {
joachim99@8 1400 m_dSubCurrent = dSubCurrent;
joachim99@8 1401 recalc( bRedrawUpdate );
joachim99@8 1402 }
joachim99@8 1403
joachim99@8 1404
joachim99@8 1405 void qt_enter_modal(QWidget*);
joachim99@8 1406 void qt_leave_modal(QWidget*);
joachim99@8 1407
joachim99@51 1408 void ProgressDialog::enterEventLoop( KIO::Job* pJob, const QString& jobInfo )
joachim99@8 1409 {
joachim99@51 1410 m_pJob = pJob;
joachim99@51 1411 m_currentJobInfo = jobInfo;
joachim99@51 1412 startTimer( 3000 ); /* 3 s delay */
joachim99@51 1413
joachim99@8 1414 // instead of using exec() the eventloop is entered and exited often without hiding/showing the window.
joachim99@8 1415 #if QT_VERSION==230
joachim99@8 1416 //qApp->enter_loop();
joachim99@8 1417 #else
joachim99@8 1418 qt_enter_modal(this);
joachim99@8 1419 qApp->eventLoop()->enterLoop();
joachim99@8 1420 qt_leave_modal(this);
joachim99@8 1421 #endif
joachim99@8 1422 }
joachim99@8 1423
joachim99@8 1424 void ProgressDialog::exitEventLoop()
joachim99@8 1425 {
joachim99@51 1426 killTimers();
joachim99@51 1427 m_pJob = 0;
joachim99@8 1428 #if QT_VERSION==230
joachim99@8 1429 //qApp->exit_loop();
joachim99@8 1430 #else
joachim99@8 1431 qApp->eventLoop()->exitLoop();
joachim99@8 1432 #endif
joachim99@8 1433 }
joachim99@8 1434
joachim99@8 1435 void ProgressDialog::recalc( bool bUpdate )
joachim99@8 1436 {
joachim99@8 1437 if( (bUpdate && m_dSubCurrent == 0) || m_t1.elapsed()>200 )
joachim99@8 1438 {
joachim99@8 1439 m_pProgressBar->setProgress( int( 1000.0 * m_dCurrent ) );
joachim99@8 1440 m_pSubProgressBar->setProgress( int( 1000.0 * ( m_dSubCurrent * (m_dSubMax - m_dSubMin) + m_dSubMin ) ) );
joachim99@8 1441 if ( !isVisible() ) show();
joachim99@51 1442 m_pSlowJobInfo->setText("");
joachim99@8 1443 qApp->processEvents();
joachim99@8 1444 m_t1.restart();
joachim99@8 1445 }
joachim99@8 1446 }
joachim99@8 1447
joachim99@8 1448 void ProgressDialog::start()
joachim99@8 1449 {
joachim99@8 1450 setInformation("",0, true);
joachim99@8 1451 setSubInformation("",0);
joachim99@8 1452 m_bWasCancelled = false;
joachim99@8 1453 m_t1.restart();
joachim99@8 1454 m_t2.restart();
joachim99@8 1455 show();
joachim99@8 1456 }
joachim99@8 1457
joachim99@8 1458 #include <qtimer.h>
joachim99@8 1459 void ProgressDialog::show()
joachim99@8 1460 {
joachim99@51 1461 killTimers();
joachim99@24 1462 if ( !isVisible() )
joachim99@24 1463 {
joachim99@8 1464 #if QT_VERSION==230
joachim99@24 1465 QWidget::show();
joachim99@8 1466 #else
joachim99@24 1467 QDialog::show();
joachim99@8 1468 #endif
joachim99@24 1469 }
joachim99@8 1470 }
joachim99@8 1471
joachim99@8 1472 void ProgressDialog::hide()
joachim99@8 1473 {
joachim99@51 1474 killTimers();
joachim99@8 1475 // Calling QDialog::hide() directly doesn't always work. (?)
joachim99@8 1476 QTimer::singleShot( 100, this, SLOT(delayedHide()) );
joachim99@8 1477 }
joachim99@8 1478
joachim99@8 1479 void ProgressDialog::delayedHide()
joachim99@8 1480 {
joachim99@51 1481 if (m_pJob!=0)
joachim99@51 1482 {
joachim99@51 1483 m_pJob->kill(false);
joachim99@51 1484 m_pJob = 0;
joachim99@51 1485 }
joachim99@8 1486 QDialog::hide();
joachim99@51 1487 m_pInformation->setText( "" );
joachim99@51 1488 m_dSubCurrent = 0;
joachim99@51 1489 m_dSubMin = 0;
joachim99@51 1490 m_dSubMax = 1;
joachim99@51 1491 m_dCurrent = 0;
joachim99@51 1492 m_pProgressBar->setProgress( 0 );
joachim99@51 1493 m_pSubProgressBar->setProgress( 0 );
joachim99@51 1494 m_pSubInformation->setText("");
joachim99@51 1495 m_pSlowJobInfo->setText("");
joachim99@8 1496 }
joachim99@8 1497
joachim99@8 1498 void ProgressDialog::reject()
joachim99@8 1499 {
joachim99@8 1500 m_bWasCancelled = true;
joachim99@8 1501 QDialog::reject();
joachim99@8 1502 }
joachim99@8 1503
joachim99@51 1504 void ProgressDialog::slotAbort()
joachim99@51 1505 {
joachim99@51 1506 reject();
joachim99@51 1507 }
joachim99@51 1508
joachim99@8 1509 bool ProgressDialog::wasCancelled()
joachim99@8 1510 {
joachim99@8 1511 if( m_t2.elapsed()>100 )
joachim99@8 1512 {
joachim99@8 1513 qApp->processEvents();
joachim99@8 1514 m_t2.restart();
joachim99@8 1515 }
joachim99@8 1516 return m_bWasCancelled;
joachim99@8 1517 }
joachim99@8 1518
joachim99@8 1519 // The progressbar goes from 0 to 1 usually.
joachim99@8 1520 // By supplying a subrange transformation the subCurrent-values
joachim99@8 1521 // 0 to 1 will be transformed to dMin to dMax instead.
joachim99@8 1522 // Requirement: 0 < dMin < dMax < 1
joachim99@8 1523 void ProgressDialog::setSubRangeTransformation( double dMin, double dMax )
joachim99@8 1524 {
joachim99@8 1525 m_dSubMin = dMin;
joachim99@8 1526 m_dSubMax = dMax;
joachim99@8 1527 m_dSubCurrent = 0;
joachim99@8 1528 }
joachim99@8 1529
joachim99@51 1530 void ProgressDialog::timerEvent(QTimerEvent*)
joachim99@51 1531 {
joachim99@51 1532 if( !isVisible() )
joachim99@51 1533 {
joachim99@51 1534 show();
joachim99@51 1535 }
joachim99@51 1536 m_pSlowJobInfo->setText( m_currentJobInfo );
joachim99@51 1537 }
joachim99@51 1538
joachim99@8 1539
joachim99@8 1540 #include "fileaccess.moc"