[3250] | 1 | /* |
---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
| 10 | |
---|
| 11 | This program is distributed in the hope that it will be useful, |
---|
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 14 | GNU General Public License for more details. |
---|
| 15 | |
---|
| 16 | You should have received a copy of the GNU General Public License |
---|
| 17 | along with this program; if not, write to the Free Software Foundation, |
---|
| 18 | Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
| 19 | |
---|
| 20 | |
---|
| 21 | ### File Specific: |
---|
| 22 | main-programmer: Benjamin Grauer |
---|
| 23 | |
---|
| 24 | */ |
---|
| 25 | |
---|
| 26 | #include "orxonox_gui_update.h" |
---|
| 27 | #include <iostream> |
---|
| 28 | #include <string> |
---|
| 29 | |
---|
| 30 | #include "orxonox_gui.h" |
---|
| 31 | #include <stdio.h> |
---|
[3266] | 32 | #ifdef HAVE_CURL |
---|
[3263] | 33 | #include <curl/curl.h> |
---|
| 34 | #include <curl/types.h> |
---|
[3266] | 35 | #include <curl/easy.h> |
---|
| 36 | #endif /* HAVE_CURL */ |
---|
[3250] | 37 | using namespace std; |
---|
| 38 | |
---|
| 39 | /** |
---|
| 40 | \brief Creates an Audio-Frame |
---|
| 41 | */ |
---|
| 42 | OrxonoxGuiUpdate::OrxonoxGuiUpdate () |
---|
| 43 | { |
---|
[3253] | 44 | this->updateFrame = new Frame ("Update-Options:"); |
---|
[3261] | 45 | this->updateFrame->setGroupName ("update"); |
---|
[3253] | 46 | this->updateBox = new Box ('v'); |
---|
[3250] | 47 | |
---|
[3261] | 48 | // the Button for autoUpdating |
---|
| 49 | this->autoUpdate = new CheckButton ("auto update"); |
---|
| 50 | this->updateBox->fill(this->autoUpdate); |
---|
| 51 | this->autoUpdate->setFlagName ("update", "u", 0); |
---|
| 52 | this->autoUpdate->saveable = true; |
---|
[3253] | 53 | |
---|
[3261] | 54 | this->updateSourceWindowCreate (); |
---|
| 55 | this->updateBox->fill(this->updateSourceWindowGetButton()); |
---|
| 56 | |
---|
| 57 | this->updateDataWindowCreate (); |
---|
| 58 | this->updateBox->fill(this->updateDataWindowGetButton()); |
---|
| 59 | |
---|
[3253] | 60 | this->updateFrame->fill(this->updateBox); |
---|
[3250] | 61 | } |
---|
| 62 | |
---|
| 63 | /** |
---|
| 64 | \brief Return the Frame |
---|
| 65 | \return Returns the Audio-frame |
---|
| 66 | */ |
---|
| 67 | Widget* OrxonoxGuiUpdate::getWidget () |
---|
| 68 | { |
---|
| 69 | return updateFrame; |
---|
| 70 | } |
---|
[3253] | 71 | |
---|
[3261] | 72 | void OrxonoxGuiUpdate::updateDataWindowCreate (void) |
---|
[3253] | 73 | { |
---|
[3261] | 74 | updateDataWindow = new Window ("update orxonox::Data"); |
---|
| 75 | updateDataBox = new Box ('v'); |
---|
[3253] | 76 | |
---|
[3257] | 77 | // the close-Button of the Update Window. |
---|
[3261] | 78 | // updateWindowClose = new Button ("close"); |
---|
[3257] | 79 | #ifdef HAVE_GTK2 |
---|
[3261] | 80 | // updateWindowClose->connectSignal("button_press_event", updateWindow, Window::windowClose); |
---|
[3257] | 81 | #endif /* HAVE_GTK2 */ |
---|
[3261] | 82 | // updateWindowBox->fill(updateWindowClose); |
---|
[3253] | 83 | |
---|
[3261] | 84 | updateDataBar = new ProgressBar (); |
---|
| 85 | updateDataBox->fill(updateDataBar); |
---|
[3263] | 86 | |
---|
| 87 | updateDataBegin = new Button ("begin Download"); |
---|
| 88 | updateDataBegin->connectSignal ("button_press_event", updateDataBar, updateDataFunc); |
---|
| 89 | updateDataBox->fill(updateDataBegin); |
---|
| 90 | |
---|
[3261] | 91 | updateDataWindow->fill (updateDataBox); |
---|
[3253] | 92 | |
---|
[3261] | 93 | updateDataWindowButton = new Button ("update orxonox::Data"); |
---|
[3254] | 94 | #ifdef HAVE_GTK2 |
---|
[3261] | 95 | updateDataWindowButton->connectSignal("button_press_event", updateDataWindow, Window::windowOpen); |
---|
| 96 | updateDataWindow->connectSignal("destroy", updateDataWindow, Window::windowClose); |
---|
| 97 | updateDataWindow->connectSignal("delete_event", updateDataWindow, Window::windowClose); |
---|
[3253] | 98 | #endif /* HAVE_GTK2 */ |
---|
| 99 | |
---|
| 100 | } |
---|
| 101 | |
---|
[3258] | 102 | /** |
---|
| 103 | \returns A Pointer to the Button of the UpdaterWindow |
---|
| 104 | */ |
---|
[3259] | 105 | Button* OrxonoxGuiUpdate::updateDataWindowGetButton(void) |
---|
| 106 | { |
---|
[3261] | 107 | return updateDataWindowButton; |
---|
[3259] | 108 | } |
---|
| 109 | |
---|
| 110 | void OrxonoxGuiUpdate::updateSourceWindowCreate (void) |
---|
| 111 | { |
---|
| 112 | // the button, that opens this Window. |
---|
[3261] | 113 | updateSourceWindowButton = new Button ("update orxonox::Source"); |
---|
[3259] | 114 | |
---|
| 115 | // the Window itself |
---|
| 116 | updateSourceWindow = new Window ("update orxonox::Source"); |
---|
| 117 | |
---|
[3261] | 118 | updateSourceBox = new Box (); |
---|
[3259] | 119 | |
---|
| 120 | updateSourceBar = new ProgressBar (); |
---|
[3261] | 121 | updateSourceBox->fill(updateSourceBar); |
---|
[3259] | 122 | test = new Button ("increment"); |
---|
[3261] | 123 | |
---|
| 124 | #ifdef HAVE_GTK2 |
---|
[3259] | 125 | test->connectSignal("button_press_event", updateSourceBar, updateSourceFunc); |
---|
[3261] | 126 | #endif /* HAVE_GTK2 */ |
---|
| 127 | |
---|
| 128 | updateSourceBox->fill(test); |
---|
| 129 | updateSourceWindow->fill(updateSourceBox); |
---|
[3259] | 130 | #ifdef HAVE_GTK2 |
---|
[3261] | 131 | updateSourceWindowButton->connectSignal("button_press_event", updateSourceWindow, Window::windowOpen); |
---|
| 132 | updateSourceWindow->connectSignal("destroy", updateSourceWindow, Window::windowClose); |
---|
| 133 | updateSourceWindow->connectSignal("delete_event", updateSourceWindow, Window::windowClose); |
---|
[3259] | 134 | #endif /* HAVE_GTK2 */ |
---|
| 135 | |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | Button* OrxonoxGuiUpdate::updateSourceWindowGetButton(void) |
---|
| 139 | { |
---|
[3261] | 140 | return updateSourceWindowButton; |
---|
[3259] | 141 | } |
---|
| 142 | |
---|
| 143 | |
---|
| 144 | #ifdef HAVE_GTK2 |
---|
[3258] | 145 | /** |
---|
| 146 | \brief updates the Data of orxonox. |
---|
| 147 | \param w The widget, that executed this Function. |
---|
| 148 | \param event The event that trigered this Function. |
---|
| 149 | \param button The Button, that triggered this event. |
---|
| 150 | */ |
---|
[3263] | 151 | gint OrxonoxGuiUpdate::updateDataFunc(GtkWidget* w, GdkEventKey* event, void* bar) |
---|
[3254] | 152 | { |
---|
[3263] | 153 | FileInfo* info = new FileInfo; |
---|
[3254] | 154 | |
---|
[3267] | 155 | info->fileName = "03%20orxonox%203'.mp3"; |
---|
| 156 | info->webRoot = "http://www.orxonox.ethz.ch/files/"; |
---|
| 157 | info->localRoot = "./"; |
---|
[3263] | 158 | info->Bar = (ProgressBar*)bar; |
---|
[3267] | 159 | PRINTF(3)("Preparing to download file %s.\n", info->fileName); |
---|
[3268] | 160 | download(info); |
---|
[3254] | 161 | } |
---|
| 162 | |
---|
[3258] | 163 | /** |
---|
| 164 | \brief updates the source of orxonox. |
---|
| 165 | \param w The widget, that executed this Function. |
---|
| 166 | \param event The event that trigered this Function. |
---|
| 167 | \param button The Button, that triggered this event. |
---|
| 168 | */ |
---|
[3259] | 169 | gint OrxonoxGuiUpdate::updateSourceFunc(GtkWidget* w, GdkEventKey* event, void* bar) |
---|
[3254] | 170 | { |
---|
[3259] | 171 | ProgressBar* tmpBar = static_cast<ProgressBar*>(bar); |
---|
| 172 | tmpBar->setTotalSize(20); |
---|
| 173 | tmpBar->setProgress(tmpBar->getProgress()+1); |
---|
[3254] | 174 | } |
---|
[3263] | 175 | #endif /* HAVE_GTK2 */ |
---|
[3254] | 176 | |
---|
[3263] | 177 | #ifdef HAVE_CURL |
---|
| 178 | size_t OrxonoxGuiUpdate::curlWriteFunc (void* ptr, size_t size, size_t nmemb, FILE* stream) |
---|
| 179 | { |
---|
| 180 | return fwrite(ptr, size, nmemb, stream); |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | size_t OrxonoxGuiUpdate::curlReadFunc (void* ptr, size_t size, size_t nmemb, FILE* stream) |
---|
| 184 | { |
---|
| 185 | return fread(ptr, size, nmemb, stream); |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | |
---|
| 189 | int OrxonoxGuiUpdate::curlProgressFunc (ProgressBar* Bar, double totalSize, double progress, double upTotal, double upProgress) |
---|
| 190 | { |
---|
| 191 | Bar->setProgress(progress); |
---|
| 192 | Bar->setTotalSize(totalSize); |
---|
| 193 | return 0; |
---|
| 194 | } |
---|
| 195 | |
---|
[3266] | 196 | |
---|
[3268] | 197 | void* OrxonoxGuiUpdate::download (void* fileInfo) |
---|
[3263] | 198 | { |
---|
[3266] | 199 | |
---|
[3263] | 200 | PRINTF(3)("Downloading.\n"); |
---|
| 201 | FileInfo* info = (FileInfo*)fileInfo; |
---|
| 202 | CURL* curl; |
---|
| 203 | CURLcode res; |
---|
| 204 | FILE* outfile; |
---|
| 205 | curl = curl_easy_init(); |
---|
[3267] | 206 | char* fileOnNet = new char [strlen(info->webRoot)+strlen(info->fileName)+1]; |
---|
| 207 | strcpy (fileOnNet, info->webRoot); |
---|
| 208 | strcat (fileOnNet, info->fileName); |
---|
| 209 | char* fileOnDisk = new char [strlen(info->localRoot)+strlen(info->fileName)+1]; |
---|
| 210 | strcpy (fileOnDisk, info->localRoot); |
---|
| 211 | strcat (fileOnDisk, info->fileName); |
---|
[3263] | 212 | |
---|
| 213 | if(curl) |
---|
| 214 | { |
---|
[3267] | 215 | outfile = fopen(fileOnDisk, "w"); |
---|
[3263] | 216 | |
---|
[3267] | 217 | curl_easy_setopt(curl, CURLOPT_URL, fileOnNet); |
---|
[3263] | 218 | curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile); |
---|
| 219 | curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlWriteFunc); |
---|
| 220 | curl_easy_setopt(curl, CURLOPT_READFUNCTION, curlReadFunc); |
---|
| 221 | curl_easy_setopt(curl, CURLOPT_NOPROGRESS, FALSE); |
---|
| 222 | curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, curlProgressFunc); |
---|
| 223 | curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, info->Bar); |
---|
| 224 | |
---|
| 225 | res = curl_easy_perform(curl); |
---|
| 226 | |
---|
| 227 | fclose(outfile); |
---|
| 228 | curl_easy_cleanup(curl); |
---|
| 229 | } |
---|
| 230 | return NULL; |
---|
| 231 | } |
---|
| 232 | |
---|
[3268] | 233 | #ifdef HAVE_GTK2 |
---|
| 234 | gint OrxonoxGuiUpdate::cancelDownload(GtkWidget* w, GdkEventKey* event, void* bar) |
---|
| 235 | { |
---|
| 236 | } |
---|
| 237 | #endif /* HAVE_GTK2 */ |
---|
| 238 | |
---|
[3263] | 239 | #endif /* HAVE_CURL */ |
---|
| 240 | |
---|
| 241 | |
---|
| 242 | /* |
---|
| 243 | int main(int argc, char **argv) |
---|
| 244 | { |
---|
| 245 | GtkWidget *Window, *Frame, *Frame2; |
---|
| 246 | GtkAdjustment *adj; |
---|
| 247 | |
---|
| 248 | // Init thread |
---|
| 249 | g_thread_init(NULL); |
---|
| 250 | gtk_init(&argc, &argv); |
---|
| 251 | Window = gtk_window_new(GTK_WINDOW_TOPLEVEL); |
---|
| 252 | Frame = gtk_frame_new(NULL); |
---|
| 253 | gtk_frame_set_shadow_type(GTK_FRAME(Frame), GTK_SHADOW_OUT); |
---|
| 254 | gtk_container_add(GTK_CONTAINER(Window), Frame); |
---|
| 255 | Frame2 = gtk_frame_new(NULL); |
---|
| 256 | gtk_frame_set_shadow_type(GTK_FRAME(Frame2), GTK_SHADOW_IN); |
---|
| 257 | gtk_container_add(GTK_CONTAINER(Frame), Frame2); |
---|
| 258 | gtk_container_set_border_width(GTK_CONTAINER(Frame2), 5); |
---|
| 259 | adj = (GtkAdjustment*)gtk_adjustment_new(0, 0, 100, 0, 0, 0); |
---|
| 260 | Bar = gtk_progress_bar_new_with_adjustment(adj); |
---|
| 261 | gtk_container_add(GTK_CONTAINER(Frame2), Bar); |
---|
| 262 | gtk_widget_show_all(Window); |
---|
| 263 | |
---|
| 264 | if (!g_thread_create(&my_thread, argv[1], FALSE, NULL) != 0) |
---|
| 265 | g_warning("can't create the thread"); |
---|
| 266 | |
---|
| 267 | |
---|
| 268 | gdk_threads_enter(); |
---|
| 269 | gtk_main(); |
---|
| 270 | gdk_threads_leave(); |
---|
| 271 | return 0; |
---|
| 272 | } |
---|
| 273 | |
---|
| 274 | |
---|
| 275 | |
---|
| 276 | |
---|
| 277 | |
---|
| 278 | size_t writeFunc(void *ptr, size_t size, size_t nmemb, FILE *stream) |
---|
| 279 | { |
---|
| 280 | return fwrite(ptr, size, nmemb, stream); |
---|
| 281 | } |
---|
| 282 | |
---|
| 283 | size_t my_read_func(void *ptr, size_t size, size_t nmemb, FILE *stream) |
---|
| 284 | { |
---|
| 285 | return fread(ptr, size, nmemb, stream); |
---|
| 286 | } |
---|
| 287 | |
---|
| 288 | int my_progress_func(GtkWidget *Bar, |
---|
| 289 | double t, // dltotal |
---|
| 290 | double d, // dlnow |
---|
| 291 | double ultotal, |
---|
| 292 | double ulnow) |
---|
| 293 | { |
---|
| 294 | gdk_threads_enter(); |
---|
| 295 | gtk_progress_set_value(GTK_PROGRESS(Bar), d*100.0/t); |
---|
| 296 | gdk_threads_leave(); |
---|
| 297 | return 0; |
---|
| 298 | } |
---|
| 299 | |
---|
| 300 | void *my_thread(void *ptr) |
---|
| 301 | { |
---|
| 302 | CURL *curl; |
---|
| 303 | CURLcode res; |
---|
| 304 | FILE *outfile; |
---|
| 305 | gchar *url = ptr; |
---|
| 306 | |
---|
| 307 | curl = curl_easy_init(); |
---|
| 308 | if(curl) |
---|
| 309 | { |
---|
| 310 | outfile = fopen("test.curl", "w"); |
---|
| 311 | |
---|
| 312 | curl_easy_setopt(curl, CURLOPT_URL, url); |
---|
| 313 | curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile); |
---|
| 314 | curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_write_func); |
---|
| 315 | curl_easy_setopt(curl, CURLOPT_READFUNCTION, my_read_func); |
---|
| 316 | curl_easy_setopt(curl, CURLOPT_NOPROGRESS, FALSE); |
---|
| 317 | curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, my_progress_func); |
---|
| 318 | curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, Bar); |
---|
| 319 | |
---|
| 320 | res = curl_easy_perform(curl); |
---|
| 321 | |
---|
| 322 | fclose(outfile); |
---|
| 323 | curl_easy_cleanup(curl); |
---|
| 324 | } |
---|
| 325 | |
---|
| 326 | return NULL; |
---|
| 327 | } |
---|
| 328 | |
---|
| 329 | */ |
---|
| 330 | |
---|
| 331 | |
---|
| 332 | |
---|
| 333 | |
---|
| 334 | |
---|
| 335 | |
---|
| 336 | |
---|
| 337 | |
---|
| 338 | |
---|
| 339 | |
---|
| 340 | |
---|
| 341 | |
---|
| 342 | |
---|
| 343 | |
---|
| 344 | |
---|
| 345 | |
---|
| 346 | |
---|
| 347 | |
---|
| 348 | |
---|
| 349 | |
---|
| 350 | |
---|
| 351 | |
---|
| 352 | |
---|
| 353 | |
---|