Exemplo n.º 1
0
void
nsTypeAheadFind::PlayNotFoundSound()
{
  if (mNotFoundSoundURL.IsEmpty())    // no sound
    return;

  if (!mSoundInterface)
    mSoundInterface = do_CreateInstance("@mozilla.org/sound;1");

  if (mSoundInterface) {
    mIsSoundInitialized = true;

    if (mNotFoundSoundURL.EqualsLiteral("beep")) {
      mSoundInterface->Beep();
      return;
    }

    nsCOMPtr<nsIURI> soundURI;
    if (mNotFoundSoundURL.EqualsLiteral("default"))
      NS_NewURI(getter_AddRefs(soundURI), NS_LITERAL_CSTRING(TYPEAHEADFIND_NOTFOUND_WAV_URL));
    else
      NS_NewURI(getter_AddRefs(soundURI), mNotFoundSoundURL);

    nsCOMPtr<nsIURL> soundURL(do_QueryInterface(soundURI));
    if (soundURL)
      mSoundInterface->Play(soundURL);
  }
}
NS_IMETHODIMP
nsDownload::OnStateChange(nsIWebProgress* aWebProgress,
                          nsIRequest* aRequest, PRUint32 aStateFlags,
                          nsresult aStatus)
{
    // Record the start time only if it hasn't been set.
    if (LL_IS_ZERO(mStartTime) && (aStateFlags & STATE_START))
        SetStartTime(PR_Now());

    // When we break the ref cycle with mPersist, we don't want to lose
    // access to out member vars!
    nsRefPtr<nsDownload> kungFuDeathGrip(this);

    // We need to update mDownloadState before updating the dialog, because
    // that will close and call CancelDownload if it was the last open window.
    nsresult rv = NS_OK;
    if (aStateFlags & STATE_STOP) {
        if (mDownloadState == DOWNLOADING || mDownloadState == NOTSTARTED) {
            mDownloadState = FINISHED;

            // Set file size at the end of a transfer (for unknown transfer amounts)
            if (mMaxBytes == -1)
                mMaxBytes = mCurrBytes;

            // Files less than 1Kb shouldn't show up as 0Kb.
            if (mMaxBytes < 1024) {
                mCurrBytes = 1024;
                mMaxBytes  = 1024;
            }

            mPercentComplete = 100;

            // Play a sound or show an alert when the download finishes
            PRBool playSound = PR_FALSE;
            PRBool showAlert = PR_FALSE;
            nsXPIDLCString soundStr;

            nsCOMPtr<nsIPrefService> prefs = do_GetService("@mozilla.org/preferences-service;1");

            if (prefs) {
                nsCOMPtr<nsIPrefBranch> prefBranch;
                prefs->GetBranch(nsnull, getter_AddRefs(prefBranch));
                if (prefBranch) {
                    rv = prefBranch->GetBoolPref("browser.download.finished_download_sound", &playSound);
                    if (NS_SUCCEEDED(rv) && playSound)
                        prefBranch->GetCharPref("browser.download.finished_sound_url", getter_Copies(soundStr));
                    rv = prefBranch->GetBoolPref("browser.download.finished_download_alert", &showAlert);
                    if (NS_FAILED(rv))
                        showAlert = PR_FALSE;
                }
            }

            if (!soundStr.IsEmpty()) {
                if (!mDownloadManager->mSoundInterface) {
                    mDownloadManager->mSoundInterface = do_CreateInstance("@mozilla.org/sound;1");
                }
                if (mDownloadManager->mSoundInterface) {
                    nsCOMPtr<nsIURI> soundURI;

                    NS_NewURI(getter_AddRefs(soundURI), soundStr);
                    nsCOMPtr<nsIURL> soundURL(do_QueryInterface(soundURI));
                    if (soundURL)
                        mDownloadManager->mSoundInterface->Play(soundURL);
                    else
                        mDownloadManager->mSoundInterface->Beep();
                }
            }
            if (showAlert)
                DisplayDownloadFinishedAlert();

            nsCAutoString path;
            rv = GetFilePathUTF8(mTarget, path);
            // can't do an early return; have to break reference cycle below
            if (NS_SUCCEEDED(rv)) {
                mDownloadManager->DownloadEnded(path, nsnull);
            }
        }

        // break the cycle we created in AddDownload
        mCancelable = nsnull;
        // and the one with the progress dialog
        if (mDialog) {
            mDialog->SetObserver(nsnull);
            mDialog = nsnull;
        }
    }

    if (mDownloadManager->MustUpdateUI()) {
        nsCOMPtr<nsIDownloadProgressListener> internalListener;
        mDownloadManager->GetInternalListener(getter_AddRefs(internalListener));
        if (internalListener)
            internalListener->OnStateChange(aWebProgress, aRequest, aStateFlags, aStatus, this);
    }

    if (mDialogListener) {
        mDialogListener->OnStateChange(aWebProgress, aRequest, aStateFlags, aStatus, this);
        if (aStateFlags & STATE_STOP) {
            // Break this cycle, too
            mDialogListener = nsnull;
        }
    }

    return rv;
}