[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> |
---|
[3263] | 32 | #include <curl/curl.h> |
---|
| 33 | #include <curl/types.h> |
---|
| 34 | #include <curl/easy.h> /* new for v7 */ |
---|
[3250] | 35 | using namespace std; |
---|
| 36 | |
---|
| 37 | /** |
---|
| 38 | \brief Creates an Audio-Frame |
---|
| 39 | */ |
---|
| 40 | OrxonoxGuiUpdate::OrxonoxGuiUpdate () |
---|
| 41 | { |
---|
[3253] | 42 | this->updateFrame = new Frame ("Update-Options:"); |
---|
[3261] | 43 | this->updateFrame->setGroupName ("update"); |
---|
[3253] | 44 | this->updateBox = new Box ('v'); |
---|
[3250] | 45 | |
---|
[3261] | 46 | // the Button for autoUpdating |
---|
| 47 | this->autoUpdate = new CheckButton ("auto update"); |
---|
| 48 | this->updateBox->fill(this->autoUpdate); |
---|
| 49 | this->autoUpdate->setFlagName ("update", "u", 0); |
---|
| 50 | this->autoUpdate->saveable = true; |
---|
[3253] | 51 | |
---|
[3261] | 52 | this->updateSourceWindowCreate (); |
---|
| 53 | this->updateBox->fill(this->updateSourceWindowGetButton()); |
---|
| 54 | |
---|
| 55 | this->updateDataWindowCreate (); |
---|
| 56 | this->updateBox->fill(this->updateDataWindowGetButton()); |
---|
| 57 | |
---|
[3253] | 58 | this->updateFrame->fill(this->updateBox); |
---|
[3250] | 59 | } |
---|
| 60 | |
---|
| 61 | /** |
---|
| 62 | \brief Return the Frame |
---|
| 63 | \return Returns the Audio-frame |
---|
| 64 | */ |
---|
| 65 | Widget* OrxonoxGuiUpdate::getWidget () |
---|
| 66 | { |
---|
| 67 | return updateFrame; |
---|
| 68 | } |
---|
[3253] | 69 | |
---|
[3261] | 70 | void OrxonoxGuiUpdate::updateDataWindowCreate (void) |
---|
[3253] | 71 | { |
---|
[3261] | 72 | updateDataWindow = new Window ("update orxonox::Data"); |
---|
| 73 | updateDataBox = new Box ('v'); |
---|
[3253] | 74 | |
---|
[3257] | 75 | // the close-Button of the Update Window. |
---|
[3261] | 76 | // updateWindowClose = new Button ("close"); |
---|
[3257] | 77 | #ifdef HAVE_GTK2 |
---|
[3261] | 78 | // updateWindowClose->connectSignal("button_press_event", updateWindow, Window::windowClose); |
---|
[3257] | 79 | #endif /* HAVE_GTK2 */ |
---|
[3261] | 80 | // updateWindowBox->fill(updateWindowClose); |
---|
[3253] | 81 | |
---|
[3261] | 82 | updateDataBar = new ProgressBar (); |
---|
| 83 | updateDataBox->fill(updateDataBar); |
---|
[3263] | 84 | |
---|
| 85 | updateDataBegin = new Button ("begin Download"); |
---|
| 86 | updateDataBegin->connectSignal ("button_press_event", updateDataBar, updateDataFunc); |
---|
| 87 | updateDataBox->fill(updateDataBegin); |
---|
| 88 | |
---|
[3261] | 89 | updateDataWindow->fill (updateDataBox); |
---|
[3253] | 90 | |
---|
[3261] | 91 | updateDataWindowButton = new Button ("update orxonox::Data"); |
---|
[3254] | 92 | #ifdef HAVE_GTK2 |
---|
[3261] | 93 | updateDataWindowButton->connectSignal("button_press_event", updateDataWindow, Window::windowOpen); |
---|
| 94 | updateDataWindow->connectSignal("destroy", updateDataWindow, Window::windowClose); |
---|
| 95 | updateDataWindow->connectSignal("delete_event", updateDataWindow, Window::windowClose); |
---|
[3253] | 96 | #endif /* HAVE_GTK2 */ |
---|
| 97 | |
---|
| 98 | } |
---|
| 99 | |
---|
[3258] | 100 | /** |
---|
| 101 | \returns A Pointer to the Button of the UpdaterWindow |
---|
| 102 | */ |
---|
[3259] | 103 | Button* OrxonoxGuiUpdate::updateDataWindowGetButton(void) |
---|
| 104 | { |
---|
[3261] | 105 | return updateDataWindowButton; |
---|
[3259] | 106 | } |
---|
| 107 | |
---|
| 108 | void OrxonoxGuiUpdate::updateSourceWindowCreate (void) |
---|
| 109 | { |
---|
| 110 | // the button, that opens this Window. |
---|
[3261] | 111 | updateSourceWindowButton = new Button ("update orxonox::Source"); |
---|
[3259] | 112 | |
---|
| 113 | // the Window itself |
---|
| 114 | updateSourceWindow = new Window ("update orxonox::Source"); |
---|
| 115 | |
---|
[3261] | 116 | updateSourceBox = new Box (); |
---|
[3259] | 117 | |
---|
| 118 | updateSourceBar = new ProgressBar (); |
---|
[3261] | 119 | updateSourceBox->fill(updateSourceBar); |
---|
[3259] | 120 | test = new Button ("increment"); |
---|
[3261] | 121 | |
---|
| 122 | #ifdef HAVE_GTK2 |
---|
[3259] | 123 | test->connectSignal("button_press_event", updateSourceBar, updateSourceFunc); |
---|
[3261] | 124 | #endif /* HAVE_GTK2 */ |
---|
| 125 | |
---|
| 126 | updateSourceBox->fill(test); |
---|
| 127 | updateSourceWindow->fill(updateSourceBox); |
---|
[3259] | 128 | #ifdef HAVE_GTK2 |
---|
[3261] | 129 | updateSourceWindowButton->connectSignal("button_press_event", updateSourceWindow, Window::windowOpen); |
---|
| 130 | updateSourceWindow->connectSignal("destroy", updateSourceWindow, Window::windowClose); |
---|
| 131 | updateSourceWindow->connectSignal("delete_event", updateSourceWindow, Window::windowClose); |
---|
[3259] | 132 | #endif /* HAVE_GTK2 */ |
---|
| 133 | |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | Button* OrxonoxGuiUpdate::updateSourceWindowGetButton(void) |
---|
| 137 | { |
---|
[3261] | 138 | return updateSourceWindowButton; |
---|
[3259] | 139 | } |
---|
| 140 | |
---|
| 141 | |
---|
| 142 | #ifdef HAVE_GTK2 |
---|
[3258] | 143 | /** |
---|
| 144 | \brief updates the Data of orxonox. |
---|
| 145 | \param w The widget, that executed this Function. |
---|
| 146 | \param event The event that trigered this Function. |
---|
| 147 | \param button The Button, that triggered this event. |
---|
| 148 | */ |
---|
[3263] | 149 | gint OrxonoxGuiUpdate::updateDataFunc(GtkWidget* w, GdkEventKey* event, void* bar) |
---|
[3254] | 150 | { |
---|
[3263] | 151 | FileInfo* info = new FileInfo; |
---|
[3254] | 152 | |
---|
[3263] | 153 | info->fileOnDisk = new char [5]; |
---|
| 154 | strcpy (info->fileOnDisk, "test"); |
---|
| 155 | info->fileOnNet = new char [100]; |
---|
| 156 | strcpy(info->fileOnNet, "http://www.orxonox.ethz.ch/files/taskHorizon.at.bermuda-funk.ogg"); |
---|
| 157 | info->Bar = (ProgressBar*)bar; |
---|
| 158 | PRINTF(3)("Preparing to download file %s.\n", info->fileOnNet); |
---|
| 159 | //downloadThread (info); |
---|
| 160 | |
---|
| 161 | if (!g_thread_create(&downloadThread, info, FALSE, NULL) != 0) |
---|
| 162 | PRINTF(1)("can't create the thread"); |
---|
[3254] | 163 | } |
---|
| 164 | |
---|
[3258] | 165 | /** |
---|
| 166 | \brief updates the source of orxonox. |
---|
| 167 | \param w The widget, that executed this Function. |
---|
| 168 | \param event The event that trigered this Function. |
---|
| 169 | \param button The Button, that triggered this event. |
---|
| 170 | */ |
---|
[3259] | 171 | gint OrxonoxGuiUpdate::updateSourceFunc(GtkWidget* w, GdkEventKey* event, void* bar) |
---|
[3254] | 172 | { |
---|
[3259] | 173 | ProgressBar* tmpBar = static_cast<ProgressBar*>(bar); |
---|
| 174 | tmpBar->setTotalSize(20); |
---|
| 175 | tmpBar->setProgress(tmpBar->getProgress()+1); |
---|
[3254] | 176 | } |
---|
[3263] | 177 | #endif /* HAVE_GTK2 */ |
---|
[3254] | 178 | |
---|
[3263] | 179 | #ifdef HAVE_CURL |
---|
| 180 | size_t OrxonoxGuiUpdate::curlWriteFunc (void* ptr, size_t size, size_t nmemb, FILE* stream) |
---|
| 181 | { |
---|
| 182 | return fwrite(ptr, size, nmemb, stream); |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | size_t OrxonoxGuiUpdate::curlReadFunc (void* ptr, size_t size, size_t nmemb, FILE* stream) |
---|
| 186 | { |
---|
| 187 | return fread(ptr, size, nmemb, stream); |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | |
---|
| 191 | int OrxonoxGuiUpdate::curlProgressFunc (ProgressBar* Bar, double totalSize, double progress, double upTotal, double upProgress) |
---|
| 192 | { |
---|
| 193 | // gdk_threads_enter(); |
---|
| 194 | Bar->setProgress(progress); |
---|
| 195 | Bar->setTotalSize(totalSize); |
---|
| 196 | // gdk_threads_leave(); |
---|
| 197 | return 0; |
---|
| 198 | |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | void* OrxonoxGuiUpdate::downloadThread (void* fileInfo) |
---|
| 202 | { |
---|
| 203 | PRINTF(3)("Downloading.\n"); |
---|
| 204 | FileInfo* info = (FileInfo*)fileInfo; |
---|
| 205 | CURL* curl; |
---|
| 206 | CURLcode res; |
---|
| 207 | FILE* outfile; |
---|
| 208 | curl = curl_easy_init(); |
---|
| 209 | |
---|
| 210 | if(curl) |
---|
| 211 | { |
---|
| 212 | outfile = fopen(info->fileOnDisk, "w"); |
---|
| 213 | |
---|
| 214 | curl_easy_setopt(curl, CURLOPT_URL, info->fileOnNet); |
---|
| 215 | curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile); |
---|
| 216 | curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlWriteFunc); |
---|
| 217 | curl_easy_setopt(curl, CURLOPT_READFUNCTION, curlReadFunc); |
---|
| 218 | curl_easy_setopt(curl, CURLOPT_NOPROGRESS, FALSE); |
---|
| 219 | curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, curlProgressFunc); |
---|
| 220 | curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, info->Bar); |
---|
| 221 | |
---|
| 222 | res = curl_easy_perform(curl); |
---|
| 223 | |
---|
| 224 | fclose(outfile); |
---|
| 225 | curl_easy_cleanup(curl); |
---|
| 226 | } |
---|
| 227 | return NULL; |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | #endif /* HAVE_CURL */ |
---|
| 231 | |
---|
| 232 | |
---|
| 233 | /* |
---|
| 234 | int main(int argc, char **argv) |
---|
| 235 | { |
---|
| 236 | GtkWidget *Window, *Frame, *Frame2; |
---|
| 237 | GtkAdjustment *adj; |
---|
| 238 | |
---|
| 239 | // Init thread |
---|
| 240 | g_thread_init(NULL); |
---|
| 241 | gtk_init(&argc, &argv); |
---|
| 242 | Window = gtk_window_new(GTK_WINDOW_TOPLEVEL); |
---|
| 243 | Frame = gtk_frame_new(NULL); |
---|
| 244 | gtk_frame_set_shadow_type(GTK_FRAME(Frame), GTK_SHADOW_OUT); |
---|
| 245 | gtk_container_add(GTK_CONTAINER(Window), Frame); |
---|
| 246 | Frame2 = gtk_frame_new(NULL); |
---|
| 247 | gtk_frame_set_shadow_type(GTK_FRAME(Frame2), GTK_SHADOW_IN); |
---|
| 248 | gtk_container_add(GTK_CONTAINER(Frame), Frame2); |
---|
| 249 | gtk_container_set_border_width(GTK_CONTAINER(Frame2), 5); |
---|
| 250 | adj = (GtkAdjustment*)gtk_adjustment_new(0, 0, 100, 0, 0, 0); |
---|
| 251 | Bar = gtk_progress_bar_new_with_adjustment(adj); |
---|
| 252 | gtk_container_add(GTK_CONTAINER(Frame2), Bar); |
---|
| 253 | gtk_widget_show_all(Window); |
---|
| 254 | |
---|
| 255 | if (!g_thread_create(&my_thread, argv[1], FALSE, NULL) != 0) |
---|
| 256 | g_warning("can't create the thread"); |
---|
| 257 | |
---|
| 258 | |
---|
| 259 | gdk_threads_enter(); |
---|
| 260 | gtk_main(); |
---|
| 261 | gdk_threads_leave(); |
---|
| 262 | return 0; |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | |
---|
| 266 | |
---|
| 267 | |
---|
| 268 | |
---|
| 269 | size_t writeFunc(void *ptr, size_t size, size_t nmemb, FILE *stream) |
---|
| 270 | { |
---|
| 271 | return fwrite(ptr, size, nmemb, stream); |
---|
| 272 | } |
---|
| 273 | |
---|
| 274 | size_t my_read_func(void *ptr, size_t size, size_t nmemb, FILE *stream) |
---|
| 275 | { |
---|
| 276 | return fread(ptr, size, nmemb, stream); |
---|
| 277 | } |
---|
| 278 | |
---|
| 279 | int my_progress_func(GtkWidget *Bar, |
---|
| 280 | double t, // dltotal |
---|
| 281 | double d, // dlnow |
---|
| 282 | double ultotal, |
---|
| 283 | double ulnow) |
---|
| 284 | { |
---|
| 285 | gdk_threads_enter(); |
---|
| 286 | gtk_progress_set_value(GTK_PROGRESS(Bar), d*100.0/t); |
---|
| 287 | gdk_threads_leave(); |
---|
| 288 | return 0; |
---|
| 289 | } |
---|
| 290 | |
---|
| 291 | void *my_thread(void *ptr) |
---|
| 292 | { |
---|
| 293 | CURL *curl; |
---|
| 294 | CURLcode res; |
---|
| 295 | FILE *outfile; |
---|
| 296 | gchar *url = ptr; |
---|
| 297 | |
---|
| 298 | curl = curl_easy_init(); |
---|
| 299 | if(curl) |
---|
| 300 | { |
---|
| 301 | outfile = fopen("test.curl", "w"); |
---|
| 302 | |
---|
| 303 | curl_easy_setopt(curl, CURLOPT_URL, url); |
---|
| 304 | curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile); |
---|
| 305 | curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_write_func); |
---|
| 306 | curl_easy_setopt(curl, CURLOPT_READFUNCTION, my_read_func); |
---|
| 307 | curl_easy_setopt(curl, CURLOPT_NOPROGRESS, FALSE); |
---|
| 308 | curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, my_progress_func); |
---|
| 309 | curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, Bar); |
---|
| 310 | |
---|
| 311 | res = curl_easy_perform(curl); |
---|
| 312 | |
---|
| 313 | fclose(outfile); |
---|
| 314 | curl_easy_cleanup(curl); |
---|
| 315 | } |
---|
| 316 | |
---|
| 317 | return NULL; |
---|
| 318 | } |
---|
| 319 | |
---|
| 320 | */ |
---|
| 321 | |
---|
| 322 | |
---|
| 323 | |
---|
| 324 | |
---|
| 325 | |
---|
| 326 | |
---|
| 327 | |
---|
| 328 | |
---|
| 329 | |
---|
| 330 | |
---|
| 331 | |
---|
| 332 | |
---|
| 333 | |
---|
| 334 | |
---|
| 335 | |
---|
| 336 | |
---|
| 337 | |
---|
| 338 | |
---|
| 339 | |
---|
| 340 | |
---|
| 341 | |
---|
| 342 | |
---|
| 343 | |
---|
| 344 | |
---|