/////////////////////////////////////////////////////////////////////////////
// Name:        uploaddlg.cpp
// Purpose:     wxPackageUploadDialog
// Author:      Francesco Montorsi
// Modified by:
// Created:     01/01/2007 16:58:55
// RCS-ID:      $Id: uploaddlg.cpp,v 1.2 2007/01/01 21:36:45 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/filename.h>
#include <wx/uri.h>

#include "guipkg/uploaddlg.h"



// ----------------------------------------------------------------------------
// wxPackageUploadDlg
// ----------------------------------------------------------------------------

IMPLEMENT_DYNAMIC_CLASS( wxPackageUploadDlg, wxDialog )
BEGIN_EVENT_TABLE( wxPackageUploadDlg, wxDialog )
    EVT_CHECKBOX(wxID_ANY, wxPackageUploadDlg::OnAnonymous)
END_EVENT_TABLE()

wxPackageUploadDlg::wxPackageUploadDlg( )
{
}

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

bool wxPackageUploadDlg::Create( wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
{
////@begin wxPackageUploadDlg member initialisation
    m_pFileSizeLabel = NULL;
    m_pFile = NULL;
    m_pUser = NULL;
    m_pPassword = NULL;
    m_pAnonymous = NULL;
    m_pDir = NULL;
////@end wxPackageUploadDlg member initialisation

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

    CreateControls();
    if (GetSizer())
    {
        GetSizer()->SetSizeHints(this);

        SetMinSize(GetMinSize().Scale(2.0, 1));
    }
    CentreOnScreen();

    return true;
}

void wxPackageUploadDlg::CreateControls()
{    
////@begin wxPackageUploadDlg content construction
    wxPackageUploadDlg* itemDialog1 = this;

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

    m_pFileSizeLabel = new wxStaticText( itemDialog1, wxID_STATIC, _("File to upload (SIZE SIZE):"), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer2->Add(m_pFileSizeLabel, 0, wxALIGN_LEFT|wxALL|wxADJUST_MINSIZE, 5);

    m_pFile = new wxTextCtrl( itemDialog1, ID_UPLOAD_FILENAME, _T(""), wxDefaultPosition, wxDefaultSize, wxTE_READONLY|wxTE_CENTRE|wxSTATIC_BORDER );
    m_pFile->SetBackgroundColour(wxColour(211, 211, 211));
    itemBoxSizer2->Add(m_pFile, 0, wxGROW|wxLEFT|wxRIGHT|wxBOTTOM|wxADJUST_MINSIZE, 5);

    wxStaticText* itemStaticText5 = new wxStaticText( itemDialog1, wxID_STATIC, _("Please enter the following info for a correct upload:"), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer2->Add(itemStaticText5, 0, wxALIGN_LEFT|wxALL|wxADJUST_MINSIZE, 5);

    wxStaticBox* itemStaticBoxSizer6Static = new wxStaticBox(itemDialog1, wxID_ANY, _("Authentication"));
    wxStaticBoxSizer* itemStaticBoxSizer6 = new wxStaticBoxSizer(itemStaticBoxSizer6Static, wxVERTICAL);
    itemBoxSizer2->Add(itemStaticBoxSizer6, 0, wxGROW|wxALL, 5);

    wxFlexGridSizer* itemFlexGridSizer7 = new wxFlexGridSizer(2, 2, 0, 0);
    itemFlexGridSizer7->AddGrowableCol(1);
    itemStaticBoxSizer6->Add(itemFlexGridSizer7, 1, wxGROW|wxLEFT|wxRIGHT, 5);

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

    m_pUser = new wxTextCtrl( itemDialog1, ID_UPLOAD_USER, _T(""), wxDefaultPosition, wxDefaultSize, 0 );
    itemFlexGridSizer7->Add(m_pUser, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxStaticText* itemStaticText10 = new wxStaticText( itemDialog1, wxID_STATIC, _("Password:"), wxDefaultPosition, wxDefaultSize, 0 );
    itemFlexGridSizer7->Add(itemStaticText10, 0, wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL|wxALL|wxADJUST_MINSIZE, 5);

    m_pPassword = new wxTextCtrl( itemDialog1, ID_UPLOAD_PASSWORD, _T(""), wxDefaultPosition, wxDefaultSize, 0 );
    itemFlexGridSizer7->Add(m_pPassword, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    m_pAnonymous = new wxCheckBox( itemDialog1, ID_UPLOAD_ANONYMOUS, _("Anonymous"), wxDefaultPosition, wxDefaultSize, 0 );
    m_pAnonymous->SetValue(false);
    itemStaticBoxSizer6->Add(m_pAnonymous, 0, wxALIGN_RIGHT|wxLEFT|wxRIGHT|wxBOTTOM, 5);

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

    wxStaticText* itemStaticText14 = new wxStaticText( itemDialog1, wxID_STATIC, _("Server and \ndirectory:"), wxDefaultPosition, wxDefaultSize, wxALIGN_CENTRE );
    itemBoxSizer13->Add(itemStaticText14, 0, wxALIGN_CENTER_VERTICAL|wxALL|wxADJUST_MINSIZE, 5);

    m_pDir = new wxTextCtrl( itemDialog1, ID_UPLOAD_DIRECTORY, _T(""), wxDefaultPosition, wxDefaultSize, 0 );
    m_pDir->SetHelpText(_("This is an URL like ftp://upload.sourceforge.net/incoming"));
    if (ShowToolTips())
        m_pDir->SetToolTip(_("This is an URL like ftp://upload.sourceforge.net/incoming"));
    itemBoxSizer13->Add(m_pDir, 1, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxStdDialogButtonSizer* itemStdDialogButtonSizer16 = new wxStdDialogButtonSizer;

    itemBoxSizer2->Add(itemStdDialogButtonSizer16, 0, wxALIGN_CENTER_HORIZONTAL|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 wxPackageUploadDlg content construction
}

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

void wxPackageUploadDlg::SetFileToUpload(const wxString &packagefile)
{
    wxULongLong sz = wxFileName::GetSize(packagefile);
    m_pFileSizeLabel->SetLabel(
        wxT("The file to upload (") + wxFileName::GetHumanReadableSize(sz) + wxT(") is:"));
    m_pFile->SetLabel(packagefile);
}

wxString wxPackageUploadDlg::GetPath() const
{
    wxURI uri(m_pDir->GetValue());
    return uri.GetPath();
}

wxString wxPackageUploadDlg::GetServer() const
{
    wxURI uri(m_pDir->GetValue());
    return uri.GetServer();
}

void wxPackageUploadDlg::SetDefaults(const wxString &defaults)
{
    wxURI uri(defaults);

    if (uri.GetScheme() != wxT("ftp"))
        return;

    m_pUser->SetValue(uri.GetUser());
    m_pPassword->SetValue(uri.GetPassword());
    m_pDir->SetValue(uri.GetScheme() + wxT("://") + uri.GetServer() + uri.GetPath());

    bool isAnonymous = uri.GetUser().IsEmpty();
    m_pAnonymous->SetValue(isAnonymous);

    wxCommandEvent fake;
    fake.SetInt(isAnonymous);
    OnAnonymous(fake);
}


// ----------------------------------------------------------------------------
// wxPackageUploadDlg - event handlers
// ----------------------------------------------------------------------------

void wxPackageUploadDlg::OnAnonymous(wxCommandEvent &event)
{
    m_pUser->Enable(!event.IsChecked());
    m_pPassword->Enable(!event.IsChecked());
}



