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
Line 
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>
32#include <stdlib.h>
33
34using namespace std;
35
36/**
37   \brief Creates an Audio-Frame
38*/
39OrxonoxGuiUpdate::OrxonoxGuiUpdate ()
40{
41  this->getSystemInfo();
42#ifdef HAVE_CURL
43  this->checkForUpdates();
44#endif /* HAVE_CURL */
45
46  this->updateFrame = new Frame ("Update-Options:");
47  this->updateFrame->setGroupName ("update");
48  this->updateBox = new Box ('v');
49#ifdef HAVE_CURL
50
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);
55  this->autoUpdate->saveability();
56
57  this->updateSourceWindowCreate ();
58  this->updateBox->fill(this->updateSourceWindowGetButton());
59
60  this->updateDataWindowCreate ();
61  this->updateBox->fill(this->updateDataWindowGetButton());
62
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 */
67  this->updateFrame->fill(this->updateBox);
68
69}
70
71/**
72   \brief Return the Frame
73   \return Returns the Audio-frame
74*/
75Widget* OrxonoxGuiUpdate::getWidget ()
76{
77  return updateFrame;
78}
79
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";
99  PRINTF(4)("Installation of orxonox-data will go to this directory: %s\n", installDataDir);
100
101  installSourceDir = "/usr/games/bin";
102  PRINTF(4)("Installation of orxonox-source will go to this directory: %s\n", installSourceDir);
103
104  userName = getenv("USER");
105  PRINTF(4)("Logged in username is: %s\n", userName);
106}
107
108#ifdef HAVE_CURL
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
121/**
122   \brief Creates a window, and all it contains for the Data-update.
123*/
124void OrxonoxGuiUpdate::updateDataWindowCreate (void)
125{
126  updateDataWindow = new Window ("update orxonox::Data");   
127  updateDataBox = new Box ('v');
128
129  // the close-Button of the Update Window.
130  //  updateWindowClose = new Button ("close");
131#ifdef HAVE_GTK2
132  //  updateWindowClose->connectSignal("button_press_event", updateWindow, Window::windowClose);
133#endif /* HAVE_GTK2 */
134  //  updateWindowBox->fill(updateWindowClose);
135
136  updateDataBar = new ProgressBar ();
137  updateDataBox->fill(updateDataBar);
138
139  FileInfo* dataInfo = new FileInfo;
140  dataInfo->bar = updateDataBar;
141
142  updateDataBegin = new Button ("begin.");
143  dataInfo->stateButton = updateDataBegin;
144#ifdef HAVE_GTK2
145  dataInfo->buttonSignal = updateDataBegin->connectSignal ("button_press_event", dataInfo, updateDataFunc);
146#endif /* HAVE_GTK2 */
147  updateDataBox->fill(updateDataBegin);
148
149  updateDataWindow->fill (updateDataBox);
150
151  updateDataWindowButton = new Button ("update orxonox::Data");
152#ifdef HAVE_GTK2
153  updateDataWindowButton->connectSignal("button_press_event", updateDataWindow, Window::windowOpen);
154  updateDataWindow->connectSignal("destroy", updateDataWindow, Window::windowClose);
155  updateDataWindow->connectSignal("delete_event", updateDataWindow, Window::windowClose);
156#endif /* HAVE_GTK2 */
157
158}
159
160/**
161   \returns A Pointer to the Button of the UpdaterDataWindow
162*/
163Button* OrxonoxGuiUpdate::updateDataWindowGetButton(void)
164{
165  return updateDataWindowButton;
166}
167
168/**
169   \brief Creates a window, and all it contains for the Source-update.
170*/
171void OrxonoxGuiUpdate::updateSourceWindowCreate (void)
172{
173  // the button, that opens this Window.
174  updateSourceWindowButton = new Button ("update orxonox::Source");
175
176  // the Window itself
177  updateSourceWindow = new Window ("update orxonox::Source");
178
179  updateSourceBox = new Box ();
180
181  updateSourceBar = new ProgressBar ();
182  updateSourceBox->fill(updateSourceBar);
183  test = new Button ("increment");
184
185#ifdef HAVE_GTK2
186  test->connectSignal("button_press_event", updateSourceBar, updateSourceFunc);
187#endif /* HAVE_GTK2 */
188
189  updateSourceBox->fill(test);
190  updateSourceWindow->fill(updateSourceBox); 
191#ifdef HAVE_GTK2
192  updateSourceWindowButton->connectSignal("button_press_event", updateSourceWindow, Window::windowOpen);
193  updateSourceWindow->connectSignal("destroy", updateSourceWindow, Window::windowClose);
194  updateSourceWindow->connectSignal("delete_event", updateSourceWindow, Window::windowClose);
195#endif /* HAVE_GTK2 */
196
197}
198
199/**
200   \returns A Pointer to the Button of the UpdaterSourceWindow
201*/
202Button* OrxonoxGuiUpdate::updateSourceWindowGetButton(void)
203{
204  return updateSourceWindowButton;
205}
206
207
208#ifdef HAVE_GTK2
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*/
215gint OrxonoxGuiUpdate::updateDataFunc(GtkWidget* w, GdkEventKey* event, void* info)
216{
217  FileInfo* dataInfo = (FileInfo*)info;
218
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);
223  downloadWithStyle(dataInfo);
224}
225
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*/
232gint OrxonoxGuiUpdate::updateSourceFunc(GtkWidget* w, GdkEventKey* event, void* bar)
233{
234  ProgressBar* tmpBar = static_cast<ProgressBar*>(bar);
235  tmpBar->setTotalSize(20);
236  tmpBar->setProgress(tmpBar->getProgress()+1);
237}
238#endif /* HAVE_GTK2 */
239
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*/
247size_t OrxonoxGuiUpdate::curlWriteFunc (void* ptr, size_t size, size_t nmemb, FILE* stream)
248{
249  return fwrite(ptr, size, nmemb, stream);
250}
251
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*/
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
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*/
273int OrxonoxGuiUpdate::curlProgressFunc (ProgressBar* Bar, double totalSize, double progress, double upTotal, double upProgress)
274{
275  Bar->setProgress(progress);
276  Bar->setTotalSize(totalSize);
277#ifdef HAVE_GTK2
278#ifndef HAVE_PTHREAD_H
279  while(gtk_events_pending()) gtk_main_iteration();
280#endif
281#endif
282  return 0;
283}
284
285/**
286   \brief The Curl handle for only one CURL (static).
287*/
288CURL* OrxonoxGuiUpdate::curlHandle = NULL;
289
290#ifdef HAVE_PTHREAD_H
291/** \brief The download Thread ID */
292pthread_t* OrxonoxGuiUpdate::downloadThreadID = new pthread_t;
293/**   \brief The download Thread ID*/
294pthread_t* OrxonoxGuiUpdate::downloadThreadFinishID = new pthread_t;
295#endif /* HAVE_PTHREAD_H */
296/**
297   \brief A bool parameter that shows if we are downloading.
298*/
299bool OrxonoxGuiUpdate::isDownloading = false;
300
301
302/**
303   \brief Initializes a Download without displaying it.
304   \param fileInfo the FileInfo.
305
306   !! BE AWARE THIS WILL NOT BE THREADED. !!
307*/
308bool OrxonoxGuiUpdate::download(void* fileInfo)
309{
310  if (isDownloading)
311    {
312      PRINTF(2)("unable to Download. already getting some file\n");
313      return false;
314    }
315  PRINTF(3)("Downloading.\n");
316  FileInfo* info = (FileInfo*)fileInfo;
317  CURLcode res;
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);
340      curl_easy_setopt(localCurl, CURLOPT_NOPROGRESS, true);
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;
366  curlHandle = curl_easy_init();
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);
373
374  if(curlHandle)
375    {
376     
377      info->fileHandle = fopen(fileOnDisk, "w");
378     
379      curl_easy_setopt(curlHandle, CURLOPT_URL, fileOnNet);
380      curl_easy_setopt(curlHandle, CURLOPT_WRITEDATA, info->fileHandle);
381      curl_easy_setopt(curlHandle, CURLOPT_WRITEFUNCTION, curlWriteFunc);
382      curl_easy_setopt(curlHandle, CURLOPT_READFUNCTION, curlReadFunc);
383      curl_easy_setopt(curlHandle, CURLOPT_NOPROGRESS, false);
384      curl_easy_setopt(curlHandle, CURLOPT_PROGRESSFUNCTION, curlProgressFunc);
385      curl_easy_setopt(curlHandle, CURLOPT_PROGRESSDATA, info->bar);
386
387      if (!isDownloading)
388        {
389          pthread_join(*downloadThreadFinishID, NULL);
390
391#ifdef HAVE_GTK2
392          info->stateButton->disconnectSignal(info->buttonSignal);
393          info->buttonSignal = info->stateButton->connectSignal("button_press_event", info, cancelDownload);
394#endif /* HAVE_GTK2 */
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         
401          //! \todo check if threads really were created.
402#ifdef HAVE_PTHREAD_H
403          pthread_create(downloadThreadID, NULL, downloadThread, info);
404          pthread_create(downloadThreadFinishID, NULL, downloadThreadFinished, info); 
405#else
406          downloadThread(info);
407          downloadThreadFinished(info);
408#endif /* HAVE_PTHREAD_H */
409         
410          //      res = curl_easy_perform(curlHandle);
411         
412          //      fclose(outfile);
413        }
414      else 
415        PRINTF(1)("thread already in use\n");
416
417    }
418  return true;
419}
420
421/**
422   \brief The downloading process (either threaded or not).
423   \param fileInfo the FileInfo.
424
425   \todo Threads get locked, if the cancel button is pressed in to small intervals.
426*/
427void* OrxonoxGuiUpdate::downloadThread(void* fileInfo)
428{
429  isDownloading = true;
430  curl_easy_perform(curlHandle);
431}
432
433/**
434   \brief Finishes a downloading process.
435   \param fileInfo the FileInfo.
436*/
437void* OrxonoxGuiUpdate::downloadThreadFinished(void* fileInfo)
438{ 
439  FileInfo* info = (FileInfo*)fileInfo;
440#ifdef HAVE_PTHREAD_H
441  pthread_join (*downloadThreadID, NULL);
442#ifdef HAVE_GTK2
443  gdk_threads_enter();
444#endif /* HAVE_GTK2 */
445#endif /* HAVE_PTHREAD_H */
446  if (curlHandle)
447    curl_easy_cleanup(curlHandle);
448 
449  PRINTF(3)("Closing the downloaded file.\n");
450  fclose(info->fileHandle);
451
452  if (isDownloading)
453    info->stateButton->setTitle("go on");
454  //  else
455  //    info->stateButton->setTitle("done");
456#ifdef HAVE_GTK2
457  info->stateButton->disconnectSignal(info->buttonSignal);
458  info->buttonSignal = info->stateButton->connectSignal("button_press_event", info, updateDataFunc);
459#endif /* HAVE_GTK2 */
460  isDownloading = false;
461#ifdef HAVE_PTHREAD_H
462#ifdef HAVE_GTK2
463  gdk_threads_leave();
464#endif /* HAVE_GTK2 */
465#endif /* HAVE_PTHREAD_H */
466
467}
468
469#ifdef HAVE_GTK2
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*/
478gint OrxonoxGuiUpdate::cancelDownload(GtkWidget* w, GdkEventKey* event, void* bar)
479{
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*/
485}
486#endif /* HAVE_GTK2 */
487
488#endif /* HAVE_CURL */
Note: See TracBrowser for help on using the repository browser.