/////////////////////////////////////////////////////////////////////////////
// Name:        batchdownloaddlg.cpp
// Purpose:     wxPackageBatchDownloadDlg
// Author:      Francesco Montorsi
// Modified by:
// Created:     15/07/2006 12:43:10
// RCS-ID:      $Id: batchdownloaddlg.cpp,v 1.4 2007/01/31 13:00:54 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 <wx/stdpaths.h>
#include <wx/animate.h>

#include "guipm/batchdownloaddlg.h"
#include "wxp/package.h"



// ----------------------------------------------------------------------------
// wxPackageBatchDownloadDlg
// ----------------------------------------------------------------------------

IMPLEMENT_DYNAMIC_CLASS( wxPackageBatchDownloadDlg, wxDialog )
BEGIN_EVENT_TABLE( wxPackageBatchDownloadDlg, wxDialog )
END_EVENT_TABLE()


wxPackageBatchDownloadDlg::wxPackageBatchDownloadDlg( )
{
}

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

bool wxPackageBatchDownloadDlg::Create( wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
{
////@begin wxPackageBatchDownloadDlg member initialisation
    m_pPackageList = NULL;
    m_pAnimation = NULL;
    m_pTotalSize = NULL;
////@end wxPackageBatchDownloadDlg member initialisation

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

    CreateControls();
    if (GetSizer())
    {
        GetSizer()->SetSizeHints(this);
    }
    CentreOnScreen();
    return true;
}

void wxPackageBatchDownloadDlg::CreateControls()
{
    m_pDestination = new wxDirPickerCtrl(this, ID_BATCHDOWNLOAD_DESTINATION);
    m_pAnimation = new wxAnimationCtrl(this, ID_BATCHDOWNLOAD_ANIMATION);

    wxAnimation anim;
    wxString imgpath = wxStandardPaths::Get().GetDataDir() + 
                       wxFileName::GetPathSeparator() + 
                       wxT("images") + wxFileName::GetPathSeparator();
    if (anim.LoadFile(imgpath + wxT("internetfiletransfer.gif")))
    {
        m_pAnimation->SetAnimation(anim);    
        m_pAnimation->Play();
    }

////@begin wxPackageBatchDownloadDlg content construction
    wxPackageBatchDownloadDlg* itemDialog1 = this;

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

    wxStaticText* itemStaticText3 = new wxStaticText( itemDialog1, wxID_STATIC, _("Are you sure you want to queue the download of all the\nfollowing packages?"), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer2->Add(itemStaticText3, 0, wxALIGN_LEFT|wxALL|wxADJUST_MINSIZE, 10);

    wxBoxSizer* itemBoxSizer4 = new wxBoxSizer(wxHORIZONTAL);
    itemBoxSizer2->Add(itemBoxSizer4, 0, wxGROW|wxALL, 5);

    wxString* m_pPackageListStrings = NULL;
    m_pPackageList = new wxListBox( itemDialog1, ID_BATCHDOWNLOAD_LIST, wxDefaultPosition, wxSize(150, 120), 0, m_pPackageListStrings, wxLB_SINGLE );
    itemBoxSizer4->Add(m_pPackageList, 1, wxALIGN_CENTER_VERTICAL|wxLEFT|wxRIGHT|wxBOTTOM, 5);

    m_pAnimation = (wxAnimationCtrl*) FindWindow(ID_BATCHDOWNLOAD_ANIMATION);
    wxASSERT( m_pAnimation != NULL );
    itemBoxSizer4->Add(m_pAnimation, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxBoxSizer* itemBoxSizer7 = new wxBoxSizer(wxHORIZONTAL);
    itemBoxSizer2->Add(itemBoxSizer7, 0, wxGROW|wxLEFT, 5);

    wxStaticText* itemStaticText8 = new wxStaticText( itemDialog1, wxID_STATIC, _("Approximate total download size:"), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer7->Add(itemStaticText8, 0, wxALIGN_CENTER_VERTICAL|wxALL|wxADJUST_MINSIZE, 5);

    m_pTotalSize = new wxStaticText( itemDialog1, ID_BATCHDOWNLOAD_SIZE, _("Static text"), wxDefaultPosition, wxDefaultSize, wxALIGN_CENTRE|wxST_NO_AUTORESIZE );
    m_pTotalSize->SetFont(wxFont(8, wxSWISS, wxNORMAL, wxBOLD, false, _T("Tahoma")));
    itemBoxSizer7->Add(m_pTotalSize, 0, wxGROW|wxALL, 5);

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

    wxStaticText* itemStaticText11 = new wxStaticText( itemDialog1, wxID_STATIC, _("The downloaded packages will be placed at:"), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer2->Add(itemStaticText11, 0, wxALIGN_LEFT|wxALL|wxADJUST_MINSIZE, 10);

    wxWindow* itemWindow12 = (wxWindow*) FindWindow(ID_BATCHDOWNLOAD_DESTINATION);
    wxASSERT( itemWindow12 != NULL );
    itemBoxSizer2->Add(itemWindow12, 0, wxGROW|wxLEFT|wxRIGHT|wxBOTTOM, 10);

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

    wxStdDialogButtonSizer* itemStdDialogButtonSizer14 = new wxStdDialogButtonSizer;

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

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

    itemStdDialogButtonSizer14->Realize();

////@end wxPackageBatchDownloadDlg content construction
}

void wxPackageBatchDownloadDlg::SetPackages(const wxPackageArray &arr)
{
    wxULongLong n, size=0;
    bool na = false;

    m_pPackageList->Clear();
    for (size_t i=0; i<arr.GetCount(); i++)
    {
        n = arr[i].GetCompressedSize();
        size += n;

        // build the string to append to the package list
        wxString str = arr[i].GetName();
        if (n > 0)
            str += wxT(" (") + wxFileName::GetHumanReadableSize(n) + wxT(")");
        else
        {
            str += wxT(" (size not available)");
            na = true;      // there's at least a package of unknown size!
        }
        m_pPackageList->Append(str);
    }

    if (size > 0)
    {
        m_pTotalSize->SetLabel(wxFileName::GetHumanReadableSize(size));
        if (na) m_pTotalSize->SetLabel(wxT("> ") + m_pTotalSize->GetLabel());
    }
    else
        m_pTotalSize->SetLabel(wxT("Not available"));
}

