summaryrefslogtreecommitdiffstats
path: root/khelpcenter/history.cpp
blob: 62a434dd26432ffa89f27e65185a72e951f21906 (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
/*
 *  This file is part of the KDE Help Center
 *
 *  Copyright (C) 2002 Frerich Raabe <raabe@kde.org>
 *
 *  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.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
#include "history.h"
#include "view.h"

#include <kaction.h>
#include <kapplication.h>
#include <kdebug.h>
#include <kmainwindow.h>
#include <kpopupmenu.h>
#include <kstdguiitem.h>
#include <kstringhandler.h>



using namespace KHC;

History *History::m_instance = 0;

History &History::self()
{
  if  ( !m_instance )
    m_instance = new History;
  return *m_instance;
}

History::History() : TQObject(),
  m_goBuffer( 0 )
{
  m_entries.setAutoDelete( true );
}

History::~History()
{
}

void History::setupActions( KActionCollection *coll )
{
  TQPair<KGuiItem, KGuiItem> backForward = KStdGuiItem::backAndForward();

  m_backAction = new KToolBarPopupAction( backForward.first, ALT+Key_Left,
      this, TQT_SLOT( back() ), coll, "back" );
  connect( m_backAction->popupMenu(), TQT_SIGNAL( activated( int ) ),
           TQT_SLOT( backActivated( int ) ) );
  connect( m_backAction->popupMenu(), TQT_SIGNAL( aboutToShow() ),
           TQT_SLOT( fillBackMenu() ) );
  m_backAction->setEnabled( false );

  m_forwardAction = new KToolBarPopupAction( backForward.second, ALT+Key_Right,
      this, TQT_SLOT( forward() ), coll,
      "forward" );
  connect( m_forwardAction->popupMenu(), TQT_SIGNAL( activated( int ) ),
           TQT_SLOT( forwardActivated( int ) ) );
  connect( m_forwardAction->popupMenu(), TQT_SIGNAL( aboutToShow() ),
           TQT_SLOT( fillForwardMenu() ) );
  m_forwardAction->setEnabled( false );
}

void History::installMenuBarHook( KMainWindow *mainWindow )
{
  TQPopupMenu *goMenu = dynamic_cast<TQPopupMenu *>(
      mainWindow->guiFactory()->container( "go_web", mainWindow ) );
  if ( goMenu ) {
    connect( goMenu, TQT_SIGNAL( aboutToShow() ), TQT_SLOT( fillGoMenu() ) );
    connect( goMenu, TQT_SIGNAL( activated( int ) ),
             TQT_SLOT( goMenuActivated( int ) ) );
    m_goMenuIndex = goMenu->count();
  }
}

void History::createEntry()
{
  kdDebug() << "History::createEntry()" << endl;

  // First, remove any forward history
  Entry * current = m_entries.current();
  if (current)
  {
    m_entries.tqat( m_entries.count() - 1 ); // go to last one
    for ( ; m_entries.current() != current ; )
    {
      if ( !m_entries.removeLast() ) { // and remove from the end (faster and easier)
        Q_ASSERT(0);
        return;
      }
      else
        m_entries.tqat( m_entries.count() - 1 );
    }
    // Now current is the current again.

    // If current entry is empty reuse it.
    if ( !current->view ) return;
  }

  // Append a new entry
  m_entries.append( new Entry ); // made current
  Q_ASSERT( m_entries.tqat() == (int) m_entries.count() - 1 );
}

void History::updateCurrentEntry( View *view )
{
  if ( m_entries.isEmpty() )
    return;

  KURL url = view->url();
  
  Entry *current = m_entries.current();

  TQDataStream stream( current->buffer, IO_WriteOnly );
  view->browserExtension()->saveState( stream );

  current->view = view;

  if ( url.isEmpty() ) {
    kdDebug() << "History::updateCurrentEntry(): internal url" << endl;
    url = view->internalUrl();
  }

  kdDebug() << "History::updateCurrentEntry(): " << view->title()
            << " (URL: " << url.url() << ")" << endl;

  current->url = url;
  current->title = view->title();

  current->search = view->state() == View::Search;
}

void History::updateActions()
{
  m_backAction->setEnabled( canGoBack() );
  m_forwardAction->setEnabled( canGoForward() );
}

void History::back()
{
  kdDebug( 1400 ) << "History::back()" << endl;
  goHistoryActivated( -1 );
}

void History::backActivated( int id )
{
  kdDebug( 1400 ) << "History::backActivated(): id = " << id << endl;
  goHistoryActivated( -( m_backAction->popupMenu()->indexOf( id ) + 1 ) );
}

void History::forward()
{
  kdDebug( 1400 ) << "History::forward()" << endl;
  goHistoryActivated( 1 );
}

void History::forwardActivated( int id )
{
  kdDebug( 1400 ) << "History::forwardActivated(): id = " << id << endl;
  goHistoryActivated( m_forwardAction->popupMenu()->indexOf( id ) + 1 );
}

void History::goHistoryActivated( int steps )
{
  kdDebug( 1400 ) << "History::goHistoryActivated(): m_goBuffer = " << m_goBuffer << endl;
  if ( m_goBuffer )
    return;
  m_goBuffer = steps;
  TQTimer::singleShot( 0, this, TQT_SLOT( goHistoryDelayed() ) );
}

void History::goHistoryDelayed()
{
  kdDebug( 1400 ) << "History::goHistoryDelayed(): m_goBuffer = " << m_goBuffer << endl;
  if ( !m_goBuffer )
    return;
  int steps = m_goBuffer;
  m_goBuffer = 0;
  goHistory( steps );
}

void History::goHistory( int steps )
{
  kdDebug() << "History::goHistory(): " << steps << endl;

  // If current entry is empty remove it.
  Entry *current = m_entries.current();
  if ( current && !current->view ) m_entries.remove();

  int newPos = m_entries.tqat() + steps;

  current = m_entries.tqat( newPos );
  if ( !current ) {
    kdError() << "No History entry at position " << newPos << endl;
    return;
  }

  if ( !current->view ) {
    kdWarning() << "Empty history entry." << endl;
    return;
  }

  if ( current->search ) {
    kdDebug() << "History::goHistory(): search" << endl;
    current->view->lastSearch();
    return;
  }

  if ( current->url.protocol() == "khelpcenter" ) {
    kdDebug() << "History::goHistory(): internal" << endl;
    emit goInternalUrl( current->url );
    return;
  }

  kdDebug() << "History::goHistory(): restore state" << endl;

  emit goUrl( current->url );

  Entry h( *current );
  h.buffer.detach();

  TQDataStream stream( h.buffer, IO_ReadOnly );

  h.view->closeURL();
  updateCurrentEntry( h.view );
  h.view->browserExtension()->restoreState( stream );

  updateActions();
}

void History::fillBackMenu()
{
  TQPopupMenu *menu = m_backAction->popupMenu();
  menu->clear();
  fillHistoryPopup( menu, true, false, false );
}

void History::fillForwardMenu()
{
  TQPopupMenu *menu = m_forwardAction->popupMenu();
  menu->clear();
  fillHistoryPopup( menu, false, true, false );
}

void History::fillGoMenu()
{
  KMainWindow *mainWindow = static_cast<KMainWindow *>( kapp->mainWidget() );
  TQPopupMenu *goMenu = dynamic_cast<TQPopupMenu *>( mainWindow->guiFactory()->container( TQString::tqfromLatin1( "go" ), mainWindow ) );
  if ( !goMenu || m_goMenuIndex == -1 )
    return;

  for ( int i = goMenu->count() - 1 ; i >= m_goMenuIndex; i-- )
    goMenu->removeItemAt( i );

  // TODO perhaps smarter algorithm (rename existing items, create new ones only if not enough) ?

  // Ok, we want to show 10 items in all, among which the current url...

  if ( m_entries.count() <= 9 )
  {
    // First case: limited history in both directions -> show it all
    m_goMenuHistoryStartPos = m_entries.count() - 1; // Start right from the end
  } else
    // Second case: big history, in one or both directions
  {
    // Assume both directions first (in this case we place the current URL in the middle)
    m_goMenuHistoryStartPos = m_entries.tqat() + 4;

    // Forward not big enough ?
    if ( m_entries.tqat() > (int)m_entries.count() - 4 )
      m_goMenuHistoryStartPos = m_entries.count() - 1;
  }
  Q_ASSERT( m_goMenuHistoryStartPos >= 0 && (uint)m_goMenuHistoryStartPos < m_entries.count() );
  m_goMenuHistoryCurrentPos = m_entries.tqat(); // for slotActivated
  fillHistoryPopup( goMenu, false, false, true, m_goMenuHistoryStartPos );
}

void History::goMenuActivated( int id )
{
  KMainWindow *mainWindow = static_cast<KMainWindow *>( kapp->mainWidget() );
  TQPopupMenu *goMenu = dynamic_cast<TQPopupMenu *>( mainWindow->guiFactory()->container( TQString::tqfromLatin1( "go" ), mainWindow ) );
  if ( !goMenu )
    return;

  // 1 for first item in the list, etc.
  int index = goMenu->indexOf(id) - m_goMenuIndex + 1;
  if ( index > 0 )
  {
    kdDebug(1400) << "Item clicked has index " << index << endl;
    // -1 for one step back, 0 for don't move, +1 for one step forward, etc.
    int steps = ( m_goMenuHistoryStartPos+1 ) - index - m_goMenuHistoryCurrentPos; // make a drawing to understand this :-)
    kdDebug(1400) << "Emit activated with steps = " << steps << endl;
    goHistory( steps );
  }
}

void History::fillHistoryPopup( TQPopupMenu *popup, bool onlyBack, bool onlyForward, bool checkCurrentItem, uint startPos )
{
  Q_ASSERT ( popup ); // kill me if this 0... :/

  Entry * current = m_entries.current();
  TQPtrListIterator<Entry> it( m_entries );
  if (onlyBack || onlyForward)
  {
    it += m_entries.tqat(); // Jump to current item
    if ( !onlyForward ) --it; else ++it; // And move off it
  } else if ( startPos )
    it += startPos; // Jump to specified start pos

  uint i = 0;
  while ( it.current() )
  {
    TQString text = it.current()->title;
    text = KStringHandler::csqueeze(text, 50); //CT: squeeze
    text.replace( "&", "&&" );
    if ( checkCurrentItem && it.current() == current )
    {
      int id = popup->insertItem( text ); // no pixmap if checked
      popup->setItemChecked( id, true );
    } else
      popup->insertItem( text );
    if ( ++i > 10 )
      break;
    if ( !onlyForward ) --it; else ++it;
  }
}

bool History::canGoBack() const
{
  return m_entries.tqat() > 0;
}

bool History::canGoForward() const
{
  return m_entries.tqat() != static_cast<int>( m_entries.count() ) - 1;
}

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