qtinternetradio/ui/src/irplaylist.cpp
changeset 3 ee64f059b8e1
child 12 608f67c22514
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 #include <QFile>
       
    18 #include <QTextStream>
       
    19 #include <QStringList>
       
    20 #include <HbGlobal>
       
    21 #include "irplaylist.h"
       
    22 #include "irqisdsdatastructure.h"
       
    23 
       
    24 const QString KEqual("=");
       
    25 const QString KFile("File");
       
    26 const QString KTitle("Title");
       
    27 const QString KLength("Length");
       
    28 const QString KPlsExtension(".pls");
       
    29 const QString KM3uExtension(".m3u");
       
    30 const QString KHttpProtocol("http");
       
    31 const QString KMmsProtocol("mms");
       
    32 const QString KRtspProtocol("rtsp");
       
    33 const QString KM3uComment("#EXTINF");
       
    34 
       
    35 IRPlayListItem::IRPlayListItem(const QString &aFile, const QString &aTitle, int aLength) :
       
    36                                      iFile(aFile), iTitle(aTitle), iLength(aLength)
       
    37 {
       
    38 }
       
    39 
       
    40 const QString & IRPlayListItem::file() const
       
    41 {
       
    42     return iFile;
       
    43 }
       
    44 
       
    45 const QString & IRPlayListItem::title() const
       
    46 {
       
    47     return iTitle;
       
    48 }
       
    49 
       
    50 IRPlayList::IRPlayList()
       
    51 {
       
    52 }
       
    53 
       
    54 IRPlayList::~IRPlayList()
       
    55 {
       
    56     clearPlayList();
       
    57 }
       
    58     
       
    59 void IRPlayList::parseFile(const QString &aFileName)
       
    60 {
       
    61     iFileName = aFileName;
       
    62     QString extension = aFileName.right(4);
       
    63     
       
    64     if (0 == extension.compare(KPlsExtension, Qt::CaseInsensitive))
       
    65     {
       
    66         parsePlsFile(aFileName);
       
    67     }
       
    68     else if (0 == extension.compare(KM3uExtension, Qt::CaseInsensitive))
       
    69     {
       
    70         parseM3uFile(aFileName);
       
    71     }
       
    72     else
       
    73     {
       
    74         Q_ASSERT(false);
       
    75     }
       
    76 }
       
    77     
       
    78 int IRPlayList::getNumberOfEntries() const
       
    79 {
       
    80     return iItemList.count();
       
    81 }
       
    82 
       
    83 IRPlayListItem* IRPlayList::getEntry(int aIndex)
       
    84 {
       
    85     if (aIndex >=0 && aIndex < iItemList.count())
       
    86     {
       
    87         return iItemList.at(aIndex);
       
    88     }
       
    89     
       
    90     return NULL;
       
    91 }
       
    92 
       
    93 /*
       
    94  * create an object of IRQPreset for item at aIndex.
       
    95  * Note : caller must be responsible for deleting the returned object
       
    96  */
       
    97 IRQPreset* IRPlayList::getPresetForEntry(int aIndex)
       
    98 {
       
    99     IRQPreset *preset = NULL;
       
   100     if (aIndex >=0 && aIndex < iItemList.count())
       
   101     {
       
   102         preset = new IRQPreset;
       
   103         IRPlayListItem *item = iItemList.at(aIndex);
       
   104         IRQChannelServerURL server;
       
   105         server.bitrate = 0;
       
   106         server.url = item->file();
       
   107         server.serverName = item->title();
       
   108         
       
   109         preset->insertChannelServer(server);
       
   110         preset->name = item->title();
       
   111         preset->description = item->file();
       
   112         preset->shortDesc = item->file();
       
   113         preset->type = 0;
       
   114         preset->uniqID = 0;
       
   115         preset->presetId = 0;
       
   116     }
       
   117     
       
   118     return preset;
       
   119 }
       
   120 
       
   121 const QString& IRPlayList::getFileName() const
       
   122 {
       
   123     return iFileName;
       
   124 }
       
   125 
       
   126 void IRPlayList::deleteItem(int aIndex)
       
   127 {
       
   128     if (aIndex >=0 && aIndex < iItemList.count())
       
   129     {
       
   130         IRPlayListItem *item = iItemList.at(aIndex);
       
   131         delete item;
       
   132         iItemList.removeAt(aIndex);
       
   133     }
       
   134 }
       
   135 
       
   136 void IRPlayList::parsePlsFile(const QString &aFileName)
       
   137 {
       
   138     if (aFileName.length() == 0)
       
   139     {
       
   140         return;
       
   141     }
       
   142   
       
   143     clearPlayList();
       
   144     
       
   145     QFile file(aFileName);
       
   146     if (!file.open(QIODevice::ReadOnly))
       
   147     {
       
   148         return;
       
   149     }
       
   150     
       
   151     QTextStream stream(&file);
       
   152     QString readStr = stream.readLine().trimmed();
       
   153     if (readStr.compare("[playlist]", Qt::CaseInsensitive))
       
   154     {
       
   155         file.close();
       
   156         return;
       
   157     }
       
   158     
       
   159     readStr = stream.readLine().trimmed();
       
   160     QStringList splitStr = readStr.split(KEqual);
       
   161     if (splitStr.count() != 2)
       
   162     {
       
   163         file.close();
       
   164         return;
       
   165     }
       
   166     
       
   167     int nbEntries = 0;
       
   168     if (0 == splitStr.first().compare("NumberOfEntries", Qt::CaseInsensitive))
       
   169     {
       
   170         nbEntries = splitStr.last().toInt();
       
   171     }
       
   172     
       
   173     if (nbEntries <= 0)
       
   174     {
       
   175         file.close();
       
   176         return;
       
   177     }
       
   178     
       
   179     for (int i = 0; i < nbEntries; ++i)
       
   180     {
       
   181         //read file line : File=xxx     
       
   182         QString filePath = readValue(stream, KFile);
       
   183         if (filePath.length() == 0)
       
   184         {
       
   185             break;
       
   186         }
       
   187         
       
   188         //read title line : Title=xxx
       
   189         QString title = readValue(stream, KTitle);
       
   190         if (title.length() == 0)
       
   191         {
       
   192             break;
       
   193         }
       
   194         
       
   195         //read length line : Length=xxx
       
   196         QString strLength = readValue(stream, KLength);
       
   197         if (strLength.length() == 0)
       
   198         {
       
   199             break;
       
   200         }
       
   201         
       
   202         int length = strLength.toInt();
       
   203         
       
   204         //all information is ready
       
   205         if (length == -1)
       
   206         {
       
   207             IRPlayListItem *item = new IRPlayListItem(filePath, title, length);
       
   208             iItemList.append(item);
       
   209         }
       
   210     }
       
   211     
       
   212     file.close();
       
   213 }
       
   214 
       
   215 void IRPlayList::parseM3uFile(const QString &aFileName)
       
   216 {
       
   217     clearPlayList();
       
   218     
       
   219     QFile file(aFileName);
       
   220     if (!file.open(QIODevice::ReadOnly))
       
   221     {
       
   222         return;
       
   223     }
       
   224     
       
   225     QTextStream stream(&file);
       
   226     QString previousLine;
       
   227     QString currentLine;
       
   228     
       
   229     while (!stream.atEnd())
       
   230     {
       
   231         currentLine = stream.readLine().trimmed();
       
   232         if (currentLine.startsWith(KHttpProtocol, Qt::CaseInsensitive) ||
       
   233             currentLine.startsWith(KMmsProtocol, Qt::CaseInsensitive) ||
       
   234             currentLine.startsWith(KRtspProtocol, Qt::CaseInsensitive) )
       
   235         {
       
   236             IRPlayListItem *item = NULL;
       
   237             if (previousLine.startsWith(KM3uComment, Qt::CaseInsensitive))
       
   238             {
       
   239                 //previousLine can be used as station name
       
   240                 item = new IRPlayListItem(currentLine, previousLine, -1);
       
   241             }
       
   242             else
       
   243             {
       
   244                 //no station name is available
       
   245                 item = new IRPlayListItem(currentLine, hbTrId("txt_irad_info_unnamed"), -1);
       
   246             }
       
   247             iItemList.append(item);
       
   248         }
       
   249         
       
   250         previousLine = currentLine;
       
   251     }
       
   252     
       
   253     file.close();
       
   254 }
       
   255 
       
   256 void IRPlayList::clearPlayList()
       
   257 {
       
   258     while (!iItemList.isEmpty())
       
   259     {
       
   260         IRPlayListItem *firstItem = iItemList.takeFirst();
       
   261         delete firstItem;
       
   262     }
       
   263 }
       
   264 
       
   265 QString IRPlayList::readValue(QTextStream &aStream, const QString &aKey)
       
   266 {
       
   267     QString readStr;
       
   268     QStringList splitStr;
       
   269     
       
   270     do
       
   271     {
       
   272         readStr = aStream.readLine().trimmed();    
       
   273     }while (readStr.length() == 0 && !aStream.atEnd());
       
   274             
       
   275     splitStr = readStr.split(KEqual);
       
   276     if (splitStr.count() != 2 || !splitStr.first().startsWith(aKey, Qt::CaseInsensitive))
       
   277     {
       
   278         return QString();
       
   279     }
       
   280     else
       
   281     {
       
   282         return splitStr.last();
       
   283     }
       
   284 }