/* 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 #ifdef HAVE_CURL #include #include #include #endif /* HAVE_CURL */ 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 = "03%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); } /** \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; } void* OrxonoxGuiUpdate::download (void* fileInfo) { PRINTF(3)("Downloading.\n"); FileInfo* info = (FileInfo*)fileInfo; CURL* curl; CURLcode res; FILE* outfile; curl = 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(curl) { outfile = fopen(fileOnDisk, "w"); curl_easy_setopt(curl, CURLOPT_URL, fileOnNet); curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlWriteFunc); curl_easy_setopt(curl, CURLOPT_READFUNCTION, curlReadFunc); curl_easy_setopt(curl, CURLOPT_NOPROGRESS, FALSE); curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, curlProgressFunc); curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, info->Bar); res = curl_easy_perform(curl); fclose(outfile); curl_easy_cleanup(curl); } return NULL; } #ifdef HAVE_GTK2 gint OrxonoxGuiUpdate::cancelDownload(GtkWidget* w, GdkEventKey* event, void* bar) { } #endif /* HAVE_GTK2 */ #endif /* HAVE_CURL */ /* int main(int argc, char **argv) { GtkWidget *Window, *Frame, *Frame2; GtkAdjustment *adj; // Init thread g_thread_init(NULL); gtk_init(&argc, &argv); Window = gtk_window_new(GTK_WINDOW_TOPLEVEL); Frame = gtk_frame_new(NULL); gtk_frame_set_shadow_type(GTK_FRAME(Frame), GTK_SHADOW_OUT); gtk_container_add(GTK_CONTAINER(Window), Frame); Frame2 = gtk_frame_new(NULL); gtk_frame_set_shadow_type(GTK_FRAME(Frame2), GTK_SHADOW_IN); gtk_container_add(GTK_CONTAINER(Frame), Frame2); gtk_container_set_border_width(GTK_CONTAINER(Frame2), 5); adj = (GtkAdjustment*)gtk_adjustment_new(0, 0, 100, 0, 0, 0); Bar = gtk_progress_bar_new_with_adjustment(adj); gtk_container_add(GTK_CONTAINER(Frame2), Bar); gtk_widget_show_all(Window); if (!g_thread_create(&my_thread, argv[1], FALSE, NULL) != 0) g_warning("can't create the thread"); gdk_threads_enter(); gtk_main(); gdk_threads_leave(); return 0; } size_t writeFunc(void *ptr, size_t size, size_t nmemb, FILE *stream) { return fwrite(ptr, size, nmemb, stream); } size_t my_read_func(void *ptr, size_t size, size_t nmemb, FILE *stream) { return fread(ptr, size, nmemb, stream); } int my_progress_func(GtkWidget *Bar, double t, // dltotal double d, // dlnow double ultotal, double ulnow) { gdk_threads_enter(); gtk_progress_set_value(GTK_PROGRESS(Bar), d*100.0/t); gdk_threads_leave(); return 0; } void *my_thread(void *ptr) { CURL *curl; CURLcode res; FILE *outfile; gchar *url = ptr; curl = curl_easy_init(); if(curl) { outfile = fopen("test.curl", "w"); curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_write_func); curl_easy_setopt(curl, CURLOPT_READFUNCTION, my_read_func); curl_easy_setopt(curl, CURLOPT_NOPROGRESS, FALSE); curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, my_progress_func); curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, Bar); res = curl_easy_perform(curl); fclose(outfile); curl_easy_cleanup(curl); } return NULL; } */