summaryrefslogtreecommitdiffstats
path: root/vcs/cvsservice/changelog.cpp
blob: 93aec07d0ac8b43214a645fe52437b67608853ab (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                                    *
 *   mario.scalas@libero.it                                                *
 *                                                                         *
 *   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 <tqdatetime.h>
#include <tqfile.h>
#include <textstream.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 );

    TQDate currDate = TQDate::currentDate();
    date = currDate.toString( "yyyy-MM-dd" );
}

ChangeLogEntry::~ChangeLogEntry()
{
}

void ChangeLogEntry::addLine( const TQString &aLine )
{
    lines << aLine;
}

void ChangeLogEntry::addLines( const TQStringList &someLines )
{
    lines += someLines;
}

void streamCopy( TQTextStream &is, TQTextStream &os )
{
    while (!is.eof())
        os << is.readLine() << "\n";  // readLine() eats '\n' !!
}

void ChangeLogEntry::addToLog( const TQString &logFilePath, const bool prepend, const TQString &startLineString )
{
    if (prepend) // add on head
    {
        TQString fakeLogFilePath = logFilePath + ".fake";

        TQFile fakeFile( fakeLogFilePath );
        TQFile changeLogFile( logFilePath );
        {
            if (!fakeFile.open( IO_WriteOnly | IO_Append))
                return;

            if (changeLogFile.open( IO_ReadOnly )) // A Changelog already exist
            {
                TQTextStream is( &changeLogFile );
                TQTextStream 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
            {
                TQTextStream 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 ))
        {
            TQTextStream os( &changeLogFile );
            TQTextStream 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
    {
        TQFile f( logFilePath );
        if (!f.open( IO_WriteOnly | IO_Append))
            return;

        TQTextStream t( &f );
        t << toString( startLineString );
    }
}

TQString ChangeLogEntry::toString( const TQString &startLineString ) const
{
    TQString header = date + " " + authorName + " <" + authorEmail + ">\n";

    return header + startLineString + lines.join( "\n" + startLineString ) + "\n\n";
}