Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/branches/updater: changed the FileInfo-struct

File size: 9.4 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->fileName = "03%20orxonox%203'.mp3";
156  info->webRoot  = "http://www.orxonox.ethz.ch/files/";
157  info->localRoot = "./";
158  info->Bar = (ProgressBar*)bar;
159  PRINTF(3)("Preparing to download file %s.\n", info->fileName);
160  //  downloadThread (info);
161
162  if (downloadThreadID == NULL)
163    {
164      if ( (downloadThreadID =g_thread_create(&downloadThread, info, TRUE, NULL)) == 0 )
165        PRINTF(1)("can't create the thread");
166    }
167  else 
168    PRINTF(1)("thread already in use\n");
169
170}
171
172/**
173   \brief updates the source of orxonox.
174   \param w The widget, that executed this Function.
175   \param event The event that trigered this Function.
176   \param button The Button, that triggered this event.
177*/
178gint OrxonoxGuiUpdate::updateSourceFunc(GtkWidget* w, GdkEventKey* event, void* bar)
179{
180  ProgressBar* tmpBar = static_cast<ProgressBar*>(bar);
181  tmpBar->setTotalSize(20);
182  tmpBar->setProgress(tmpBar->getProgress()+1);
183}
184#endif /* HAVE_GTK2 */
185
186#ifdef HAVE_CURL
187size_t OrxonoxGuiUpdate::curlWriteFunc (void* ptr, size_t size, size_t nmemb, FILE* stream)
188{
189  return fwrite(ptr, size, nmemb, stream);
190}
191
192size_t OrxonoxGuiUpdate::curlReadFunc (void* ptr, size_t size, size_t nmemb, FILE* stream)
193{
194  return fread(ptr, size, nmemb, stream);
195}
196
197
198int OrxonoxGuiUpdate::curlProgressFunc (ProgressBar* Bar, double totalSize, double progress, double upTotal, double upProgress)
199{
200  gdk_threads_enter();
201  Bar->setProgress(progress);
202  Bar->setTotalSize(totalSize);
203  gdk_threads_leave();
204  return 0;
205
206}
207
208GThread* OrxonoxGuiUpdate::downloadThreadID = NULL;
209
210void* OrxonoxGuiUpdate::downloadThread (void* fileInfo)
211{
212 
213  PRINTF(3)("Downloading.\n");
214  FileInfo* info = (FileInfo*)fileInfo;
215  CURL* curl;
216  CURLcode res;
217  FILE* outfile;
218  curl = curl_easy_init();
219  char* fileOnNet = new char [strlen(info->webRoot)+strlen(info->fileName)+1];
220  strcpy (fileOnNet, info->webRoot);
221  strcat (fileOnNet, info->fileName);
222  char* fileOnDisk = new char [strlen(info->localRoot)+strlen(info->fileName)+1];
223  strcpy (fileOnDisk, info->localRoot);
224  strcat (fileOnDisk, info->fileName);
225
226  if(curl)
227    {
228      outfile = fopen(fileOnDisk, "w");
229     
230      curl_easy_setopt(curl, CURLOPT_URL, fileOnNet);
231      curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile);
232      curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlWriteFunc);
233      curl_easy_setopt(curl, CURLOPT_READFUNCTION, curlReadFunc);
234      curl_easy_setopt(curl, CURLOPT_NOPROGRESS, FALSE);
235      curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, curlProgressFunc);
236      curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, info->Bar);
237     
238      res = curl_easy_perform(curl);
239     
240      fclose(outfile);
241      curl_easy_cleanup(curl);
242    }
243  downloadThreadID = NULL;
244  return NULL;
245}
246
247#endif /* HAVE_CURL */
248
249
250/*
251 int main(int argc, char **argv)
252 {
253   GtkWidget *Window, *Frame, *Frame2;
254   GtkAdjustment *adj;
255   
256   // Init thread
257   g_thread_init(NULL);
258   gtk_init(&argc, &argv);
259   Window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
260   Frame = gtk_frame_new(NULL);
261   gtk_frame_set_shadow_type(GTK_FRAME(Frame), GTK_SHADOW_OUT);
262   gtk_container_add(GTK_CONTAINER(Window), Frame);
263   Frame2 = gtk_frame_new(NULL);
264   gtk_frame_set_shadow_type(GTK_FRAME(Frame2), GTK_SHADOW_IN);
265   gtk_container_add(GTK_CONTAINER(Frame), Frame2);
266   gtk_container_set_border_width(GTK_CONTAINER(Frame2), 5);
267   adj = (GtkAdjustment*)gtk_adjustment_new(0, 0, 100, 0, 0, 0);
268   Bar = gtk_progress_bar_new_with_adjustment(adj);
269   gtk_container_add(GTK_CONTAINER(Frame2), Bar);
270   gtk_widget_show_all(Window);
271 
272   if (!g_thread_create(&my_thread, argv[1], FALSE, NULL) != 0)
273     g_warning("can't create the thread");
274 
275 
276   gdk_threads_enter();
277   gtk_main();
278  gdk_threads_leave();
279   return 0;
280 }
281
282
283
284
285
286size_t writeFunc(void *ptr, size_t size, size_t nmemb, FILE *stream)
287 {
288   return fwrite(ptr, size, nmemb, stream);
289 }
290 
291 size_t my_read_func(void *ptr, size_t size, size_t nmemb, FILE *stream)
292 {
293   return fread(ptr, size, nmemb, stream);
294 }
295 
296 int my_progress_func(GtkWidget *Bar,
297                      double t, // dltotal
298                      double d, // dlnow
299                      double ultotal,
300                      double ulnow)
301 {
302   gdk_threads_enter();
303   gtk_progress_set_value(GTK_PROGRESS(Bar), d*100.0/t);
304   gdk_threads_leave();
305   return 0;
306 }
307
308 void *my_thread(void *ptr)
309 {
310   CURL *curl;
311   CURLcode res;
312   FILE *outfile;
313   gchar *url = ptr;
314 
315   curl = curl_easy_init();
316   if(curl)
317   {
318     outfile = fopen("test.curl", "w");
319 
320     curl_easy_setopt(curl, CURLOPT_URL, url);
321     curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile);
322     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_write_func);
323     curl_easy_setopt(curl, CURLOPT_READFUNCTION, my_read_func);
324     curl_easy_setopt(curl, CURLOPT_NOPROGRESS, FALSE);
325     curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, my_progress_func);
326     curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, Bar);
327 
328     res = curl_easy_perform(curl);
329 
330     fclose(outfile);
331     curl_easy_cleanup(curl);
332   }
333 
334   return NULL;
335 }
336 
337*/
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
Note: See TracBrowser for help on using the repository browser.