nsBookmarksService::OnStopRequest(nsIRequest* request, nsISupports *ctxt, nsresult status)
nsBookmarksService::OnStopRequest(nsIChannel* channel, nsISupports *ctxt, nsresult status, const PRUnichar *errorMsg)
nsBookmarksService::OnStopRequest(nsIRequest* request, nsISupports *ctxt,                    nsresult status){    nsresult rv;    nsAutoString url;    if (NS_SUCCEEDED(rv = GetURLFromResource(busyResource, url)))    {#ifdef  DEBUG_BOOKMARK_PING_OUTPUT        printf("Finished polling '%s'\n", NS_ConvertUCS2toUTF8(url).get());#endif    }    nsCOMPtr<nsIChannel> channel = do_QueryInterface(request);    nsCOMPtr<nsIHttpChannel>    httpChannel = do_QueryInterface(channel);    if (httpChannel)    {        nsAutoString            eTagValue, lastModValue, contentLengthValue;        nsCAutoString val;        if (NS_SUCCEEDED(httpChannel->GetResponseHeader(NS_LITERAL_CSTRING("ETag"), val)))            CopyASCIItoUCS2(val, eTagValue);        if (NS_SUCCEEDED(httpChannel->GetResponseHeader(NS_LITERAL_CSTRING("Last-Modified"), val)))            CopyASCIItoUCS2(val, lastModValue);        if (NS_SUCCEEDED(httpChannel->GetResponseHeader(NS_LITERAL_CSTRING("Content-Length"), val)))            CopyASCIItoUCS2(val, contentLengthValue);        val.Truncate();        PRBool      changedFlag = PR_FALSE;        PRUint32    respStatus;        if (NS_SUCCEEDED(rv = httpChannel->GetResponseStatus(&respStatus)))        {            if ((respStatus >= 200) && (respStatus <= 299))            {                if (!eTagValue.IsEmpty())                {#ifdef  DEBUG_BOOKMARK_PING_OUTPUT                    printf("eTag: '%s'\n", NS_ConvertUCS2toUTF8(eTagValue).get());#endif                    nsAutoString        eTagStr;                    nsCOMPtr<nsIRDFNode>    currentETagNode;                    if (NS_SUCCEEDED(rv = mInner->GetTarget(busyResource, kWEB_LastPingETag,                        PR_TRUE, getter_AddRefs(currentETagNode))) && (rv != NS_RDF_NO_VALUE))                    {                        nsCOMPtr<nsIRDFLiteral> currentETagLit = do_QueryInterface(currentETagNode);                        if (currentETagLit)                        {                            const PRUnichar* currentETagStr = nsnull;                            currentETagLit->GetValueConst(&currentETagStr);                            if ((currentETagStr) &&                                !eTagValue.Equals(nsDependentString(currentETagStr),                                                  nsCaseInsensitiveStringComparator()))                            {                                changedFlag = PR_TRUE;                            }                            eTagStr.Assign(eTagValue);                            nsCOMPtr<nsIRDFLiteral> newETagLiteral;                            if (NS_SUCCEEDED(rv = gRDF->GetLiteral(eTagStr.get(),                                getter_AddRefs(newETagLiteral))))                            {                                rv = mInner->Change(busyResource, kWEB_LastPingETag,                                    currentETagNode, newETagLiteral);                            }                        }                    }                    else                    {                        eTagStr.Assign(eTagValue);                        nsCOMPtr<nsIRDFLiteral> newETagLiteral;                        if (NS_SUCCEEDED(rv = gRDF->GetLiteral(eTagStr.get(),                            getter_AddRefs(newETagLiteral))))                        {                            rv = mInner->Assert(busyResource, kWEB_LastPingETag,                                newETagLiteral, PR_TRUE);                        }                    }                }            }        }        if ((changedFlag == PR_FALSE) && (!lastModValue.IsEmpty()))        {#ifdef  DEBUG_BOOKMARK_PING_OUTPUT            printf("Last-Modified: '%s'\n", lastModValue.get());#endif            nsAutoString        lastModStr;            nsCOMPtr<nsIRDFNode>    currentLastModNode;            if (NS_SUCCEEDED(rv = mInner->GetTarget(busyResource, kWEB_LastPingModDate,                PR_TRUE, getter_AddRefs(currentLastModNode))) && (rv != NS_RDF_NO_VALUE))            {                nsCOMPtr<nsIRDFLiteral> currentLastModLit = do_QueryInterface(currentLastModNode);                if (currentLastModLit)                {                    const PRUnichar* currentLastModStr = nsnull;                    currentLastModLit->GetValueConst(&currentLastModStr);                    if ((currentLastModStr) &&                        !lastModValue.Equals(nsDependentString(currentLastModStr),                                             nsCaseInsensitiveStringComparator()))                    {                        changedFlag = PR_TRUE;                    }                    lastModStr.Assign(lastModValue);                    nsCOMPtr<nsIRDFLiteral> newLastModLiteral;                    if (NS_SUCCEEDED(rv = gRDF->GetLiteral(lastModStr.get(),                        getter_AddRefs(newLastModLiteral))))                    {                        rv = mInner->Change(busyResource, kWEB_LastPingModDate,                            currentLastModNode, newLastModLiteral);                    }                }            }            else            {                lastModStr.Assign(lastModValue);                nsCOMPtr<nsIRDFLiteral> newLastModLiteral;                if (NS_SUCCEEDED(rv = gRDF->GetLiteral(lastModStr.get(),                    getter_AddRefs(newLastModLiteral))))                {                    rv = mInner->Assert(busyResource, kWEB_LastPingModDate,                        newLastModLiteral, PR_TRUE);                }            }        }        if ((changedFlag == PR_FALSE) && (!contentLengthValue.IsEmpty()))        {#ifdef  DEBUG_BOOKMARK_PING_OUTPUT            printf("Content-Length: '%s'\n", contentLengthValue.get());#endif            nsAutoString        contentLenStr;            nsCOMPtr<nsIRDFNode>    currentContentLengthNode;            if (NS_SUCCEEDED(rv = mInner->GetTarget(busyResource, kWEB_LastPingContentLen,                PR_TRUE, getter_AddRefs(currentContentLengthNode))) && (rv != NS_RDF_NO_VALUE))            {                nsCOMPtr<nsIRDFLiteral> currentContentLengthLit = do_QueryInterface(currentContentLengthNode);                if (currentContentLengthLit)                {                    const PRUnichar *currentContentLengthStr = nsnull;                    currentContentLengthLit->GetValueConst(&currentContentLengthStr);                    if ((currentContentLengthStr) &&                        !contentLengthValue.Equals(nsDependentString(currentContentLengthStr),                                                   nsCaseInsensitiveStringComparator()))                    {                        changedFlag = PR_TRUE;                    }                    contentLenStr.Assign(contentLengthValue);                    nsCOMPtr<nsIRDFLiteral> newContentLengthLiteral;                    if (NS_SUCCEEDED(rv = gRDF->GetLiteral(contentLenStr.get(),                        getter_AddRefs(newContentLengthLiteral))))                    {                        rv = mInner->Change(busyResource, kWEB_LastPingContentLen,                            currentContentLengthNode, newContentLengthLiteral);                    }                }            }            else            {                contentLenStr.Assign(contentLengthValue);                nsCOMPtr<nsIRDFLiteral> newContentLengthLiteral;                if (NS_SUCCEEDED(rv = gRDF->GetLiteral(contentLenStr.get(),                    getter_AddRefs(newContentLengthLiteral))))                {                    rv = mInner->Assert(busyResource, kWEB_LastPingContentLen,                        newContentLengthLiteral, PR_TRUE);                }            }        }        // update last poll date        nsCOMPtr<nsIRDFDate>    dateLiteral;        if (NS_SUCCEEDED(rv = gRDF->GetDateLiteral(PR_Now(), getter_AddRefs(dateLiteral))))        {            nsCOMPtr<nsIRDFNode>    lastPingNode;            if (NS_SUCCEEDED(rv = mInner->GetTarget(busyResource, kWEB_LastPingDate, PR_TRUE,                getter_AddRefs(lastPingNode))) && (rv != NS_RDF_NO_VALUE))            {                rv = mInner->Change(busyResource, kWEB_LastPingDate, lastPingNode, dateLiteral);            }            else            {                rv = mInner->Assert(busyResource, kWEB_LastPingDate, dateLiteral, PR_TRUE);            }            NS_ASSERTION(rv == NS_RDF_ASSERTION_ACCEPTED, "unable to assert new time");//          mDirty = PR_TRUE;        }        else        {            NS_ERROR("unable to get date literal for now");        }        // If its changed, set the appropriate info        if (changedFlag == PR_TRUE)        {#ifdef  DEBUG_BOOKMARK_PING_OUTPUT            printf("URL has changed!\n\n");#endif            nsAutoString        schedule;            nsCOMPtr<nsIRDFNode>    scheduleNode;            if (NS_SUCCEEDED(rv = mInner->GetTarget(busyResource, kWEB_Schedule, PR_TRUE,                getter_AddRefs(scheduleNode))) && (rv != NS_RDF_NO_VALUE))            {                nsCOMPtr<nsIRDFLiteral> scheduleLiteral = do_QueryInterface(scheduleNode);                if (scheduleLiteral)                {                    const PRUnichar     *scheduleUni = nsnull;                    if (NS_SUCCEEDED(rv = scheduleLiteral->GetValueConst(&scheduleUni))                        && (scheduleUni))                    {                        schedule = scheduleUni;                    }                }            }            // update icon?            if (FindInReadable(NS_LITERAL_STRING("icon"),                               schedule,                               nsCaseInsensitiveStringComparator()))            {                nsCOMPtr<nsIRDFLiteral> statusLiteral;                if (NS_SUCCEEDED(rv = gRDF->GetLiteral(NS_LITERAL_STRING("new").get(), getter_AddRefs(statusLiteral))))                {                    nsCOMPtr<nsIRDFNode>    currentStatusNode;                    if (NS_SUCCEEDED(rv = mInner->GetTarget(busyResource, kWEB_Status, PR_TRUE,                        getter_AddRefs(currentStatusNode))) && (rv != NS_RDF_NO_VALUE))                    {                        rv = mInner->Change(busyResource, kWEB_Status, currentStatusNode, statusLiteral);                    }                    else                    {                        rv = mInner->Assert(busyResource, kWEB_Status, statusLiteral, PR_TRUE);                    }                    NS_ASSERTION(rv == NS_RDF_ASSERTION_ACCEPTED, "unable to assert changed status");                }            }                        // play a sound?            if (FindInReadable(NS_LITERAL_STRING("sound"),                               schedule,                               nsCaseInsensitiveStringComparator()))            {                nsCOMPtr<nsISound> soundInterface =                        do_CreateInstance(kSoundCID, &rv);                if (NS_SUCCEEDED(rv))                {                    // for the moment, just beep                    soundInterface->Beep();                }            }                        PRBool      openURLFlag = PR_FALSE;            // show an alert?            if (FindInReadable(NS_LITERAL_STRING("alert"),                               schedule,                               nsCaseInsensitiveStringComparator()))            {                nsCOMPtr<nsIInterfaceRequestor> interfaces;                nsCOMPtr<nsIPrompt> prompter;                channel->GetNotificationCallbacks(getter_AddRefs(interfaces));                if (interfaces)                    interfaces->GetInterface(NS_GET_IID(nsIPrompt), getter_AddRefs(prompter));                if (!prompter)                {                    nsCOMPtr<nsIWindowWatcher> wwatch(do_GetService(NS_WINDOWWATCHER_CONTRACTID));                    if (wwatch)                        wwatch->GetNewPrompter(0, getter_AddRefs(prompter));                }                if (prompter)                {                    nsAutoString    promptStr;                    getLocaleString("WebPageUpdated", promptStr);                    if (!promptStr.IsEmpty())   promptStr.AppendLiteral("\n\n");                    nsCOMPtr<nsIRDFNode>    nameNode;                    if (NS_SUCCEEDED(mInner->GetTarget(busyResource, kNC_Name,                        PR_TRUE, getter_AddRefs(nameNode))))                    {                        nsCOMPtr<nsIRDFLiteral> nameLiteral = do_QueryInterface(nameNode);                        if (nameLiteral)                        {                            const PRUnichar *nameUni = nsnull;                            if (NS_SUCCEEDED(rv = nameLiteral->GetValueConst(&nameUni))                                && (nameUni))                            {                                nsAutoString    info;                                getLocaleString("WebPageTitle", info);                                promptStr += info;                                promptStr.AppendLiteral(" ");                                promptStr += nameUni;                                promptStr.AppendLiteral("\n");                                getLocaleString("WebPageURL", info);                                promptStr += info;                                promptStr.AppendLiteral(" ");                            }                        }                    }                    promptStr.Append(url);                                        nsAutoString    temp;                    getLocaleString("WebPageAskDisplay", temp);                    if (!temp.IsEmpty())                    {                        promptStr.AppendLiteral("\n\n");                        promptStr += temp;                    }                    nsAutoString    stopOption;                    getLocaleString("WebPageAskStopOption", stopOption);                    PRBool      stopCheckingFlag = PR_FALSE;                    rv = prompter->ConfirmCheck(nsnull, promptStr.get(), stopOption.get(),                                  &stopCheckingFlag, &openURLFlag);                    if (NS_FAILED(rv))                    {                        openURLFlag = PR_FALSE;                        stopCheckingFlag = PR_FALSE;                    }                    if (stopCheckingFlag == PR_TRUE)                    {#ifdef  DEBUG_BOOKMARK_PING_OUTPUT                        printf("\nStop checking this URL.\n");#endif                        rv = mInner->Unassert(busyResource, kWEB_Schedule, scheduleNode);                        NS_ASSERTION(NS_SUCCEEDED(rv), "unable to unassert kWEB_Schedule");                    }                }            }            // open the URL in a new window?            if ((openURLFlag == PR_TRUE) ||                FindInReadable(NS_LITERAL_STRING("open"),                               schedule,                               nsCaseInsensitiveStringComparator()))            {                if (NS_SUCCEEDED(rv))                {                    nsCOMPtr<nsIWindowWatcher> wwatch(do_GetService(NS_WINDOWWATCHER_CONTRACTID));                    if (wwatch)                    {                        nsCOMPtr<nsIDOMWindow> newWindow;            nsCOMPtr<nsISupportsArray> suppArray;            rv = NS_NewISupportsArray(getter_AddRefs(suppArray));            if (NS_FAILED(rv)) return rv;            nsCOMPtr<nsISupportsString> suppString(do_CreateInstance("@mozilla.org/supports-string;1", &rv));            if (!suppString) return rv;            rv = suppString->SetData(url);            if (NS_FAILED(rv)) return rv;            suppArray->AppendElement(suppString);                nsCOMPtr<nsICmdLineHandler> handler(do_GetService("@mozilla.org/commandlinehandler/general-startup;1?type=browser", &rv));                if (NS_FAILED(rv)) return rv;               nsXPIDLCString chromeUrl;            rv = handler->GetChromeUrlForTask(getter_Copies(chromeUrl));            if (NS_FAILED(rv)) return rv;            wwatch->OpenWindow(0, chromeUrl, "_blank", "chrome,dialog=no,all",                                suppArray, getter_AddRefs(newWindow));                    }                }            }        }#ifdef  DEBUG_BOOKMARK_PING_OUTPUT        else        {            printf("URL has not changed status.\n\n");        }#endif    }    busyResource = nsnull;    busySchedule = PR_FALSE;    return NS_OK;}