aboutsummaryrefslogtreecommitdiffstats
path: root/src/modules/logview/LogFile.cpp
blob: 43d98f01c0d4c99090cea3f374cebfd9988b19fe (about) (plain) (blame)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//=============================================================================
//
//   File : LogFile.cpp
//   Creation date : Thu Apr 14 2011 19:21:59 by Elvio Basello
//
//   This file is part of the KVIrc IRC client distribution
//   Copyright (C) 2011 Elvio Basello (hellvis69 at gmail dot com)
//
//   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.
//
//   This program is distributed in the HOPE that it will be USEFUL,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//   See the GNU General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program. If not, write to the Free Software Foundation,
//   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
//=============================================================================

#include "LogFile.h"

#include "KviQString.h"
#include "KviCString.h"
#include "KviOptions.h"
#include "KviFileUtils.h"

#include <QFileInfo>

#ifdef COMPILE_ZLIB_SUPPORT
#include <zlib.h>
#endif

LogFile::LogFile(const QString & szName)
{
	m_szFilename = szName;

	QFileInfo fi(m_szFilename);
	QString szTmpName = fi.fileName();

	m_bCompressed = (fi.suffix() == "gz");
	if(m_bCompressed)
	{
		//removes trailing dot and extension
		szTmpName.chop(3);
	}
	QString szTypeToken = szTmpName.section('_', 0, 0);
	// Ignore non-logs files, this includes '.' and '..'
	if(KviQString::equalCI(szTypeToken, "channel") || KviQString::equalCI(szTypeToken, "deadchannel"))
	{
		m_szType = "channel";
		m_eType = Channel;
	}
	else if(KviQString::equalCI(szTypeToken, "console"))
	{
		m_szType = "console";
		m_eType = Console;
	}
	else if(KviQString::equalCI(szTypeToken, "query"))
	{
		m_szType = "query";
		m_eType = Query;
	}
	else if(KviQString::equalCI(szTypeToken, "dccchat"))
	{
		m_szType = "dccchat";
		m_eType = DccChat;
	}
	else
	{
		m_szType = "";
		m_eType = Other;
	}

	KviCString szUndecoded = szTmpName.section('.', 0, 0);
	szUndecoded.cutToFirst('_');
	m_szName = szUndecoded.hexDecode(szUndecoded.ptr()).ptr();

	szUndecoded = szTmpName.section('.', 1).section('_', 0, -2);
	m_szNetwork = szUndecoded.hexDecode(szUndecoded.ptr()).ptr();

	QString szDate = szTmpName.section('_', -1).section('.', 0, -2);

	switch(KVI_OPTION_UINT(KviOption_uintOutputDatetimeFormat))
	{
		case 1:
			m_date = QDate::fromString(szDate, Qt::ISODate);
			break;
		case 2:
			m_date = QDate::fromString(szDate, Qt::SystemLocaleShortDate);
			if(!m_date.isValid())
			{
				// some locale date formats use '/' as a separator; we change them to '-'
				// when creating log files. Try to reverse that change here
				QString szUnescapedDate = szDate;
				szUnescapedDate.replace('-', KVI_PATH_SEPARATOR_CHAR);
				m_date = QDate::fromString(szUnescapedDate, Qt::SystemLocaleShortDate);
				if(m_date.isValid())
				{
					//qt4 defaults to 1900 for years. So "11" means "1911" instead of "2011".. what a pity
					if(m_date.year() < 1990)
						m_date = m_date.addYears(100);
				}
			}
			break;
		case 0:
		default:
			m_date = QDate::fromString(szDate, "yyyy.MM.dd");
			break;
	}
	if(!m_date.isValid())
	{
		// probably the log has been created when the OutputDatetimeFormat option
		// was set to a different value. Try to guess its format
		m_date = QDate::fromString(szDate, "yyyy.MM.dd");
		if(!m_date.isValid())
		{
			m_date = QDate::fromString(szDate, Qt::ISODate);
			if(!m_date.isValid())
			{
				m_date = QDate::fromString(szDate, Qt::SystemLocaleShortDate);
				if(!m_date.isValid())
				{
					// some locale date formats use '/' as a separator; we change them to '-'
					// when creating log files. Try to reverse that change here
					QString szUnescapedDate = szDate;
					szUnescapedDate.replace('-', KVI_PATH_SEPARATOR_CHAR);
					m_date = QDate::fromString(szUnescapedDate, Qt::SystemLocaleShortDate);
					if(m_date.isValid())
					{
						//qt4 defaults to 1900 for years. So "11" means "1911" instead of "2011".. what a pity
						if(m_date.year() < 1990)
							m_date = m_date.addYears(100);
					}
				}
				// if the date is still not valid, we give up
			}
		}
	}
}

void LogFile::getText(QString & szText)
{
	QString szLogName = fileName();
	QFile logFile;
#ifdef COMPILE_ZLIB_SUPPORT
	if(m_bCompressed)
	{
		gzFile file = gzopen(szLogName.toLocal8Bit().data(), "rb");
		if(file)
		{
			char cBuff[1025];
			int iLen;
			QByteArray data;
			//QCString data;
			iLen = gzread(file, cBuff, 1024);
			while(iLen > 0)
			{
				cBuff[iLen] = 0;
				data.append(cBuff);
				iLen = gzread(file, cBuff, 1024);
			}
			gzclose(file);
			szText = QString::fromUtf8(data);
		}
		else
		{
			qDebug("Can't open compressed file %s", szLogName.toLocal8Bit().data());
		}
	}
	else
	{
#endif
		logFile.setFileName(szLogName);

		if(!logFile.open(QIODevice::ReadOnly))
			return;

		QByteArray bytes;
		bytes = logFile.readAll();
		szText = QString::fromUtf8(bytes.data(), bytes.size());
		logFile.close();
#ifdef COMPILE_ZLIB_SUPPORT
	}
#endif
}