summaryrefslogtreecommitdiffstats
path: root/kxmleditor/kxmleditorshell.cpp
blob: 91188d0684c10ecbbe59b73294e02a73b4ca5afe (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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/***************************************************************************
                          kxmleditorshell.cpp  -  description
                             -------------------
    begin                : Thu Oct 18 2001
    copyright            : (C) 2001, 2002, 2003 by The KXMLEditor Team
    email                : lvanek@users.sourceforge.net
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/
 /**
 @file
 */

#include "kxmleditorshell.h"
#include "kxeshellmanager.h"
#include "../part/kxmleditorpart.h"
#include "../part/kxedocument.h"

#include <klibloader.h>
#include <kstdaction.h>
#include <tdeapplication.h>
#include <tdelocale.h>
#include <kdebug.h>
#include <tdefiledialog.h>
#include <kkeydialog.h>
#include <kedittoolbar.h>
#include <kstatusbar.h>
#include <tdeparts/browserextension.h>
#include <tdeparts/partmanager.h>
#include <tdemessagebox.h>

#include <tdepopupmenu.h>

#include <tqevent.h>

#include "dcopiface_shell.h"


KXEShellManager *KXMLEditorShell::s_manager=0L;

KXMLEditorShell::KXMLEditorShell( const char * name, WFlags f )
	: KParts::MainWindow( name, f ),
	  m_pPart(0),
	  m_pKXEShellIface(0)
{
	//////////////////////////////
	// CREATE ACTIONS
	//////////////////////////////

	setXMLFile("kxmleditorshell.rc");

	KStdAction::openNew( this,SLOT(slotActFileNew()),actionCollection() );
	KStdAction::open( this, SLOT(slotFileOpen()), actionCollection() );
	m_pActFileOpenRecent = KStdAction::openRecent( this, SLOT(slotFileOpenRecent(const KURL&)), actionCollection() );
	m_pActFileOpenRecent->loadEntries( instance()->config() );
	m_pActReload = new TDEAction( i18n("Reloa&d"), "reload", Key_F5, this, SLOT(slotFileReload()), actionCollection(), "reload" );
	
	TDEToggleAction * pActToggleMainToolBar = KStdAction::showToolbar( this, SLOT(slotToggleMainToolBar()), actionCollection() );
	TDEToggleAction * pActToggleStatusBar = KStdAction::showStatusbar( this, SLOT(slotToggleStatusBar()), actionCollection() );
	KStdAction::keyBindings( this, SLOT(slotConfigureKeys()), actionCollection() );
	KStdAction::configureToolbars( this, SLOT(slotConfigureToolbars()), actionCollection() );

	KStdAction::mail( this, SLOT(mail()), actionCollection() );
	KStdAction::close( this, SLOT(slotActClose()), actionCollection() );
	KStdAction::quit( this, SLOT(close()), actionCollection());

	// L.V. moved to part pActPrint = KStdAction::print( 0, 0, actionCollection(), "print" );

	pActCloseW = new TDEAction( i18n("&Close"), 0, 0, this,SLOT(slotActWindowClose()), actionCollection(), "window_close" );
	pActCloseAllW = new TDEAction( i18n("Close &all"), 0, 0, this,SLOT(slotActWindowCloseAll()), actionCollection(), "window_close_all" );
	pActCloseAllO = new TDEAction( i18n("Close all &others"), 0, 0, this,SLOT(slotActWindowCloseAllOthers()), actionCollection(), "window_close_all_others" );

	//////////////////////////////
	// CREATE STATUSBAR
	//////////////////////////////
	statusBar()->message( i18n("Ready.") );

	//////////////////////////////
	// MISC. INITIALIZATION
	//////////////////////////////

	manager()->addShell(this);

	setAutoSaveSettings();
	pActToggleMainToolBar->setChecked( ! toolBar()->isHidden() );
	pActToggleStatusBar->setChecked( ! statusBar()->isHidden() );

	m_pKXEShellIface = new KXMLEditorShellIface(*this);

	statusBar()->setSizeGripEnabled(true);

	setPart(0L);		// start empty

}

KXMLEditorShell::~KXMLEditorShell()
{
	if (m_pKXEShellIface)
		delete m_pKXEShellIface;

	manager()->removeShell(this);
}

/////////////////////////////////////////////////////////////////////
//                     ACTION SLOTS
/////////////////////////////////////////////////////////////////////

void KXMLEditorShell::slotFileOpen()
{
	statusBar()->message( i18n("Opening file...") );

	KURL url = KFileDialog::getOpenURL( TQString::null,
	                                    i18n(FILE_DIALOG_FILTER),
	                                    this );
	openURL(url);
	
	// L.V.: tried to meve save functionality to Part
	//m_pActFileSave->setEnabled(true); // TODO: L.V. Why File Save ? Why not Save as... ?

	statusBar()->message( i18n("Ready.") );
}

void KXMLEditorShell::slotFileOpenRecent(const KURL & url)
{
	statusBar()->message( i18n("Opening file...") );

	openURL(url);

	statusBar()->message( i18n("Ready.") );
}

void KXMLEditorShell::slotFileReload()
{
	statusBar()->message( i18n("Reloading file...") );

	// Try to (re)open the file.
	// In case the file is modified, a Save/Discard/Cancel message box pops up.
	if ( part() && ! part()->url().isEmpty() )
		part()->openURL( part()->url() ); 

	statusBar()->message( i18n("Ready.") );
}

void KXMLEditorShell::slotToggleMainToolBar()
{
	statusBar()->message( i18n("Toggle the toolbar...") );

	if( toolBar()->isHidden() )
		toolBar()->show();
	else
		toolBar()->hide();

	statusBar()->message( i18n("Ready.") );
}


void KXMLEditorShell::slotToggleStatusBar()
{
	statusBar()->message( i18n("Toggle the statusbar...") );

	if( statusBar()->isHidden() )
		statusBar()->show();
	else
		statusBar()->hide();

	statusBar()->message( i18n("Ready.") );
}

void KXMLEditorShell::slotConfigureKeys()
{
	KKeyDialog dlg;
	dlg.insert(part()->actionCollection());
	dlg.insert(actionCollection());
	dlg.configure();
}

void KXMLEditorShell::slotConfigureToolbars()
{
	KEditToolbar dlg( factory() );
	if ( dlg.exec() )
		createGUI( part() );
}

/////////////////////////////////////////////////////////////////////
//                    ADDITIONAL FUNCTIONS
/////////////////////////////////////////////////////////////////////

bool KXMLEditorShell::openURL( const KURL & url )
{
	if ( url.isEmpty() )
		return false;

	// About this function, the style guide (
	// http://developer.kde.org/documentation/standards/kde/style/basics/index.html )
	// says that it should open a new window if the document is _not_
	// in its initial state.  This is what we do here.

	// If there is no part yet created, create new one...
	if (!part())
  {
		setPart(createXMLPart(this));
	}

	// We have to differenciate, if this shell could
	// be used or a new one has to be created.
	if ( part()->url().isEmpty() && ! part()->isModified() )
	{
		// this one can be used - try to open the file in it
		if ( ! part()->openURL(url) )
			return false;
	}
	else
	{
		// a new one has to be created
		KXMLEditorShell * pShell = new KXMLEditorShell;
		// and a new part too...
		pShell->setPart(createXMLPart(pShell));

		// try to open the file in the shell
		if ( ! pShell->part()->openURL(url) )
			return false;

		// show the new shell
		pShell->show();
	}

	// on success, the URL could be added to our list of recent files
	m_pActFileOpenRecent->addURL(url);

	return true;
}

bool KXMLEditorShell::queryClose()
{
	if ( part() && ! part()->closeURL() )
		return false;

	m_pActFileOpenRecent->saveEntries( instance()->config() );

	return KParts::MainWindow::queryClose();
}

void KXMLEditorShell::saveProperties( TDEConfig * pConfig )
{
	if (part())
	{
		pConfig->writeEntry( "CurrentURL", part()->url().url() );
	}
	KParts::MainWindow::saveProperties(pConfig);
}

void KXMLEditorShell::readProperties( TDEConfig * pConfig )
{
	KParts::MainWindow::readProperties(pConfig);

/// @todo Check if ths is a deadcode
//	TQString strURL = pConfig->readEntry( "CurrentURL", "" );
//	if ( strURL.isEmpty())
//	part()->openURL( strURL );
}

void KXMLEditorShell::slotEnableBrowserExtActions( const char * pszName, bool bEnabled )
{
	TDEAction * pAct = actionCollection()->action( pszName );
	if ( pAct )
		pAct->setEnabled( bEnabled );
	else
		kdDebug() << "KXMLEditor " << k_funcinfo << " unknown action (" << pszName << ")" << endl;
}

// Set main window caption (by using the base class functionality),
// Enables/disables Save button (corresponding to the parts modified status)
void KXMLEditorShell::setCaption( const TQString & strCaption )
{
	if (part())
	{
	  KParts::MainWindow::setCaption( strCaption, part()->isModified() ); // base class functionality
	}
}


//
// Called when creating new file is requested.
//
void KXMLEditorShell::slotActFileNew()
{

	// About this function, the style guide (
	// http://developer.kde.org/documentation/standards/kde/style/basics/index.html )
	// says that it should open a new window if the document is _not_
	// in its initial state.  This is what we do here..
	KXMLEditorShell * pShell;
	KParts::ReadWritePart* pPart;

	// 1. This shell is empty
	if (!part())
	{
		pPart = createXMLPart(this);
		pShell=this;
	}
	// 2. The shell contains modified part
	else if (! part()->url().isEmpty() || part()->isModified() )
	{
		pShell = new KXMLEditorShell;
		pPart = createXMLPart(pShell);
		pShell->show();
	}
	// 3. The shell contains part, and it is empty and not modified
	else
	{
		pPart=part();
		pShell=this;
	}

	pShell->setPart(pPart);
	// if there is a way to determine in KXMLEditorPart that it was created "for new file" then
	// this part should be changed.
	((KXMLEditorPart *)pPart)->document()->newFile();
}

void KXMLEditorShell::mail()
{
	// L.V.: tried to meve save functionality to Part
	//slotFileSave();							
	
	// first - our XML file have to be saved.
	part()->save();
	
	if (!part()->url().isEmpty())
	{
		TQStringList urls(part()->url().url());			// we can prepare then list of attachments (1 element in the list)
		TQString theSubject(part()->url().fileName(false));	// and set the mail subject
		kapp->invokeMailer(TQString::null,				// finally we can compose e-mail (addressed to no-one) out of that
						TQString::null,
						TQString::null,
						theSubject,				// subject
						TQString::null,				// body
						TQString::null,
						urls);						// attachments
	}
}

void KXMLEditorShell::setPart(KParts::ReadWritePart* pPart)
{
	if (pPart==m_pPart)
		return;	// nothing to do here...
	else
	{
		if(pPart)
		{
			connect(static_cast <KXMLEditorPart *> (pPart), SIGNAL(sigAddRecentURL(const KURL &)), 
							this, SLOT(slotAddRecentURL(const KURL &)) );
		}
	}

	if (m_pPart)
	{
		factory()->removeClient(m_pPart);
		delete m_pPart;
	}
	m_pPart = pPart;
	if (pPart)
	{
		// get the parts browser extension and connect additional actions to it
		KParts::BrowserExtension * pBrowserExt = 0;
		if ( (pBrowserExt = KParts::BrowserExtension::childObject(pPart)) )  // if there is one
		{
			connect( pBrowserExt, SIGNAL(enableAction( const char *, bool )), this, SLOT(slotEnableBrowserExtActions( const char *, bool )) );

			// L.V. moved to part. connect(pActPrint,SIGNAL(activated()),pBrowserExt, SLOT(print()));
			//pActPrint->setEnabled(pBrowserExt->isActionEnabled("print"));
		}

		createGUI(pPart);
		setCentralWidget(pPart->widget() );
		pPart->widget()->show();
		stateChanged("part");
	}
	else
	{
		createGUI(0L);
		setCentralWidget(0L);
		stateChanged("empty");
	}

	// keep pointer to window menu
	m_windowMenu = static_cast<TDEPopupMenu*>(factory()->container("window", this));
	if(!m_windowMenu)
		kdDebug() << "KXMLEditorShell::setPart - m_windowMenu is NULL" << endl;
	else
		connect(m_windowMenu,SIGNAL(aboutToShow()),this,SLOT(slotBeforeWindowMenuShown()));
}

KParts::ReadWritePart* KXMLEditorShell::createXMLPart(TQObject *parent)
{
	return  createPart(parent,"libkxmleditorpart", "KParts::ReadWritePart");
}

KParts::ReadWritePart* KXMLEditorShell::createPart(TQObject *parent, const TQString& libName, const TQString& className)
{
	KParts::ReadWritePart* newPart = 0L;
	KLibFactory * pFactory = KLibLoader::self()->factory(libName.local8Bit());
	if (pFactory)
		newPart = static_cast <KParts::ReadWritePart*> ( pFactory->create( parent, "KXMLEditor view", className.utf8() ) );
	else
		kdFatal() << "KXMLEditorShell::KXMLEditorShell no " << libName << " found" << endl;
	return newPart;
}

void KXMLEditorShell::slotActClose()
{
	if (part() && part()->queryClose())
		setPart(0L);
}

void KXMLEditorShell::close()
{
	if (queryClose())
	{
		if (TDEMainWindow::memberList->count()==1)
			kapp->deref();
		delete this;
	}
}

void KXMLEditorShell::slotActWindowCloseAllOthers()
{
	TQPtrListIterator<TDEMainWindow> it(*TDEMainWindow::memberList);
	for (it.toFirst();it.current();++it)
	{
		if (it.current()!=this)
		it.current()->close();
	}
}

    void KXMLEditorShell::slotActWindowClose()
{
	this->close();
}

void KXMLEditorShell::slotActWindowCloseAll()
{
	slotActWindowCloseAllOthers();
	slotActWindowClose();
}

KXEShellManager* KXMLEditorShell::manager()
{
	if (!s_manager)
	{
		s_manager = new KXEShellManager();
	}
	return s_manager;
}

void KXMLEditorShell::slotBeforeWindowMenuShown()
{
	// first we have to clean all shell items...
	m_windowMenu->clear();

	// then we plug-in neccessary actions...
	pActCloseW->plug(m_windowMenu);
	pActCloseAllW->plug(m_windowMenu);
	pActCloseAllO->plug(m_windowMenu);
	m_windowMenu->insertSeparator();

	// and then we can add entries for all shells
	// that are registered in Shell Manager...
	TQPtrListIterator<KXMLEditorShell> it (*(manager()->shells()));
	for (it.toFirst(); it.current();++it)
	{
		int id = m_windowMenu->insertItem(it.current()->caption(),it.current(),SLOT(slotActivate()));
		m_windowMenu->setItemChecked(id,it.current()==this);
	}
}

void KXMLEditorShell::slotActivate()
{
	/// @todo windows have to be activated over EWMH
	showNormal();			// show it if it's minimized
	raise();				// bring that to front
}

// Add URL to recent file list
void KXMLEditorShell::slotAddRecentURL(const KURL &url)
{
	m_pActFileOpenRecent->addURL(url);
}

#include "kxmleditorshell.moc"