BOOL COSMCtrlMapOperationsDlg::ForceMapnikRerenderHelper()
{
  //Validate our parameters
  AFXASSUME(m_pOSMCtrl != NULL);

  //Next get the server to connect to
  COSMCtrlMapnikTileProvider MapnikTileProvider;
  CStringW sServer(MapnikTileProvider.GetDownloadServer());

  //Accumulate how many tiles we have request to rerender and which ones indicated a failure to rerender
  int nTilesRerendered = 0;
  int nTilesNotRerendered = 0;

  //Next create the WinHTTP session object
  CWinHTTPSession session;
  HRESULT hr = m_pOSMCtrl->CreateSession(session, 0);
  if (SUCCEEDED(hr))
  {
    //Now create the connection object from the session object
    CWinHTTPConnection connection;
    hr = connection.Initialize(session, sServer, MapnikTileProvider.GetDownloadPort());
    if (SUCCEEDED(hr))
    {
      //Iterate across the array of tiles to download
      BOOL bSuccess = TRUE;
      for (std::vector<COSMCtrlMapOperationsDlgTile>::size_type i=0; i<m_Tiles.size() && bSuccess; i++)
      {
        //Pull out the next tile to download
        const COSMCtrlMapOperationsDlgTile& tile = m_Tiles[i];

        //Now issue the request to rerender
        COSMCtrlMapOperationsDlgEvent dlgEvent;
        dlgEvent.m_bSuccess = false;
        LPCWSTR pwszAcceptTypes[2];
        pwszAcceptTypes[0] = L"*/*";
        pwszAcceptTypes[1] = NULL;
        CSyncWinHTTPDownloader winHttpRequest;
        //winHttpRequest.m_sFileToDownloadInto = sFile;
        CString sObject(MapnikTileProvider.GetDownloadObject(tile.m_nZoom, tile.m_nTileX, tile.m_nTileY) + _T("/dirty"));
        hr = winHttpRequest.Initialize(connection, CStringW(sObject), NULL, NULL, NULL, pwszAcceptTypes, WINHTTP_FLAG_REFRESH);
        if (FAILED(hr))
        {
          //report the error
          TRACE(_T("COSMCtrlMapOperationsDlg::ForceMapnikRerenderHelper, Failed to create request for object \"%s\", Error:%08X\n"), sObject.operator LPCTSTR(), hr);
            
          //Update the stats
          ++nTilesNotRerendered;
        }
        else
        {
          hr = winHttpRequest.SendRequestSync();
          if (FAILED(hr))
          {
            //report the error
            TRACE(_T("COSMCtrlMapOperationsDlg::ForceMapnikRerenderHelper, Failed to send request for object \"%s\", Error:%08X\n"), sObject.operator LPCTSTR(), hr);
            
            //Update the stats
            ++nTilesNotRerendered;
          }
          else
          {
            CStringA sResponse;
            sResponse.Append(reinterpret_cast<LPCSTR>(winHttpRequest.m_Response.GetData()), static_cast<int>(winHttpRequest.m_Response.GetSize()));
            if (sResponse.Find("Tile submitted for rendering") != -1)
            {
              //Update the stats
              ++nTilesRerendered;
              dlgEvent.m_bSuccess = true;
            }
            else
            {
              //Update the stats
              ++nTilesNotRerendered;
            }  
          }
        }

        //Update the UI          
        dlgEvent.m_Event = COSMCtrlMapOperationsDlgEvent::SimpleStringStatus;
        dlgEvent.m_sString = sObject;
        dlgEvent.m_nItemData = i + 1;
        AddEvent(dlgEvent);

        //Check if we have been cancelled before we loop around
        bSuccess = (WaitForSingleObject(m_WorkerTerminateEvent, 0) == WAIT_TIMEOUT);
      }
    }
  }

  //Finally add a event about how many items have been downloaded
  COSMCtrlMapOperationsDlgEvent dlgEvent;
  dlgEvent.m_Event = COSMCtrlMapOperationsDlgEvent::SimpleStringStatus;
  CString sTilesRerendered;
  sTilesRerendered.Format(_T("%d"), nTilesRerendered);
  CString sTilesNotRerendered;
  sTilesNotRerendered.Format(_T("%d"), nTilesNotRerendered);
  AfxFormatString2(dlgEvent.m_sString, IDS_OSMCTRL_RERENDER_TILES_STATS, sTilesRerendered, sTilesNotRerendered);
  AddEvent(dlgEvent);

  return TRUE;
}
BOOL COSMCtrlMapOperationsDlg::DownloadTiles(BOOL bSkipIfTileAlreadyExists)
{
  //Validate our parameters
  AFXASSUME(m_pOSMCtrl != NULL);

  //Pull out the tile provider we will be using
  IOSMCtrlTileProvider* pTileProvider = m_pOSMCtrl->GetTileProvider();
  AFXASSUME(pTileProvider != NULL);

  //Next get the server to connect to
  CStringW sServer(pTileProvider->GetDownloadServer());

  //Accumulate how many tiles we have deleted and not deleted
  int nTilesDownloaded = 0;
  int nTilesNotDownloaded = 0;

  //Next create the WinHTTP session object
  CWinHTTPSession session;
  HRESULT hr = m_pOSMCtrl->CreateSession(session, 0);
  if (SUCCEEDED(hr))
  {
    //Now create the connection object from the session object
    CWinHTTPConnection connection;
    hr = connection.Initialize(session, sServer, pTileProvider->GetDownloadPort());
    if (SUCCEEDED(hr))
    {
      //Iterate across the array of tiles to download
      BOOL bSuccess = TRUE;
      for (std::vector<COSMCtrlMapOperationsDlgTile>::size_type i=0; i<m_Tiles.size() && bSuccess; i++)
      {
        //Pull out the next tile to download
        const COSMCtrlMapOperationsDlgTile& tile = m_Tiles[i];

        //Create the sub directories if we can
        CString sCacheDirectory(m_pOSMCtrl->GetCacheDirectory());
        CString sSubDirectory;
        sSubDirectory.Format(_T("%s\\%d"), sCacheDirectory.operator LPCTSTR(), tile.m_nZoom);
        CreateDirectory(sSubDirectory, NULL);
        sSubDirectory.Format(_T("%s\\%d\\%d"), sCacheDirectory.operator LPCTSTR(), tile.m_nZoom, tile.m_nTileX);
        CreateDirectory(sSubDirectory, NULL);

        //Form the path to the tile we will be downloading to and determine if we should do the download
        CString sFile(COSMCtrl::GetTileCachePath(sCacheDirectory, tile.m_nZoom, tile.m_nTileX, tile.m_nTileY, FALSE));
        BOOL bDownload = TRUE;
        if (bSkipIfTileAlreadyExists)
          bDownload = (GetFileAttributes(sFile) == INVALID_FILE_ATTRIBUTES);

        //Now download the specific tile to the cache if required
        COSMCtrlMapOperationsDlgEvent dlgEvent;
        dlgEvent.m_bSuccess = false;
        if (bDownload)
        {
          //We will accept any mime type
          LPCWSTR pwszAcceptTypes[2];
          pwszAcceptTypes[0] = L"*/*";
          pwszAcceptTypes[1] = NULL;
          CSyncWinHTTPDownloader winHttpRequest;
          winHttpRequest.m_sFileToDownloadInto = sFile;
          CString sObject(pTileProvider->GetDownloadObject(tile.m_nZoom, tile.m_nTileX, tile.m_nTileY));
          hr = winHttpRequest.Initialize(connection, CStringW(sObject), NULL, NULL, NULL, pwszAcceptTypes, WINHTTP_FLAG_REFRESH);
          if (FAILED(hr))
          {
            //report the error
            TRACE(_T("COSMCtrlMapOperationsDlg::DownloadTiles, Failed to create request for tile \"%s\", Error:%08X\n"), sFile.operator LPCTSTR(), hr);
            
            //Ensure any remants of a bad download file are nuked
            DeleteFile(sFile);
            
            //Update the stats
            ++nTilesNotDownloaded;
          }
          else
          {
            hr = winHttpRequest.SendRequestSync();
            if (FAILED(hr))
            {
              //report the error
              TRACE(_T("COSMCtrlMapOperationsDlg::DownloadTiles, Failed to send request for tile \"%s\", Error:%08X\n"), sFile.operator LPCTSTR(), hr);
            
              //Ensure any remants of a bad download file are nuked
              DeleteFile(sFile);
            
              //Update the stats
              ++nTilesNotDownloaded;
            }
            else
            {
              //Update the stats
              ++nTilesDownloaded;
              dlgEvent.m_bSuccess = true;
            }
          }
        }
        else
        {
          //Update the stats
          ++nTilesNotDownloaded;
        }

        //Update the UI          
        dlgEvent.m_Event = COSMCtrlMapOperationsDlgEvent::SimpleStringStatus;
        dlgEvent.m_sString = sFile;
        dlgEvent.m_nItemData = i + 1;
        AddEvent(dlgEvent);

        //Check if we have been cancelled before we loop around
        bSuccess = (WaitForSingleObject(m_WorkerTerminateEvent, 0) == WAIT_TIMEOUT);
      }
    }
  }

  //Finally add a event about how many items have been downloaded
  COSMCtrlMapOperationsDlgEvent dlgEvent;
  dlgEvent.m_Event = COSMCtrlMapOperationsDlgEvent::SimpleStringStatus;
  CString sTilesDownloaded;
  sTilesDownloaded.Format(_T("%d"), nTilesDownloaded);
  CString sTilesNotDownloaded;
  sTilesNotDownloaded.Format(_T("%d"), nTilesNotDownloaded);
  AfxFormatString2(dlgEvent.m_sString, IDS_OSMCTRL_DOWNLOAD_TILES_STATS, sTilesDownloaded, sTilesNotDownloaded);
  AddEvent(dlgEvent);

  return TRUE;
}
BOOL COSMCtrlMapOperationsDlg::DownloadTiles(BOOL bSkipIfTileAlreadyExists)
{
  //Validate our parameters
  AFXASSUME(m_pOSMCtrl);

  //What will be the return value from this function (assume the best)
  BOOL bSuccess = TRUE;

  CSingleLock sl(&m_pOSMCtrl->m_csData, TRUE);
  IOSMCtrlTileProvider* pTileProvider = m_pOSMCtrl->GetTileProvider();
  CString sCacheDirectory(m_pOSMCtrl->m_sCacheDirectory);
  ASSERT(sCacheDirectory.GetLength());
  BOOL bUseIfModifiedSinceHeader(m_pOSMCtrl->m_bUseIfModifiedSinceHeader);
  sl.Unlock();

  //Next get the server to connect to
  CString sServer(pTileProvider->GetDownloadServer());

  //Accumulate how many tiles we have deleted and not deleted
  int nTilesDownloaded = 0;
  int nTilesNotDownloaded = 0;

  //Next create the Wininet session object
  ASSERT(m_hSession == NULL);
  HRESULT hr = m_pOSMCtrl->CreateSession(m_hSession);
  if (SUCCEEDED(hr))
  {
    //Now create the connection object from the session object
    HINTERNET hConnection = NULL;
    hr = m_pOSMCtrl->CreateConnection(m_hSession, sServer, 80, hConnection);
    if (SUCCEEDED(hr))
    {
      //Iterate across the array of tiles to download
      for (INT_PTR i=0; i<m_Tiles.GetSize() && bSuccess; i++)
      {
        //Pull out the next tile to download
        const COSMCtrlMapOperationsDlgTile& tile = m_Tiles.ElementAt(i);

        //Create the sub directories if we can
        CString sSubDirectory;
        sSubDirectory.Format(_T("%s\\%d"), sCacheDirectory.operator LPCTSTR(), tile.m_nZoom);
        CreateDirectory(sSubDirectory, NULL);
        sSubDirectory.Format(_T("%s\\%d\\%d"), sCacheDirectory.operator LPCTSTR(), tile.m_nZoom, tile.m_nTileX);
        CreateDirectory(sSubDirectory, NULL);

        //Form the name of the tile we will be downloading
        CString sObject(pTileProvider->GetDownloadObject(tile.m_nZoom, tile.m_nTileX, tile.m_nTileY));

        //Form the path to the tile we will be downloading to  and determine if we should do the download
        CString sFile(COSMCtrl::GetTileCachePath(sCacheDirectory, tile.m_nZoom, tile.m_nTileX, tile.m_nTileY, FALSE));
        BOOL bDownload = TRUE;
        if (bSkipIfTileAlreadyExists)
          bDownload = (GetFileAttributes(sFile) == INVALID_FILE_ATTRIBUTES);

        //Now download the specific tile to the cache if required
        COSMCtrlMapOperationsDlgEvent dlgEvent;
        dlgEvent.m_bSuccess = false;
        if (bDownload)
        {
          hr = m_pOSMCtrl->DownloadTile(hConnection, sObject, bUseIfModifiedSinceHeader, !bSkipIfTileAlreadyExists, tile.m_nZoom, tile.m_nTileX, tile.m_nTileY, sFile);
          if (FAILED(hr))
          {
            //report the error
            TRACE(_T("COSMCtrlMapOperationsDlg::DownloadTiles, Failed to download tile \"%s\", Error:%08X\n"), sFile.operator LPCTSTR(), hr);
            
            //Ensure any remants of a bad download file are nuked
            DeleteFile(sFile);
            
            //Update the stats
            ++nTilesNotDownloaded;
          }
          else
          {
            //Update the stats
            ++nTilesDownloaded;
            dlgEvent.m_bSuccess = true;
          }
        }
        else
        {
          //Update the stats
          ++nTilesNotDownloaded;
        }

        //Update the UI          
        dlgEvent.m_Event = COSMCtrlMapOperationsDlgEvent::SimpleStringStatus;
        dlgEvent.m_sString = sFile;
        dlgEvent.m_nItemData = i + 1;
        AddEvent(dlgEvent);

        //Check if we have been cancelled before we loop around
        bSuccess = (WaitForSingleObject(m_WorkerTerminateEvent, 0) == WAIT_TIMEOUT);
      }
      
      //Close the wininet connection
    #ifdef COSMCTRL_NOWINHTTP
      InternetCloseHandle(hConnection);
    #else
      WinHttpCloseHandle(hConnection);
    #endif
    }

    //Clean up the wininet session before we exit
  #ifdef COSMCTRL_NOWINHTTP
    InternetCloseHandle(m_hSession);
  #else
    WinHttpCloseHandle(m_hSession);
  #endif
    m_hSession = NULL;
  }

  //Finally add a event about how many items have been downloaded
  COSMCtrlMapOperationsDlgEvent dlgEvent;
  dlgEvent.m_Event = COSMCtrlMapOperationsDlgEvent::SimpleStringStatus;
  CString sTilesDownloaded;
  sTilesDownloaded.Format(_T("%d"), nTilesDownloaded);
  CString sTilesNotDownloaded;
  sTilesNotDownloaded.Format(_T("%d"), nTilesNotDownloaded);
  AfxFormatString2(dlgEvent.m_sString, IDS_OSMCTRL_DOWNLOAD_TILES_STATS, sTilesDownloaded, sTilesNotDownloaded);
  AddEvent(dlgEvent);

  return TRUE;
}
BOOL COSMCtrlMapOperationsDlg::ForceMapnikRerenderHelper()
{
  //Validate our parameters
  AFXASSUME(m_pOSMCtrl);

  //What will be the return value from this function (assume the best)
  BOOL bSuccess = TRUE;

  //Next get the server to connect to
  COSMCtrlMapnikTileProvider MapnikTileProvider;
  CString sServer(MapnikTileProvider.GetDownloadServer());

  //Accumulate how many tiles we have request to rerender and which ones indicated a failure to rerender
  int nTilesRerendered = 0;
  int nTilesNotRerendered = 0;

  //Next create the Wininet session object
  ASSERT(m_hSession == NULL);
  HRESULT hr = m_pOSMCtrl->CreateSession(m_hSession);
  if (SUCCEEDED(hr))
  {
    //Now create the connection object from the session object
    HINTERNET hConnection = NULL;
    hr = m_pOSMCtrl->CreateConnection(m_hSession, sServer, 80, hConnection);
    if (SUCCEEDED(hr))
    {
      //Iterate across the array of tiles to rerender
      for (INT_PTR i=0; i<m_Tiles.GetSize() && bSuccess; i++)
      {
        //Pull out the next tile to download
        const COSMCtrlMapOperationsDlgTile& tile = m_Tiles.ElementAt(i);

        //Form the name of the tile we will be rerendering
        CString sObject(MapnikTileProvider.GetDownloadObject(tile.m_nZoom, tile.m_nTileX, tile.m_nTileY) + _T("/dirty"));

        //Now issue the request to rerender
        COSMCtrlMapOperationsDlgEvent dlgEvent;
        dlgEvent.m_bSuccess = false;
        CStringA sResponse;
        hr = m_pOSMCtrl->DownloadPage(hConnection, sObject, TRUE, sResponse);
        if (FAILED(hr))
        {
          //report the error
          TRACE(_T("COSMCtrlMapOperationsDlg::ForceMapnikRerenderHelper, Failed to download page \"%s\", Error:%08X\n"), sObject.operator LPCTSTR(), hr);
          
          //Update the stats
          ++nTilesNotRerendered;
        }
        else
        {
          //Screen scrape the response to see if it worked
          if (sResponse.Find("Tile submitted for rendering") != -1)
          {
            //Update the stats
            ++nTilesRerendered;
            dlgEvent.m_bSuccess = true;
          }
          else
          {
            //Update the stats
            ++nTilesNotRerendered;
          }  
        }

        //Update the UI          
        dlgEvent.m_Event = COSMCtrlMapOperationsDlgEvent::SimpleStringStatus;
        dlgEvent.m_sString = sObject;
        dlgEvent.m_nItemData = i + 1;
        AddEvent(dlgEvent);

        //Check if we have been cancelled before we loop around
        bSuccess = (WaitForSingleObject(m_WorkerTerminateEvent, 0) == WAIT_TIMEOUT);
      }
      
      //Close the wininet connection
    #ifdef COSMCTRL_NOWINHTTP
      InternetCloseHandle(hConnection);
    #else
      WinHttpCloseHandle(hConnection);
    #endif
    }

    //Clean up the wininet session before we exit
  #ifdef COSMCTRL_NOWINHTTP
    InternetCloseHandle(m_hSession);
  #else
    WinHttpCloseHandle(m_hSession);
  #endif
    m_hSession = NULL;
  }

  //Finally add a event about how many items have been downloaded
  COSMCtrlMapOperationsDlgEvent dlgEvent;
  dlgEvent.m_Event = COSMCtrlMapOperationsDlgEvent::SimpleStringStatus;
  CString sTilesRerendered;
  sTilesRerendered.Format(_T("%d"), nTilesRerendered);
  CString sTilesNotRerendered;
  sTilesNotRerendered.Format(_T("%d"), nTilesNotRerendered);
  AfxFormatString2(dlgEvent.m_sString, IDS_OSMCTRL_RERENDER_TILES_STATS, sTilesRerendered, sTilesNotRerendered);
  AddEvent(dlgEvent);

  return TRUE;
}
Exemple #5
0
void C4ChatControl::OnConnectBtn(C4GUI::Control *btn) {
  // check parameters
  StdCopyStrBuf sNick(pEdtLoginNick->GetText());
  StdCopyStrBuf sPass(pEdtLoginPass->GetText());
  StdCopyStrBuf sRealName(pEdtLoginRealName->GetText());
  StdCopyStrBuf sChannel(pEdtLoginChannel->GetText());
  StdCopyStrBuf sServer(Config.IRC.Server);
  if (C4InVal::ValidateString(sNick, C4InVal::VAL_IRCName)) {
    GetScreen()->ShowErrorMessage(LoadResStr("IDS_ERR_INVALIDNICKNAME"));
    GetDlg()->SetFocus(pEdtLoginNick, false);
    return;
  }
  if (sPass.getLength() &&
      C4InVal::ValidateString(sPass, C4InVal::VAL_IRCPass)) {
    GetScreen()->ShowErrorMessage(
        LoadResStr("IDS_ERR_INVALIDPASSWORDMAX31CHARA"));
    GetDlg()->SetFocus(pEdtLoginPass, false);
    return;
  }
  if (sChannel.getLength() &&
      C4InVal::ValidateString(sChannel, C4InVal::VAL_IRCChannel)) {
    GetScreen()->ShowErrorMessage(LoadResStr("IDS_ERR_INVALIDCHANNELNAME"));
    GetDlg()->SetFocus(pEdtLoginChannel, false);
    return;
  }
  if (!sPass.getLength()) sPass.Clear();
  if (!sChannel.getLength()) sChannel.Clear();
  // store back config values
  SCopy(sNick.getData(), Config.IRC.Nick, CFG_MaxString);
  SCopy(sRealName.getData(), Config.IRC.RealName, CFG_MaxString);
  SCopy(sChannel.getData(), Config.IRC.Channel, CFG_MaxString);
  // show chat warning
  StdStrBuf sWarnMsg;
  sWarnMsg.Format(LoadResStr("IDS_MSG_YOUAREABOUTTOCONNECTTOAPU"),
                  sServer.getData());
  if (!GetScreen()->ShowMessageModal(
          sWarnMsg.getData(), LoadResStr("IDS_MSG_CHATDISCLAIMER"),
          C4GUI::MessageDialog::btnOKAbort, C4GUI::Ico_Notify,
          &Config.Startup.HideMsgIRCDangerous))
    return;
  // set up IRC callback
  pIRCClient->SetNotify(&Application.InteractiveThread);
  // initiate connection
  if (!pIRCClient->Connect(sServer.getData(), sNick.getData(),
                           sRealName.getData(), sPass.getData(),
                           sChannel.getData())) {
    GetScreen()->ShowErrorMessage(
        FormatString(LoadResStr("IDS_ERR_IRCCONNECTIONFAILED"),
                     pIRCClient->GetError()).getData());
    return;
  }
  // enable client execution
  Application.InteractiveThread.AddProc(pIRCClient);
  // reset chat sheets (close queries, etc.)
  ClearChatSheets();
  // connection message
  ChatSheet *pServerSheet = GetServerSheet();
  if (pServerSheet) {
    pServerSheet->SetChatTitle(sServer.getData());
    pServerSheet->AddTextLine(FormatString(LoadResStr("IDS_NET_CONNECTING"),
                                           sServer.getData(), "").getData(),
                              C4GUI_MessageFontClr);
  }
  // switch to server window
  UpdateShownPage();
}