summaryrefslogtreecommitdiffstats
path: root/khelpcenter/view.cpp
blob: 5e6b8f94111ad7d06157adda273c2f3b4e30f0d3 (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
#include "view.h"

#include "formatter.h"
#include "history.h"

#include <dom/html_document.h>
#include <dom/html_misc.h>
#include <kaction.h>
#include <kactioncollection.h>
#include <kapplication.h>
#include <kdebug.h>
#include <khtml_settings.h>
#include <khtmlview.h>
#include <klocale.h>
#include <kpopupmenu.h>
#include <kstandarddirs.h>

#include <tqfileinfo.h>
#include <tqclipboard.h>

using namespace KHC;

View::View( TQWidget *parentWidget, const char *widgetName,
                  TQObject *parent, const char *name, KHTMLPart::GUIProfile prof, KActionCollection *col )
    : KHTMLPart( parentWidget, widgetName, parent, name, prof ), mState( Docu ), mActionCollection(col)
{
    setJScriptEnabled(false);
    setJavaEnabled(false);
    setPluginsEnabled(false);

    mFormatter = new Formatter;
    if ( !mFormatter->readTemplates() ) {
      kdDebug() << "Unable to read Formatter templates." << endl;
    }

    m_zoomStepping = 10;

    connect( this, TQT_SIGNAL( setWindowCaption( const TQString & ) ),
             this, TQT_SLOT( setTitle( const TQString & ) ) );
    connect( this, TQT_SIGNAL( popupMenu( const TQString &, const TQPoint& ) ),
             this, TQT_SLOT( showMenu( const TQString &, const TQPoint& ) ) );
             
    TQString css = langLookup("common/kde-default.css");
    if (!css.isEmpty())
    {
       TQFile css_file(css);
       if (css_file.open(IO_ReadOnly))
       {
          TQTextStream s(&css_file);
          TQString stylesheet = s.read();
          preloadStyleSheet("help:/common/kde-default.css", stylesheet);
       }
    }

    view()->installEventFilter( this );
}

View::~View()
{
  delete mFormatter;
}

void View::copySelectedText()
{
  kapp->clipboard()->setText( selectedText() );
}

bool View::openURL( const KURL &url )
{
    if ( url.protocol().lower() == "about" )
    {
        showAboutPage();
        return true;
    }
    mState = Docu;
    return KHTMLPart::openURL( url );
}

void View::saveState( TQDataStream &stream )
{
    stream << mState;
    if ( mState == Docu )
        KHTMLPart::saveState( stream );
}

void View::restoreState( TQDataStream &stream )
{
    stream >> mState;
    if ( mState == Docu )
        KHTMLPart::restoreState( stream );
    else if ( mState == About )
        showAboutPage();
}

void View::showAboutPage()
{
    TQString file = locate( "data", "khelpcenter/intro.html.in" );
    if ( file.isEmpty() )
        return;

    TQFile f( file );

    if ( !f.open( IO_ReadOnly ) )
    return;

    mState = About;

    emit started( 0 );

    TQTextStream t( &f );

    TQString res = t.read();

    res = res.arg( i18n("Conquer your Desktop!") )
          .arg( langLookup( "khelpcenter/konq.css" ) )
          .arg( langLookup( "khelpcenter/pointers.png" ) )
          .arg( langLookup( "khelpcenter/khelpcenter.png" ) )
          .arg( i18n("Help Center") )
          .arg( langLookup( "khelpcenter/lines.png" ) )
          .arg( i18n( "Welcome to the K Desktop Environment" ) )
          .arg( i18n( "The KDE team welcomes you to user-friendly UNIX computing" ) )
          .arg( i18n( "KDE is a powerful graphical desktop environment for UNIX workstations. A\n"
                      "KDE desktop combines ease of use, contemporary functionality and outstanding\n"
                      "graphical design with the technological superiority of the UNIX operating\n"
                      "system." ) )
          .arg( i18n( "What is the K Desktop Environment?" ) )
          .arg( i18n( "Contacting the KDE Project" ) )
          .arg( i18n( "Supporting the KDE Project" ) )
          .arg( i18n( "Useful links" ) )
          .arg( i18n( "Getting the most out of KDE" ) )
          .arg( i18n( "General Documentation" ) )
          .arg( i18n( "A Quick Start Guide to the Desktop" ) )
          .arg( i18n( "KDE Users' guide" ) )
          .arg( i18n( "Frequently asked questions" ) )
          .arg( i18n( "Basic Applications" ) )
          .arg( i18n( "The Kicker Desktop Panel" ) )
          .arg( i18n( "The KDE Control Center" ) )
          .arg( i18n( "The Konqueror File manager and Web Browser" ) )
          .arg( langLookup( "khelpcenter/kdelogo2.png" ) );
    begin( KURL( "about:khelpcenter" ) );
    write( res );
    end();
    emit completed();
}

TQString View::langLookup( const TQString &fname )
{
    TQStringList search;

    // assemble the local search paths
    const TQStringList localDoc = KGlobal::dirs()->resourceDirs("html");

    // look up the different languages
    for (int id=localDoc.count()-1; id >= 0; --id)
    {
        TQStringList langs = KGlobal::locale()->languageList();
        langs.append( "en" );
        langs.remove( "C" );
        TQStringList::ConstIterator lang;
        for (lang = langs.begin(); lang != langs.end(); ++lang)
            search.append(TQString("%1%2/%3").arg(localDoc[id]).arg(*lang).arg(fname));
    }

    // try to locate the file
    TQStringList::Iterator it;
    for (it = search.begin(); it != search.end(); ++it)
    {
        TQFileInfo info(*it);
        if (info.exists() && info.isFile() && info.isReadable())
            return *it;

        // Fall back to the index.docbook for this language if we couldn't find its
        // specific docbook file. If we are not looking up docbook (images,
        // css etc) then look in other languages first.
        if ( ( *it ).endsWith( "docbook" ) )
        {
            TQString file = (*it).left((*it).findRev('/')) + "/index.docbook";
            info.setFile(file);
            if (info.exists() && info.isFile() && info.isReadable())
            {
                return *it;
            }
        }
    }

    return TQString::null;
}

void View::setTitle( const TQString &title )
{
    mTitle = title;
}

void View::beginSearchResult()
{
  mState = Search;

  begin();
  mSearchResult = "";
}

void View::writeSearchResult( const TQString &str )
{
  write( str );
  mSearchResult += str;
}

void View::endSearchResult()
{
  end();
  if ( !mSearchResult.isEmpty() ) emit searchResultCacheAvailable();
}

void View::beginInternal( const KURL &url )
{
  mInternalUrl = url;
  begin();
}

KURL View::internalUrl() const
{
  return mInternalUrl;
}

void View::lastSearch()
{
  if ( mSearchResult.isEmpty() ) return;
 
  mState = Search;
  
  begin();
  write( mSearchResult );
  end();
}

void View::slotIncFontSizes()
{
  setZoomFactor( zoomFactor() + m_zoomStepping );
}

void View::slotDecFontSizes()
{
  setZoomFactor( zoomFactor() - m_zoomStepping );
}

void View::showMenu( const TQString& url, const TQPoint& pos)
{
  KPopupMenu* pop = new KPopupMenu(view());
  if (url.isEmpty())
  {
    KAction *action;
    action = mActionCollection->action("go_home");
    if (action) action->plug(pop);

    pop->insertSeparator();

    action = mActionCollection->action("prevPage");
    if (action) action->plug(pop);
    action = mActionCollection->action("nextPage");
    if (action) action->plug(pop);

    pop->insertSeparator();

    History::self().m_backAction->plug(pop);
    History::self().m_forwardAction->plug(pop);
  }
  else
  {
    pop->insertItem(i18n("Copy Link Address"), this, TQT_SLOT(slotCopyLink()));
    mCopyURL = completeURL(url).url();
  }
	
  pop->exec(pos);
  delete pop;
}

void View::slotCopyLink()
{
  TQApplication::clipboard()->setText(mCopyURL);
}

bool View::prevPage(bool checkOnly)
{
  const DOM::HTMLCollection links = htmlDocument().links();

  // The first link on a page (top-left corner) would be the Prev link.
  const DOM::Node prevLinkNode = links.item( 0 );
  KURL prevURL = urlFromLinkNode( prevLinkNode );
  if (!prevURL.isValid())
    return false;

  if (!checkOnly)
    openURL( prevURL );
  return true;
}

bool View::nextPage(bool checkOnly)
{
  const DOM::HTMLCollection links = htmlDocument().links();

  KURL nextURL;

  // If we're on the first page, the "Next" link is the last link
  if ( baseURL().path().endsWith( "/index.html" ) )
    nextURL = urlFromLinkNode( links.item( links.length() - 1 ) );
  else
    nextURL = urlFromLinkNode( links.item( links.length() - 2 ) );

  if (!nextURL.isValid())
    return false;

  // If we get a mail link instead of a http URL, or the next link points
  // to an index.html page (a index.html page is always the first page
  // there can't be a Next link pointing to it!) there's probably nowhere
  // to go. Next link at all.
  if ( nextURL.protocol() == "mailto" ||
       nextURL.path().endsWith( "/index.html" ) )
    return false;

  if (!checkOnly)
    openURL( nextURL );
  return true;
}

bool View::eventFilter( TQObject *o, TQEvent *e )
{
  if ( e->type() != TQEvent::KeyPress ||
       htmlDocument().links().length() == 0 )
    return KHTMLPart::eventFilter( o, e );

  TQKeyEvent *ke = TQT_TQKEYEVENT( e );
  if ( ke->state() & TQt::ShiftButton && ke->key() == Key_Space ) {
    // If we're on the first page, it does not make sense to go back.
    if ( baseURL().path().endsWith( "/index.html" ) )
      return KHTMLPart::eventFilter( o, e );

    const TQScrollBar * const scrollBar = view()->verticalScrollBar();
    if ( scrollBar->value() == scrollBar->minValue() ) {
      if (prevPage())
         return true;
    }
  } else if ( ke->key() == Key_Space ) {
    const TQScrollBar * const scrollBar = view()->verticalScrollBar();
    if ( scrollBar->value() == scrollBar->maxValue() ) {
      if (nextPage())
        return true;
    }
  }
  return KHTMLPart::eventFilter( o, e );
}

KURL View::urlFromLinkNode( const DOM::Node &n ) const
{
  if ( n.isNull() || n.nodeType() != DOM::Node::ELEMENT_NODE )
    return KURL();

  DOM::Element elem = static_cast<DOM::Element>( n );

  KURL href ( elem.getAttribute( "href" ).string() );
  if ( !href.protocol().isNull() )
    return href;

  TQString path = baseURL().path();
  path.truncate( path.findRev( '/' ) + 1 );
  path += href.url();

  KURL url = baseURL();
  url.setRef( TQString::null );
  url.setEncodedPathAndQuery( path );

  return url;
}

void View::slotReload( const KURL &url )
{
  const_cast<KHTMLSettings *>( settings() )->init( kapp->config() );
  KParts::URLArgs args = browserExtension()->urlArgs();
  args.reload = true;
  browserExtension()->setURLArgs( args );
  if ( url.isEmpty() )
    openURL( baseURL() );
  else
    openURL( url );
}

#include "view.moc"
// vim:ts=2:sw=2:et