1/*
2 * wpa_gui - EventHistory class
3 * Copyright (c) 2005-2006, Jouni Malinen <j@w1.fi>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
11 *
12 * See README and COPYING for more details.
13 */
14
15#include <QHeaderView>
16#include <QScrollBar>
17
18#include "eventhistory.h"
19
20
21int EventListModel::rowCount(const QModelIndex &) const
22{
23	return msgList.count();
24}
25
26
27int EventListModel::columnCount(const QModelIndex &) const
28{
29	return 2;
30}
31
32
33QVariant EventListModel::data(const QModelIndex &index, int role) const
34{
35	if (!index.isValid())
36		return QVariant();
37
38        if (role == Qt::DisplayRole)
39		if (index.column() == 0) {
40			if (index.row() >= timeList.size())
41				return QVariant();
42			return timeList.at(index.row());
43		} else {
44			if (index.row() >= msgList.size())
45				return QVariant();
46			return msgList.at(index.row());
47		}
48        else
49		return QVariant();
50}
51
52
53QVariant EventListModel::headerData(int section, Qt::Orientation orientation,
54				    int role) const
55{
56	if (role != Qt::DisplayRole)
57		return QVariant();
58
59	if (orientation == Qt::Horizontal) {
60		switch (section) {
61		case 0:
62			return QString("Timestamp");
63		case 1:
64			return QString("Message");
65		default:
66			return QVariant();
67		}
68	} else
69		return QString("%1").arg(section);
70}
71
72
73void EventListModel::addEvent(QString time, QString msg)
74{
75	beginInsertRows(QModelIndex(), msgList.size(), msgList.size() + 1);
76	timeList << time;
77	msgList << msg;
78	endInsertRows();
79}
80
81
82EventHistory::EventHistory(QWidget *parent, const char *, bool, Qt::WFlags)
83	: QDialog(parent)
84{
85	setupUi(this);
86
87	connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
88
89	eventListView->setItemsExpandable(FALSE);
90	eventListView->setRootIsDecorated(FALSE);
91	elm = new EventListModel(parent);
92	eventListView->setModel(elm);
93}
94
95
96EventHistory::~EventHistory()
97{
98	destroy();
99	delete elm;
100}
101
102
103void EventHistory::languageChange()
104{
105	retranslateUi(this);
106}
107
108
109void EventHistory::addEvents(WpaMsgList msgs)
110{
111	WpaMsgList::iterator it;
112	for (it = msgs.begin(); it != msgs.end(); it++)
113		addEvent(*it);
114}
115
116
117void EventHistory::addEvent(WpaMsg msg)
118{
119	bool scroll = true;
120
121	if (eventListView->verticalScrollBar()->value() <
122	    eventListView->verticalScrollBar()->maximum())
123	    	scroll = false;
124
125	elm->addEvent(msg.getTimestamp().toString("yyyy-MM-dd hh:mm:ss.zzz"),
126		      msg.getMsg());
127
128	if (scroll)
129		eventListView->scrollToBottom();
130}
131