|
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 #include <QFile> |
|
19 #include <QTextStream> |
|
20 #include <QStringList> |
|
21 #include <QLocalSocket> |
|
22 #include <qcoreapplication.h> |
|
23 |
|
24 #include "irsymbiandocument.h" |
|
25 #include "irsymbianapplication.h" |
|
26 #include "irsymbianappui.h" |
|
27 |
|
28 const QString KEqual("="); |
|
29 const QString KFile("File"); |
|
30 const QString KTitle("Title"); |
|
31 const QString KLength("Length"); |
|
32 const QString KPlsExtension(".pls"); |
|
33 const QString KM3uExtension(".m3u"); |
|
34 |
|
35 IRSymbianDocument * IRSymbianDocument::documentInstance = NULL; |
|
36 |
|
37 IRPlsPlayListItem::IRPlsPlayListItem(const QString &aFile, const QString &aTitle, int aLength) : |
|
38 iFile(aFile), iTitle(aTitle), iLength(aLength) |
|
39 { |
|
40 } |
|
41 |
|
42 const QString & IRPlsPlayListItem::file() const |
|
43 { |
|
44 return iFile; |
|
45 } |
|
46 |
|
47 const QString & IRPlsPlayListItem::title() const |
|
48 { |
|
49 return iTitle; |
|
50 } |
|
51 |
|
52 IRSymbianDocument * IRSymbianDocument::getInstance() |
|
53 { |
|
54 if (NULL == documentInstance) |
|
55 { |
|
56 documentInstance = new IRSymbianDocument(*IRSymbianApplication::getInstance()); |
|
57 } |
|
58 |
|
59 return documentInstance; |
|
60 } |
|
61 |
|
62 bool IRSymbianDocument::isPlsAvailable() const |
|
63 { |
|
64 return iPlsList.count() > 0; |
|
65 } |
|
66 |
|
67 int IRSymbianDocument::getNumberOfEntries() const |
|
68 { |
|
69 return iPlsList.count(); |
|
70 } |
|
71 |
|
72 IRPlsPlayListItem* IRSymbianDocument::getEntry(int aIndex) |
|
73 { |
|
74 if (aIndex >=0 && aIndex < iPlsList.count()) |
|
75 { |
|
76 return iPlsList.at(aIndex); |
|
77 } |
|
78 |
|
79 return NULL; |
|
80 } |
|
81 |
|
82 IRSymbianDocument::IRSymbianDocument(CEikApplication &mainApplication) : QS60MainDocument(mainApplication) |
|
83 { |
|
84 |
|
85 } |
|
86 |
|
87 IRSymbianDocument::~IRSymbianDocument() |
|
88 { |
|
89 clearPlsList(); |
|
90 } |
|
91 |
|
92 void IRSymbianDocument::OpenFileL(CFileStore*& aFileStore, RFile& aFile) |
|
93 { |
|
94 aFileStore = NULL; |
|
95 TBuf<255> fileName; |
|
96 aFile.FullName(fileName); |
|
97 TParsePtr ptr(fileName); |
|
98 TPtrC extension = ptr.Ext(); |
|
99 QString qExtension = QString::fromUtf16(extension.Ptr(), extension.Length()); |
|
100 |
|
101 if (0 == qExtension.compare(KPlsExtension, Qt::CaseInsensitive) || |
|
102 0 == qExtension.compare(KM3uExtension, Qt::CaseInsensitive) ) |
|
103 { |
|
104 QString qFileName = QString::fromUtf16(fileName.Ptr(), fileName.Length()); |
|
105 IRSymbianApplication *symbianApp = static_cast<IRSymbianApplication*>(Application()); |
|
106 if (symbianApp->getInstanceFlag()) |
|
107 { |
|
108 //if an instance is already running, send the file name to that instance through socket |
|
109 QLocalSocket socket; |
|
110 QString serverName = QCoreApplication::applicationName(); |
|
111 socket.connectToServer(serverName); |
|
112 if (socket.waitForConnected(500)) |
|
113 { |
|
114 QTextStream stream(&socket); |
|
115 stream << qFileName; |
|
116 stream.flush(); |
|
117 socket.waitForBytesWritten(); |
|
118 } |
|
119 } |
|
120 else |
|
121 { |
|
122 parseFile(qFileName); |
|
123 } |
|
124 } |
|
125 } |
|
126 |
|
127 CEikAppUi* IRSymbianDocument::CreateAppUiL() |
|
128 { |
|
129 return new (ELeave) IRSymbianAppui; |
|
130 } |
|
131 |
|
132 void IRSymbianDocument::parseFile(const QString &aFileName) |
|
133 { |
|
134 QString extension = aFileName.right(4); |
|
135 |
|
136 if (0 == extension.compare(KPlsExtension, Qt::CaseInsensitive)) |
|
137 { |
|
138 parsePlsFile(aFileName); |
|
139 } |
|
140 else if (0 == extension.compare(KM3uExtension, Qt::CaseInsensitive)) |
|
141 { |
|
142 parseM3uFile(aFileName); |
|
143 } |
|
144 else |
|
145 { |
|
146 Q_ASSERT(false); |
|
147 } |
|
148 } |
|
149 |
|
150 void IRSymbianDocument::parsePlsFile(const QString &aFileName) |
|
151 { |
|
152 if (aFileName.length() == 0) |
|
153 { |
|
154 return; |
|
155 } |
|
156 |
|
157 clearPlsList(); |
|
158 |
|
159 QFile file(aFileName); |
|
160 if (!file.open(QIODevice::ReadOnly)) |
|
161 { |
|
162 return; |
|
163 } |
|
164 |
|
165 QTextStream stream(&file); |
|
166 QString readStr = stream.readLine().trimmed(); |
|
167 if (readStr.compare("[playlist]", Qt::CaseInsensitive)) |
|
168 { |
|
169 file.close(); |
|
170 return; |
|
171 } |
|
172 |
|
173 readStr = stream.readLine().trimmed(); |
|
174 QStringList splitStr = readStr.split(KEqual); |
|
175 if (splitStr.count() != 2) |
|
176 { |
|
177 file.close(); |
|
178 return; |
|
179 } |
|
180 |
|
181 int nbEntries = 0; |
|
182 if (0 == splitStr.first().compare("NumberOfEntries", Qt::CaseInsensitive)) |
|
183 { |
|
184 nbEntries = splitStr.last().toInt(); |
|
185 } |
|
186 |
|
187 if (nbEntries <= 0) |
|
188 { |
|
189 file.close(); |
|
190 return; |
|
191 } |
|
192 |
|
193 for (int i = 0; i < nbEntries; ++i) |
|
194 { |
|
195 //read file line : File=xxx |
|
196 QString filePath = readValue(stream, KFile); |
|
197 if (filePath.length() == 0) |
|
198 { |
|
199 break; |
|
200 } |
|
201 |
|
202 //read title line : Title=xxx |
|
203 QString title = readValue(stream, KTitle); |
|
204 if (title.length() == 0) |
|
205 { |
|
206 break; |
|
207 } |
|
208 |
|
209 //read length line : Length=xxx |
|
210 QString strLength = readValue(stream, KLength); |
|
211 if (strLength.length() == 0) |
|
212 { |
|
213 break; |
|
214 } |
|
215 |
|
216 int length = strLength.toInt(); |
|
217 |
|
218 //all information is ready |
|
219 if (length == -1) |
|
220 { |
|
221 IRPlsPlayListItem *item = new IRPlsPlayListItem(filePath, title, length); |
|
222 iPlsList.append(item); |
|
223 } |
|
224 } |
|
225 |
|
226 file.close(); |
|
227 } |
|
228 |
|
229 void IRSymbianDocument::parseM3uFile(const QString &aFileName) |
|
230 { |
|
231 clearPlsList(); |
|
232 |
|
233 QFile file(aFileName); |
|
234 if (!file.open(QIODevice::ReadOnly)) |
|
235 { |
|
236 return; |
|
237 } |
|
238 |
|
239 QTextStream stream(&file); |
|
240 QString readStr; |
|
241 int stationCount = 0; |
|
242 |
|
243 while (!stream.atEnd()) |
|
244 { |
|
245 readStr = stream.readLine().trimmed(); |
|
246 if (readStr.startsWith("http://", Qt::CaseInsensitive)) |
|
247 { |
|
248 IRPlsPlayListItem *item = new IRPlsPlayListItem(readStr, "Station " + QString::number(++stationCount), -1); |
|
249 iPlsList.append(item); |
|
250 } |
|
251 } |
|
252 |
|
253 file.close(); |
|
254 } |
|
255 |
|
256 void IRSymbianDocument::clearPlsList() |
|
257 { |
|
258 while (!iPlsList.isEmpty()) |
|
259 { |
|
260 IRPlsPlayListItem *firstItem = iPlsList.takeFirst(); |
|
261 delete firstItem; |
|
262 } |
|
263 } |
|
264 |
|
265 QString IRSymbianDocument::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 } |