Adding a progress text to show the percent already downloaded of the addons inthe addons dialog

git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/trunk@5648 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
xapantu 2010-07-05 17:47:05 +00:00
parent 8ea7b2d2b9
commit 6a51351181
8 changed files with 106 additions and 46 deletions

View File

@ -48,6 +48,8 @@ Addons::Addons()
{
this->index = -1;
std::cout << "Loading an xml file for addons: ";
int download_state = 0;
m_download_state = &download_state;
download("list");
std::string xml_file = file_manager->getConfigDir() + "/" + "list";
std::cout << xml_file << std::endl;
@ -317,18 +319,10 @@ void Addons::Install()
//download of the addons file
download(std::string("file/" + this->m_addons_list[this->index].file),
this->m_addons_list[this->index].name);
this->m_addons_list[this->index].name, m_download_state);
file_manager->checkAndCreateDirForAddons(this->m_addons_list[this->index].name,
this->m_addons_list[this->index].type + "s/");
//creating of the data folders
#ifdef FIXME_ADDON
// mkdir does no not exist in windows, see filemanager checkandcreatedir
mkdir(std::string(file_manager->getAddonsDir() + "/" + "data").c_str(), 0777);
mkdir(dest_file.c_str(), 0777);
mkdir(std::string(dest_file + this->m_addons_list[this->index].name).c_str(), 0777);
#endif
//extract the zip in the addons folder called like the addons name
std::string dest_file =file_manager->getAddonsDir() + "/" + "data" + "/" +
this->m_addons_list[this->index].type + "s/" +
@ -444,4 +438,11 @@ int Addons::RemoveDirectory(char const *name)
#endif
return 1;
}
int Addons::getDownloadState()
{
pthread_mutex_lock(&download_mutex);
int value = *m_download_state;
pthread_mutex_unlock(&download_mutex);
return value;
}
#endif

View File

@ -46,6 +46,7 @@ class Addons
int RemoveDirectory(char const *name);
void GetInstalledAddons();
std::string type;
int * m_download_state;
public:
Addons();
@ -102,6 +103,7 @@ class Addons
bool NextType(std::string type);
bool PreviousType(std::string type);
int getDownloadState();
};

View File

@ -27,23 +27,40 @@
#include "io/file_manager.hpp"
// ------------------------------------------------------------------------------------------------------
bool download(std::string file, std::string save)
bool download(std::string file, std::string save, int * progress_data)
{
CURL *session = curl_easy_init();
std::cout << "Downloading: " << std::string(UserConfigParams::m_server_addons.toString() + "/" + file) << std::endl;
curl_easy_setopt(session, CURLOPT_URL, std::string(UserConfigParams::m_server_addons.toString() + "/" + file).c_str());
FILE * fp;
FILE * fout;
if(save != "")
fp = fopen(std::string(file_manager->getConfigDir() + std::string("/") + save).c_str(), "w");
fout = fopen(std::string(file_manager->getConfigDir() + std::string("/") + save).c_str(), "w");
else
fp = fopen(std::string(file_manager->getConfigDir() + std::string("/") + file).c_str(), "w");
curl_easy_setopt(session, CURLOPT_WRITEDATA, fp);
fout = fopen(std::string(file_manager->getConfigDir() + std::string("/") + file).c_str(), "w");
//from and out
curl_easy_setopt(session, CURLOPT_WRITEDATA, fout);
curl_easy_setopt(session, CURLOPT_WRITEFUNCTION, fwrite);
//init the mutex for the progress function
pthread_mutex_init(&download_mutex, NULL);
curl_easy_setopt(session, CURLOPT_PROGRESSFUNCTION, &progressDownload);
curl_easy_setopt(session, CURLOPT_NOPROGRESS, 0);
//needed, else, the progress function doesn't work
curl_easy_setopt(session, CURLOPT_NOPROGRESS, 0);
//to update the progress bar
curl_easy_setopt(session, CURLOPT_PROGRESSDATA, progress_data);
int succes = curl_easy_perform(session);
fclose(fp);
//close the file where we downloaded the content
fclose(fout);
//stop curl
curl_easy_cleanup(session);
if(succes == 0)
{
std::cout << "Download successfull" << std::endl;
@ -60,6 +77,13 @@ int progressDownload (void *clientp, double dltotal, double dlnow, double ultota
int progress = dlnow/dltotal*100;
if(isnan(dlnow/dltotal*100))
progress = 0;
pthread_mutex_lock(&download_mutex);
if(clientp != NULL)
{
int * progress_data = (int*)clientp;
*progress_data = progress;
}
pthread_mutex_unlock(&download_mutex);
std::cout << "Download progress: " << progress << "%" << std::endl;
return 0;
}

View File

@ -20,9 +20,13 @@
#ifndef HEADER_NETWORK_HPP
#define HEADER_NETWORK_HPP
/** Download a file. The file name isn't absolute, the server in the config will be added to file. */
bool download(std::string file, std::string save = "");
/* * Download a file. The file name isn't absolute, the server in the config will be added to file.
* progress_data is used to have the state of the download (in %)*/
bool download(std::string file, std::string save = "", int * progress_data = 0);
int progressDownload (void *clientp, double dltotal, double dlnow, double ultotal, double ulnow);
static pthread_mutex_t download_mutex;
#endif
#endif

View File

@ -42,6 +42,7 @@ AddonsLoading::AddonsLoading(Addons * id, const float w, const float h) :
{
this->addons = id;
m_can_install = false;
m_percent_update = false;
pthread_mutex_init(&mutex_can_install, NULL);
core::rect< s32 > area_right(10, 0, m_area.getWidth()/2, m_area.getHeight());
@ -92,7 +93,8 @@ AddonsLoading::AddonsLoading(Addons * id, const float w, const float h) :
description->setParent(m_irrlicht_window);
description->m_text = StringUtils::insertValues(_("Description: %i"), this->addons->GetDescription().c_str());
description->add();
m_children.push_back(description);
version = new LabelWidget();
version->m_x = area_left.UpperLeftCorner.X;
version->m_y = area_left.UpperLeftCorner.Y + area_left.getHeight()/6 + area_left.getHeight()/3;
@ -104,7 +106,17 @@ AddonsLoading::AddonsLoading(Addons * id, const float w, const float h) :
m_children.push_back(version);
version->add();
m_children.push_back(description);
m_progress = new LabelWidget();
m_progress->m_x = 180;
m_progress->m_y = m_area.getHeight()-45;
m_progress->m_text = "";
m_progress->m_w = m_area.getWidth() - 180;
m_progress->m_h = 25;
m_progress->setParent(m_irrlicht_window);
m_children.push_back(m_progress);
m_progress->add();
this->loadInfo();
}
void AddonsLoading::loadInfo()
@ -187,6 +199,7 @@ GUIEngine::EventPropagation AddonsLoading::processEvent(const std::string& event
m_next->setDeactivated();
m_previous->setDeactivated();
this->install_button->setDeactivated();
m_percent_update = true;
pthread_t thread;
pthread_create(&thread, NULL, &AddonsLoading::startInstall, this);
}
@ -201,6 +214,10 @@ void AddonsLoading::onUpdate(float delta)
{
this->close();
}
if(m_percent_update)
{
m_progress->setText(std::string(StringUtils::toString(addons->getDownloadState()) + "\% downloaded").c_str());
}
pthread_mutex_unlock(&(mutex_can_install));
}
@ -230,6 +247,7 @@ void * AddonsLoading::startInstall(void* pthis)
}
pthread_mutex_lock(&(obj->mutex_can_install));
obj->m_can_install = true;
obj->m_percent_update = false;
pthread_mutex_unlock(&(obj->mutex_can_install));
return NULL;
}

