curlParse/main.cc


Home Back


#include <wx/wxprec.h>
#ifndef WX_PRECOMP
    #include <wx/wx.h>
#endif
#include "wx/xrc/xmlres.h"
#include <wx/utils.h> 

#include "gui.cpp" 

#include <wx/process.h>





 
class threadProgress : public wxThread
{
   public:
    threadProgress();
    wxString dest;
    long totalSize=0;
    long currentSize=0;
    wxGauge *bar;
    wxStaticText *label;
    int stop=0;
 
 
   private:
    virtual void *Entry();
};
 
threadProgress::threadProgress()
{
 
}
 
void *threadProgress::Entry()
{
    wxFile ff; 
    ff.Open(dest); 
    wxString m;
    long percento=0;
    
    while(totalSize>=currentSize)
    {
        if (stop==1) continue;
        sleep(1);
        currentSize=ff.Length();
        //printf("Current: %ld/%ld\n",currentSize,totalSize);
        
        if (totalSize>0 && currentSize>0) percento= currentSize*100/totalSize;
        m="Current: "+wxString::Format(wxT("%ld"),currentSize)+"/"+wxString::Format(wxT("%ld"),totalSize)+" bytes";
        wxTheApp->CallAfter( [=] 
        {
          this->bar->SetValue(percento);
          this->label->SetLabel(m);
        });
    
    }
    
    
 
 
 
 
 return 0;
}
 
class dwnFrame{
    public:
        threadProgress *progress;
        wxFrame frame0;
        wxStaticText *label0;
        wxGauge *bar;
        long totalSize=0;
        wxProcess *p;
        wxString url,dest;
        void ShowFrame();
        void quit(wxCloseEvent& event);
} ;


void dwnFrame::quit(wxCloseEvent& event)
{
    
        printf("Quit.");
        long pid=p->GetPid();
        p->Kill(pid, wxSIGKILL,wxKILL_NOCHILDREN);
        progress->stop=1;
        //this->frame0.Close();
        exit(0);
}

 
void dwnFrame::ShowFrame()
{
    wxXmlResource::Get()->LoadFrame(&frame0, NULL,  wxT("frame0"));
    label0 = XRCCTRL(frame0, "label0", wxStaticText);
    bar = XRCCTRL(frame0, "bar", wxGauge);
    frame0.Bind(wxEVT_CLOSE_WINDOW, &dwnFrame::quit, this);
    
    wxArrayString output;
    wxArrayString errors;
    
    const wxString cmdTotalSize="curl -sI "+url;
    wxExecute(cmdTotalSize,  output, errors,wxEXEC_HIDE_CONSOLE );
    
    for (size_t i=0;i<=output.GetCount()-1;i++)
    {
        if (output[i].BeforeFirst(':').IsSameAs("content-length"))
        {
            totalSize=wxAtoi(output[i].AfterLast(':'));
            label0->SetLabel(output[i].AfterLast(':'));
        }
    }
    
    wxSize s(600,100);
    frame0.SetMinSize(s);
    frame0.Show();
    
    
    p = new wxProcess(wxPROCESS_DEFAULT);
    const wxString cmdDownload="curl "+url+" -o "+dest;
    wxExecute(cmdDownload,wxEXEC_ASYNC ,p );
    
    
    
    threadProgress *thread = new threadProgress();
    progress=thread;
    thread->bar=this->bar;
    thread->label=this->label0;
    thread->totalSize=totalSize;
    thread->dest=dest;
    thread->Create();
    thread->Run();
 
}
 

 



class MyApp: public wxApp
{
    virtual bool OnInit();
}; 
bool MyApp::OnInit()
{
    wxXmlResource::Get()->InitAllHandlers();  
    InitXmlResource(); 
 
    dwnFrame *MainWin = new dwnFrame();
    
    MainWin->url="https://www.freemedialab.org/listing/containers/systemd-nspawn/x86_64/Alpine/alpine-firefox.tar.bz2";
    MainWin->dest="alpine-firefox.tar.bz2"; 
    MainWin->ShowFrame();                   
 
    return true;
}
 
 
IMPLEMENT_APP(MyApp)

Powered by Code, a simple repository browser by Fabio Di Matteo