engine/src/PodcastUtils.cpp
changeset 2 29cda98b007e
child 15 93d9f66bf50b
equal deleted inserted replaced
1:5f8e5adbbed9 2:29cda98b007e
       
     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, "&amp;");
       
   114 	_LIT(KQuot, "&quot;");
       
   115 	_LIT(KNbsp, "&nbsp;");
       
   116 	_LIT(KCopy, "&copy;");
       
   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 
       
   129 EXPORT_C void PodcastUtils::ReplaceString(TDes & aString, const TDesC& aStringToReplace, const TDesC& aReplacement)
       
   130 	{
       
   131 	TInt pos=aString.Find(aStringToReplace);
       
   132 	TInt offset = 0;
       
   133 	while (pos != KErrNotFound)
       
   134 		{
       
   135 		aString.Replace(offset+pos, aStringToReplace.Length(), aReplacement);
       
   136 		offset += pos + aStringToReplace.Length()+1;
       
   137 		if (offset > aString.Length())
       
   138 			{
       
   139 			return;
       
   140 			}
       
   141 
       
   142 		pos=aString.Mid(offset).Find(aStringToReplace);
       
   143 		}
       
   144 	}
       
   145 
       
   146 EXPORT_C void PodcastUtils::ReplaceChar(TDes & aString, TUint aCharToReplace, TUint aReplacement)
       
   147 	{
       
   148 	TInt strLen=aString.Length();
       
   149 	for (TInt i=0; i < strLen; i++)
       
   150 		{
       
   151 		if (aString[i] == aCharToReplace)
       
   152 			{
       
   153 			aString[i] = aReplacement;
       
   154 			}
       
   155 		}
       
   156 	}
       
   157 
       
   158 EXPORT_C void PodcastUtils::EnsureProperPathName(TFileName &aPath)
       
   159 	{
       
   160 	// from the SDK: The following characters cannot occur in the path: < >: " / |*
       
   161 	
       
   162 	ReplaceChar(aPath, '/', '_'); // better not to add \\ in case we have multiple /
       
   163 	ReplaceChar(aPath, ':', '_');
       
   164 	ReplaceChar(aPath, '?', '_');
       
   165 	ReplaceChar(aPath, '|', '_');
       
   166 	ReplaceChar(aPath, '*', '_');
       
   167 	ReplaceChar(aPath, '<', '_');
       
   168 	ReplaceChar(aPath, '>', '_');
       
   169 	ReplaceChar(aPath, '"', '_');
       
   170 
       
   171 	//buf.Append(_L("\\"));
       
   172 	}
       
   173 
       
   174 EXPORT_C void PodcastUtils::FileNameFromUrl(const TDesC& aUrl, TFileName &aFileName)
       
   175 	{
       
   176 	TInt pos = aUrl.LocateReverse('/');
       
   177 	
       
   178 	if (pos != KErrNotFound) 
       
   179 		{	
       
   180 		TPtrC str = aUrl.Mid(pos+1);
       
   181 		pos = str.Locate('?');
       
   182 		if (pos != KErrNotFound) 
       
   183 			{
       
   184 			aFileName.Copy(str.Left(pos));
       
   185 			} 
       
   186 		else 
       
   187 			{
       
   188 			aFileName.Copy(str);
       
   189 			}
       
   190 		}
       
   191 	DP2("FileNameFromUrl in: %S, out: %S", &aUrl, &aFileName);
       
   192 	}
       
   193 
       
   194 EXPORT_C void PodcastUtils::SQLEncode(TDes &aString)
       
   195 	{
       
   196 	ReplaceString(aString, _L("\""), _L("\"\""));
       
   197 	//ReplaceString(aString, _L("'"), _L("''"));	
       
   198 	}
       
   199 
       
   200 EXPORT_C void PodcastUtils::XMLEncode(TDes &aString)
       
   201 	{
       
   202 	ReplaceString(aString, _L("\""), _L("&quot;"));	
       
   203 	ReplaceString(aString, _L("<"), _L("&lt;"));
       
   204 	ReplaceString(aString, _L(">"), _L("&gt;"));
       
   205 	ReplaceString(aString, _L("&"), _L("&amp;"));
       
   206 	}