summaryrefslogtreecommitdiffstats
path: root/krusader/KViewer/krviewer.cpp
blob: f6e0357500c300d94a0811b7fd3f927a90539418 (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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
/***************************************************************************
                         krviewer.cpp  -  description
                            -------------------
   begin                : Thu Apr 18 2002
   copyright            : (C) 2002 by Shie Erlich & Rafi Yanai
   email                :
***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/ 
// TQt includes
#include <tqdatastream.h>
#include <tqfile.h>
#include <tqpopupmenu.h> 
#include <tqtimer.h>
// TDE includes
#include <tdemenubar.h>
#include <kmimetype.h>
#include <tdelocale.h>
#include <tdeparts/part.h>
#include <tdeparts/componentfactory.h>
#include <tdemessagebox.h>
#include <klibloader.h>
#include <ktrader.h>
#include <tdeio/netaccess.h>
#include <tdeio/jobclasses.h>
#include <tdeio/job.h>
#include <kstatusbar.h>
#include <kdebug.h>
#include <klargefile.h>
#include <tdehtml_part.h>
#include <kprocess.h>
#include <tdefileitem.h> 
// Krusader includes
#include "krviewer.h"
#include "../krusader.h"
#include "../defaults.h"
#include "../kicons.h"

#include "panelviewer.h"

#define VIEW_ICON     "viewmag"
#define EDIT_ICON     "edit"
#define MODIFIED_ICON "document-save-as"


TQPtrList<KrViewer> KrViewer::viewers;

KrViewer::KrViewer( TQWidget *parent, const char *name ) :
KParts::MainWindow( parent, name ), manager( this, this ), tabBar( this ), returnFocusTo( 0 ), returnFocusTab( 0 ),
                                    reservedKeys(), reservedKeyIDs() {

	//setWFlags(WType_TopLevel | WDestructiveClose);
	setXMLFile( "krviewer.rc" ); // kpart-related xml file
	setHelpMenuEnabled( false );

	setAutoSaveSettings( "KrViewerWindow", true );
	tmpFile.setAutoDelete( true );

	connect( &manager, TQ_SIGNAL( activePartChanged( KParts::Part* ) ),
	         this, TQ_SLOT( createGUI( KParts::Part* ) ) );
	connect( &tabBar, TQ_SIGNAL( currentChanged( TQWidget *) ),
	         this, TQ_SLOT( tabChanged(TQWidget*) ) );
	connect( &tabBar, TQ_SIGNAL( closeRequest( TQWidget *) ),
	         this, TQ_SLOT( tabCloseRequest(TQWidget*) ) );

	tabBar.setTabReorderingEnabled(false);
#if KDE_IS_VERSION(3,4,0)
	tabBar.setAutomaticResizeTabs(true);
#endif
//	"edit"
//	"document-save-as"
	setCentralWidget( &tabBar );

	printAction = KStdAction::print( this, TQ_SLOT( print() ), 0, 0 );
	copyAction = KStdAction::copy( this, TQ_SLOT( copy() ), 0, 0 );

	viewerMenu = new TQPopupMenu( this );
	viewerMenu->insertItem( i18n( "&Generic viewer" ), this, TQ_SLOT( viewGeneric() ), CTRL + SHIFT + Key_G, 1 );
	viewerMenu->insertItem( i18n( "&Text viewer" ), this, TQ_SLOT( viewText() ), CTRL + SHIFT + Key_T, 2 );
	viewerMenu->insertItem( i18n( "&Hex viewer" ), this, TQ_SLOT( viewHex() ), CTRL + SHIFT + Key_H, 3 );
	viewerMenu->insertSeparator();
	viewerMenu->insertItem( i18n( "Text &editor" ), this, TQ_SLOT( editText() ), CTRL + SHIFT + Key_E, 4 );
	viewerMenu->insertSeparator();
	viewerMenu->insertItem( i18n( "&Next tab" ), this, TQ_SLOT( nextTab() ), ALT+Key_Right );
	viewerMenu->insertItem( i18n( "&Previous tab" ), this, TQ_SLOT( prevTab() ), ALT+Key_Left );

	detachActionIndex = viewerMenu->insertItem( i18n( "&Detach tab" ), this, TQ_SLOT( detachTab() ), CTRL + SHIFT + Key_D );
	//no point in detaching only one tab..
	viewerMenu->setItemEnabled(detachActionIndex,false);	
	viewerMenu->insertSeparator();
	viewerMenu->insertItem( printAction->text(), this, TQ_SLOT( print() ), printAction->shortcut() );
	viewerMenu->insertItem( copyAction->text(), this, TQ_SLOT( copy() ), copyAction->shortcut() );
	viewerMenu->insertSeparator();
	tabCloseID = viewerMenu->insertItem( i18n( "&Close current tab" ), this, TQ_SLOT( tabCloseRequest() ), Key_Escape );
	closeID = viewerMenu->insertItem( i18n( "&Quit" ), this, TQ_SLOT( close() ), CTRL + Key_Q );

	//toolBar() ->insertLined("Edit:",1,"",this,"",true ,i18n("Enter an URL to edit and press enter"));
	
	tabBar.setHoverCloseButton(true);

	checkModified();
}

KrViewer::~KrViewer() {

	disconnect( &manager, TQ_SIGNAL( activePartChanged( KParts::Part* ) ),
	            this, TQ_SLOT( createGUI( KParts::Part* ) ) );

	viewers.remove( this );
	delete printAction;
	delete copyAction;
}

void KrViewer::createGUI( KParts::Part* part ) {
	if ( part == 0 )   /*     TDEHTMLPart calls this function with 0 at destruction.    */
		return ;        /*   Can cause crash after JavaScript self.close() if removed  */

	
	// and show the new part widget
	connect( part, TQ_SIGNAL( setStatusBarText( const TQString& ) ),
	         this, TQ_SLOT( slotSetStatusBarText( const TQString& ) ) );

	KParts::MainWindow::createGUI( part );
	toolBar() ->insertLineSeparator(0);

	PanelViewerBase *pvb = getPanelViewerBase( part );
	if( pvb )
		updateActions( pvb );

	toolBar() ->show();
	statusBar() ->show();

	// the KParts part may override the viewer shortcuts. We prevent it
	// by installing an event filter on the menuBar() and the part
	reservedKeys.clear();
	reservedKeyIDs.clear();
	
	// getting the key sequences of the viewer menu
	for( unsigned w=0; w != viewerMenu->count(); w++ )
	{
		int id = viewerMenu->idAt( w );
		TQKeySequence sequence = viewerMenu->accel( id );
		if( sequence.count() > 0 )
		{
			reservedKeys.push_back( sequence[ 0 ] );
			reservedKeyIDs.push_back( id );
		}
	}
	
	// and "fix" the menubar
	menuBar() ->removeItem( 70 );
	menuBar() ->insertItem( i18n( "&KrViewer" ), viewerMenu, 70 );
	menuBar() ->show();
	
	// filtering out the key events
	menuBar() ->installEventFilter( this );
	part->installEventFilter( this );
}

