summaryrefslogtreecommitdiffstats
path: root/ksystemlog/src/findManager.cpp
blob: 63b4cb2a8abce12df48f4988220407b17bb9a3a4 (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
//
// C++ Implementation: findManager
//
// Description: 
//
//
// Author: Nicolas Ternisien <nicolas.ternisien@gmail.com>, (C) 2005
//
// Copyright: See COPYING file that comes with this distribution
//
//

//Project includes
#include "findManager.h"

#include "ksystemlog.h"


FindManager::FindManager(KSystemLog* parent, const char* name) :
	TQObject(parent, name),
	main(parent),
	findDialog(NULL),
	findManager(NULL),
	previousItemFound(NULL),
	currentItemFound(NULL) {
	
	
}

FindManager::~FindManager() {
	if (findDialog!=NULL)
		delete findDialog;
		
	if (findManager!=NULL)
		delete findManager;
}

void FindManager::slotFind() {

	if (findManager!=NULL) {
		delete findManager;
		findManager=NULL;
	}
	
	if (findDialog==NULL) {
		kdDebug() << "KFindDialog creation" << endl;
		//TODO Save the Find options to Config file
		findDialog=new KFindDialog(false, main, "find_dialog");
		connect(findDialog, TQ_SIGNAL(okClicked()), this, TQ_SLOT(slotFindNext()));
	
		findDialog->show();
	}
	else {
		findDialog->show();
	
		//KWin::activateWindow(findDialog->winId());
		//return;
	}

}


void FindManager::slotFirstFind() {
	kdDebug() << "First Find" << endl;
	
	LogManager* currentManager=main->activeLogManager();
	TDEListView* list=currentManager->getView()->getLogList();
	
	//Delete the previous KFind object (if it exists)
	if (findManager!=NULL) {
		delete findManager;
		findManager=NULL;
	}

	//TODO Is useful?
	if (previousItemFound!=NULL) {
		list->setSelected(previousItemFound, false);
	}
	
	//This creates a find-next-prompt dialog if needed.
	findManager=new KFind(findDialog->pattern(), findDialog->options(), main, findDialog);
	connect(findManager, TQ_SIGNAL(highlight(const TQString&, int, int)), this, TQ_SLOT(highlightSearch(const TQString&, int, int)));
	connect(findManager, TQ_SIGNAL(findNext()), this, TQ_SLOT(slotFindNext()));
	
	//Do not keep the KFindDialog open
	findDialog->hide();
	
	
	//findManager->closeFindNextDialog();
	
	TQListViewItemIterator it(list, TQListViewItemIterator::Selected);
	int itemPos=-1;
	int currentPos;
	while (it.current()) {
		list->setSelected(it.current(), false);
		
		currentPos=list->itemPos(it.current());
		if (currentPos>=itemPos) {
			itemPos=currentPos;
			previousItemFound=static_cast<LogListItem*> (it.current());
		}
		
		++it;
	}
	//Unselect all previous items (the last one if always selected)
	if (previousItemFound!=NULL)
		list->setSelected(previousItemFound, true);
	
	
	
	//If we search from the cursor, the iterator goes to the first selected item
	if (findDialog->options() & KFindDialog::FromCursor) {
		currentItemFound=currentManager->getView()->getFirstSelectedItem();
		
		//If nothing is selected, then we initialize the iterator classicaly
		if (currentItemFound==NULL) {
			currentItemFound=static_cast<LogListItem*> (list->firstChild());
		}
		
		previousItemFound=currentItemFound;
	}
	//If we search from the last, the iterator is initialized to the last item
	else if (findDialog->options() & KFindDialog::FindBackwards) {
		currentItemFound=static_cast<LogListItem*> (list->lastItem());
	}
	//Default initialization
	else {
		currentItemFound=static_cast<LogListItem*> (list->firstChild());
	}
	
}

void FindManager::slotFindNext() {
	//If we attempt to go to the next search, without have opened the find dialog, we open it
	if (findDialog==NULL) {
		slotFind();
		return;
	}
	
	//If the KFind object has not been initialized, we call the right method for that
	if (findManager==NULL) {
		slotFirstFind();
	}

	
	//Keep the Find Dialog opened
	/*
	if (findManager->options() != findDialog->options() || findManager->pattern()!=findDialog->pattern()) {
		kdDebug() << "Reinitialize the search..." << endl;
		slotFirstFind();
	}
	*/


	KFind::Result res = KFind::NoMatch;
	while (res==KFind::NoMatch && currentItemFound!=NULL) {

		//Always need new data, because a line is only selected one time
		//if (findManager->needData())
			findManager->setData(currentItemFound->exportToText());

		//Let KFind inspect the text fragment, and display a dialog if a match is found
		res=findManager->find();

		//Always need new data, because a line is only selected one time
		//if (res==KFind::NoMatch ) {
			if (findDialog->options() & KFindDialog::FindBackwards)
				currentItemFound=static_cast<LogListItem*> (currentItemFound->itemAbove());
			else
				currentItemFound=static_cast<LogListItem*> (currentItemFound->itemBelow());
		//}
	}


	//End of the view
	if (res==KFind::NoMatch) {
		if (findManager->shouldRestart()) {
			delete findManager;
			findManager=NULL;
			findDialog->setOptions(findDialog->options() & !KFindDialog::FromCursor);
			slotFindNext();
		}
		else {
			findManager->closeFindNextDialog();
		}
	}

}


void FindManager::highlightSearch(const TQString& /*text*/, int /*matchingIndex*/, int /*matchingLength*/) {
	LogManager* currentManager=main->activeLogManager();
	TDEListView* list=currentManager->getView()->getLogList();
		
	if (previousItemFound!=NULL) {
		list->setSelected(previousItemFound, false);
	}
	
	if (currentItemFound!=NULL) {
		list->setSelected(currentItemFound, true);
		list->ensureItemVisible(currentItemFound);
		previousItemFound=currentItemFound;
	}

}


#include "findManager.moc"