rdf/datasource/src/nsFileSystemDataSource.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 /*
michael@0 7 Implementation for a file system RDF data store.
michael@0 8 */
michael@0 9
michael@0 10 #include "nsFileSystemDataSource.h"
michael@0 11
michael@0 12 #include <ctype.h> // for toupper()
michael@0 13 #include <stdio.h>
michael@0 14 #include "nsArrayEnumerator.h"
michael@0 15 #include "nsCOMArray.h"
michael@0 16 #include "nsISupportsArray.h"
michael@0 17 #include "nsIRDFDataSource.h"
michael@0 18 #include "nsIRDFObserver.h"
michael@0 19 #include "nsIServiceManager.h"
michael@0 20 #include "nsXPIDLString.h"
michael@0 21 #include "nsRDFCID.h"
michael@0 22 #include "rdfutil.h"
michael@0 23 #include "rdf.h"
michael@0 24 #include "nsEnumeratorUtils.h"
michael@0 25 #include "nsIURL.h"
michael@0 26 #include "nsIFileURL.h"
michael@0 27 #include "nsNetUtil.h"
michael@0 28 #include "nsIChannel.h"
michael@0 29 #include "nsIFile.h"
michael@0 30 #include "nsEscape.h"
michael@0 31 #include "nsCRTGlue.h"
michael@0 32 #include "nsAutoPtr.h"
michael@0 33
michael@0 34 #ifdef XP_WIN
michael@0 35 #include "windef.h"
michael@0 36 #include "winbase.h"
michael@0 37 #include "nsILineInputStream.h"
michael@0 38 #include "nsDirectoryServiceDefs.h"
michael@0 39 #endif
michael@0 40
michael@0 41 #define NS_MOZICON_SCHEME "moz-icon:"
michael@0 42
michael@0 43 static const char kFileProtocol[] = "file://";
michael@0 44
michael@0 45 bool
michael@0 46 FileSystemDataSource::isFileURI(nsIRDFResource *r)
michael@0 47 {
michael@0 48 bool isFileURIFlag = false;
michael@0 49 const char *uri = nullptr;
michael@0 50
michael@0 51 r->GetValueConst(&uri);
michael@0 52 if ((uri) && (!strncmp(uri, kFileProtocol, sizeof(kFileProtocol) - 1)))
michael@0 53 {
michael@0 54 // XXX HACK HACK HACK
michael@0 55 if (!strchr(uri, '#'))
michael@0 56 {
michael@0 57 isFileURIFlag = true;
michael@0 58 }
michael@0 59 }
michael@0 60 return(isFileURIFlag);
michael@0 61 }
michael@0 62
michael@0 63
michael@0 64
michael@0 65 bool
michael@0 66 FileSystemDataSource::isDirURI(nsIRDFResource* source)
michael@0 67 {
michael@0 68 nsresult rv;
michael@0 69 const char *uri = nullptr;
michael@0 70
michael@0 71 rv = source->GetValueConst(&uri);
michael@0 72 if (NS_FAILED(rv)) return(false);
michael@0 73
michael@0 74 nsCOMPtr<nsIFile> aDir;
michael@0 75
michael@0 76 rv = NS_GetFileFromURLSpec(nsDependentCString(uri), getter_AddRefs(aDir));
michael@0 77 if (NS_FAILED(rv)) return(false);
michael@0 78
michael@0 79 bool isDirFlag = false;
michael@0 80
michael@0 81 rv = aDir->IsDirectory(&isDirFlag);
michael@0 82 if (NS_FAILED(rv)) return(false);
michael@0 83
michael@0 84 return(isDirFlag);
michael@0 85 }
michael@0 86
michael@0 87
michael@0 88 nsresult
michael@0 89 FileSystemDataSource::Init()
michael@0 90 {
michael@0 91 nsresult rv;
michael@0 92
michael@0 93 mRDFService = do_GetService("@mozilla.org/rdf/rdf-service;1");
michael@0 94 NS_ENSURE_TRUE(mRDFService, NS_ERROR_FAILURE);
michael@0 95
michael@0 96 rv = mRDFService->GetResource(NS_LITERAL_CSTRING("NC:FilesRoot"),
michael@0 97 getter_AddRefs(mNC_FileSystemRoot));
michael@0 98 nsresult tmp = mRDFService->GetResource(NS_LITERAL_CSTRING(NC_NAMESPACE_URI "child"),
michael@0 99 getter_AddRefs(mNC_Child));
michael@0 100 if (NS_FAILED(tmp)) {
michael@0 101 rv = tmp;
michael@0 102 }
michael@0 103 tmp = mRDFService->GetResource(NS_LITERAL_CSTRING(NC_NAMESPACE_URI "Name"),
michael@0 104 getter_AddRefs(mNC_Name));
michael@0 105 if (NS_FAILED(tmp)) {
michael@0 106 rv = tmp;
michael@0 107 }
michael@0 108 tmp = mRDFService->GetResource(NS_LITERAL_CSTRING(NC_NAMESPACE_URI "URL"),
michael@0 109 getter_AddRefs(mNC_URL));
michael@0 110 if (NS_FAILED(tmp)) {
michael@0 111 rv = tmp;
michael@0 112 }
michael@0 113 tmp = mRDFService->GetResource(NS_LITERAL_CSTRING(NC_NAMESPACE_URI "Icon"),
michael@0 114 getter_AddRefs(mNC_Icon));
michael@0 115 if (NS_FAILED(tmp)) {
michael@0 116 rv = tmp;
michael@0 117 }
michael@0 118 tmp = mRDFService->GetResource(NS_LITERAL_CSTRING(NC_NAMESPACE_URI "Content-Length"),
michael@0 119 getter_AddRefs(mNC_Length));
michael@0 120 if (NS_FAILED(tmp)) {
michael@0 121 rv = tmp;
michael@0 122 }
michael@0 123 tmp = mRDFService->GetResource(NS_LITERAL_CSTRING(NC_NAMESPACE_URI "IsDirectory"),
michael@0 124 getter_AddRefs(mNC_IsDirectory));
michael@0 125 if (NS_FAILED(tmp)) {
michael@0 126 rv = tmp;
michael@0 127 }
michael@0 128 tmp = mRDFService->GetResource(NS_LITERAL_CSTRING(WEB_NAMESPACE_URI "LastModifiedDate"),
michael@0 129 getter_AddRefs(mWEB_LastMod));
michael@0 130 if (NS_FAILED(tmp)) {
michael@0 131 rv = tmp;
michael@0 132 }
michael@0 133 tmp = mRDFService->GetResource(NS_LITERAL_CSTRING(NC_NAMESPACE_URI "FileSystemObject"),
michael@0 134 getter_AddRefs(mNC_FileSystemObject));
michael@0 135 if (NS_FAILED(tmp)) {
michael@0 136 rv = tmp;
michael@0 137 }
michael@0 138 tmp = mRDFService->GetResource(NS_LITERAL_CSTRING(NC_NAMESPACE_URI "pulse"),
michael@0 139 getter_AddRefs(mNC_pulse));
michael@0 140 if (NS_FAILED(tmp)) {
michael@0 141 rv = tmp;
michael@0 142 }
michael@0 143 tmp = mRDFService->GetResource(NS_LITERAL_CSTRING(RDF_NAMESPACE_URI "instanceOf"),
michael@0 144 getter_AddRefs(mRDF_InstanceOf));
michael@0 145 if (NS_FAILED(tmp)) {
michael@0 146 rv = tmp;
michael@0 147 }
michael@0 148 tmp = mRDFService->GetResource(NS_LITERAL_CSTRING(RDF_NAMESPACE_URI "type"),
michael@0 149 getter_AddRefs(mRDF_type));
michael@0 150
michael@0 151 static const char16_t kTrue[] = {'t','r','u','e','\0'};
michael@0 152 static const char16_t kFalse[] = {'f','a','l','s','e','\0'};
michael@0 153
michael@0 154 tmp = mRDFService->GetLiteral(kTrue, getter_AddRefs(mLiteralTrue));
michael@0 155 if (NS_FAILED(tmp)) {
michael@0 156 rv = tmp;
michael@0 157 }
michael@0 158 tmp = mRDFService->GetLiteral(kFalse, getter_AddRefs(mLiteralFalse));
michael@0 159 if (NS_FAILED(tmp)) {
michael@0 160 rv = tmp;
michael@0 161 }
michael@0 162 NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE);
michael@0 163
michael@0 164 #ifdef USE_NC_EXTENSION
michael@0 165 rv = mRDFService->GetResource(NS_LITERAL_CSTRING(NC_NAMESPACE_URI "extension"),
michael@0 166 getter_AddRefs(mNC_extension));
michael@0 167 NS_ENSURE_SUCCESS(rv, rv);
michael@0 168 #endif
michael@0 169
michael@0 170 #ifdef XP_WIN
michael@0 171 rv = mRDFService->GetResource(NS_LITERAL_CSTRING(NC_NAMESPACE_URI "IEFavorite"),
michael@0 172 getter_AddRefs(mNC_IEFavoriteObject));
michael@0 173 tmp = mRDFService->GetResource(NS_LITERAL_CSTRING(NC_NAMESPACE_URI "IEFavoriteFolder"),
michael@0 174 getter_AddRefs(mNC_IEFavoriteFolder));
michael@0 175 if (NS_FAILED(tmp)) {
michael@0 176 rv = tmp;
michael@0 177 }
michael@0 178 NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE);
michael@0 179
michael@0 180 nsCOMPtr<nsIFile> file;
michael@0 181 NS_GetSpecialDirectory(NS_WIN_FAVORITES_DIR, getter_AddRefs(file));
michael@0 182 if (file)
michael@0 183 {
michael@0 184 nsCOMPtr<nsIURI> furi;
michael@0 185 NS_NewFileURI(getter_AddRefs(furi), file);
michael@0 186 NS_ENSURE_TRUE(furi, NS_ERROR_FAILURE);
michael@0 187
michael@0 188 file->GetNativePath(ieFavoritesDir);
michael@0 189 }
michael@0 190 #endif
michael@0 191
michael@0 192 return NS_OK;
michael@0 193 }
michael@0 194
michael@0 195 //static
michael@0 196 nsresult
michael@0 197 FileSystemDataSource::Create(nsISupports* aOuter, const nsIID& aIID, void **aResult)
michael@0 198 {
michael@0 199 NS_ENSURE_NO_AGGREGATION(aOuter);
michael@0 200
michael@0 201 nsRefPtr<FileSystemDataSource> self = new FileSystemDataSource();
michael@0 202 if (!self)
michael@0 203 return NS_ERROR_OUT_OF_MEMORY;
michael@0 204
michael@0 205 nsresult rv = self->Init();
michael@0 206 NS_ENSURE_SUCCESS(rv, rv);
michael@0 207
michael@0 208 return self->QueryInterface(aIID, aResult);
michael@0 209 }
michael@0 210
michael@0 211 NS_IMPL_ISUPPORTS(FileSystemDataSource, nsIRDFDataSource)
michael@0 212
michael@0 213 NS_IMETHODIMP
michael@0 214 FileSystemDataSource::GetURI(char **uri)
michael@0 215 {
michael@0 216 NS_PRECONDITION(uri != nullptr, "null ptr");
michael@0 217 if (! uri)
michael@0 218 return NS_ERROR_NULL_POINTER;
michael@0 219
michael@0 220 if ((*uri = NS_strdup("rdf:files")) == nullptr)
michael@0 221 return NS_ERROR_OUT_OF_MEMORY;
michael@0 222
michael@0 223 return NS_OK;
michael@0 224 }
michael@0 225
michael@0 226
michael@0 227
michael@0 228 NS_IMETHODIMP
michael@0 229 FileSystemDataSource::GetSource(nsIRDFResource* property,
michael@0 230 nsIRDFNode* target,
michael@0 231 bool tv,
michael@0 232 nsIRDFResource** source /* out */)
michael@0 233 {
michael@0 234 NS_PRECONDITION(property != nullptr, "null ptr");
michael@0 235 if (! property)
michael@0 236 return NS_ERROR_NULL_POINTER;
michael@0 237
michael@0 238 NS_PRECONDITION(target != nullptr, "null ptr");
michael@0 239 if (! target)
michael@0 240 return NS_ERROR_NULL_POINTER;
michael@0 241
michael@0 242 NS_PRECONDITION(source != nullptr, "null ptr");
michael@0 243 if (! source)
michael@0 244 return NS_ERROR_NULL_POINTER;
michael@0 245
michael@0 246 *source = nullptr;
michael@0 247 return NS_RDF_NO_VALUE;
michael@0 248 }
michael@0 249
michael@0 250
michael@0 251
michael@0 252 NS_IMETHODIMP
michael@0 253 FileSystemDataSource::GetSources(nsIRDFResource *property,
michael@0 254 nsIRDFNode *target,
michael@0 255 bool tv,
michael@0 256 nsISimpleEnumerator **sources /* out */)
michael@0 257 {
michael@0 258 // NS_NOTYETIMPLEMENTED("write me");
michael@0 259 return NS_ERROR_NOT_IMPLEMENTED;
michael@0 260 }
michael@0 261
michael@0 262
michael@0 263
michael@0 264 NS_IMETHODIMP
michael@0 265 FileSystemDataSource::GetTarget(nsIRDFResource *source,
michael@0 266 nsIRDFResource *property,
michael@0 267 bool tv,
michael@0 268 nsIRDFNode **target /* out */)
michael@0 269 {
michael@0 270 NS_PRECONDITION(source != nullptr, "null ptr");
michael@0 271 if (! source)
michael@0 272 return NS_ERROR_NULL_POINTER;
michael@0 273
michael@0 274 NS_PRECONDITION(property != nullptr, "null ptr");
michael@0 275 if (! property)
michael@0 276 return NS_ERROR_NULL_POINTER;
michael@0 277
michael@0 278 NS_PRECONDITION(target != nullptr, "null ptr");
michael@0 279 if (! target)
michael@0 280 return NS_ERROR_NULL_POINTER;
michael@0 281
michael@0 282 *target = nullptr;
michael@0 283
michael@0 284 nsresult rv = NS_RDF_NO_VALUE;
michael@0 285
michael@0 286 // we only have positive assertions in the file system data source.
michael@0 287 if (! tv)
michael@0 288 return NS_RDF_NO_VALUE;
michael@0 289
michael@0 290 if (source == mNC_FileSystemRoot)
michael@0 291 {
michael@0 292 if (property == mNC_pulse)
michael@0 293 {
michael@0 294 nsIRDFLiteral *pulseLiteral;
michael@0 295 mRDFService->GetLiteral(MOZ_UTF16("12"), &pulseLiteral);
michael@0 296 *target = pulseLiteral;
michael@0 297 return NS_OK;
michael@0 298 }
michael@0 299 }
michael@0 300 else if (isFileURI(source))
michael@0 301 {
michael@0 302 if (property == mNC_Name)
michael@0 303 {
michael@0 304 nsCOMPtr<nsIRDFLiteral> name;
michael@0 305 rv = GetName(source, getter_AddRefs(name));
michael@0 306 if (NS_FAILED(rv)) return(rv);
michael@0 307 if (!name) rv = NS_RDF_NO_VALUE;
michael@0 308 if (rv == NS_RDF_NO_VALUE) return(rv);
michael@0 309 return name->QueryInterface(NS_GET_IID(nsIRDFNode), (void**) target);
michael@0 310 }
michael@0 311 else if (property == mNC_URL)
michael@0 312 {
michael@0 313 nsCOMPtr<nsIRDFLiteral> url;
michael@0 314 rv = GetURL(source, nullptr, getter_AddRefs(url));
michael@0 315 if (NS_FAILED(rv)) return(rv);
michael@0 316 if (!url) rv = NS_RDF_NO_VALUE;
michael@0 317 if (rv == NS_RDF_NO_VALUE) return(rv);
michael@0 318
michael@0 319 return url->QueryInterface(NS_GET_IID(nsIRDFNode), (void**) target);
michael@0 320 }
michael@0 321 else if (property == mNC_Icon)
michael@0 322 {
michael@0 323 nsCOMPtr<nsIRDFLiteral> url;
michael@0 324 bool isFavorite = false;
michael@0 325 rv = GetURL(source, &isFavorite, getter_AddRefs(url));
michael@0 326 if (NS_FAILED(rv)) return(rv);
michael@0 327 if (isFavorite || !url) rv = NS_RDF_NO_VALUE;
michael@0 328 if (rv == NS_RDF_NO_VALUE) return(rv);
michael@0 329
michael@0 330 const char16_t *uni = nullptr;
michael@0 331 url->GetValueConst(&uni);
michael@0 332 if (!uni) return(NS_RDF_NO_VALUE);
michael@0 333 nsAutoString urlStr;
michael@0 334 urlStr.Assign(NS_LITERAL_STRING(NS_MOZICON_SCHEME).get());
michael@0 335 urlStr.Append(uni);
michael@0 336
michael@0 337 rv = mRDFService->GetLiteral(urlStr.get(), getter_AddRefs(url));
michael@0 338 if (NS_FAILED(rv) || !url) return(NS_RDF_NO_VALUE);
michael@0 339 return url->QueryInterface(NS_GET_IID(nsIRDFNode), (void**) target);
michael@0 340 }
michael@0 341 else if (property == mNC_Length)
michael@0 342 {
michael@0 343 nsCOMPtr<nsIRDFInt> fileSize;
michael@0 344 rv = GetFileSize(source, getter_AddRefs(fileSize));
michael@0 345 if (NS_FAILED(rv)) return(rv);
michael@0 346 if (!fileSize) rv = NS_RDF_NO_VALUE;
michael@0 347 if (rv == NS_RDF_NO_VALUE) return(rv);
michael@0 348
michael@0 349 return fileSize->QueryInterface(NS_GET_IID(nsIRDFNode), (void**) target);
michael@0 350 }
michael@0 351 else if (property == mNC_IsDirectory)
michael@0 352 {
michael@0 353 *target = (isDirURI(source)) ? mLiteralTrue : mLiteralFalse;
michael@0 354 NS_ADDREF(*target);
michael@0 355 return NS_OK;
michael@0 356 }
michael@0 357 else if (property == mWEB_LastMod)
michael@0 358 {
michael@0 359 nsCOMPtr<nsIRDFDate> lastMod;
michael@0 360 rv = GetLastMod(source, getter_AddRefs(lastMod));
michael@0 361 if (NS_FAILED(rv)) return(rv);
michael@0 362 if (!lastMod) rv = NS_RDF_NO_VALUE;
michael@0 363 if (rv == NS_RDF_NO_VALUE) return(rv);
michael@0 364
michael@0 365 return lastMod->QueryInterface(NS_GET_IID(nsIRDFNode), (void**) target);
michael@0 366 }
michael@0 367 else if (property == mRDF_type)
michael@0 368 {
michael@0 369 nsCString type;
michael@0 370 rv = mNC_FileSystemObject->GetValueUTF8(type);
michael@0 371 if (NS_FAILED(rv)) return(rv);
michael@0 372
michael@0 373 #ifdef XP_WIN
michael@0 374 // under Windows, if its an IE favorite, return that type
michael@0 375 if (!ieFavoritesDir.IsEmpty())
michael@0 376 {
michael@0 377 nsCString uri;
michael@0 378 rv = source->GetValueUTF8(uri);
michael@0 379 if (NS_FAILED(rv)) return(rv);
michael@0 380
michael@0 381 NS_ConvertUTF8toUTF16 theURI(uri);
michael@0 382
michael@0 383 if (theURI.Find(ieFavoritesDir) == 0)
michael@0 384 {
michael@0 385 if (theURI[theURI.Length() - 1] == '/')
michael@0 386 {
michael@0 387 rv = mNC_IEFavoriteFolder->GetValueUTF8(type);
michael@0 388 }
michael@0 389 else
michael@0 390 {
michael@0 391 rv = mNC_IEFavoriteObject->GetValueUTF8(type);
michael@0 392 }
michael@0 393 if (NS_FAILED(rv)) return(rv);
michael@0 394 }
michael@0 395 }
michael@0 396 #endif
michael@0 397
michael@0 398 NS_ConvertUTF8toUTF16 url(type);
michael@0 399 nsCOMPtr<nsIRDFLiteral> literal;
michael@0 400 mRDFService->GetLiteral(url.get(), getter_AddRefs(literal));
michael@0 401 rv = literal->QueryInterface(NS_GET_IID(nsIRDFNode), (void**) target);
michael@0 402 return(rv);
michael@0 403 }
michael@0 404 else if (property == mNC_pulse)
michael@0 405 {
michael@0 406 nsCOMPtr<nsIRDFLiteral> pulseLiteral;
michael@0 407 mRDFService->GetLiteral(MOZ_UTF16("12"), getter_AddRefs(pulseLiteral));
michael@0 408 rv = pulseLiteral->QueryInterface(NS_GET_IID(nsIRDFNode), (void**) target);
michael@0 409 return(rv);
michael@0 410 }
michael@0 411 else if (property == mNC_Child)
michael@0 412 {
michael@0 413 // Oh this is evil. Somebody kill me now.
michael@0 414 nsCOMPtr<nsISimpleEnumerator> children;
michael@0 415 rv = GetFolderList(source, false, true, getter_AddRefs(children));
michael@0 416 if (NS_FAILED(rv) || (rv == NS_RDF_NO_VALUE)) return(rv);
michael@0 417
michael@0 418 bool hasMore;
michael@0 419 rv = children->HasMoreElements(&hasMore);
michael@0 420 if (NS_FAILED(rv)) return(rv);
michael@0 421
michael@0 422 if (hasMore)
michael@0 423 {
michael@0 424 nsCOMPtr<nsISupports> isupports;
michael@0 425 rv = children->GetNext(getter_AddRefs(isupports));
michael@0 426 if (NS_FAILED(rv)) return(rv);
michael@0 427
michael@0 428 return isupports->QueryInterface(NS_GET_IID(nsIRDFNode), (void**) target);
michael@0 429 }
michael@0 430 }
michael@0 431 #ifdef USE_NC_EXTENSION
michael@0 432 else if (property == mNC_extension)
michael@0 433 {
michael@0 434 nsCOMPtr<nsIRDFLiteral> extension;
michael@0 435 rv = GetExtension(source, getter_AddRefs(extension));
michael@0 436 if (!extension) rv = NS_RDF_NO_VALUE;
michael@0 437 if (rv == NS_RDF_NO_VALUE) return(rv);
michael@0 438 return extension->QueryInterface(NS_GET_IID(nsIRDFNode), (void**) target);
michael@0 439 }
michael@0 440 #endif
michael@0 441 }
michael@0 442
michael@0 443 return(NS_RDF_NO_VALUE);
michael@0 444 }
michael@0 445
michael@0 446
michael@0 447
michael@0 448 NS_IMETHODIMP
michael@0 449 FileSystemDataSource::GetTargets(nsIRDFResource *source,
michael@0 450 nsIRDFResource *property,
michael@0 451 bool tv,
michael@0 452 nsISimpleEnumerator **targets /* out */)
michael@0 453 {
michael@0 454 NS_PRECONDITION(source != nullptr, "null ptr");
michael@0 455 if (! source)
michael@0 456 return NS_ERROR_NULL_POINTER;
michael@0 457
michael@0 458 NS_PRECONDITION(property != nullptr, "null ptr");
michael@0 459 if (! property)
michael@0 460 return NS_ERROR_NULL_POINTER;
michael@0 461
michael@0 462 NS_PRECONDITION(targets != nullptr, "null ptr");
michael@0 463 if (! targets)
michael@0 464 return NS_ERROR_NULL_POINTER;
michael@0 465
michael@0 466 *targets = nullptr;
michael@0 467
michael@0 468 // we only have positive assertions in the file system data source.
michael@0 469 if (! tv)
michael@0 470 return NS_RDF_NO_VALUE;
michael@0 471
michael@0 472 nsresult rv;
michael@0 473
michael@0 474 if (source == mNC_FileSystemRoot)
michael@0 475 {
michael@0 476 if (property == mNC_Child)
michael@0 477 {
michael@0 478 return GetVolumeList(targets);
michael@0 479 }
michael@0 480 else if (property == mNC_pulse)
michael@0 481 {
michael@0 482 nsCOMPtr<nsIRDFLiteral> pulseLiteral;
michael@0 483 mRDFService->GetLiteral(MOZ_UTF16("12"),
michael@0 484 getter_AddRefs(pulseLiteral));
michael@0 485 return NS_NewSingletonEnumerator(targets, pulseLiteral);
michael@0 486 }
michael@0 487 }
michael@0 488 else if (isFileURI(source))
michael@0 489 {
michael@0 490 if (property == mNC_Child)
michael@0 491 {
michael@0 492 return GetFolderList(source, false, false, targets);
michael@0 493 }
michael@0 494 else if (property == mNC_Name)
michael@0 495 {
michael@0 496 nsCOMPtr<nsIRDFLiteral> name;
michael@0 497 rv = GetName(source, getter_AddRefs(name));
michael@0 498 if (NS_FAILED(rv)) return rv;
michael@0 499
michael@0 500 return NS_NewSingletonEnumerator(targets, name);
michael@0 501 }
michael@0 502 else if (property == mNC_URL)
michael@0 503 {
michael@0 504 nsCOMPtr<nsIRDFLiteral> url;
michael@0 505 rv = GetURL(source, nullptr, getter_AddRefs(url));
michael@0 506 if (NS_FAILED(rv)) return rv;
michael@0 507
michael@0 508 return NS_NewSingletonEnumerator(targets, url);
michael@0 509 }
michael@0 510 else if (property == mRDF_type)
michael@0 511 {
michael@0 512 nsCString uri;
michael@0 513 rv = mNC_FileSystemObject->GetValueUTF8(uri);
michael@0 514 if (NS_FAILED(rv)) return rv;
michael@0 515
michael@0 516 NS_ConvertUTF8toUTF16 url(uri);
michael@0 517
michael@0 518 nsCOMPtr<nsIRDFLiteral> literal;
michael@0 519 rv = mRDFService->GetLiteral(url.get(), getter_AddRefs(literal));
michael@0 520 if (NS_FAILED(rv)) return rv;
michael@0 521
michael@0 522 return NS_NewSingletonEnumerator(targets, literal);
michael@0 523 }
michael@0 524 else if (property == mNC_pulse)
michael@0 525 {
michael@0 526 nsCOMPtr<nsIRDFLiteral> pulseLiteral;
michael@0 527 rv = mRDFService->GetLiteral(MOZ_UTF16("12"),
michael@0 528 getter_AddRefs(pulseLiteral));
michael@0 529 if (NS_FAILED(rv)) return rv;
michael@0 530
michael@0 531 return NS_NewSingletonEnumerator(targets, pulseLiteral);
michael@0 532 }
michael@0 533 }
michael@0 534
michael@0 535 return NS_NewEmptyEnumerator(targets);
michael@0 536 }
michael@0 537
michael@0 538
michael@0 539
michael@0 540 NS_IMETHODIMP
michael@0 541 FileSystemDataSource::Assert(nsIRDFResource *source,
michael@0 542 nsIRDFResource *property,
michael@0 543 nsIRDFNode *target,
michael@0 544 bool tv)
michael@0 545 {
michael@0 546 return NS_RDF_ASSERTION_REJECTED;
michael@0 547 }
michael@0 548
michael@0 549
michael@0 550
michael@0 551 NS_IMETHODIMP
michael@0 552 FileSystemDataSource::Unassert(nsIRDFResource *source,
michael@0 553 nsIRDFResource *property,
michael@0 554 nsIRDFNode *target)
michael@0 555 {
michael@0 556 return NS_RDF_ASSERTION_REJECTED;
michael@0 557 }
michael@0 558
michael@0 559
michael@0 560
michael@0 561 NS_IMETHODIMP
michael@0 562 FileSystemDataSource::Change(nsIRDFResource* aSource,
michael@0 563 nsIRDFResource* aProperty,
michael@0 564 nsIRDFNode* aOldTarget,
michael@0 565 nsIRDFNode* aNewTarget)
michael@0 566 {
michael@0 567 return NS_RDF_ASSERTION_REJECTED;
michael@0 568 }
michael@0 569
michael@0 570
michael@0 571
michael@0 572 NS_IMETHODIMP
michael@0 573 FileSystemDataSource::Move(nsIRDFResource* aOldSource,
michael@0 574 nsIRDFResource* aNewSource,
michael@0 575 nsIRDFResource* aProperty,
michael@0 576 nsIRDFNode* aTarget)
michael@0 577 {
michael@0 578 return NS_RDF_ASSERTION_REJECTED;
michael@0 579 }
michael@0 580
michael@0 581
michael@0 582
michael@0 583 NS_IMETHODIMP
michael@0 584 FileSystemDataSource::HasAssertion(nsIRDFResource *source,
michael@0 585 nsIRDFResource *property,
michael@0 586 nsIRDFNode *target,
michael@0 587 bool tv,
michael@0 588 bool *hasAssertion /* out */)
michael@0 589 {
michael@0 590 NS_PRECONDITION(source != nullptr, "null ptr");
michael@0 591 if (! source)
michael@0 592 return NS_ERROR_NULL_POINTER;
michael@0 593
michael@0 594 NS_PRECONDITION(property != nullptr, "null ptr");
michael@0 595 if (! property)
michael@0 596 return NS_ERROR_NULL_POINTER;
michael@0 597
michael@0 598 NS_PRECONDITION(target != nullptr, "null ptr");
michael@0 599 if (! target)
michael@0 600 return NS_ERROR_NULL_POINTER;
michael@0 601
michael@0 602 NS_PRECONDITION(hasAssertion != nullptr, "null ptr");
michael@0 603 if (! hasAssertion)
michael@0 604 return NS_ERROR_NULL_POINTER;
michael@0 605
michael@0 606 // we only have positive assertions in the file system data source.
michael@0 607 *hasAssertion = false;
michael@0 608
michael@0 609 if (! tv) {
michael@0 610 return NS_OK;
michael@0 611 }
michael@0 612
michael@0 613 if ((source == mNC_FileSystemRoot) || isFileURI(source))
michael@0 614 {
michael@0 615 if (property == mRDF_type)
michael@0 616 {
michael@0 617 nsCOMPtr<nsIRDFResource> resource( do_QueryInterface(target) );
michael@0 618 if (resource.get() == mRDF_type)
michael@0 619 {
michael@0 620 *hasAssertion = true;
michael@0 621 }
michael@0 622 }
michael@0 623 #ifdef USE_NC_EXTENSION
michael@0 624 else if (property == mNC_extension)
michael@0 625 {
michael@0 626 // Cheat just a little here by making dirs always match
michael@0 627 if (isDirURI(source))
michael@0 628 {
michael@0 629 *hasAssertion = true;
michael@0 630 }
michael@0 631 else
michael@0 632 {
michael@0 633 nsCOMPtr<nsIRDFLiteral> extension;
michael@0 634 GetExtension(source, getter_AddRefs(extension));
michael@0 635 if (extension.get() == target)
michael@0 636 {
michael@0 637 *hasAssertion = true;
michael@0 638 }
michael@0 639 }
michael@0 640 }
michael@0 641 #endif
michael@0 642 else if (property == mNC_IsDirectory)
michael@0 643 {
michael@0 644 bool isDir = isDirURI(source);
michael@0 645 bool isEqual = false;
michael@0 646 target->EqualsNode(mLiteralTrue, &isEqual);
michael@0 647 if (isEqual)
michael@0 648 {
michael@0 649 *hasAssertion = isDir;
michael@0 650 }
michael@0 651 else
michael@0 652 {
michael@0 653 target->EqualsNode(mLiteralFalse, &isEqual);
michael@0 654 if (isEqual)
michael@0 655 *hasAssertion = !isDir;
michael@0 656 }
michael@0 657 }
michael@0 658 }
michael@0 659
michael@0 660 return NS_OK;
michael@0 661 }
michael@0 662
michael@0 663
michael@0 664
michael@0 665 NS_IMETHODIMP
michael@0 666 FileSystemDataSource::HasArcIn(nsIRDFNode *aNode, nsIRDFResource *aArc, bool *result)
michael@0 667 {
michael@0 668 return NS_ERROR_NOT_IMPLEMENTED;
michael@0 669 }
michael@0 670
michael@0 671
michael@0 672
michael@0 673 NS_IMETHODIMP
michael@0 674 FileSystemDataSource::HasArcOut(nsIRDFResource *aSource, nsIRDFResource *aArc, bool *result)
michael@0 675 {
michael@0 676 *result = false;
michael@0 677
michael@0 678 if (aSource == mNC_FileSystemRoot)
michael@0 679 {
michael@0 680 *result = (aArc == mNC_Child || aArc == mNC_pulse);
michael@0 681 }
michael@0 682 else if (isFileURI(aSource))
michael@0 683 {
michael@0 684 if (aArc == mNC_pulse)
michael@0 685 {
michael@0 686 *result = true;
michael@0 687 }
michael@0 688 else if (isDirURI(aSource))
michael@0 689 {
michael@0 690 #ifdef XP_WIN
michael@0 691 *result = isValidFolder(aSource);
michael@0 692 #else
michael@0 693 *result = true;
michael@0 694 #endif
michael@0 695 }
michael@0 696 else if (aArc == mNC_pulse || aArc == mNC_Name || aArc == mNC_Icon ||
michael@0 697 aArc == mNC_URL || aArc == mNC_Length || aArc == mWEB_LastMod ||
michael@0 698 aArc == mNC_FileSystemObject || aArc == mRDF_InstanceOf ||
michael@0 699 aArc == mRDF_type)
michael@0 700 {
michael@0 701 *result = true;
michael@0 702 }
michael@0 703 }
michael@0 704 return NS_OK;
michael@0 705 }
michael@0 706
michael@0 707
michael@0 708
michael@0 709 NS_IMETHODIMP
michael@0 710 FileSystemDataSource::ArcLabelsIn(nsIRDFNode *node,
michael@0 711 nsISimpleEnumerator ** labels /* out */)
michael@0 712 {
michael@0 713 // NS_NOTYETIMPLEMENTED("write me");
michael@0 714 return NS_ERROR_NOT_IMPLEMENTED;
michael@0 715 }
michael@0 716
michael@0 717
michael@0 718
michael@0 719 NS_IMETHODIMP
michael@0 720 FileSystemDataSource::ArcLabelsOut(nsIRDFResource *source,
michael@0 721 nsISimpleEnumerator **labels /* out */)
michael@0 722 {
michael@0 723 NS_PRECONDITION(source != nullptr, "null ptr");
michael@0 724 if (! source)
michael@0 725 return NS_ERROR_NULL_POINTER;
michael@0 726
michael@0 727 NS_PRECONDITION(labels != nullptr, "null ptr");
michael@0 728 if (! labels)
michael@0 729 return NS_ERROR_NULL_POINTER;
michael@0 730
michael@0 731 if (source == mNC_FileSystemRoot)
michael@0 732 {
michael@0 733 nsCOMArray<nsIRDFResource> resources;
michael@0 734 resources.SetCapacity(2);
michael@0 735
michael@0 736 resources.AppendObject(mNC_Child);
michael@0 737 resources.AppendObject(mNC_pulse);
michael@0 738
michael@0 739 return NS_NewArrayEnumerator(labels, resources);
michael@0 740 }
michael@0 741 else if (isFileURI(source))
michael@0 742 {
michael@0 743 nsCOMArray<nsIRDFResource> resources;
michael@0 744 resources.SetCapacity(2);
michael@0 745
michael@0 746 if (isDirURI(source))
michael@0 747 {
michael@0 748 #ifdef XP_WIN
michael@0 749 if (isValidFolder(source))
michael@0 750 {
michael@0 751 resources.AppendObject(mNC_Child);
michael@0 752 }
michael@0 753 #else
michael@0 754 resources.AppendObject(mNC_Child);
michael@0 755 #endif
michael@0 756 resources.AppendObject(mNC_pulse);
michael@0 757 }
michael@0 758
michael@0 759 return NS_NewArrayEnumerator(labels, resources);
michael@0 760 }
michael@0 761
michael@0 762 return NS_NewEmptyEnumerator(labels);
michael@0 763 }
michael@0 764
michael@0 765
michael@0 766
michael@0 767 NS_IMETHODIMP
michael@0 768 FileSystemDataSource::GetAllResources(nsISimpleEnumerator** aCursor)
michael@0 769 {
michael@0 770 NS_NOTYETIMPLEMENTED("sorry!");
michael@0 771 return NS_ERROR_NOT_IMPLEMENTED;
michael@0 772 }
michael@0 773
michael@0 774
michael@0 775
michael@0 776 NS_IMETHODIMP
michael@0 777 FileSystemDataSource::AddObserver(nsIRDFObserver *n)
michael@0 778 {
michael@0 779 return NS_ERROR_NOT_IMPLEMENTED;
michael@0 780 }
michael@0 781
michael@0 782
michael@0 783
michael@0 784 NS_IMETHODIMP
michael@0 785 FileSystemDataSource::RemoveObserver(nsIRDFObserver *n)
michael@0 786 {
michael@0 787 return NS_ERROR_NOT_IMPLEMENTED;
michael@0 788 }
michael@0 789
michael@0 790
michael@0 791
michael@0 792 NS_IMETHODIMP
michael@0 793 FileSystemDataSource::GetAllCmds(nsIRDFResource* source,
michael@0 794 nsISimpleEnumerator/*<nsIRDFResource>*/** commands)
michael@0 795 {
michael@0 796 return(NS_NewEmptyEnumerator(commands));
michael@0 797 }
michael@0 798
michael@0 799
michael@0 800
michael@0 801 NS_IMETHODIMP
michael@0 802 FileSystemDataSource::IsCommandEnabled(nsISupportsArray/*<nsIRDFResource>*/* aSources,
michael@0 803 nsIRDFResource* aCommand,
michael@0 804 nsISupportsArray/*<nsIRDFResource>*/* aArguments,
michael@0 805 bool* aResult)
michael@0 806 {
michael@0 807 return(NS_ERROR_NOT_IMPLEMENTED);
michael@0 808 }
michael@0 809
michael@0 810
michael@0 811
michael@0 812 NS_IMETHODIMP
michael@0 813 FileSystemDataSource::DoCommand(nsISupportsArray/*<nsIRDFResource>*/* aSources,
michael@0 814 nsIRDFResource* aCommand,
michael@0 815 nsISupportsArray/*<nsIRDFResource>*/* aArguments)
michael@0 816 {
michael@0 817 return(NS_ERROR_NOT_IMPLEMENTED);
michael@0 818 }
michael@0 819
michael@0 820
michael@0 821
michael@0 822 NS_IMETHODIMP
michael@0 823 FileSystemDataSource::BeginUpdateBatch()
michael@0 824 {
michael@0 825 return NS_OK;
michael@0 826 }
michael@0 827
michael@0 828
michael@0 829
michael@0 830 NS_IMETHODIMP
michael@0 831 FileSystemDataSource::EndUpdateBatch()
michael@0 832 {
michael@0 833 return NS_OK;
michael@0 834 }
michael@0 835
michael@0 836
michael@0 837
michael@0 838 nsresult
michael@0 839 FileSystemDataSource::GetVolumeList(nsISimpleEnumerator** aResult)
michael@0 840 {
michael@0 841 nsCOMArray<nsIRDFResource> volumes;
michael@0 842 nsCOMPtr<nsIRDFResource> vol;
michael@0 843
michael@0 844 #ifdef XP_WIN
michael@0 845
michael@0 846 int32_t driveType;
michael@0 847 wchar_t drive[32];
michael@0 848 int32_t volNum;
michael@0 849
michael@0 850 for (volNum = 0; volNum < 26; volNum++)
michael@0 851 {
michael@0 852 swprintf( drive, L"%c:\\", volNum + (char16_t)'A');
michael@0 853
michael@0 854 driveType = GetDriveTypeW(drive);
michael@0 855 if (driveType != DRIVE_UNKNOWN && driveType != DRIVE_NO_ROOT_DIR)
michael@0 856 {
michael@0 857 nsAutoCString url;
michael@0 858 url.AppendPrintf("file:///%c|/", volNum + 'A');
michael@0 859 nsresult rv = mRDFService->GetResource(url, getter_AddRefs(vol));
michael@0 860 if (NS_FAILED(rv))
michael@0 861 return rv;
michael@0 862
michael@0 863 volumes.AppendObject(vol);
michael@0 864 }
michael@0 865 }
michael@0 866 #endif
michael@0 867
michael@0 868 #ifdef XP_UNIX
michael@0 869 mRDFService->GetResource(NS_LITERAL_CSTRING("file:///"), getter_AddRefs(vol));
michael@0 870 volumes.AppendObject(vol);
michael@0 871 #endif
michael@0 872
michael@0 873 return NS_NewArrayEnumerator(aResult, volumes);
michael@0 874 }
michael@0 875
michael@0 876
michael@0 877
michael@0 878 #ifdef XP_WIN
michael@0 879 bool
michael@0 880 FileSystemDataSource::isValidFolder(nsIRDFResource *source)
michael@0 881 {
michael@0 882 bool isValid = true;
michael@0 883 if (ieFavoritesDir.IsEmpty()) return(isValid);
michael@0 884
michael@0 885 nsresult rv;
michael@0 886 nsCString uri;
michael@0 887 rv = source->GetValueUTF8(uri);
michael@0 888 if (NS_FAILED(rv)) return(isValid);
michael@0 889
michael@0 890 NS_ConvertUTF8toUTF16 theURI(uri);
michael@0 891 if (theURI.Find(ieFavoritesDir) == 0)
michael@0 892 {
michael@0 893 isValid = false;
michael@0 894
michael@0 895 nsCOMPtr<nsISimpleEnumerator> folderEnum;
michael@0 896 if (NS_SUCCEEDED(rv = GetFolderList(source, true, false, getter_AddRefs(folderEnum))))
michael@0 897 {
michael@0 898 bool hasAny = false, hasMore;
michael@0 899 while (NS_SUCCEEDED(folderEnum->HasMoreElements(&hasMore)) &&
michael@0 900 hasMore)
michael@0 901 {
michael@0 902 hasAny = true;
michael@0 903
michael@0 904 nsCOMPtr<nsISupports> isupports;
michael@0 905 if (NS_FAILED(rv = folderEnum->GetNext(getter_AddRefs(isupports))))
michael@0 906 break;
michael@0 907 nsCOMPtr<nsIRDFResource> res = do_QueryInterface(isupports);
michael@0 908 if (!res) break;
michael@0 909
michael@0 910 nsCOMPtr<nsIRDFLiteral> nameLiteral;
michael@0 911 if (NS_FAILED(rv = GetName(res, getter_AddRefs(nameLiteral))))
michael@0 912 break;
michael@0 913
michael@0 914 const char16_t *uniName;
michael@0 915 if (NS_FAILED(rv = nameLiteral->GetValueConst(&uniName)))
michael@0 916 break;
michael@0 917 nsAutoString name(uniName);
michael@0 918
michael@0 919 // An empty folder, or a folder that contains just "desktop.ini",
michael@0 920 // is considered to be a IE Favorite; otherwise, its a folder
michael@0 921 if (!name.LowerCaseEqualsLiteral("desktop.ini"))
michael@0 922 {
michael@0 923 isValid = true;
michael@0 924 break;
michael@0 925 }
michael@0 926 }
michael@0 927 if (!hasAny) isValid = true;
michael@0 928 }
michael@0 929 }
michael@0 930 return(isValid);
michael@0 931 }
michael@0 932 #endif
michael@0 933
michael@0 934
michael@0 935
michael@0 936 nsresult
michael@0 937 FileSystemDataSource::GetFolderList(nsIRDFResource *source, bool allowHidden,
michael@0 938 bool onlyFirst, nsISimpleEnumerator** aResult)
michael@0 939 {
michael@0 940 if (!isDirURI(source))
michael@0 941 return(NS_RDF_NO_VALUE);
michael@0 942
michael@0 943 nsresult rv;
michael@0 944
michael@0 945 const char *parentURI = nullptr;
michael@0 946 rv = source->GetValueConst(&parentURI);
michael@0 947 if (NS_FAILED(rv))
michael@0 948 return(rv);
michael@0 949 if (!parentURI)
michael@0 950 return(NS_ERROR_UNEXPECTED);
michael@0 951
michael@0 952 nsCOMPtr<nsIURI> aIURI;
michael@0 953 if (NS_FAILED(rv = NS_NewURI(getter_AddRefs(aIURI), nsDependentCString(parentURI))))
michael@0 954 return(rv);
michael@0 955
michael@0 956 nsCOMPtr<nsIFileURL> fileURL = do_QueryInterface(aIURI);
michael@0 957 if (!fileURL)
michael@0 958 return NS_OK;
michael@0 959
michael@0 960 nsCOMPtr<nsIFile> aDir;
michael@0 961 if (NS_FAILED(rv = fileURL->GetFile(getter_AddRefs(aDir))))
michael@0 962 return(rv);
michael@0 963
michael@0 964 // ensure that we DO NOT resolve aliases
michael@0 965 aDir->SetFollowLinks(false);
michael@0 966
michael@0 967 nsCOMPtr<nsISimpleEnumerator> dirContents;
michael@0 968 if (NS_FAILED(rv = aDir->GetDirectoryEntries(getter_AddRefs(dirContents))))
michael@0 969 return(rv);
michael@0 970 if (!dirContents)
michael@0 971 return(NS_ERROR_UNEXPECTED);
michael@0 972
michael@0 973 nsCOMArray<nsIRDFResource> resources;
michael@0 974 bool hasMore;
michael@0 975 while(NS_SUCCEEDED(rv = dirContents->HasMoreElements(&hasMore)) &&
michael@0 976 hasMore)
michael@0 977 {
michael@0 978 nsCOMPtr<nsISupports> isupports;
michael@0 979 if (NS_FAILED(rv = dirContents->GetNext(getter_AddRefs(isupports))))
michael@0 980 break;
michael@0 981
michael@0 982 nsCOMPtr<nsIFile> aFile = do_QueryInterface(isupports);
michael@0 983 if (!aFile)
michael@0 984 break;
michael@0 985
michael@0 986 if (!allowHidden)
michael@0 987 {
michael@0 988 bool hiddenFlag = false;
michael@0 989 if (NS_FAILED(rv = aFile->IsHidden(&hiddenFlag)))
michael@0 990 break;
michael@0 991 if (hiddenFlag)
michael@0 992 continue;
michael@0 993 }
michael@0 994
michael@0 995 nsAutoString leafStr;
michael@0 996 if (NS_FAILED(rv = aFile->GetLeafName(leafStr)))
michael@0 997 break;
michael@0 998 if (leafStr.IsEmpty())
michael@0 999 continue;
michael@0 1000
michael@0 1001 nsAutoCString fullURI;
michael@0 1002 fullURI.Assign(parentURI);
michael@0 1003 if (fullURI.Last() != '/')
michael@0 1004 {
michael@0 1005 fullURI.Append('/');
michael@0 1006 }
michael@0 1007
michael@0 1008 char *escLeafStr = nsEscape(NS_ConvertUTF16toUTF8(leafStr).get(), url_Path);
michael@0 1009 leafStr.Truncate();
michael@0 1010
michael@0 1011 if (!escLeafStr)
michael@0 1012 continue;
michael@0 1013
michael@0 1014 nsAutoCString leaf(escLeafStr);
michael@0 1015 NS_Free(escLeafStr);
michael@0 1016 escLeafStr = nullptr;
michael@0 1017
michael@0 1018 // using nsEscape() [above] doesn't escape slashes, so do that by hand
michael@0 1019 int32_t aOffset;
michael@0 1020 while ((aOffset = leaf.FindChar('/')) >= 0)
michael@0 1021 {
michael@0 1022 leaf.Cut((uint32_t)aOffset, 1);
michael@0 1023 leaf.Insert("%2F", (uint32_t)aOffset);
michael@0 1024 }
michael@0 1025
michael@0 1026 // append the encoded name
michael@0 1027 fullURI.Append(leaf);
michael@0 1028
michael@0 1029 bool dirFlag = false;
michael@0 1030 rv = aFile->IsDirectory(&dirFlag);
michael@0 1031 if (NS_SUCCEEDED(rv) && dirFlag)
michael@0 1032 {
michael@0 1033 fullURI.Append('/');
michael@0 1034 }
michael@0 1035
michael@0 1036 nsCOMPtr<nsIRDFResource> fileRes;
michael@0 1037 mRDFService->GetResource(fullURI, getter_AddRefs(fileRes));
michael@0 1038
michael@0 1039 resources.AppendObject(fileRes);
michael@0 1040
michael@0 1041 if (onlyFirst)
michael@0 1042 break;
michael@0 1043 }
michael@0 1044
michael@0 1045 return NS_NewArrayEnumerator(aResult, resources);
michael@0 1046 }
michael@0 1047
michael@0 1048 nsresult
michael@0 1049 FileSystemDataSource::GetLastMod(nsIRDFResource *source, nsIRDFDate **aResult)
michael@0 1050 {
michael@0 1051 *aResult = nullptr;
michael@0 1052
michael@0 1053 nsresult rv;
michael@0 1054 const char *uri = nullptr;
michael@0 1055
michael@0 1056 rv = source->GetValueConst(&uri);
michael@0 1057 if (NS_FAILED(rv)) return(rv);
michael@0 1058 if (!uri)
michael@0 1059 return(NS_ERROR_UNEXPECTED);
michael@0 1060
michael@0 1061 nsCOMPtr<nsIURI> aIURI;
michael@0 1062 if (NS_FAILED(rv = NS_NewURI(getter_AddRefs(aIURI), nsDependentCString(uri))))
michael@0 1063 return(rv);
michael@0 1064
michael@0 1065 nsCOMPtr<nsIFileURL> fileURL = do_QueryInterface(aIURI);
michael@0 1066 if (!fileURL)
michael@0 1067 return NS_OK;
michael@0 1068
michael@0 1069 nsCOMPtr<nsIFile> aFile;
michael@0 1070 if (NS_FAILED(rv = fileURL->GetFile(getter_AddRefs(aFile))))
michael@0 1071 return(rv);
michael@0 1072 if (!aFile)
michael@0 1073 return(NS_ERROR_UNEXPECTED);
michael@0 1074
michael@0 1075 // ensure that we DO NOT resolve aliases
michael@0 1076 aFile->SetFollowLinks(false);
michael@0 1077
michael@0 1078 PRTime lastModDate;
michael@0 1079 if (NS_FAILED(rv = aFile->GetLastModifiedTime(&lastModDate)))
michael@0 1080 return(rv);
michael@0 1081
michael@0 1082 // convert from milliseconds to seconds
michael@0 1083 mRDFService->GetDateLiteral(lastModDate * PR_MSEC_PER_SEC, aResult);
michael@0 1084
michael@0 1085 return(NS_OK);
michael@0 1086 }
michael@0 1087
michael@0 1088
michael@0 1089
michael@0 1090 nsresult
michael@0 1091 FileSystemDataSource::GetFileSize(nsIRDFResource *source, nsIRDFInt **aResult)
michael@0 1092 {
michael@0 1093 *aResult = nullptr;
michael@0 1094
michael@0 1095 nsresult rv;
michael@0 1096 const char *uri = nullptr;
michael@0 1097
michael@0 1098 rv = source->GetValueConst(&uri);
michael@0 1099 if (NS_FAILED(rv))
michael@0 1100 return(rv);
michael@0 1101 if (!uri)
michael@0 1102 return(NS_ERROR_UNEXPECTED);
michael@0 1103
michael@0 1104 nsCOMPtr<nsIURI> aIURI;
michael@0 1105 if (NS_FAILED(rv = NS_NewURI(getter_AddRefs(aIURI), nsDependentCString(uri))))
michael@0 1106 return(rv);
michael@0 1107
michael@0 1108 nsCOMPtr<nsIFileURL> fileURL = do_QueryInterface(aIURI);
michael@0 1109 if (!fileURL)
michael@0 1110 return NS_OK;
michael@0 1111
michael@0 1112 nsCOMPtr<nsIFile> aFile;
michael@0 1113 if (NS_FAILED(rv = fileURL->GetFile(getter_AddRefs(aFile))))
michael@0 1114 return(rv);
michael@0 1115 if (!aFile)
michael@0 1116 return(NS_ERROR_UNEXPECTED);
michael@0 1117
michael@0 1118 // ensure that we DO NOT resolve aliases
michael@0 1119 aFile->SetFollowLinks(false);
michael@0 1120
michael@0 1121 // don't do anything with directories
michael@0 1122 bool isDir = false;
michael@0 1123 if (NS_FAILED(rv = aFile->IsDirectory(&isDir)))
michael@0 1124 return(rv);
michael@0 1125 if (isDir)
michael@0 1126 return(NS_RDF_NO_VALUE);
michael@0 1127
michael@0 1128 int64_t aFileSize64;
michael@0 1129 if (NS_FAILED(rv = aFile->GetFileSize(&aFileSize64)))
michael@0 1130 return(rv);
michael@0 1131
michael@0 1132 // convert 64bits to 32bits
michael@0 1133 int32_t aFileSize32 = int32_t(aFileSize64);
michael@0 1134 mRDFService->GetIntLiteral(aFileSize32, aResult);
michael@0 1135
michael@0 1136 return(NS_OK);
michael@0 1137 }
michael@0 1138
michael@0 1139
michael@0 1140
michael@0 1141 nsresult
michael@0 1142 FileSystemDataSource::GetName(nsIRDFResource *source, nsIRDFLiteral **aResult)
michael@0 1143 {
michael@0 1144 nsresult rv;
michael@0 1145 const char *uri = nullptr;
michael@0 1146
michael@0 1147 rv = source->GetValueConst(&uri);
michael@0 1148 if (NS_FAILED(rv))
michael@0 1149 return(rv);
michael@0 1150 if (!uri)
michael@0 1151 return(NS_ERROR_UNEXPECTED);
michael@0 1152
michael@0 1153 nsCOMPtr<nsIURI> aIURI;
michael@0 1154 if (NS_FAILED(rv = NS_NewURI(getter_AddRefs(aIURI), nsDependentCString(uri))))
michael@0 1155 return(rv);
michael@0 1156
michael@0 1157 nsCOMPtr<nsIFileURL> fileURL = do_QueryInterface(aIURI);
michael@0 1158 if (!fileURL)
michael@0 1159 return NS_OK;
michael@0 1160
michael@0 1161 nsCOMPtr<nsIFile> aFile;
michael@0 1162 if (NS_FAILED(rv = fileURL->GetFile(getter_AddRefs(aFile))))
michael@0 1163 return(rv);
michael@0 1164 if (!aFile)
michael@0 1165 return(NS_ERROR_UNEXPECTED);
michael@0 1166
michael@0 1167 // ensure that we DO NOT resolve aliases
michael@0 1168 aFile->SetFollowLinks(false);
michael@0 1169
michael@0 1170 nsAutoString name;
michael@0 1171 if (NS_FAILED(rv = aFile->GetLeafName(name)))
michael@0 1172 return(rv);
michael@0 1173 if (name.IsEmpty())
michael@0 1174 return(NS_ERROR_UNEXPECTED);
michael@0 1175
michael@0 1176 #ifdef XP_WIN
michael@0 1177 // special hack for IE favorites under Windows; strip off the
michael@0 1178 // trailing ".url" or ".lnk" at the end of IE favorites names
michael@0 1179 int32_t nameLen = name.Length();
michael@0 1180 if ((strncmp(uri, ieFavoritesDir.get(), ieFavoritesDir.Length()) == 0) && (nameLen > 4))
michael@0 1181 {
michael@0 1182 nsAutoString extension;
michael@0 1183 name.Right(extension, 4);
michael@0 1184 if (extension.LowerCaseEqualsLiteral(".url") ||
michael@0 1185 extension.LowerCaseEqualsLiteral(".lnk"))
michael@0 1186 {
michael@0 1187 name.Truncate(nameLen - 4);
michael@0 1188 }
michael@0 1189 }
michael@0 1190 #endif
michael@0 1191
michael@0 1192 mRDFService->GetLiteral(name.get(), aResult);
michael@0 1193
michael@0 1194 return NS_OK;
michael@0 1195 }
michael@0 1196
michael@0 1197
michael@0 1198
michael@0 1199 #ifdef USE_NC_EXTENSION
michael@0 1200 nsresult
michael@0 1201 FileSystemDataSource::GetExtension(nsIRDFResource *source, nsIRDFLiteral **aResult)
michael@0 1202 {
michael@0 1203 nsCOMPtr<nsIRDFLiteral> name;
michael@0 1204 nsresult rv = GetName(source, getter_AddRefs(name));
michael@0 1205 if (NS_FAILED(rv))
michael@0 1206 return rv;
michael@0 1207
michael@0 1208 const char16_t* unicodeLeafName;
michael@0 1209 rv = name->GetValueConst(&unicodeLeafName);
michael@0 1210 if (NS_FAILED(rv))
michael@0 1211 return rv;
michael@0 1212
michael@0 1213 nsAutoString filename(unicodeLeafName);
michael@0 1214 int32_t lastDot = filename.RFindChar('.');
michael@0 1215 if (lastDot == -1)
michael@0 1216 {
michael@0 1217 mRDFService->GetLiteral(EmptyString().get(), aResult);
michael@0 1218 }
michael@0 1219 else
michael@0 1220 {
michael@0 1221 nsAutoString extension;
michael@0 1222 filename.Right(extension, (filename.Length() - lastDot));
michael@0 1223 mRDFService->GetLiteral(extension.get(), aResult);
michael@0 1224 }
michael@0 1225
michael@0 1226 return NS_OK;
michael@0 1227 }
michael@0 1228 #endif
michael@0 1229
michael@0 1230 #ifdef XP_WIN
michael@0 1231 nsresult
michael@0 1232 FileSystemDataSource::getIEFavoriteURL(nsIRDFResource *source, nsString aFileURL, nsIRDFLiteral **urlLiteral)
michael@0 1233 {
michael@0 1234 nsresult rv = NS_OK;
michael@0 1235
michael@0 1236 *urlLiteral = nullptr;
michael@0 1237
michael@0 1238 nsCOMPtr<nsIFile> f;
michael@0 1239 NS_GetFileFromURLSpec(NS_ConvertUTF16toUTF8(aFileURL), getter_AddRefs(f));
michael@0 1240
michael@0 1241 bool value;
michael@0 1242
michael@0 1243 if (NS_SUCCEEDED(f->IsDirectory(&value)) && value)
michael@0 1244 {
michael@0 1245 if (isValidFolder(source))
michael@0 1246 return(NS_RDF_NO_VALUE);
michael@0 1247 aFileURL.AppendLiteral("desktop.ini");
michael@0 1248 }
michael@0 1249 else if (aFileURL.Length() > 4)
michael@0 1250 {
michael@0 1251 nsAutoString extension;
michael@0 1252
michael@0 1253 aFileURL.Right(extension, 4);
michael@0 1254 if (!extension.LowerCaseEqualsLiteral(".url"))
michael@0 1255 {
michael@0 1256 return(NS_RDF_NO_VALUE);
michael@0 1257 }
michael@0 1258 }
michael@0 1259
michael@0 1260 nsCOMPtr<nsIInputStream> strm;
michael@0 1261 NS_NewLocalFileInputStream(getter_AddRefs(strm),f);
michael@0 1262 nsCOMPtr<nsILineInputStream> linereader = do_QueryInterface(strm, &rv);
michael@0 1263
michael@0 1264 nsAutoString line;
michael@0 1265 nsAutoCString cLine;
michael@0 1266 while(NS_SUCCEEDED(rv))
michael@0 1267 {
michael@0 1268 bool isEOF;
michael@0 1269 rv = linereader->ReadLine(cLine, &isEOF);
michael@0 1270 CopyASCIItoUTF16(cLine, line);
michael@0 1271
michael@0 1272 if (isEOF)
michael@0 1273 {
michael@0 1274 if (line.Find("URL=", true) == 0)
michael@0 1275 {
michael@0 1276 line.Cut(0, 4);
michael@0 1277 rv = mRDFService->GetLiteral(line.get(), urlLiteral);
michael@0 1278 break;
michael@0 1279 }
michael@0 1280 else if (line.Find("CDFURL=", true) == 0)
michael@0 1281 {
michael@0 1282 line.Cut(0, 7);
michael@0 1283 rv = mRDFService->GetLiteral(line.get(), urlLiteral);
michael@0 1284 break;
michael@0 1285 }
michael@0 1286 line.Truncate();
michael@0 1287 }
michael@0 1288 }
michael@0 1289
michael@0 1290 return(rv);
michael@0 1291 }
michael@0 1292 #endif
michael@0 1293
michael@0 1294
michael@0 1295
michael@0 1296 nsresult
michael@0 1297 FileSystemDataSource::GetURL(nsIRDFResource *source, bool *isFavorite, nsIRDFLiteral** aResult)
michael@0 1298 {
michael@0 1299 if (isFavorite) *isFavorite = false;
michael@0 1300
michael@0 1301 nsresult rv;
michael@0 1302 nsCString uri;
michael@0 1303
michael@0 1304 rv = source->GetValueUTF8(uri);
michael@0 1305 if (NS_FAILED(rv))
michael@0 1306 return(rv);
michael@0 1307
michael@0 1308 NS_ConvertUTF8toUTF16 url(uri);
michael@0 1309
michael@0 1310 #ifdef XP_WIN
michael@0 1311 // under Windows, if its an IE favorite, munge the URL
michael@0 1312 if (!ieFavoritesDir.IsEmpty())
michael@0 1313 {
michael@0 1314 if (url.Find(ieFavoritesDir) == 0)
michael@0 1315 {
michael@0 1316 if (isFavorite) *isFavorite = true;
michael@0 1317 rv = getIEFavoriteURL(source, url, aResult);
michael@0 1318 return(rv);
michael@0 1319 }
michael@0 1320 }
michael@0 1321 #endif
michael@0 1322
michael@0 1323 // if we fall through to here, its not any type of bookmark
michael@0 1324 // stored in the platform native file system, so just set the URL
michael@0 1325
michael@0 1326 mRDFService->GetLiteral(url.get(), aResult);
michael@0 1327
michael@0 1328 return(NS_OK);
michael@0 1329 }

mercurial