Logo Search packages:      
Sourcecode: kmess version File versions  Download package

QString KMessShared::nextSequentialFile ( const QString &  baseDirPath,
const QString &  baseFileName,
const QString &  extension,
QString &  lastFile 
) [static]

Given a base directory and filename, returns the full path to the next non-existing sequentially numbered file. The parameter previousSequentialFile is set to the full path of the file immediately previous to this in the sequence.

Take the following examples:

   QString lastFile;
   QString nextFile = nextSequentialFile( "./", "foo@bar.com", "xml", lastFile );

Assume that only the base file, foo@bar.com.xml exists. Then:

   lastFile = "./foo@bar.com.xml"
   nextFile = "./foo@bar.com.1.xml"

Assume that the files foo@bar.com.xml, foo@bar.com.1.xml and foo@bar.com.2.xml exist. Then:

   previousFile = "./foo@bar.com.2.xml"
   nextFile = "./foo@bar.com.3.xml"

etc.

Parameters:
baseDirPathThe full path to the base directory.
baseFileNameThe base file name, minus the extension or numbering.
extensionThe extension of the file.
lastFile[out] Set to the full path of the last existing file in the sequence, or an empty string if none exists.
Returns:
The full path to the next unused sequential file.

Definition at line 748 of file kmessshared.cpp.

References compareFileNames().

Referenced by Chat::endChatLogging(), and ChatHistoryManager::lastChatLog().

{
  QDir baseDir( baseDirPath );

  baseDir.setFilter( QDir::Files );
  baseDir.setNameFilters( QStringList() << ( baseFileName + "*." + extension ) );

  // Get the list of all .1, .2, etc log files.
  // See KMessShared::compareFileNames()

  QStringList numberedFiles( baseDir.entryList() );
  qSort( numberedFiles.begin(), numberedFiles.end(), KMessShared::compareFileNames );

  // There are no matching files
  if( numberedFiles.size() == 0 )
  {
    lastFile = QString();
    return baseDir.absoluteFilePath( baseFileName + "." + extension );
  }

  QString nextFile;
  lastFile = baseDir.absoluteFilePath( numberedFiles.last() );

  // one or more numbered files exist. grab the number of the most recent, increment it.
  // that becomes the number of the next numbered file.

  int pos = lastFile.indexOf( baseFileName ) + baseFileName.length() + 1;
  QString numberPart( lastFile.mid( pos, lastFile.lastIndexOf( extension ) - pos - 1 ) );
  bool ok = false;
  int number = numberPart.toInt( &ok );

  // The file is in form: baseFileName.extension
  if( numberPart == extension || ! ok )
  {
    return baseDir.absoluteFilePath( baseFileName + ".1." + extension );
  }
  // The file is in form: baseFileName.number.extension
  else
  {
    return baseDir.absoluteFilePath( baseFileName + "." + QString::number( number + 1 ) + "." + extension );
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:


Generated by  Doxygen 1.6.0   Back to index