Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/branches/updater: getting some system information

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