author | teknolog |
Thu, 25 Feb 2010 21:08:38 +0000 | |
changeset 15 | 93d9f66bf50b |
parent 2 | 29cda98b007e |
child 18 | cda6e6257514 |
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 |
{ |
|
71 |
#ifdef UIQ |
|
72 |
_LIT(KLineBreak, "\r\n"); |
|
73 |
#else |
|
74 |
const TChar KLineBreak(CEditableText::ELineBreak); |
|
75 |
#endif |
|
76 |
_LIT(KNewLine, "\n"); |
|
77 |
ReplaceString(str, KNewLine, KNullDesC); |
|
78 |
||
79 |
// DP2("CleanHtml %d, %S", str.Length(), &str); |
|
80 |
TInt startPos = str.Locate('<'); |
|
81 |
TInt endPos = str.Locate('>'); |
|
82 |
//DP3("length: %d, startPos: %d, endPos: %d", str.Length(), startPos, endPos); |
|
83 |
HBufC* tmpBuf = HBufC::NewLC(KMaxDescriptionLength); |
|
84 |
TPtr tmp(tmpBuf->Des()); |
|
85 |
while (startPos != KErrNotFound && endPos != KErrNotFound && endPos > startPos) { |
|
86 |
//DP1("Cleaning out %S", &str.Mid(startPos, endPos-startPos+1)); |
|
87 |
tmp.Copy(str.Left(startPos)); |
|
88 |
TPtrC ptr=str.Mid(startPos, endPos-startPos+1); |
|
89 |
_LIT(KHtmlBr, "<br>"); |
|
90 |
_LIT(KHtmlBr2, "<br />"); |
|
91 |
_LIT(KHtmlBr3, "<br/>"); |
|
92 |
_LIT(KHtmlP, "<p>"); |
|
93 |
if (ptr.CompareF(KHtmlBr)== 0 || ptr.CompareF(KHtmlBr2)== 0 || ptr.CompareF(KHtmlBr3)== 0) |
|
94 |
{ |
|
95 |
tmp.Append(KLineBreak); |
|
96 |
} |
|
97 |
else if (ptr.CompareF(KHtmlP) == 0) |
|
98 |
{ |
|
99 |
tmp.Append(KLineBreak); |
|
100 |
tmp.Append(KLineBreak); |
|
101 |
} |
|
102 |
||
103 |
if (str.Length() > endPos+1) { |
|
104 |
tmp.Append(str.Mid(endPos+1)); |
|
105 |
} |
|
106 |
||
107 |
str.Copy(tmp); |
|
108 |
startPos = str.Locate('<'); |
|
109 |
endPos = str.Locate('>'); |
|
110 |
} |
|
111 |
||
112 |
str.Trim(); |
|
113 |
_LIT(KAmp, "&"); |
|
114 |
_LIT(KQuot, """); |
|
115 |
_LIT(KNbsp, " "); |
|
116 |
_LIT(KCopy, "©"); |
|
117 |
_LIT(KCopyReplacement, "(c)"); |
|
118 |
if(str.Locate('&') != KErrNotFound) { |
|
119 |
ReplaceString(str, KAmp, KNullDesC); |
|
120 |
ReplaceString(str, KQuot, KNullDesC); |
|
121 |
ReplaceString(str, KNbsp, KNullDesC); |
|
122 |
ReplaceString(str, KCopy, KCopyReplacement); |
|
123 |
} |
|
124 |
ReplaceChar(str, '"', '\''); |
|
125 |
||
126 |
CleanupStack::PopAndDestroy(tmpBuf); |
|
127 |
} |
|
128 |
||
15
93d9f66bf50b
Cleaning description better for second line in search results
teknolog
parents:
2
diff
changeset
|
129 |
EXPORT_C void PodcastUtils::RemoveAllFormatting(TDes & aString) |
93d9f66bf50b
Cleaning description better for second line in search results
teknolog
parents:
2
diff
changeset
|
130 |
{ |
93d9f66bf50b
Cleaning description better for second line in search results
teknolog
parents:
2
diff
changeset
|
131 |
// 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
|
132 |
ReplaceString(aString,_L("\r\n"), _L(" ")); |
93d9f66bf50b
Cleaning description better for second line in search results
teknolog
parents:
2
diff
changeset
|
133 |
|
93d9f66bf50b
Cleaning description better for second line in search results
teknolog
parents:
2
diff
changeset
|
134 |
ReplaceString(aString,_L("\n"), _L(" ")); |
93d9f66bf50b
Cleaning description better for second line in search results
teknolog
parents:
2
diff
changeset
|
135 |
ReplaceString(aString,_L("\r"), _L(" ")); |
93d9f66bf50b
Cleaning description better for second line in search results
teknolog
parents:
2
diff
changeset
|
136 |
|
93d9f66bf50b
Cleaning description better for second line in search results
teknolog
parents:
2
diff
changeset
|
137 |
} |
93d9f66bf50b
Cleaning description better for second line in search results
teknolog
parents:
2
diff
changeset
|
138 |
|
2 | 139 |
EXPORT_C void PodcastUtils::ReplaceString(TDes & aString, const TDesC& aStringToReplace, const TDesC& aReplacement) |
140 |
{ |
|
141 |
TInt pos=aString.Find(aStringToReplace); |
|
142 |
TInt offset = 0; |
|
143 |
while (pos != KErrNotFound) |
|
144 |
{ |
|
145 |
aString.Replace(offset+pos, aStringToReplace.Length(), aReplacement); |
|
146 |
offset += pos + aStringToReplace.Length()+1; |
|
147 |
if (offset > aString.Length()) |
|
148 |
{ |
|
149 |
return; |
|
150 |
} |
|
151 |
||
152 |
pos=aString.Mid(offset).Find(aStringToReplace); |
|
153 |
} |
|
154 |
} |
|
155 |
||
156 |
EXPORT_C void PodcastUtils::ReplaceChar(TDes & aString, TUint aCharToReplace, TUint aReplacement) |
|
157 |
{ |
|
158 |
TInt strLen=aString.Length(); |
|
159 |
for (TInt i=0; i < strLen; i++) |
|
160 |
{ |
|
161 |
if (aString[i] == aCharToReplace) |
|
162 |
{ |
|
163 |
aString[i] = aReplacement; |
|
164 |
} |
|
165 |
} |
|
166 |
} |
|
167 |
||
168 |
EXPORT_C void PodcastUtils::EnsureProperPathName(TFileName &aPath) |
|
169 |
{ |
|
170 |
// from the SDK: The following characters cannot occur in the path: < >: " / |* |
|
171 |
||
172 |
ReplaceChar(aPath, '/', '_'); // better not to add \\ in case we have multiple / |
|
173 |
ReplaceChar(aPath, ':', '_'); |
|
174 |
ReplaceChar(aPath, '?', '_'); |
|
175 |
ReplaceChar(aPath, '|', '_'); |
|
176 |
ReplaceChar(aPath, '*', '_'); |
|
177 |
ReplaceChar(aPath, '<', '_'); |
|
178 |
ReplaceChar(aPath, '>', '_'); |
|
179 |
ReplaceChar(aPath, '"', '_'); |
|
180 |
||
181 |
//buf.Append(_L("\\")); |
|
182 |
} |
|
183 |
||
184 |
EXPORT_C void PodcastUtils::FileNameFromUrl(const TDesC& aUrl, TFileName &aFileName) |
|
185 |
{ |
|
186 |
TInt pos = aUrl.LocateReverse('/'); |
|
187 |
||
188 |
if (pos != KErrNotFound) |
|
189 |
{ |
|
190 |
TPtrC str = aUrl.Mid(pos+1); |
|
191 |
pos = str.Locate('?'); |
|
192 |
if (pos != KErrNotFound) |
|
193 |
{ |
|
194 |
aFileName.Copy(str.Left(pos)); |
|
195 |
} |
|
196 |
else |
|
197 |
{ |
|
198 |
aFileName.Copy(str); |
|
199 |
} |
|
200 |
} |
|
201 |
DP2("FileNameFromUrl in: %S, out: %S", &aUrl, &aFileName); |
|
202 |
} |
|
203 |
||
204 |
EXPORT_C void PodcastUtils::SQLEncode(TDes &aString) |
|
205 |
{ |
|
206 |
ReplaceString(aString, _L("\""), _L("\"\"")); |
|
207 |
//ReplaceString(aString, _L("'"), _L("''")); |
|
208 |
} |
|
209 |
||
210 |
EXPORT_C void PodcastUtils::XMLEncode(TDes &aString) |
|
211 |
{ |
|
212 |
ReplaceString(aString, _L("\""), _L(""")); |
|
213 |
ReplaceString(aString, _L("<"), _L("<")); |
|
214 |
ReplaceString(aString, _L(">"), _L(">")); |
|
215 |
ReplaceString(aString, _L("&"), _L("&")); |
|
216 |
} |