/////////////////////////////////////////////////////////////////////////////
// Name:        proppanel_basics.cpp
// Purpose:     wxPackagePropertiesPanel
//              (stuff for handling the "basics" page)
// Author:      Francesco Montorsi
// Modified by:
// Created:     28/06/2006 19:22:47
// RCS-ID:      $Id: proppanel_basics.cpp,v 1.5 2007/01/02 19:33:00 frm Exp $
// Copyright:   (c) 2006 Julian Smart, 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 "guipkg/proppanel.h"
#include "guicmn/commandlistbox.h"

#include "wxp/package.h"
#include "wxp/wxp.h"
#include "wxp/packagewxp.h"

#include "wx/miscutils.h"


// ----------------------------------------------------------------------------
// wxPackagePropertiesPanel - basics panel - I/O
// ----------------------------------------------------------------------------

void wxPackagePropertiesPanel::InitBasicsPage()
{
    m_pPrimaryCat->Append(wxArrayString(wxPC_MAX, wxPackageCategoryName));
    m_pSecondaryCat->Append(wxArrayString(wxPC_MAX, wxPackageCategoryName));
    m_pPrimaryCat->Select(0);
    m_pSecondaryCat->Select(0);
}

void wxPackagePropertiesPanel::GetBasicsPackageInfo(wxPackageInfo &ret) const
{
    // BASIC info
    ret.SetName(m_pName->GetValue());
    ret.SetVersion(m_pVersion->GetValue());
    ret.SetDescription(m_pDescription->GetValue());
    ret.SetKeywords(m_pKeywords->GetValue());
    ret.SetCategories((wxPackageCategory)m_pPrimaryCat->GetSelection(),
                      (wxPackageCategory)m_pSecondaryCat->GetSelection());
}

void wxPackagePropertiesPanel::DoSetBasicsPackageInfo(const wxPackageInfo &p)
{
    // BASIC info
    m_pName->SetValue(p.GetName());
    if (p.GetVersion().IsOk())
        m_pVersion->SetValue(p.GetVersion());
    else
        m_pVersion->SetValue(wxEmptyString);
    m_pDescription->SetValue(p.GetDescription());
    m_pKeywords->SetValue(p.GetKeywordsStr());
    m_pPrimaryCat->Select(p.GetPrimaryCategory());
    m_pSecondaryCat->Select(p.GetSecondaryCategory());
}

bool wxPackagePropertiesPanel::IsBasicsPageOk() const
{
    bool isok = true;

    // check name
    wxString name(m_pName->GetValue());
    if (name.IsEmpty())
    {
        wxLogError(wxT("The package name is not optional"));
        isok = false;
    }
    else if (!wxIsPlainWord(name))
    {
        wxLogError(wxT("Invalid name '%s'.\nYou can use only the following characters:\n%s"),
                   name.c_str(), wxGetPlainWordAllowedCharacters().c_str());
        isok = false;
    }

    // check version
    wxString ver(m_pVersion->GetValue());
    if (ver.IsEmpty())
    {
        wxLogError(wxT("The package version is not optional"));
        isok = false;
    }
    else if (!wxVersion(ver).IsOk())
    {
        wxLogError(wxT("Invalid version syntax '%s'"), ver.c_str());
        isok = false;
    }

    // check description
    wxString desc(m_pDescription->GetValue());
    if (desc.IsEmpty())
    {
        wxLogError(wxT("The package description is not optional"));
        isok = false;
    }

    // check keywords
    wxString k(m_pKeywords->GetValue());
    if (k.IsEmpty())
    {
        wxLogError(wxT("The package keywords are not optional"));
        isok = false;
    }

    // check categories
    if (m_pPrimaryCat->GetSelection() == m_pSecondaryCat->GetSelection())
    {
        wxLogWarning(wxT("The same category is selected both as primary and as secondary."));
        isok = false;
    }

    return isok;
}

