Proof-reading - fixed one usage of the i18n plural form (it wasn't doing before,...
[kdeadmin.git] / ksystemlog / src / lib / logLine.cpp
blob2f8af5cae640432e16481a40f59dc11f2ae814af
1 /***************************************************************************
2 * KSystemLog, a system log viewer tool *
3 * Copyright (C) 2007 by Nicolas Ternisien *
4 * nicolas.ternisien@gmail.com *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
20 ***************************************************************************/
22 #include "logLine.h"
24 #include <QStringList>
25 #include <QDateTime>
27 #include "logMode.h"
28 #include "logModeItemBuilder.h"
29 #include "logViewWidgetItem.h"
31 #include "logging.h"
32 #include "globals.h"
33 #include "ksystemlogConfig.h"
35 class LogLinePrivate {
36 public:
38 long internalId;
40 QDateTime time;
42 QStringList logItems;
44 QString originalFile;
46 LogLevel* logLevel;
48 LogMode* logMode;
50 bool recent;
52 LogViewWidgetItem* item;
56 LogLine::LogLine(
57 long internalId,
58 const QDateTime& dateTime,
59 const QStringList& logItems,
60 const QString& file,
61 LogLevel* logLevel,
62 LogMode* logMode) :
64 d(new LogLinePrivate()) {
66 d->internalId = internalId;
67 d->time=dateTime;
68 d->logItems = logItems;
69 d->originalFile = file;
70 d->logLevel = logLevel;
71 d->logMode = logMode;
73 //No linked item when constructs this LogLine
74 d->item = NULL;
76 //By default in newly created item has the recent state
77 setRecent(true);
81 LogLine::~LogLine() {
82 //logLevel is managed by Globals
84 //item is managed by LogMode
85 //itemBuilder is managed by LogMode
87 delete d;
91 LogMode* LogLine::logMode() const {
92 return d->logMode;
95 void LogLine::setLogMode(LogMode* logMode) {
96 d->logMode = logMode;
99 bool LogLine::equals(const LogLine& other) const {
100 if (logMode()->id() != other.logMode()->id())
101 return false;
103 if (time() != other.time())
104 return false;
106 if (logLevel()->id() != other.logLevel()->id())
107 return false;
109 if (d->logItems != other.d->logItems)
110 return false;
112 return true;
115 LogLevel* LogLine::logLevel() const {
116 return d->logLevel;
119 void LogLine::setLogLevel(LogLevel* level) {
120 d->logLevel=level;
123 QDateTime LogLine::time() const {
124 return d->time;
127 void LogLine::setLogItems(const QStringList& logItems) {
128 d->logItems = logItems;
131 QStringList LogLine::logItems() const {
132 return d->logItems;
135 QString LogLine::sourceFileName() const {
136 return d->originalFile;
139 bool LogLine::isOlderThan(const LogLine& other) const {
140 if (d->time == other.time())
141 return d->internalId < other.internalId();
143 return d->time < other.time();
146 bool LogLine::isNewerThan(const LogLine& other) const {
147 if (d->time == other.time())
148 return d->internalId > other.internalId();
150 return d->time > other.time();
153 bool LogLine::isSameTime(const LogLine& other) const {
154 return d->time==other.time();
157 long LogLine::internalId() const {
158 return d->internalId;
161 void LogLine::setRecent(bool recent) {
162 d->recent = recent;
164 if (d->item!=NULL) {
165 QFont currentFont = d->item->font(d->item->columnCount()-1);
167 //We avoid doing the same process
168 if (d->recent != currentFont.bold()) {
169 currentFont.setBold(recent);
170 d->item->setFont(d->item->columnCount()-1, currentFont);
176 QString LogLine::exportToText() const {
178 QString exporting;
180 if (d->item == NULL) {
181 logError() << "Trying to export text from NULL item" << endl;
182 return exporting;
185 for (int i=0; i < d->item->columnCount(); ++i) {
186 if (i>0)
187 exporting.append('\t');
189 exporting.append(d->item->text(i));
192 return exporting;
195 QString LogLine::formattedText() {
196 return d->logMode->itemBuilder()->createFormattedText(this);
199 void LogLine::setItem(LogViewWidgetItem* item) {
200 d->item = item;
202 initializeItem();
205 void LogLine::initializeItem() {
206 d->logMode->itemBuilder()->prepareItem(d->item);
208 //Call methods that change the look of the item
209 setRecent(d->recent);
211 if (KSystemLogConfig::colorizeLogLines()) {
212 //Last column index = d->logItems.count() = (d->logItems.count() -1) +1 (the date column)
213 d->item->setForeground(d->logItems.count(), QBrush(d->logLevel->color()));
216 d->item->toggleToolTip(KSystemLogConfig::tooltipEnabled());