Logo Search packages:      
Sourcecode: vidalia version File versions

logtreeitem.cpp

/*
**  This file is part of Vidalia, and is subject to the license terms in the
**  LICENSE file, found in the top level directory of this distribution. If you
**  did not receive the LICENSE file with this file, you may obtain it from the
**  Vidalia source package distributed by the Vidalia Project at
**  http://www.vidalia-project.net/. No part of Vidalia, including this file,
**  may be copied, modified, propagated, or distributed except according to the
**  terms described in the LICENSE file.
*/

/*
** \file logtreeitem.cpp
** \version $Id: logtreeitem.cpp 2362 2008-02-29 04:30:11Z edmanm $
** \brief Item representing a single message in the message log
*/

#include <stringutil.h>

#include "logtreeitem.h"
#include "logtreewidget.h"

/** Defines the format used for displaying the date and time of a log message.*/
#define DATETIME_FMT  "MMM dd hh:mm:ss.zzz"

/* Column index values */
#define COL_TIME    LogTreeWidget::TimeColumn
#define COL_TYPE    LogTreeWidget::TypeColumn
#define COL_MESG    LogTreeWidget::MessageColumn
#define ROLE_TYPE   Qt::UserRole


/** Default constructor. */
LogTreeItem::LogTreeItem(LogEvent::Severity type, QString message, 
                         QDateTime timestamp)
: QTreeWidgetItem()
{
  static quint32 seqnum = 0;
  
  /* Set this message's sequence number */
  _seqnum = seqnum++;
  /* Set the item's log time */
  setTimestamp(timestamp);
  /* Set the item's severity and appropriate color. */
  setSeverity(type);
  /* Set the item's message text. */
  setMessage(message);
}

/** Returns a printable string representing the fields of this item. */
QString
LogTreeItem::toString() const
{
  return QString("%1 [%2] %3\n").arg(text(COL_TIME))
                                .arg(text(COL_TYPE))
                                .arg(text(COL_MESG).trimmed());
}

/** Sets the item's log time. */
void
LogTreeItem::setTimestamp(QDateTime timestamp)
{
  QString strtime = timestamp.toString(DATETIME_FMT);
  setText(COL_TIME, strtime);
  setToolTip(COL_TIME, strtime);
}

/** Sets the item's severity and the appropriate background color. */
void
LogTreeItem::setSeverity(LogEvent::Severity type)
{
  /* Change row and text color for serious warnings and errors. */
  if (type == LogEvent::Error) {
    /* Critical messages are red with white text. */
    for (int i = 0; i < 3; i++) {
      setBackgroundColor(i, Qt::red);
      setTextColor(i, Qt::white);
    }
  } else if (type == LogEvent::Warn) {
    /* Warning messages are yellow with black text. */
    for (int i = 0; i < 3; i++) {
      setBackgroundColor(i, Qt::yellow);
    }
  }
  
  setTextAlignment(COL_TYPE, Qt::AlignCenter);
  setText(COL_TYPE, LogEvent::severityToString(type));
  setData(COL_TYPE, ROLE_TYPE, (uint)type);
}

/** Sets the item's message text. */
void
LogTreeItem::setMessage(QString message)
{
  setText(COL_MESG, message);
  setToolTip(COL_MESG, string_wrap(message, 80, " ", "\r\n"));
}

/** Returns the severity associated with this log item. */
LogEvent::Severity
LogTreeItem::severity() const
{
  return (LogEvent::Severity)data(COL_TYPE, ROLE_TYPE).toUInt();
}

/** Returns the timestamp for this log message. */
QDateTime
LogTreeItem::timestamp() const
{
  return QDateTime::fromString(text(COL_TIME), DATETIME_FMT);
}

/** Returns the message for this log item. */
QString
LogTreeItem::message() const
{
  return text(COL_MESG);
}

/** Compares <b>other</b> to this log message item based on the current sort
 * column. */
bool
LogTreeItem::operator<(const QTreeWidgetItem &other) const
{
  LogTreeItem *that = (LogTreeItem *)&other;
  int sortColumn = (treeWidget() ? treeWidget()->sortColumn() : COL_TIME);
   
  switch (sortColumn) {
    case COL_TIME:
      /* Sort chronologically */
      return (this->_seqnum < that->_seqnum);
    case COL_TYPE:
      /* Sort by severity, then chronologically */
      if (this->severity() == that->severity()) {
        return (this->_seqnum < that->_seqnum);
      }
      /* The comparison is flipped because higher severities have 
       * lower numeric values */
      return (this->severity() > that->severity());
    default:
      /* Sort by message, then chronologically */
      QString thisMessage = this->message().toLower();
      QString thatMessage = that->message().toLower();
      
      if (thisMessage == thatMessage) {
        return (this->_seqnum < that->_seqnum);
      }
      return (thisMessage < thatMessage);
  }
  return QTreeWidgetItem::operator<(other);
}


Generated by  Doxygen 1.6.0   Back to index