bool KrViewer::eventFilter (  TQObject * /* watched */, TQEvent * e )
{
	if( e->type() == TQEvent::AccelOverride )
	{
		TQKeyEvent* ke = (TQKeyEvent*) e;
		if( reservedKeys.contains( ke->key() ) )
		{
			ke->accept();
			
			int id = reservedKeyIDs[ reservedKeys.findIndex( ke->key() ) ];
			if( id != -1 )
			{
				// don't activate the close functions immediately!
				// it can cause crash
				if( id == tabCloseID )
					TQTimer::singleShot( 0, this, TQ_SLOT( tabCloseRequest() ) );
				else if( id == closeID )
					TQTimer::singleShot( 0, this, TQ_SLOT( close() ) );
				else {
					int index = viewerMenu->indexOf( id );
					viewerMenu->activateItemAt( index );
				}
			}
			return true;
		}
	}
	else if( e->type() == TQEvent::KeyPress )
	{
		TQKeyEvent* ke = (TQKeyEvent*) e;
		if( reservedKeys.contains( ke->key() ) )
		{
			ke->accept();
			return true;
		}
	}
	return false;
}
void KrViewer::keyPressEvent( TQKeyEvent *e ) {
	switch ( e->key() ) {
		case Key_F10:
			close();
			break;
		case Key_Escape:
			tabCloseRequest();
			break;
		default:
			e->ignore();
			break;
	}
}

KrViewer* KrViewer::getViewer(bool new_window){
	if( !new_window ){
		if( !viewers.first() ){	
			viewers.prepend( new KrViewer() ); // add to first (active)
		}
		else {
			if( viewers.first()->isMinimized() ) // minimized? -> show it again
				viewers.first()->showNormal();
			viewers.first()->raise();
			viewers.first()->setActiveWindow();
		}
		return viewers.first();
	}
	else {
		KrViewer *newViewer = new KrViewer();
		viewers.prepend( newViewer );
		return newViewer;
	}
}	

