summaryrefslogtreecommitdiffstats
path: root/ksystemlog/src/view.cpp
blob: e786a9e16e8958470fe719019f519853878d36c0 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/***************************************************************************
 *   Copyright (C) 2005 by Nicolas Ternisien                               *
 *   nicolas.ternisien@gmail.com                                           *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program 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 General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.             *
 ***************************************************************************/

//TQt includes
#include <tqpainter.h>
#include <tqlayout.h>
#include <tqhbox.h>
#include <tqlabel.h>
#include <tqstring.h>
#include <tqwhatsthis.h>
#include <tqtooltip.h>

#include <tqlistview.h>

#include <tqpixmap.h>

//KDE includes
#include <kurl.h>

#include <ktrader.h>
#include <klibloader.h>
#include <tdemessagebox.h>
#include <krun.h>
#include <tdelocale.h>
#include <kiconloader.h>
#include <kdebug.h>
//For compatibility with old versions of KDE
#include <tdeversion.h>

//Project includes
#include "view.h"

#include "logListItem.h"
#include "logLine.h"
#include "viewToolTip.h"

#include "ksystemlogConfig.h"
#include "ksystemlog.h"


View::View(TQWidget *parent) :
	DCOPObject("KSystemLogInterface"),
	TQWidget(parent),
	logManager(NULL),
	table(NULL),
	columns(NULL),
	firstLoad(true)
	{

	// setup our layout manager to automatically add our widgets
	TQVBoxLayout* topLayout = new TQVBoxLayout(this);
	topLayout->setAutoAdd(true);
	
	
#if defined(TDE_MAKE_VERSION) && TDE_VERSION >= TDE_MAKE_VERSION(3,3,0)
	filterBar=new TQHBox(this);
	filterBar->setSpacing(5);
	filterBar->setSizePolicy( TQSizePolicy( TQSizePolicy::Minimum, TQSizePolicy::Fixed ) );
	
	
	clearSearch=new TDEToolBarButton( TQApplication::reverseLayout() ? "clear_left" : "locationbar_erase", 0, filterBar);
	clearSearch->setText(i18n("Clear the filter"));
	
	TQWhatsThis::add(clearSearch, i18n("This button clears the filter in one click."));
	TQToolTip::add(clearSearch, i18n("Clear the filter"));
	
	
	TQLabel* label=new TQLabel(i18n("Filter:"), filterBar);
	
	search=new LogLineFilter(filterBar, (TDEListView*)NULL);
	label->setBuddy(search);
	
	TQWhatsThis::add(search, i18n("Allows you to select only list items that match the content of this text."));
	TQToolTip::add(search, i18n("Type your item filter here"));
	
	label=new TQLabel(i18n("Column:"), filterBar);
	
	this->initSearchFilter(filterBar);
	
	label->setBuddy(searchFilter);
	
	toggleFilterBar();
#endif
	
	
	this->initLogList();


#if defined(TDE_MAKE_VERSION) && TDE_VERSION >= TDE_MAKE_VERSION(3,3,0)
	//We initialize the table after, after the initialization of the table
	search->setListView(table);
	
	//Connect the pressed signal of the clear button to the clear slot of "clear"
	connect(clearSearch, TQT_SIGNAL(pressed()), search, TQT_SLOT(clear()));
#endif

}


View::~View() {
#if defined(TDE_MAKE_VERSION) && (TDE_VERSION >= TDE_MAKE_VERSION(3,3,0))
	delete clearSearch;
	
	delete search;
		
	delete searchFilter;

	delete filterBar;
#endif
	
	kdDebug() << "Destroying View" << endl;
}

void View::saveConfig() {
	kdDebug() << "Saving View Layout..." << endl;
	
	KSystemLogConfig* configXT=KSystemLogConfig::self();
	TDEConfig* config=configXT->config();
	TQString group="List";
	group.append(logManager->getIndex());
	
	table->saveLayout(config, group);

}

void View::setLogManager(LogManager* manager) {
	logManager=manager;
}

//TODO Deprecate this method as soon as possible
void View::openURL(TQString url) {
	kdDebug() << "DCOP Interface : " << url << endl;
}

