blob: a63a2b929aefb8cd9cf18354665da42683fc2a74 (
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
|
/***************************************************************************
* Copyright (C) 2003 by Mario Scalas *
* [email protected] *
* *
* 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. *
* *
***************************************************************************/
#include <qdatetime.h>
#include <qfile.h>
#include <qtextstream.h>
#include <kemailsettings.h>
#include "changelog.h"
ChangeLogEntry::ChangeLogEntry()
{
KEMailSettings emailConfig;
emailConfig.setProfile( emailConfig.defaultProfileName() );
authorEmail = emailConfig.getSetting( KEMailSettings::EmailAddress );
authorName = emailConfig.getSetting( KEMailSettings::RealName );
QDate currDate = QDate::currentDate();
date = currDate.toString( "yyyy-MM-dd" );
}
ChangeLogEntry::~ChangeLogEntry()
{
}
void ChangeLogEntry::addLine( const QString &aLine )
{
lines << aLine;
}
void ChangeLogEntry::addLines( const QStringList &someLines )
{
lines += someLines;
}
void streamCopy( QTextStream &is, QTextStream &os )
{
while (!is.eof())
os << is.readLine() << "\n"; // readLine() eats '\n' !!
}
void ChangeLogEntry::addToLog( const QString &logFilePath, const bool prepend, const QString &startLineString )
{
if (prepend) // add on head
{
QString fakeLogFilePath = logFilePath + ".fake";
QFile fakeFile( fakeLogFilePath );
QFile changeLogFile( logFilePath );
{
if (!fakeFile.open( IO_WriteOnly | IO_Append))
return;
if (changeLogFile.open( IO_ReadOnly )) // A Changelog already exist
{
QTextStream is( &changeLogFile );
QTextStream os( &fakeFile );
// Put current entry
os << toString( startLineString );
// Write the rest of the change log file
streamCopy( is, os );
}
else // ChangeLog doesn't exist: just write our entry
{
QTextStream t( &fakeFile );
t << toString( startLineString );
}
fakeFile.close();
changeLogFile.close();
}
// Ok, now we have the change log we need in fakeLogFilePath: we should ask for a
// 'mv fakeLogFilePath logFilePath'-like command ... :-/
if (!fakeFile.open( IO_ReadOnly ))
return;
if (changeLogFile.open( IO_WriteOnly ))
{
QTextStream os( &changeLogFile );
QTextStream is( &fakeFile );
// Write the rest of the change log file
streamCopy( is, os );
}
fakeFile.close();
fakeFile.remove(); // fake changelog is no more needed!
changeLogFile.close();
}
else // add on tail
{
QFile f( logFilePath );
if (!f.open( IO_WriteOnly | IO_Append))
return;
QTextStream t( &f );
t << toString( startLineString );
}
}
QString ChangeLogEntry::toString( const QString &startLineString ) const
{
QString header = date + " " + authorName + " <" + authorEmail + ">\n";
return header + startLineString + lines.join( "\n" + startLineString ) + "\n\n";
}
|