nsresult
sbWatchFolderService::HandleRootPathMissing()
{
  // If the unit tests are running, don't show the dialog (Don't bother
  // checking result of call too).
  nsresult rv;
  bool isUnitTestsRunning = PR_FALSE;
  mPrefMgr->GetIsUnitTestsRunning(&isUnitTestsRunning);
  if (isUnitTestsRunning) {
    return NS_OK;
  }

  sbStringBundle bundle;
  nsString dialogTitle = bundle.Get("watch_folder.root_path_missing.title");

  nsTArray<nsString> params;
  params.AppendElement(mWatchPath);
  nsString dialogText =
    bundle.Format("watch_folder.root_path_missing.text", params);

  nsCOMPtr<nsIDOMWindow> songbirdWindow;
  rv = GetSongbirdWindow(getter_AddRefs(songbirdWindow));
  NS_ENSURE_SUCCESS(rv, rv);

  nsCOMPtr<sbIPrompter> prompter =
    do_CreateInstance("@songbirdnest.com/Songbird/Prompter;1", &rv);
  NS_ENSURE_SUCCESS(rv, rv);

  rv = prompter->SetWaitForWindow(PR_TRUE);
  NS_ENSURE_SUCCESS(rv, rv);

  rv = prompter->Alert(songbirdWindow,
                       dialogTitle.BeginReading(),
                       dialogText.BeginReading());
  NS_ENSURE_SUCCESS(rv, rv);

  return NS_OK;
}
nsresult
sbWatchFolderService::HandleSessionLoadError()
{
  NS_ASSERTION(NS_IsMainThread(),
      "HandleSessionLoadError() not called on main thread!");
  NS_ENSURE_STATE(mFileSystemWatcher);

  // If the unit tests are running, don't show the dialog (Don't bother
  // checking result of call too).
  nsresult rv;
  bool isUnitTestsRunning = PR_FALSE;
  mPrefMgr->GetIsUnitTestsRunning(&isUnitTestsRunning);
  if (isUnitTestsRunning) {
    return NS_OK;
  }

  // If this method gets called, than the watcher could not load the stored
  // session. The tree will need to be re-initialized, this time without
  // loading a session.
  if (!mFileSystemWatcherGUID.IsEmpty()) {
    // Attempt the remove the session data. Don't bother returning the result
    // if it fails, just warn about it.
    rv = mFileSystemWatcher->DeleteSession(mFileSystemWatcherGUID);
    if (NS_FAILED(rv)) {
      NS_WARNING("Could not delete the bad session data file!");
    }

    mFileSystemWatcherGUID.Truncate();

    // Clear the GUID preference.
    nsCOMPtr<nsIPrefBranch2> prefBranch =
      do_GetService("@mozilla.org/preferences-service;1", &rv);
    NS_ENSURE_SUCCESS(rv, rv);

    rv = prefBranch->ClearUserPref(PREF_WATCHFOLDER_SESSIONGUID);
    NS_ENSURE_SUCCESS(rv, rv);
  }

  rv = mFileSystemWatcher->Init(this, mWatchPath, PR_TRUE);
  NS_ENSURE_SUCCESS(rv, rv);

  rv = mFileSystemWatcher->StartWatching();
  NS_ENSURE_SUCCESS(rv, rv);

  sbStringBundle bundle;
  nsString dialogTitle =
    bundle.Get("watch_folder.session_load_error.rescan_title");

  nsTArray<nsString> params;
  params.AppendElement(mWatchPath);
  nsString dialogText =
    bundle.Format("watch_folder.session_load_error.rescan_text", params);

  nsCOMPtr<nsIDOMWindow> songbirdWindow;
  rv = GetSongbirdWindow(getter_AddRefs(songbirdWindow));
  NS_ENSURE_SUCCESS(rv, rv);

  nsCOMPtr<sbIPrompter> prompter =
    do_CreateInstance("@songbirdnest.com/Songbird/Prompter;1", &rv);
  NS_ENSURE_SUCCESS(rv, rv);

  rv = prompter->SetWaitForWindow(PR_TRUE);
  NS_ENSURE_SUCCESS(rv, rv);

  bool shouldRescan = PR_FALSE;
  prompter->Confirm(songbirdWindow,
                    dialogTitle.BeginReading(),
                    dialogText.BeginReading(),
                    &shouldRescan);

  // Only start the scan if the user picked the YES button (option 0).
  if (shouldRescan) {
    // The user elected to rescan their watched directory. Setup the directory
    // scan service.
    nsCOMPtr<sbIDirectoryImportService> dirImportService =
      do_GetService("@songbirdnest.com/Songbird/DirectoryImportService;1", &rv);
    NS_ENSURE_SUCCESS(rv, rv);

    // The directory import service wants the paths as an array.
    nsCOMPtr<nsILocalFile> watchPathFile =
      do_CreateInstance("@mozilla.org/file/local;1", &rv);
    NS_ENSURE_SUCCESS(rv, rv);

    rv = watchPathFile->InitWithPath(mWatchPath);
    NS_ENSURE_SUCCESS(rv, rv);

    nsCOMPtr<nsIMutableArray> dirArray =
      do_CreateInstance("@songbirdnest.com/moz/xpcom/threadsafe-array;1", &rv);

    rv = dirArray->AppendElement(watchPathFile, PR_FALSE);
    NS_ENSURE_SUCCESS(rv, rv);

    nsCOMPtr<sbIDirectoryImportJob> importJob;
    rv = dirImportService->Import(dirArray,
                                  nsnull,  // defaults to main library
                                  -1,
                                  getter_AddRefs(importJob));
    NS_ENSURE_SUCCESS(rv, rv);

    nsCOMPtr<sbIJobProgressService> progressService =
      do_GetService("@songbirdnest.com/Songbird/JobProgressService;1", &rv);
    if (NS_SUCCEEDED(rv) && progressService) {
      nsCOMPtr<sbIJobProgress> job = do_QueryInterface(importJob, &rv);
      NS_ENSURE_SUCCESS(rv, rv);

      rv = progressService->ShowProgressDialog(job, nsnull, 1);
      NS_ENSURE_SUCCESS(rv, rv);
    }
  }

  return NS_OK;
}
nsresult
sbWatchFolder::HandleSessionLoadError()
{
  NS_ASSERTION(NS_IsMainThread(),
      "HandleSessionLoadError() not called on main thread!");
  NS_ENSURE_STATE(mFileSystemWatcher);

  // If the unit tests are running, don't show the dialog (Don't bother
  // checking result of call too).
  nsresult rv;
  if (!mCanInteract) {
    return NS_OK;
  }

  // If this method gets called, than the watcher could not load the stored
  // session. The tree will need to be re-initialized, this time without
  // loading a session.
  if (!mFileSystemWatcherGUID.IsEmpty()) {
    // Attempt the remove the session data. Don't bother returning the result
    // if it fails, just warn about it.
    rv = mFileSystemWatcher->DeleteSession(mFileSystemWatcherGUID);
    if (NS_FAILED(rv)) {
      NS_WARNING("Could not delete the bad session data file!");
    }

    mFileSystemWatcherGUID.Truncate();
  }

  rv = mFileSystemWatcher->Init(this, mWatchPath, PR_TRUE);
  NS_ENSURE_SUCCESS(rv, rv);

  rv = mFileSystemWatcher->StartWatching();
  NS_ENSURE_SUCCESS(rv, rv);

  sbStringBundle bundle;
  nsString dialogTitle =
    bundle.Get("watch_folder.session_load_error.rescan_title");

  nsTArray<nsString> params;
  params.AppendElement(mWatchPath);
  nsString dialogText =
    bundle.Format("watch_folder.session_load_error.rescan_text", params);

  nsCOMPtr<nsIDOMWindow> songbirdWindow;
  rv = GetSongbirdWindow(getter_AddRefs(songbirdWindow));
  NS_ENSURE_SUCCESS(rv, rv);

  nsCOMPtr<sbIPrompter> prompter =
    do_CreateInstance("@songbirdnest.com/Songbird/Prompter;1", &rv);
  NS_ENSURE_SUCCESS(rv, rv);

  rv = prompter->SetWaitForWindow(PR_TRUE);
  NS_ENSURE_SUCCESS(rv, rv);

  PRBool shouldRescan = PR_FALSE;
  prompter->Confirm(songbirdWindow,
                    dialogTitle.BeginReading(),
                    dialogText.BeginReading(),
                    &shouldRescan);

  // Only start the scan if the user picked the YES button (option 0).
  if (shouldRescan) {
      // The user elected to rescan their watched directory.
      rv = Rescan();
      NS_ENSURE_SUCCESS(rv, rv);
  }

  return NS_OK;
}