/////////////////////////////////////////////////////////////////////////////
// Name:        decompressdlg.cpp
// Purpose:     wxPackageDecompressDlg
// Author:      Francesco Montorsi
// Modified by: 
// Created:     17/07/2006 23:38:05
// RCS-ID:      $Id: decompressdlg.cpp,v 1.4 2007/01/27 23:32:08 frm Exp $
// Copyright:   (c) 2006 Francesco Montorsi
// Licence:     wxWidgets license
/////////////////////////////////////////////////////////////////////////////


// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif


#include "guipm/decompressdlg.h"
#include <wx/filepicker.h>

#include "wxp/package.h"


// ----------------------------------------------------------------------------
// wxPackageDecompressDlg
// ----------------------------------------------------------------------------

IMPLEMENT_DYNAMIC_CLASS( wxPackageDecompressDlg, wxDialog )
BEGIN_EVENT_TABLE( wxPackageDecompressDlg, wxDialog )
    EVT_DIRPICKER_CHANGED(ID_DECOMPRESS_DESTINATION, 
                          wxPackageDecompressDlg::OnDestinationChange)

    EVT_BUTTON( wxID_OK, wxPackageDecompressDlg::OnOK )
END_EVENT_TABLE()


wxPackageDecompressDlg::wxPackageDecompressDlg( )
{
}

