Vidalia  0.3.1
LogTreeItem.cpp
Go to the documentation of this file.
1 /*
2 ** This file is part of Vidalia, and is subject to the license terms in the
3 ** LICENSE file, found in the top level directory of this distribution. If you
4 ** did not receive the LICENSE file with this file, you may obtain it from the
5 ** Vidalia source package distributed by the Vidalia Project at
6 ** http://www.torproject.org/projects/vidalia.html. No part of Vidalia,
7 ** including this file, may be copied, modified, propagated, or distributed
8 ** except according to the terms described in the LICENSE file.
9 */
10 
11 /*
12 ** \file LogTreeItem.cpp
13 ** \brief Item representing a single message in the message log
14 */
15 
16 #include "LogTreeItem.h"
17 #include "LogTreeWidget.h"
18 
19 #include "stringutil.h"
20 
21 /** Defines the format used for displaying the date and time of a log message.*/
22 #define DATETIME_FMT "MMM dd hh:mm:ss.zzz"
23 
24 /* Column index values */
25 #define COL_TIME LogTreeWidget::TimeColumn
26 #define COL_TYPE LogTreeWidget::TypeColumn
27 #define COL_MESG LogTreeWidget::MessageColumn
28 #define ROLE_TYPE Qt::UserRole
29 
30 
31 /** Default constructor. */
32 LogTreeItem::LogTreeItem(tc::Severity type, const QString &message,
33  const QDateTime &timestamp)
34  : QTreeWidgetItem()
35 {
36  static quint32 seqnum = 0;
37 
38  /* Set this message's sequence number */
39  _seqnum = seqnum++;
40  /* Set the item's log time */
41  setTimestamp(timestamp);
42  /* Set the item's severity and appropriate color. */
43  setSeverity(type);
44  /* Set the item's message text. */
45  setMessage(message);
46 }
47 
48 /** Returns a printable string representing the fields of this item. */
49 QString
51 {
52  return QString("%1 [%2] %3").arg(text(COL_TIME))
53  .arg(text(COL_TYPE))
54  .arg(text(COL_MESG).trimmed());
55 }
56 
57 /** Sets the item's log time. */
58 void
59 LogTreeItem::setTimestamp(const QDateTime &timestamp)
60 {
61  QString strtime = timestamp.toString(DATETIME_FMT);
62  setText(COL_TIME, strtime);
63  setToolTip(COL_TIME, strtime);
64 }
65 
66 /** Sets the item's severity and the appropriate background color. */
67 void
69 {
70  /* Change row and text color for serious warnings and errors. */
71  if (type == tc::ErrorSeverity) {
72  /* Critical messages are red with white text. */
73  for (int i = 0; i < 3; i++) {
74  setBackgroundColor(i, Qt::red);
75  setTextColor(i, Qt::white);
76  }
77  } else if (type == tc::WarnSeverity) {
78  /* Warning messages are yellow with black text. */
79  for (int i = 0; i < 3; i++) {
80  setBackgroundColor(i, Qt::yellow);
81  }
82  }
83 
84  setTextAlignment(COL_TYPE, Qt::AlignCenter);
85  setText(COL_TYPE, severityToString(type));
86  setData(COL_TYPE, ROLE_TYPE, (uint)type);
87 }
88 
89 /** Sets the item's message text. */
90 void
91 LogTreeItem::setMessage(const QString &message)
92 {
93  setText(COL_MESG, message);
94  setToolTip(COL_MESG, string_wrap(message, 80, " ", "\r\n"));
95 }
96 
97 /** Returns the severity associated with this log item. */
100 {
101  return (tc::Severity)data(COL_TYPE, ROLE_TYPE).toUInt();
102 }
103 
104 /** Returns the timestamp for this log message. */
105 QDateTime
107 {
108  return QDateTime::fromString(text(COL_TIME), DATETIME_FMT);
109 }
110 
111 /** Returns the message for this log item. */
112 QString
114 {
115  return text(COL_MESG);
116 }
117 
118 /** Converts a tc::Severity enum value to a localized string description. */
119 QString
121 {
122  QString str;
123  switch (severity) {
124  case tc::DebugSeverity: str = tr("Debug"); break;
125  case tc::InfoSeverity: str = tr("Info"); break;
126  case tc::NoticeSeverity: str = tr("Notice"); break;
127  case tc::WarnSeverity: str = tr("Warning"); break;
128  case tc::ErrorSeverity: str = tr("Error"); break;
129  default: str = tr("Unknown"); break;
130  }
131  return str;
132 }
133 
134 /** Compares <b>other</b> to this log message item based on the current sort
135  * column. */
136 bool
137 LogTreeItem::operator<(const QTreeWidgetItem &other) const
138 {
139  LogTreeItem *that = (LogTreeItem *)&other;
140  int sortColumn = (treeWidget() ? treeWidget()->sortColumn() : COL_TIME);
141 
142  switch (sortColumn) {
143  case COL_TIME:
144  /* Sort chronologically */
145  return (this->_seqnum < that->_seqnum);
146  case COL_TYPE:
147  /* Sort by severity, then chronologically */
148  if (this->severity() == that->severity()) {
149  return (this->_seqnum < that->_seqnum);
150  }
151  /* The comparison is flipped because higher severities have
152  * lower numeric values */
153  return (this->severity() > that->severity());
154  default:
155  /* Sort by message, then chronologically */
156  QString thisMessage = this->message().toLower();
157  QString thatMessage = that->message().toLower();
158 
159  if (thisMessage == thatMessage) {
160  return (this->_seqnum < that->_seqnum);
161  }
162  return (thisMessage < thatMessage);
163  }
164  return QTreeWidgetItem::operator<(other);
165 }
166