mpviewplugins/mpdetailsviewplugin/src/mpdetailssharedialog.cpp
changeset 29 8192e5b5c935
child 32 c163ef0b758d
equal deleted inserted replaced
25:3ec52facab4d 29:8192e5b5c935
       
     1 /*
       
     2 * Copyright (c) 2009 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 "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: 
       
    15 *
       
    16 */
       
    17 #ifdef SHARE_FUNC_ENABLED
       
    18 
       
    19 #include "mpdetailssharedialog.h"
       
    20 #include "mpsharedata.h"
       
    21 #include "mpsongdata.h"
       
    22 #include "mptrace.h"
       
    23 #include <QObject>
       
    24 #include <QGraphicsWebView>
       
    25 #include <QGraphicsScene>
       
    26 #include <QWebPage>
       
    27 #include <QWebFrame>
       
    28 #include <QNetworkAccessManager>
       
    29 #include <QNetworkDiskCache>
       
    30 #include <QDesktopServices>
       
    31 #include <QNetworkProxyFactory>
       
    32 #include <hbmessagebox.h>
       
    33 #include <QFile>
       
    34 #include <QTextStream>
       
    35 
       
    36 
       
    37 // SHARE_INDEX_FILE defines where the index.html file is loaded from.
       
    38 #define SHARE_INDEX_URL "qrc://shareview/index.html"
       
    39 
       
    40 #ifdef Q_OS_SYMBIAN
       
    41 // Symbian target.
       
    42 #ifdef SHARE_PLAYER_RND
       
    43 // For R&D testing, index.html may be loaded from E: drive.
       
    44 // User must manually place index.html in the correct location.
       
    45 // If the RND file does not exist, then SHARE_INDEX_URL will be used.
       
    46 #define RND_SHARE_INDEX_URL "file://e:/index.html"
       
    47 #define RND_SHARE_INDEX_FILE "e:\\index.html"
       
    48 #define RND_OVI_LOGIN_FILE "e:\\ovicredentials.txt"
       
    49 #define RND_DEFAULT_USER "hipsu"
       
    50 #define RND_DEFAULT_PASS "hipsupass"
       
    51 #endif
       
    52 #else
       
    53 // Assume Windows target.
       
    54 #define RND_SHARE_INDEX_URL "file://c:/temp/index.html"
       
    55 #define RND_SHARE_INDEX_FILE "c:\\temp\\index.html"
       
    56 #define RND_OVI_LOGIN_FILE "c:\\temp\\ovicredentials.txt"
       
    57 
       
    58 #endif
       
    59 
       
    60 // For allowing local caching of javascript files loaded by popup
       
    61 class MpNetworkAccessManager : public QNetworkAccessManager
       
    62 {
       
    63 public:
       
    64     MpNetworkAccessManager() { };
       
    65 
       
    66 private:
       
    67     QNetworkReply *createRequest( Operation op,
       
    68                                  const QNetworkRequest &request,
       
    69                                  QIODevice *outgoingData = 0 )
       
    70     {
       
    71         TX_ENTRY
       
    72         QVariant val = request.attribute( QNetworkRequest::CacheLoadControlAttribute );
       
    73         qDebug() << "request cachecontrol - " << val.typeName() << val.toInt();
       
    74 
       
    75          // Change the cache load control attrbute!
       
    76         QNetworkRequest req = request;
       
    77         req.setAttribute( QNetworkRequest::CacheLoadControlAttribute,
       
    78                            QVariant( QNetworkRequest::PreferCache ) ); // was PreferNetwork
       
    79         TX_EXIT
       
    80         return QNetworkAccessManager::createRequest( op, req, outgoingData );
       
    81     }
       
    82 };
       
    83 
       
    84 
       
    85 /*!
       
    86  Constructor.
       
    87  */
       
    88 MpDetailsShareDialog::MpDetailsShareDialog()
       
    89     : mShareWebView( 0 ),
       
    90       mShareNetAccMan( 0 )
       
    91 {
       
    92 }
       
    93 
       
    94 void MpDetailsShareDialog::initialize( MpSongData* aSongData )
       
    95 {
       
    96     TX_ENTRY
       
    97     if ( !initUser() )
       
    98     {
       
    99         emit closeShareDialog();
       
   100         return;
       
   101     }
       
   102     initShareData( aSongData );
       
   103     initNetworkAccessManager();
       
   104     initWebView();
       
   105     initSignalSlots();
       
   106 
       
   107     setDismissPolicy( HbDialog::NoDismiss );
       
   108 
       
   109     // No timeout needed for the dialog.
       
   110     setTimeout( HbPopup::NoTimeout );
       
   111 
       
   112 #ifdef SHARE_PLAYER_RND
       
   113     // Test whether the RND file exists.
       
   114     QFile file( RND_SHARE_INDEX_FILE );
       
   115     if ( file.exists() )
       
   116     {
       
   117         // Load the RND URL from the specified location to the web view.
       
   118         TX_LOG_ARGS( "share: Use RND index.html file " << RND_SHARE_INDEX_FILE )
       
   119         mShareWebView->load( QUrl( RND_SHARE_INDEX_URL ) );
       
   120     }
       
   121     else
       
   122 #endif
       
   123     {
       
   124         // Load the production URL from the application resources to the web view.
       
   125         TX_LOG_ARGS( "share: Use QRC index.html file " << SHARE_INDEX_URL )
       
   126         mShareWebView->load( QUrl( SHARE_INDEX_URL ) );
       
   127     }
       
   128     TX_EXIT
       
   129 }
       
   130 
       
   131 
       
   132 /*!
       
   133  Destructor.
       
   134  */
       
   135 MpDetailsShareDialog::~MpDetailsShareDialog()
       
   136 {
       
   137     TX_ENTRY
       
   138     logoutPlayer();
       
   139     delete mShareNetAccMan;
       
   140     TX_EXIT
       
   141 }
       
   142 
       
   143 void MpDetailsShareDialog::initShareData( MpSongData* aSongData )
       
   144 {
       
   145     TX_ENTRY
       
   146     // Set information for the share data.
       
   147     mShareData.setOwner( this );
       
   148     mShareData.setSongData( aSongData );
       
   149     // TODO need localized error message.
       
   150     mShareData.setErrorMessage( "An error occured. Sharing is not currently available" );
       
   151     // TODO need language setting.
       
   152     mShareData.setLanguage( "en" );
       
   153     TX_EXIT
       
   154 }
       
   155 
       
   156 void MpDetailsShareDialog::initNetworkAccessManager()
       
   157 {
       
   158     TX_ENTRY
       
   159     // Make our own network access manager to allow JS retrieval from local cache,
       
   160     // since configuration for the default network access manager seems to be
       
   161     // to always redownload from network.
       
   162     mShareNetAccMan = new MpNetworkAccessManager();
       
   163     mShareNetAccMan->proxyFactory()->setUseSystemConfiguration( true );
       
   164     QNetworkDiskCache* diskCache = new QNetworkDiskCache( this );
       
   165     QString location = QDesktopServices::storageLocation( QDesktopServices::CacheLocation );
       
   166     diskCache->setCacheDirectory( location );
       
   167     mShareNetAccMan->setCache( diskCache );
       
   168     TX_EXIT
       
   169 }
       
   170 
       
   171 void MpDetailsShareDialog::initWebView()
       
   172 {
       
   173     TX_ENTRY
       
   174     mShareWebView = new QGraphicsWebView();
       
   175     mShareWebView->settings()->setAttribute( QWebSettings::LocalContentCanAccessRemoteUrls, true );
       
   176     mShareWebView->settings()->setAttribute( QWebSettings::LocalStorageDatabaseEnabled, true );
       
   177     mShareWebView->settings()->enablePersistentStorage();
       
   178     mShareWebView->page()->setNetworkAccessManager( mShareNetAccMan );
       
   179     setContentWidget( mShareWebView );
       
   180     TX_EXIT
       
   181 }
       
   182 
       
   183 void MpDetailsShareDialog::initSignalSlots()
       
   184 {
       
   185     TX_ENTRY
       
   186     // Connect various signals to slots for interface to webview.
       
   187     connect( mShareWebView, SIGNAL( loadFinished( bool ) ), SLOT( onIndexLoad( bool ) ) );
       
   188     connect( mShareWebView->page()->mainFrame(), SIGNAL( javaScriptWindowObjectCleared() ), this, SLOT( addContext() ) );
       
   189     connect( mShareWebView->page(), SIGNAL( windowCloseRequested() ), this, SIGNAL( closeShareDialog() ) );
       
   190     TX_EXIT
       
   191 }
       
   192 
       
   193 /*!
       
   194  initUser is used as temporary solution until Single Sign On is implemented in platform.
       
   195  */
       
   196 bool MpDetailsShareDialog::initUser()
       
   197 {
       
   198     TX_ENTRY
       
   199     bool result = false;
       
   200 #ifdef SHARE_PLAYER_RND
       
   201     // ovicredentials.txt is used as temporary solution until Single Sign On is implemented in platform.
       
   202     QFile file( RND_OVI_LOGIN_FILE );
       
   203     if ( !file.open( QFile::ReadOnly ) )
       
   204     {
       
   205 //        errorHandler( "share", QString( RND_OVI_LOGIN_FILE ) + " missing" );
       
   206         mShareData.setUsername( RND_DEFAULT_USER );
       
   207         mShareData.setPassword( RND_DEFAULT_PASS );
       
   208         result = true;
       
   209     }
       
   210     else
       
   211     {
       
   212         QTextStream stream ( &file );
       
   213         QString strCred = stream.readLine( 0 );
       
   214         file.close();
       
   215         QStringList slCred = strCred.split( ":" );
       
   216         if ( slCred.length() > 1 )
       
   217         {
       
   218             mShareData.setUsername( slCred[ 0 ] );
       
   219             mShareData.setPassword( slCred[ 1 ] );
       
   220             result = true;
       
   221         }
       
   222         else
       
   223         {
       
   224             errorHandler( "share", QString( RND_OVI_LOGIN_FILE ) + " username:password expected" );
       
   225         }
       
   226     }
       
   227     TX_LOG_ARGS( "share: credentials " << mShareData.username() << " / " << mShareData.password() )
       
   228 #else
       
   229     // TODO: Single Sign On stuff.
       
   230 #endif // SHARE_PLAYER_RND
       
   231     TX_EXIT
       
   232     return result;
       
   233 }
       
   234 
       
   235 /*!
       
   236  Release resources from share player.
       
   237  */
       
   238 void MpDetailsShareDialog::logoutPlayer()
       
   239 {
       
   240     TX_ENTRY
       
   241     if (mShareWebView)
       
   242     {
       
   243         mShareWebView->page()->mainFrame()->evaluateJavaScript( "music.teardown();" );
       
   244     }
       
   245     TX_EXIT
       
   246 }
       
   247 
       
   248 /*!
       
   249  Adds the shared data context to the javascript of the loaded page.
       
   250  */
       
   251 void MpDetailsShareDialog::addContext()
       
   252 {
       
   253     TX_ENTRY
       
   254     if (mShareWebView)
       
   255     {
       
   256         mShareWebView->page()->mainFrame()->addToJavaScriptWindowObject( "context", &mShareData );
       
   257     }
       
   258     TX_EXIT
       
   259 }
       
   260 
       
   261 /*!
       
   262  Updates the shared data context in the javascript of the loaded page.
       
   263  */
       
   264 void MpDetailsShareDialog::updateSharedData()
       
   265 {
       
   266     TX_ENTRY
       
   267     if (mShareWebView)
       
   268     {
       
   269         mShareWebView->page()->mainFrame()->evaluateJavaScript( "music.updateContextArea();" );
       
   270         mShareWebView->page()->mainFrame()->evaluateJavaScript( "music.updateMetadata();" );
       
   271     }
       
   272     TX_EXIT
       
   273 }
       
   274 
       
   275 /*!
       
   276  Slot to call when index.html loading completes.
       
   277  */
       
   278 void MpDetailsShareDialog::onIndexLoad( bool aOk )
       
   279 {
       
   280     TX_ENTRY
       
   281     if ( !aOk )
       
   282     {
       
   283         // Close the popup window, failed to load index.html.
       
   284         // This is pretty serious and most likely unrecoverable error.
       
   285         // Only thing we can do really is to close the share player
       
   286         // dialog - TODO do we need to show any error message to user?
       
   287         TX_LOG_ARGS( "share: failed to load index.html" )
       
   288         emit closeShareDialog();
       
   289     }
       
   290     TX_EXIT
       
   291 }
       
   292 
       
   293 /*!
       
   294  Slot to call for debug output.
       
   295  */
       
   296 void MpDetailsShareDialog::debugJs( QString s )
       
   297 {
       
   298     TX_ENTRY
       
   299     TX_LOG_ARGS( "share: debugJs: " << s )
       
   300     TX_EXIT
       
   301 }
       
   302 
       
   303 /*!
       
   304  Slot to call for displaying an error message to the user.
       
   305  */
       
   306 void MpDetailsShareDialog::errorHandler( QString error, QString message )
       
   307 {
       
   308     TX_ENTRY
       
   309     TX_LOG_ARGS( "share: errorHandler: " << error << ": " << message )
       
   310     HbMessageBox::warning( error + ": " + message ); // For week16 hbwidgets
       
   311 //    HbMessageBox::launchWarningMessageBox( error + ": " + message ); // For week12 hbwidgets
       
   312     TX_EXIT
       
   313 }
       
   314 
       
   315 /*!
       
   316  Slot to call to clear the web view cache.
       
   317  */
       
   318 void MpDetailsShareDialog::clearCache()
       
   319 {
       
   320     TX_ENTRY
       
   321     QAbstractNetworkCache* cache = mShareNetAccMan ? mShareNetAccMan->cache() : NULL;
       
   322     if ( cache )
       
   323     {
       
   324         TX_LOG_ARGS( "share: clearCache: clearing cache" )
       
   325         cache->clear();
       
   326 #ifdef SHARE_PLAYER_RND
       
   327         errorHandler( "Cache", "Cleared cache!" );
       
   328 #endif
       
   329     }
       
   330     else
       
   331     {
       
   332         TX_LOG_ARGS( "share: clearCache: unable to clear cache" )
       
   333 #ifdef SHARE_PLAYER_RND
       
   334         errorHandler( "Cache", "Could not clear cache!" );
       
   335 #endif
       
   336     }
       
   337     TX_EXIT
       
   338 }
       
   339 
       
   340 #endif // SHARE_FUNC_ENABLED