[3250] | 1 | /* |
---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | Copyright (C) 2004 orx |
---|
| 4 | |
---|
| 5 | This program is free software; you can redistribute it and/or modify |
---|
| 6 | it under the terms of the GNU General Public License as published by |
---|
| 7 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 8 | any later version. |
---|
| 9 | |
---|
| 10 | This program is distributed in the hope that it will be useful, |
---|
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 13 | GNU General Public License for more details. |
---|
| 14 | |
---|
| 15 | You should have received a copy of the GNU General Public License |
---|
| 16 | along with this program; if not, write to the Free Software Foundation, |
---|
| 17 | Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
| 18 | |
---|
| 19 | |
---|
| 20 | ### File Specific: |
---|
| 21 | main-programmer: Benjamin Grauer |
---|
| 22 | |
---|
| 23 | */ |
---|
| 24 | |
---|
[4047] | 25 | #include "gui_update.h" |
---|
[4037] | 26 | #include <string.h> |
---|
[3250] | 27 | |
---|
[4047] | 28 | #include "gui.h" |
---|
[3250] | 29 | #include <stdio.h> |
---|
[3285] | 30 | #include <stdlib.h> |
---|
[3270] | 31 | |
---|
[3250] | 32 | using namespace std; |
---|
| 33 | |
---|
| 34 | /** |
---|
[3318] | 35 | \brief Creates an Update-Frame |
---|
[3250] | 36 | */ |
---|
[4056] | 37 | GuiUpdate::GuiUpdate(void) |
---|
[3250] | 38 | { |
---|
[4068] | 39 | FileDialog* dataDirDialog; //!< A FileDialog for the selection of the DataRepos |
---|
| 40 | Button* dataDirButton; //!< A Button for the selection of the DataRepos |
---|
| 41 | OptionLabel* dataDirLabel; //!< A Label fot the selection of the DataRepos |
---|
[4083] | 42 | |
---|
[4068] | 43 | |
---|
[4044] | 44 | this->tmpDir = NULL; |
---|
| 45 | this->homeDir = NULL; |
---|
| 46 | this->installSourceDir = NULL; |
---|
| 47 | this->userName = NULL; |
---|
| 48 | |
---|
[3285] | 49 | this->getSystemInfo(); |
---|
| 50 | |
---|
[3315] | 51 | this->updateFrame = new Frame("Update-Options:"); |
---|
| 52 | this->updateFrame->setGroupName("update"); |
---|
| 53 | this->updateBox = new Box('v'); |
---|
[4083] | 54 | |
---|
| 55 | dataDirButton = new Button("Data Directory"); |
---|
| 56 | dataDirLabel = new OptionLabel("DataDir", "unknown"); |
---|
| 57 | dataDirLabel->saveability(); |
---|
| 58 | dataDirDialog = new FileDialog("data-Repos-location"); |
---|
| 59 | dataDirDialog->setDefaultFileName("data"); |
---|
| 60 | dataDirDialog->setMask(DATA_IDENTIFIER); |
---|
| 61 | this->checkDataDir("../data/" DATA_IDENTIFIER, dataDirLabel); |
---|
| 62 | dataDirDialog->disableFileOpts(); |
---|
| 63 | dataDirDialog->setOpenUpButton(dataDirButton); |
---|
| 64 | //dataDirDialog->setChangeOption(dataDirLabel); |
---|
| 65 | dataDirDialog->setOKFunc(dataDirLabel, GuiUpdate::checkDataDir); |
---|
| 66 | updateBox->fill(dataDirButton); |
---|
| 67 | updateBox->fill(dataDirLabel); |
---|
| 68 | |
---|
[3281] | 69 | #ifdef HAVE_CURL |
---|
[3250] | 70 | |
---|
[3261] | 71 | // the Button for autoUpdating |
---|
[3315] | 72 | this->autoUpdate = new CheckButton("auto update"); |
---|
[3261] | 73 | this->updateBox->fill(this->autoUpdate); |
---|
[3315] | 74 | this->autoUpdate->setFlagName("update", "u", 0); |
---|
[3288] | 75 | this->autoUpdate->saveability(); |
---|
[3253] | 76 | |
---|
[4068] | 77 | |
---|
| 78 | |
---|
| 79 | |
---|
[3315] | 80 | this->updateSourceWindowCreate(); |
---|
[3261] | 81 | this->updateBox->fill(this->updateSourceWindowGetButton()); |
---|
| 82 | |
---|
[3315] | 83 | this->updateDataWindowCreate(); |
---|
[3261] | 84 | this->updateBox->fill(this->updateDataWindowGetButton()); |
---|
| 85 | |
---|
[3281] | 86 | #else /* HAVE_CURL */ |
---|
[4083] | 87 | Label* noCurlLabel = new Label("since you do not have cURL-support,\nupdate options are not availible"); |
---|
[3281] | 88 | this->updateBox->fill(noCurlLabel); |
---|
| 89 | #endif /* HAVE_CURL */ |
---|
[3253] | 90 | this->updateFrame->fill(this->updateBox); |
---|
[4024] | 91 | this->setMainWidget(this->updateFrame); |
---|
[4068] | 92 | |
---|
| 93 | |
---|
[3318] | 94 | } |
---|
[3281] | 95 | |
---|
[3318] | 96 | /** |
---|
| 97 | \brief Destructs the Update-stuff |
---|
| 98 | */ |
---|
[4056] | 99 | GuiUpdate::~GuiUpdate(void) |
---|
[3318] | 100 | { |
---|
| 101 | |
---|
[3250] | 102 | } |
---|
| 103 | |
---|
[4083] | 104 | /** |
---|
| 105 | \brief checks if the Folder containing selected File is data.oxd, and if so sets it. |
---|
| 106 | \param |
---|
| 107 | */ |
---|
| 108 | bool GuiUpdate::checkDataDir(const char* fileName, void* object) |
---|
| 109 | { |
---|
| 110 | if (!strcmp(fileName+(strlen(fileName)-strlen(DATA_IDENTIFIER)), DATA_IDENTIFIER)) |
---|
| 111 | { |
---|
| 112 | char* tmpName = new char[strlen(fileName)-strlen(DATA_IDENTIFIER)+1]; |
---|
| 113 | strncpy(tmpName, fileName, strlen(fileName)-strlen(DATA_IDENTIFIER)); |
---|
| 114 | tmpName[strlen(fileName)-strlen(DATA_IDENTIFIER)] = '\0'; |
---|
| 115 | static_cast<OptionLabel*>(object)->setValue(tmpName); |
---|
| 116 | delete tmpName; |
---|
| 117 | return true; |
---|
| 118 | } |
---|
| 119 | else |
---|
| 120 | return false; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | |
---|
[3285] | 124 | /** |
---|
| 125 | \brief Look what info we can get from this system |
---|
| 126 | */ |
---|
[4056] | 127 | bool GuiUpdate::getSystemInfo(void) |
---|
[3285] | 128 | { |
---|
| 129 | PRINTF(3)("Grabbing system information\n"); |
---|
[3315] | 130 | this->tmpDir = getenv("TMPDIR"); |
---|
| 131 | if(!tmpDir) |
---|
| 132 | this->tmpDir = "/tmp"; |
---|
| 133 | PRINTF(4)("Temporary directory is: %s\n", this->tmpDir); |
---|
[3285] | 134 | |
---|
| 135 | #ifdef __WIN32__ |
---|
[3315] | 136 | this->homeDir = getenv("USERPROFILE"); |
---|
[3285] | 137 | #else |
---|
[3315] | 138 | this->homeDir = getenv("HOME"); |
---|
[3285] | 139 | #endif |
---|
| 140 | PRINTF(4)("Home directory is %s\n", homeDir); |
---|
| 141 | |
---|
[3315] | 142 | this->installDataDir = "/usr/share/games/orxonox"; |
---|
| 143 | PRINTF(4)("Installation of orxonox-data will go to this directory: %s\n", this->installDataDir); |
---|
[3285] | 144 | |
---|
[3315] | 145 | this->installSourceDir = "/usr/games/bin"; |
---|
| 146 | PRINTF(4)("Installation of orxonox-source will go to this directory: %s\n", this->installSourceDir); |
---|
[3285] | 147 | |
---|
[3315] | 148 | this->userName = getenv("USER"); |
---|
| 149 | PRINTF(4)("Logged in username is: %s\n", this->userName); |
---|
[3285] | 150 | } |
---|
| 151 | |
---|
[3281] | 152 | #ifdef HAVE_CURL |
---|
[4056] | 153 | bool* GuiUpdate::checkForUpdates(void) |
---|
[3286] | 154 | { |
---|
| 155 | PRINTF(3)("checking for new version of orxonox\n"); |
---|
| 156 | FileInfo updateFileInfo; |
---|
| 157 | updateFileInfo.fileName = "update_info"; |
---|
| 158 | updateFileInfo.webRoot = "http://www.orxonox.ethz.ch/files/data"; |
---|
[3298] | 159 | updateFileInfo.localRoot = this->tmpDir; |
---|
[3286] | 160 | |
---|
| 161 | download(&updateFileInfo); |
---|
| 162 | } |
---|
[4062] | 163 | |
---|
[3275] | 164 | /** |
---|
| 165 | \brief Creates a window, and all it contains for the Data-update. |
---|
| 166 | */ |
---|
[4056] | 167 | void GuiUpdate::updateDataWindowCreate(void) |
---|
[3253] | 168 | { |
---|
[3315] | 169 | this->updateDataWindow = new Window("update orxonox::Data"); |
---|
| 170 | this->updateDataBox = new Box('v'); |
---|
[3253] | 171 | |
---|
[3257] | 172 | // the close-Button of the Update Window. |
---|
[3315] | 173 | // updateWindowClose = new Button("close"); |
---|
[4062] | 174 | #ifdef HAVE_GTK2 |
---|
[3261] | 175 | // updateWindowClose->connectSignal("button_press_event", updateWindow, Window::windowClose); |
---|
[3257] | 176 | #endif /* HAVE_GTK2 */ |
---|
[3261] | 177 | // updateWindowBox->fill(updateWindowClose); |
---|
[3253] | 178 | |
---|
[3315] | 179 | this->updateDataBar = new ProgressBar(); |
---|
| 180 | this->updateDataBox->fill(this->updateDataBar); |
---|
[3263] | 181 | |
---|
[3274] | 182 | FileInfo* dataInfo = new FileInfo; |
---|
[3315] | 183 | dataInfo->bar = this->updateDataBar; |
---|
[3274] | 184 | |
---|
[3315] | 185 | this->updateDataBegin = new Button("begin."); |
---|
| 186 | dataInfo->stateButton = this->updateDataBegin; |
---|
[4062] | 187 | #ifdef HAVE_GTK2 |
---|
[3315] | 188 | dataInfo->buttonSignal = updateDataBegin->connectSignal("button_press_event", dataInfo, updateDataFunc); |
---|
[3291] | 189 | #endif /* HAVE_GTK2 */ |
---|
[3315] | 190 | this->updateDataBox->fill(this->updateDataBegin); |
---|
[3263] | 191 | |
---|
[3315] | 192 | this->updateDataWindow->fill(this->updateDataBox); |
---|
[3253] | 193 | |
---|
[3315] | 194 | this->updateDataWindowButton = new Button("update orxonox::Data"); |
---|
[4062] | 195 | #ifdef HAVE_GTK2 |
---|
[3315] | 196 | this->updateDataWindowButton->connectSignal("button_press_event", this->updateDataWindow, Window::windowOpen); |
---|
| 197 | this->updateDataWindow->connectSignal("destroy", this->updateDataWindow, Window::windowClose); |
---|
| 198 | this->updateDataWindow->connectSignal("delete_event", this->updateDataWindow, Window::windowClose); |
---|
[3253] | 199 | #endif /* HAVE_GTK2 */ |
---|
| 200 | |
---|
| 201 | } |
---|
| 202 | |
---|
[3258] | 203 | /** |
---|
[3275] | 204 | \returns A Pointer to the Button of the UpdaterDataWindow |
---|
[3258] | 205 | */ |
---|
[4056] | 206 | Button* GuiUpdate::updateDataWindowGetButton(void) |
---|
[3259] | 207 | { |
---|
[3315] | 208 | return this->updateDataWindowButton; |
---|
[3259] | 209 | } |
---|
| 210 | |
---|
[3275] | 211 | /** |
---|
| 212 | \brief Creates a window, and all it contains for the Source-update. |
---|
| 213 | */ |
---|
[4056] | 214 | void GuiUpdate::updateSourceWindowCreate(void) |
---|
[3259] | 215 | { |
---|
| 216 | // the button, that opens this Window. |
---|
[3315] | 217 | this->updateSourceWindowButton = new Button("update orxonox::Source"); |
---|
[3259] | 218 | |
---|
| 219 | // the Window itself |
---|
[3315] | 220 | this->updateSourceWindow = new Window("update orxonox::Source"); |
---|
[3259] | 221 | |
---|
[3315] | 222 | this->updateSourceBox = new Box(); |
---|
[3259] | 223 | |
---|
[3315] | 224 | this->updateSourceBar = new ProgressBar(); |
---|
| 225 | this->updateSourceBox->fill(this->updateSourceBar); |
---|
| 226 | test = new Button("increment"); |
---|
[3261] | 227 | |
---|
| 228 | #ifdef HAVE_GTK2 |
---|
[3259] | 229 | test->connectSignal("button_press_event", updateSourceBar, updateSourceFunc); |
---|
[3261] | 230 | #endif /* HAVE_GTK2 */ |
---|
| 231 | |
---|
[3315] | 232 | this->updateSourceBox->fill(test); |
---|
| 233 | this->updateSourceWindow->fill(updateSourceBox); |
---|
[3259] | 234 | #ifdef HAVE_GTK2 |
---|
[3315] | 235 | this->updateSourceWindowButton->connectSignal("button_press_event", this->updateSourceWindow, Window::windowOpen); |
---|
| 236 | this->updateSourceWindow->connectSignal("destroy", this->updateSourceWindow, Window::windowClose); |
---|
| 237 | this->updateSourceWindow->connectSignal("delete_event", this->updateSourceWindow, Window::windowClose); |
---|
[3259] | 238 | #endif /* HAVE_GTK2 */ |
---|
| 239 | |
---|
| 240 | } |
---|
| 241 | |
---|
[3275] | 242 | /** |
---|
| 243 | \returns A Pointer to the Button of the UpdaterSourceWindow |
---|
| 244 | */ |
---|
[4056] | 245 | Button* GuiUpdate::updateSourceWindowGetButton(void) |
---|
[3259] | 246 | { |
---|
[3315] | 247 | return this->updateSourceWindowButton; |
---|
[3259] | 248 | } |
---|
| 249 | |
---|
| 250 | |
---|
| 251 | #ifdef HAVE_GTK2 |
---|
[3258] | 252 | /** |
---|
| 253 | \brief updates the Data of orxonox. |
---|
| 254 | \param w The widget, that executed this Function. |
---|
| 255 | \param event The event that trigered this Function. |
---|
| 256 | \param button The Button, that triggered this event. |
---|
| 257 | */ |
---|
[4056] | 258 | gint GuiUpdate::updateDataFunc(GtkWidget* w, GdkEventKey* event, void* info) |
---|
[3254] | 259 | { |
---|
[3315] | 260 | FileInfo* dataInfo =(FileInfo*)info; |
---|
[3254] | 261 | |
---|
[3274] | 262 | dataInfo->fileName = "02%20orxonox%203.mp3"; |
---|
| 263 | dataInfo->webRoot = "http://www.orxonox.ethz.ch/files/"; |
---|
| 264 | dataInfo->localRoot = "./"; |
---|
| 265 | PRINTF(3)("Preparing to download file %s.\n", dataInfo->fileName); |
---|
[3286] | 266 | downloadWithStyle(dataInfo); |
---|
[3254] | 267 | } |
---|
| 268 | |
---|
[3258] | 269 | /** |
---|
| 270 | \brief updates the source of orxonox. |
---|
| 271 | \param w The widget, that executed this Function. |
---|
| 272 | \param event The event that trigered this Function. |
---|
| 273 | \param button The Button, that triggered this event. |
---|
| 274 | */ |
---|
[4056] | 275 | gint GuiUpdate::updateSourceFunc(GtkWidget* w, GdkEventKey* event, void* bar) |
---|
[3254] | 276 | { |
---|
[3259] | 277 | ProgressBar* tmpBar = static_cast<ProgressBar*>(bar); |
---|
| 278 | tmpBar->setTotalSize(20); |
---|
| 279 | tmpBar->setProgress(tmpBar->getProgress()+1); |
---|
[3254] | 280 | } |
---|
[3263] | 281 | #endif /* HAVE_GTK2 */ |
---|
[3254] | 282 | |
---|
[3275] | 283 | /** |
---|
| 284 | \brief The Function Curl calls to write out the File. |
---|
| 285 | \param ptr A Pointer to the date to write. |
---|
| 286 | \param size The size in bytes of one nmemb to write. |
---|
| 287 | \param nmemb The Count of size to write. |
---|
| 288 | \param stream Filehandler to write to. |
---|
| 289 | */ |
---|
[4056] | 290 | size_t GuiUpdate::curlWriteFunc(void* ptr, size_t size, size_t nmemb, FILE* stream) |
---|
[3263] | 291 | { |
---|
| 292 | return fwrite(ptr, size, nmemb, stream); |
---|
| 293 | } |
---|
| 294 | |
---|
[3275] | 295 | /** |
---|
| 296 | \brief The Function Curl calls to write out the File. |
---|
| 297 | \param ptr A Pointer to the date to write to. |
---|
| 298 | \param size The size in bytes of one nmemb to write. |
---|
| 299 | \param nmemb The Count of size to write. |
---|
| 300 | \param stream Filehandler to get data from. |
---|
| 301 | */ |
---|
[4056] | 302 | size_t GuiUpdate::curlReadFunc(void* ptr, size_t size, size_t nmemb, FILE* stream) |
---|
[3263] | 303 | { |
---|
| 304 | return fread(ptr, size, nmemb, stream); |
---|
| 305 | } |
---|
| 306 | |
---|
| 307 | |
---|
[3275] | 308 | /** |
---|
| 309 | \brief An update Function for the GUI, to show the progress. |
---|
| 310 | \param Bar th ProgressBar to update |
---|
| 311 | \param totalSize The total size of the download in bytes. |
---|
| 312 | \param progress The current Progress of the download in bytes. |
---|
| 313 | \param upTotal not needed |
---|
| 314 | \param upProgress not needed |
---|
| 315 | */ |
---|
[4056] | 316 | int GuiUpdate::curlProgressFunc(ProgressBar* bar, double totalSize, double progress, double upTotal, double upProgress) |
---|
[3263] | 317 | { |
---|
[3315] | 318 | bar->setProgress(progress); |
---|
| 319 | bar->setTotalSize(totalSize); |
---|
[3274] | 320 | #ifdef HAVE_GTK2 |
---|
| 321 | while(gtk_events_pending()) gtk_main_iteration(); |
---|
| 322 | #endif |
---|
[3263] | 323 | return 0; |
---|
| 324 | } |
---|
| 325 | |
---|
[3275] | 326 | /** |
---|
[3315] | 327 | \brief The Curl handle for only one CURL(static). |
---|
[3275] | 328 | */ |
---|
[4056] | 329 | CURL* GuiUpdate::curlHandle = NULL; |
---|
[3273] | 330 | |
---|
[4064] | 331 | //! A bool parameter that shows if we are downloading. |
---|
[4056] | 332 | bool GuiUpdate::isDownloading = false; |
---|
[3266] | 333 | |
---|
[4064] | 334 | //! A parameter to see, if downloading has been canceled |
---|
| 335 | bool GuiUpdate::downloadCanceled = false; |
---|
[3286] | 336 | |
---|
[3275] | 337 | /** |
---|
[3286] | 338 | \brief Initializes a Download without displaying it. |
---|
[3275] | 339 | \param fileInfo the FileInfo. |
---|
[3286] | 340 | |
---|
| 341 | !! BE AWARE THIS WILL NOT BE THREADED. !! |
---|
[3275] | 342 | */ |
---|
[4056] | 343 | bool GuiUpdate::download(void* fileInfo) |
---|
[3263] | 344 | { |
---|
[3315] | 345 | if(isDownloading) |
---|
[3274] | 346 | { |
---|
| 347 | PRINTF(2)("unable to Download. already getting some file\n"); |
---|
| 348 | return false; |
---|
| 349 | } |
---|
[3263] | 350 | PRINTF(3)("Downloading.\n"); |
---|
[3315] | 351 | FileInfo* info =(FileInfo*)fileInfo; |
---|
[3263] | 352 | CURLcode res; |
---|
[3286] | 353 | CURL* localCurl; |
---|
| 354 | localCurl = curl_easy_init(); |
---|
| 355 | char* fileOnNet = new char [strlen(info->webRoot)+strlen(info->fileName)+2]; |
---|
[3315] | 356 | strcpy(fileOnNet, info->webRoot); |
---|
| 357 | if(fileOnNet[strlen(fileOnNet)] != '/') //!< \todo windows-shit |
---|
| 358 | strcat(fileOnNet, "/"); |
---|
| 359 | strcat(fileOnNet, info->fileName); |
---|
[3286] | 360 | char* fileOnDisk = new char [strlen(info->localRoot)+strlen(info->fileName)+2]; |
---|
[3315] | 361 | strcpy(fileOnDisk, info->localRoot); |
---|
| 362 | if(fileOnDisk[strlen(fileOnDisk)] != '/') //!< \todo windows-shit |
---|
| 363 | strcat(fileOnDisk, "/"); |
---|
| 364 | strcat(fileOnDisk, info->fileName); |
---|
[3286] | 365 | |
---|
| 366 | if(localCurl) |
---|
| 367 | { |
---|
| 368 | |
---|
| 369 | info->fileHandle = fopen(fileOnDisk, "w"); |
---|
| 370 | |
---|
| 371 | curl_easy_setopt(localCurl, CURLOPT_URL, fileOnNet); |
---|
| 372 | curl_easy_setopt(localCurl, CURLOPT_WRITEDATA, info->fileHandle); |
---|
| 373 | curl_easy_setopt(localCurl, CURLOPT_WRITEFUNCTION, curlWriteFunc); |
---|
| 374 | curl_easy_setopt(localCurl, CURLOPT_READFUNCTION, curlReadFunc); |
---|
[3291] | 375 | curl_easy_setopt(localCurl, CURLOPT_NOPROGRESS, true); |
---|
[3286] | 376 | |
---|
| 377 | curl_easy_perform(localCurl); |
---|
| 378 | |
---|
| 379 | curl_easy_cleanup(localCurl); |
---|
[3315] | 380 | fclose(info->fileHandle); |
---|
[3286] | 381 | } |
---|
| 382 | } |
---|
| 383 | |
---|
| 384 | /** |
---|
| 385 | \brief Initializes a Download with some style. |
---|
| 386 | \param fileInfo the FileInfo. |
---|
| 387 | \todo release thread-lock. |
---|
| 388 | |
---|
| 389 | Downloading with a Button that gets a different Name while downloading, and a Bar, that gets to be updated. More to be followed |
---|
| 390 | */ |
---|
[4056] | 391 | bool GuiUpdate::downloadWithStyle(void* fileInfo) |
---|
[3286] | 392 | { |
---|
[3315] | 393 | if(isDownloading) |
---|
[3286] | 394 | { |
---|
| 395 | PRINTF(2)("unable to Download. already getting some file\n"); |
---|
| 396 | return false; |
---|
| 397 | } |
---|
| 398 | PRINTF(3)("Downloading.\n"); |
---|
[3315] | 399 | FileInfo* info =(FileInfo*)fileInfo; |
---|
[3286] | 400 | CURLcode res; |
---|
[3270] | 401 | curlHandle = curl_easy_init(); |
---|
[3267] | 402 | char* fileOnNet = new char [strlen(info->webRoot)+strlen(info->fileName)+1]; |
---|
[3315] | 403 | strcpy(fileOnNet, info->webRoot); |
---|
| 404 | strcat(fileOnNet, info->fileName); |
---|
[3267] | 405 | char* fileOnDisk = new char [strlen(info->localRoot)+strlen(info->fileName)+1]; |
---|
[3315] | 406 | strcpy(fileOnDisk, info->localRoot); |
---|
| 407 | strcat(fileOnDisk, info->fileName); |
---|
[3273] | 408 | |
---|
[3270] | 409 | if(curlHandle) |
---|
[3263] | 410 | { |
---|
[3271] | 411 | |
---|
[3272] | 412 | info->fileHandle = fopen(fileOnDisk, "w"); |
---|
[3274] | 413 | |
---|
[3270] | 414 | curl_easy_setopt(curlHandle, CURLOPT_URL, fileOnNet); |
---|
[3272] | 415 | curl_easy_setopt(curlHandle, CURLOPT_WRITEDATA, info->fileHandle); |
---|
[3270] | 416 | curl_easy_setopt(curlHandle, CURLOPT_WRITEFUNCTION, curlWriteFunc); |
---|
| 417 | curl_easy_setopt(curlHandle, CURLOPT_READFUNCTION, curlReadFunc); |
---|
[3291] | 418 | curl_easy_setopt(curlHandle, CURLOPT_NOPROGRESS, false); |
---|
[3270] | 419 | curl_easy_setopt(curlHandle, CURLOPT_PROGRESSFUNCTION, curlProgressFunc); |
---|
[3274] | 420 | curl_easy_setopt(curlHandle, CURLOPT_PROGRESSDATA, info->bar); |
---|
[3271] | 421 | |
---|
[3315] | 422 | if(!isDownloading) |
---|
[3271] | 423 | { |
---|
[3291] | 424 | #ifdef HAVE_GTK2 |
---|
[3284] | 425 | info->stateButton->disconnectSignal(info->buttonSignal); |
---|
| 426 | info->buttonSignal = info->stateButton->connectSignal("button_press_event", info, cancelDownload); |
---|
[3291] | 427 | #endif /* HAVE_GTK2 */ |
---|
[3284] | 428 | info->stateButton->setTitle("please wait"); |
---|
| 429 | |
---|
[3273] | 430 | downloadThread(info); |
---|
[3274] | 431 | downloadThreadFinished(info); |
---|
[3273] | 432 | |
---|
[3272] | 433 | // res = curl_easy_perform(curlHandle); |
---|
| 434 | |
---|
| 435 | // fclose(outfile); |
---|
[3271] | 436 | } |
---|
[3274] | 437 | else |
---|
| 438 | PRINTF(1)("thread already in use\n"); |
---|
| 439 | |
---|
[3263] | 440 | } |
---|
[3274] | 441 | return true; |
---|
[3263] | 442 | } |
---|
[3275] | 443 | |
---|
| 444 | /** |
---|
[3315] | 445 | \brief The downloading process(either threaded or not). |
---|
[3275] | 446 | \param fileInfo the FileInfo. |
---|
[3282] | 447 | |
---|
| 448 | \todo Threads get locked, if the cancel button is pressed in to small intervals. |
---|
[3275] | 449 | */ |
---|
[4056] | 450 | void* GuiUpdate::downloadThread(void* fileInfo) |
---|
[3271] | 451 | { |
---|
[3284] | 452 | isDownloading = true; |
---|
[3274] | 453 | curl_easy_perform(curlHandle); |
---|
| 454 | } |
---|
| 455 | |
---|
[3275] | 456 | /** |
---|
| 457 | \brief Finishes a downloading process. |
---|
| 458 | \param fileInfo the FileInfo. |
---|
| 459 | */ |
---|
[4056] | 460 | void* GuiUpdate::downloadThreadFinished(void* fileInfo) |
---|
[3274] | 461 | { |
---|
[3315] | 462 | FileInfo* info =(FileInfo*)fileInfo; |
---|
| 463 | if(curlHandle) |
---|
[3271] | 464 | curl_easy_cleanup(curlHandle); |
---|
[3274] | 465 | |
---|
[3271] | 466 | PRINTF(3)("Closing the downloaded file.\n"); |
---|
[3272] | 467 | fclose(info->fileHandle); |
---|
| 468 | |
---|
[3315] | 469 | if(isDownloading) |
---|
[3274] | 470 | info->stateButton->setTitle("go on"); |
---|
| 471 | // else |
---|
| 472 | // info->stateButton->setTitle("done"); |
---|
[3291] | 473 | #ifdef HAVE_GTK2 |
---|
[3274] | 474 | info->stateButton->disconnectSignal(info->buttonSignal); |
---|
| 475 | info->buttonSignal = info->stateButton->connectSignal("button_press_event", info, updateDataFunc); |
---|
[3291] | 476 | #endif /* HAVE_GTK2 */ |
---|
[3272] | 477 | isDownloading = false; |
---|
[3274] | 478 | |
---|
[3271] | 479 | } |
---|
| 480 | |
---|
[3268] | 481 | #ifdef HAVE_GTK2 |
---|
[3275] | 482 | /** |
---|
| 483 | \brief canceles a downloading session. |
---|
| 484 | \param w The widget, that executed this Function. |
---|
| 485 | \param event The event that trigered this Function. |
---|
| 486 | \param bar The Bar, that triggered this event. |
---|
| 487 | |
---|
| 488 | \todo canceling a session in non-threaded mode. |
---|
| 489 | */ |
---|
[4056] | 490 | gint GuiUpdate::cancelDownload(GtkWidget* w, GdkEventKey* event, void* bar) |
---|
[3268] | 491 | { |
---|
[4061] | 492 | PRINTF(4)("Cannot cancle the Downloading process until after this File\n"); |
---|
[3268] | 493 | } |
---|
| 494 | #endif /* HAVE_GTK2 */ |
---|
| 495 | |
---|
[3263] | 496 | #endif /* HAVE_CURL */ |
---|