1
/***************************************************************************
2
* Copyright (C) 2005 by Nicolas Ternisien *
3
* nicolas.ternisien@gmail.com *
5
* This program is free software; you can redistribute it and/or modify *
6
* it under the terms of the GNU General Public License as published by *
7
* the Free Software Foundation; either version 2 of the License, or *
8
* (at your option) any later version. *
10
* This program is distributed in the hope that it will be useful, *
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13
* GNU General Public License for more details. *
15
* You should have received a copy of the GNU General Public License *
16
* along with this program; if not, write to the *
17
* Free Software Foundation, Inc., *
18
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19
***************************************************************************/
23
#include <kiconloader.h>
28
#include "childLogLine.h"
29
#include "parentLogLine.h"
31
#include "logListItem.h"
36
#include "itemFactory.h"
38
LogListItem* ItemFactory::createLogListItem(QListView* view, LogLine* line) {
40
//If it is a parent Log Line
41
if (line->isParentLogLine()==true)
42
return(createParentItem(view, (ParentLogLine*)line));
44
//If it's a child Log Line
45
if (line->isChildLogLine()==true)
46
return(createChildItem(view, (ChildLogLine*)line));
49
//If it's a Samba mode item
50
if (line->getType()==Globals::sambaMode->id) {
51
kdDebug() << "Samba... Di Janero!!" << endl;
52
LogListItem* item=createBasicItem(view, line);
53
item->setMultiLinesEnabled(true);
57
//It's a normal Log Line
58
return(createBasicItem(view, line));
63
LogListItem* ItemFactory::createParentItem(QListView* view, ParentLogLine* line) {
64
//kdDebug() << "Creating a parent item : " << line->getLogLevel()->name << endl;
66
LogListItem* item=new LogListItem(view, line);
68
//item->setExpandable(true);
71
if (line->getGroupBy()==GROUP_BY_LOG_LEVEL) {
72
item->setText(0, line->getLogLevel()->name);
73
item->setPixmap(0, line->getLogLevel()->pixmap);
75
if (line->getLogLevel()==Globals::informationLogLevel) {
77
view->ensureItemVisible(item);
85
else if (line->getGroupBy()==GROUP_BY_DAY) {
86
//TODO Move these tests to a dedicated static method (to be able to reuse it)
87
QDate today=QDate::currentDate();
88
QDate yesterday=today.addDays(-1);
90
QDate date=line->getTime().date();
93
item->setText(0, i18n("Today"));
95
view->ensureItemVisible(item);
97
else if (date==yesterday) {
98
item->setText(0, i18n("Yesterday"));
102
item->setText(0, date.toString());
103
item->setOpen(false);
106
item->setPixmap(0, SmallIcon(GROUP_BY_DAY_ICON));
109
else if (line->getGroupBy()==GROUP_BY_HOUR) {
110
QString string(i18n("%1, %2h").arg(line->getTime().date().toString(), line->getTime().time().toString("h")));
112
item->setText(0, string);
113
item->setPixmap(0, SmallIcon(GROUP_BY_HOUR_ICON));
115
QDate today=QDate::currentDate();
116
QTime time=QTime::currentTime();
117
if (line->getTime().date()==today && line->getTime().time().hour()==time.hour()) {
118
kdDebug() << "Date equals, hour equals" << line->getTime().date().toString() << " and " << line->getTime().time().toString() << endl;
120
view->ensureItemVisible(item);
123
item->setOpen(false);
128
else if (line->getGroupBy()==GROUP_BY_LOG_FILE) {
129
item->setText(0, line->getOriginalFile());
130
item->setPixmap(0, SmallIcon(GROUP_BY_LOG_FILE_ICON));
132
//Group by a specific column
134
int index=ParentLogLine::getGroupedColumnIndex(line->getColumns(), line->getGroupByColumn());
136
QStringList& list=line->getItemList();
138
if (index<0 || index>=(int)list.size())
139
item->setText(0, i18n("none"));
141
item->setText(0, list[index]);
143
item->setPixmap(0, SmallIcon(GROUP_BY_COLUMN_ICON));
149
LogListItem* ItemFactory::createChildItem(QListView* /*view*/, ChildLogLine* line) {
151
LogListItem* parent=line->getParent()->getLogListItem();
154
kdDebug() << "Parent log list item NULL !!!" << endl;
156
//kdDebug() << "The log level of the parent is " << parent->getLogLine()->getLogLevel()->name << endl;
158
LogListItem* item=new LogListItem(parent, line);
165
LogListItem* ItemFactory::createBasicItem(QListView* view, LogLine* line) {
166
LogListItem* item=new LogListItem(view, line);
173
void ItemFactory::initItem(LogListItem* item) {
174
LogLine* line=item->getLogLine();
176
//If it's a Xorg Log Line
177
if (line->getType() == Globals::xorgMode->id) {
183
initDefaultItem(item);
187
void ItemFactory::initDefaultItem(LogListItem* item) {
188
LogLine* line=item->getLogLine();
190
item->setText(0, line->getTime().toString(Qt::LocalDate));
193
QStringList& labels=line->getItemList();
194
QStringList::iterator it;
195
for(it=labels.begin(); it!=labels.end(); ++it) {
196
item->setText(i, *it);
200
item->setPixmap(0, line->getLogLevel()->pixmap);
204
void ItemFactory::initXorgItem(LogListItem* item) {
205
LogLine* line=item->getLogLine();
207
item->setText(0, "");
210
QStringList& labels=line->getItemList();
211
QStringList::iterator it;
212
for(it=labels.begin(); it!=labels.end(); ++it) {
213
item->setText(i, *it);
217
item->setPixmap(0, line->getLogLevel()->pixmap);
223
QString ItemFactory::createFormattedText(LogLine* line) {
225
//Special case if this is a ParentLogLine (Group By feature)
226
if (line->isParentLogLine())
227
return(createParentFormattedText(line));
230
if (line->getType() == Globals::cronMode->id)
231
return(createCronFormattedText(line));
233
if (line->getType() == Globals::xorgMode->id)
234
return(createXorgFormattedText(line));
236
if (line->getType() == Globals::acpidMode->id)
237
return(createAcpidFormattedText(line));
239
if (line->getType() == Globals::cupsMode->id)
240
return(createCupsFormattedText(line));
242
if (line->getType() == Globals::cupsAccessMode->id)
243
return(createCupsAccessFormattedText(line));
245
if (line->getType() == Globals::apacheMode->id)
246
return(createApacheFormattedText(line));
248
if (line->getType() == Globals::apacheAccessMode->id)
249
return(createApacheAccessFormattedText(line));
251
if (line->getType() == Globals::sambaMode->id)
252
return(createSambaFormattedText(line));
254
//Returns the default formating
255
return(createDefaultFormattedText(line));
259
QString ItemFactory::createToolTipText(LogLine* line) {
260
if (line->getType() == Globals::xorgMode->id)
261
return(createXorgToolTipText(line));
263
return(createDefaultToolTipText(line));
267
QString ItemFactory::createParentFormattedText(LogLine* line) {
270
ParentLogLine* parent=(ParentLogLine*) line;
272
//All Group By cases are showed here
273
if (parent->getGroupBy()==GROUP_BY_LOG_LEVEL) {
274
result.append(i18n("<div align='center'><b>Group:</b> %1</div>").arg(line->getLogLevel()->name));
276
else if (parent->getGroupBy()==GROUP_BY_DAY) {
277
result.append(i18n("<div align='center'><b>Group:</b> %1</div>").arg(line->getTime().date().toString()));
279
else if (parent->getGroupBy()==GROUP_BY_HOUR) {
280
QString string(i18n("%1, %2 hour").arg(line->getTime().date().toString(), line->getTime().time().toString("h")));
281
result.append(i18n("<div align='center'><b>Group:</b> %1</div>").arg(string));
283
else if (parent->getGroupBy()==GROUP_BY_LOG_FILE) {
284
result.append(i18n("<div align='center'><b>Group:</b> %1</div>").arg(line->getOriginalFile()));
287
QStringList& list=line->getItemList();
288
int index=ParentLogLine::getGroupedColumnIndex(parent->getColumns(), parent->getGroupByColumn());
289
if (index<0 || index>=(int) list.size())
290
result.append(i18n("<div align='center'><b>Group:</b> none</div>"));
292
result.append(i18n("<div align='center'><b>Group:</b> %1</div>").arg(list[index]));
299
QString ItemFactory::createCronFormattedText(LogLine* line) {
301
QStringList& items=line->getItemList();
303
result.append("<table>");
305
result.append(labelMessageFormat(i18n("Date:"), line->getTime().toString(Qt::LocalDate)));
306
result.append(labelMessageFormat(i18n("Hostname:"), items[0]));
307
result.append(labelMessageFormat(i18n("Process:"), items[1]));
308
result.append(labelMessageFormat(i18n("User:"), items[2]));
309
result.append(labelMessageFormat(i18n("Level:"), line->getLogLevel()->name));
310
result.append(labelMessageFormat(i18n("Original file:"), line->getOriginalFile()));
312
result.append("</table>");
317
QString ItemFactory::createDefaultFormattedText(LogLine* line) {
320
QStringList& items=line->getItemList();
322
result.append("<table>");
324
result.append(labelMessageFormat(i18n("Date:"), line->getTime().toString(Qt::LocalDate)));
325
result.append(labelMessageFormat(i18n("Hostname:"), items[0]));
326
result.append(labelMessageFormat(i18n("Process:"), items[1]));
327
result.append(labelMessageFormat(i18n("Level:"), line->getLogLevel()->name));
328
result.append(labelMessageFormat(i18n("Original file:"), line->getOriginalFile()));
330
result.append("</table>");
335
QString ItemFactory::createAcpidFormattedText(LogLine* line) {
338
QStringList& items=line->getItemList();
340
result.append("<table>");
342
result.append(labelMessageFormat(i18n("Date:"), line->getTime().toString(Qt::LocalDate)));
343
result.append(labelMessageFormat(i18n("Level:"), line->getLogLevel()->name));
344
result.append(labelMessageFormat(i18n("Type:"), items[0]));
346
result.append("</table>");
351
QString ItemFactory::createCupsFormattedText(LogLine* line) {
354
result.append("<table>");
356
result.append(labelMessageFormat(i18n("Date:"), line->getTime().toString(Qt::LocalDate)));
357
result.append(labelMessageFormat(i18n("Level:"), line->getLogLevel()->name));
359
result.append("</table>");
364
QString ItemFactory::createCupsAccessFormattedText(LogLine* line) {
367
QStringList& items=line->getItemList();
369
result.append("<table>");
371
result.append(labelMessageFormat(i18n("Date:"), line->getTime().toString(Qt::LocalDate)));
372
result.append(labelMessageFormat(i18n("Level:"), line->getLogLevel()->name));
373
result.append(labelMessageFormat(i18n("Hostname:"), items[0]));
374
result.append(labelMessageFormat(i18n("Identification:"), items[1]));
375
result.append(labelMessageFormat(i18n("Username:"), items[2]));
376
result.append(labelMessageFormat(i18n("HTTP Response:"), items[3]));
377
result.append(labelMessageFormat(i18n("Bytes Sent:"), items[4]));
379
result.append("</table>");
385
QString ItemFactory::createApacheFormattedText(LogLine* line) {
388
QStringList& items=line->getItemList();
390
result.append("<table>");
392
result.append(labelMessageFormat(i18n("Date:"), line->getTime().toString(Qt::LocalDate)));
393
result.append(labelMessageFormat(i18n("Level:"), line->getLogLevel()->name));
394
result.append(labelMessageFormat(i18n("Client:"), items[0]));
396
result.append("</table>");
401
QString ItemFactory::createApacheAccessFormattedText(LogLine* line) {
404
QStringList& items=line->getItemList();
406
result.append("<table>");
408
result.append(labelMessageFormat(i18n("Date:"), line->getTime().toString(Qt::LocalDate)));
409
result.append(labelMessageFormat(i18n("Level:"), line->getLogLevel()->name));
410
result.append(labelMessageFormat(i18n("Hostname:"), items[0]));
411
result.append(labelMessageFormat(i18n("Identification:"), items[1]));
412
result.append(labelMessageFormat(i18n("Username:"), items[2]));
413
result.append(labelMessageFormat(i18n("HTTP Response:"), items[3]));
414
result.append(labelMessageFormat(i18n("Bytes Sent:"), items[4]));
415
result.append(labelMessageFormat(i18n("Agent Identity:"), items[5]));
416
result.append(labelMessageFormat(i18n("HTTP Request:"), items[6]));
418
result.append("</table>");
424
QString ItemFactory::createSambaFormattedText(LogLine* line) {
427
QStringList& items=line->getItemList();
429
result.append("<table>");
431
result.append(labelMessageFormat(i18n("Date:"), line->getTime().toString(Qt::LocalDate)));
432
result.append(labelMessageFormat(i18n("Level:"), line->getLogLevel()->name));
433
result.append(labelMessageFormat(i18n("Source File:"), items[0]));
434
result.append(labelMessageFormat(i18n("Function:"), items[1]));
435
result.append(labelMessageFormat(i18n("Line:"), items[2]));
437
result.append("</table>");
443
QString ItemFactory::createXorgFormattedText(LogLine* line) {
444
//It uses the same formating than the Tooltip
445
return(createXorgToolTipText(line));
448
QString ItemFactory::createDefaultToolTipText(LogLine* line) {
451
result.append("<table>");
453
result.append(labelMessageFormat(i18n("Date:"), line->getTime().toString(Qt::LocalDate)));
454
result.append(labelMessageFormat(i18n("Level:"), line->getLogLevel()->name));
455
result.append(labelMessageFormat(i18n("Original file:"), line->getOriginalFile()));
457
result.append("</table>");
462
QString ItemFactory::createXorgToolTipText(LogLine* line) {
465
QStringList& items=line->getItemList();
467
result.append("<table>");
469
if (items[0].isEmpty())
470
result.append(labelMessageFormat(i18n("Type:"), i18n("none")));
472
result.append(labelMessageFormat(i18n("Type:"), items[0]));
474
result.append(labelMessageFormat(i18n("Original file:"), line->getOriginalFile()));
476
result.append("</table>");
484
QString ItemFactory::labelMessageFormat(QString label, QString value) {
485
return ("<tr><td align='right'><b><nobr>" + label + "</nobr></b></td><td>" + messageFormat(value) + "</td></tr>");
488
QString ItemFactory::messageFormat(QString& message) {
489
QString transformation(message);
490
transformation.replace(QRegExp("&"), "&");
491
transformation.replace(QRegExp("<"), "<");
492
transformation.replace(QRegExp(">"), ">");
493
return(transformation);