qtinternetradio/ui/src/irstationshare.cpp
changeset 3 ee64f059b8e1
child 8 3b03c28289e6
equal deleted inserted replaced
2:2e1adbfc62af 3:ee64f059b8e1
       
     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 
       
    18 
       
    19 #include <QTextStream>
       
    20 #include <QFile>
       
    21 #include <QStringList>
       
    22 #include <shareui.h>
       
    23 
       
    24 #include "irstationshare.h"
       
    25 #include "irqisdsdatastructure.h"
       
    26 
       
    27 const char KPlsFilePath[]  = "c:\\data\\share.pls";
       
    28 const char KPlsFileHeading[]  = "[playlist]";
       
    29 const char KPlsFileEntryNum[]  = "NumberOfEntries";
       
    30 const char KPlsFileEnding[]   = "Version=2";
       
    31 const char KPlsEntryElementFile[]   = "File";
       
    32 const char KPlsEntryElementTitle[]  = "Title";
       
    33 const char KPlsEntryElementLength[] = "Length";
       
    34 const char KPlsIndefiniteLength[] = "-1";
       
    35 
       
    36 static void constructPlsElement( QTextStream &aOutput, 
       
    37                           const IRQPreset &aPreset, 
       
    38                           int &aStationIndexBase);
       
    39 
       
    40 // ---------------------------------------------------------------------------
       
    41 // Constructor
       
    42 // ---------------------------------------------------------------------------
       
    43 //
       
    44 IRStationShare::IRStationShare()
       
    45             : iPlsFilePath(KPlsFilePath)
       
    46 {
       
    47 }             
       
    48 // ---------------------------------------------------------------------------
       
    49 // Destructor
       
    50 // ---------------------------------------------------------------------------
       
    51 //
       
    52 IRStationShare::~IRStationShare()
       
    53 {
       
    54 }
       
    55 
       
    56 // ---------------------------------------------------------------------------
       
    57 // NOTE : MUST ensure no changes to all the elements in the list,
       
    58 //        so, can ONLY access element via const_iterator
       
    59 // ---------------------------------------------------------------------------
       
    60 //
       
    61 bool IRStationShare::shareStations(const QList<IRQPreset*> &aPresetList)
       
    62 {
       
    63     if(!constructPlsFile(aPresetList))
       
    64     {
       
    65         return false;
       
    66     }
       
    67     
       
    68     ShareUi shareDialog;
       
    69     QStringList fileList;
       
    70     fileList.append(iPlsFilePath);
       
    71     return shareDialog.send(fileList,true);
       
    72 }
       
    73 
       
    74 bool IRStationShare::shareStations(const IRQPreset &aPreset)
       
    75 {
       
    76     if(!constructPlsFile(aPreset))
       
    77     {
       
    78         return false;
       
    79     }
       
    80     
       
    81     ShareUi shareDialog;
       
    82     QStringList fileList;
       
    83     fileList.append(iPlsFilePath);
       
    84     return shareDialog.send(fileList,true);
       
    85 }
       
    86 
       
    87 // ---------------------------------------------------------------------------
       
    88 // constructPlsFile
       
    89 // NOTE : MUST ensure no changes to all the elements in the list,
       
    90 //        so, can ONLY access element via const_iterator
       
    91 // ---------------------------------------------------------------------------
       
    92 //
       
    93 bool IRStationShare::constructPlsFile(const QList<IRQPreset*> &aPresetList)
       
    94 {
       
    95     if(0 == aPresetList.count())
       
    96     {
       
    97         return false;
       
    98     }
       
    99     
       
   100     QFile plsFile(iPlsFilePath);
       
   101     if( !plsFile.open(QIODevice::ReadWrite | QIODevice::Truncate) )
       
   102     {
       
   103         return false;
       
   104     }
       
   105     QTextStream outputStream( &plsFile );
       
   106     outputStream<<KPlsFileHeading<<endl;
       
   107     
       
   108     int entryNum = 0;
       
   109     for (QList<IRQPreset*>::const_iterator it = aPresetList.begin(); it != aPresetList.end(); ++it)
       
   110     {
       
   111         entryNum += (*it)->getChannelURLCount();
       
   112     }
       
   113     QString totalEntry;
       
   114     totalEntry.setNum(entryNum);
       
   115     outputStream<<KPlsFileEntryNum<<"="<<totalEntry<<endl;
       
   116     
       
   117     int stationIndexBase = 1;
       
   118     for (QList<IRQPreset*>::const_iterator it = aPresetList.begin(); it != aPresetList.end(); ++it)
       
   119     {
       
   120         constructPlsElement(outputStream,*(*it),stationIndexBase);
       
   121     }
       
   122     
       
   123     outputStream<<KPlsFileEnding;
       
   124     plsFile.close();
       
   125     return true;
       
   126 }
       
   127 
       
   128 bool IRStationShare::constructPlsFile(const IRQPreset &aPreset)
       
   129 {       
       
   130     QFile plsFile(iPlsFilePath);
       
   131     if( !plsFile.open(QIODevice::ReadWrite | QIODevice::Truncate) )
       
   132     {
       
   133         return false;
       
   134     }
       
   135     QTextStream outputStream( &plsFile );
       
   136     outputStream<<KPlsFileHeading<<endl;
       
   137     
       
   138     QString totalEntry;
       
   139     totalEntry.setNum(aPreset.getChannelURLCount());
       
   140     outputStream<<KPlsFileEntryNum<<"="<<totalEntry<<endl;
       
   141     
       
   142     int stationIndexBase = 1;
       
   143     constructPlsElement(outputStream,aPreset,stationIndexBase);
       
   144     
       
   145     outputStream<<KPlsFileEnding;
       
   146     plsFile.close();
       
   147     return true;
       
   148 }
       
   149 
       
   150 static void constructPlsElement( QTextStream &aOutput,
       
   151                           const IRQPreset &aPreset,
       
   152                           int &aStationIndexBase)
       
   153 {
       
   154     QList<int> bitrateList;
       
   155     aPreset.getAvailableBitrates(bitrateList);
       
   156     
       
   157     int urlIndex = 1;
       
   158     QString stationName;
       
   159     QList<QString> *urlList = NULL;
       
   160     
       
   161     for( int i=0; i<bitrateList.count(); i++ )
       
   162     {
       
   163         urlList = aPreset.getURLsForBitrate(bitrateList.at(i));
       
   164         for( int j=0; j<urlList->count(); j++ )
       
   165         {
       
   166             QString stationUrl = urlList->at(j);
       
   167             
       
   168             if( aPreset.getChannelURLCount() <= 1 )
       
   169             {
       
   170                 stationName = aPreset.name;
       
   171             }
       
   172             else
       
   173             {
       
   174                 QString stationUrlIndex;
       
   175                 stationUrlIndex.setNum(urlIndex);             
       
   176                 stationName = aPreset.name + " #[" + stationUrlIndex + "]";
       
   177             }
       
   178 
       
   179             QString stationIndex;
       
   180             stationIndex.setNum(aStationIndexBase);              
       
   181             aOutput<<KPlsEntryElementFile<<aStationIndexBase
       
   182                    <<"="<<stationUrl<<endl;
       
   183             aOutput<<KPlsEntryElementTitle<<aStationIndexBase
       
   184                    <<"="<<stationName<<endl;            
       
   185             aOutput<<KPlsEntryElementLength<<aStationIndexBase
       
   186                    <<"="<<KPlsIndefiniteLength<<endl;            
       
   187             
       
   188             urlIndex++;
       
   189             aStationIndexBase++;            
       
   190         }
       
   191         
       
   192         delete urlList;
       
   193     }
       
   194 }
       
   195