summaryrefslogtreecommitdiffstats
path: root/kmyfirewall/kmfwidgets/kmftemplatechooser.cpp
blob: 9a5b47714eac687e9f006caa9c4e6e8931154f13 (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
//
// C++ Implementation:
//
// Description:
//
//
// Author: Christian Hubinger <chubinger@irrsinnig.org>, (C) 2004
//
// Copyright: See COPYING file that comes with this distribution
//
// License: GPL
//

// TQt includes
#include <tqlistbox.h>
#include <tqfile.h>
#include <tqdom.h>
#include <tqlabel.h>
#include <tqdir.h>
#include <tqpushbutton.h>

// KDE includes
#include <kstandarddirs.h>
#include <tdeglobal.h>
#include <kdebug.h>
#include <tdelocale.h>
#include <tdemessagebox.h>
#include <tdeapplication.h>

// project includes
#include "../core/kmfconfig.h"
#include "xmlnames.h"

#include "kmftemplatechooser.h"
namespace KMF {
KMFTemplateChooser::KMFTemplateChooser(TQWidget* parent, const char* name, bool modal, WFlags fl) : KMyFirewallTemplateChooser(parent,name, modal,fl) {
	connect( lb_templates, TQT_SIGNAL( highlighted ( int ) ), 
		this, TQT_SLOT( slotNewTemplateSelected( int ) ) );
	connect( lb_templates, TQT_SIGNAL( doubleClicked( TQListBoxItem* ) ), 
		this, TQT_SLOT( slotNewTemplateSelected( TQListBoxItem* ) ) );
	connect( b_help, TQT_SIGNAL( clicked() ),
		this, TQT_SLOT( slotHelp() ) );

	parseTemplates();
	b_accept->setEnabled( false );
}

KMFTemplateChooser::~KMFTemplateChooser() {}

/*$SPECIALIZATION$*/
void KMFTemplateChooser::reject() {
	TQDialog::reject();
}

void KMFTemplateChooser::accept() {
	if ( lb_templates->currentItem() == -1 ) {
		KMessageBox::error( this, i18n("No Template selected.") );
		return;
	}
	if ( *m_templateFilePaths.at( lb_templates->currentItem() ) == "-1" ) {
		// emit sigLoadEmptyDocument();
	} else {
		emit sigLoadTemplate( *m_templateFilePaths.at( lb_templates->currentItem() ) );
	}
	TQDialog::accept();
}

void KMFTemplateChooser::slotHelp() {
	// kdDebug() << "void KMFTemplateChooser::slotHelp()" << endl;
	kapp->invokeHelp();
}

void KMFTemplateChooser::slotNewTemplateSelected( TQListBoxItem* i ){
	// kdDebug() << "void KMFTemplateChooser::slotNewTemplateSelected( " << index  << " )" << endl;
	slotNewTemplateSelected( lb_templates->index( i ) );
	accept();
}

void KMFTemplateChooser::slotNewTemplateSelected( int index ){
	// kdDebug() << "void KMFTemplateChooser::slotNewTemplateSelected( " << index  << " )" << endl;
	b_accept->setEnabled( true );
	lbl_description->setText( *m_templateDescriptions.at( index ) );
}



void KMFTemplateChooser::parseTemplates(){
	lb_templates->clear();
	lbl_description->clear();
	
	// Add Empty template
	lb_templates->insertItem( i18n("Empty Ruleset") );
	m_templateFilePaths.append( "-1" );
	if ( KMFConfig::useGenericInterface() ) {
		m_templateDescriptions.append( i18n("Clean Ruleset that does only setup connection tracking e.g block everything not related to connections you initialised.") );
	} else {
		m_templateDescriptions.append( i18n("Clean Ruleset that does not do anything. Use this if you like to setup your firewall from scratch.") );
	}
	
	TDEStandardDirs std_dir;
	TQString tmp_dir = std_dir.findResourceDir( "data", "kmyfirewall/templates/" );
	
	TQDir dir( tmp_dir + "/kmyfirewall/templates/" );
	kdDebug() << "Found Data dir at: " << dir.path() << endl;
	TQString type;
	if ( KMFConfig::useGenericInterface() ) {
		type = "*.tkmfgrs";
	} else {
		type = "*.tkmfrs";
	}
	TQStringList templates = dir.entryList( type );
	if ( templates.isEmpty() ) {
		KMessageBox::information( this, i18n("No templates (%1) could be found; please check your installation.").arg( type ) );
		return;
	}
	for ( TQStringList::Iterator it = templates.begin(); it != templates.end(); ++it ) {
        parseFile( dir.path() + "/" + *it );
    }
}

void KMFTemplateChooser::parseFile( const TQString& file ) {
	// kdDebug() << "Parsing Template: " << file << endl;
	TQFile f( file );
	
	if ( !f.open( IO_ReadOnly ) ) {
		KMessageBox::information( this, i18n("Template %1 could not be opened.").arg( file ) );
		return;
	}
	
	TQDomDocument doc;
	if ( !doc.setContent( &f ) ) {
		f.close();
		KMessageBox::information( this, i18n("Template %1 is not a valid XML document.").arg( file ) );
		return;
	}
	
	TQDomElement root = doc.documentElement();
	TQDomNodeList list = root.elementsByTagName ( XML::Abstract_Element );
	if ( list.count() == 0 ) {
		KMessageBox::information( this, i18n("Template %1 does not contain the \"abstract\" tag.").arg( file ) );
		return;
	}
	TQDomNode node = list.item( 0 );
	TQString desc = node.toElement().attribute( XML::Description_Attribute );
	TQString name = node.toElement().attribute( XML::Name_Attribute );
	lb_templates->insertItem( name );
	m_templateFilePaths.append( file );
	m_templateDescriptions.append( desc );
}

}

#include "kmftemplatechooser.moc"