/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2004 orx This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. ### File Specific: main-programmer: Benjamin Grauer */ #include "orxonox_gui_update.h" #include #include #include "orxonox_gui.h" #include using namespace std; /** \brief Creates an Audio-Frame */ OrxonoxGuiUpdate::OrxonoxGuiUpdate () { this->updateFrame = new Frame ("Update-Options:"); this->updateFrame->setGroupName ("update"); this->updateBox = new Box ('v'); // the Button for autoUpdating this->autoUpdate = new CheckButton ("auto update"); this->updateBox->fill(this->autoUpdate); this->autoUpdate->setFlagName ("update", "u", 0); this->autoUpdate->saveable = true; this->updateSourceWindowCreate (); this->updateBox->fill(this->updateSourceWindowGetButton()); this->updateDataWindowCreate (); this->updateBox->fill(this->updateDataWindowGetButton()); this->updateFrame->fill(this->updateBox); } /** \brief Return the Frame \return Returns the Audio-frame */ Widget* OrxonoxGuiUpdate::getWidget () { return updateFrame; } void OrxonoxGuiUpdate::updateDataWindowCreate (void) { updateDataWindow = new Window ("update orxonox::Data"); updateDataBox = new Box ('v'); // the close-Button of the Update Window. // updateWindowClose = new Button ("close"); #ifdef HAVE_GTK2 // updateWindowClose->connectSignal("button_press_event", updateWindow, Window::windowClose); #endif /* HAVE_GTK2 */ // updateWindowBox->fill(updateWindowClose); updateDataBar = new ProgressBar (); updateDataBox->fill(updateDataBar); updateDataBegin = new Button ("begin Download"); updateDataBegin->connectSignal ("button_press_event", updateDataBar, updateDataFunc); updateDataBox->fill(updateDataBegin); updateDataWindow->fill (updateDataBox); updateDataWindowButton = new Button ("update orxonox::Data"); #ifdef HAVE_GTK2 updateDataWindowButton->connectSignal("button_press_event", updateDataWindow, Window::windowOpen); updateDataWindow->connectSignal("destroy", updateDataWindow, Window::windowClose); updateDataWindow->connectSignal("delete_event", updateDataWindow, Window::windowClose); #endif /* HAVE_GTK2 */ } /** \returns A Pointer to the Button of the UpdaterWindow */ Button* OrxonoxGuiUpdate::updateDataWindowGetButton(void) { return updateDataWindowButton; } void OrxonoxGuiUpdate::updateSourceWindowCreate (void) { // the button, that opens this Window. updateSourceWindowButton = new Button ("update orxonox::Source"); // the Window itself updateSourceWindow = new Window ("update orxonox::Source"); updateSourceBox = new Box (); updateSourceBar = new ProgressBar (); updateSourceBox->fill(updateSourceBar); test = new Button ("increment"); #ifdef HAVE_GTK2 test->connectSignal("button_press_event", updateSourceBar, updateSourceFunc); #endif /* HAVE_GTK2 */ updateSourceBox->fill(test); updateSourceWindow->fill(updateSourceBox); #ifdef HAVE_GTK2 updateSourceWindowButton->connectSignal("button_press_event", updateSourceWindow, Window::windowOpen); updateSourceWindow->connectSignal("destroy", updateSourceWindow, Window::windowClose); updateSourceWindow->connectSignal("delete_event", updateSourceWindow, Window::windowClose); #endif /* HAVE_GTK2 */ } Button* OrxonoxGuiUpdate::updateSourceWindowGetButton(void) { return updateSourceWindowButton; } #ifdef HAVE_GTK2 /** \brief updates the Data of orxonox. \param w The widget, that executed this Function. \param event The event that trigered this Function. \param button The Button, that triggered this event. */ gint OrxonoxGuiUpdate::updateDataFunc(GtkWidget* w, GdkEventKey* event, void* bar) { FileInfo* info = new FileInfo; info->fileName = "02%20orxonox%203.mp3"; info->webRoot = "http://www.orxonox.ethz.ch/files/"; info->localRoot = "./"; info->Bar = (ProgressBar*)bar; PRINTF(3)("Preparing to download file %s.\n", info->fileName); download (info); /* if (downloadThreadID == NULL) { if ( (downloadThreadID =g_thread_create(&downloadThread, info, TRUE, NULL)) == 0 ) PRINTF(1)("can't create the thread"); } else PRINTF(1)("thread already in use\n"); */ } /** \brief updates the source of orxonox. \param w The widget, that executed this Function. \param event The event that trigered this Function. \param button The Button, that triggered this event. */ gint OrxonoxGuiUpdate::updateSourceFunc(GtkWidget* w, GdkEventKey* event, void* bar) { ProgressBar* tmpBar = static_cast(bar); tmpBar->setTotalSize(20); tmpBar->setProgress(tmpBar->getProgress()+1); } #endif /* HAVE_GTK2 */ #ifdef HAVE_CURL size_t OrxonoxGuiUpdate::curlWriteFunc (void* ptr, size_t size, size_t nmemb, FILE* stream) { return fwrite(ptr, size, nmemb, stream); } size_t OrxonoxGuiUpdate::curlReadFunc (void* ptr, size_t size, size_t nmemb, FILE* stream) { return fread(ptr, size, nmemb, stream); } int OrxonoxGuiUpdate::curlProgressFunc (ProgressBar* Bar, double totalSize, double progress, double upTotal, double upProgress) { Bar->setProgress(progress); Bar->setTotalSize(totalSize); return 0; } CURL* OrxonoxGuiUpdate::curlHandle = NULL; pthread_t* OrxonoxGuiUpdate::downloadThreadID = new pthread_t; bool OrxonoxGuiUpdate::isDownloading = false; void* OrxonoxGuiUpdate::download (void* fileInfo) { PRINTF(3)("Downloading.\n"); FileInfo* info = (FileInfo*)fileInfo; CURLcode res; curlHandle = curl_easy_init(); char* fileOnNet = new char [strlen(info->webRoot)+strlen(info->fileName)+1]; strcpy (fileOnNet, info->webRoot); strcat (fileOnNet, info->fileName); char* fileOnDisk = new char [strlen(info->localRoot)+strlen(info->fileName)+1]; strcpy (fileOnDisk, info->localRoot); strcat (fileOnDisk, info->fileName); pthread_t test; if(curlHandle) { info->fileHandle = fopen(fileOnDisk, "w"); curl_easy_setopt(curlHandle, CURLOPT_URL, fileOnNet); curl_easy_setopt(curlHandle, CURLOPT_WRITEDATA, info->fileHandle); curl_easy_setopt(curlHandle, CURLOPT_WRITEFUNCTION, curlWriteFunc); curl_easy_setopt(curlHandle, CURLOPT_READFUNCTION, curlReadFunc); curl_easy_setopt(curlHandle, CURLOPT_NOPROGRESS, FALSE); curl_easy_setopt(curlHandle, CURLOPT_PROGRESSFUNCTION, curlProgressFunc); curl_easy_setopt(curlHandle, CURLOPT_PROGRESSDATA, info->Bar); if (!isDownloading) { //! \todo check if threads really were created. pthread_create(downloadThreadID, NULL, downloadThread, info); // else // PRINTF(1)("thread already in use\n"); // res = curl_easy_perform(curlHandle); // fclose(outfile); } } return NULL; } void* OrxonoxGuiUpdate::downloadThread(void* fileInfo) { isDownloading = true; FileInfo* info = (FileInfo*)fileInfo; curl_easy_perform(curlHandle); if (curlHandle) curl_easy_cleanup(curlHandle); pthread_join(*downloadThreadID, NULL); PRINTF(3)("Closing the downloaded file.\n"); fclose(info->fileHandle); isDownloading = false; } #ifdef HAVE_GTK2 gint OrxonoxGuiUpdate::cancelDownload(GtkWidget* w, GdkEventKey* event, void* bar) { // curl_easy_cleanup(curlHandle); // curlHandle = NULL; } #endif /* HAVE_GTK2 */ #endif /* HAVE_CURL */