|
S60 3rd Edition SDK for Symbian OS Example Applications Guide |
00001 /* 00002 * ============================================================================ 00003 * Name : BrCtlSampleAppLinkResolver.cpp 00004 * Part of : BrCtlSampleApp 00005 * Interface : Browser Control API 00006 * Description : Observer interface which handles resolving embedded links 00007 * Version : 00008 * 00009 * Copyright (c) 2005 Nokia Corporation. 00010 * This material, including documentation and any related 00011 * computer programs, is protected by copyright controlled by 00012 * Nokia Corporation. 00013 * ============================================================================ 00014 */ 00015 00016 // INCLUDE FILES 00017 #include <apmrec.h> 00018 #include <apgcli.h> 00019 #include <eikappui.h> 00020 #include <eikenv.h> 00021 #include "BrCtlSampleAppLinkResolver.h" 00022 00023 // ================= MEMBER FUNCTIONS ======================= 00024 00025 // --------------------------------------------------------- 00026 // CBrCtlSampleAppLinkResolver::ResolveEmbeddedLinkL 00027 // --------------------------------------------------------- 00028 // 00029 TBool 00030 CBrCtlSampleAppLinkResolver::ResolveEmbeddedLinkL(const TDesC& aEmbeddedUrl, 00031 const TDesC& /*aCurrentUrl*/, 00032 TBrCtlLoadContentType /*aLoadContentType*/, 00033 MBrCtlLinkContent& aEmbeddedLinkContent) 00034 { 00035 if (IsFileScheme(aEmbeddedUrl)) 00036 { 00037 GetFileNameL(aEmbeddedUrl); 00038 HBufC8* buf = ReadFileLC(*iFileName); 00039 HBufC* contentType = NULL; 00040 TPtrC p(NULL, 0); 00041 contentType = RecognizeLC(*iFileName, *buf); 00042 aEmbeddedLinkContent.HandleResolveComplete(*contentType, p, buf); 00043 CleanupStack::PopAndDestroy(2); // contentType, buf 00044 return ETrue; 00045 } 00046 return EFalse; 00047 } 00048 00049 // --------------------------------------------------------- 00050 // CBrCtlSampleAppLinkResolver::ResolveLinkL 00051 // --------------------------------------------------------- 00052 // 00053 TBool 00054 CBrCtlSampleAppLinkResolver::ResolveLinkL(const TDesC& aUrl, 00055 const TDesC& /*aCurrentUrl*/, 00056 MBrCtlLinkContent& aBrCtlLinkContent) 00057 { 00058 if (IsFileScheme(aUrl)) 00059 { 00060 GetFileNameL(aUrl); 00061 HBufC8* buf = ReadFileLC(*iFileName); 00062 HBufC* contentType = NULL; 00063 TPtrC p(NULL, 0); 00064 contentType = RecognizeLC(*iFileName, *buf); 00065 aBrCtlLinkContent.HandleResolveComplete(*contentType, p, buf); 00066 CleanupStack::PopAndDestroy(2); // contentType, buf 00067 return ETrue; 00068 } 00069 return EFalse; 00070 } 00071 00072 // --------------------------------------------------------- 00073 // CBrCtlSampleAppLinkResolver::CancelAll 00074 // --------------------------------------------------------- 00075 // 00076 void 00077 CBrCtlSampleAppLinkResolver::CancelAll() 00078 { 00079 } 00080 00081 00082 // --------------------------------------------------------- 00083 // CBrCtlSampleAppLinkResolver::ConstructL 00084 // --------------------------------------------------------- 00085 // 00086 void 00087 CBrCtlSampleAppLinkResolver::ConstructL() 00088 { 00089 } 00090 00091 // --------------------------------------------------------- 00092 // CBrCtlSampleAppLinkResolver::~CBrCtlSampleAppLinkResolver 00093 // --------------------------------------------------------- 00094 // 00095 CBrCtlSampleAppLinkResolver::~CBrCtlSampleAppLinkResolver() 00096 { 00097 delete iFileName; 00098 } 00099 00100 00101 // --------------------------------------------------------- 00102 // CBrCtlSampleAppLinkResolver::NewL 00103 // --------------------------------------------------------- 00104 // 00105 CBrCtlSampleAppLinkResolver* 00106 CBrCtlSampleAppLinkResolver::NewL() 00107 { 00108 CBrCtlSampleAppLinkResolver* self = new(ELeave)CBrCtlSampleAppLinkResolver; 00109 CleanupStack::PushL(self); 00110 self->ConstructL(); 00111 CleanupStack::Pop(); 00112 return self; 00113 } 00114 00115 // --------------------------------------------------------- 00116 // CBrCtlSampleAppLinkResolver::IsFileScheme 00117 // --------------------------------------------------------- 00118 // 00119 TBool 00120 CBrCtlSampleAppLinkResolver::IsFileScheme(const TDesC& aFileName) 00121 { 00122 _LIT(KFileScheme, "file://"); 00123 if (aFileName.Length() > 0 && aFileName.Ptr() != NULL) 00124 { 00125 if (aFileName.FindF(KFileScheme) == 0) 00126 { 00127 return ETrue; 00128 } 00129 } 00130 return EFalse; 00131 } 00132 00133 // --------------------------------------------------------- 00134 // CBrCtlSampleAppLinkResolver::ReadFile 00135 // --------------------------------------------------------- 00136 // 00137 HBufC8* 00138 CBrCtlSampleAppLinkResolver::ReadFileLC(const TDesC& aFileName) 00139 { 00140 RFs rfs; 00141 RFile file; 00142 User::LeaveIfError(rfs.Connect()); 00143 CleanupClosePushL(rfs); 00144 User::LeaveIfError(file.Open(rfs, aFileName, EFileRead)); 00145 CleanupClosePushL(file); 00146 TInt size; 00147 User::LeaveIfError(file.Size(size)); 00148 HBufC8* buf = HBufC8::NewLC(size); 00149 TPtr8 bufPtr(buf->Des()); 00150 User::LeaveIfError(file.Read(bufPtr)); 00151 CleanupStack::Pop(); // buf 00152 CleanupStack::PopAndDestroy(2); // file, rfs 00153 CleanupStack::PushL(buf); 00154 return buf; 00155 } 00156 00157 // --------------------------------------------------------- 00158 // CBrCtlSampleAppLinkResolver::RecognizeL 00159 // --------------------------------------------------------- 00160 // 00161 HBufC* 00162 CBrCtlSampleAppLinkResolver::RecognizeLC(const TDesC& aFileName, const TDesC8& aData) 00163 { 00164 TDataRecognitionResult dataType; 00165 RApaLsSession apaSession; 00166 TInt ret; 00167 HBufC* contentTypeString = NULL; 00168 00169 User::LeaveIfError(apaSession.Connect()); 00170 // Ask the application architecture to find the file type 00171 ret = apaSession.RecognizeData(aFileName, aData, dataType); 00172 apaSession.Close(); 00173 00174 if (ret == KErrNone && 00175 (dataType.iConfidence == CApaDataRecognizerType::ECertain) || 00176 (dataType.iConfidence == CApaDataRecognizerType::EProbable)) 00177 { 00178 // If the file type was found, try to match it to a known file type 00179 TPtrC8 mimeTypePtr = dataType.iDataType.Des8(); 00180 TInt len = mimeTypePtr.Length() + 1; 00181 contentTypeString = HBufC::NewL(len); 00182 contentTypeString->Des().Copy(mimeTypePtr); 00183 contentTypeString->Des().ZeroTerminate(); 00184 } 00185 CleanupStack::PushL(contentTypeString); 00186 return contentTypeString; 00187 } 00188 00189 // ----------------------------------------------------------------------------- 00190 // CBrCtlSampleAppLinkResolver::GetFileNameL 00191 // Translate the file name from a URL to a valid file name in the system. 00192 // ----------------------------------------------------------------------------- 00193 // 00194 TBool 00195 CBrCtlSampleAppLinkResolver::GetFileNameL(const TDesC& aFileName) 00196 { 00197 // This function accepts URLs in the following format: 00198 // file://filename.xxx 00199 // file:///filename.xxx 00200 // file://c:/filename.xxx 00201 // file:///c:/filename.xxx 00202 // 00203 _LIT(KFileScheme, "file://"); 00204 _LIT(KDefaultDrivePath, "C:\\"); 00205 _LIT(KPathChar, "\\"); 00206 00207 TInt count; 00208 TInt index = 0; 00209 TBool drvLetter = EFalse; 00210 TUint16 c; 00211 00212 // Verify the file scheme 00213 TPtrC urlPtr(aFileName); 00214 if (urlPtr.FindF(KFileScheme) != 0) 00215 { 00216 return EFalse; 00217 } 00218 urlPtr.Set(urlPtr.Mid(KFileScheme().Length())); 00219 00220 // make sure there are enough characters in the filename before 00221 // trying to check them 00222 count = urlPtr.Length(); 00223 if(count == 0) 00224 { 00225 return EFalse; // no filename, so can't look at urlPtr[0] 00226 } 00227 00228 // Skip the first '/' if there is one 00229 if (urlPtr[0] == '/') 00230 { 00231 urlPtr.Set(urlPtr.Mid(1)); 00232 } 00233 count = urlPtr.Length(); 00234 00235 // Is there a drive letter? 00236 if(count > 1) 00237 { 00238 // can check for drive letter 00239 if (urlPtr[1 + index] == ':') 00240 { 00241 drvLetter = ETrue; 00242 } 00243 } 00244 if(drvLetter == EFalse) 00245 { 00246 // 3 additional characters for the string "c:\" 00247 count = urlPtr.Length() + 3; 00248 } 00249 iFileName = HBufC::NewL(count); 00250 if (!drvLetter) 00251 { 00252 iFileName->Des().Append(KDefaultDrivePath); 00253 } 00254 00255 TBool fragment(EFalse); 00256 // Convert relative path containing /./ and /../ to absolute path 00257 for (; index < urlPtr.Length() && !fragment; index ++) 00258 { 00259 switch(urlPtr[index]) 00260 { 00261 case '#': //Check if there is a fragment '#' 00262 { 00263 fragment = ETrue; 00264 continue; // Just stop there 00265 } 00266 00267 case '/': 00268 { 00269 iFileName->Des().Append(KPathChar); 00270 break; 00271 } 00272 case '.': 00273 { 00274 if (index > 1 && urlPtr[index - 1] == '/') 00275 { 00276 if (index < count - 1 && urlPtr[index + 1] == '/') 00277 { 00278 index ++; // skip ./ 00279 break; 00280 } 00281 if (index > 2 && index < count - 3 && 00282 urlPtr[index + 1] == '.' && urlPtr[index + 2] == '/') 00283 { 00284 TInt i = index - 2; 00285 00286 for (; i > 0 && urlPtr[i] != '/'; i--) {} // skip /../ 00287 00288 iFileName->Des().SetLength(iFileName->Des().Length() - (index - i)); 00289 index += 2; 00290 break; 00291 } 00292 } 00293 } 00294 // no break 00295 //lint -fallthrough 00296 00297 default: 00298 { 00299 c = urlPtr[index]; 00300 iFileName->Des().Append(&c, 1); 00301 break; 00302 } 00303 } // end of switch 00304 } 00305 return ETrue; 00306 } 00307 00308 // End of File 00309
|
© Nokia 2006 |