summaryrefslogtreecommitdiffstats
path: root/lib/widgets/kdevhtmlpart.cpp
blob: c99dcf36b3d7bd662fb1ff770a13ab205cf74ffd (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
#include <tqfile.h>
#include <tqclipboard.h>
#include <tqapplication.h>

#include <kxmlguiclient.h>
#include <kaction.h>
#include <kstdaction.h>
#include <kstandarddirs.h>
#include <klocale.h>
#include <kpopupmenu.h>
#include <kiconloader.h>
#include <kmainwindow.h>
#include <khtmlview.h>
#include <khtml_settings.h>
#include <kconfig.h>

#include <kdevmainwindow.h>


#include "kdevhtmlpart.h"

KDevHTMLPart::KDevHTMLPart()
  : KHTMLPart(0L, 0L, 0L, "KDevHTMLPart", DefaultGUI )
{
  setXMLFile(locate("data", "kdevelop/kdevhtml_partui.rc"), true);
  
  connect(browserExtension(), TQT_SIGNAL(openURLRequestDelayed(const KURL &,const KParts::URLArgs &)),
          this, TQT_SLOT(openURLRequest(const KURL &)) );

  connect(this, TQT_SIGNAL(started(KIO::Job *)), this, TQT_SLOT(slotStarted(KIO::Job* )));
  connect(this, TQT_SIGNAL(completed()), this, TQT_SLOT(slotCompleted()));
  connect(this, TQT_SIGNAL(canceled(const TQString &)), this, TQT_SLOT(slotCancelled(const TQString &)));

  KActionCollection * actions = actionCollection();// new KActionCollection( this );
  reloadAction = new KAction( i18n( "Reload" ), "reload", 0,
    this, TQT_SLOT( slotReload() ), actions, "doc_reload" );
  reloadAction->setWhatsThis(i18n("<b>Reload</b><p>Reloads the current document."));
  stopAction = new KAction( i18n( "Stop" ), "stop", 0,
    this, TQT_SLOT( slotStop() ), actions, "doc_stop" );
  stopAction->setWhatsThis(i18n("<b>Stop</b><p>Stops the loading of current document."));
  duplicateAction = new KAction( i18n( "Duplicate Tab" ), "window_new", 0,
    this, TQT_SLOT( slotDuplicate() ), actions, "doc_dup" );
  duplicateAction->setWhatsThis(i18n("<b>Duplicate window</b><p>Opens current document in a new window."));
  printAction = KStdAction::print(this, TQT_SLOT(slotPrint()), actions, "print_doc");
  copyAction = KStdAction::copy(this, TQT_SLOT(slotCopy()), actions, "copy_doc_selection");

  connect( this, TQT_SIGNAL(popupMenu(const TQString &, const TQPoint &)), this, TQT_SLOT(popup(const TQString &, const TQPoint &)));
  connect(this, TQT_SIGNAL(selectionChanged()), this, TQT_SLOT(slotSelectionChanged()));

//BEGIN documentation history stuff  
    
  m_backAction = new KToolBarPopupAction(i18n("Back"), "back", 0,
    this, TQT_SLOT(slotBack()),
    actions, "browser_back");
  m_backAction->setEnabled( false );
  m_backAction->setToolTip(i18n("Back"));
  m_backAction->setWhatsThis(i18n("<b>Back</b><p>Moves backwards one step in the <b>documentation</b> browsing history."));

  connect(m_backAction->popupMenu(), TQT_SIGNAL(aboutToShow()),
         this, TQT_SLOT(slotBackAboutToShow()));
  connect(m_backAction->popupMenu(), TQT_SIGNAL(activated(int)),
         this, TQT_SLOT(slotPopupActivated(int)));

  m_forwardAction = new KToolBarPopupAction(i18n("Forward"), "forward", 0,
    this, TQT_SLOT(slotForward()),
    actions, "browser_forward");
  m_forwardAction->setEnabled( false );
  m_forwardAction->setToolTip(i18n("Forward"));
  m_forwardAction->setWhatsThis(i18n("<b>Forward</b><p>Moves forward one step in the <b>documentation</b> browsing history."));

  connect(m_forwardAction->popupMenu(), TQT_SIGNAL(aboutToShow()),
         this, TQT_SLOT(slotForwardAboutToShow()));
  connect(m_forwardAction->popupMenu(), TQT_SIGNAL(activated(int)),
         this, TQT_SLOT(slotPopupActivated(int)));
  
  m_restoring = false;
  m_Current = m_history.end();
//END documentation history stuff  
  
  //settings:
  KConfig *appConfig = KGlobal::config();
  appConfig->setGroup("KHTMLPart");
  setStandardFont(appConfig->readEntry("StandardFont",
      settings()->stdFontName()));
  setFixedFont(appConfig->readEntry("FixedFont",
      settings()->fixedFontName()));
  setZoomFactor(appConfig->readEntry("Zoom", "100").toInt());
}

void KDevHTMLPart::popup( const TQString & url, const TQPoint & p )
{
//  KPopupMenu popup( i18n( "Documentation Viewer" ), this->widget() );
  KPopupMenu popup(this->widget());

  bool needSep = false;  
  int idNewWindow = -2;
  if (!url.isEmpty() && (m_options & CanOpenInNewWindow))
  {
    idNewWindow = popup.insertItem(SmallIcon("window_new"),i18n("Open in New Tab"));
    popup.setWhatsThis(idNewWindow, i18n("<b>Open in new window</b><p>Opens current link in a new window."));
    needSep = true;
  }
  if (m_options & CanDuplicate)
  {
      duplicateAction->plug(&popup);
      needSep = true;
  }
  if (needSep)
      popup.insertSeparator();
    
  m_backAction->plug( &popup );
  m_forwardAction->plug( &popup );
  reloadAction->plug(&popup);
//  stopAction->plug(&popup);
  popup.insertSeparator();

  copyAction->plug( &popup );
  popup.insertSeparator();
  
  printAction->plug(&popup);
  popup.insertSeparator();
    
  KAction * incFontAction = this->action("incFontSizes");
  KAction * decFontAction = this->action("decFontSizes");
  if ( incFontAction && decFontAction )
  {
    incFontAction->plug( &popup );
    decFontAction->plug( &popup );
    popup.insertSeparator();
  }

  KAction *ac = action("setEncoding");
  if (ac)
    ac->plug(&popup);

  int r = popup.exec(p);

  if (r == idNewWindow)
  {
    KURL kurl;
    if (!KURL(url).path().startsWith("/"))
    {
        kdDebug() << "processing relative url: " << url << endl;
        if (url.startsWith("#"))
        {
            kurl = KURL(KDevHTMLPart::url());
            kurl.setRef(url.mid(1));
        }
        else
            kurl = KURL(KDevHTMLPart::url().upURL().url(true)+url);
    }
    else
        kurl = KURL(url);

    if (kurl.isValid())
        slotOpenInNewWindow(kurl);
  }
}

void KDevHTMLPart::setContext(const TQString &context)
{
  m_context = context;
}


TQString KDevHTMLPart::context() const
{
  return m_context;
}


// Note: this function is a copy of code in kdecore/kconfigbase.cpp ;)
static bool isUtf8(const char *buf) {
  int i, n;
  register unsigned char c;
  bool gotone = false;

#define F 0   /* character never appears in text */
#define T 1   /* character appears in plain ASCII text */
#define I 2   /* character appears in ISO-8859 text */
#define X 3   /* character appears in non-ISO extended ASCII (Mac, IBM PC) */

  static const unsigned char text_chars[256] = {
  /*                  BEL BS HT LF    FF CR    */
        F, F, F, F, F, F, F, T, T, T, T, F, T, T, F, F,  /* 0x0X */
        /*                              ESC          */
        F, F, F, F, F, F, F, F, F, F, F, T, F, F, F, F,  /* 0x1X */
        T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T,  /* 0x2X */
        T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T,  /* 0x3X */
        T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T,  /* 0x4X */
        T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T,  /* 0x5X */
        T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T,  /* 0x6X */
        T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, F,  /* 0x7X */
        /*            NEL                            */
        X, X, X, X, X, T, X, X, X, X, X, X, X, X, X, X,  /* 0x8X */
        X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,  /* 0x9X */
        I, I, I, I, I, I, I, I, I, I, I, I, I, I, I, I,  /* 0xaX */
        I, I, I, I, I, I, I, I, I, I, I, I, I, I, I, I,  /* 0xbX */
        I, I, I, I, I, I, I, I, I, I, I, I, I, I, I, I,  /* 0xcX */
        I, I, I, I, I, I, I, I, I, I, I, I, I, I, I, I,  /* 0xdX */
        I, I, I, I, I, I, I, I, I, I, I, I, I, I, I, I,  /* 0xeX */
        I, I, I, I, I, I, I, I, I, I, I, I, I, I, I, I   /* 0xfX */
  };

  /* *ulen = 0; */
  for (i = 0; (c = buf[i]); i++) {
    if ((c & 0x80) == 0) {        /* 0xxxxxxx is plain ASCII */
      /*
       * Even if the whole file is valid UTF-8 sequences,
       * still reject it if it uses weird control characters.
       */

      if (text_chars[c] != T)
        return false;

    } else if ((c & 0x40) == 0) { /* 10xxxxxx never 1st byte */
      return false;
    } else {                           /* 11xxxxxx begins UTF-8 */
      int following;

    if ((c & 0x20) == 0) {             /* 110xxxxx */
      following = 1;
    } else if ((c & 0x10) == 0) {      /* 1110xxxx */
      following = 2;
    } else if ((c & 0x08) == 0) {      /* 11110xxx */
      following = 3;
    } else if ((c & 0x04) == 0) {      /* 111110xx */
      following = 4;
    } else if ((c & 0x02) == 0) {      /* 1111110x */
      following = 5;
    } else
      return false;

      for (n = 0; n < following; n++) {
        i++;
        if (!(c = buf[i]))
          goto done;

        if ((c & 0x80) == 0 || (c & 0x40))
          return false;
      }
      gotone = true;
    }
  }
done:
  return gotone;   /* don't claim it's UTF-8 if it's all 7-bit */
}
#undef F
#undef T
#undef I
#undef X

TQString KDevHTMLPart::resolveEnvVarsInURL(const TQString& url)
{
  // check for environment variables and make necessary translations
  TQString path = url;
  int nDollarPos = path.find( '$' );

  // Note: the while loop below is a copy of code in kdecore/kconfigbase.cpp ;)
  while( nDollarPos != -1 && nDollarPos+1 < static_cast<int>(path.length())) {
    // there is at least one $
    if( (path)[nDollarPos+1] == '(' ) {
      uint nEndPos = nDollarPos+1;
      // the next character is no $
      while ( (nEndPos <= path.length()) && (path[nEndPos]!=')') )
          nEndPos++;
      nEndPos++;
      TQString cmd = path.mid( nDollarPos+2, nEndPos-nDollarPos-3 );

      TQString result;
      FILE *fs = popen(TQFile::encodeName(cmd).data(), "r");
      if (fs)
      {
         TQTextStream ts(fs, IO_ReadOnly);
         result = ts.read().stripWhiteSpace();
         pclose(fs);
      }
      path.replace( nDollarPos, nEndPos-nDollarPos, result );
    } else if( (path)[nDollarPos+1] != '$' ) {
      uint nEndPos = nDollarPos+1;
      // the next character is no $
      TQString aVarName;
      if (path[nEndPos]=='{')
      {
        while ( (nEndPos <= path.length()) && (path[nEndPos]!='}') )
            nEndPos++;
        nEndPos++;
        aVarName = path.mid( nDollarPos+2, nEndPos-nDollarPos-3 );
      }
      else
      {
        while ( nEndPos <= path.length() && (path[nEndPos].isNumber()
                || path[nEndPos].isLetter() || path[nEndPos]=='_' )  )
            nEndPos++;
        aVarName = path.mid( nDollarPos+1, nEndPos-nDollarPos-1 );
      }
      const char* pEnv = 0;
      if (!aVarName.isEmpty())
           pEnv = getenv( aVarName.ascii() );
      if( pEnv ) {
        // !!! Sergey A. Sukiyazov <[email protected]> !!!
        // A environment variables may contain values in 8bit
        // locale cpecified encoding or in UTF8 encoding.
        if (isUtf8( pEnv ))
            path.replace( nDollarPos, nEndPos-nDollarPos, TQString::fromUtf8(pEnv) );
        else
            path.replace( nDollarPos, nEndPos-nDollarPos, TQString::fromLocal8Bit(pEnv) );
      } else
      path.remove( nDollarPos, nEndPos-nDollarPos );
    } else {
      // remove one of the dollar signs
      path.remove( nDollarPos, 1 );
      nDollarPos++;
    }
    nDollarPos = path.find( '$', nDollarPos );
  }

  return path;
}

bool KDevHTMLPart::openURL(const KURL &url)
{
  TQString path = resolveEnvVarsInURL(url.url());
  KURL newUrl(path);
  
  bool retval = KHTMLPart::openURL(newUrl);
  if ( retval )
  {
    emit fileNameChanged(this);
	if ( !m_restoring ) 
	{
		addHistoryEntry();
	}
  }
  
  m_backAction->setEnabled( m_Current != m_history.begin() );
  m_forwardAction->setEnabled( m_Current != m_history.fromLast() );
  
  return retval;
}

void KDevHTMLPart::openURLRequest(const KURL &url)
{
	openURL( url );
}

void KDevHTMLPart::slotReload( )
{
	openURL( url() );
}

void KDevHTMLPart::slotStop( )
{
    closeURL();
}

void KDevHTMLPart::slotStarted( KIO::Job * )
{
    stopAction->setEnabled(true);
}

void KDevHTMLPart::slotCompleted( )
{
    stopAction->setEnabled(false);
}

void KDevHTMLPart::slotCancelled( const TQString & /*errMsg*/ )
{
    stopAction->setEnabled(false);
}

/*void KDevHTMLPart::slotDuplicate( )
{
    PartController::getInstance()->showDocument(url(), true);
}*/

void KDevHTMLPart::slotPrint( )
{
    view()->print();
}

void KDevHTMLPart::slotBack()
{
	if ( m_Current != m_history.begin() )
	{
		--m_Current;
		m_restoring = true;
		openURL( (*m_Current).url );
		m_restoring = false;
	}
}

void KDevHTMLPart::slotForward()
{
	if (  m_Current != m_history.fromLast() )
	{
		++m_Current;
		m_restoring = true;
		openURL( (*m_Current).url );
		m_restoring = false;
	}
}

void KDevHTMLPart::slotBackAboutToShow()
{
	KPopupMenu *popup = m_backAction->popupMenu();
	popup->clear();

	if ( m_Current == m_history.begin() ) return;

	TQValueList<DocumentationHistoryEntry>::Iterator it = m_Current;
	--it;
	
	int i = 0;
	while( i < 10 )
	{
		if ( it == m_history.begin() )
		{
			popup->insertItem( (*it).url.url(), (*it).id );
			return;
		} 
		
		popup->insertItem( (*it).url.url(), (*it).id );
		++i;
		--it;
	} 
}

void KDevHTMLPart::slotForwardAboutToShow()
{
	KPopupMenu *popup = m_forwardAction->popupMenu();
	popup->clear();

	if ( m_Current == m_history.fromLast() ) return;

	TQValueList<DocumentationHistoryEntry>::Iterator it = m_Current;
	++it;
	
	int i = 0;
	while( i < 10 )
	{
		if ( it == m_history.fromLast() )
		{
			popup->insertItem( (*it).url.url(), (*it).id );
			return;
		} 
		
		popup->insertItem( (*it).url.url(), (*it).id );
		++i;
		++it;
	} 
}

void KDevHTMLPart::slotPopupActivated( int id )
{
	kdDebug(9000) << "id: " << id << endl;

	TQValueList<DocumentationHistoryEntry>::Iterator it = m_history.begin();
	while( it != m_history.end() )
	{
		kdDebug(9000) << "(*it).id: " << (*it).id << endl;
		if ( (*it).id == id )
		{
			m_Current = it;
			m_restoring = true;
			openURL( (*m_Current).url );
			m_restoring = false;
			return;
		}
		++it;
	}
}

void KDevHTMLPart::addHistoryEntry()
{
	TQValueList<DocumentationHistoryEntry>::Iterator it = m_Current;
	
	// if We're not already the last entry, we truncate the list here before adding an entry
	if ( it != m_history.end() && it != m_history.fromLast() )
	{
		m_history.erase( ++it, m_history.end() );
	}
	
	DocumentationHistoryEntry newEntry( url() );
		
	// Only save the new entry if it is different from the last
	if ( newEntry.url != (*m_Current).url )
	{
		m_history.append( newEntry );
		m_Current = m_history.fromLast();
	}
}

void KDevHTMLPart::slotCopy( )
{
    TQString text = selectedText();
    text.replace( TQChar( 0xa0 ), ' ' );
    QClipboard *cb = TQApplication::clipboard();
    disconnect( cb, TQT_SIGNAL( selectionChanged() ), this, TQT_SLOT( slotClearSelection() ) );
    cb->setText(text);
    connect( cb, TQT_SIGNAL( selectionChanged() ), this, TQT_SLOT( slotClearSelection() ) );
}

void KDevHTMLPart::slotSelectionChanged( )
{
    if (selectedText().isEmpty())
        copyAction->setEnabled(false);
    else
        copyAction->setEnabled(true);
}

#include "kdevhtmlpart.moc"