wxPackageDecompressDlg::wxPackageDecompressDlg( wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
{
    Create(parent, id, caption, pos, size, style);
}

bool wxPackageDecompressDlg::Create( wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
{
////@begin wxPackageDecompressDlg member initialisation
    m_pCompressedFileName = NULL;
    m_pRequiredSpace = NULL;
    m_pFreeSpace = NULL;
    m_pProgress = NULL;
////@end wxPackageDecompressDlg member initialisation

    SetExtraStyle(GetExtraStyle()|wxWS_EX_BLOCK_EVENTS);
    wxDialog::Create( parent, id, caption, pos, size, style );

    CreateControls();
    if (GetSizer())
    {
        wxSize sz = GetSizer()->GetMinSize();
        sz.Scale(2, 1);
        GetSizer()->SetMinSize(sz);
        GetSizer()->SetSizeHints(this);
    }
    CentreOnScreen();

    return true;
}

void wxPackageDecompressDlg::CreateControls()
{
    m_pDestination = new wxDirPickerCtrl(this, ID_DECOMPRESS_DESTINATION);
    
////@begin wxPackageDecompressDlg content construction
    wxPackageDecompressDlg* itemDialog1 = this;

    wxBoxSizer* itemBoxSizer2 = new wxBoxSizer(wxVERTICAL);
    itemDialog1->SetSizer(itemBoxSizer2);

    wxStaticText* itemStaticText3 = new wxStaticText( itemDialog1, wxID_STATIC, _("The files extracted from:"), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer2->Add(itemStaticText3, 0, wxALIGN_LEFT|wxALL|wxADJUST_MINSIZE, 5);

    m_pCompressedFileName = new wxTextCtrl( itemDialog1, wxID_STATIC, _T(""), wxDefaultPosition, wxDefaultSize, wxTE_CENTRE|wxNO_BORDER );
    m_pCompressedFileName->SetFont(wxFont(8, wxSWISS, wxNORMAL, wxBOLD, false, _T("")));
    itemBoxSizer2->Add(m_pCompressedFileName, 0, wxGROW|wxLEFT|wxRIGHT|wxTOP|wxADJUST_MINSIZE, 5);

    wxStaticText* itemStaticText5 = new wxStaticText( itemDialog1, wxID_STATIC, _("will be placed in:"), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer2->Add(itemStaticText5, 0, wxALIGN_LEFT|wxALL|wxADJUST_MINSIZE, 5);

    wxDirPickerCtrl* itemWindow6 = (wxDirPickerCtrl*) FindWindow(ID_DECOMPRESS_DESTINATION);
    wxASSERT( itemWindow6 != NULL );
    itemBoxSizer2->Add(itemWindow6, 1, wxGROW|wxLEFT|wxRIGHT, 10);

    wxFlexGridSizer* itemFlexGridSizer7 = new wxFlexGridSizer(2, 2, 0, 0);
    itemFlexGridSizer7->AddGrowableCol(1);
    itemBoxSizer2->Add(itemFlexGridSizer7, 0, wxGROW|wxALL, 5);

    wxStaticText* itemStaticText8 = new wxStaticText( itemDialog1, wxID_STATIC, _("Approximate required space:"), wxDefaultPosition, wxDefaultSize, 0 );
    itemFlexGridSizer7->Add(itemStaticText8, 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxALL|wxADJUST_MINSIZE, 5);

    m_pRequiredSpace = new wxStaticText( itemDialog1, wxID_STATIC, _("Static text"), wxDefaultPosition, wxDefaultSize, wxALIGN_CENTRE|wxST_NO_AUTORESIZE );
    itemFlexGridSizer7->Add(m_pRequiredSpace, 0, wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL|wxALL|wxADJUST_MINSIZE, 5);

    wxStaticText* itemStaticText10 = new wxStaticText( itemDialog1, wxID_STATIC, _("Free space in selected destination:"), wxDefaultPosition, wxDefaultSize, 0 );
    itemFlexGridSizer7->Add(itemStaticText10, 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxALL|wxADJUST_MINSIZE, 5);

    m_pFreeSpace = new wxStaticText( itemDialog1, wxID_STATIC, _("Static text"), wxDefaultPosition, wxDefaultSize, wxALIGN_CENTRE|wxST_NO_AUTORESIZE );
    itemFlexGridSizer7->Add(m_pFreeSpace, 0, wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL|wxALL|wxADJUST_MINSIZE, 5);

    wxStaticLine* itemStaticLine12 = new wxStaticLine( itemDialog1, wxID_STATIC, wxDefaultPosition, wxDefaultSize, wxLI_HORIZONTAL );
    itemBoxSizer2->Add(itemStaticLine12, 0, wxGROW|wxALL, 10);

    wxStaticText* itemStaticText13 = new wxStaticText( itemDialog1, wxID_STATIC, _("Progress:"), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer2->Add(itemStaticText13, 0, wxALIGN_LEFT|wxALL|wxADJUST_MINSIZE, 5);

    m_pProgress = new wxGauge( itemDialog1, ID_DECOMPRESS_PROGRESS, 100, wxDefaultPosition, wxDefaultSize, wxGA_HORIZONTAL );
    m_pProgress->SetValue(0);
    itemBoxSizer2->Add(m_pProgress, 0, wxGROW|wxALL, 5);

    itemBoxSizer2->Add(5, 5, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5);

    wxStdDialogButtonSizer* itemStdDialogButtonSizer16 = new wxStdDialogButtonSizer;

    itemBoxSizer2->Add(itemStdDialogButtonSizer16, 0, wxALIGN_RIGHT|wxALL, 5);
    wxButton* itemButton17 = new wxButton( itemDialog1, wxID_OK, _("&OK"), wxDefaultPosition, wxDefaultSize, 0 );
    itemStdDialogButtonSizer16->AddButton(itemButton17);

    wxButton* itemButton18 = new wxButton( itemDialog1, wxID_CANCEL, _("&Cancel"), wxDefaultPosition, wxDefaultSize, 0 );
    itemStdDialogButtonSizer16->AddButton(itemButton18);

    itemStdDialogButtonSizer16->Realize();

////@end wxPackageDecompressDlg content construction

    m_pCompressedFileName->SetBackgroundColour(
        wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE));

    // set gauge range
    m_pProgress->SetRange(1000);    // see wxProgressHandler description
}

bool wxPackageDecompressDlg::ShowToolTips()
{
    return true;
}

void wxPackageDecompressDlg::SetPackage(const wxPackage &pkg)
{ 
    m_pkg = pkg;
    SetDestination(pkg.GetDecompressionPath());

    // update labels
    m_pRequiredSpace->SetLabel(
        wxFileName::GetHumanReadableSize(pkg.GetDecompressedSize()));
    m_pCompressedFileName->SetValue(pkg.GetDownloadPath());
    UpdateFreeSpace(); 
}

void wxPackageDecompressDlg::UpdateFreeSpace()
{
    wxLongLong free;
    if (!wxGetDiskSpace(m_pDestination->GetPath(), NULL, &free))
    {
        m_pFreeSpace->SetLabel(wxT("Not available"));
        return;
    }

    wxULongLong sz(free.GetHi(), free.GetLo());
    m_pFreeSpace->SetLabel(wxFileName::GetHumanReadableSize(sz));
}



// ----------------------------------------------------------------------------
// wxPackageDecompressDlg - event handlers
// ----------------------------------------------------------------------------

void wxPackageDecompressDlg::OnDestinationChange(wxFileDirPickerEvent &)
{
    UpdateFreeSpace();
}

void wxPackageDecompressDlg::OnOK(wxCommandEvent &WXUNUSED(ev))
{
    class myProgressHandler : public wxProgressHandler
    {
    public:
        myProgressHandler(wxGauge *p) { m_gauge=p; }

        bool OnUpdate(unsigned long progress, const wxString &WXUNUSED(text))
        { 
            m_gauge->SetValue(progress); 
            wxYieldIfNeeded();
            return true; 
        }

        wxGauge *m_gauge;
    };

    myProgressHandler handler(m_pProgress);
    if (!m_pkg.Decompress(m_pDestination->GetPath(), &handler))
        EndModal(wxID_CANCEL);
    EndModal(wxID_OK);
}

