NS_IMETHODIMP nsAutoSyncState::OnStopRunningUrl(nsIURI* aUrl, nsresult aExitCode)
{
  nsresult rv;
  nsCOMPtr <nsIMsgFolder> ownerFolder = do_QueryReferent(mOwnerFolder, &rv);
  NS_ENSURE_SUCCESS(rv, rv);
  nsCOMPtr<nsIAutoSyncManager> autoSyncMgr = do_GetService(NS_AUTOSYNCMANAGER_CONTRACTID, &rv);
  NS_ENSURE_SUCCESS(rv, rv);
  nsCOMPtr<nsIUrlListener> autoSyncMgrListener = do_QueryInterface(autoSyncMgr, &rv);
  NS_ENSURE_SUCCESS(rv, rv);

  if (mSyncState == stStatusIssued)
  {
    nsCOMPtr <nsIMsgImapMailFolder> imapFolder = do_QueryReferent(mOwnerFolder, &rv);
    NS_ENSURE_SUCCESS(rv, rv);
    int32_t serverTotal, serverUnseen, serverRecent, serverNextUID;
    imapFolder->GetServerTotal(&serverTotal);
    imapFolder->GetServerUnseen(&serverUnseen);
    imapFolder->GetServerRecent(&serverRecent);
    imapFolder->GetServerNextUID(&serverNextUID);
    if (serverNextUID != mLastNextUID || serverTotal != mLastServerTotal ||
        serverUnseen != mLastServerUnseen || serverRecent != mLastServerRecent)
    {
      nsCString folderName;
      ownerFolder->GetURI(folderName);
      PR_LOG(gAutoSyncLog, PR_LOG_DEBUG,
             ("folder %s status changed serverNextUID = %lx lastNextUID = %lx\n", folderName.get(),
              serverNextUID, mLastNextUID));
      PR_LOG(gAutoSyncLog, PR_LOG_DEBUG,
             ("serverTotal = %lx lastServerTotal = %lx serverRecent = %lx lastServerRecent = %lx\n",
              serverTotal, mLastServerTotal, serverRecent, mLastServerRecent));
      SetServerCounts(serverTotal, serverRecent, serverUnseen, serverNextUID);
      SetState(nsAutoSyncState::stUpdateIssued);
      return imapFolder->UpdateFolderWithListener(nullptr, autoSyncMgrListener);
    }
    else
    {
      ownerFolder->SetMsgDatabase(nullptr);
      // nothing more to do.
      SetState(nsAutoSyncState::stCompletedIdle);
      // autoSyncMgr needs this notification, so manufacture it.
      return autoSyncMgrListener->OnStopRunningUrl(nullptr, NS_OK);
    }
  }
  //XXXemre how we recover from this error?
  rv = ownerFolder->ReleaseSemaphore(ownerFolder);
  NS_ASSERTION(NS_SUCCEEDED(rv), "*** Cannot release folder semaphore");

  nsCOMPtr<nsIMsgMailNewsUrl> mailUrl = do_QueryInterface(aUrl);
  if (mailUrl)
    rv = mailUrl->UnRegisterListener(this);

  return autoSyncMgr->OnDownloadCompleted(this, aExitCode);
}
void CFolderJoin::UpdateServers(LTBOOL bForce)
{
	// Determine if our selection has changed...

	int nOldSel   = m_pServerList->GetSelectedItem();
	int nOldStart = m_pServerList->GetStartIndex();


	// Determine if any data changed...

	if (!bForce)
	{
		return;
	}


	// Get our filters...

	int nGameFilter = GetGameFilter();
	int nPopFilter  = GetPopFilter();

	char sVer[16];
	sprintf(sVer,"%d.%.3d",GAME_HANDSHAKE_VER_MAJOR,(GAME_HANDSHAKE_VER_MINOR-1));


	// Get the previous selected item...

	void*  pCurHandle = GetCurGameServerHandle();
    LTBOOL bSelSet    = LTFALSE;


	// Rebuild the entire list...

	m_pServerList->RemoveAllControls();

    CGameSpyServer* pGame = GetGameSpyClientMgr()->GetFirstServer();
	int nIndex = -1;

	while (pGame && pGame->IsInitialized())
	{
		// Check our filters...

        LTBOOL bPass = LTTRUE;

		if (0 == pGame->GetMaxPlayers())
			bPass = LTFALSE;

		if (m_nVersionFilter)
		{
			char* sGameVer = pGame->GetStringValue("gamever");
			if (sGameVer && stricmp(sVer,sGameVer)!=0)
				bPass = LTFALSE;
		}

		if (nGameFilter != NGT_FILTER_ALL)
		{
			char* sType = pGame->GetGameType();
			int   nType = -1;

			if (stricmp(sType, "deathmatch") == 0) nType = DEATHMATCH;
			if (stricmp(sType, "H.A.R.M. vs. UNITY") == 0) nType = COOPERATIVE_ASSAULT;
            if (nType != nGameFilter) bPass = LTFALSE;
		}

		if (nPopFilter != POP_FILTER_ALL)
		{
			int nPlrs = pGame->GetNumPlayers();
			int nMax  = pGame->GetMaxPlayers();

			switch (nPopFilter)
			{
                case POP_FILTER_NOTEMPTY: if (nPlrs == 0) bPass = LTFALSE; break;
                case POP_FILTER_NOTFULL:  if (nPlrs >= nMax) bPass = LTFALSE; break;
                case POP_FILTER_NOTBOTH:  if (nPlrs == 0 || nPlrs >= nMax) bPass = LTFALSE; break;
			}
		}


		if (!bPass)
		{
            pGame = GetGameSpyClientMgr()->GetNextServer();
			continue;
		}


		// Add a new control for this game service...

		nIndex = AddServerCtrl(pGame);


		// Update the current selection...

		if (pGame->GetHandle() == pCurHandle)
		{
			m_pServerList->SelectItem(nIndex);
            bSelSet = LTTRUE;
		}

		// Get the next game service...

        pGame = GetGameSpyClientMgr()->GetNextServer();
	}


	// Update our current game service if necessary...

	if (bSelSet)
	{
		int nOldOffset = nOldSel - nOldStart;
		int nNewSel = m_pServerList->GetSelectedItem();
		int nNewStart = m_pServerList->GetStartIndex();

		int nStart = m_pServerList->GetSelectedItem() - nOldOffset;
		if (nStart >= 0)
			m_pServerList->SetStartIndex(nStart);

		void* pHandle = (void*)m_pServerList->GetControl(nNewSel)->GetParam1();
		SetCurGameServerHandle(pHandle);
	}
	else
	{
		m_pServerList->SelectItem(CListCtrl::kNoSelection);
        SetCurGameServerHandle(LTNULL);
	}


	// Update our server counts...

    uint32 dwNumServers = GetGameSpyClientMgr()->GetNumServers();
	SetServerCounts(dwNumServers, nIndex+1);
}