void KrViewer::view( KURL url, TQWidget * parent ) {
	Mode defaultMode = Generic;
	bool defaultWindow = false;

	krConfig->setGroup( "General" );
	defaultWindow = krConfig->readBoolEntry( "View In Separate Window",_ViewInSeparateWindow );

	TQString modeString = krConfig->readEntry( "Default Viewer Mode","generic" );

	if( modeString == "generic" ) defaultMode = Generic;
	else if( modeString == "text" ) defaultMode = Text;
	else if( modeString == "hex" ) defaultMode = Hex;

	view(url,defaultMode,defaultWindow, parent );
}

void KrViewer::view( KURL url, Mode mode,  bool new_window, TQWidget * parent ) {
	KrViewer* viewer = getViewer(new_window);

	PanelViewerBase* viewWidget = new PanelViewer(&viewer->tabBar);
	KParts::Part* part = viewWidget->openURL(url,mode);
	viewer->addTab(viewWidget,i18n( "Viewing" ),VIEW_ICON,part);

	viewer->returnFocusTo = parent;
	viewer->returnFocusTab = viewWidget;
}

void KrViewer::edit( KURL url, TQWidget * parent ) {
	edit( url, Text, -1, parent );
}

void KrViewer::edit( KURL url, Mode mode, int new_window, TQWidget * parent ) {
	krConfig->setGroup( "General" );
	TQString edit = krConfig->readEntry( "Editor", _Editor );
	
	if( new_window == -1 )
		new_window = krConfig->readBoolEntry( "View In Separate Window",_ViewInSeparateWindow );

	if ( edit != "internal editor" ) {
		TDEProcess proc;
		// if the file is local, pass a normal path and not a url. this solves
		// the problem for editors that aren't url-aware
		if ( url.isLocalFile() )
			proc << TQStringList::split( ' ', edit ) << url.path();
		else proc << TQStringList::split( ' ', edit ) << url.prettyURL();
		if ( !proc.start( TDEProcess::DontCare ) )
			KMessageBox::sorry( krApp, i18n( "Can't open " ) + "\"" + edit + "\"" );
		return ;
	}

	KrViewer* viewer = getViewer(new_window);

	PanelViewerBase* editWidget = new PanelEditor(&viewer->tabBar);
	KParts::Part* part = editWidget->openURL(url,mode);
	viewer->addTab(editWidget,i18n("Editing"),EDIT_ICON,part);
	
	viewer->returnFocusTo = parent;
	viewer->returnFocusTab = editWidget;
}

void KrViewer::addTab(PanelViewerBase* pvb, TQString msg, TQString iconName ,KParts::Part* part){
	if( !part ) return;

	KURL url = pvb->url();
	setCaption( msg+": " + url.prettyURL() );

	TQIconSet icon = TQIconSet(krLoader->loadIcon(iconName,TDEIcon::Small));

	manager.addPart( part, this );
	manager.setActivePart( part );
	tabBar.insertTab(pvb,icon,url.fileName()+"("+msg+")");	
	tabBar.setCurrentPage(tabBar.indexOf(pvb));
	tabBar.setTabToolTip(pvb,msg+": " + url.prettyURL());

	updateActions( pvb );

	// now we can offer the option to detach tabs (we have more than one)
	if( tabBar.count() > 1 ){
		viewerMenu->setItemEnabled(detachActionIndex,true);	
	}

	show();
	tabBar.show();
	
	connect( pvb, TQ_SIGNAL( urlChanged( PanelViewerBase *, const KURL & ) ), 
	         this,  TQ_SLOT( tabURLChanged(PanelViewerBase *, const KURL & ) ) );
}

void KrViewer::tabURLChanged( PanelViewerBase *pvb, const KURL & url ) {
	TQString msg = pvb->isEditor() ? i18n( "Editing" ) : i18n( "Viewing" );
	tabBar.setTabLabel( pvb, url.fileName()+"("+msg+")" );
	tabBar.setTabToolTip(pvb,msg+": " + url.prettyURL());
}

void KrViewer::tabChanged(TQWidget* w){
	manager.setActivePart( static_cast<PanelViewerBase*>(w)->part() );
	
	if( static_cast<PanelViewerBase*>(w) != returnFocusTab ) {
		returnFocusTo = 0;
		returnFocusTab = 0;
	}
	
	// set this viewer to be the main viewer
	if( viewers.remove( this ) ) viewers.prepend( this ); // move to first
}

