summaryrefslogtreecommitdiffstats
path: root/kmyfirewall/ipteditor/kmfipteditorpart.cpp
blob: 2fb2366f10841337cc9bb4c29fb90f6087d8a256 (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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/
/*
Author: Christian Hubinger <chubinger@irrsinnig.org>, (C) 2001
*/

#include "kmfipteditorpart.h"


// QT includes
#include <tqfile.h>
#include <tqtextstream.h>
#include <tqmultilineedit.h>


// KDE includes
#include <kinstance.h>
#include <kaction.h>
#include <kstdaction.h>
#include <kaccel.h>
#include <kfiledialog.h>
#include <klocale.h>
#include <kdebug.h>
#include <kmessagebox.h>
#include <kiconloader.h>
#include <kaboutdata.h>
#include <klocale.h>

// Project includes
#include "kmfruleedit.h"
#include "../core/kmyfirewallinterface.h"
#include "../kmfwidgets/kmfmainwindow.h"
#include "../kmfwidgets/kmfiptdocoptions.h"
#include "../core/kmfnetwork.h"
#include "../core/kmfdoc.h"
#include "../core/kmfiptdoc.h"
namespace KMF {
KMFIPTEditorPart::KMFIPTEditorPart( TQWidget *parentWidget, const char *widgetName,
                                  TQObject *parent, const char *name )
	: KParts::ReadWritePart( parent, name ) {
	KMFMainWindow *app = 0;
	// we need an instance
	setInstance( KMFIPTEditorPartFactory::instance() );
	
	
	app = dynamic_cast<KMFMainWindow*>( parent );
	if ( ! app ) {
		KMessageBox::error(0,"Oops wrong parent class found for kmfinstallerplugin!!!");
	}
	
	// this should be your custom internal widget
	m_ruleedit = new KMFRuleEdit( parentWidget, widgetName );
	m_ruleedit->setKMFMainWindow( app );
	m_ruleedit->setFocusPolicy( TQ_ClickFocus );
	
	m_editdoc = new KMFIPTDocOptions( parentWidget , "m_editdoc" );
	m_editdoc->hide();
	
	m_ruleedit->loadDoc( app->network() );
	m_editdoc->loadDoc( app->network()->currentDocAsIPTDoc() );
	
	connect( app, TQT_SIGNAL( sigUpdateView() ),
		m_ruleedit, TQT_SLOT( slotUpdateView() ) );

	connect( app, TQT_SIGNAL( sigUpdateView( NetfilterObject* ) ),
		m_ruleedit, TQT_SIGNAL( sigUpdateView( NetfilterObject* ) ) );

	connect( app, TQT_SIGNAL( sigEnableActions( bool ) ),
		this, TQT_SLOT( slotEnableActions( bool ) ) );

	connect ( m_editdoc, TQT_SIGNAL( sigConfigChanged() ),
	          m_ruleedit, TQT_SLOT( slotUpdateView() ) );

	// notify the part that this is our internal widget
	setWidget( m_ruleedit );
	
	m_actionEditChain = new KAction( i18n( "&Edit Chain" ), TQIconSet( BarIcon( "configure_toolbars", KMFIPTEditorPartFactory::instance() ) ),
	                                 0 , this, TQT_SLOT( slotEditChain() ), actionCollection(), "edit_chain" );

	m_actionNewChain = new KAction( i18n( "Add New Chain..." ), TQIconSet( BarIcon( "view_tree", KMFIPTEditorPartFactory::instance() ) ),
	                                0 , this, TQT_SLOT( slotNewChain() ), actionCollection(), "new_chain" );

	m_actionDelChain = new KAction( i18n( "Delete Chain" ), TQIconSet( BarIcon( "editdelete", KMFIPTEditorPartFactory::instance() ) ),
	                                0 , this, TQT_SLOT( slotDelChain() ), actionCollection(), "del_chain" );

	
	m_actionNewRule = new KAction( i18n( "Add New Rule..." ), TQIconSet( KGlobal:: iconLoader()->loadIcon( "rule", KIcon::Toolbar ) ) ,
								   KStdAccel::shortcut(KStdAccel::New) , this, TQT_SLOT( slotNewRule() ), actionCollection(), "new_rule" );
	
//	m_actionNewRule->setIconSet( TQIconSet(  loader->loadIcon( "rule-22", KIcon::User ) ) );							   
								   
	m_actionDelRule = new KAction( i18n( "Delete Rule" ), TQIconSet( BarIcon( "editdelete", KMFIPTEditorPartFactory::instance() ) ),
								   KStdAccel::shortcut(KStdAccel::DeleteWordBack), this, TQT_SLOT( slotDelRule() ), actionCollection(), "del_rule" );
	
	m_actionEditDocOptions = new KAction( i18n( "&Configure Firewall Options..." ), "configure", 0 , this, TQT_SLOT( slotEditDocOptions() ),
	                                    actionCollection(), "edit_doc_options" );

	
	m_actionEditNetwork = new KAction( i18n( "&Configure the Network" ), TQIconSet( BarIcon( "configure_toolbars", KMFIPTEditorPartFactory::instance() ) ), 0 , this, TQT_SLOT( slotEditNetwork() ),
	                                    actionCollection(), "edit_network" );

	
	// set our XML-UI resource file
	setXMLFile( "kmfipteditorpartui.rc" );

	// we are read-write by default
	setReadWrite( true );

	// we are not modified since we haven't done anything yet
	setModified( false );
}

KMFIPTEditorPart::~KMFIPTEditorPart() {}

void KMFIPTEditorPart::setReadWrite( bool rw ) {
	// notify your internal widget of the read-write state

	ReadWritePart::setReadWrite( rw );
}

void KMFIPTEditorPart::slotEditDocOptions() {
	KMFMainWindow* app = dynamic_cast<KMFMainWindow*>( parent() );
	if ( ! app ) {
		KMessageBox::error(0,"Oops wrong parent class found for KMFIPTEditorPart!!!");
		return;
	}
	m_editdoc->loadDoc( app->network()->currentDocAsIPTDoc() );
	m_editdoc ->show();
}


void KMFIPTEditorPart::slotEditChain() {
	m_ruleedit->slotEditChain();
}

void KMFIPTEditorPart::slotNewChain() {
	m_ruleedit->slotAddChain();
}

void KMFIPTEditorPart::slotDelChain() {
	m_ruleedit->slotDelChain();
}

void KMFIPTEditorPart::slotNewRule() {
	m_ruleedit->slotAddRule();
}

void KMFIPTEditorPart::slotDelRule() {
	m_ruleedit->slotDelRule();
}

void KMFIPTEditorPart::slotEditNetwork() {
	kdDebug() << "KMFIPTEditorPart::slotEditNetwork()" << endl;
	m_ruleedit->slotEditNetwork();
}


//###########################################
void KMFIPTEditorPart::setModified( bool modified ) {
	// get a handle on our Save action and make sure it is valid
	KAction * save = actionCollection() ->action( KStdAction::stdName( KStdAction::Save ) );
	if ( !save )
		return ;

	// if so, we either enable or disable it based on the current
	// state
	if ( modified )
		save->setEnabled( true );
	else
		save->setEnabled( false );

	// in any event, we want our parent to do it's thing
	ReadWritePart::setModified( modified );
}

bool KMFIPTEditorPart::openFile() {
	// m_file is always local so we can use TQFile on it
	//     TQFile file(m_file);
	//     if (file.open(IO_ReadOnly) == false)
	//         return false;
	//
	//     // our example widget is text-based, so we use TQTextStream instead
	//     // of a raw TQDataStream
	//     TQTextStream stream(&file);
	//     TQString str;
	//     while (!stream.eof())
	//         str += stream.readLine() + "\n";
	//
	//     file.close();
	//
	//     // now that we have the entire file, display it
	// //     m_widget->setText(str);
	//
	//     // just for fun, set the status bar
	//     emit setStatusBarText( m_url.prettyURL() );

	return true;
}

bool KMFIPTEditorPart::saveFile() {
	// if we aren't read-write, return immediately
	/*    if (isReadWrite() == false)
	        return false;
	 
	    // m_file is always local, so we use TQFile
	    TQFile file(m_file);
	    if (file.open(IO_WriteOnly) == false)
	        return false;
	 
	    // use TQTextStream to dump the text to the file
	    TQTextStream stream(&file);
	    stream << m_widget->text();
	 
	    file.close();
	*/ 
	return true;
}

void KMFIPTEditorPart::fileOpen() {
	// this slot is called whenever the File->Open menu is selected,
	// the Open shortcut is pressed (usually CTRL+O) or the Open toolbar
	// button is clicked
	/*    TQString file_name = KFileDialog::getOpenFileName();
	 
	    if (file_name.isEmpty() == false)
	        openURL(file_name);*/
}

void KMFIPTEditorPart::fileSaveAs() {
	// this slot is called whenever the File->Save As menu is selected,
	/*    TQString file_name = KFileDialog::getSaveFileName();
	    if (file_name.isEmpty() == false)
	        saveAs(file_name);*/
}

void KMFIPTEditorPart::slotEnableActions( bool on ) {
	if ( on ) {
		m_actionEditChain->setEnabled( true );
		m_actionNewChain->setEnabled( true );
		m_actionNewRule->setEnabled( true );
		m_actionDelChain->setEnabled( true );
		m_actionDelRule->setEnabled( true );
		m_actionEditDocOptions->setEnabled( true );
	} else {
		m_actionEditChain->setEnabled( false );
		m_actionNewChain->setEnabled( false );
		m_actionNewRule->setEnabled( false );
		m_actionDelChain->setEnabled( false );
		m_actionDelRule->setEnabled( false );
		m_actionEditDocOptions->setEnabled( false );

	}
}


// It's usually safe to leave the factory code alone.. with the
// notable exception of the KAboutData data

KInstance* KMFIPTEditorPartFactory::s_instance = 0L;
KAboutData* KMFIPTEditorPartFactory::s_about = 0L;

KMFIPTEditorPartFactory::KMFIPTEditorPartFactory()
		: KParts::Factory() {}

KMFIPTEditorPartFactory::~KMFIPTEditorPartFactory() {
	delete s_instance;
	delete s_about;

	s_instance = 0L;
}

KParts::Part* KMFIPTEditorPartFactory::createPartObject( TQWidget *parentWidget, const char *widgetName,
        TQObject *parent, const char *name,
        const char *classname, const TQStringList& ) {
	// Create an instance of our Part
	KMFIPTEditorPart * obj = new KMFIPTEditorPart( parentWidget, widgetName, parent, name );

	// See if we are to be read-write or not
	if ( TQCString( classname ) == "KParts::ReadOnlyPart" )
		obj->setReadWrite( false );

	return obj;
}

KInstance* KMFIPTEditorPartFactory::instance() {
	if ( !s_instance ) {
		s_about = new KAboutData( "kmfipteditorpart", I18N_NOOP( "kmfipteditorpartPart" ), "0.1" );
		s_about->addAuthor( "Christian Hubinger", 0, "chubinger@irrsinnig.org" );
		s_instance = new KInstance( s_about );
	}
	return s_instance;
}

extern "C" {
	void* init_libkmfipteditorpart() {
		return new KMFIPTEditorPartFactory;
	}
}

}

#include "kmfipteditorpart.moc"