Vidalia 0.2.15
|
00001 /* 00002 ** This file is part of Vidalia, and is subject to the license terms in the 00003 ** LICENSE file, found in the top level directory of this distribution. If you 00004 ** did not receive the LICENSE file with this file, you may obtain it from the 00005 ** Vidalia source package distributed by the Vidalia Project at 00006 ** http://www.torproject.org/projects/vidalia.html. No part of Vidalia, 00007 ** including this file, may be copied, modified, propagated, or distributed 00008 ** except according to the terms described in the LICENSE file. 00009 */ 00010 00011 /* 00012 ** \file LogTreeItem.cpp 00013 ** \brief Item representing a single message in the message log 00014 */ 00015 00016 #include "LogTreeItem.h" 00017 #include "LogTreeWidget.h" 00018 00019 #include "stringutil.h" 00020 00021 /** Defines the format used for displaying the date and time of a log message.*/ 00022 #define DATETIME_FMT "MMM dd hh:mm:ss.zzz" 00023 00024 /* Column index values */ 00025 #define COL_TIME LogTreeWidget::TimeColumn 00026 #define COL_TYPE LogTreeWidget::TypeColumn 00027 #define COL_MESG LogTreeWidget::MessageColumn 00028 #define ROLE_TYPE Qt::UserRole 00029 00030 00031 /** Default constructor. */ 00032 LogTreeItem::LogTreeItem(tc::Severity type, const QString &message, 00033 const QDateTime ×tamp) 00034 : QTreeWidgetItem() 00035 { 00036 static quint32 seqnum = 0; 00037 00038 /* Set this message's sequence number */ 00039 _seqnum = seqnum++; 00040 /* Set the item's log time */ 00041 setTimestamp(timestamp); 00042 /* Set the item's severity and appropriate color. */ 00043 setSeverity(type); 00044 /* Set the item's message text. */ 00045 setMessage(message); 00046 } 00047 00048 /** Returns a printable string representing the fields of this item. */ 00049 QString 00050 LogTreeItem::toString() const 00051 { 00052 return QString("%1 [%2] %3").arg(text(COL_TIME)) 00053 .arg(text(COL_TYPE)) 00054 .arg(text(COL_MESG).trimmed()); 00055 } 00056 00057 /** Sets the item's log time. */ 00058 void 00059 LogTreeItem::setTimestamp(const QDateTime ×tamp) 00060 { 00061 QString strtime = timestamp.toString(DATETIME_FMT); 00062 setText(COL_TIME, strtime); 00063 setToolTip(COL_TIME, strtime); 00064 } 00065 00066 /** Sets the item's severity and the appropriate background color. */ 00067 void 00068 LogTreeItem::setSeverity(tc::Severity type) 00069 { 00070 /* Change row and text color for serious warnings and errors. */ 00071 if (type == tc::ErrorSeverity) { 00072 /* Critical messages are red with white text. */ 00073 for (int i = 0; i < 3; i++) { 00074 setBackgroundColor(i, Qt::red); 00075 setTextColor(i, Qt::white); 00076 } 00077 } else if (type == tc::WarnSeverity) { 00078 /* Warning messages are yellow with black text. */ 00079 for (int i = 0; i < 3; i++) { 00080 setBackgroundColor(i, Qt::yellow); 00081 } 00082 } 00083 00084 setTextAlignment(COL_TYPE, Qt::AlignCenter); 00085 setText(COL_TYPE, severityToString(type)); 00086 setData(COL_TYPE, ROLE_TYPE, (uint)type); 00087 } 00088 00089 /** Sets the item's message text. */ 00090 void 00091 LogTreeItem::setMessage(const QString &message) 00092 { 00093 setText(COL_MESG, message); 00094 setToolTip(COL_MESG, string_wrap(message, 80, " ", "\r\n")); 00095 } 00096 00097 /** Returns the severity associated with this log item. */ 00098 tc::Severity 00099 LogTreeItem::severity() const 00100 { 00101 return (tc::Severity)data(COL_TYPE, ROLE_TYPE).toUInt(); 00102 } 00103 00104 /** Returns the timestamp for this log message. */ 00105 QDateTime 00106 LogTreeItem::timestamp() const 00107 { 00108 return QDateTime::fromString(text(COL_TIME), DATETIME_FMT); 00109 } 00110 00111 /** Returns the message for this log item. */ 00112 QString 00113 LogTreeItem::message() const 00114 { 00115 return text(COL_MESG); 00116 } 00117 00118 /** Converts a tc::Severity enum value to a localized string description. */ 00119 QString 00120 LogTreeItem::severityToString(tc::Severity severity) 00121 { 00122 QString str; 00123 switch (severity) { 00124 case tc::DebugSeverity: str = tr("Debug"); break; 00125 case tc::InfoSeverity: str = tr("Info"); break; 00126 case tc::NoticeSeverity: str = tr("Notice"); break; 00127 case tc::WarnSeverity: str = tr("Warning"); break; 00128 case tc::ErrorSeverity: str = tr("Error"); break; 00129 default: str = tr("Unknown"); break; 00130 } 00131 return str; 00132 } 00133 00134 /** Compares <b>other</b> to this log message item based on the current sort 00135 * column. */ 00136 bool 00137 LogTreeItem::operator<(const QTreeWidgetItem &other) const 00138 { 00139 LogTreeItem *that = (LogTreeItem *)&other; 00140 int sortColumn = (treeWidget() ? treeWidget()->sortColumn() : COL_TIME); 00141 00142 switch (sortColumn) { 00143 case COL_TIME: 00144 /* Sort chronologically */ 00145 return (this->_seqnum < that->_seqnum); 00146 case COL_TYPE: 00147 /* Sort by severity, then chronologically */ 00148 if (this->severity() == that->severity()) { 00149 return (this->_seqnum < that->_seqnum); 00150 } 00151 /* The comparison is flipped because higher severities have 00152 * lower numeric values */ 00153 return (this->severity() > that->severity()); 00154 default: 00155 /* Sort by message, then chronologically */ 00156 QString thisMessage = this->message().toLower(); 00157 QString thatMessage = that->message().toLower(); 00158 00159 if (thisMessage == thatMessage) { 00160 return (this->_seqnum < that->_seqnum); 00161 } 00162 return (thisMessage < thatMessage); 00163 } 00164 return QTreeWidgetItem::operator<(other); 00165 } 00166