View File

@ -34,15 +34,17 @@ private:
GUIEngine::LabelWidget * description;
GUIEngine::LabelWidget * version;
GUIEngine::LabelWidget * author;
GUIEngine::LabelWidget * m_progress;
GUIEngine::ButtonWidget * m_back_button;
GUIEngine::ButtonWidget * install_button;
GUIEngine::IconButtonWidget * icon;
GUIEngine::IconButtonWidget * m_next;
GUIEngine::IconButtonWidget * m_previous;
GUIEngine::IconButtonWidget * icon;
GUIEngine::IconButtonWidget * m_next;
GUIEngine::IconButtonWidget * m_previous;
static void * startInstall(void*);
static void * downloadIcon(void*);
void loadInfo();
bool m_can_install;
bool m_percent_update;
public:
/**
* Creates a modal dialog with given percentage of screen width and height

View File

@ -61,30 +61,37 @@ MainMenuScreen::MainMenuScreen() : Screen("main.stkgui")
}
#endif
#ifdef ADDONS_MANAGER
void MainMenuScreen::downloadRss()
void MainMenuScreen::changeNewsText(std::string action)
{
LabelWidget* w = this->getWidget<LabelWidget>("info_addons");
FILE* newsFile = NULL;
char buffer[1024] = "";
newsFile = fopen(std::string(file_manager->getConfigDir() + "/news").c_str(), "r+");
if (newsFile == NULL)
if(action == "news")
{
fprintf(stderr, "Warning: cannot open new files\n");
return;
}
std::string info = std::string("");
while (fgets(buffer, 1024, newsFile) != NULL)
{
info += std::string(buffer);
}
FILE* newsFile = NULL;
char buffer[1024] = "";
newsFile = fopen(std::string(file_manager->getConfigDir() + "/news").c_str(), "r+");
if (newsFile == NULL)
{
fprintf(stderr, "Warning: cannot open news files\n");
return;
}
std::string info = std::string("");
while (fgets(buffer, 1024, newsFile) != NULL)
{
info += std::string(buffer);
}
fclose(newsFile);
// to remove the break line.
//info.replace(info.size()-1,1, "");
std::cout << info << std::endl;
w->setText(std::string(info).c_str());
fclose(newsFile);
// to remove the break line.
//info.replace(info.size()-1,1, "");
std::cout << info << std::endl;
w->setText(std::string(info).c_str());
}
if(action == "offline")
{
w->setText(_("Can't access stkaddons server..."));
}
}
#endif
// ------------------------------------------------------------------------------------------------------
@ -206,10 +213,12 @@ void MainMenuScreen::eventCallback(Widget* widget, const std::string& name, cons
// ------------------------------------------------------------------------------------------------------
void * MainMenuScreen::downloadNews( void * pthis)
{
download("news");
MainMenuScreen * pt = (MainMenuScreen*)pthis;
pt->downloadRss();
if(download("news"))
pt->changeNewsText("news");
else
pt->changeNewsText("offline");
return NULL;
}
#endif

View File

@ -32,7 +32,7 @@ class MainMenuScreen : public GUIEngine::Screen, public GUIEngine::ScreenSinglet
MainMenuScreen();
public:
void downloadRss();
void changeNewsText(std::string action);
/** \brief implement callback from parent class GUIEngine::Screen */
virtual void loadedFromFile();