author | Sebastian Brannstrom <sebastianb@symbian.org> |
Tue, 19 Oct 2010 14:02:32 +0100 | |
branch | symbian1 |
changeset 260 | 72b950ec8da2 |
parent 246 | 140a404c6b53 |
child 336 | 3d6c1417e8bd |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2 |
* Copyright (c) 2007-2010 Sebastian Brannstrom, Lars Persson, EmbedDev AB |
|
3 |
* |
|
4 |
* All rights reserved. |
|
5 |
* This component and the accompanying materials are made available |
|
6 |
* under the terms of the License "Eclipse Public License v1.0" |
|
7 |
* which accompanies this distribution, and is available |
|
8 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
9 |
* |
|
10 |
* Initial Contributors: |
|
11 |
* EmbedDev AB - initial contribution. |
|
12 |
* |
|
13 |
* Contributors: |
|
14 |
* |
|
15 |
* Description: |
|
16 |
* |
|
17 |
*/ |
|
18 |
||
19 |
#include <e32std.h> |
|
20 |
#include <e32base.h> |
|
21 |
#include <TXTETEXT.H> // for ELineBreak |
|
22 |
#include "PodcastUtils.h" |
|
23 |
#include "debug.h" |
|
24 |
#include "FeedEngine.h" // for KMaxDescriptionLength |
|
25 |
||
26 |
EXPORT_C void PodcastUtils::FixProtocolsL(TDes &aUrl) |
|
27 |
{ |
|
28 |
HBufC* urlCopy = aUrl.AllocLC(); |
|
29 |
TPtr urlCopyPtr (urlCopy->Des()); |
|
30 |
urlCopyPtr.LowerCase(); |
|
31 |
||
32 |
// url is always present so access that |
|
33 |
// Some pod links are written in format itpc://mylink.net/podcast.xml |
|
34 |
// Symbian HTTP stack does not like itpc:// |
|
35 |
// Try to use a HTTP instead. |
|
36 |
TInt p = urlCopyPtr.Find(KItpcPrefix); |
|
37 |
if (p >= 0) |
|
38 |
{ |
|
39 |
aUrl.Delete(p, KItpcPrefix().Length()); |
|
40 |
} |
|
41 |
||
42 |
// Some pod links are written in format pcast://mylink.net/podcast.xml |
|
43 |
// Symbian HTTP stack does not like itpc:// |
|
44 |
// Try to use a HTTP instead. |
|
45 |
p = urlCopyPtr.Find(KPcastPrefix); |
|
46 |
if (p >= 0) |
|
47 |
{ |
|
48 |
aUrl.Delete(p, KPcastPrefix().Length()); |
|
49 |
} |
|
50 |
||
51 |
// The URL must start with http://, otherwise the HTTP stack fails. |
|
52 |
TInt pos = urlCopyPtr.Find(KURLPrefix); |
|
53 |
if (pos == KErrNotFound) |
|
54 |
{ |
|
55 |
HBufC* newUrl = HBufC::NewL(aUrl.Length() + KURLPrefix().Length()); |
|
56 |
TPtr ptr = newUrl->Des(); |
|
57 |
ptr.Append(KURLPrefix()); |
|
58 |
ptr.Append(aUrl); |
|
59 |
||
60 |
// replace the url buffer |
|
61 |
aUrl.Copy(*newUrl); |
|
62 |
delete newUrl; |
|
63 |
} |
|
64 |
||
65 |
CleanupStack::PopAndDestroy(urlCopy); |
|
66 |
} |
|
67 |
||
68 |
||
69 |
EXPORT_C void PodcastUtils::CleanHtmlL(TDes &str) |
|
70 |
{ |
|
240
03e8cc4066ba
Fix for bug 2818
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
161
diff
changeset
|
71 |
DP("PodcastUtils::CleanHtmlL BEGIN"); |
21 | 72 |
if (str.Length() == 0) |
73 |
{ |
|
74 |
return; |
|
75 |
} |
|
76 |
||
240
03e8cc4066ba
Fix for bug 2818
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
161
diff
changeset
|
77 |
DP(" miscellaneous"); |
18 | 78 |
// miscellaneous cleanup |
2 | 79 |
const TChar KLineBreak(CEditableText::ELineBreak); |
80 |
_LIT(KNewLine, "\n"); |
|
19 | 81 |
_LIT(KNewLineWindows, "\r\n"); |
2 | 82 |
ReplaceString(str, KNewLine, KNullDesC); |
19 | 83 |
ReplaceString(str, KNewLineWindows, KNullDesC); |
18 | 84 |
|
240
03e8cc4066ba
Fix for bug 2818
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
161
diff
changeset
|
85 |
DP(" strip HTML"); |
18 | 86 |
// strip out HTML tags |
87 |
||
2 | 88 |
TInt startPos = str.Locate('<'); |
89 |
TInt endPos = str.Locate('>'); |
|
90 |
HBufC* tmpBuf = HBufC::NewLC(KMaxDescriptionLength); |
|
91 |
TPtr tmp(tmpBuf->Des()); |
|
92 |
while (startPos != KErrNotFound && endPos != KErrNotFound && endPos > startPos) { |
|
93 |
//DP1("Cleaning out %S", &str.Mid(startPos, endPos-startPos+1)); |
|
94 |
tmp.Copy(str.Left(startPos)); |
|
95 |
TPtrC ptr=str.Mid(startPos, endPos-startPos+1); |
|
96 |
_LIT(KHtmlBr, "<br>"); |
|
97 |
_LIT(KHtmlBr2, "<br />"); |
|
98 |
_LIT(KHtmlBr3, "<br/>"); |
|
99 |
_LIT(KHtmlP, "<p>"); |
|
100 |
if (ptr.CompareF(KHtmlBr)== 0 || ptr.CompareF(KHtmlBr2)== 0 || ptr.CompareF(KHtmlBr3)== 0) |
|
101 |
{ |
|
102 |
tmp.Append(KLineBreak); |
|
103 |
} |
|
104 |
else if (ptr.CompareF(KHtmlP) == 0) |
|
105 |
{ |
|
106 |
tmp.Append(KLineBreak); |
|
107 |
tmp.Append(KLineBreak); |
|
108 |
} |
|
109 |
||
110 |
if (str.Length() > endPos+1) { |
|
111 |
tmp.Append(str.Mid(endPos+1)); |
|
112 |
} |
|
113 |
||
114 |
str.Copy(tmp); |
|
115 |
startPos = str.Locate('<'); |
|
116 |
endPos = str.Locate('>'); |
|
117 |
} |
|
18 | 118 |
|
240
03e8cc4066ba
Fix for bug 2818
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
161
diff
changeset
|
119 |
DP(" change HTML encoded chars to unicode"); |
18 | 120 |
// change HTML encoded chars to unicode |
121 |
startPos = str.Locate('&'); |
|
122 |
endPos = str.Locate(';'); |
|
123 |
while (startPos != KErrNotFound && endPos != KErrNotFound && endPos > startPos) |
|
124 |
{ |
|
125 |
TPtrC ptr(str.Mid(startPos+1, endPos-startPos)); |
|
126 |
// check for whitespace |
|
127 |
if (ptr.Locate(' ') == KErrNotFound) |
|
128 |
{ |
|
129 |
// numerical constant |
|
130 |
if (ptr[0] == '#') |
|
131 |
{ |
|
132 |
TUint length = endPos - startPos; |
|
133 |
if (length > 2) |
|
134 |
{ |
|
135 |
tmp.Copy(str.Left(startPos)); |
|
136 |
ptr.Set(str.Mid(startPos+2, length-2)); |
|
137 |
||
138 |
TUint charCode = 0; |
|
139 |
||
140 |
if (ptr[0] == 'x') |
|
141 |
{ |
|
142 |
// hexadecimal |
|
143 |
ptr.Set(ptr.Mid(1)); |
|
144 |
TLex16 lex(ptr); |
|
145 |
lex.Val(charCode, EHex); |
|
146 |
} |
|
147 |
else |
|
148 |
{ |
|
149 |
//decimal |
|
150 |
TLex16 lex(ptr); |
|
151 |
lex.Val(charCode, EDecimal); |
|
152 |
} |
|
153 |
||
154 |
TChar charChar(charCode); |
|
155 |
tmp.Append(charChar); |
|
156 |
tmp.Append(str.Mid(endPos+1)); |
|
157 |
str.Copy(tmp); |
|
158 |
} |
|
159 |
} |
|
160 |
// literal constant |
|
161 |
else |
|
162 |
{ |
|
163 |
_LIT(KAmp, "amp;"); |
|
164 |
_LIT(KQuot, "quot;"); |
|
165 |
_LIT(KNbsp, "nbsp;"); |
|
166 |
_LIT(KCopy, "copy;"); |
|
167 |
||
168 |
// copy start of string |
|
169 |
tmp.Copy(str.Left(startPos)); |
|
170 |
||
171 |
if (ptr.CompareF(KAmp) == 0) |
|
172 |
{ |
|
173 |
tmp.Append('&'); |
|
174 |
} |
|
175 |
else if (ptr.CompareF(KQuot) == 0) |
|
176 |
{ |
|
177 |
tmp.Append('"'); |
|
178 |
} |
|
179 |
else if (ptr.CompareF(KNbsp) == 0) |
|
180 |
{ |
|
181 |
tmp.Append(' '); |
|
182 |
} |
|
183 |
else if (ptr.CompareF(KCopy) == 0) |
|
184 |
{ |
|
185 |
tmp.Append('\xA9'); |
|
186 |
} |
|
187 |
||
188 |
// copy end of string |
|
189 |
tmp.Append(str.Mid(endPos+1)); |
|
190 |
str.Copy(tmp); |
|
191 |
} |
|
192 |
} |
|
193 |
||
194 |
TInt newPos = str.Mid(startPos+1).Locate('&'); |
|
195 |
||
196 |
if (newPos != KErrNotFound) |
|
197 |
{ |
|
198 |
startPos = startPos+1 + newPos; |
|
199 |
endPos = str.Locate(';'); |
|
200 |
} |
|
201 |
else |
|
202 |
{ |
|
203 |
startPos = KErrNotFound; |
|
204 |
endPos = KErrNotFound; |
|
205 |
} |
|
206 |
} |
|
207 |
||
2 | 208 |
CleanupStack::PopAndDestroy(tmpBuf); |
19 | 209 |
|
240
03e8cc4066ba
Fix for bug 2818
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
161
diff
changeset
|
210 |
DP(" trim"); |
161 | 211 |
if(str.Length()>1) |
212 |
{ |
|
240
03e8cc4066ba
Fix for bug 2818
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
161
diff
changeset
|
213 |
DP1("str.Length() ==%d", str.Length()); |
161 | 214 |
// chop away newlines at start |
240
03e8cc4066ba
Fix for bug 2818
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
161
diff
changeset
|
215 |
while (str.Length() && (str[0] == KLineBreak)) { |
03e8cc4066ba
Fix for bug 2818
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
161
diff
changeset
|
216 |
DP("mid"); |
161 | 217 |
str = str.Mid(1); |
218 |
} |
|
219 |
||
220 |
// chop away newlines at end |
|
19 | 221 |
|
240
03e8cc4066ba
Fix for bug 2818
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
161
diff
changeset
|
222 |
while (str.Length() && (str[str.Length()-1] == KLineBreak)) { |
03e8cc4066ba
Fix for bug 2818
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
161
diff
changeset
|
223 |
DP("left"); |
161 | 224 |
str = str.Left(str.Length()-1); |
225 |
} |
|
19 | 226 |
|
161 | 227 |
str.Trim(); |
228 |
} |
|
240
03e8cc4066ba
Fix for bug 2818
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
161
diff
changeset
|
229 |
DP("PodcastUtils::CleanHtmlL END"); |
2 | 230 |
} |
231 |
||
15
93d9f66bf50b
Cleaning description better for second line in search results
teknolog
parents:
2
diff
changeset
|
232 |
EXPORT_C void PodcastUtils::RemoveAllFormatting(TDes & aString) |
93d9f66bf50b
Cleaning description better for second line in search results
teknolog
parents:
2
diff
changeset
|
233 |
{ |
93d9f66bf50b
Cleaning description better for second line in search results
teknolog
parents:
2
diff
changeset
|
234 |
// check for combination first so we prevent adding two spaces |
93d9f66bf50b
Cleaning description better for second line in search results
teknolog
parents:
2
diff
changeset
|
235 |
ReplaceString(aString,_L("\r\n"), _L(" ")); |
93d9f66bf50b
Cleaning description better for second line in search results
teknolog
parents:
2
diff
changeset
|
236 |
|
93d9f66bf50b
Cleaning description better for second line in search results
teknolog
parents:
2
diff
changeset
|
237 |
ReplaceString(aString,_L("\n"), _L(" ")); |
93d9f66bf50b
Cleaning description better for second line in search results
teknolog
parents:
2
diff
changeset
|
238 |
ReplaceString(aString,_L("\r"), _L(" ")); |
93d9f66bf50b
Cleaning description better for second line in search results
teknolog
parents:
2
diff
changeset
|
239 |
|
93d9f66bf50b
Cleaning description better for second line in search results
teknolog
parents:
2
diff
changeset
|
240 |
} |
93d9f66bf50b
Cleaning description better for second line in search results
teknolog
parents:
2
diff
changeset
|
241 |
|
2 | 242 |
EXPORT_C void PodcastUtils::ReplaceString(TDes & aString, const TDesC& aStringToReplace, const TDesC& aReplacement) |
243 |
{ |
|
244 |
TInt pos=aString.Find(aStringToReplace); |
|
245 |
TInt offset = 0; |
|
246 |
while (pos != KErrNotFound) |
|
247 |
{ |
|
248 |
aString.Replace(offset+pos, aStringToReplace.Length(), aReplacement); |
|
249 |
offset += pos + aStringToReplace.Length()+1; |
|
250 |
if (offset > aString.Length()) |
|
251 |
{ |
|
252 |
return; |
|
253 |
} |
|
254 |
||
255 |
pos=aString.Mid(offset).Find(aStringToReplace); |
|
256 |
} |
|
257 |
} |
|
258 |
||
259 |
EXPORT_C void PodcastUtils::ReplaceChar(TDes & aString, TUint aCharToReplace, TUint aReplacement) |
|
260 |
{ |
|
261 |
TInt strLen=aString.Length(); |
|
262 |
for (TInt i=0; i < strLen; i++) |
|
263 |
{ |
|
264 |
if (aString[i] == aCharToReplace) |
|
265 |
{ |
|
266 |
aString[i] = aReplacement; |
|
267 |
} |
|
268 |
} |
|
269 |
} |
|
270 |
||
271 |
EXPORT_C void PodcastUtils::EnsureProperPathName(TFileName &aPath) |
|
272 |
{ |
|
273 |
// from the SDK: The following characters cannot occur in the path: < >: " / |* |
|
274 |
||
275 |
ReplaceChar(aPath, '/', '_'); // better not to add \\ in case we have multiple / |
|
276 |
ReplaceChar(aPath, ':', '_'); |
|
277 |
ReplaceChar(aPath, '?', '_'); |
|
278 |
ReplaceChar(aPath, '|', '_'); |
|
279 |
ReplaceChar(aPath, '*', '_'); |
|
280 |
ReplaceChar(aPath, '<', '_'); |
|
281 |
ReplaceChar(aPath, '>', '_'); |
|
282 |
ReplaceChar(aPath, '"', '_'); |
|
283 |
||
284 |
//buf.Append(_L("\\")); |
|
285 |
} |
|
286 |
||
287 |
EXPORT_C void PodcastUtils::FileNameFromUrl(const TDesC& aUrl, TFileName &aFileName) |
|
288 |
{ |
|
289 |
TInt pos = aUrl.LocateReverse('/'); |
|
290 |
||
291 |
if (pos != KErrNotFound) |
|
292 |
{ |
|
293 |
TPtrC str = aUrl.Mid(pos+1); |
|
294 |
pos = str.Locate('?'); |
|
295 |
if (pos != KErrNotFound) |
|
296 |
{ |
|
297 |
aFileName.Copy(str.Left(pos)); |
|
298 |
} |
|
299 |
else |
|
300 |
{ |
|
301 |
aFileName.Copy(str); |
|
302 |
} |
|
303 |
} |
|
304 |
DP2("FileNameFromUrl in: %S, out: %S", &aUrl, &aFileName); |
|
305 |
} |
|
306 |
||
307 |
EXPORT_C void PodcastUtils::SQLEncode(TDes &aString) |
|
308 |
{ |
|
309 |
ReplaceString(aString, _L("\""), _L("\"\"")); |
|
310 |
//ReplaceString(aString, _L("'"), _L("''")); |
|
311 |
} |
|
312 |
||
313 |
EXPORT_C void PodcastUtils::XMLEncode(TDes &aString) |
|
314 |
{ |
|
315 |
ReplaceString(aString, _L("\""), _L(""")); |
|
316 |
ReplaceString(aString, _L("<"), _L("<")); |
|
317 |
ReplaceString(aString, _L(">"), _L(">")); |
|
318 |
ReplaceString(aString, _L("&"), _L("&")); |
|
319 |
} |
|
60 | 320 |
|
321 |
EXPORT_C TBool PodcastUtils::IsVideoShow(TDesC &aUrl) |
|
322 |
{ |
|
323 |
if (aUrl.Find(KVideoFormat1) != KErrNotFound || |
|
324 |
aUrl.Find(KVideoFormat2) != KErrNotFound || |
|
246
140a404c6b53
Fix for bug 3626 - show filenames are now generated from UIDs
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
240
diff
changeset
|
325 |
aUrl.Find(KVideoFormat3) != KErrNotFound || |
140a404c6b53
Fix for bug 3626 - show filenames are now generated from UIDs
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
240
diff
changeset
|
326 |
aUrl.Find(KVideoFormat4) != KErrNotFound) |
60 | 327 |
{ |
328 |
return ETrue; |
|
329 |
} |
|
330 |
||
331 |
return EFalse; |
|
332 |
} |