Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/branches/updater: using pthread as Threader.

File size: 7.8 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;
203pthread_t* OrxonoxGuiUpdate::downloadThreadID = new pthread_t;
204bool OrxonoxGuiUpdate::isDownloading = false;
205
206void* OrxonoxGuiUpdate::download (void* fileInfo)
207{
208 
209  PRINTF(3)("Downloading.\n");
210  FileInfo* info = (FileInfo*)fileInfo;
211  CURLcode res;
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  pthread_t test;
220  if(curlHandle)
221    {
222     
223      info->fileHandle = fopen(fileOnDisk, "w");
224
225      curl_easy_setopt(curlHandle, CURLOPT_URL, fileOnNet);
226      curl_easy_setopt(curlHandle, CURLOPT_WRITEDATA, info->fileHandle);
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 (!isDownloading)
234        {
235          //! \todo check if threads really were created.
236          pthread_create(downloadThreadID, NULL, downloadThread, info);
237          //      else
238          //    PRINTF(1)("thread already in use\n");
239          //      res = curl_easy_perform(curlHandle);
240         
241          //      fclose(outfile);
242        }
243    }
244  return NULL;
245}
246void* OrxonoxGuiUpdate::downloadThread(void* fileInfo)
247{
248  isDownloading = true;
249
250  FileInfo* info = (FileInfo*)fileInfo;
251  curl_easy_perform(curlHandle);
252  if (curlHandle)
253    curl_easy_cleanup(curlHandle);
254
255  pthread_join(*downloadThreadID, NULL);
256  PRINTF(3)("Closing the downloaded file.\n");
257  fclose(info->fileHandle);
258
259  isDownloading = false;
260}
261
262
263#ifdef HAVE_GTK2
264gint OrxonoxGuiUpdate::cancelDownload(GtkWidget* w, GdkEventKey* event, void* bar)
265{
266  //  curl_easy_cleanup(curlHandle);
267  //  curlHandle = NULL;
268}
269#endif /* HAVE_GTK2 */
270
271#endif /* HAVE_CURL */
Note: See TracBrowser for help on using the repository browser.