bool View::isTooltipEnabled() {
	return(logManager->isTooltipEnabled());
}

void View::toggleFilterBar() {
#if defined(TDE_MAKE_VERSION) && TDE_VERSION >= TDE_MAKE_VERSION(3,3,0)
	//The newly created bar is only displayed if the config file allow it
	if (KSystemLogConfig::toggleFilterBar()==true)
		filterBar->show();
	else
		filterBar->hide();
#endif
}



/**
 * Delete the "count" first items
 */
void View::deleteOldItems(int count) {
	TQListViewItem* item;
	for(int i=0; i<count; i++) {
		item=table->lastItem();
		table->takeItem(item);
	}
}

int View::getItemCount() {

	int count=0;
	
	if (logManager->getGroupBy()==NO_GROUP_BY) {
		count=table->childCount();
	}
	else {
		TQPtrList<TQListViewItem> lst;
		TQListViewItemIterator it(table);
		while (it.current()!=NULL) {
			count+=it.current()->childCount();
			++it;
		}
	}
		
	return(count);
}



void View::setColumns(LogViewColumns* list) {
	kdDebug() << "Change columns" << endl;
	
	columns=list;

	updateList();

	//TODO Maybe with the deleting of the config group, this boolean is useless
	if (firstLoad==true) {
		KSystemLogConfig* configXT=KSystemLogConfig::self();
		TQString group="List";
		group.append(logManager->getIndex());
		
		//We first restore the layout from the config
		table->restoreLayout(configXT->config(), group);
		
		//Then we delete it from config, to avoid reloading problem
		configXT->config()->deleteGroup(group);
		
		firstLoad=false;
	}
	
	updateSearchFilter();
	
}

void View::setFirstItemVisible() {
	table->ensureItemVisible(table->firstChild());
}

void View::setLastItemVisible() {
	table->ensureItemVisible(table->lastItem());
}

void View::setFirstItemSelected() {
	table->setCurrentItem(table->firstChild());
	table->setSelected(table->firstChild(), true);
	table->ensureItemVisible(table->firstChild());
}

void View::setLastItemSelected() {
	table->setCurrentItem(table->lastItem());
	table->setSelected(table->lastItem(), true);
	table->ensureItemVisible(table->lastItem());
}

void View::updateSearchFilter() {
#if defined(TDE_MAKE_VERSION) && TDE_VERSION >= TDE_MAKE_VERSION(3,3,0)
	//We first delete all items
	int count=searchFilter->count() - 1;
	while (count>=0) {
		searchFilter->removeItem(count);
		count--;
	}
	
	//Then we insert the default items
	searchFilter->insertItem(i18n("All"));
	
	LogViewColumns::Iterator it = columns->begin();
	
	LogViewColumn* column;
	while(it!=columns->end()) {
		column=*it;
		if (column->isFiltred==true)
			searchFilter->insertItem(column->columnName);
		it++;
	}

	searchFilter->setCurrentItem(0);
#endif
}


void View::updateList() {
	
	//First, delete all current columns
	int count=table->columns() - 1;
	while (count>=0) {
		table->removeColumn(count);
		count--;
	}
	

	LogViewColumns::Iterator it = columns->begin();
	
	LogViewColumn* column;
	//Add all columns of the columns list object
	while(it!=columns->end()) {
		column=*it;
		table->addColumn(column->columnName, -1);
		it++;
	}
	
	//Adjust them to the size of the current maximum item
	int i=0;
	while (i<table->columns()) {
		table->adjustColumn(i);
		i++;
	}
	
	//TODO Try to reduce size of first column with a 
}

void View::initLogList() {
	
	table=new TDEListView(this, "log_list");

	TQWhatsThis::add(table, i18n("<qt><p>This is the main view of KSystemLog. It displays the last lines of the selected log. Please see the documentation to discovers the meaning of each icons and existing log.</p><p>Log lines in <b>bold</b> are the last added to the list.</p></qt>"));
	
	table->addColumn("Message");
	
	table->setSorting(0, true);

	//table->setRootIsDecorated(true);
	table->setShowSortIndicator(true);
	table->setAllColumnsShowFocus(true);
	
	//This method is not implemented for the moment by TDEListView class
	table->setAutoOpen(false);
	
	//This is buggy but it's not my fault (I hope :-)
	table->setFullWidth(true);

	//TODO Find a color from TQColorGroup
	TQColor* alternate=new TQColor(246,246,255);
	table->setAlternateBackground(*alternate);
	
	table->setSelectionModeExt(TDEListView::Extended);
	
	toolTip=new ViewToolTip(this->getLogList()->viewport(), this);

}


