summaryrefslogtreecommitdiffstats
path: root/ksystemlog/src/logLineTree.cpp
blob: 77d32a18552d1a1220441e69fcc7364ce4935851 (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
/***************************************************************************
 *   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.             *
 ***************************************************************************/

#include "logLineTree.h"

#include "parentLogLine.h"
#include "childLogLine.h"
#include "logLine.h"

#include "view.h"


LogLineTree::LogLineTree(LogViewColumns* cols, groupByType type, int column) :
	LogLineList()
	{
	
	columns=cols;
	groupBy=type;
	groupByColumn=column;

}

LogLineTree::~LogLineTree() {

}


void LogLineTree::synchronizeRemovedLines(View* view) {
	TQPtrListIterator<LogLine> it(removedList);
	
	LogLine* line=it.current();
	
	kdDebug() << "Removing old items from tree..." << endl;
	
	//We removed the old items
	while(line!=NULL) {
		if (line->itemExists()) {
			line->removeItem(view->getLogList());
		}
	
		//TODO Delete the line object
		
		++it;
		line=it.current();
	}
	
	removedList.clear();
	
}

LogLine* LogLineTree::synchronizeAddedLines(View* view) {

	/**
	 * This old code had to do the same thing than the above code, but
	 * it was buggy for a strange reason.
	 * This code wasn't able to find Parent Log Lines in the addedList object
	 */
	/*
	//We first add the new parent items
	TQPtrListIterator<LogLine> it(addedList);
	
	LogLine* line=it.current();
	
	kdDebug() << "Inserting " << parents.count() << " parent log item" << endl;
	
	int i=0;
	while (line!=NULL) {
		if (line->isParentLogLine()==true) {
			ParentLogLine* parent=(ParentLogLine*) (line);
			parent->insertItem(view->getLogList());
			
			addedList.remove(it.current());
			++i;
		}
		
		++it;

		line=it.current();
	}
	
	kdDebug() << "Insertion of " << i << " parent log items" << endl;
	*/
	
	kdDebug() << "Adding parent items to the tree..." << endl;
	
	//We first add the new parent items
	TQPtrListIterator<ParentLogLine> it(parents);
	
	LogLine* line=it.current();
		
	while (line!=NULL) {
		//Even if the parent has already been added, it will automatically be added once time
		line->insertItem(view->getLogList());
		
		addedList.remove(it.current());
		
		++it;
		line=it.current();
	}
	

	//Returns line, even if line is NULL (must be tested by the caller class)
	return(LogLineList::synchronizeAddedLines(view));
}

bool LogLineTree::remove(LogLine* line) {
	/**
	 * Parent log line actions : 
	 * - Only remove this line is it does not have any children left
	 */
	if (line->isParentLogLine()==true) {
		ParentLogLine* parent=(ParentLogLine*) line;
		
		if (parent->hasChildren()==false) {
			parents.remove(parent);
			return(LogLineList::remove(line));
		}

	}
	/**
	 * Child log line actions : 
	 * - First remove the dependency from the parent
	 * - Remove parent if it does not have child left
	 * - Finally remove child
	 */
	else if (line->isChildLogLine()==true) {
		ChildLogLine* child=(ChildLogLine*) line;
		ParentLogLine* parent=child->getParent();
		parent->removeChild(child);
		
		//Remove parent if it does not have longer children
		if (parent->hasChildren()==false) {
			parents.remove(parent);
			LogLineList::remove(parent);
		}
		
		//Remove the children
		return(LogLineList::remove(line));
		
	}
	
	//Default case, with normal log lines
	return(LogLineList::remove(line));
	
}

void LogLineTree::insert(LogLine* line) {
	
	TQPtrListIterator<ParentLogLine> it(parents);
	ParentLogLine* parent=it.current();
	
	bool insertion=false;
	
	while (parent!=NULL) {
		insertion=parent->isChild(line);
		if (insertion==true) {

			TQDate date=line->getTime().date();
			TQTime time=line->getTime().time();
			
			ChildLogLine* newChild=new ChildLogLine(date, time, line->getItemList(), line->getOriginalFile(), line->getLogLevel(), line->getType());
			
			parent->addChild(newChild);
			newChild->setParent(parent);
			
			//Insert this child by the classical way
			LogLineList::insert(newChild);
			
			delete line;
			
			return;
		}
		
		++it;
		parent=it.current();
	}
	
	TQDate date=line->getTime().date();
	TQTime time=line->getTime().time();
	
	ParentLogLine* newParent=new ParentLogLine(date, time, line->getItemList(), line->getOriginalFile(), line->getLogLevel(), line->getType(), this->groupBy, this->groupByColumn, this->columns);
	//Insert this parent by the classical way
	LogLineList::insert(newParent);
	
	parents.append(newParent);
	
	ChildLogLine* newChild=new ChildLogLine(date, time, line->getItemList(), line->getOriginalFile(), line->getLogLevel(), line->getType());
	newParent->addChild(newChild);
	newChild->setParent(newParent);
	//Insert this child by the classical way
	LogLineList::insert(newChild);
	
	delete line;


}