/* 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 #include using namespace std; /** \brief Creates an Audio-Frame */ OrxonoxGuiUpdate::OrxonoxGuiUpdate () { this->getSystemInfo(); this->updateFrame = new Frame ("Update-Options:"); this->updateFrame->setGroupName ("update"); this->updateBox = new Box ('v'); #ifdef HAVE_CURL // 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()); #else /* HAVE_CURL */ Label* noCurlLabel = new Label("since you do not have cURL,\nthis option is not availible"); this->updateBox->fill(noCurlLabel); #endif /* HAVE_CURL */ this->updateFrame->fill(this->updateBox); } /** \brief Return the Frame \return Returns the Audio-frame */ Widget* OrxonoxGuiUpdate::getWidget () { return updateFrame; } /** \brief Look what info we can get from this system */ bool OrxonoxGuiUpdate::getSystemInfo(void) { PRINTF(3)("Grabbing system information\n"); tmpDir = getenv("TMPDIR"); if (!tmpDir) tmpDir = "/tmp"; PRINTF(4)("Temporary directory is: %s\n", tmpDir); #ifdef __WIN32__ homeDir = getenv ("USERPROFILE"); #else homeDir = getenv("HOME"); #endif PRINTF(4)("Home directory is %s\n", homeDir); installDataDir = "/usr/share/games/orxonox"; PRINTF(4)("Installation of orxonox-data will go to this directory is %s\n", installDataDir); installSourceDir = "/usr/games/bin"; PRINTF(4)("Installation of orxonox-source will go to this directory is %s\n", installSourceDir); userName = getenv("USER"); PRINTF(4)("Logged in username is: %s\n", userName); } #ifdef HAVE_CURL /** \brief Creates a window, and all it contains for the Data-update. */ 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); FileInfo* dataInfo = new FileInfo; dataInfo->bar = updateDataBar; updateDataBegin = new Button ("begin."); dataInfo->stateButton = updateDataBegin; dataInfo->buttonSignal = updateDataBegin->connectSignal ("button_press_event", dataInfo, 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 UpdaterDataWindow */ Button* OrxonoxGuiUpdate::updateDataWindowGetButton(void) { return updateDataWindowButton; } /** \brief Creates a window, and all it contains for the Source-update. */ 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 */ } /** \returns A Pointer to the Button of the UpdaterSourceWindow */ 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* info) { FileInfo* dataInfo = (FileInfo*)info; dataInfo->fileName = "02%20orxonox%203.mp3"; dataInfo->webRoot = "http://www.orxonox.ethz.ch/files/"; dataInfo->localRoot = "./"; PRINTF(3)("Preparing to download file %s.\n", dataInfo->fileName); download (dataInfo); } /** \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 */ /** \brief The Function Curl calls to write out the File. \param ptr A Pointer to the date to write. \param size The size in bytes of one nmemb to write. \param nmemb The Count of size to write. \param stream Filehandler to write to. */ size_t OrxonoxGuiUpdate::curlWriteFunc (void* ptr, size_t size, size_t nmemb, FILE* stream) { return fwrite(ptr, size, nmemb, stream); } /** \brief The Function Curl calls to write out the File. \param ptr A Pointer to the date to write to. \param size The size in bytes of one nmemb to write. \param nmemb The Count of size to write. \param stream Filehandler to get data from. */ size_t OrxonoxGuiUpdate::curlReadFunc (void* ptr, size_t size, size_t nmemb, FILE* stream) { return fread(ptr, size, nmemb, stream); } /** \brief An update Function for the GUI, to show the progress. \param Bar th ProgressBar to update \param totalSize The total size of the download in bytes. \param progress The current Progress of the download in bytes. \param upTotal not needed \param upProgress not needed */ int OrxonoxGuiUpdate::curlProgressFunc (ProgressBar* Bar, double totalSize, double progress, double upTotal, double upProgress) { Bar->setProgress(progress); Bar->setTotalSize(totalSize); #ifdef HAVE_GTK2 #ifndef HAVE_PTHREAD_H while(gtk_events_pending()) gtk_main_iteration(); #endif #endif return 0; } /** \brief The Curl handle for only one CURL (static). */ CURL* OrxonoxGuiUpdate::curlHandle = NULL; #ifdef HAVE_PTHREAD_H /** \brief The download Thread ID */ pthread_t* OrxonoxGuiUpdate::downloadThreadID = new pthread_t; /** \brief The download Thread ID*/ pthread_t* OrxonoxGuiUpdate::downloadThreadFinishID = new pthread_t; #endif /* HAVE_PTHREAD_H */ /** \brief A bool parameter that shows if we are downloading. */ bool OrxonoxGuiUpdate::isDownloading = false; /** \brief Initializes a Download \param fileInfo the FileInfo. */ bool OrxonoxGuiUpdate::download (void* fileInfo) { if (isDownloading) { PRINTF(2)("unable to Download. already getting some file\n"); return false; } 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); 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) { pthread_join(*downloadThreadFinishID, NULL); info->stateButton->disconnectSignal(info->buttonSignal); info->buttonSignal = info->stateButton->connectSignal("button_press_event", info, cancelDownload); #ifdef HAVE_PTHREAD_H info->stateButton->setTitle("cancel"); #else /* HAVE_PTHREAD_H */ info->stateButton->setTitle("please wait"); #endif /* HAVE_PTHREAD_H */ //! \todo check if threads really were created. #ifdef HAVE_PTHREAD_H pthread_create(downloadThreadID, NULL, downloadThread, info); pthread_create(downloadThreadFinishID, NULL, downloadThreadFinished, info); #else downloadThread(info); downloadThreadFinished(info); #endif /* HAVE_PTHREAD_H */ // res = curl_easy_perform(curlHandle); // fclose(outfile); } else PRINTF(1)("thread already in use\n"); } return true; } /** \brief The downloading process (either threaded or not). \param fileInfo the FileInfo. \todo Threads get locked, if the cancel button is pressed in to small intervals. */ void* OrxonoxGuiUpdate::downloadThread(void* fileInfo) { isDownloading = true; curl_easy_perform(curlHandle); } /** \brief Finishes a downloading process. \param fileInfo the FileInfo. */ void* OrxonoxGuiUpdate::downloadThreadFinished(void* fileInfo) { FileInfo* info = (FileInfo*)fileInfo; #ifdef HAVE_PTHREAD_H pthread_join (*downloadThreadID, NULL); gdk_threads_enter(); #endif /* HAVE_PTHREAD_H */ if (curlHandle) curl_easy_cleanup(curlHandle); PRINTF(3)("Closing the downloaded file.\n"); fclose(info->fileHandle); if (isDownloading) info->stateButton->setTitle("go on"); // else // info->stateButton->setTitle("done"); info->stateButton->disconnectSignal(info->buttonSignal); info->buttonSignal = info->stateButton->connectSignal("button_press_event", info, updateDataFunc); isDownloading = false; #ifdef HAVE_PTHREAD_H gdk_threads_leave(); #endif /* HAVE_PTHREAD_H */ } #ifdef HAVE_GTK2 /** \brief canceles a downloading session. \param w The widget, that executed this Function. \param event The event that trigered this Function. \param bar The Bar, that triggered this event. \todo canceling a session in non-threaded mode. */ gint OrxonoxGuiUpdate::cancelDownload(GtkWidget* w, GdkEventKey* event, void* bar) { #ifdef HAVE_PTHREAD_H pthread_cancel(*downloadThreadID); #else PRINTF(2)("Cannot cancle the Downloading process until after this File, because no threading was enabled"); #endif /* HAVE_PTHREAD_H*/ } #endif /* HAVE_GTK2 */ #endif /* HAVE_CURL */