HaiDownlodManager - simples programa para baixar arquivos

Publicado por Gabriel Duarte Guimarães Costa (última atualização em 08/03/2010)

[ Hits: 5.264 ]

Homepage: http://kinuxlinux.org/gabriel_duarte

Download HaiDownLoader.cpp




Um simples programa para baixar arquivos construído usando C/C++, FLTK, libcURL e POSIX Threads.

http://github.com/gabrield/HaiDownloadManager

  



Esconder código-fonte

/* compile with "g++ HaiDownLoader.cpp -lfltk -o HaiDownLoader -pedantic -Wall -lcurl" */
 
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
 
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Input.H>
#include <FL/Fl_Progress.H>
#include <FL/Fl_File_Chooser.H>
#include <FL/fl_ask.H>
#include <FL/filename.H>
 
#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>
 
 
Fl_Progress *pdown = (Fl_Progress*)0;
Fl_Input *f2save = (Fl_Input*)0;
 
size_t write_func(void *ptr, size_t size, size_t nmemb, FILE *stream)
{
  return fwrite(ptr, size, nmemb, stream);
}
 
 
size_t read_func(void *ptr, size_t size, size_t nmemb, FILE *stream)
{
  return fread(ptr, size, nmemb, stream);
}
 
 
int progress_func(Fl_Progress *bar, double t,
                                    double d,
                                    double ultotal,
                                    double ulnow)
{
 
  char prgs[5];
 
  Fl::check();
  //if ((d*100.0/t) == 100.0) fl_alert("Download is finished! :D");
  bar->value(d*100.0/t);
  sprintf(prgs, "%2.2f%%", d*100.0/t);
  bar->copy_label(prgs);
  bar->redraw();
  //printf("%2.2f kb / %2.2f kb (%g %%)\n", d/1024, t/1024, d*100.0/t);
  return 0;
}
 
 
void perform_curl(const char *url, const char *filename, FILE *outfile)
{
    CURL *curl;
    CURLcode res;
 
    curl = curl_easy_init();
 
    outfile = fopen(filename, "w");
    curl_easy_setopt(curl, CURLOPT_URL, url);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_func);
    curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_func);
    curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
    curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, progress_func);
    curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, pdown);
    printf("URL = %s\n", url);
    printf("FILE = %s\n", filename);
 
    res = curl_easy_perform(curl);
 
    fclose(outfile);
    fl_alert("Download is finished! :D");
    /* always cleanup */
    curl_easy_cleanup(curl);
    return;
}
 
 
void *download_thread(void *ptr)
{
    /*CURL *curl;*/
    /*CURLcode res;*/
    FILE *outfile;
    char *url = (char*)ptr;
    char p2save[256];
    int choice = 0;
 
 
    sprintf(p2save, "%s/%s", f2save->value(), fl_filename_name(url));
    outfile = fopen(p2save, "r");
    if(outfile)
    {
        choice  = fl_choice("File already exists, do you want to overwrite it?", "No", "Yes", NULL);
        switch(choice)
        {
            case 0:
                fclose(outfile);
                break;
            case 1:
                fclose(outfile);
                perform_curl((const char*)url, (const char*)p2save, outfile);
                break;
        }
    }
    else
    {
        perform_curl((const char*)url, (const char*)p2save, outfile);
    }
    return NULL;
}
 
 
void save_file(Fl_Widget*, void *f2save)
{
    char *folder;
    Fl_Input *path = (Fl_Input*)f2save;
 
    folder = fl_dir_chooser("Place to save file",NULL, 0);
    path->value(folder);
 
    return;
}
 
 
void download_file(Fl_Widget*, void *url)
{
    pthread_t dthread;
    pthread_attr_t attr;
    Fl_Input *turl = (Fl_Input *)url;
 
    if(turl->size() == 0 || f2save->size() == 0)
    {
        if(turl->size() == 0)
        {
            fl_alert("Empty URL!");
        }
 
        if (f2save->size() == 0)
        {
            fl_alert("You must select a directory to save the file!");
        }
        return;
    }
    else
    {
        /* Initialize and set thread detached attribute */
         pthread_attr_init(&attr);
         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
 
         pthread_create(&dthread, &attr, download_thread, (void*)turl->value());
         pthread_join(dthread, NULL);
    }
    return;
}
 
 
void t_exit(Fl_Widget *, void *)
{
    printf("\n");
    exit(0);
}
 
 
int main(int argc, char *argv[])
{
    Fl_Window *mwin;
    Fl_Input *url;
    Fl_Button *btf2save, *bt2down;
    Fl_Group *grp;
    //Fl::scheme("gtk+");
    curl_global_init(CURL_GLOBAL_ALL);
 
    mwin = new Fl_Window((Fl::w() - 360)/2, (Fl::h() - 180)/2, 360, 180, "HaiDownLoader");
    bt2down = new Fl_Button((mwin->w()-350)/2, 150, 350, 25, "Download");
    bt2down->box(FL_GTK_UP_BOX);
    mwin->begin();
    {
        grp = new Fl_Group(5, 5, 350, 140);
        grp->box(FL_GTK_ROUND_UP_BOX);
        {
            url = new Fl_Input(65, 30, 235, 25, "URL:");
 
            f2save = new Fl_Input(135, 60, 165, 25, "Output Directory:");
 
            btf2save = new Fl_Button(305, 60, 40, 25, "...");
            btf2save->box(FL_GTK_UP_BOX);
 
            pdown = new Fl_Progress((grp->w()-250)/2, 100, 250, 25, "0%");
            pdown->color(FL_GREEN);
            pdown->minimum(0);
            pdown->maximum(100);
            pdown->label("0 %");
        }
    }
 
    bt2down->callback(download_file, (void*)url);
    btf2save->callback(save_file, (void*)f2save);
    mwin->end();
    mwin->callback(t_exit);
    mwin->show();
    return Fl::run();
}

Scripts recomendados

Obtendo o tempo em C

Goldbach

Fractal Estocástico

Jogo da Forca

Operadores Lógicos


  

Comentários

Nenhum comentário foi encontrado.


Contribuir com comentário




Patrocínio

Site hospedado pelo provedor RedeHost.
Linux banner

Destaques

Artigos

Dicas

Tópicos

Top 10 do mês

Scripts