summaryrefslogtreecommitdiffstats
path: root/atlantik/client/eventlogwidget.cpp
blob: 49c297eb61eae0fc186a56cf64379c04afd250bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright (c) 2003-2004 Rob Kaper <cap@capsi.com>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License version 2.1 as published by the Free Software Foundation.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this library; see the file COPYING.LIB.  If not, write to
// the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
// Boston, MA 02110-1301, USA.

#include <iostream>

#include <tqheader.h>
#include <tqlayout.h>
#include <tqdatetime.h>

#include <tdelocale.h>
#include <tdelistview.h>
#include <kdialogbase.h>
#include <tdefiledialog.h>
#include <kiconloader.h>
#include <kpushbutton.h>
#include <kstringhandler.h>

#include "event.h"
#include "eventlogwidget.moc"

EventLog::EventLog()
{
}

void EventLog::addEvent(const TQString &description, const TQString &icon)
{
	Event *event = new Event(TQDateTime::currentDateTime(), description, icon);
	m_events.append(event);
	emit newEvent(event);
}

TQPtrList<Event> EventLog::events()
{
	return m_events;
}

EventLogWidget::EventLogWidget(EventLog *eventLog, TQWidget *parent, const char *name)
	: TQWidget(parent, name,
	  WType_Dialog | WStyle_Customize | WStyle_DialogBorder | WStyle_Title |
	  WStyle_Minimize | WStyle_ContextHelp )
{
	m_eventLog = eventLog;

	connect(m_eventLog, TQ_SIGNAL(newEvent(Event *)), this, TQ_SLOT(addEvent(Event *)));

	setCaption(i18n("Event Log"));

	TQVBoxLayout *listCompBox = new TQVBoxLayout(this, KDialog::marginHint());

	m_eventList = new TDEListView(this, "eventList");
	listCompBox->addWidget(m_eventList);

	m_eventList->addColumn(i18n("Date/Time"));
	m_eventList->addColumn(i18n("Description"));
	m_eventList->header()->setClickEnabled( false );

	TQHBoxLayout *actionBox = new TQHBoxLayout(this, 0, KDialog::spacingHint());
	listCompBox->addItem(actionBox);

	actionBox->addItem(new TQSpacerItem(20, 20, TQSizePolicy::Expanding, TQSizePolicy::Minimum));

	m_saveButton = new KPushButton(BarIcon("document-save", TDEIcon::SizeSmall), i18n("&Save As..."), this);
	actionBox->addWidget(m_saveButton);

	connect(m_saveButton, TQ_SIGNAL(clicked()), this, TQ_SLOT(save()));

	// Populate
	TQPtrList<Event> events = m_eventLog->events();
	for (TQPtrListIterator<Event> it( events ); (*it) ; ++it)
		addEvent( (*it) );
}

void EventLogWidget::addEvent(Event *event)
{
	// FIXME: allow a way to view non-squeezed message
	// FIXME: allow a way to show older messages

	if ( m_eventList->childCount() >= 25 )
		delete m_eventList->firstChild();

	TQString description = KStringHandler::rsqueeze( event->description(), 200 );
	TDEListViewItem *item = new TDEListViewItem(m_eventList, event->dateTime().toString("yyyy-MM-dd hh:mm:ss zzz"), description);
	if (event->icon().isEmpty())
		item->setPixmap(1, TQPixmap(SmallIcon("atlantik")));
	else
		item->setPixmap(1, TQPixmap(SmallIcon(event->icon())));

	m_eventList->ensureItemVisible(item);
}

void EventLogWidget::closeEvent(TQCloseEvent *e)
{
	e->accept();
}

void EventLogWidget::save()
{
	TQFile file( KFileDialog::getSaveFileName() );
	if ( file.open( IO_WriteOnly ) )
	{
		TQTextStream stream(&file);

		stream << i18n( "Atlantik log file, saved at %1." ).arg( TQDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss") ) << endl;

		TQPtrList<Event> events = m_eventLog->events();
		for (TQPtrListIterator<Event> it( events ); (*it) ; ++it)
			stream << (*it)->dateTime().toString("yyyy-MM-dd hh:mm:ss") << " " << (*it)->description() << endl;
		file.close();
	}
}