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