return CallQueryInterface(visitCountLiteral, aTarget); } else if (aProperty == kNC_AgeInDays) { PRInt64 lastVisitDate; rv = GetRowValue(row, kToken_LastVisitDateColumn, &lastVisitDate); if (NS_FAILED(rv)) return rv;  PRInt32 days = GetAgeInDays(NormalizeTime(GetNow()), lastVisitDate);  nsCOMPtr<nsIRDFInt> ageLiteral; rv = gRDFService->GetIntLiteral(days, getter_AddRefs(ageLiteral)); if (NS_FAILED(rv)) return rv;  *aTarget = ageLiteral; NS_ADDREF(*aTarget); return NS_OK; } else if (aProperty == kNC_Name || aProperty == kNC_NameSort) {  nsAutoString title; rv = GetRowValue(row, kToken_NameColumn, title); if (NS_FAILED(rv) || title.IsEmpty()) {  nsCOMPtr<nsIURI> aUri; rv = NS_NewURI(getter_AddRefs(aUri), uri); if (NS_FAILED(rv)) return rv; nsCOMPtr<nsIURL> urlObj(do_QueryInterface(aUri)); if (!urlObj) return NS_ERROR_FAILURE;  nsCAutoString filename; rv = urlObj->GetFileName(filename); if (NS_FAILED(rv) || filename.IsEmpty()) {  rv = urlObj->GetPath(filename); if (strcmp(filename.get(), "/") == 0) {   return GetTarget(aSource, kNC_Hostname, aTruthValue, aTarget);  } }  if (NS_FAILED(rv)) return rv;   title = NS_ConvertUTF8toUCS2(filename); } if (NS_FAILED(rv)) return rv;
len = yarn.mYarn_Fill / sizeof(PRUnichar); nsAutoString str((const PRUnichar*) yarn.mYarn_Buf, len);
nsGlobalHistory::GetTarget(nsIRDFResource* aSource,                           nsIRDFResource* aProperty,                           PRBool aTruthValue,                           nsIRDFNode** aTarget){    NS_PRECONDITION(aSource != nsnull, "null ptr");  if (! aSource)    return NS_ERROR_NULL_POINTER;  NS_PRECONDITION(aProperty != nsnull, "null ptr");  if (! aProperty)    return NS_ERROR_NULL_POINTER;  nsresult rv;  // Initialize return value.  *aTarget = nsnull;  // Only "positive" assertions here.  if (! aTruthValue)    return NS_RDF_NO_VALUE;    // XXX eventually use IsFindResource to simply return the first    // matching row?  if (aProperty == kNC_child &&      (aSource == kNC_HistoryRoot ||       aSource == kNC_HistoryByDate ||       IsFindResource(aSource))) {          // If they're asking for all the children of the HistoryRoot, call    // through to GetTargets() and return the first one.    nsCOMPtr<nsISimpleEnumerator> targets;    rv = GetTargets(aSource, aProperty, aTruthValue, getter_AddRefs(targets));    if (NS_FAILED(rv)) return rv;        PRBool hasMore;    rv = targets->HasMoreElements(&hasMore);    if (NS_FAILED(rv)) return rv;        if (! hasMore) return NS_RDF_NO_VALUE;        nsCOMPtr<nsISupports> isupports;    rv = targets->GetNext(getter_AddRefs(isupports));    if (NS_FAILED(rv)) return rv;        return CallQueryInterface(isupports, aTarget);  }  else if ((aProperty == kNC_Date) ||           (aProperty == kNC_FirstVisitDate) ||           (aProperty == kNC_VisitCount) ||           (aProperty == kNC_AgeInDays) ||           (aProperty == kNC_Name) ||           (aProperty == kNC_NameSort) ||           (aProperty == kNC_Hostname) ||           (aProperty == kNC_Referrer) ||           (aProperty == kNC_URL)) {    const char* uri;    rv = aSource->GetValueConst(&uri);    if (NS_FAILED(rv)) return rv;    // url is self-referential, so we'll always just return itself    // however, don't return the URLs of find resources    if (aProperty == kNC_URL && !IsFindResource(aSource)) {            nsCOMPtr<nsIRDFLiteral> uriLiteral;      rv = gRDFService->GetLiteral(NS_ConvertUTF8toUCS2(uri).get(), getter_AddRefs(uriLiteral));      if (NS_FAILED(rv))    return(rv);      *aTarget = uriLiteral;      NS_ADDREF(*aTarget);      return NS_OK;    }    // find URIs are special    if (((aProperty == kNC_Name) || (aProperty == kNC_NameSort)) &&        IsFindResource(aSource)) {            // for sorting, we sort by uri, so just return the URI as a literal      if (aProperty == kNC_NameSort) {        nsCOMPtr<nsIRDFLiteral> uriLiteral;        rv = gRDFService->GetLiteral(NS_ConvertUTF8toUCS2(uri).get(),                                     getter_AddRefs(uriLiteral));        if (NS_FAILED(rv))    return(rv);                *aTarget = uriLiteral;        NS_ADDREF(*aTarget);        return NS_OK;      }      else         return GetFindUriName(uri, aTarget);    }    // ok, we got this far, so we have to retrieve something from    // the row in the database    nsCOMPtr<nsIMdbRow> row;    rv = FindRow(kToken_URLColumn, uri, getter_AddRefs(row));    if (NS_FAILED(rv)) return NS_RDF_NO_VALUE;    mdb_err err;    // ...and then depending on the property they want, we'll pull the    // cell they want out of it.    if (aProperty == kNC_Date  ||        aProperty == kNC_FirstVisitDate) {      // Last visit date      PRInt64 i;      if (aProperty == kNC_Date)        rv = GetRowValue(row, kToken_LastVisitDateColumn, &i);      else        rv = GetRowValue(row, kToken_FirstVisitDateColumn, &i);      if (NS_FAILED(rv)) return rv;      nsCOMPtr<nsIRDFDate> date;      rv = gRDFService->GetDateLiteral(i, getter_AddRefs(date));      if (NS_FAILED(rv)) return rv;      return CallQueryInterface(date, aTarget);    }    else if (aProperty == kNC_VisitCount) {      mdbYarn yarn;      err = row->AliasCellYarn(mEnv, kToken_VisitCountColumn, &yarn);      if (err != 0) return NS_ERROR_FAILURE;      PRInt32 visitCount = 0;      rv = GetRowValue(row, kToken_VisitCountColumn, &visitCount);      if (NS_FAILED(rv) || visitCount <1)        visitCount = 1;         // assume we've visited at least once      nsCOMPtr<nsIRDFInt> visitCountLiteral;      rv = gRDFService->GetIntLiteral(visitCount,                                      getter_AddRefs(visitCountLiteral));      if (NS_FAILED(rv)) return rv;      return CallQueryInterface(visitCountLiteral, aTarget);    }    else if (aProperty == kNC_AgeInDays) {      PRInt64 lastVisitDate;      rv = GetRowValue(row, kToken_LastVisitDateColumn, &lastVisitDate);      if (NS_FAILED(rv)) return rv;            PRInt32 days = GetAgeInDays(NormalizeTime(GetNow()), lastVisitDate);      nsCOMPtr<nsIRDFInt> ageLiteral;      rv = gRDFService->GetIntLiteral(days, getter_AddRefs(ageLiteral));      if (NS_FAILED(rv)) return rv;      *aTarget = ageLiteral;      NS_ADDREF(*aTarget);      return NS_OK;    }    else if (aProperty == kNC_Name ||             aProperty == kNC_NameSort) {      // Site name (i.e., page title)      nsAutoString title;      rv = GetRowValue(row, kToken_NameColumn, title);      if (NS_FAILED(rv) || title.IsEmpty()) {        // yank out the filename from the url, use that        nsCOMPtr<nsIURI> aUri;        rv = NS_NewURI(getter_AddRefs(aUri), uri);        if (NS_FAILED(rv)) return rv;        nsCOMPtr<nsIURL> urlObj(do_QueryInterface(aUri));        if (!urlObj)            return NS_ERROR_FAILURE;        nsCAutoString filename;        rv = urlObj->GetFileName(filename);        if (NS_FAILED(rv) || filename.IsEmpty()) {          // ok fine. we'll use the file path. then we give up!          rv = urlObj->GetPath(filename);          if (strcmp(filename.get(), "/") == 0) {            // if the top of a site does not have a title            // (common for redirections) then return the hostname            return GetTarget(aSource, kNC_Hostname, aTruthValue, aTarget);                      }        }        if (NS_FAILED(rv)) return rv;                // assume the url is in UTF8        title = NS_ConvertUTF8toUCS2(filename);      }      if (NS_FAILED(rv)) return rv;      nsCOMPtr<nsIRDFLiteral> name;      rv = gRDFService->GetLiteral(title.get(), getter_AddRefs(name));      if (NS_FAILED(rv)) return rv;      return CallQueryInterface(name, aTarget);    }    else if (aProperty == kNC_Hostname ||             aProperty == kNC_Referrer) {            nsCAutoString str;      if (aProperty == kNC_Hostname)        rv = GetRowValue(row, kToken_HostnameColumn, str);      else if (aProperty == kNC_Referrer)        rv = GetRowValue(row, kToken_ReferrerColumn, str);            if (NS_FAILED(rv)) return rv;            nsCOMPtr<nsIRDFResource> resource;      rv = gRDFService->GetResource(str,                                    getter_AddRefs(resource));      if (NS_FAILED(rv)) return rv;      return CallQueryInterface(resource, aTarget);    }    else {      NS_NOTREACHED("huh, how'd I get here?");    }  }  return NS_RDF_NO_VALUE;}