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