webengine/webkitutils/HistoryProvider/HistoryController.cpp
changeset 0 dd21522fd290
child 13 10e98eab6f85
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 /*
       
     2 * Copyright (c) 2004 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Implementation of HistoryController
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <../bidi.h>
       
    21 #include "HistoryController.h"
       
    22 #include "BrCtlDefs.h"
       
    23 #include "HistoryView.h"
       
    24 #include "HistoryEntry.h"
       
    25 #include "BrCtlDialogsProvider.h"
       
    26 
       
    27 // EXTERNAL DATA STRUCTURES
       
    28 
       
    29 // EXTERNAL FUNCTION PROTOTYPES
       
    30 
       
    31 // CONSTANTS
       
    32 
       
    33 // MACROS
       
    34 
       
    35 // LOCAL CONSTANTS AND MACROS
       
    36 
       
    37 // MODULE DATA STRUCTURES
       
    38 
       
    39 // LOCAL FUNCTION PROTOTYPES
       
    40 
       
    41 // FORWARD DECLARATIONS
       
    42 
       
    43 // ============================= LOCAL FUNCTIONS ===============================
       
    44 
       
    45 // ============================= CLASSES METHODS===============================
       
    46 
       
    47 // ============================ MEMBER FUNCTIONS ===============================
       
    48 
       
    49 EXPORT_C HistoryControllerInterface* HistoryController::initWithCallback( HistoryCallback* historyCallback,
       
    50                                                       bool historyAllowed, bool backListAllowed )
       
    51 {
       
    52     HistoryController* self = new HistoryController(historyCallback, historyAllowed, backListAllowed);
       
    53     return self;
       
    54 }
       
    55 
       
    56 
       
    57 HistoryController::~HistoryController()
       
    58 {
       
    59     clearHistoryList();
       
    60 }
       
    61 
       
    62 /**
       
    63 */
       
    64 void HistoryController::insert( const TPtrC8& url, const TPtrC8& requestUrl, 
       
    65                                TPtrC& formContentType, WebCore::FormData* formData)
       
    66 {
       
    67     updateCurrentEntryPositionIfNeeded();
       
    68     if (m_historyLoadOffset) {
       
    69         m_historyLoadOffset = 0;
       
    70         m_tempCurrentIndex = m_currentIndex;
       
    71         return;
       
    72     }
       
    73     HistoryEntry* entry = HistoryEntry::initWithUrlAndFormData(url, requestUrl, formContentType, 
       
    74         formData, m_historyCallback, m_historyCallback->wmlMode());
       
    75     if (entry) {
       
    76         int i;
       
    77         // Remove all entries after this entry
       
    78         for (i = historyLength() - 1; i > m_currentIndex; i--) {
       
    79             HistoryEntry* deadEntry = m_historyStack[i];
       
    80             m_historyStack.Remove(i);
       
    81             delete deadEntry;
       
    82         }
       
    83         // Make sure we don't exceed the limit
       
    84         if (historyLength() >= KHistoryStackSize) {
       
    85             HistoryEntry* deadEntry = m_historyStack[0];
       
    86             m_historyStack.Remove(0);
       
    87             delete deadEntry;
       
    88             m_currentIndex--;
       
    89         }
       
    90         int err = m_historyStack.Append(entry);
       
    91 	    m_historyStack.Compress();
       
    92         if (err != KErrNone) { 
       
    93             delete entry;
       
    94         }
       
    95         else {
       
    96             m_currentIndex++;
       
    97         }
       
    98         m_tempCurrentIndex = m_currentIndex;
       
    99     }    
       
   100 }
       
   101 
       
   102 /**
       
   103 */
       
   104 bool HistoryController::containsItemForURL (const TPtrC& url)
       
   105 {
       
   106     HBufC8* tempHBuf8 = HBufC8::New(url.Length());
       
   107     if (! tempHBuf8)  return EFalse;
       
   108 	tempHBuf8->Des().Copy( url );
       
   109 
       
   110     for (int i = historyLength() - 1; i>=0; i--){
       
   111         HistoryEntry* deadEntry = m_historyStack[i];
       
   112         if(deadEntry->requestUrl().Compare(tempHBuf8->Des()) == 0 ) {
       
   113             if(tempHBuf8) delete tempHBuf8;    
       
   114             return ETrue;
       
   115         }
       
   116     }
       
   117     if(tempHBuf8) delete tempHBuf8;
       
   118     return EFalse;
       
   119 }
       
   120 
       
   121 /**
       
   122 */
       
   123 HBufC* HistoryController::pageInfoLC( TBrCtlDefs::TBrCtlPageInfo brCtlPageInfo )
       
   124 {
       
   125     HBufC* pageInfo = NULL;
       
   126     
       
   127     switch( brCtlPageInfo )
       
   128     {
       
   129     case TBrCtlDefs::EPageInfoTitle:
       
   130         {
       
   131             TPtrC pageTitle = m_historyView->getCenterEntryTitle();
       
   132             if( pageTitle.Length() != 0 ) {
       
   133                 pageInfo = pageTitle.AllocL();
       
   134             }
       
   135             break;
       
   136         }
       
   137     case TBrCtlDefs::EPageInfoUrl:
       
   138         {
       
   139             const HistoryEntry* currentEntry = entryByIndex(m_currentIndex);
       
   140             if( currentEntry && currentEntry->responseUrl().Length() ) {
       
   141                 pageInfo = HBufC::NewL(currentEntry->responseUrl().Length());
       
   142                 pageInfo->Des().Copy(currentEntry->responseUrl());
       
   143             }
       
   144             break;
       
   145         }
       
   146     default:
       
   147         {
       
   148             break;
       
   149         }
       
   150     }
       
   151     CleanupStack::PushL( pageInfo );
       
   152     return pageInfo;
       
   153 }
       
   154 
       
   155 /**
       
   156 */
       
   157 void HistoryController::handleHistoryCommandL(int command)
       
   158 {
       
   159     m_tempCurrentIndex = m_currentIndex;
       
   160     switch( command )
       
   161     {
       
   162     case TBrCtlDefs::ECommandReload:
       
   163         {
       
   164         // Get current url from history and call LoadUrlL(url)
       
   165         loadHistoryUrl( EHistoryStackDirectionCurrent, TBrCtlDefs::ECacheModeNoCache, m_historyLoadOffset);
       
   166         break;
       
   167         }
       
   168     case TBrCtlDefs::ECommandBack:
       
   169         {
       
   170         if (m_historyViewEnabled && m_backListAllowed) {
       
   171             showHistoryViewL(true);
       
   172         }
       
   173         else {
       
   174             loadHistoryUrl( EHistoryStackDirectionPrevious, TBrCtlDefs::ECacheModeHistory, -1);
       
   175         }
       
   176         break;
       
   177         }
       
   178     case TBrCtlDefs::ECommandOneStepBack:
       
   179         {
       
   180         loadHistoryUrl( EHistoryStackDirectionPrevious, TBrCtlDefs::ECacheModeHistory, -1);
       
   181         break;
       
   182         }
       
   183     case TBrCtlDefs::ECommandForward:
       
   184         {
       
   185         // Get next url from history and call LoadUrlL(url)
       
   186         loadHistoryUrl( EHistoryStackDirectionNext, TBrCtlDefs::ECacheModeHistory, 1);
       
   187         break;
       
   188         }
       
   189     case TBrCtlDefs::ECommandShowHistory:
       
   190         {
       
   191         if (m_historyViewEnabled && m_backListAllowed) {
       
   192             showHistoryViewL(false);
       
   193         }
       
   194         else {
       
   195             showHistoryListL();
       
   196         }
       
   197         break;
       
   198         }
       
   199     case TBrCtlDefs::ECommandClearHistory:
       
   200         {
       
   201         // Clear History contents
       
   202         clearHistoryList();
       
   203         break;
       
   204         }
       
   205     case TBrCtlDefs::ECommandCancel:
       
   206         {
       
   207         if (m_historyViewEnabled && m_backListAllowed) {
       
   208             closeHistoryView();
       
   209             m_historyLoadOffset = 0;
       
   210         }
       
   211         break;
       
   212         }
       
   213     default:
       
   214         {
       
   215         break;
       
   216         }
       
   217     }
       
   218 }
       
   219 
       
   220 /**
       
   221 */
       
   222 void HistoryController::clearHistoryList()
       
   223 {
       
   224     int i;
       
   225     for (i = historyLength() - 1; i >= 0; i--) {
       
   226         if (i != m_currentIndex) {
       
   227             HistoryEntry* deadEntry = m_historyStack[i];
       
   228             m_historyStack.Remove(i);
       
   229             delete deadEntry;
       
   230         }
       
   231     }
       
   232     m_currentIndex  = (m_currentIndex != -1) ? 0: m_currentIndex ;
       
   233     m_tempCurrentIndex = m_currentIndex;
       
   234     m_historyLoadOffset = 0;
       
   235 }
       
   236 
       
   237 /**
       
   238 */
       
   239 bool HistoryController::canGoBackOrForward(int distance)
       
   240 {
       
   241     int newCurrent = m_currentIndex + distance;
       
   242     if ( newCurrent >= 0 && newCurrent < m_historyStack.Count()) {
       
   243         return true;
       
   244     }
       
   245     return false;
       
   246 }
       
   247 
       
   248 /**
       
   249 */
       
   250 void HistoryController::updateHistoryEntryThumbnailL(const CFbsBitmap* bitmap)
       
   251 {
       
   252     if(m_historyViewEnabled) {
       
   253         HistoryEntry* entry = entryByIndex(m_currentIndex);
       
   254         if (entry) {
       
   255             TSize bmsize = bitmap->SizeInPixels();
       
   256             TRect parentControlRect = m_historyCallback->parent()->Rect();
       
   257             int historyViewWidth = parentControlRect.Width();
       
   258             int historyViewHeight( parentControlRect.Height());
       
   259             // Find out that in either portrait or landscape view what is the max height
       
   260             int maxDimension = (historyViewWidth > historyViewHeight)? historyViewWidth:historyViewHeight;
       
   261             int thumbnailHeight = Min(bmsize.iHeight, maxDimension*KCenterThumbnailHeightPercent/100);
       
   262             int thumbnailWidth = Min(bmsize.iWidth, maxDimension*KCenterThumbnailWidthPercent/100);
       
   263             entry->storeThumbnail(bitmap, TRect(0,0,thumbnailWidth, thumbnailHeight));
       
   264         }
       
   265     }
       
   266 }
       
   267 
       
   268 /**
       
   269 */
       
   270 void HistoryController::setCurrentEntryTitle( const TPtrC& pageTitle )
       
   271 {
       
   272     HistoryEntry* entry = entryByIndex(m_currentIndex);
       
   273     if (entry) {
       
   274         entry->setPageTitle(pageTitle);
       
   275     }
       
   276 }
       
   277 
       
   278 /**
       
   279 */
       
   280 void HistoryController::updateCurrentEntryPositionIfNeeded()
       
   281 {
       
   282     HistoryEntry* entry = entryByIndex(m_tempCurrentIndex);
       
   283     if (entry) {
       
   284         TPoint cp(m_historyCallback->currentPosition());
       
   285         if (!m_historyCallback->wmlMode()) {
       
   286             // update the entry position
       
   287             entry->setPosition(cp) ;
       
   288         }
       
   289     }
       
   290 }
       
   291 
       
   292 
       
   293 
       
   294 /**
       
   295 */
       
   296 TPoint HistoryController::currentEntryPosition()
       
   297 {
       
   298     HistoryEntry* entry = entryByIndex(m_currentIndex);
       
   299     if (entry) {
       
   300         return entry->position();
       
   301     }
       
   302     return TPoint(0, 0);
       
   303 }
       
   304 
       
   305 /**
       
   306  */
       
   307 //void HistoryController::setRequestUrlL(const TPtrC& aUrl);
       
   308 
       
   309 /**
       
   310  * Sets Url Response 
       
   311  */
       
   312 //void HistoryController::setResponseUrlL(const TPtrC& aUrl);
       
   313 
       
   314 /**
       
   315  * Gets the entry 
       
   316  */
       
   317 HistoryEntry* HistoryController::entry(THistoryStackDirection direction)
       
   318 {
       
   319     int index = 0;
       
   320     switch (direction)
       
   321         {
       
   322         case EHistoryStackDirectionPrevious:
       
   323             index = m_currentIndex - 1;
       
   324             break;
       
   325         case EHistoryStackDirectionNext:
       
   326             index = m_currentIndex + 1;
       
   327             break;
       
   328         case EHistoryStackDirectionCurrent:
       
   329         default:
       
   330             index = m_currentIndex;
       
   331             break;
       
   332         }
       
   333     return entryByIndex(index);
       
   334 }
       
   335 
       
   336 /**
       
   337  */
       
   338 TPtrC8 HistoryController::requestUrl()
       
   339 {
       
   340 	    HistoryEntry* entry = entryByIndex(m_currentIndex);
       
   341         if (! entry) {
       
   342             return NULL;
       
   343         }
       
   344         return entry->requestUrl();
       
   345 }
       
   346 
       
   347 /**
       
   348 */
       
   349 TPtrC8 HistoryController::responseUrl ()
       
   350 {
       
   351 	    HistoryEntry* entry = entryByIndex(m_currentIndex);
       
   352         if (! entry) {
       
   353             return NULL;
       
   354         }
       
   355         return entry->responseUrl();
       
   356 }
       
   357 
       
   358 /**
       
   359 */
       
   360 HistoryEntry* HistoryController::entryByIndex (int historyIndex)
       
   361 {
       
   362     if (historyIndex >= 0 && historyIndex < m_historyStack.Count()) {
       
   363         return m_historyStack[historyIndex];
       
   364     }
       
   365     return NULL;    
       
   366 }
       
   367 
       
   368 /**
       
   369  */
       
   370 int HistoryController::index (THistoryStackDirection direction)
       
   371 {
       
   372 	//return m_historyStack.index (direction);
       
   373     TInt index = 0;
       
   374     switch (direction)
       
   375         {
       
   376         case EHistoryStackDirectionPrevious:
       
   377         index = currentIndex() - 1;
       
   378         break;
       
   379 
       
   380         case EHistoryStackDirectionNext:
       
   381         index = currentIndex() + 1;
       
   382         break;
       
   383 
       
   384         case EHistoryStackDirectionCurrent:
       
   385         default:
       
   386         index = currentIndex();
       
   387         break;
       
   388         }
       
   389 
       
   390     return index;
       
   391 }
       
   392 
       
   393 /**
       
   394 */
       
   395 void HistoryController::deleteEntry(int index)
       
   396 {
       
   397     if (index >= 0 && index < m_historyStack.Count()) {
       
   398         HistoryEntry* deadEntry = m_historyStack[index];
       
   399         m_historyStack.Remove(index);
       
   400         delete deadEntry;
       
   401 		// Shift current page if removing previous pages
       
   402         if (index <= m_currentIndex && m_currentIndex > 0)
       
   403         	{
       
   404         	m_currentIndex--;
       
   405         	}
       
   406     }
       
   407 }
       
   408 
       
   409 /**
       
   410 */
       
   411 void HistoryController::setCurrentL ( THistoryStackDirection direction )
       
   412 {
       
   413 	setCurrentIndex(index(direction));
       
   414 }
       
   415 
       
   416 /**
       
   417 */
       
   418 void HistoryController::updateGlobalHistoryForReload()
       
   419 {
       
   420     HistoryEntry* entry = m_historyStack[m_currentIndex];
       
   421     entry->touch();
       
   422     updateCurrentEntryPositionIfNeeded();
       
   423 }
       
   424 
       
   425 /**
       
   426 */
       
   427 void HistoryController::goBackOrForward(int distance)
       
   428 {
       
   429     m_tempCurrentIndex = m_currentIndex;
       
   430     m_currentIndex += distance;
       
   431     m_historyLoadOffset = distance;
       
   432     const HistoryEntry* currentEntry = entryByIndex(m_currentIndex);
       
   433     if (currentEntry) {
       
   434         TPtrC8 url = currentEntry->requestUrl();
       
   435         m_historyCallback->doHistoryGet( url, TBrCtlDefs::ECacheModeHistory );
       
   436     }
       
   437 }
       
   438 
       
   439 /**
       
   440 */
       
   441 void HistoryController::closeHistoryView()
       
   442 {
       
   443     m_historyLoadOffset = m_historyView->historyLoadOffset();
       
   444     delete m_historyView;
       
   445     m_historyView = NULL;
       
   446     // Update the display
       
   447     m_historyCallback->makeVisible(true);
       
   448     m_historyCallback->parent()->DrawNow();
       
   449     // inform UI that we have exited the History View
       
   450     // This should update the softkeys
       
   451     m_historyCallback->stateChanged(false);
       
   452     // delete bitmaps (they can be recreated from buffer)
       
   453     int i;
       
   454     for (i = 0; i < historyLength(); i++ ) {
       
   455         HistoryEntry* entry = m_historyStack[i];
       
   456         entry->deleteThumbnail();
       
   457     }
       
   458     //reset deferred timers on closing history view
       
   459     m_historyCallback->deferTimers(false);
       
   460 }
       
   461 
       
   462 /**
       
   463  */
       
   464 void HistoryController::updateDisplay() const
       
   465 {
       
   466     m_historyCallback->parent()->DrawNow();
       
   467 }
       
   468 
       
   469 /**
       
   470  */
       
   471 HistoryController::HistoryController(HistoryCallback* historyCallback, bool historyAllowed, bool backListAllowed) : m_historyStack(20)
       
   472 {
       
   473     m_historyView = NULL;
       
   474     m_historyViewEnabled = historyAllowed;
       
   475     m_backListAllowed = backListAllowed; 
       
   476     m_historyLoadOffset = 0;
       
   477     m_historyCallback = historyCallback;
       
   478     m_currentIndex = -1;
       
   479     m_tempCurrentIndex = m_currentIndex;
       
   480     m_possibleWmlOEB = false;
       
   481 }
       
   482 
       
   483 /**
       
   484 */
       
   485 void HistoryController::showHistoryViewL(bool previous)
       
   486 {
       
   487     HistoryEntry* entry = entryByIndex(m_currentIndex);
       
   488     if (entry && ! entry->thumbnail()) {
       
   489         // get scaled page from PageScaler;
       
   490         // update the history with new bitmap
       
   491         CFbsBitmap* scaledPage = m_historyCallback->scaledPage();
       
   492         if(scaledPage) {
       
   493             // Get the browser control rect
       
   494             updateHistoryEntryThumbnailL(scaledPage);
       
   495         }
       
   496             // ignore err since we will use the default image
       
   497     }
       
   498     //Defer refresh timers on showing history view
       
   499     m_historyCallback->deferTimers(true);
       
   500     m_historyView = HistoryView::NewL( *this, previous );
       
   501     m_historyCallback->makeVisible(false);
       
   502     m_historyCallback->parent()->DrawNow();
       
   503     // inform UI that we have enterd the History View
       
   504     // This should update the softkeys
       
   505     m_historyCallback->stateChanged(true);
       
   506     m_historyView->updateState(0);
       
   507 }
       
   508 
       
   509 /**
       
   510 */
       
   511 void HistoryController::showHistoryListL()
       
   512 {
       
   513     SelectArray* historyList = new( ELeave ) CArrayFixFlat<TBrCtlSelectOptionData>(10);
       
   514     CleanupStack::PushL( historyList );
       
   515     for( int i = m_historyStack.Count() - 1; i >= 0; i-- ) {
       
   516         TBrCtlSelectOptionData t( TBrCtlSelectOptionData(entryByIndex(i)->pageTitle(), i == m_currentIndex, false, false) );
       
   517         historyList->AppendL(t);
       
   518     }
       
   519     // Display history dialog
       
   520     bool ret = m_historyCallback->dialogSelectOption(historyList);
       
   521     if (ret) {
       
   522         int index = 0;
       
   523         for (; index < historyList->Count(); index++) {
       
   524             if ((*historyList)[index].IsSelected())
       
   525                 break;
       
   526         }
       
   527         // Initialize a history load
       
   528         m_tempCurrentIndex = m_currentIndex;
       
   529         m_currentIndex = m_historyStack.Count() - index - 1;
       
   530         m_historyLoadOffset = index;
       
   531         loadHistoryUrl(EHistoryStackDirectionCurrent, TBrCtlDefs::ECacheModeNoCache, m_historyLoadOffset);
       
   532     }
       
   533     // else, user cancelled the dialog; do nothing about it
       
   534     historyList->Reset();
       
   535     CleanupStack::PopAndDestroy(); // historyList
       
   536 }
       
   537 
       
   538 /**
       
   539 * LoadHistoryEntry
       
   540 *
       
   541 * @since 3.x
       
   542 * @return void
       
   543 */
       
   544 //void HistoryController::loadHistoryEntryL( CArrayFixFlat<TBrCtlSelectOptionData>& aHistoryList );
       
   545 
       
   546 void HistoryController::loadHistoryUrl(THistoryStackDirection direction, TBrCtlDefs::TBrCtlCacheMode cacheMode, int historyLoadOffset)
       
   547 {
       
   548     const HistoryEntry* currentEntry = entry(direction);
       
   549     if(currentEntry) {
       
   550         m_historyLoadOffset = historyLoadOffset;
       
   551         if (direction != EHistoryStackDirectionCurrent) {
       
   552             m_currentIndex += historyLoadOffset;
       
   553         }
       
   554         TPtrC8 url = currentEntry->requestUrl();
       
   555         if(currentEntry->postContentType().Length() && currentEntry->formData()) {
       
   556             m_historyCallback->doHistoryPost(url, cacheMode, currentEntry->postContentType(), currentEntry->formData());
       
   557         }
       
   558         else {
       
   559             m_historyCallback->doHistoryGet( url, cacheMode );
       
   560         }
       
   561     }
       
   562 }
       
   563 
       
   564 void HistoryController::performTransition(int direction)
       
   565 {
       
   566 	m_historyView->performTransition(direction);	
       
   567 }
       
   568 
       
   569 /**
       
   570 */
       
   571 void HistoryController::updateCurrentEntryZoomLevelIfNeeded()
       
   572 {
       
   573     HistoryEntry* entry = entryByIndex(m_tempCurrentIndex); 
       
   574     if (entry) {
       
   575         int cp(m_historyCallback->currentZoomLevel());
       
   576         if (!m_historyCallback->wmlMode()) {
       
   577             // update the entry position
       
   578             entry->setZoomLevel(cp) ;
       
   579         }
       
   580     }
       
   581 }
       
   582 
       
   583 /**
       
   584 */
       
   585 int HistoryController::currentEntryZoomLevel()
       
   586 {
       
   587     HistoryEntry* entry = entryByIndex(m_currentIndex);
       
   588 
       
   589     if (entry) {
       
   590         return entry->zoomLevel();
       
   591     }
       
   592     return 0; // default value should use in this case
       
   593 }
       
   594 
       
   595 /**
       
   596 */
       
   597 void HistoryController::updateCurrentEntryMinZoomLevelIfNeeded()
       
   598 {
       
   599     HistoryEntry* entry = entryByIndex(m_tempCurrentIndex); 
       
   600 
       
   601     if (entry) {
       
   602         int cp(m_historyCallback->minZoomLevel());
       
   603         if (!m_historyCallback->wmlMode()) {
       
   604             // update the entry position
       
   605             entry->setMinZoomLevel(cp) ;
       
   606         }
       
   607     }
       
   608 }
       
   609 
       
   610 /**
       
   611 */
       
   612 int HistoryController::currentEntryMinZoomLevel()
       
   613 {
       
   614     HistoryEntry* entry = entryByIndex(m_currentIndex);
       
   615 
       
   616     if (entry) {
       
   617         return entry->minZoomLevel();
       
   618     }
       
   619     return 0; // default value should use in this case
       
   620 }
       
   621 
       
   622 //  End of File