Exemplo n.º 1
0
void Import::fetch()
{
  if(m_download)
   return;

  auto_string url(4096, 0);
  GetWindowText(m_url, &url[0], (int)url.size());

  // remove extra nulls from the string
  url.resize(url.find(AUTO_STR('\0')));

  if(url.empty()) {
    close();
    return;
  }

  setWaiting(true);

  Download *dl = m_download = new Download({}, from_autostring(url));

  dl->onFinish([=] {
    const Download::State state = dl->state();
    if(state == Download::Aborted) {
      // at this point `this` is deleted, so there is nothing else
      // we can do without crashing
      return; 
    }

    setWaiting(false);

    if(state != Download::Success) {
      const string msg = "Download failed: " + dl->contents();
      MessageBox(handle(), make_autostring(msg).c_str(), TITLE, MB_OK);
      SetFocus(m_url);
      return;
    }

    if(!import())
      SetFocus(m_url);
  });

  dl->setCleanupHandler([=] {
    // if we are still alive
    if(dl->state() != Download::Aborted)
      m_download = nullptr;

    delete dl;
  });

  dl->start();
}