summaryrefslogtreecommitdiffstats
path: root/src/modules/logview/logviewmdiwindow.cpp
blob: 97d50355ce226354fd1c215e7a3e231f35f0b864 (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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
//
//   File : logviewmdiwindow.cpp
//   Creation date : Tue Apr 23 2002 18:08:22 by Juanjo �varez
//
//   This file is part of the KVirc irc client distribution
//   Copyright (C) 1999-2000 Szymon Stefanek (pragma at kvirc dot net)
//
//   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 opinion) 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.
//

#include "logviewmdiwindow.h"
#include "logviewwidget.h"
#include "kvi_iconmanager.h"
#include "kvi_locale.h"
#include "kvi_module.h"
#include "kvi_options.h"
#include "kvi_frame.h"
#include "kvi_ircview.h"
#include "kvi_qstring.h"
#include "kvi_qcstring.h"
#include "kvi_app.h"
#include "kvi_fileutils.h"
#ifdef COMPILE_USE_QT4
	#include <tq3progressdialog.h>
#else
	#include <tqprogressdialog.h> 
#endif
#include "kvi_valuelist.h"
#include "kvi_accel.h"
#include <tqpixmap.h>
#include <tqsplitter.h>
#include <tqtoolbutton.h>
#include "kvi_tal_listview.h"
#include <tqfileinfo.h>
#include <tqdir.h>
#include "kvi_tal_popupmenu.h"
#include <tqcursor.h>
#include <tqlayout.h>
#include <tqpushbutton.h>



#ifdef COMPILE_ZLIB_SUPPORT
	#include <zlib.h>
#endif
#include <tqtextcodec.h>
#include "kvi_styled_controls.h"
#include <tqdatetimeedit.h>
#include <tqlineedit.h>
#include <tqlabel.h>

extern KviLogViewMDIWindow * g_pLogViewWindow;

KviLogViewMDIWindow::KviLogViewMDIWindow(KviModuleExtensionDescriptor * d,KviFrame * lpFrm)
: KviWindow(KVI_WINDOW_TYPE_LOGVIEW,lpFrm,"logview"), KviModuleExtension(d)
{
	g_pLogViewWindow = this;
//	m_pLogViewWidget = new KviLogViewWidget(this);
	#ifdef COMPILE_USE_QT4
	m_pSplitter = new TQSplitter(Qt::Horizontal,this,"main_splitter");
#else
	m_pSplitter = new TQSplitter(Qt::Horizontal,this,"main_splitter");
#endif
	m_pTabWidget = new TQTabWidget(m_pSplitter);
	
	m_pIndexTab  = new KviTalVBox(m_pTabWidget);
	m_pTabWidget->insertTab(m_pIndexTab,__tr2qs_ctx("Index","logview"));
	m_pListView = new KviTalListView(m_pIndexTab);
	m_pListView->addColumn(__tr2qs_ctx("Log File","logview"),135);
	m_pListView->setColumnWidthMode(0,KviTalListView::Maximum);
	m_pListView->setAllColumnsShowFocus(true);
	m_pListView->setMultiSelection(false);
	m_pListView->setShowSortIndicator(true);
	m_pListView->setRootIsDecorated(true);
	connect(m_pListView,TQT_SIGNAL(selectionChanged(KviTalListViewItem *)),this,TQT_SLOT(itemSelected(KviTalListViewItem *)));
	connect(m_pListView,TQT_SIGNAL(rightButtonClicked ( KviTalListViewItem * , const TQPoint &, int )),this,TQT_SLOT(rightButtonClicked ( KviTalListViewItem * , const TQPoint &, int )));
	
	m_pSearchTab  = new TQWidget(m_pTabWidget);
	m_pTabWidget->insertTab(m_pSearchTab,__tr2qs_ctx("Filter","logview"));

	TQGridLayout *tqlayout = new TQGridLayout(m_pSearchTab,10,2,3,5);
	
	m_pShowChannelsCheck = new KviStyledCheckBox(__tr2qs_ctx("Show channel logs","logview"),m_pSearchTab);
	m_pShowChannelsCheck->setChecked(true);
	tqlayout->addMultiCellWidget(m_pShowChannelsCheck,0,0,0,1);
	
	m_pShowQueryesCheck  = new KviStyledCheckBox(__tr2qs_ctx("Show query logs","logview"),m_pSearchTab);
	m_pShowQueryesCheck->setChecked(true);
	tqlayout->addMultiCellWidget(m_pShowQueryesCheck,1,1,0,1);
	
	m_pShowConsolesCheck = new KviStyledCheckBox(__tr2qs_ctx("Show console logs","logview"),m_pSearchTab);
	m_pShowConsolesCheck->setChecked(true);
	tqlayout->addMultiCellWidget(m_pShowConsolesCheck,2,2,0,1);

	m_pShowDccChatCheck  = new KviStyledCheckBox(__tr2qs_ctx("Show DCC chat logs","logview"),m_pSearchTab);
	m_pShowDccChatCheck->setChecked(true);
	tqlayout->addMultiCellWidget(m_pShowDccChatCheck,3,3,0,1);
	
	m_pShowOtherCheck   = new KviStyledCheckBox(__tr2qs_ctx("Show other logs","logview"),m_pSearchTab);
	m_pShowOtherCheck->setChecked(true);
	tqlayout->addMultiCellWidget(m_pShowOtherCheck,4,4,0,1);
	
	TQLabel *l;
	l = new TQLabel(__tr2qs_ctx("Contents filter","logview"),m_pSearchTab);
	tqlayout->addMultiCellWidget(l,5,5,0,1);

	l = new TQLabel(__tr2qs_ctx("Log name mask:","logview"),m_pSearchTab);
	m_pFileNameMask = new TQLineEdit(m_pSearchTab);
	connect(m_pFileNameMask,TQT_SIGNAL(returnPressed()),this,TQT_SLOT(applyFilter()));
	tqlayout->addWidget(l,6,0);
	tqlayout->addWidget(m_pFileNameMask,6,1);

	l = new TQLabel(__tr2qs_ctx("Log contents mask:","logview"),m_pSearchTab);
	m_pContentsMask = new TQLineEdit(m_pSearchTab);
	connect(m_pContentsMask,TQT_SIGNAL(returnPressed()),this,TQT_SLOT(applyFilter()));
	tqlayout->addWidget(l,7,0);
	tqlayout->addWidget(m_pContentsMask,7,1);

	m_pEnableFromFilter = new KviStyledCheckBox(__tr2qs_ctx("Only older than","logview"),m_pSearchTab);
	m_pFromDateEdit = new TQDateEdit(m_pSearchTab);
	m_pFromDateEdit->setDate(TQDate::tqcurrentDate());
	tqlayout->addWidget(m_pEnableFromFilter,8,0);
	tqlayout->addWidget(m_pFromDateEdit,8,1);
	connect(m_pEnableFromFilter,TQT_SIGNAL(toggled(bool)),m_pFromDateEdit,TQT_SLOT(setEnabled(bool)));
	m_pFromDateEdit->setEnabled(false);

	m_pEnableToFilter = new KviStyledCheckBox(__tr2qs_ctx("Only newier than","logview"),m_pSearchTab);
	m_pToDateEdit = new TQDateEdit(m_pSearchTab);
	m_pToDateEdit->setDate(TQDate::tqcurrentDate());
	tqlayout->addWidget(m_pEnableToFilter,9,0);
	tqlayout->addWidget(m_pToDateEdit,9,1);
	connect(m_pEnableToFilter,TQT_SIGNAL(toggled(bool)),m_pToDateEdit,TQT_SLOT(setEnabled(bool)));
	m_pToDateEdit->setEnabled(false);

	TQPushButton *pb = new TQPushButton(__tr2qs_ctx("Apply filter","logview"),m_pSearchTab);
	connect(pb,TQT_SIGNAL(clicked()),this,TQT_SLOT(applyFilter()));
	tqlayout->addWidget(pb,10,1);

	TQWidget *w = new TQWidget(m_pSearchTab);
	w->tqsetSizePolicy(TQSizePolicy::Ignored,TQSizePolicy::Ignored);
	tqlayout->addWidget(w,11,1);

	m_pIrcView = new KviIrcView(m_pSplitter,g_pFrame,this);
#ifdef COMPILE_USE_QT4
	m_pIrcView->setFocusPolicy(TTQ_ClickFocus);
#else
		m_pIrcView->setFocusPolicy(TQ_ClickFocus);
#endif
	KviValueList<int> li;
	li.append(110);
	li.append(width()-110);
	m_pSplitter->setSizes(li);
	
	g_pApp->getLocalKvircDirectory(m_szLogDirectory,KviApp::Log);
	KviTQString::ensureLastCharIs(m_szLogDirectory,'/'); // Does this work on Windows?
	
	cacheFileList();
	setupItemList();
	KviAccel *a = new KviAccel( this );
        a->connectItem( a->insertItem(TQt::Key_F+TQt::CTRL),
                        m_pIrcView,
                        TQT_SLOT(toggleToolWidget()) );
}



KviLogViewMDIWindow::~KviLogViewMDIWindow()
{
    g_pLogViewWindow = 0;
}

void KviLogViewMDIWindow::applyFilter()
{
	setupItemList();
}


TQPixmap * KviLogViewMDIWindow::myIconPtr()
{
    return g_pIconManager->getSmallIcon(KVI_SMALLICON_LOG);
}


void KviLogViewMDIWindow::resizeEvent(TQResizeEvent *e)
{
	m_pSplitter->setGeometry(0,0,width(),height());
}

void KviLogViewMDIWindow::fillCaptionBuffers()
{
	m_szPlainTextCaption = __tr2qs_ctx("Log Viewer","logview");

	m_szHtmlActiveCaption = TQString("<nobr><font color=\"%1\"><b>").tqarg(TQString(KVI_OPTION_COLOR(KviOption_colorCaptionTextActive).name()).ascii());
	m_szHtmlActiveCaption.append(m_szPlainTextCaption);
	m_szHtmlActiveCaption.append("</b></font></nobr>");

	m_szHtmlInactiveCaption = TQString("<nobr><font color=\"%1\"><b>").tqarg(TQString(KVI_OPTION_COLOR(KviOption_colorCaptionTextInactive).name()).ascii());
	m_szHtmlInactiveCaption.append(m_szPlainTextCaption);
	m_szHtmlInactiveCaption.append("</b></font></nobr>");
}

void KviLogViewMDIWindow::die()
{
	close();
}

TQSize KviLogViewMDIWindow::tqsizeHint() const
{
	TQSize ret(m_pSplitter->tqsizeHint().width(),m_pIrcView->tqsizeHint().height());
	return ret;
}

void KviLogViewMDIWindow::setupItemList()
{
	m_pListView->clear();
	KviLogFile *pFile;
	//m_logList.begin();
	KviLogListViewItem *pLastCategory=0;
	KviLogListViewItemFolder *pLastGroupItem;
	TQString szLastGroup;
	TQString szCurGroup;
	const bool bShowChannel=m_pShowChannelsCheck->isChecked();
	const bool bShowQuery=m_pShowQueryesCheck->isChecked();
	const bool bShowConsole=m_pShowConsolesCheck->isChecked();
	const bool bShowOther=m_pShowOtherCheck->isChecked();
	const bool bShowDccChat=m_pShowDccChatCheck->isChecked();

	const bool filterFromDate=m_pEnableFromFilter->isChecked();
	const bool filterToDate=m_pEnableToFilter->isChecked();

	const TQString nameFilterText = m_pFileNameMask->text();
	const bool enableNameFilter = !nameFilterText.isEmpty();

	const TQString contentFilterText = m_pContentsMask->text();
	const bool enableContentFilter = !contentFilterText.isEmpty();

	TQDate fromDate = m_pFromDateEdit->date();
	TQDate toDate   = m_pToDateEdit->date();

	TQString textBuffer;
#ifdef COMPILE_USE_QT4
	Q3ProgressDialog progress( __tr2qs_ctx("Filtering files...","logview"),
		__tr2qs_ctx("Abort filtering","logview"), m_logList.count(),
                          this, "progress", TRUE );
#else
	TQProgressDialog progress( __tr2qs_ctx("Filtering files...","logview"),
		__tr2qs_ctx("Abort filtering","logview"), m_logList.count(),
                          this, "progress", TRUE );
#endif


	int i=0;
	for(pFile=m_logList.first();pFile;pFile=m_logList.next())
	{
		progress.setProgress( i );
		i++;
		g_pApp->processEvents();

		if ( progress.wasCanceled() )
			break;

		if(pFile->type()==KviLogFile::Channel && !bShowChannel)
			continue;
		if(pFile->type()==KviLogFile::Console && !bShowConsole)
			continue;
		if(pFile->type()==KviLogFile::DccChat && !bShowDccChat)
			continue;
		if(pFile->type()==KviLogFile::Other && !bShowOther)
			continue;
		if(pFile->type()==KviLogFile::Query && !bShowQuery)
			continue;

		if(filterFromDate)
			if(pFile->date()<fromDate)
				continue;

		if(filterToDate)
			if(pFile->date()>toDate)
				continue;

		if(enableNameFilter)
			if(!KviTQString::matchStringCI(nameFilterText,pFile->name()))
				continue;

		if(enableContentFilter)
		{
			pFile->getText(textBuffer,m_szLogDirectory);
			if(!KviTQString::matchStringCI(contentFilterText,textBuffer))
				continue;
		}

		if(pLastCategory)
		{
			if(pLastCategory->m_type!=pFile->type())
				pLastCategory = new KviLogListViewItemType(m_pListView,pFile->type());
		} else {
			pLastCategory = new KviLogListViewItemType(m_pListView,pFile->type());
		}

		KviTQString::sprintf(szCurGroup,__tr2qs_ctx("%Q on %Q","logview"),&(pFile->name()),
			&(pFile->network())
			);

		if(szLastGroup!=szCurGroup) {
			szLastGroup=szCurGroup;
			pLastGroupItem=new KviLogListViewItemFolder(pLastCategory,szLastGroup);
		}
		new KviLogListViewLog(pLastGroupItem,pFile->type(),pFile);
	}
	progress.setProgress( m_logList.count() );
}

void KviLogViewMDIWindow::cacheFileList()
{
	TQStringList m_pFileNames = getFileNames();
	m_pFileNames.sort();
	TQString szFname;

    for(TQStringList::Iterator it = m_pFileNames.begin(); it != m_pFileNames.end(); ++it)
    {
        szFname=(*it);
        TQFileInfo fi(szFname);
		if(fi.extension(false)=="gz" || fi.extension(false)=="log")
			m_logList.append(new KviLogFile(szFname));
	}
}

void KviLogViewMDIWindow::itemSelected(KviTalListViewItem * it)
{
	bool bCompressed=0;
	//A parent node
	m_pIrcView->clearBuffer();
	if(!it || !it->parent() || !(((KviLogListViewItem *)it)->m_pFileData) )
	{
		return;
	}
	
	TQString text;
	((KviLogListViewItem *)it)->m_pFileData->getText(text,m_szLogDirectory);

	TQStringList lines=TQStringList::split('\n',text);
	bool bOk;
	int iMsgType;
	for ( TQStringList::Iterator it = lines.begin(); it != lines.end(); ++it ) {
		TQString num=(*it).section(' ',0,0);
		iMsgType=num.toInt(&bOk);
		if(bOk)
			outputNoFmt(iMsgType,(*it).section(' ',1),KviIrcView::NoRepaint | KviIrcView::NoTimestamp);
		else
			outputNoFmt(0,*it,KviIrcView::NoRepaint | KviIrcView::NoTimestamp);
	}
	m_pIrcView->tqrepaint(false);
}

TQStringList KviLogViewMDIWindow::getFileNames()
{
    TQString logPath;
    g_pApp->getLocalKvircDirectory(logPath,KviApp::Log);
    TQString qPath(logPath);
    TQDir logDir(qPath);
    return logDir.entryList();
}

void KviLogViewMDIWindow::rightButtonClicked ( KviTalListViewItem * it, const TQPoint &, int )
{
	if(!it) return;
	if(((KviLogListViewItem *)it)->fileName(0).isEmpty()) return;
	KviTalPopupMenu* popup = new KviTalPopupMenu(this);
	popup->insertItem(*(g_pIconManager->getSmallIcon(KVI_SMALLICON_TQUIT)),__tr2qs_ctx("Remove file","logview"),this,TQT_SLOT(deleteCurrent()));
	popup->exec( TQCursor::pos() );
}

void KviLogViewMDIWindow::deleteCurrent()
{
	KviLogListViewItem* pItem = (KviLogListViewItem *)(m_pListView->currentItem());
	if(pItem)
	{
		if(!pItem->fileName(0).isNull())
		{
			TQString szFname;
			g_pApp->getLocalKvircDirectory(szFname,KviApp::Log,pItem->m_pFileData->fileName());
			KviFileUtils::removeFile(szFname);
			delete pItem;
			m_pIrcView->clearBuffer();
		}
	}
}

KviLogFile::KviLogFile(const TQString& name)
{
	m_szFilename=name;
	TQFileInfo fi(m_szFilename);
	m_bCompressed=(fi.extension(false)=="gz");
	TQString typeToken = m_szFilename.section('_',0,0);
    // Ignore non-logs files, this includes '.' and '..'
	if( KviTQString::equalCI(typeToken,"channel") )
		m_type = Channel;
	else if( KviTQString::equalCI(typeToken,"console") )
		m_type = Console;
	else if( KviTQString::equalCI(typeToken,"dccchat") )
		m_type = DccChat;
	else if( KviTQString::equalCI(typeToken,"query") )
		m_type = Query;
	else
		m_type = Other;

	KviStr undecoded = m_szFilename.section('.',0,0);
	undecoded.cutToFirst('_');
	m_szName = undecoded.hexDecode(undecoded.ptr()).ptr();

	undecoded = m_szFilename.section('.',1,1).section('_',0,0);
	m_szNetwork = undecoded.hexDecode(undecoded.ptr()).ptr();
	
	TQString szDate = m_szFilename.section('.',1,3).section('_',1,1);
	int iYear = szDate.section('.',0,0).toInt();
	int iMonth = szDate.section('.',1,1).toInt();
	int iDay = szDate.section('.',2,2).toInt();
	m_date.setYMD(iYear,iMonth,iDay);
	
	//debug("type=%i, name=%s, net=%s, date=%i %i %i",m_type,m_szName.ascii(),m_szNetwork.ascii(),iYear,iMonth,iDay);
}

void KviLogFile::getText(TQString & text,const TQString& logDir){
	TQString logName = logDir;
	TQFile logFile;
	logName.append(fileName());
#ifdef COMPILE_ZLIB_SUPPORT
	if(m_bCompressed)
	{
		gzFile file=gzopen(logName.local8Bit().data(),"rb");
		if(file)
		{
			char buff[1025];
			int len;
			KviTQCString data;
			//TQCString data;
			len=gzread(file,buff,1024);
			while(len>0)
			{
				buff[len]=0;
				data.append(buff);
				len=gzread(file,buff,1024);
			}
			gzclose(file);
			text = TQString::fromUtf8(data);
		} else {
			debug("Cannot open compressed file %s",logName.local8Bit().data());
		}
	} else {
#endif
		logFile.setName(logName);
		
		if(!logFile.open(IO_ReadOnly))
		return;
	
		TQByteArray bytes;
		bytes=logFile.readAll();
		text = TQString::fromUtf8(bytes.data(), bytes.size());
		logFile.close();
#ifdef COMPILE_ZLIB_SUPPORT
	}
#endif
}

#include "logviewmdiwindow.moc"