LogTreeItem.cpp

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