Coherent UI  2.5.3
A modern user interface library for games
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
Sample - Downloads

This sample demonstrates the download support available in Cohernet UI. It allows the user to click on files on the web and downloads them in the working folder of the application. It also shows how a client can inbitiate a download request on any URL without having to navigate to it (press F1).

Note
Supplied code is not representative for a well-designed, high-performance solution. It's written for simplicity and its purpose is to demonstrate a feature of Coherent UI.

Building the sample

The sample solution is already configured and you only have to compile and run it.

It uses a sample mini game framework that provides very basic functionality.

The output will be in the Coherent/Samples/UI/bin directory.

Key points

  1. When a download is initiated the Coherent::UI::EventListener::OnDownloadStarted method is called and the Coherent::UI::Download item is saved to monitor it's progress
  2. The title bar is updated with the name of the file currenlty being downloaded and the amount of bytes downloaded so far.
  3. As soon as the download is complete the Coherent::UI::EventListener::OnDownloadComplete method is called and the downloaded bytes are saved to disk.

Sample walkthrough

Downloads are disabled by default in Coherent UI. The user must override the Coherent::UI::EventListener::OnDownloadStarted method and return true if she wants a particular file to be downloaded. All relevant information such as URL, referrer, size etc. can be inspected in that method.

virtual bool OnDownloadStarted(Coherent::UI::Download* downloadItem)
{
// keep track of the last download item so that we can show it in the title bar
m_LastDownloadItem = downloadItem;
// Allow all downloads
return true;
}

In this sample we allow for any file to be downloaded. We also save the download object as we can use it to monitor progress or cancel the task.

Every frame the title bar of the application is updated with the name of the file currently in download and the progress it made.

std::ostringstream title;
auto downloadTask = m_ContextListener->GetLastDownloadTask();
if(downloadTask)
{
title << "Downloading file " << ConvertWideStringToUTF8(downloadTask->GetFilename()) << " - " << downloadTask->GetDownloadedBytesSoFar() << " bytes downloaded";
}
m_Window.SetTitle(title.str().c_str());

When the donwload is complete the Coherent::UI::EventListener::OnDownloadComplete method is called and the file is saved to disk.

virtual void OnDownloadComplete(Coherent::UI::Download* downloadItem, const char* data, unsigned size)
{
std::string fn(ConvertWideStringToUTF8(downloadItem->GetFilename()));
std::ofstream fout(fn.c_str(), std::ios::binary);
fout.write(data, size);
if(m_LastDownloadItem->GetId() == downloadItem->GetId()) {
m_LastDownloadItem = nullptr;
}
downloadItem->Destroy();
}

Download requests can also be initiated by the client without the need to navigate to a particular file. This is done with the Coherent::View::DownloadUrl method. In the sample pressing F1 causes such a download task to begin.

// Pressing F1 will cause a client-requested download
if(event.Type == Coherent::UI::KeyEventData::KeyUp && event.KeyCode == 0x70/*VK_F1*/)
{
m_ViewListener->GetView()->DownloadUrl(L"http://download.thinkbroadband.com/5MB.zip");
}