void KrViewer::tabCloseRequest(TQWidget *w){
	if( !w ) return;
	
	// important to save as returnFocusTo will be cleared at removePart
	TQWidget * returnFocusToThisWidget = returnFocusTo;
	
	PanelViewerBase* pvb = static_cast<PanelViewerBase*>(w);
	
	if( !pvb->queryClose() )
		return;
		
	manager.removePart(pvb->part());
	
	pvb->closeURL();
	
	tabBar.removePage(w);

	if( tabBar.count() <= 0 ){
		if( returnFocusToThisWidget ){ 
			returnFocusToThisWidget->raise();
			returnFocusToThisWidget->setActiveWindow();
		}
		else {
			krApp->raise();
			krApp->setActiveWindow();
		}
		delete this;
		return;
	} else if( tabBar.count() == 1 ){
		//no point in detaching only one tab..
		viewerMenu->setItemEnabled(detachActionIndex,false);
	}

	if( returnFocusToThisWidget ){ 
		returnFocusToThisWidget->raise();
		returnFocusToThisWidget->setActiveWindow();
	}
}

void KrViewer::tabCloseRequest(){
	tabCloseRequest( tabBar.currentPage() ); 
}

bool KrViewer::queryClose() {
	for( int i=0; i != tabBar.count(); i++ ) {
		PanelViewerBase* pvb = static_cast<PanelViewerBase*>( tabBar.page( i ) );
		if( !pvb )
			continue;
		
		tabBar.setCurrentPage( i );
		
		if( !pvb->queryClose() )
			return false;
	}
	return true;
}

bool KrViewer::queryExit() {
	return true; // don't let the reference counter reach zero
}

void KrViewer::viewGeneric(){
	PanelViewerBase* pvb = static_cast<PanelViewerBase*>( tabBar.currentPage() );
	if( !pvb ) return;

	PanelViewerBase* viewerWidget = new PanelViewer(&tabBar);
	KParts::Part* part = viewerWidget->openURL(pvb->url(),Generic);
	addTab(viewerWidget,i18n("Viewing"),VIEW_ICON,part);
}

void KrViewer::viewText(){
	PanelViewerBase* pvb = static_cast<PanelViewerBase*>( tabBar.currentPage() );
	if( !pvb ) return;

	PanelViewerBase* viewerWidget = new PanelViewer(&tabBar);
	KParts::Part* part = viewerWidget->openURL(pvb->url(),Text);
	addTab(viewerWidget,i18n("Viewing"),VIEW_ICON,part);
}

void KrViewer::viewHex(){
	PanelViewerBase* pvb = static_cast<PanelViewerBase*>( tabBar.currentPage() );
	if( !pvb ) return;

	PanelViewerBase* viewerWidget = new PanelViewer(&tabBar);
	KParts::Part* part = viewerWidget->openURL(pvb->url(),Hex);
	addTab(viewerWidget,i18n("Viewing"),VIEW_ICON,part);
}

void KrViewer::editText(){
	PanelViewerBase* pvb = static_cast<PanelViewerBase*>( tabBar.currentPage() );
	if( !pvb ) return;

	PanelViewerBase* editWidget = new PanelEditor(&tabBar);
	KParts::Part* part = editWidget->openURL(pvb->url(),Text);
	addTab(editWidget,i18n("Editing"),EDIT_ICON,part);
}

void KrViewer::checkModified(){
	TQTimer::singleShot( 1000, this, TQ_SLOT(checkModified()) );

	PanelViewerBase* pvb = static_cast<PanelViewerBase*>( tabBar.currentPage() );
	if( !pvb ) return;

	if( !pvb->part()->url().equals( pvb->url(), true ) ) {
		pvb->setUrl( pvb->part()->url() );
	}

	// add a * to modified files.
	if( pvb->isModified() ){
		TQString label = tabBar.tabLabel(pvb);
		if( !label.startsWith("*" + pvb->part()->url().fileName() ) ){
			label.prepend("*");
			TQIconSet icon = TQIconSet(krLoader->loadIcon(MODIFIED_ICON,TDEIcon::Small));

			tabBar.changeTab(pvb,icon,label);
		}
	}
	// remove the * from previously modified files.
	else {
		TQString label = tabBar.tabLabel(pvb);
		if( label.startsWith("*" + pvb->part()->url().fileName() ) ){
			label = label.mid( 1 );
			TQIconSet icon = TQIconSet(krLoader->loadIcon(EDIT_ICON,TDEIcon::Small));

			tabBar.changeTab(pvb,icon,label);
		}		
	}
}

