Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/branches/updater: threads are cool after all. trying again.

File size: 7.9 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
33using namespace std;
34
35/**
36   \brief Creates an Audio-Frame
37*/
38OrxonoxGuiUpdate::OrxonoxGuiUpdate ()
39{
40  this->updateFrame = new Frame ("Update-Options:");
41  this->updateFrame->setGroupName ("update");
42  this->updateBox = new Box ('v');
43
44  // the Button for autoUpdating
45  this->autoUpdate = new CheckButton ("auto update");
46  this->updateBox->fill(this->autoUpdate);
47  this->autoUpdate->setFlagName ("update", "u", 0);
48  this->autoUpdate->saveable = true;
49
50  this->updateSourceWindowCreate ();
51  this->updateBox->fill(this->updateSourceWindowGetButton());
52
53  this->updateDataWindowCreate ();
54  this->updateBox->fill(this->updateDataWindowGetButton());
55
56  this->updateFrame->fill(this->updateBox);
57}
58
59/**
60   \brief Return the Frame
61   \return Returns the Audio-frame
62*/
63Widget* OrxonoxGuiUpdate::getWidget ()
64{
65  return updateFrame;
66}
67
68void OrxonoxGuiUpdate::updateDataWindowCreate (void)
69{
70  updateDataWindow = new Window ("update orxonox::Data");   
71  updateDataBox = new Box ('v');
72
73  // the close-Button of the Update Window.
74  //  updateWindowClose = new Button ("close");
75#ifdef HAVE_GTK2
76  //  updateWindowClose->connectSignal("button_press_event", updateWindow, Window::windowClose);
77#endif /* HAVE_GTK2 */
78  //  updateWindowBox->fill(updateWindowClose);
79
80  updateDataBar = new ProgressBar ();
81  updateDataBox->fill(updateDataBar);
82
83  updateDataBegin = new Button ("begin Download");
84  updateDataBegin->connectSignal ("button_press_event", updateDataBar, updateDataFunc);
85  updateDataBox->fill(updateDataBegin);
86
87  updateDataWindow->fill (updateDataBox);
88
89  updateDataWindowButton = new Button ("update orxonox::Data");
90#ifdef HAVE_GTK2
91  updateDataWindowButton->connectSignal("button_press_event", updateDataWindow, Window::windowOpen);
92  updateDataWindow->connectSignal("destroy", updateDataWindow, Window::windowClose);
93  updateDataWindow->connectSignal("delete_event", updateDataWindow, Window::windowClose);
94#endif /* HAVE_GTK2 */
95
96}
97
98/**
99   \returns A Pointer to the Button of the UpdaterWindow
100*/
101Button* OrxonoxGuiUpdate::updateDataWindowGetButton(void)
102{
103  return updateDataWindowButton;
104}
105
106void OrxonoxGuiUpdate::updateSourceWindowCreate (void)
107{
108  // the button, that opens this Window.
109  updateSourceWindowButton = new Button ("update orxonox::Source");
110
111  // the Window itself
112  updateSourceWindow = new Window ("update orxonox::Source");
113
114  updateSourceBox = new Box ();
115
116  updateSourceBar = new ProgressBar ();
117  updateSourceBox->fill(updateSourceBar);
118  test = new Button ("increment");
119
120#ifdef HAVE_GTK2
121  test->connectSignal("button_press_event", updateSourceBar, updateSourceFunc);
122#endif /* HAVE_GTK2 */
123
124  updateSourceBox->fill(test);
125  updateSourceWindow->fill(updateSourceBox); 
126#ifdef HAVE_GTK2
127  updateSourceWindowButton->connectSignal("button_press_event", updateSourceWindow, Window::windowOpen);
128  updateSourceWindow->connectSignal("destroy", updateSourceWindow, Window::windowClose);
129  updateSourceWindow->connectSignal("delete_event", updateSourceWindow, Window::windowClose);
130#endif /* HAVE_GTK2 */
131
132}
133
134Button* OrxonoxGuiUpdate::updateSourceWindowGetButton(void)
135{
136  return updateSourceWindowButton;
137}
138
139
140#ifdef HAVE_GTK2
141/**
142   \brief updates the Data of orxonox.
143   \param w The widget, that executed this Function.
144   \param event The event that trigered this Function.
145   \param button The Button, that triggered this event.
146*/
147gint OrxonoxGuiUpdate::updateDataFunc(GtkWidget* w, GdkEventKey* event, void* bar)
148{
149  FileInfo* info = new FileInfo;
150
151  info->fileName = "02%20orxonox%203.mp3";
152  info->webRoot  = "http://www.orxonox.ethz.ch/files/";
153  info->localRoot = "./";
154  info->Bar = (ProgressBar*)bar;
155  PRINTF(3)("Preparing to download file %s.\n", info->fileName);
156  download (info);
157  /*
158  if (downloadThreadID == NULL)
159    {
160      if ( (downloadThreadID =g_thread_create(&downloadThread, info, TRUE, NULL)) == 0 )
161        PRINTF(1)("can't create the thread");
162    }
163  else
164  PRINTF(1)("thread already in use\n");
165  */
166}
167
168/**
169   \brief updates the source of orxonox.
170   \param w The widget, that executed this Function.
171   \param event The event that trigered this Function.
172   \param button The Button, that triggered this event.
173*/
174gint OrxonoxGuiUpdate::updateSourceFunc(GtkWidget* w, GdkEventKey* event, void* bar)
175{
176  ProgressBar* tmpBar = static_cast<ProgressBar*>(bar);
177  tmpBar->setTotalSize(20);
178  tmpBar->setProgress(tmpBar->getProgress()+1);
179}
180#endif /* HAVE_GTK2 */
181
182#ifdef HAVE_CURL
183size_t OrxonoxGuiUpdate::curlWriteFunc (void* ptr, size_t size, size_t nmemb, FILE* stream)
184{
185  return fwrite(ptr, size, nmemb, stream);
186}
187
188size_t OrxonoxGuiUpdate::curlReadFunc (void* ptr, size_t size, size_t nmemb, FILE* stream)
189{
190  return fread(ptr, size, nmemb, stream);
191}
192
193
194int OrxonoxGuiUpdate::curlProgressFunc (ProgressBar* Bar, double totalSize, double progress, double upTotal, double upProgress)
195{
196  Bar->setProgress(progress);
197  Bar->setTotalSize(totalSize);
198  return 0;
199
200}
201
202CURL* OrxonoxGuiUpdate::curlHandle = NULL;
203GThread* OrxonoxGuiUpdate::downloadThreadID = NULL;
204
205void* OrxonoxGuiUpdate::download (void* fileInfo)
206{
207 
208  PRINTF(3)("Downloading.\n");
209  FileInfo* info = (FileInfo*)fileInfo;
210  CURLcode res;
211  FILE* outfile;
212  curlHandle = curl_easy_init();
213  char* fileOnNet = new char [strlen(info->webRoot)+strlen(info->fileName)+1];
214  strcpy (fileOnNet, info->webRoot);
215  strcat (fileOnNet, info->fileName);
216  char* fileOnDisk = new char [strlen(info->localRoot)+strlen(info->fileName)+1];
217  strcpy (fileOnDisk, info->localRoot);
218  strcat (fileOnDisk, info->fileName);
219
220  if(curlHandle)
221    {
222     
223      outfile = fopen(fileOnDisk, "w");
224
225      curl_easy_setopt(curlHandle, CURLOPT_URL, fileOnNet);
226      curl_easy_setopt(curlHandle, CURLOPT_WRITEDATA, outfile);
227      curl_easy_setopt(curlHandle, CURLOPT_WRITEFUNCTION, curlWriteFunc);
228      curl_easy_setopt(curlHandle, CURLOPT_READFUNCTION, curlReadFunc);
229      curl_easy_setopt(curlHandle, CURLOPT_NOPROGRESS, FALSE);
230      curl_easy_setopt(curlHandle, CURLOPT_PROGRESSFUNCTION, curlProgressFunc);
231      curl_easy_setopt(curlHandle, CURLOPT_PROGRESSDATA, info->Bar);
232
233      if (!downloadThreadID)
234        {
235          //! \todo check if threads really were created.
236          downloadThreadID = g_thread_create(&downloadThread, info, TRUE, NULL);
237
238          g_thread_create(&downloadThreadFinished, outfile, TRUE, NULL);
239        }
240      else 
241        PRINTF(1)("thread already in use\n");
242      //      res = curl_easy_perform(curlHandle);
243     
244      //      fclose(outfile);
245    }
246  //  downloadThreadID = NULL;
247  return NULL;
248}
249void* OrxonoxGuiUpdate::downloadThread(void* fileInfo)
250{
251  //  gdk_threads_enter();
252  curl_easy_perform(curlHandle);
253  if (curlHandle)
254    curl_easy_cleanup(curlHandle);
255  gdk_threads_leave();
256  downloadThreadID = NULL;
257  //  g_thread_exit (NULL);
258}
259
260void* OrxonoxGuiUpdate::downloadThreadFinished(void* outfile)
261{
262  g_thread_join(downloadThreadID);
263  PRINTF(3)("Closing the downloaded file.\n");
264  fclose((FILE*)outfile);
265}
266
267#ifdef HAVE_GTK2
268gint OrxonoxGuiUpdate::cancelDownload(GtkWidget* w, GdkEventKey* event, void* bar)
269{
270  //  curl_easy_cleanup(curlHandle);
271  //  curlHandle = NULL;
272}
273#endif /* HAVE_GTK2 */
274
275#endif /* HAVE_CURL */
Note: See TracBrowser for help on using the repository browser.