Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/branches/updater: race-condition-prevention.

File size: 9.2 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#ifdef HAVE_CURL
33#include <curl/curl.h>
34#include <curl/types.h>
35#include <curl/easy.h>
36#endif /* HAVE_CURL */
37using namespace std;
38
39/**
40   \brief Creates an Audio-Frame
41*/
42OrxonoxGuiUpdate::OrxonoxGuiUpdate ()
43{
44  this->updateFrame = new Frame ("Update-Options:");
45  this->updateFrame->setGroupName ("update");
46  this->updateBox = new Box ('v');
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  this->updateFrame->fill(this->updateBox);
61}
62
63/**
64   \brief Return the Frame
65   \return Returns the Audio-frame
66*/
67Widget* OrxonoxGuiUpdate::getWidget ()
68{
69  return updateFrame;
70}
71
72void OrxonoxGuiUpdate::updateDataWindowCreate (void)
73{
74  updateDataWindow = new Window ("update orxonox::Data");   
75  updateDataBox = new Box ('v');
76
77  // the close-Button of the Update Window.
78  //  updateWindowClose = new Button ("close");
79#ifdef HAVE_GTK2
80  //  updateWindowClose->connectSignal("button_press_event", updateWindow, Window::windowClose);
81#endif /* HAVE_GTK2 */
82  //  updateWindowBox->fill(updateWindowClose);
83
84  updateDataBar = new ProgressBar ();
85  updateDataBox->fill(updateDataBar);
86
87  updateDataBegin = new Button ("begin Download");
88  updateDataBegin->connectSignal ("button_press_event", updateDataBar, updateDataFunc);
89  updateDataBox->fill(updateDataBegin);
90
91  updateDataWindow->fill (updateDataBox);
92
93  updateDataWindowButton = new Button ("update orxonox::Data");
94#ifdef HAVE_GTK2
95  updateDataWindowButton->connectSignal("button_press_event", updateDataWindow, Window::windowOpen);
96  updateDataWindow->connectSignal("destroy", updateDataWindow, Window::windowClose);
97  updateDataWindow->connectSignal("delete_event", updateDataWindow, Window::windowClose);
98#endif /* HAVE_GTK2 */
99
100}
101
102/**
103   \returns A Pointer to the Button of the UpdaterWindow
104*/
105Button* OrxonoxGuiUpdate::updateDataWindowGetButton(void)
106{
107  return updateDataWindowButton;
108}
109
110void OrxonoxGuiUpdate::updateSourceWindowCreate (void)
111{
112  // the button, that opens this Window.
113  updateSourceWindowButton = new Button ("update orxonox::Source");
114
115  // the Window itself
116  updateSourceWindow = new Window ("update orxonox::Source");
117
118  updateSourceBox = new Box ();
119
120  updateSourceBar = new ProgressBar ();
121  updateSourceBox->fill(updateSourceBar);
122  test = new Button ("increment");
123
124#ifdef HAVE_GTK2
125  test->connectSignal("button_press_event", updateSourceBar, updateSourceFunc);
126#endif /* HAVE_GTK2 */
127
128  updateSourceBox->fill(test);
129  updateSourceWindow->fill(updateSourceBox); 
130#ifdef HAVE_GTK2
131  updateSourceWindowButton->connectSignal("button_press_event", updateSourceWindow, Window::windowOpen);
132  updateSourceWindow->connectSignal("destroy", updateSourceWindow, Window::windowClose);
133  updateSourceWindow->connectSignal("delete_event", updateSourceWindow, Window::windowClose);
134#endif /* HAVE_GTK2 */
135
136}
137
138Button* OrxonoxGuiUpdate::updateSourceWindowGetButton(void)
139{
140  return updateSourceWindowButton;
141}
142
143
144#ifdef HAVE_GTK2
145/**
146   \brief updates the Data of orxonox.
147   \param w The widget, that executed this Function.
148   \param event The event that trigered this Function.
149   \param button The Button, that triggered this event.
150*/
151gint OrxonoxGuiUpdate::updateDataFunc(GtkWidget* w, GdkEventKey* event, void* bar)
152{
153  FileInfo* info = new FileInfo;
154
155  info->fileOnDisk = new char [5];
156  strcpy (info->fileOnDisk, "test");
157  info->fileOnNet = new char [100];
158  strcpy(info->fileOnNet, "http://www.orxonox.ethz.ch/files/02%20orxonox%203.mp3");
159  info->Bar = (ProgressBar*)bar;
160  PRINTF(3)("Preparing to download file %s.\n", info->fileOnNet);
161  //  downloadThread (info);
162
163  if (downloadThreadID == NULL)
164    {
165        if ( (downloadThreadID =g_thread_create(&downloadThread, info, TRUE, NULL)) == 0 )
166          PRINTF(1)("can't create the thread");
167    }
168  else 
169    PRINTF(1)("thread already in use\n");
170
171}
172
173/**
174   \brief updates the source of orxonox.
175   \param w The widget, that executed this Function.
176   \param event The event that trigered this Function.
177   \param button The Button, that triggered this event.
178*/
179gint OrxonoxGuiUpdate::updateSourceFunc(GtkWidget* w, GdkEventKey* event, void* bar)
180{
181  ProgressBar* tmpBar = static_cast<ProgressBar*>(bar);
182  tmpBar->setTotalSize(20);
183  tmpBar->setProgress(tmpBar->getProgress()+1);
184}
185#endif /* HAVE_GTK2 */
186
187#ifdef HAVE_CURL
188size_t OrxonoxGuiUpdate::curlWriteFunc (void* ptr, size_t size, size_t nmemb, FILE* stream)
189{
190  return fwrite(ptr, size, nmemb, stream);
191}
192
193size_t OrxonoxGuiUpdate::curlReadFunc (void* ptr, size_t size, size_t nmemb, FILE* stream)
194{
195  return fread(ptr, size, nmemb, stream);
196}
197
198
199int OrxonoxGuiUpdate::curlProgressFunc (ProgressBar* Bar, double totalSize, double progress, double upTotal, double upProgress)
200{
201  gdk_threads_enter();
202  Bar->setProgress(progress);
203  Bar->setTotalSize(totalSize);
204  gdk_threads_leave();
205  return 0;
206
207}
208
209GThread* OrxonoxGuiUpdate::downloadThreadID = NULL;
210
211void* OrxonoxGuiUpdate::downloadThread (void* fileInfo)
212{
213 
214  PRINTF(3)("Downloading.\n");
215  FileInfo* info = (FileInfo*)fileInfo;
216  CURL* curl;
217  CURLcode res;
218  FILE* outfile;
219  curl = curl_easy_init();
220
221  if(curl)
222    {
223      outfile = fopen(info->fileOnDisk, "w");
224     
225      curl_easy_setopt(curl, CURLOPT_URL, info->fileOnNet);
226      curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile);
227      curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlWriteFunc);
228      curl_easy_setopt(curl, CURLOPT_READFUNCTION, curlReadFunc);
229      curl_easy_setopt(curl, CURLOPT_NOPROGRESS, FALSE);
230      curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, curlProgressFunc);
231      curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, info->Bar);
232     
233      res = curl_easy_perform(curl);
234     
235      fclose(outfile);
236      curl_easy_cleanup(curl);
237    }
238  downloadThreadID = NULL;
239  return NULL;
240}
241
242#endif /* HAVE_CURL */
243
244
245/*
246 int main(int argc, char **argv)
247 {
248   GtkWidget *Window, *Frame, *Frame2;
249   GtkAdjustment *adj;
250   
251   // Init thread
252   g_thread_init(NULL);
253   gtk_init(&argc, &argv);
254   Window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
255   Frame = gtk_frame_new(NULL);
256   gtk_frame_set_shadow_type(GTK_FRAME(Frame), GTK_SHADOW_OUT);
257   gtk_container_add(GTK_CONTAINER(Window), Frame);
258   Frame2 = gtk_frame_new(NULL);
259   gtk_frame_set_shadow_type(GTK_FRAME(Frame2), GTK_SHADOW_IN);
260   gtk_container_add(GTK_CONTAINER(Frame), Frame2);
261   gtk_container_set_border_width(GTK_CONTAINER(Frame2), 5);
262   adj = (GtkAdjustment*)gtk_adjustment_new(0, 0, 100, 0, 0, 0);
263   Bar = gtk_progress_bar_new_with_adjustment(adj);
264   gtk_container_add(GTK_CONTAINER(Frame2), Bar);
265   gtk_widget_show_all(Window);
266 
267   if (!g_thread_create(&my_thread, argv[1], FALSE, NULL) != 0)
268     g_warning("can't create the thread");
269 
270 
271   gdk_threads_enter();
272   gtk_main();
273  gdk_threads_leave();
274   return 0;
275 }
276
277
278
279
280
281size_t writeFunc(void *ptr, size_t size, size_t nmemb, FILE *stream)
282 {
283   return fwrite(ptr, size, nmemb, stream);
284 }
285 
286 size_t my_read_func(void *ptr, size_t size, size_t nmemb, FILE *stream)
287 {
288   return fread(ptr, size, nmemb, stream);
289 }
290 
291 int my_progress_func(GtkWidget *Bar,
292                      double t, // dltotal
293                      double d, // dlnow
294                      double ultotal,
295                      double ulnow)
296 {
297   gdk_threads_enter();
298   gtk_progress_set_value(GTK_PROGRESS(Bar), d*100.0/t);
299   gdk_threads_leave();
300   return 0;
301 }
302
303 void *my_thread(void *ptr)
304 {
305   CURL *curl;
306   CURLcode res;
307   FILE *outfile;
308   gchar *url = ptr;
309 
310   curl = curl_easy_init();
311   if(curl)
312   {
313     outfile = fopen("test.curl", "w");
314 
315     curl_easy_setopt(curl, CURLOPT_URL, url);
316     curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile);
317     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_write_func);
318     curl_easy_setopt(curl, CURLOPT_READFUNCTION, my_read_func);
319     curl_easy_setopt(curl, CURLOPT_NOPROGRESS, FALSE);
320     curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, my_progress_func);
321     curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, Bar);
322 
323     res = curl_easy_perform(curl);
324 
325     fclose(outfile);
326     curl_easy_cleanup(curl);
327   }
328 
329   return NULL;
330 }
331 
332*/
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
Note: See TracBrowser for help on using the repository browser.