void KrViewer::nextTab(){
	int index = (tabBar.currentPageIndex()+1)%tabBar.count();
	tabBar.setCurrentPage( index );
}

void KrViewer::prevTab(){
	int index = (tabBar.currentPageIndex()-1)%tabBar.count();
	while( index < 0 ) index+=tabBar.count();
	tabBar.setCurrentPage( index );
}

void KrViewer::detachTab(){
	PanelViewerBase* pvb = static_cast<PanelViewerBase*>( tabBar.currentPage() );
	if( !pvb ) return;

	KrViewer* viewer = getViewer(true);

	manager.removePart(pvb->part());
	tabBar.removePage(pvb);
	
	if( tabBar.count() == 1 ) {
		//no point in detaching only one tab..
		viewerMenu->setItemEnabled(detachActionIndex,false);
	}
	
	pvb->reparent(&viewer->tabBar,TQPoint(0,0));

	if( pvb->isEditor() )
		viewer->addTab(pvb,i18n( "Editing" ),EDIT_ICON,pvb->part());
	else
		viewer->addTab(pvb,i18n( "Viewing" ),VIEW_ICON,pvb->part());
}

void KrViewer::windowActivationChange ( bool /* oldActive */ ) {
	if( isActiveWindow() )
		if( viewers.remove( this ) ) viewers.prepend( this ); // move to first
}

void KrViewer::print() {
	PanelViewerBase* pvb = static_cast<PanelViewerBase*>( tabBar.currentPage() );
	if( !pvb ) return;
	
	KParts::BrowserExtension * ext = KParts::BrowserExtension::childObject( pvb->part() );
	if( ext && ext->isActionEnabled( "print" ) )
		Invoker( ext, TQ_SLOT( print() ) ).invoke();
}

void KrViewer::copy() {
	PanelViewerBase* pvb = static_cast<PanelViewerBase*>( tabBar.currentPage() );
	if( !pvb ) return;
	
	KParts::BrowserExtension * ext = KParts::BrowserExtension::childObject( pvb->part() );
	if( ext && ext->isActionEnabled( "copy" ) )
		Invoker( ext, TQ_SLOT( copy() ) ).invoke();
}

PanelViewerBase * KrViewer::getPanelViewerBase( KParts::Part * part ) {
	for( int i=0; i != tabBar.count(); i++ ) {
		PanelViewerBase *pvb = static_cast<PanelViewerBase*>( tabBar.page( i ) );
		if( pvb && pvb->part() == part )
			return pvb;
	}
	return 0;
}

void KrViewer::updateActions( PanelViewerBase * pvb ) {
	if( pvb->isEditor() ) {
		printAction->unplugAll();
		copyAction->unplugAll();
	}
	else {
		if( !printAction->isPlugged( toolBar() ) )
			printAction->plug( toolBar(), 0 );
		if( !copyAction->isPlugged( toolBar() ) )
			copyAction->plug( toolBar(), 1 );
	}
}

#if 0
bool KrViewer::editGeneric( TQString mimetype, KURL _url ) {
	KParts::ReadWritePart * kedit_part = 0L;
	KLibFactory *factory = 0;
	TDETrader::OfferList offers = TDETrader::self() ->query( mimetype );

	// in theory, we only care about the first one.. but let's try all
	// offers just in case the first can't be loaded for some reason
	TDETrader::OfferList::Iterator it( offers.begin() );
	for ( ; it != offers.end(); ++it ) {
		KService::Ptr ptr = ( *it );
		// we now know that our offer can handle mimetype and is a part.
		// since it is a part, it must also have a library... let's try to
		// load that now
		factory = KLibLoader::self() ->factory( ptr->library().latin1() );
		if ( factory ) {
			kedit_part = static_cast<KParts::ReadWritePart *>( factory->create( this,
			             ptr->name().latin1(), "KParts::ReadWritePart" ) );
			if ( kedit_part )
				if ( kedit_part->openURL( _url ) ) break;
				else {
					delete kedit_part;
					kedit_part = 0L;
				}
		}
	}

	if ( !kedit_part ) {
		KMessageBox::error( this, i18n( "Sorry, can't find internal editor" ) );
		return false;
	}

	setCentralWidget( kedit_part->widget() );
	createGUI( kedit_part );
	kedit_part->widget() ->show();
	return true;
}

