1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/rdf/datasource/src/nsFileSystemDataSource.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,1329 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +/* 1.10 + Implementation for a file system RDF data store. 1.11 + */ 1.12 + 1.13 +#include "nsFileSystemDataSource.h" 1.14 + 1.15 +#include <ctype.h> // for toupper() 1.16 +#include <stdio.h> 1.17 +#include "nsArrayEnumerator.h" 1.18 +#include "nsCOMArray.h" 1.19 +#include "nsISupportsArray.h" 1.20 +#include "nsIRDFDataSource.h" 1.21 +#include "nsIRDFObserver.h" 1.22 +#include "nsIServiceManager.h" 1.23 +#include "nsXPIDLString.h" 1.24 +#include "nsRDFCID.h" 1.25 +#include "rdfutil.h" 1.26 +#include "rdf.h" 1.27 +#include "nsEnumeratorUtils.h" 1.28 +#include "nsIURL.h" 1.29 +#include "nsIFileURL.h" 1.30 +#include "nsNetUtil.h" 1.31 +#include "nsIChannel.h" 1.32 +#include "nsIFile.h" 1.33 +#include "nsEscape.h" 1.34 +#include "nsCRTGlue.h" 1.35 +#include "nsAutoPtr.h" 1.36 + 1.37 +#ifdef XP_WIN 1.38 +#include "windef.h" 1.39 +#include "winbase.h" 1.40 +#include "nsILineInputStream.h" 1.41 +#include "nsDirectoryServiceDefs.h" 1.42 +#endif 1.43 + 1.44 +#define NS_MOZICON_SCHEME "moz-icon:" 1.45 + 1.46 +static const char kFileProtocol[] = "file://"; 1.47 + 1.48 +bool 1.49 +FileSystemDataSource::isFileURI(nsIRDFResource *r) 1.50 +{ 1.51 + bool isFileURIFlag = false; 1.52 + const char *uri = nullptr; 1.53 + 1.54 + r->GetValueConst(&uri); 1.55 + if ((uri) && (!strncmp(uri, kFileProtocol, sizeof(kFileProtocol) - 1))) 1.56 + { 1.57 + // XXX HACK HACK HACK 1.58 + if (!strchr(uri, '#')) 1.59 + { 1.60 + isFileURIFlag = true; 1.61 + } 1.62 + } 1.63 + return(isFileURIFlag); 1.64 +} 1.65 + 1.66 + 1.67 + 1.68 +bool 1.69 +FileSystemDataSource::isDirURI(nsIRDFResource* source) 1.70 +{ 1.71 + nsresult rv; 1.72 + const char *uri = nullptr; 1.73 + 1.74 + rv = source->GetValueConst(&uri); 1.75 + if (NS_FAILED(rv)) return(false); 1.76 + 1.77 + nsCOMPtr<nsIFile> aDir; 1.78 + 1.79 + rv = NS_GetFileFromURLSpec(nsDependentCString(uri), getter_AddRefs(aDir)); 1.80 + if (NS_FAILED(rv)) return(false); 1.81 + 1.82 + bool isDirFlag = false; 1.83 + 1.84 + rv = aDir->IsDirectory(&isDirFlag); 1.85 + if (NS_FAILED(rv)) return(false); 1.86 + 1.87 + return(isDirFlag); 1.88 +} 1.89 + 1.90 + 1.91 +nsresult 1.92 +FileSystemDataSource::Init() 1.93 +{ 1.94 + nsresult rv; 1.95 + 1.96 + mRDFService = do_GetService("@mozilla.org/rdf/rdf-service;1"); 1.97 + NS_ENSURE_TRUE(mRDFService, NS_ERROR_FAILURE); 1.98 + 1.99 + rv = mRDFService->GetResource(NS_LITERAL_CSTRING("NC:FilesRoot"), 1.100 + getter_AddRefs(mNC_FileSystemRoot)); 1.101 + nsresult tmp = mRDFService->GetResource(NS_LITERAL_CSTRING(NC_NAMESPACE_URI "child"), 1.102 + getter_AddRefs(mNC_Child)); 1.103 + if (NS_FAILED(tmp)) { 1.104 + rv = tmp; 1.105 + } 1.106 + tmp = mRDFService->GetResource(NS_LITERAL_CSTRING(NC_NAMESPACE_URI "Name"), 1.107 + getter_AddRefs(mNC_Name)); 1.108 + if (NS_FAILED(tmp)) { 1.109 + rv = tmp; 1.110 + } 1.111 + tmp = mRDFService->GetResource(NS_LITERAL_CSTRING(NC_NAMESPACE_URI "URL"), 1.112 + getter_AddRefs(mNC_URL)); 1.113 + if (NS_FAILED(tmp)) { 1.114 + rv = tmp; 1.115 + } 1.116 + tmp = mRDFService->GetResource(NS_LITERAL_CSTRING(NC_NAMESPACE_URI "Icon"), 1.117 + getter_AddRefs(mNC_Icon)); 1.118 + if (NS_FAILED(tmp)) { 1.119 + rv = tmp; 1.120 + } 1.121 + tmp = mRDFService->GetResource(NS_LITERAL_CSTRING(NC_NAMESPACE_URI "Content-Length"), 1.122 + getter_AddRefs(mNC_Length)); 1.123 + if (NS_FAILED(tmp)) { 1.124 + rv = tmp; 1.125 + } 1.126 + tmp = mRDFService->GetResource(NS_LITERAL_CSTRING(NC_NAMESPACE_URI "IsDirectory"), 1.127 + getter_AddRefs(mNC_IsDirectory)); 1.128 + if (NS_FAILED(tmp)) { 1.129 + rv = tmp; 1.130 + } 1.131 + tmp = mRDFService->GetResource(NS_LITERAL_CSTRING(WEB_NAMESPACE_URI "LastModifiedDate"), 1.132 + getter_AddRefs(mWEB_LastMod)); 1.133 + if (NS_FAILED(tmp)) { 1.134 + rv = tmp; 1.135 + } 1.136 + tmp = mRDFService->GetResource(NS_LITERAL_CSTRING(NC_NAMESPACE_URI "FileSystemObject"), 1.137 + getter_AddRefs(mNC_FileSystemObject)); 1.138 + if (NS_FAILED(tmp)) { 1.139 + rv = tmp; 1.140 + } 1.141 + tmp = mRDFService->GetResource(NS_LITERAL_CSTRING(NC_NAMESPACE_URI "pulse"), 1.142 + getter_AddRefs(mNC_pulse)); 1.143 + if (NS_FAILED(tmp)) { 1.144 + rv = tmp; 1.145 + } 1.146 + tmp = mRDFService->GetResource(NS_LITERAL_CSTRING(RDF_NAMESPACE_URI "instanceOf"), 1.147 + getter_AddRefs(mRDF_InstanceOf)); 1.148 + if (NS_FAILED(tmp)) { 1.149 + rv = tmp; 1.150 + } 1.151 + tmp = mRDFService->GetResource(NS_LITERAL_CSTRING(RDF_NAMESPACE_URI "type"), 1.152 + getter_AddRefs(mRDF_type)); 1.153 + 1.154 + static const char16_t kTrue[] = {'t','r','u','e','\0'}; 1.155 + static const char16_t kFalse[] = {'f','a','l','s','e','\0'}; 1.156 + 1.157 + tmp = mRDFService->GetLiteral(kTrue, getter_AddRefs(mLiteralTrue)); 1.158 + if (NS_FAILED(tmp)) { 1.159 + rv = tmp; 1.160 + } 1.161 + tmp = mRDFService->GetLiteral(kFalse, getter_AddRefs(mLiteralFalse)); 1.162 + if (NS_FAILED(tmp)) { 1.163 + rv = tmp; 1.164 + } 1.165 + NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE); 1.166 + 1.167 +#ifdef USE_NC_EXTENSION 1.168 + rv = mRDFService->GetResource(NS_LITERAL_CSTRING(NC_NAMESPACE_URI "extension"), 1.169 + getter_AddRefs(mNC_extension)); 1.170 + NS_ENSURE_SUCCESS(rv, rv); 1.171 +#endif 1.172 + 1.173 +#ifdef XP_WIN 1.174 + rv = mRDFService->GetResource(NS_LITERAL_CSTRING(NC_NAMESPACE_URI "IEFavorite"), 1.175 + getter_AddRefs(mNC_IEFavoriteObject)); 1.176 + tmp = mRDFService->GetResource(NS_LITERAL_CSTRING(NC_NAMESPACE_URI "IEFavoriteFolder"), 1.177 + getter_AddRefs(mNC_IEFavoriteFolder)); 1.178 + if (NS_FAILED(tmp)) { 1.179 + rv = tmp; 1.180 + } 1.181 + NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE); 1.182 + 1.183 + nsCOMPtr<nsIFile> file; 1.184 + NS_GetSpecialDirectory(NS_WIN_FAVORITES_DIR, getter_AddRefs(file)); 1.185 + if (file) 1.186 + { 1.187 + nsCOMPtr<nsIURI> furi; 1.188 + NS_NewFileURI(getter_AddRefs(furi), file); 1.189 + NS_ENSURE_TRUE(furi, NS_ERROR_FAILURE); 1.190 + 1.191 + file->GetNativePath(ieFavoritesDir); 1.192 + } 1.193 +#endif 1.194 + 1.195 + return NS_OK; 1.196 +} 1.197 + 1.198 +//static 1.199 +nsresult 1.200 +FileSystemDataSource::Create(nsISupports* aOuter, const nsIID& aIID, void **aResult) 1.201 +{ 1.202 + NS_ENSURE_NO_AGGREGATION(aOuter); 1.203 + 1.204 + nsRefPtr<FileSystemDataSource> self = new FileSystemDataSource(); 1.205 + if (!self) 1.206 + return NS_ERROR_OUT_OF_MEMORY; 1.207 + 1.208 + nsresult rv = self->Init(); 1.209 + NS_ENSURE_SUCCESS(rv, rv); 1.210 + 1.211 + return self->QueryInterface(aIID, aResult); 1.212 +} 1.213 + 1.214 +NS_IMPL_ISUPPORTS(FileSystemDataSource, nsIRDFDataSource) 1.215 + 1.216 +NS_IMETHODIMP 1.217 +FileSystemDataSource::GetURI(char **uri) 1.218 +{ 1.219 + NS_PRECONDITION(uri != nullptr, "null ptr"); 1.220 + if (! uri) 1.221 + return NS_ERROR_NULL_POINTER; 1.222 + 1.223 + if ((*uri = NS_strdup("rdf:files")) == nullptr) 1.224 + return NS_ERROR_OUT_OF_MEMORY; 1.225 + 1.226 + return NS_OK; 1.227 +} 1.228 + 1.229 + 1.230 + 1.231 +NS_IMETHODIMP 1.232 +FileSystemDataSource::GetSource(nsIRDFResource* property, 1.233 + nsIRDFNode* target, 1.234 + bool tv, 1.235 + nsIRDFResource** source /* out */) 1.236 +{ 1.237 + NS_PRECONDITION(property != nullptr, "null ptr"); 1.238 + if (! property) 1.239 + return NS_ERROR_NULL_POINTER; 1.240 + 1.241 + NS_PRECONDITION(target != nullptr, "null ptr"); 1.242 + if (! target) 1.243 + return NS_ERROR_NULL_POINTER; 1.244 + 1.245 + NS_PRECONDITION(source != nullptr, "null ptr"); 1.246 + if (! source) 1.247 + return NS_ERROR_NULL_POINTER; 1.248 + 1.249 + *source = nullptr; 1.250 + return NS_RDF_NO_VALUE; 1.251 +} 1.252 + 1.253 + 1.254 + 1.255 +NS_IMETHODIMP 1.256 +FileSystemDataSource::GetSources(nsIRDFResource *property, 1.257 + nsIRDFNode *target, 1.258 + bool tv, 1.259 + nsISimpleEnumerator **sources /* out */) 1.260 +{ 1.261 +// NS_NOTYETIMPLEMENTED("write me"); 1.262 + return NS_ERROR_NOT_IMPLEMENTED; 1.263 +} 1.264 + 1.265 + 1.266 + 1.267 +NS_IMETHODIMP 1.268 +FileSystemDataSource::GetTarget(nsIRDFResource *source, 1.269 + nsIRDFResource *property, 1.270 + bool tv, 1.271 + nsIRDFNode **target /* out */) 1.272 +{ 1.273 + NS_PRECONDITION(source != nullptr, "null ptr"); 1.274 + if (! source) 1.275 + return NS_ERROR_NULL_POINTER; 1.276 + 1.277 + NS_PRECONDITION(property != nullptr, "null ptr"); 1.278 + if (! property) 1.279 + return NS_ERROR_NULL_POINTER; 1.280 + 1.281 + NS_PRECONDITION(target != nullptr, "null ptr"); 1.282 + if (! target) 1.283 + return NS_ERROR_NULL_POINTER; 1.284 + 1.285 + *target = nullptr; 1.286 + 1.287 + nsresult rv = NS_RDF_NO_VALUE; 1.288 + 1.289 + // we only have positive assertions in the file system data source. 1.290 + if (! tv) 1.291 + return NS_RDF_NO_VALUE; 1.292 + 1.293 + if (source == mNC_FileSystemRoot) 1.294 + { 1.295 + if (property == mNC_pulse) 1.296 + { 1.297 + nsIRDFLiteral *pulseLiteral; 1.298 + mRDFService->GetLiteral(MOZ_UTF16("12"), &pulseLiteral); 1.299 + *target = pulseLiteral; 1.300 + return NS_OK; 1.301 + } 1.302 + } 1.303 + else if (isFileURI(source)) 1.304 + { 1.305 + if (property == mNC_Name) 1.306 + { 1.307 + nsCOMPtr<nsIRDFLiteral> name; 1.308 + rv = GetName(source, getter_AddRefs(name)); 1.309 + if (NS_FAILED(rv)) return(rv); 1.310 + if (!name) rv = NS_RDF_NO_VALUE; 1.311 + if (rv == NS_RDF_NO_VALUE) return(rv); 1.312 + return name->QueryInterface(NS_GET_IID(nsIRDFNode), (void**) target); 1.313 + } 1.314 + else if (property == mNC_URL) 1.315 + { 1.316 + nsCOMPtr<nsIRDFLiteral> url; 1.317 + rv = GetURL(source, nullptr, getter_AddRefs(url)); 1.318 + if (NS_FAILED(rv)) return(rv); 1.319 + if (!url) rv = NS_RDF_NO_VALUE; 1.320 + if (rv == NS_RDF_NO_VALUE) return(rv); 1.321 + 1.322 + return url->QueryInterface(NS_GET_IID(nsIRDFNode), (void**) target); 1.323 + } 1.324 + else if (property == mNC_Icon) 1.325 + { 1.326 + nsCOMPtr<nsIRDFLiteral> url; 1.327 + bool isFavorite = false; 1.328 + rv = GetURL(source, &isFavorite, getter_AddRefs(url)); 1.329 + if (NS_FAILED(rv)) return(rv); 1.330 + if (isFavorite || !url) rv = NS_RDF_NO_VALUE; 1.331 + if (rv == NS_RDF_NO_VALUE) return(rv); 1.332 + 1.333 + const char16_t *uni = nullptr; 1.334 + url->GetValueConst(&uni); 1.335 + if (!uni) return(NS_RDF_NO_VALUE); 1.336 + nsAutoString urlStr; 1.337 + urlStr.Assign(NS_LITERAL_STRING(NS_MOZICON_SCHEME).get()); 1.338 + urlStr.Append(uni); 1.339 + 1.340 + rv = mRDFService->GetLiteral(urlStr.get(), getter_AddRefs(url)); 1.341 + if (NS_FAILED(rv) || !url) return(NS_RDF_NO_VALUE); 1.342 + return url->QueryInterface(NS_GET_IID(nsIRDFNode), (void**) target); 1.343 + } 1.344 + else if (property == mNC_Length) 1.345 + { 1.346 + nsCOMPtr<nsIRDFInt> fileSize; 1.347 + rv = GetFileSize(source, getter_AddRefs(fileSize)); 1.348 + if (NS_FAILED(rv)) return(rv); 1.349 + if (!fileSize) rv = NS_RDF_NO_VALUE; 1.350 + if (rv == NS_RDF_NO_VALUE) return(rv); 1.351 + 1.352 + return fileSize->QueryInterface(NS_GET_IID(nsIRDFNode), (void**) target); 1.353 + } 1.354 + else if (property == mNC_IsDirectory) 1.355 + { 1.356 + *target = (isDirURI(source)) ? mLiteralTrue : mLiteralFalse; 1.357 + NS_ADDREF(*target); 1.358 + return NS_OK; 1.359 + } 1.360 + else if (property == mWEB_LastMod) 1.361 + { 1.362 + nsCOMPtr<nsIRDFDate> lastMod; 1.363 + rv = GetLastMod(source, getter_AddRefs(lastMod)); 1.364 + if (NS_FAILED(rv)) return(rv); 1.365 + if (!lastMod) rv = NS_RDF_NO_VALUE; 1.366 + if (rv == NS_RDF_NO_VALUE) return(rv); 1.367 + 1.368 + return lastMod->QueryInterface(NS_GET_IID(nsIRDFNode), (void**) target); 1.369 + } 1.370 + else if (property == mRDF_type) 1.371 + { 1.372 + nsCString type; 1.373 + rv = mNC_FileSystemObject->GetValueUTF8(type); 1.374 + if (NS_FAILED(rv)) return(rv); 1.375 + 1.376 +#ifdef XP_WIN 1.377 + // under Windows, if its an IE favorite, return that type 1.378 + if (!ieFavoritesDir.IsEmpty()) 1.379 + { 1.380 + nsCString uri; 1.381 + rv = source->GetValueUTF8(uri); 1.382 + if (NS_FAILED(rv)) return(rv); 1.383 + 1.384 + NS_ConvertUTF8toUTF16 theURI(uri); 1.385 + 1.386 + if (theURI.Find(ieFavoritesDir) == 0) 1.387 + { 1.388 + if (theURI[theURI.Length() - 1] == '/') 1.389 + { 1.390 + rv = mNC_IEFavoriteFolder->GetValueUTF8(type); 1.391 + } 1.392 + else 1.393 + { 1.394 + rv = mNC_IEFavoriteObject->GetValueUTF8(type); 1.395 + } 1.396 + if (NS_FAILED(rv)) return(rv); 1.397 + } 1.398 + } 1.399 +#endif 1.400 + 1.401 + NS_ConvertUTF8toUTF16 url(type); 1.402 + nsCOMPtr<nsIRDFLiteral> literal; 1.403 + mRDFService->GetLiteral(url.get(), getter_AddRefs(literal)); 1.404 + rv = literal->QueryInterface(NS_GET_IID(nsIRDFNode), (void**) target); 1.405 + return(rv); 1.406 + } 1.407 + else if (property == mNC_pulse) 1.408 + { 1.409 + nsCOMPtr<nsIRDFLiteral> pulseLiteral; 1.410 + mRDFService->GetLiteral(MOZ_UTF16("12"), getter_AddRefs(pulseLiteral)); 1.411 + rv = pulseLiteral->QueryInterface(NS_GET_IID(nsIRDFNode), (void**) target); 1.412 + return(rv); 1.413 + } 1.414 + else if (property == mNC_Child) 1.415 + { 1.416 + // Oh this is evil. Somebody kill me now. 1.417 + nsCOMPtr<nsISimpleEnumerator> children; 1.418 + rv = GetFolderList(source, false, true, getter_AddRefs(children)); 1.419 + if (NS_FAILED(rv) || (rv == NS_RDF_NO_VALUE)) return(rv); 1.420 + 1.421 + bool hasMore; 1.422 + rv = children->HasMoreElements(&hasMore); 1.423 + if (NS_FAILED(rv)) return(rv); 1.424 + 1.425 + if (hasMore) 1.426 + { 1.427 + nsCOMPtr<nsISupports> isupports; 1.428 + rv = children->GetNext(getter_AddRefs(isupports)); 1.429 + if (NS_FAILED(rv)) return(rv); 1.430 + 1.431 + return isupports->QueryInterface(NS_GET_IID(nsIRDFNode), (void**) target); 1.432 + } 1.433 + } 1.434 +#ifdef USE_NC_EXTENSION 1.435 + else if (property == mNC_extension) 1.436 + { 1.437 + nsCOMPtr<nsIRDFLiteral> extension; 1.438 + rv = GetExtension(source, getter_AddRefs(extension)); 1.439 + if (!extension) rv = NS_RDF_NO_VALUE; 1.440 + if (rv == NS_RDF_NO_VALUE) return(rv); 1.441 + return extension->QueryInterface(NS_GET_IID(nsIRDFNode), (void**) target); 1.442 + } 1.443 +#endif 1.444 + } 1.445 + 1.446 + return(NS_RDF_NO_VALUE); 1.447 +} 1.448 + 1.449 + 1.450 + 1.451 +NS_IMETHODIMP 1.452 +FileSystemDataSource::GetTargets(nsIRDFResource *source, 1.453 + nsIRDFResource *property, 1.454 + bool tv, 1.455 + nsISimpleEnumerator **targets /* out */) 1.456 +{ 1.457 + NS_PRECONDITION(source != nullptr, "null ptr"); 1.458 + if (! source) 1.459 + return NS_ERROR_NULL_POINTER; 1.460 + 1.461 + NS_PRECONDITION(property != nullptr, "null ptr"); 1.462 + if (! property) 1.463 + return NS_ERROR_NULL_POINTER; 1.464 + 1.465 + NS_PRECONDITION(targets != nullptr, "null ptr"); 1.466 + if (! targets) 1.467 + return NS_ERROR_NULL_POINTER; 1.468 + 1.469 + *targets = nullptr; 1.470 + 1.471 + // we only have positive assertions in the file system data source. 1.472 + if (! tv) 1.473 + return NS_RDF_NO_VALUE; 1.474 + 1.475 + nsresult rv; 1.476 + 1.477 + if (source == mNC_FileSystemRoot) 1.478 + { 1.479 + if (property == mNC_Child) 1.480 + { 1.481 + return GetVolumeList(targets); 1.482 + } 1.483 + else if (property == mNC_pulse) 1.484 + { 1.485 + nsCOMPtr<nsIRDFLiteral> pulseLiteral; 1.486 + mRDFService->GetLiteral(MOZ_UTF16("12"), 1.487 + getter_AddRefs(pulseLiteral)); 1.488 + return NS_NewSingletonEnumerator(targets, pulseLiteral); 1.489 + } 1.490 + } 1.491 + else if (isFileURI(source)) 1.492 + { 1.493 + if (property == mNC_Child) 1.494 + { 1.495 + return GetFolderList(source, false, false, targets); 1.496 + } 1.497 + else if (property == mNC_Name) 1.498 + { 1.499 + nsCOMPtr<nsIRDFLiteral> name; 1.500 + rv = GetName(source, getter_AddRefs(name)); 1.501 + if (NS_FAILED(rv)) return rv; 1.502 + 1.503 + return NS_NewSingletonEnumerator(targets, name); 1.504 + } 1.505 + else if (property == mNC_URL) 1.506 + { 1.507 + nsCOMPtr<nsIRDFLiteral> url; 1.508 + rv = GetURL(source, nullptr, getter_AddRefs(url)); 1.509 + if (NS_FAILED(rv)) return rv; 1.510 + 1.511 + return NS_NewSingletonEnumerator(targets, url); 1.512 + } 1.513 + else if (property == mRDF_type) 1.514 + { 1.515 + nsCString uri; 1.516 + rv = mNC_FileSystemObject->GetValueUTF8(uri); 1.517 + if (NS_FAILED(rv)) return rv; 1.518 + 1.519 + NS_ConvertUTF8toUTF16 url(uri); 1.520 + 1.521 + nsCOMPtr<nsIRDFLiteral> literal; 1.522 + rv = mRDFService->GetLiteral(url.get(), getter_AddRefs(literal)); 1.523 + if (NS_FAILED(rv)) return rv; 1.524 + 1.525 + return NS_NewSingletonEnumerator(targets, literal); 1.526 + } 1.527 + else if (property == mNC_pulse) 1.528 + { 1.529 + nsCOMPtr<nsIRDFLiteral> pulseLiteral; 1.530 + rv = mRDFService->GetLiteral(MOZ_UTF16("12"), 1.531 + getter_AddRefs(pulseLiteral)); 1.532 + if (NS_FAILED(rv)) return rv; 1.533 + 1.534 + return NS_NewSingletonEnumerator(targets, pulseLiteral); 1.535 + } 1.536 + } 1.537 + 1.538 + return NS_NewEmptyEnumerator(targets); 1.539 +} 1.540 + 1.541 + 1.542 + 1.543 +NS_IMETHODIMP 1.544 +FileSystemDataSource::Assert(nsIRDFResource *source, 1.545 + nsIRDFResource *property, 1.546 + nsIRDFNode *target, 1.547 + bool tv) 1.548 +{ 1.549 + return NS_RDF_ASSERTION_REJECTED; 1.550 +} 1.551 + 1.552 + 1.553 + 1.554 +NS_IMETHODIMP 1.555 +FileSystemDataSource::Unassert(nsIRDFResource *source, 1.556 + nsIRDFResource *property, 1.557 + nsIRDFNode *target) 1.558 +{ 1.559 + return NS_RDF_ASSERTION_REJECTED; 1.560 +} 1.561 + 1.562 + 1.563 + 1.564 +NS_IMETHODIMP 1.565 +FileSystemDataSource::Change(nsIRDFResource* aSource, 1.566 + nsIRDFResource* aProperty, 1.567 + nsIRDFNode* aOldTarget, 1.568 + nsIRDFNode* aNewTarget) 1.569 +{ 1.570 + return NS_RDF_ASSERTION_REJECTED; 1.571 +} 1.572 + 1.573 + 1.574 + 1.575 +NS_IMETHODIMP 1.576 +FileSystemDataSource::Move(nsIRDFResource* aOldSource, 1.577 + nsIRDFResource* aNewSource, 1.578 + nsIRDFResource* aProperty, 1.579 + nsIRDFNode* aTarget) 1.580 +{ 1.581 + return NS_RDF_ASSERTION_REJECTED; 1.582 +} 1.583 + 1.584 + 1.585 + 1.586 +NS_IMETHODIMP 1.587 +FileSystemDataSource::HasAssertion(nsIRDFResource *source, 1.588 + nsIRDFResource *property, 1.589 + nsIRDFNode *target, 1.590 + bool tv, 1.591 + bool *hasAssertion /* out */) 1.592 +{ 1.593 + NS_PRECONDITION(source != nullptr, "null ptr"); 1.594 + if (! source) 1.595 + return NS_ERROR_NULL_POINTER; 1.596 + 1.597 + NS_PRECONDITION(property != nullptr, "null ptr"); 1.598 + if (! property) 1.599 + return NS_ERROR_NULL_POINTER; 1.600 + 1.601 + NS_PRECONDITION(target != nullptr, "null ptr"); 1.602 + if (! target) 1.603 + return NS_ERROR_NULL_POINTER; 1.604 + 1.605 + NS_PRECONDITION(hasAssertion != nullptr, "null ptr"); 1.606 + if (! hasAssertion) 1.607 + return NS_ERROR_NULL_POINTER; 1.608 + 1.609 + // we only have positive assertions in the file system data source. 1.610 + *hasAssertion = false; 1.611 + 1.612 + if (! tv) { 1.613 + return NS_OK; 1.614 + } 1.615 + 1.616 + if ((source == mNC_FileSystemRoot) || isFileURI(source)) 1.617 + { 1.618 + if (property == mRDF_type) 1.619 + { 1.620 + nsCOMPtr<nsIRDFResource> resource( do_QueryInterface(target) ); 1.621 + if (resource.get() == mRDF_type) 1.622 + { 1.623 + *hasAssertion = true; 1.624 + } 1.625 + } 1.626 +#ifdef USE_NC_EXTENSION 1.627 + else if (property == mNC_extension) 1.628 + { 1.629 + // Cheat just a little here by making dirs always match 1.630 + if (isDirURI(source)) 1.631 + { 1.632 + *hasAssertion = true; 1.633 + } 1.634 + else 1.635 + { 1.636 + nsCOMPtr<nsIRDFLiteral> extension; 1.637 + GetExtension(source, getter_AddRefs(extension)); 1.638 + if (extension.get() == target) 1.639 + { 1.640 + *hasAssertion = true; 1.641 + } 1.642 + } 1.643 + } 1.644 +#endif 1.645 + else if (property == mNC_IsDirectory) 1.646 + { 1.647 + bool isDir = isDirURI(source); 1.648 + bool isEqual = false; 1.649 + target->EqualsNode(mLiteralTrue, &isEqual); 1.650 + if (isEqual) 1.651 + { 1.652 + *hasAssertion = isDir; 1.653 + } 1.654 + else 1.655 + { 1.656 + target->EqualsNode(mLiteralFalse, &isEqual); 1.657 + if (isEqual) 1.658 + *hasAssertion = !isDir; 1.659 + } 1.660 + } 1.661 + } 1.662 + 1.663 + return NS_OK; 1.664 +} 1.665 + 1.666 + 1.667 + 1.668 +NS_IMETHODIMP 1.669 +FileSystemDataSource::HasArcIn(nsIRDFNode *aNode, nsIRDFResource *aArc, bool *result) 1.670 +{ 1.671 + return NS_ERROR_NOT_IMPLEMENTED; 1.672 +} 1.673 + 1.674 + 1.675 + 1.676 +NS_IMETHODIMP 1.677 +FileSystemDataSource::HasArcOut(nsIRDFResource *aSource, nsIRDFResource *aArc, bool *result) 1.678 +{ 1.679 + *result = false; 1.680 + 1.681 + if (aSource == mNC_FileSystemRoot) 1.682 + { 1.683 + *result = (aArc == mNC_Child || aArc == mNC_pulse); 1.684 + } 1.685 + else if (isFileURI(aSource)) 1.686 + { 1.687 + if (aArc == mNC_pulse) 1.688 + { 1.689 + *result = true; 1.690 + } 1.691 + else if (isDirURI(aSource)) 1.692 + { 1.693 +#ifdef XP_WIN 1.694 + *result = isValidFolder(aSource); 1.695 +#else 1.696 + *result = true; 1.697 +#endif 1.698 + } 1.699 + else if (aArc == mNC_pulse || aArc == mNC_Name || aArc == mNC_Icon || 1.700 + aArc == mNC_URL || aArc == mNC_Length || aArc == mWEB_LastMod || 1.701 + aArc == mNC_FileSystemObject || aArc == mRDF_InstanceOf || 1.702 + aArc == mRDF_type) 1.703 + { 1.704 + *result = true; 1.705 + } 1.706 + } 1.707 + return NS_OK; 1.708 +} 1.709 + 1.710 + 1.711 + 1.712 +NS_IMETHODIMP 1.713 +FileSystemDataSource::ArcLabelsIn(nsIRDFNode *node, 1.714 + nsISimpleEnumerator ** labels /* out */) 1.715 +{ 1.716 +// NS_NOTYETIMPLEMENTED("write me"); 1.717 + return NS_ERROR_NOT_IMPLEMENTED; 1.718 +} 1.719 + 1.720 + 1.721 + 1.722 +NS_IMETHODIMP 1.723 +FileSystemDataSource::ArcLabelsOut(nsIRDFResource *source, 1.724 + nsISimpleEnumerator **labels /* out */) 1.725 +{ 1.726 + NS_PRECONDITION(source != nullptr, "null ptr"); 1.727 + if (! source) 1.728 + return NS_ERROR_NULL_POINTER; 1.729 + 1.730 + NS_PRECONDITION(labels != nullptr, "null ptr"); 1.731 + if (! labels) 1.732 + return NS_ERROR_NULL_POINTER; 1.733 + 1.734 + if (source == mNC_FileSystemRoot) 1.735 + { 1.736 + nsCOMArray<nsIRDFResource> resources; 1.737 + resources.SetCapacity(2); 1.738 + 1.739 + resources.AppendObject(mNC_Child); 1.740 + resources.AppendObject(mNC_pulse); 1.741 + 1.742 + return NS_NewArrayEnumerator(labels, resources); 1.743 + } 1.744 + else if (isFileURI(source)) 1.745 + { 1.746 + nsCOMArray<nsIRDFResource> resources; 1.747 + resources.SetCapacity(2); 1.748 + 1.749 + if (isDirURI(source)) 1.750 + { 1.751 +#ifdef XP_WIN 1.752 + if (isValidFolder(source)) 1.753 + { 1.754 + resources.AppendObject(mNC_Child); 1.755 + } 1.756 +#else 1.757 + resources.AppendObject(mNC_Child); 1.758 +#endif 1.759 + resources.AppendObject(mNC_pulse); 1.760 + } 1.761 + 1.762 + return NS_NewArrayEnumerator(labels, resources); 1.763 + } 1.764 + 1.765 + return NS_NewEmptyEnumerator(labels); 1.766 +} 1.767 + 1.768 + 1.769 + 1.770 +NS_IMETHODIMP 1.771 +FileSystemDataSource::GetAllResources(nsISimpleEnumerator** aCursor) 1.772 +{ 1.773 + NS_NOTYETIMPLEMENTED("sorry!"); 1.774 + return NS_ERROR_NOT_IMPLEMENTED; 1.775 +} 1.776 + 1.777 + 1.778 + 1.779 +NS_IMETHODIMP 1.780 +FileSystemDataSource::AddObserver(nsIRDFObserver *n) 1.781 +{ 1.782 + return NS_ERROR_NOT_IMPLEMENTED; 1.783 +} 1.784 + 1.785 + 1.786 + 1.787 +NS_IMETHODIMP 1.788 +FileSystemDataSource::RemoveObserver(nsIRDFObserver *n) 1.789 +{ 1.790 + return NS_ERROR_NOT_IMPLEMENTED; 1.791 +} 1.792 + 1.793 + 1.794 + 1.795 +NS_IMETHODIMP 1.796 +FileSystemDataSource::GetAllCmds(nsIRDFResource* source, 1.797 + nsISimpleEnumerator/*<nsIRDFResource>*/** commands) 1.798 +{ 1.799 + return(NS_NewEmptyEnumerator(commands)); 1.800 +} 1.801 + 1.802 + 1.803 + 1.804 +NS_IMETHODIMP 1.805 +FileSystemDataSource::IsCommandEnabled(nsISupportsArray/*<nsIRDFResource>*/* aSources, 1.806 + nsIRDFResource* aCommand, 1.807 + nsISupportsArray/*<nsIRDFResource>*/* aArguments, 1.808 + bool* aResult) 1.809 +{ 1.810 + return(NS_ERROR_NOT_IMPLEMENTED); 1.811 +} 1.812 + 1.813 + 1.814 + 1.815 +NS_IMETHODIMP 1.816 +FileSystemDataSource::DoCommand(nsISupportsArray/*<nsIRDFResource>*/* aSources, 1.817 + nsIRDFResource* aCommand, 1.818 + nsISupportsArray/*<nsIRDFResource>*/* aArguments) 1.819 +{ 1.820 + return(NS_ERROR_NOT_IMPLEMENTED); 1.821 +} 1.822 + 1.823 + 1.824 + 1.825 +NS_IMETHODIMP 1.826 +FileSystemDataSource::BeginUpdateBatch() 1.827 +{ 1.828 + return NS_OK; 1.829 +} 1.830 + 1.831 + 1.832 + 1.833 +NS_IMETHODIMP 1.834 +FileSystemDataSource::EndUpdateBatch() 1.835 +{ 1.836 + return NS_OK; 1.837 +} 1.838 + 1.839 + 1.840 + 1.841 +nsresult 1.842 +FileSystemDataSource::GetVolumeList(nsISimpleEnumerator** aResult) 1.843 +{ 1.844 + nsCOMArray<nsIRDFResource> volumes; 1.845 + nsCOMPtr<nsIRDFResource> vol; 1.846 + 1.847 +#ifdef XP_WIN 1.848 + 1.849 + int32_t driveType; 1.850 + wchar_t drive[32]; 1.851 + int32_t volNum; 1.852 + 1.853 + for (volNum = 0; volNum < 26; volNum++) 1.854 + { 1.855 + swprintf( drive, L"%c:\\", volNum + (char16_t)'A'); 1.856 + 1.857 + driveType = GetDriveTypeW(drive); 1.858 + if (driveType != DRIVE_UNKNOWN && driveType != DRIVE_NO_ROOT_DIR) 1.859 + { 1.860 + nsAutoCString url; 1.861 + url.AppendPrintf("file:///%c|/", volNum + 'A'); 1.862 + nsresult rv = mRDFService->GetResource(url, getter_AddRefs(vol)); 1.863 + if (NS_FAILED(rv)) 1.864 + return rv; 1.865 + 1.866 + volumes.AppendObject(vol); 1.867 + } 1.868 + } 1.869 +#endif 1.870 + 1.871 +#ifdef XP_UNIX 1.872 + mRDFService->GetResource(NS_LITERAL_CSTRING("file:///"), getter_AddRefs(vol)); 1.873 + volumes.AppendObject(vol); 1.874 +#endif 1.875 + 1.876 + return NS_NewArrayEnumerator(aResult, volumes); 1.877 +} 1.878 + 1.879 + 1.880 + 1.881 +#ifdef XP_WIN 1.882 +bool 1.883 +FileSystemDataSource::isValidFolder(nsIRDFResource *source) 1.884 +{ 1.885 + bool isValid = true; 1.886 + if (ieFavoritesDir.IsEmpty()) return(isValid); 1.887 + 1.888 + nsresult rv; 1.889 + nsCString uri; 1.890 + rv = source->GetValueUTF8(uri); 1.891 + if (NS_FAILED(rv)) return(isValid); 1.892 + 1.893 + NS_ConvertUTF8toUTF16 theURI(uri); 1.894 + if (theURI.Find(ieFavoritesDir) == 0) 1.895 + { 1.896 + isValid = false; 1.897 + 1.898 + nsCOMPtr<nsISimpleEnumerator> folderEnum; 1.899 + if (NS_SUCCEEDED(rv = GetFolderList(source, true, false, getter_AddRefs(folderEnum)))) 1.900 + { 1.901 + bool hasAny = false, hasMore; 1.902 + while (NS_SUCCEEDED(folderEnum->HasMoreElements(&hasMore)) && 1.903 + hasMore) 1.904 + { 1.905 + hasAny = true; 1.906 + 1.907 + nsCOMPtr<nsISupports> isupports; 1.908 + if (NS_FAILED(rv = folderEnum->GetNext(getter_AddRefs(isupports)))) 1.909 + break; 1.910 + nsCOMPtr<nsIRDFResource> res = do_QueryInterface(isupports); 1.911 + if (!res) break; 1.912 + 1.913 + nsCOMPtr<nsIRDFLiteral> nameLiteral; 1.914 + if (NS_FAILED(rv = GetName(res, getter_AddRefs(nameLiteral)))) 1.915 + break; 1.916 + 1.917 + const char16_t *uniName; 1.918 + if (NS_FAILED(rv = nameLiteral->GetValueConst(&uniName))) 1.919 + break; 1.920 + nsAutoString name(uniName); 1.921 + 1.922 + // An empty folder, or a folder that contains just "desktop.ini", 1.923 + // is considered to be a IE Favorite; otherwise, its a folder 1.924 + if (!name.LowerCaseEqualsLiteral("desktop.ini")) 1.925 + { 1.926 + isValid = true; 1.927 + break; 1.928 + } 1.929 + } 1.930 + if (!hasAny) isValid = true; 1.931 + } 1.932 + } 1.933 + return(isValid); 1.934 +} 1.935 +#endif 1.936 + 1.937 + 1.938 + 1.939 +nsresult 1.940 +FileSystemDataSource::GetFolderList(nsIRDFResource *source, bool allowHidden, 1.941 + bool onlyFirst, nsISimpleEnumerator** aResult) 1.942 +{ 1.943 + if (!isDirURI(source)) 1.944 + return(NS_RDF_NO_VALUE); 1.945 + 1.946 + nsresult rv; 1.947 + 1.948 + const char *parentURI = nullptr; 1.949 + rv = source->GetValueConst(&parentURI); 1.950 + if (NS_FAILED(rv)) 1.951 + return(rv); 1.952 + if (!parentURI) 1.953 + return(NS_ERROR_UNEXPECTED); 1.954 + 1.955 + nsCOMPtr<nsIURI> aIURI; 1.956 + if (NS_FAILED(rv = NS_NewURI(getter_AddRefs(aIURI), nsDependentCString(parentURI)))) 1.957 + return(rv); 1.958 + 1.959 + nsCOMPtr<nsIFileURL> fileURL = do_QueryInterface(aIURI); 1.960 + if (!fileURL) 1.961 + return NS_OK; 1.962 + 1.963 + nsCOMPtr<nsIFile> aDir; 1.964 + if (NS_FAILED(rv = fileURL->GetFile(getter_AddRefs(aDir)))) 1.965 + return(rv); 1.966 + 1.967 + // ensure that we DO NOT resolve aliases 1.968 + aDir->SetFollowLinks(false); 1.969 + 1.970 + nsCOMPtr<nsISimpleEnumerator> dirContents; 1.971 + if (NS_FAILED(rv = aDir->GetDirectoryEntries(getter_AddRefs(dirContents)))) 1.972 + return(rv); 1.973 + if (!dirContents) 1.974 + return(NS_ERROR_UNEXPECTED); 1.975 + 1.976 + nsCOMArray<nsIRDFResource> resources; 1.977 + bool hasMore; 1.978 + while(NS_SUCCEEDED(rv = dirContents->HasMoreElements(&hasMore)) && 1.979 + hasMore) 1.980 + { 1.981 + nsCOMPtr<nsISupports> isupports; 1.982 + if (NS_FAILED(rv = dirContents->GetNext(getter_AddRefs(isupports)))) 1.983 + break; 1.984 + 1.985 + nsCOMPtr<nsIFile> aFile = do_QueryInterface(isupports); 1.986 + if (!aFile) 1.987 + break; 1.988 + 1.989 + if (!allowHidden) 1.990 + { 1.991 + bool hiddenFlag = false; 1.992 + if (NS_FAILED(rv = aFile->IsHidden(&hiddenFlag))) 1.993 + break; 1.994 + if (hiddenFlag) 1.995 + continue; 1.996 + } 1.997 + 1.998 + nsAutoString leafStr; 1.999 + if (NS_FAILED(rv = aFile->GetLeafName(leafStr))) 1.1000 + break; 1.1001 + if (leafStr.IsEmpty()) 1.1002 + continue; 1.1003 + 1.1004 + nsAutoCString fullURI; 1.1005 + fullURI.Assign(parentURI); 1.1006 + if (fullURI.Last() != '/') 1.1007 + { 1.1008 + fullURI.Append('/'); 1.1009 + } 1.1010 + 1.1011 + char *escLeafStr = nsEscape(NS_ConvertUTF16toUTF8(leafStr).get(), url_Path); 1.1012 + leafStr.Truncate(); 1.1013 + 1.1014 + if (!escLeafStr) 1.1015 + continue; 1.1016 + 1.1017 + nsAutoCString leaf(escLeafStr); 1.1018 + NS_Free(escLeafStr); 1.1019 + escLeafStr = nullptr; 1.1020 + 1.1021 + // using nsEscape() [above] doesn't escape slashes, so do that by hand 1.1022 + int32_t aOffset; 1.1023 + while ((aOffset = leaf.FindChar('/')) >= 0) 1.1024 + { 1.1025 + leaf.Cut((uint32_t)aOffset, 1); 1.1026 + leaf.Insert("%2F", (uint32_t)aOffset); 1.1027 + } 1.1028 + 1.1029 + // append the encoded name 1.1030 + fullURI.Append(leaf); 1.1031 + 1.1032 + bool dirFlag = false; 1.1033 + rv = aFile->IsDirectory(&dirFlag); 1.1034 + if (NS_SUCCEEDED(rv) && dirFlag) 1.1035 + { 1.1036 + fullURI.Append('/'); 1.1037 + } 1.1038 + 1.1039 + nsCOMPtr<nsIRDFResource> fileRes; 1.1040 + mRDFService->GetResource(fullURI, getter_AddRefs(fileRes)); 1.1041 + 1.1042 + resources.AppendObject(fileRes); 1.1043 + 1.1044 + if (onlyFirst) 1.1045 + break; 1.1046 + } 1.1047 + 1.1048 + return NS_NewArrayEnumerator(aResult, resources); 1.1049 +} 1.1050 + 1.1051 +nsresult 1.1052 +FileSystemDataSource::GetLastMod(nsIRDFResource *source, nsIRDFDate **aResult) 1.1053 +{ 1.1054 + *aResult = nullptr; 1.1055 + 1.1056 + nsresult rv; 1.1057 + const char *uri = nullptr; 1.1058 + 1.1059 + rv = source->GetValueConst(&uri); 1.1060 + if (NS_FAILED(rv)) return(rv); 1.1061 + if (!uri) 1.1062 + return(NS_ERROR_UNEXPECTED); 1.1063 + 1.1064 + nsCOMPtr<nsIURI> aIURI; 1.1065 + if (NS_FAILED(rv = NS_NewURI(getter_AddRefs(aIURI), nsDependentCString(uri)))) 1.1066 + return(rv); 1.1067 + 1.1068 + nsCOMPtr<nsIFileURL> fileURL = do_QueryInterface(aIURI); 1.1069 + if (!fileURL) 1.1070 + return NS_OK; 1.1071 + 1.1072 + nsCOMPtr<nsIFile> aFile; 1.1073 + if (NS_FAILED(rv = fileURL->GetFile(getter_AddRefs(aFile)))) 1.1074 + return(rv); 1.1075 + if (!aFile) 1.1076 + return(NS_ERROR_UNEXPECTED); 1.1077 + 1.1078 + // ensure that we DO NOT resolve aliases 1.1079 + aFile->SetFollowLinks(false); 1.1080 + 1.1081 + PRTime lastModDate; 1.1082 + if (NS_FAILED(rv = aFile->GetLastModifiedTime(&lastModDate))) 1.1083 + return(rv); 1.1084 + 1.1085 + // convert from milliseconds to seconds 1.1086 + mRDFService->GetDateLiteral(lastModDate * PR_MSEC_PER_SEC, aResult); 1.1087 + 1.1088 + return(NS_OK); 1.1089 +} 1.1090 + 1.1091 + 1.1092 + 1.1093 +nsresult 1.1094 +FileSystemDataSource::GetFileSize(nsIRDFResource *source, nsIRDFInt **aResult) 1.1095 +{ 1.1096 + *aResult = nullptr; 1.1097 + 1.1098 + nsresult rv; 1.1099 + const char *uri = nullptr; 1.1100 + 1.1101 + rv = source->GetValueConst(&uri); 1.1102 + if (NS_FAILED(rv)) 1.1103 + return(rv); 1.1104 + if (!uri) 1.1105 + return(NS_ERROR_UNEXPECTED); 1.1106 + 1.1107 + nsCOMPtr<nsIURI> aIURI; 1.1108 + if (NS_FAILED(rv = NS_NewURI(getter_AddRefs(aIURI), nsDependentCString(uri)))) 1.1109 + return(rv); 1.1110 + 1.1111 + nsCOMPtr<nsIFileURL> fileURL = do_QueryInterface(aIURI); 1.1112 + if (!fileURL) 1.1113 + return NS_OK; 1.1114 + 1.1115 + nsCOMPtr<nsIFile> aFile; 1.1116 + if (NS_FAILED(rv = fileURL->GetFile(getter_AddRefs(aFile)))) 1.1117 + return(rv); 1.1118 + if (!aFile) 1.1119 + return(NS_ERROR_UNEXPECTED); 1.1120 + 1.1121 + // ensure that we DO NOT resolve aliases 1.1122 + aFile->SetFollowLinks(false); 1.1123 + 1.1124 + // don't do anything with directories 1.1125 + bool isDir = false; 1.1126 + if (NS_FAILED(rv = aFile->IsDirectory(&isDir))) 1.1127 + return(rv); 1.1128 + if (isDir) 1.1129 + return(NS_RDF_NO_VALUE); 1.1130 + 1.1131 + int64_t aFileSize64; 1.1132 + if (NS_FAILED(rv = aFile->GetFileSize(&aFileSize64))) 1.1133 + return(rv); 1.1134 + 1.1135 + // convert 64bits to 32bits 1.1136 + int32_t aFileSize32 = int32_t(aFileSize64); 1.1137 + mRDFService->GetIntLiteral(aFileSize32, aResult); 1.1138 + 1.1139 + return(NS_OK); 1.1140 +} 1.1141 + 1.1142 + 1.1143 + 1.1144 +nsresult 1.1145 +FileSystemDataSource::GetName(nsIRDFResource *source, nsIRDFLiteral **aResult) 1.1146 +{ 1.1147 + nsresult rv; 1.1148 + const char *uri = nullptr; 1.1149 + 1.1150 + rv = source->GetValueConst(&uri); 1.1151 + if (NS_FAILED(rv)) 1.1152 + return(rv); 1.1153 + if (!uri) 1.1154 + return(NS_ERROR_UNEXPECTED); 1.1155 + 1.1156 + nsCOMPtr<nsIURI> aIURI; 1.1157 + if (NS_FAILED(rv = NS_NewURI(getter_AddRefs(aIURI), nsDependentCString(uri)))) 1.1158 + return(rv); 1.1159 + 1.1160 + nsCOMPtr<nsIFileURL> fileURL = do_QueryInterface(aIURI); 1.1161 + if (!fileURL) 1.1162 + return NS_OK; 1.1163 + 1.1164 + nsCOMPtr<nsIFile> aFile; 1.1165 + if (NS_FAILED(rv = fileURL->GetFile(getter_AddRefs(aFile)))) 1.1166 + return(rv); 1.1167 + if (!aFile) 1.1168 + return(NS_ERROR_UNEXPECTED); 1.1169 + 1.1170 + // ensure that we DO NOT resolve aliases 1.1171 + aFile->SetFollowLinks(false); 1.1172 + 1.1173 + nsAutoString name; 1.1174 + if (NS_FAILED(rv = aFile->GetLeafName(name))) 1.1175 + return(rv); 1.1176 + if (name.IsEmpty()) 1.1177 + return(NS_ERROR_UNEXPECTED); 1.1178 + 1.1179 +#ifdef XP_WIN 1.1180 + // special hack for IE favorites under Windows; strip off the 1.1181 + // trailing ".url" or ".lnk" at the end of IE favorites names 1.1182 + int32_t nameLen = name.Length(); 1.1183 + if ((strncmp(uri, ieFavoritesDir.get(), ieFavoritesDir.Length()) == 0) && (nameLen > 4)) 1.1184 + { 1.1185 + nsAutoString extension; 1.1186 + name.Right(extension, 4); 1.1187 + if (extension.LowerCaseEqualsLiteral(".url") || 1.1188 + extension.LowerCaseEqualsLiteral(".lnk")) 1.1189 + { 1.1190 + name.Truncate(nameLen - 4); 1.1191 + } 1.1192 + } 1.1193 +#endif 1.1194 + 1.1195 + mRDFService->GetLiteral(name.get(), aResult); 1.1196 + 1.1197 + return NS_OK; 1.1198 +} 1.1199 + 1.1200 + 1.1201 + 1.1202 +#ifdef USE_NC_EXTENSION 1.1203 +nsresult 1.1204 +FileSystemDataSource::GetExtension(nsIRDFResource *source, nsIRDFLiteral **aResult) 1.1205 +{ 1.1206 + nsCOMPtr<nsIRDFLiteral> name; 1.1207 + nsresult rv = GetName(source, getter_AddRefs(name)); 1.1208 + if (NS_FAILED(rv)) 1.1209 + return rv; 1.1210 + 1.1211 + const char16_t* unicodeLeafName; 1.1212 + rv = name->GetValueConst(&unicodeLeafName); 1.1213 + if (NS_FAILED(rv)) 1.1214 + return rv; 1.1215 + 1.1216 + nsAutoString filename(unicodeLeafName); 1.1217 + int32_t lastDot = filename.RFindChar('.'); 1.1218 + if (lastDot == -1) 1.1219 + { 1.1220 + mRDFService->GetLiteral(EmptyString().get(), aResult); 1.1221 + } 1.1222 + else 1.1223 + { 1.1224 + nsAutoString extension; 1.1225 + filename.Right(extension, (filename.Length() - lastDot)); 1.1226 + mRDFService->GetLiteral(extension.get(), aResult); 1.1227 + } 1.1228 + 1.1229 + return NS_OK; 1.1230 +} 1.1231 +#endif 1.1232 + 1.1233 +#ifdef XP_WIN 1.1234 +nsresult 1.1235 +FileSystemDataSource::getIEFavoriteURL(nsIRDFResource *source, nsString aFileURL, nsIRDFLiteral **urlLiteral) 1.1236 +{ 1.1237 + nsresult rv = NS_OK; 1.1238 + 1.1239 + *urlLiteral = nullptr; 1.1240 + 1.1241 + nsCOMPtr<nsIFile> f; 1.1242 + NS_GetFileFromURLSpec(NS_ConvertUTF16toUTF8(aFileURL), getter_AddRefs(f)); 1.1243 + 1.1244 + bool value; 1.1245 + 1.1246 + if (NS_SUCCEEDED(f->IsDirectory(&value)) && value) 1.1247 + { 1.1248 + if (isValidFolder(source)) 1.1249 + return(NS_RDF_NO_VALUE); 1.1250 + aFileURL.AppendLiteral("desktop.ini"); 1.1251 + } 1.1252 + else if (aFileURL.Length() > 4) 1.1253 + { 1.1254 + nsAutoString extension; 1.1255 + 1.1256 + aFileURL.Right(extension, 4); 1.1257 + if (!extension.LowerCaseEqualsLiteral(".url")) 1.1258 + { 1.1259 + return(NS_RDF_NO_VALUE); 1.1260 + } 1.1261 + } 1.1262 + 1.1263 + nsCOMPtr<nsIInputStream> strm; 1.1264 + NS_NewLocalFileInputStream(getter_AddRefs(strm),f); 1.1265 + nsCOMPtr<nsILineInputStream> linereader = do_QueryInterface(strm, &rv); 1.1266 + 1.1267 + nsAutoString line; 1.1268 + nsAutoCString cLine; 1.1269 + while(NS_SUCCEEDED(rv)) 1.1270 + { 1.1271 + bool isEOF; 1.1272 + rv = linereader->ReadLine(cLine, &isEOF); 1.1273 + CopyASCIItoUTF16(cLine, line); 1.1274 + 1.1275 + if (isEOF) 1.1276 + { 1.1277 + if (line.Find("URL=", true) == 0) 1.1278 + { 1.1279 + line.Cut(0, 4); 1.1280 + rv = mRDFService->GetLiteral(line.get(), urlLiteral); 1.1281 + break; 1.1282 + } 1.1283 + else if (line.Find("CDFURL=", true) == 0) 1.1284 + { 1.1285 + line.Cut(0, 7); 1.1286 + rv = mRDFService->GetLiteral(line.get(), urlLiteral); 1.1287 + break; 1.1288 + } 1.1289 + line.Truncate(); 1.1290 + } 1.1291 + } 1.1292 + 1.1293 + return(rv); 1.1294 +} 1.1295 +#endif 1.1296 + 1.1297 + 1.1298 + 1.1299 +nsresult 1.1300 +FileSystemDataSource::GetURL(nsIRDFResource *source, bool *isFavorite, nsIRDFLiteral** aResult) 1.1301 +{ 1.1302 + if (isFavorite) *isFavorite = false; 1.1303 + 1.1304 + nsresult rv; 1.1305 + nsCString uri; 1.1306 + 1.1307 + rv = source->GetValueUTF8(uri); 1.1308 + if (NS_FAILED(rv)) 1.1309 + return(rv); 1.1310 + 1.1311 + NS_ConvertUTF8toUTF16 url(uri); 1.1312 + 1.1313 +#ifdef XP_WIN 1.1314 + // under Windows, if its an IE favorite, munge the URL 1.1315 + if (!ieFavoritesDir.IsEmpty()) 1.1316 + { 1.1317 + if (url.Find(ieFavoritesDir) == 0) 1.1318 + { 1.1319 + if (isFavorite) *isFavorite = true; 1.1320 + rv = getIEFavoriteURL(source, url, aResult); 1.1321 + return(rv); 1.1322 + } 1.1323 + } 1.1324 +#endif 1.1325 + 1.1326 + // if we fall through to here, its not any type of bookmark 1.1327 + // stored in the platform native file system, so just set the URL 1.1328 + 1.1329 + mRDFService->GetLiteral(url.get(), aResult); 1.1330 + 1.1331 + return(NS_OK); 1.1332 +}