qtinternetradio/irqcommon/src/irqutility.cpp
changeset 0 09774dfdd46b
child 3 ee64f059b8e1
equal deleted inserted replaced
-1:000000000000 0:09774dfdd46b
       
     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 #include <QString>
       
    18 #include <QByteArray>
       
    19 #include <QPixmap>
       
    20 #include <e32err.h>
       
    21 #include <fbs.h>
       
    22 #include <apmstd.h>
       
    23 #include <apgcli.h>
       
    24 #include <apgtask.h>
       
    25 #include <coemain.h> 
       
    26 
       
    27 #ifdef _DEBUG
       
    28 #include <QFile>
       
    29 #include <QTextStream>
       
    30 #endif
       
    31 
       
    32 #include "irqutility.h"
       
    33 #include "irchannelserverurl.h"
       
    34 #include "irqisdsdatastructure.h"
       
    35 #include "irisdspreset.h"
       
    36 #include "irpreset.h"
       
    37 
       
    38 _LIT8( KIRHtmlMimeType, "text/html" );
       
    39 /** Browser prefix: See browser API specification */
       
    40 _LIT( KBrowserPrefix, "4 " );
       
    41 
       
    42 //
       
    43 //convert the CIRIsdsPreset to the IRQPreset. 
       
    44 //
       
    45 EXPORT_C void IRQUtility::convertCIRIsdsPreset2IRQPrest(const CIRIsdsPreset& aCIRIsdsPreset, IRQPreset& aQIRPreset)
       
    46 {
       
    47   aQIRPreset.uniqID = aCIRIsdsPreset.UniqId(); 
       
    48   aQIRPreset.type = aCIRIsdsPreset.GetChannelType();
       
    49   aQIRPreset.index  = aCIRIsdsPreset.GetIndex();
       
    50   
       
    51   aQIRPreset.presetId = aCIRIsdsPreset.GetId();
       
    52   aQIRPreset.iChannelUrlCount  = aCIRIsdsPreset.GetUrlCount();
       
    53   aQIRPreset.name = QString::fromUtf16((aCIRIsdsPreset.GetName()).Ptr(), (aCIRIsdsPreset.GetName()).Length());
       
    54   
       
    55   aQIRPreset.languageCode  = QString::fromUtf16(aCIRIsdsPreset.GetLangId().Ptr(), aCIRIsdsPreset.GetLangId().Length());
       
    56   aQIRPreset.languageName  = QString::fromUtf16(aCIRIsdsPreset.GetLangName().Ptr(), aCIRIsdsPreset.GetLangName().Length());
       
    57   
       
    58   aQIRPreset.countryCode  = QString::fromUtf16(aCIRIsdsPreset.GetCountryId().Ptr(),aCIRIsdsPreset.GetCountryId().Length());
       
    59   aQIRPreset.countryName  = QString::fromUtf16(aCIRIsdsPreset.GetCountryName().Ptr(),aCIRIsdsPreset.GetCountryName().Length());
       
    60   
       
    61   aQIRPreset.lastModified  = QString::fromUtf16(aCIRIsdsPreset.GetLastModifiedTime().Ptr(), aCIRIsdsPreset.GetLastModifiedTime().Length());
       
    62   aQIRPreset.description  = QString::fromUtf16(aCIRIsdsPreset.GetDescription().Ptr(), aCIRIsdsPreset.GetDescription().Length());
       
    63   aQIRPreset.shortDesc  = QString::fromUtf16(aCIRIsdsPreset.GetShortDescription().Ptr(), aCIRIsdsPreset.GetShortDescription().Length());
       
    64   
       
    65   aQIRPreset.genreId  = QString::fromUtf16(aCIRIsdsPreset.GetGenreId().Ptr(), aCIRIsdsPreset.GetGenreId().Length());
       
    66   aQIRPreset.genreName  = QString::fromUtf16(aCIRIsdsPreset.GetGenreName().Ptr(), aCIRIsdsPreset.GetGenreName().Length());
       
    67   
       
    68   aQIRPreset.advertisementUrl  = QString::fromUtf16(aCIRIsdsPreset.GetAdvertisementUrl().Ptr(), aCIRIsdsPreset.GetAdvertisementUrl().Length());
       
    69   aQIRPreset.advertisementInUse  = QString::fromUtf16(aCIRIsdsPreset.GetAdvertisementInUse().Ptr(),aCIRIsdsPreset.GetAdvertisementInUse().Length());
       
    70   
       
    71   aQIRPreset.imgUrl  = QString::fromUtf16(aCIRIsdsPreset.GetImgUrl().Ptr(), aCIRIsdsPreset.GetImgUrl().Length());   
       
    72   aQIRPreset.logoData.Close();
       
    73   aQIRPreset.logoData.Create(aCIRIsdsPreset.GetLogoData());
       
    74   
       
    75   aQIRPreset.musicStoreStatus  = QString::fromUtf16(aCIRIsdsPreset.GetMusicStoreStatus().Ptr(), aCIRIsdsPreset.GetMusicStoreStatus().Length());
       
    76   aQIRPreset.clearChannelServerList();
       
    77   
       
    78   IRQChannelServerURL url;
       
    79   CIRChannelServerUrl *tempServerUrl = NULL;
       
    80   
       
    81   for(int i=0; i<aCIRIsdsPreset.iUrlArray->Count(); i++)
       
    82   {
       
    83       tempServerUrl = aCIRIsdsPreset.iUrlArray->At(i);
       
    84       url.bitrate = tempServerUrl->GetBitRate();
       
    85       url.serverName = QString::fromUtf16(tempServerUrl->GetServerName().Ptr(), tempServerUrl->GetServerName().Length());
       
    86       url.url = QString::fromUtf16(tempServerUrl->GetServerUrl().Ptr(), tempServerUrl->GetServerUrl().Length());
       
    87       aQIRPreset.insertChannelServer(url);
       
    88   }
       
    89   
       
    90   aQIRPreset.sortURLArray();   
       
    91 }
       
    92 
       
    93 //convert the IRQPreset to the CIRIsdsPreset 
       
    94 //
       
    95 EXPORT_C void IRQUtility::convertIRQPreset2CIRIsdsPreset(const IRQPreset& aQIRPreset, CIRIsdsPreset& aCIRIsdsPreset)
       
    96 {
       
    97   aCIRIsdsPreset.SetId(aQIRPreset.uniqID);
       
    98   aCIRIsdsPreset.SetIndex(aQIRPreset.index);
       
    99   aCIRIsdsPreset.SetId(aQIRPreset.presetId);
       
   100   aCIRIsdsPreset.SetUrlCount(aQIRPreset.iChannelUrlCount);
       
   101   aCIRIsdsPreset.SetChannelType(aQIRPreset.type);  
       
   102   
       
   103   TPtrC16 namePtr(reinterpret_cast<const TUint16*>(aQIRPreset.name.utf16()));
       
   104   aCIRIsdsPreset.SetName(namePtr);
       
   105   
       
   106   TPtrC16 languageCodePtr(reinterpret_cast<const TUint16*>(aQIRPreset.languageCode.utf16()));
       
   107   aCIRIsdsPreset.SetLangCode(languageCodePtr);
       
   108   
       
   109   TPtrC16 languageNamePtr(reinterpret_cast<const TUint16*>(aQIRPreset.languageName.utf16()));
       
   110   aCIRIsdsPreset.SetLang(languageNamePtr);
       
   111   
       
   112   TPtrC16 countryCode(reinterpret_cast<const TUint16*>(aQIRPreset.countryCode.utf16()));
       
   113   aCIRIsdsPreset.SetCountryCode(countryCode);
       
   114   
       
   115   TPtrC16 countryName(reinterpret_cast<const TUint16*>(aQIRPreset.countryName.utf16()));
       
   116   aCIRIsdsPreset.SetCountryName(countryName);
       
   117   
       
   118   TPtrC16 lastModified(reinterpret_cast<const TUint16*>(aQIRPreset.lastModified.utf16()));
       
   119   aCIRIsdsPreset.SetLastModifiedTime(lastModified);
       
   120   
       
   121   TPtrC16 description(reinterpret_cast<const TUint16*>(aQIRPreset.description.utf16()));
       
   122   aCIRIsdsPreset.SetDescription(description);
       
   123   
       
   124   TPtrC16 shortDescription(reinterpret_cast<const TUint16*>(aQIRPreset.shortDesc.utf16()));
       
   125   aCIRIsdsPreset.SetShortDesc(shortDescription);
       
   126   
       
   127   TPtrC16 genreId(reinterpret_cast<const TUint16*>(aQIRPreset.genreId.utf16()));
       
   128   aCIRIsdsPreset.SetGenreId(genreId);
       
   129   
       
   130   TPtrC16 genreName(reinterpret_cast<const TUint16*>(aQIRPreset.genreName.utf16()));
       
   131   aCIRIsdsPreset.SetGenreName(genreName);
       
   132   
       
   133   TPtrC16 advertisementUrl(reinterpret_cast<const TUint16*>(aQIRPreset.advertisementUrl.utf16()));
       
   134   aCIRIsdsPreset.SetAdvertisementUrl(advertisementUrl);
       
   135   
       
   136   TPtrC16 advertisementINUse(reinterpret_cast<const TUint16*>(aQIRPreset.advertisementInUse.utf16()));
       
   137   aCIRIsdsPreset.SetAdvertisementInUse(advertisementINUse);
       
   138   
       
   139   TPtrC16 imgUrl(reinterpret_cast<const TUint16*>(aQIRPreset.imgUrl.utf16()));
       
   140   aCIRIsdsPreset.SetImgUrl(imgUrl);
       
   141   
       
   142   TPtrC16 musicStoreStatus(reinterpret_cast<const TUint16*>(aQIRPreset.musicStoreStatus.utf16()));
       
   143   aCIRIsdsPreset.SetMusicStoreStatus(musicStoreStatus);   
       
   144   aCIRIsdsPreset.SetLogoData(aQIRPreset.logoData);
       
   145 
       
   146   TRAP_IGNORE(appendURLL(aQIRPreset, aCIRIsdsPreset));    
       
   147 }
       
   148 
       
   149 //convert CIRPreset to the IRQPreset 
       
   150 //
       
   151 EXPORT_C void IRQUtility::convertCIRPreset2IRQPreset(const CIRPreset& aCIRPreset, IRQPreset& aIRQPreset)
       
   152 {
       
   153   CIRIsdsPreset * cirIsdsPreset = NULL;  
       
   154   TRAPD(err, cirIsdsPreset = CIRIsdsPreset::NewL());
       
   155   if( KErrNone != err)
       
   156     return;
       
   157    
       
   158   aCIRPreset.CopyPresetData(*cirIsdsPreset);  
       
   159   convertCIRIsdsPreset2IRQPrest(*cirIsdsPreset, aIRQPreset);
       
   160   delete cirIsdsPreset;  
       
   161 }
       
   162 
       
   163 //switch from the symbian error code to the IRQ error code 
       
   164 //
       
   165 EXPORT_C void IRQUtility::convertSError2QError(const int aSError, int& aQError)
       
   166 {
       
   167   if( aSError > 0 )
       
   168   {
       
   169     aQError = aSError;
       
   170     return;
       
   171   }
       
   172   
       
   173   switch( aSError )
       
   174   {
       
   175     case KErrNone:
       
   176       aQError = EIRQErrorNone;
       
   177       break;
       
   178     case KErrNotFound:
       
   179       aQError = EIRQErrorNotFound;
       
   180       break;
       
   181     case KErrAlreadyExists:
       
   182       aQError = EIRQErrorAlreadyExist;
       
   183       break;
       
   184       
       
   185     case KErrNoMemory:
       
   186         aQError = EIRQErrorOutOfMemory;
       
   187         break;
       
   188         
       
   189     default:
       
   190       aQError = EIRQErrorGeneral;
       
   191       break;
       
   192   } 
       
   193 }
       
   194 
       
   195 /*
       
   196  * Description : check the validity of URL
       
   197  * return value: true: the URL is valid, vice versa.
       
   198  */
       
   199 EXPORT_C bool IRQUtility::isValidUrl(const QString& aUrl)
       
   200 {
       
   201     if (aUrl.size() < 8)
       
   202     {
       
   203         return false;
       
   204     }
       
   205 
       
   206     QString prefix7 = aUrl.left(7);
       
   207     QString prefix6 = aUrl.left(6);
       
   208 
       
   209     if (0 == prefix7.compare("http://", Qt::CaseInsensitive) ||
       
   210         0 == prefix7.compare("rtsp://", Qt::CaseInsensitive) ||
       
   211         0 == prefix6.compare("mms://", Qt::CaseInsensitive))
       
   212     {
       
   213         return true;
       
   214     }
       
   215 
       
   216     return false;
       
   217 }
       
   218 
       
   219 /*
       
   220  * Description : check the validity of URL
       
   221  * return value: true: the URL is valid, vice versa.
       
   222  */
       
   223 EXPORT_C bool IRQUtility::openAdvLink(const QString& aUrl)
       
   224 {
       
   225     bool retval = false;
       
   226     TRAP_IGNORE(retval = openAdvLinkL(aUrl));
       
   227 
       
   228     return retval;
       
   229 }
       
   230 
       
   231 EXPORT_C void IRQUtility::writeLog2File(const QString& aString, bool aDeleteOldFile)
       
   232 {       
       
   233 #ifdef _DEBUG 
       
   234     QFile file("C:\\data\\QTIRLog.txt");
       
   235     
       
   236     if (file.exists() && aDeleteOldFile)
       
   237     {
       
   238         bool ret = file.remove();
       
   239         if (!ret)
       
   240         {
       
   241             return;
       
   242         }
       
   243     }
       
   244 
       
   245     bool ret = file.open(QIODevice::ReadWrite);    
       
   246     if( !ret )
       
   247     {
       
   248         return;
       
   249     }
       
   250     
       
   251     QTextStream stream(&file);
       
   252     stream << aString;
       
   253     stream << "\r\n";    
       
   254 
       
   255     stream.flush();
       
   256     file.close();
       
   257    
       
   258 #else
       
   259     Q_UNUSED(aString);
       
   260     Q_UNUSED(aDeleteOldFile);
       
   261 #endif
       
   262 
       
   263 }
       
   264 
       
   265 void IRQUtility::appendURLL(const IRQPreset& aQIRPreset, CIRIsdsPreset& aCIRIsdsPreset)
       
   266 {
       
   267     int count = aQIRPreset.urlArray.count();
       
   268     for( int i=0; i<count; i++)
       
   269     {
       
   270       CIRChannelServerUrl*  tempServerUrl = CIRChannelServerUrl::NewLC(); 
       
   271       TPtrC16 serverName(reinterpret_cast<const TUint16*>(aQIRPreset.urlArray[i].serverName.utf16()));
       
   272       TPtrC16 url(reinterpret_cast<const TUint16*>(aQIRPreset.urlArray[i].url.utf16()));
       
   273       tempServerUrl->SetServerName(serverName);
       
   274       tempServerUrl->SetServerUrl(url);
       
   275       tempServerUrl->SetBitRate(aQIRPreset.urlArray[i].bitrate);
       
   276       aCIRIsdsPreset.iUrlArray->AppendL(tempServerUrl);
       
   277       CleanupStack::Pop(tempServerUrl);     
       
   278     } 
       
   279 }
       
   280 
       
   281 bool IRQUtility::openAdvLinkL(const QString& aUrl)
       
   282 {
       
   283     if (aUrl.size() == 0)
       
   284     {
       
   285         return false;
       
   286     }
       
   287 
       
   288     bool ret = true;
       
   289 
       
   290     RWsSession wsSession;
       
   291     User::LeaveIfError(wsSession.Connect());
       
   292     CleanupClosePushL(wsSession);
       
   293 
       
   294     RApaLsSession ls;
       
   295     User::LeaveIfError(ls.Connect());
       
   296     CleanupClosePushL( ls );
       
   297 
       
   298     TDataType htmlDataType( KIRHtmlMimeType );
       
   299     TUid uid;
       
   300     TPtrC16 httpLink(reinterpret_cast<const TUint16*>(aUrl.utf16()));;
       
   301 
       
   302     // Get UID of the browser.
       
   303     TInt retVal = ls.AppForDataType( htmlDataType, uid );
       
   304 
       
   305     if ( retVal == KErrNone && uid != KNullUid )
       
   306     {
       
   307         // We must check if the browser is already open and if it is, 
       
   308         // switch its currently opened file to the correct URL.
       
   309         TApaTaskList taskList( wsSession );
       
   310         TApaTask task = taskList.FindApp( uid );
       
   311         if ( task.Exists() )
       
   312         {
       
   313             HBufC8* url8 = HBufC8::NewLC( httpLink.Length() );
       
   314             url8->Des().Copy( httpLink );
       
   315             task.SendMessage( TUid::Uid( 0 ), url8->Des() );
       
   316             task.BringToForeground();
       
   317             CleanupStack::PopAndDestroy( url8 );
       
   318         }
       
   319         else
       
   320         {
       
   321             TThreadId threadId;
       
   322             HBufC* newUrl = HBufC::NewLC( httpLink.Length() + KBrowserPrefix().Length() );
       
   323             newUrl->Des().Append( KBrowserPrefix );
       
   324             newUrl->Des().Append( httpLink );
       
   325             ls.StartDocument( newUrl->Des(), uid, threadId );
       
   326             CleanupStack::PopAndDestroy( newUrl );
       
   327         }
       
   328     }
       
   329     else
       
   330     {
       
   331         ret = false;
       
   332     }
       
   333     CleanupStack::PopAndDestroy( &ls );
       
   334 
       
   335     wsSession.Flush();    
       
   336     CleanupStack::PopAndDestroy( &wsSession );
       
   337 
       
   338     return ret;
       
   339 }
       
   340 
       
   341