bool KrViewer::editText( bool create ) {
	if ( !editor_part ) {
		editor_part = static_cast<KParts::ReadWritePart*>( getPart( url, "text/plain", false, create ) );
		if ( !editor_part ) return false;
		manager.addPart( editor_part, this );
	}
	manager.setActivePart( editor_part );
	tabBar.addTab(editor_part->widget(),url.fileName());
	return true;
}

bool KrViewer::viewGeneric() {
	TQString mimetype = KMimeType::findByURL( url ) ->name();
	// ugly hack: don't try to get a part for an XML file, it usually don't work
	if ( mimetype == "text/xml" ) return false;
	if ( url.prettyURL().startsWith( "man:" ) ) mimetype = "text/html";
	if ( mimetype == "text/plain" )
		viewerMenu->setItemEnabled( 1, false );

	if ( !generic_part ) {
		if ( mimetype.contains( "html" ) ) {
			TDEHTMLPart * p = new TDEHTMLPart( this, 0, 0, 0, TDEHTMLPart::BrowserViewGUI );
			connect( p->browserExtension(), TQ_SIGNAL( openURLRequest( const KURL &, const KParts::URLArgs & ) ),
			         this, TQ_SLOT( handleOpenURLRequest( const KURL &, const KParts::URLArgs & ) ) );
			/* At JavaScript self.close() the TDEHTMLPart destroys itself.  */
			/* After destruction, just close the window */
			connect( p, TQ_SIGNAL( destroyed() ), this, TQ_SLOT( close() ) );

			p-> openURL( url );
			generic_part = p;
		} else {
			generic_part = static_cast<KParts::ReadOnlyPart*>( getPart( url, mimetype, true ) );
		}
		if ( generic_part ) manager.addPart( generic_part, this );
		
		else return false;
	}

	manager.setActivePart( generic_part );
	tabBar.addTab(generic_part->widget(),url.fileName());
	return true;
}

bool KrViewer::viewText() {
	if ( !text_part ) {
		text_part = static_cast<KParts::ReadOnlyPart*>( getPart( url, "text/plain", true ) );
		if ( !text_part ) return false;
		manager.addPart( text_part, this );
	}
	manager.setActivePart( text_part );
	tabBar.addTab(text_part->widget(),url.fileName());
	return true;
}

void KrViewer::viewHex() {
	if ( !hex_part ) {
		TQString file;
		// files that are not local must first be downloaded
		if ( !url.isLocalFile() ) {
			if ( !TDEIO::NetAccess::download( url, file ) ) {
				KMessageBox::sorry( this, i18n( "KrViewer is unable to download: " ) + url.url() );
				return ;
			}
		} else file = url.path();


		// create a hex file
		TQFile f_in( file );
		f_in.open( IO_ReadOnly );
		TQDataStream in( &f_in );

		FILE *out = KDE_fopen( tmpFile.name().local8Bit(), "w" );

		TDEIO::filesize_t fileSize = f_in.size();
		TDEIO::filesize_t address = 0;
		char buf[ 16 ];
		unsigned int* pBuff = ( unsigned int* ) buf;

		while ( address < fileSize ) {
			memset( buf, 0, 16 );
			int bufSize = ( ( fileSize - address ) > 16 ) ? 16 : ( fileSize - address );
			in.readRawBytes( buf, bufSize );
			fprintf( out, "0x%8.8llx: ", address );
			for ( int i = 0; i < 4; ++i ) {
				if ( i < ( bufSize / 4 ) ) fprintf( out, "%8.8x ", pBuff[ i ] );
				else fprintf( out, "         " );
			}
			fprintf( out, "| " );

			for ( int i = 0; i < bufSize; ++i ) {
				if ( buf[ i ] > ' ' && buf[ i ] < '~' ) fputc( buf[ i ], out );
				else fputc( '.', out );
			}
			fputc( '\n', out );

			address += 16;
		}
		// clean up
		f_in.close();
		fclose( out );
		if ( !url.isLocalFile() )
			TDEIO::NetAccess::removeTempFile( file );

		hex_part = static_cast<KParts::ReadOnlyPart*>( getPart( tmpFile.name(), "text/plain", true ) );
		if ( !hex_part ) return ;
		manager.addPart( hex_part, this );
	}
	manager.setActivePart( hex_part );
	tabBar.addTab(hex_part->widget(),url.fileName());
}
#endif


#include "krviewer.moc"