summaryrefslogtreecommitdiffstats
path: root/kmyfirewall/core/kmfprotocolcategory.cpp
blob: 619850b39abf1cebc9dc223be64ffbeec0321117 (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
//
// C++ Implementation: kmfprotocolcategory
//
// Description:
//
//
// Author: Christian Hubinger <chubinger@irrsinnig.org>, (C) 2007
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "kmfprotocolcategory.h"

// TQt includes
#include <tqdom.h>

// KDE includes
#include <kdebug.h>
#include <tdelocale.h>

// Project includes
#include "kmfundoengine.h"
#include "kmfprotocol.h"
#include "kmfprotocollibrary.h"
#include "xmlnames.h"

namespace KMF {

/* Never Ever Change This Uuids! */
const TQUuid& KMFProtocolCategory::customCategoryUuid() {
	return *(new TQUuid( "{9758012f-e2a0-4594-938f-c154a7078752}" ) );
};
const TQUuid& KMFProtocolCategory::miscCategoryUuid() {
	return *(new TQUuid( "{02e7113c-535d-447d-8a25-5c9e89fd3f79}" ) );
};	


KMFProtocolCategory* KMFProtocolCategory::createCategory( const TQString& name ) {
	KMFProtocolCategory* cat = new KMFProtocolCategory( 0, name.latin1() );
	cat->setName( name );
	return cat;
}

KMFProtocol* KMFProtocolCategory::createProtocol( const TQString& name ) {
	KMFProtocol* prot = new KMFProtocol( this, name.latin1() );
	addProtocol( prot );
	return prot;
}

KMFProtocolCategory* KMFProtocolCategory::getCustomCategory() {
	KMFProtocolCategory* catCustom = KMFProtocolLibrary::instance()->findCategory( KMFProtocolCategory::customCategoryUuid() );
	if ( ! catCustom ) {
		catCustom = KMFProtocolCategory::createCategory( i18n("Custom Protocols") );
		catCustom->setUuid( KMFProtocolCategory::customCategoryUuid().toString() );
	}
	return catCustom;
}



KMFProtocolCategory::KMFProtocolCategory ( NetfilterObject* parent, const char* name ) : NetfilterObject ( parent, name ) {
	// m_protocols;
}

KMFProtocolCategory::~KMFProtocolCategory() {}

int KMFProtocolCategory::type() {
	return NetfilterObject::PROTOCOLCATEGORY;
}

void KMFProtocolCategory::clear() {
}


TQValueList<KMFProtocol*>& KMFProtocolCategory::protocols() const {
	TQValueList<KMFProtocol*>* ret_val = new TQValueList<KMFProtocol*>;
	*ret_val = m_protocols;
	return *ret_val;
}


KMFProtocol* KMFProtocolCategory::findProtocolByName ( const TQString& name ) const {
	// kdDebug() << "KMFProtocol* KMFProtocolCategory::findProtocolByName( const TQString& name ) const" << endl;
	TQValueList< KMFProtocol* >::const_iterator it;
	for ( it = m_protocols.constBegin(); it != m_protocols.constEnd(); ++it ) {
		KMFProtocol *p = *it;
		if ( p->name() == name ) {
			return p;
		}
	}
	return 0;
}
KMFProtocol* KMFProtocolCategory::findProtocolByUuid ( const TQUuid& uuid ) const {
//	kdDebug() << "KMFProtocol* KMFProtocolCategory::findProtocolByUuid( const TQUuid& uuid ) const" << endl;
	TQValueList< KMFProtocol* >::const_iterator it;
	for ( it = m_protocols.constBegin(); it != m_protocols.constEnd(); ++it ) {
		KMFProtocol *p = *it;
		if ( p->uuid() == uuid ) {
			return p;
		}
	}
	return 0;
}

KMFProtocol* KMFProtocolCategory::addProtocol ( KMFProtocol* proto ) {
	// kdDebug() << "KMFProtocol* KMFProtocolCategory::addProtocol( KMFProtocol* " << proto->name() << " )" << endl;
	// proto->category()->delProtocol( proto );
	m_protocols.append ( proto );
	proto->setCategory( this );
	return proto;
}

void KMFProtocolCategory::delProtocol ( KMFProtocol* prot, bool destructive = false ) {
//  kdDebug() << "void KMFProtocolCategory::delProtocol( KMFProtocol* prot )" << endl;
	TQValueList<KMFProtocol*>::iterator it;
	for ( it = m_protocols.begin(); it != m_protocols.end(); ++it ) {
		KMFProtocol *p = *it;
		if ( p->name() == prot->name() ) {
			kdDebug() << "Delete protocol: " << prot->name() << " from category:  " << name() << endl;
			m_protocols.remove( p );
			if ( destructive ) {
				p->deleteLater();
			}
			
			changed();
			return;
			
		}
	}
	changed();
}

void KMFProtocolCategory::slotOnProtocolDeleted( TQObject* prot ) {
	kdDebug() << "KMFProtocolCategory::slotOnProtocolDeleted( TQObject* )" << endl;
	TQValueList<KMFProtocol*>::iterator it;
	for ( it = m_protocols.begin(); it != m_protocols.end(); ++it ) {
		KMFProtocol *p = *it;
		if ( p == prot ) {
			kdDebug() << "Delete protocol from category:  " << name() << endl;
			m_protocols.remove( p );
			changed();
			return;
		}
	}
	// deleteLater();
}

const TQDomDocument& KMFProtocolCategory::getDOMTree() {
	kdDebug() << "const TQDomDocument& KMFProtocolCategory::getDOMTree() " << endl;
	TQDomDocument doc;
	TQDomElement root = doc.createElement ( XML::ProtocolCategory_Element );
	NetfilterObject::saveUuid ( root );

	root.setAttribute (  XML::Name_Attribute, name() );
	root.setAttribute ( XML::Description_Attribute, description() );

	TQValueList< KMFProtocol* >::iterator it;
	for ( it = m_protocols.begin(); it != m_protocols.end(); ++it ) {
		KMFProtocol* p = *it;
		if ( p->customProtocol() ) {
			root.appendChild ( (*it)->getDOMTree( ) );
		}
	}
	doc.appendChild ( root );
	return * ( new TQDomDocument ( doc ) );
}

void KMFProtocolCategory::loadXML ( const TQDomDocument& doc, TQStringList& errors ) {
	// kdDebug() << "void KMFProtocolCategory::loadXML( const TQDomDocument& )" << endl;
	TQDomElement root = doc.documentElement();
	loadXML ( root, errors );
}

void KMFProtocolCategory::loadXML ( TQDomNode root, TQStringList& errors ) {
//	kdDebug() << "void KMFProtocolCategory::loadXML( TQDomNode root )" << endl;
	NetfilterObject::loadUuid ( root, errors );
	
	setName( root.toElement().attribute ( XML::Name_Attribute ) );
	setDescription( root.toElement().attribute ( XML::Description_Attribute ) );

	TQValueList< KMFProtocol* > xmlDefinedProtocols;
	TQDomNode curr = root.firstChild();
	while ( !curr.isNull() ) {
		if ( curr.isElement() && ( curr.nodeName() == XML::Protocol_Element ) ) {
			TQString name = curr.toElement().attribute (  XML::Name_Attribute );
			TQString uuid = curr.toElement().attribute (  XML::Uuid_Attribute );
			KMFProtocol *p = findProtocolByUuid( uuid );
			if ( ! p ) {
				p = createProtocol( name );
				kdDebug() << " + + Register Protocol: " << name << " with uuid: " << uuid << endl;
				TQDomDocument protocol;
				protocol.appendChild( curr.cloneNode( true ) );
				TQStringList *errors = new TQStringList();
				p->loadXML( protocol, *errors );
			}
			xmlDefinedProtocols.append ( p );
		}
// 		if ( curr.isElement() && ( curr.nodeName() == XML::ProtocolUsage_Element ) ) {
// 			TQString uuid = curr.toElement().attribute ( XML::ProtocolUuid_Attribute );
// 			KMFProtocol *p = findProtocolByUuid( uuid );
// 			if ( ! p ) {
// 				p = createProtocol( curr.toElement().attribute ( XML::Name_Attribute ) );
// 				TQDomDocument protocol;
// 				protocol.appendChild( curr.cloneNode( true ) );
// 				TQStringList *errors = new TQStringList();
// 				p->loadXML( protocol, *errors );
// 			}
// 			xmlDefinedProtocols.append ( p );
// 		}
		curr = curr.nextSibling();
	}

// 	{
// 		TQValueList< KMFProtocol* >& allprotocols = protocols();
// 		TQValueList< KMFProtocol* >::iterator itAllProtocols;
// 		for ( itAllProtocols = allprotocols.begin(); itAllProtocols != allprotocols.end(); ++itAllProtocols ) {
// 			KMFProtocol *oldProtocol = *itAllProtocols;
// 
// 			bool found = false;
// 			TQValueList< KMFProtocol* >::iterator itProtocols;
// 			for ( itProtocols = xmlDefinedProtocols.begin(); itProtocols != xmlDefinedProtocols.end() && ! found; ++itProtocols ) {
// 				KMFProtocol* p = *itProtocols;
// 				if ( p == oldProtocol ) {
// 					found = true;
// 				}
// 			}
// 
// 			if ( ! found ) {
// 				KMFUndoEngine::instance()->log ( i18n ( "Removing unused protocol: %1" ).arg ( oldProtocol->name() ), KMFError::OK, this );
// 				delProtocol ( oldProtocol );
// 			}
// 		}
// 	}
	changed();
}

}