Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/updater/src/gui/orxonox_gui_update.cc @ 3291

Last change on this file since 3291 was 3291, checked in by bensch, 19 years ago

orxonox/branches/updater: compiles without GTK again

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