summaryrefslogtreecommitdiffstats
path: root/krecipes/src/datablocks/categorytree.cpp
blob: bb61de69e6529356a9e7cfd2ee7c9e85a86a2e19 (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
/***************************************************************************
*   Copyright (C) 2004 by Jason Kivlighn                                  *
*   (jkivlighn@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.                                   *
***************************************************************************/

#include "categorytree.h"

#include <kdebug.h>

#include "element.h"

CategoryTree::CategoryTree( CategoryTree *parent ) :
		m_parent( 0 ), m_child( 0 ), m_sibling( 0 ), m_last(0), m_count(0)
{
	if ( parent )
		parent->insertItem( this );
}

CategoryTree::~CategoryTree()
{
	if ( m_parent )
		m_parent->takeItem( this );

	CategoryTree * i = m_child;
	m_child = 0;
	while ( i ) {
		i->m_parent = 0;
		CategoryTree * n = i->m_sibling;
		delete i;
		i = n;
	}
}

CategoryTree *CategoryTree::add
	( const Element &cat )
{
	CategoryTree * new_child = new CategoryTree( this );
	new_child->category = cat;

	m_count++;
	
	return new_child;
}

void CategoryTree::insertItem( CategoryTree *newChild )
{
	newChild->m_parent = this;
	if ( m_child && m_child->m_last )
		m_child->m_last->m_sibling = newChild;
	else
		m_child = newChild;

	m_child->m_last = newChild;
	m_count++;
}

void CategoryTree::takeItem( CategoryTree *tree )
{
	kdError()<< "Both these methods seem to be broken... don't use this function!" << endl;

	CategoryTree *lastItem = m_child->m_last;
#if 0
	CategoryTree ** nextChild = &m_child;
	while( nextChild && *nextChild && tree != *nextChild )
		nextChild = &((*nextChild)->m_sibling);

	if ( nextChild && tree == *nextChild ) {
		*nextChild = (*nextChild)->m_sibling;
	}
	tree->m_parent = 0;
	tree->m_sibling = 0;
#else
	for ( CategoryTree *it = firstChild(); it; it = it->nextSibling() ) {
		if ( it->nextSibling() == tree ) {
			it->m_sibling = tree->nextSibling();
			break;
		}
	}
	tree->m_parent = 0;
	tree->m_sibling = 0;
#endif
	if ( tree != m_last )
		m_child->m_last = lastItem;
	else //FIXME: unstable behavior if this is the case
		kdDebug()<<"CategoryTree::takeItem: warning - unstable behavior expected"<<endl;
	m_count--;
}

void CategoryTree::clear()
{
	CategoryTree *c = m_child;
	CategoryTree *n;
	while( c ) {
		n = c->m_sibling;
		delete c;
		c = n;
	}
}

bool CategoryTree::contains( int id ) const
{
	bool result = false;

	for ( CategoryTree * child_it = firstChild(); child_it; child_it = child_it->nextSibling() ) {
		if ( child_it->category.id == id )
			return true;

		result = child_it->contains( id );
	}

	return result;
}

CategoryTree* CategoryTree::find( int id ) const
{
	CategoryTree *result = 0;

	for ( CategoryTree * child_it = firstChild(); child_it; child_it = child_it->nextSibling() ) {
		if ( child_it->category.id == id )
			return child_it;

		result = child_it->find( id );
	}

	return result;
}