Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Dec 24, 2004, 1:15:38 PM (19 years ago)
Author:
bensch
Message:

orxonox/branches/updater: now the Data-bar downloads something, and the Progress is displayed. (not very nice, but it works).

NOT THREAD SAVE <<

File:
1 edited

Legend:

Unmodified
Added
Removed
  • orxonox/branches/updater/src/gui/orxonox_gui_update.cc

    r3261 r3263  
    3030#include "orxonox_gui.h"
    3131#include <stdio.h>
     32#include <curl/curl.h>
     33#include <curl/types.h>
     34#include <curl/easy.h> /* new for v7 */
    3235using namespace std;
    3336
     
    7982  updateDataBar = new ProgressBar ();
    8083  updateDataBox->fill(updateDataBar);
     84
     85  updateDataBegin = new Button ("begin Download");
     86  updateDataBegin->connectSignal ("button_press_event", updateDataBar, updateDataFunc);
     87  updateDataBox->fill(updateDataBegin);
     88
    8189  updateDataWindow->fill (updateDataBox);
    8290
     
    139147   \param button The Button, that triggered this event.
    140148*/
    141 gint OrxonoxGuiUpdate::updateDataFunc(GtkWidget* w, GdkEventKey* event, void* button)
    142 {
    143 
     149gint OrxonoxGuiUpdate::updateDataFunc(GtkWidget* w, GdkEventKey* event, void* bar)
     150{
     151  FileInfo* info = new FileInfo;
     152
     153  info->fileOnDisk = new char [5];
     154  strcpy (info->fileOnDisk, "test");
     155  info->fileOnNet = new char [100];
     156  strcpy(info->fileOnNet, "http://www.orxonox.ethz.ch/files/taskHorizon.at.bermuda-funk.ogg");
     157  info->Bar = (ProgressBar*)bar;
     158  PRINTF(3)("Preparing to download file %s.\n", info->fileOnNet);
     159  //downloadThread (info);
     160
     161  if (!g_thread_create(&downloadThread, info, FALSE, NULL) != 0)
     162    PRINTF(1)("can't create the thread");
    144163}
    145164
     
    156175  tmpBar->setProgress(tmpBar->getProgress()+1);
    157176}
    158 
    159 #endif /* HAVE_GTK2 */
     177#endif /* HAVE_GTK2 */
     178
     179#ifdef HAVE_CURL
     180size_t OrxonoxGuiUpdate::curlWriteFunc (void* ptr, size_t size, size_t nmemb, FILE* stream)
     181{
     182  return fwrite(ptr, size, nmemb, stream);
     183}
     184
     185size_t OrxonoxGuiUpdate::curlReadFunc (void* ptr, size_t size, size_t nmemb, FILE* stream)
     186{
     187  return fread(ptr, size, nmemb, stream);
     188}
     189
     190
     191int OrxonoxGuiUpdate::curlProgressFunc (ProgressBar* Bar, double totalSize, double progress, double upTotal, double upProgress)
     192{
     193  //  gdk_threads_enter();
     194  Bar->setProgress(progress);
     195  Bar->setTotalSize(totalSize);
     196  //  gdk_threads_leave();
     197  return 0;
     198
     199}
     200
     201void* OrxonoxGuiUpdate::downloadThread (void* fileInfo)
     202{
     203  PRINTF(3)("Downloading.\n");
     204  FileInfo* info = (FileInfo*)fileInfo;
     205  CURL* curl;
     206  CURLcode res;
     207  FILE* outfile;
     208  curl = curl_easy_init();
     209
     210  if(curl)
     211    {
     212      outfile = fopen(info->fileOnDisk, "w");
     213     
     214      curl_easy_setopt(curl, CURLOPT_URL, info->fileOnNet);
     215      curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile);
     216      curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlWriteFunc);
     217      curl_easy_setopt(curl, CURLOPT_READFUNCTION, curlReadFunc);
     218      curl_easy_setopt(curl, CURLOPT_NOPROGRESS, FALSE);
     219      curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, curlProgressFunc);
     220      curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, info->Bar);
     221     
     222      res = curl_easy_perform(curl);
     223     
     224      fclose(outfile);
     225      curl_easy_cleanup(curl);
     226    }
     227  return NULL;
     228}
     229
     230#endif /* HAVE_CURL */
     231
     232
     233/*
     234 int main(int argc, char **argv)
     235 {
     236   GtkWidget *Window, *Frame, *Frame2;
     237   GtkAdjustment *adj;
     238   
     239   // Init thread
     240   g_thread_init(NULL);
     241   gtk_init(&argc, &argv);
     242   Window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
     243   Frame = gtk_frame_new(NULL);
     244   gtk_frame_set_shadow_type(GTK_FRAME(Frame), GTK_SHADOW_OUT);
     245   gtk_container_add(GTK_CONTAINER(Window), Frame);
     246   Frame2 = gtk_frame_new(NULL);
     247   gtk_frame_set_shadow_type(GTK_FRAME(Frame2), GTK_SHADOW_IN);
     248   gtk_container_add(GTK_CONTAINER(Frame), Frame2);
     249   gtk_container_set_border_width(GTK_CONTAINER(Frame2), 5);
     250   adj = (GtkAdjustment*)gtk_adjustment_new(0, 0, 100, 0, 0, 0);
     251   Bar = gtk_progress_bar_new_with_adjustment(adj);
     252   gtk_container_add(GTK_CONTAINER(Frame2), Bar);
     253   gtk_widget_show_all(Window);
     254 
     255   if (!g_thread_create(&my_thread, argv[1], FALSE, NULL) != 0)
     256     g_warning("can't create the thread");
     257 
     258 
     259   gdk_threads_enter();
     260   gtk_main();
     261  gdk_threads_leave();
     262   return 0;
     263 }
     264
     265
     266
     267
     268
     269size_t writeFunc(void *ptr, size_t size, size_t nmemb, FILE *stream)
     270 {
     271   return fwrite(ptr, size, nmemb, stream);
     272 }
     273 
     274 size_t my_read_func(void *ptr, size_t size, size_t nmemb, FILE *stream)
     275 {
     276   return fread(ptr, size, nmemb, stream);
     277 }
     278 
     279 int my_progress_func(GtkWidget *Bar,
     280                      double t, // dltotal
     281                      double d, // dlnow
     282                      double ultotal,
     283                      double ulnow)
     284 {
     285   gdk_threads_enter();
     286   gtk_progress_set_value(GTK_PROGRESS(Bar), d*100.0/t);
     287   gdk_threads_leave();
     288   return 0;
     289 }
     290
     291 void *my_thread(void *ptr)
     292 {
     293   CURL *curl;
     294   CURLcode res;
     295   FILE *outfile;
     296   gchar *url = ptr;
     297 
     298   curl = curl_easy_init();
     299   if(curl)
     300   {
     301     outfile = fopen("test.curl", "w");
     302 
     303     curl_easy_setopt(curl, CURLOPT_URL, url);
     304     curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile);
     305     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_write_func);
     306     curl_easy_setopt(curl, CURLOPT_READFUNCTION, my_read_func);
     307     curl_easy_setopt(curl, CURLOPT_NOPROGRESS, FALSE);
     308     curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, my_progress_func);
     309     curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, Bar);
     310 
     311     res = curl_easy_perform(curl);
     312 
     313     fclose(outfile);
     314     curl_easy_cleanup(curl);
     315   }
     316 
     317   return NULL;
     318 }
     319 
     320*/
     321
     322
     323
     324
     325
     326
     327
     328
     329
     330
     331
     332
     333
     334
     335
     336
     337
     338
     339
     340
     341
     342
     343
     344
Note: See TracChangeset for help on using the changeset viewer.