vcs/cvsservice/changelog.cpp
Go to the documentation of this file.00001 /*************************************************************************** 00002 * Copyright (C) 2003 by Mario Scalas * 00003 * mario.scalas@libero.it * 00004 * * 00005 * This program is free software; you can redistribute it and/or modify * 00006 * it under the terms of the GNU General Public License as published by * 00007 * the Free Software Foundation; either version 2 of the License, or * 00008 * (at your option) any later version. * 00009 * * 00010 ***************************************************************************/ 00011 00012 #include <qdatetime.h> 00013 #include <qfile.h> 00014 #include <qtextstream.h> 00015 00016 #include <kemailsettings.h> 00017 00018 #include "changelog.h" 00019 00020 ChangeLogEntry::ChangeLogEntry() 00021 { 00022 KEMailSettings emailConfig; 00023 emailConfig.setProfile( emailConfig.defaultProfileName() ); 00024 authorEmail = emailConfig.getSetting( KEMailSettings::EmailAddress ); 00025 authorName = emailConfig.getSetting( KEMailSettings::RealName ); 00026 00027 QDate currDate = QDate::currentDate(); 00028 date = currDate.toString( "yyyy-MM-dd" ); 00029 } 00030 00031 ChangeLogEntry::~ChangeLogEntry() 00032 { 00033 } 00034 00035 void ChangeLogEntry::addLine( const QString &aLine ) 00036 { 00037 lines << aLine; 00038 } 00039 00040 void ChangeLogEntry::addLines( const QStringList &someLines ) 00041 { 00042 lines += someLines; 00043 } 00044 00045 void streamCopy( QTextStream &is, QTextStream &os ) 00046 { 00047 while (!is.eof()) 00048 os << is.readLine() << "\n"; // readLine() eats '\n' !! 00049 } 00050 00051 void ChangeLogEntry::addToLog( const QString &logFilePath, const bool prepend, const QString &startLineString ) 00052 { 00053 if (prepend) // add on head 00054 { 00055 QString fakeLogFilePath = logFilePath + ".fake"; 00056 00057 QFile fakeFile( fakeLogFilePath ); 00058 QFile changeLogFile( logFilePath ); 00059 { 00060 if (!fakeFile.open( IO_WriteOnly | IO_Append)) 00061 return; 00062 00063 if (changeLogFile.open( IO_ReadOnly )) // A Changelog already exist 00064 { 00065 QTextStream is( &changeLogFile ); 00066 QTextStream os( &fakeFile ); 00067 00068 // Put current entry 00069 os << toString( startLineString ); 00070 // Write the rest of the change log file 00071 streamCopy( is, os ); 00072 } 00073 else // ChangeLog doesn't exist: just write our entry 00074 { 00075 QTextStream t( &fakeFile ); 00076 t << toString( startLineString ); 00077 } 00078 fakeFile.close(); 00079 changeLogFile.close(); 00080 } 00081 // Ok, now we have the change log we need in fakeLogFilePath: we should ask for a 00082 // 'mv fakeLogFilePath logFilePath'-like command ... :-/ 00083 if (!fakeFile.open( IO_ReadOnly )) 00084 return; 00085 00086 if (changeLogFile.open( IO_WriteOnly )) 00087 { 00088 QTextStream os( &changeLogFile ); 00089 QTextStream is( &fakeFile ); 00090 00091 // Write the rest of the change log file 00092 streamCopy( is, os ); 00093 } 00094 fakeFile.close(); 00095 fakeFile.remove(); // fake changelog is no more needed! 00096 changeLogFile.close(); 00097 } 00098 else // add on tail 00099 { 00100 QFile f( logFilePath ); 00101 if (!f.open( IO_WriteOnly | IO_Append)) 00102 return; 00103 00104 QTextStream t( &f ); 00105 t << toString( startLineString ); 00106 } 00107 } 00108 00109 QString ChangeLogEntry::toString( const QString &startLineString ) const 00110 { 00111 QString header = date + " " + authorName + " <" + authorEmail + ">\n"; 00112 00113 return header + startLineString + lines.join( "\n" + startLineString ) + "\n\n"; 00114 }