LogListItem* View::getFirstSelectedItem() {
	TQListViewItemIterator it(table, TQListViewItemIterator::Selected);
	
	//Returns the first selected item or NULL is there is no item selected
	return(static_cast<LogListItem*> (it.current()));
}

LogListItem* View::getLastSelectedItem() {
	TQListViewItemIterator it(table, TQListViewItemIterator::Selected);
	
	TQListViewItem* item=NULL;
	while (it.current()) {
		item=it.current();
		
		it++;
	}
	
	//Returns the last selected item or NULL is there is no item selected
	return(static_cast<LogListItem*> (item));
}


void View::setSortEnabled(bool enabled) {
	if (enabled==true)
		table->setSorting(table->columns()+1);
	else
		table->setSorting(-1);
}

void View::initSearchFilter(TQWidget* filterBox) {
#if defined(TDE_MAKE_VERSION) && TDE_VERSION >= TDE_MAKE_VERSION(3,3,0)
	searchFilter=new TQComboBox(filterBox);
		
	TQWhatsThis::add(searchFilter, i18n("<qt>Allows you to apply the item filter only on the specified column here. \"<i>All</i>\" column means no specific filter.</qt>"));
	TQToolTip::add(searchFilter, i18n("Choose the filtered column here"));
	
	searchFilter->insertItem(i18n("All"));
	
	//TODO 0 is not a very good value... Improve that. and of course, try to find a better solution
	searchFilter->setMinimumSize(70, 0);

	connect(searchFilter, TQT_SIGNAL(activated(int)), search, TQT_SLOT(setFocus()));
	connect(searchFilter, TQT_SIGNAL(activated(int)), this, TQT_SLOT(changeColumnFilter(int)));
	connect(searchFilter, TQT_SIGNAL(activated(int)), search, TQT_SLOT(updateSearch()));
#endif

}


void View::addElement(TQStringList* entries, TQPixmap* icon) {
	TQStringList::Iterator it = entries->begin();
	
	TDEListViewItem* item=new TDEListViewItem(table, *(it++), *(it++), *(it++), *(it++), *(it++));
	if (icon!=NULL)
		item->setPixmap(0, *icon);
	
}

void View::addElementAtEnd(TQStringList* entries, TQPixmap* icon) {
	TQStringList::Iterator it = entries->begin();
	
	TDEListViewItem* item=new TDEListViewItem(table, table->lastItem(), *(it++), *(it++), *(it++), *(it++), *(it++));
	if (icon!=NULL)
		item->setPixmap(0, *icon);
}

TDEListView* View::getLogList() {
	return(table);
}


void View::changeColumnFilter(int column) {
#if defined(TDE_MAKE_VERSION) && TDE_VERSION >= TDE_MAKE_VERSION(3,3,0)
	TQValueList<int> filterColumns;
	
	//The user select all columns
	if (column==0) {
		search->setSearchColumns(filterColumns);
	}
	else {
		TQString columnName=searchFilter->currentText();
		
		LogViewColumns::Iterator it;
		LogViewColumn* column;
		int position=0;
		for(it=columns->begin(); it!=columns->end(); it++) {
			column=*it;
			if (column->columnName==columnName)
				break;
				
			position++;
		}
		
		filterColumns.append(position);

		search->setSearchColumns(filterColumns);
	}
	
	//search->updateSearch();
#endif
}



void View::print(TQPainter* /*p*/, int /*height*/, int /*width*/) {
	//Print log files here
}

void View::slotOnURL(const TQString& url) {
	emit changeStatusbar(url);
}

void View::slotSetTitle(const TQString& title) {
	emit changeCaption(title);
}

void View::clearLogList( ) {
	table->clear();
}



#include "view.moc"