WIZARD_FPLIB_TABLE::~WIZARD_FPLIB_TABLE()
{
    // Use this if you want to store kicad lib URL in pcbnew/cvpcb section config:
//    wxConfigBase* cfg = Kiface().KifaceSettings();

    // Use this if you want to store kicad lib URL in common section config:
    wxConfigBase* cfg = Pgm().CommonSettings();
    cfg->Write( KICAD_FPLIBS_URL_KEY, GetGithubURL() );
}
WIZARD_3DSHAPE_LIBS_DOWNLOADER::~WIZARD_3DSHAPE_LIBS_DOWNLOADER()
{
    // Use this if you want to store kicad lib URL in pcbnew/cvpcb section config:
    // wxConfigBase* cfg = Kiface().KifaceSettings();

    // Use this if you want to store kicad lib URL in common section config:
    wxConfigBase* cfg = Pgm().CommonSettings();
    cfg->Write( KICAD_3DLIBS_URL_KEY, GetGithubURL() );
    cfg->Write( KICAD_3DLIBS_LAST_DOWNLOAD_DIR, getDownloadDir() );
}
// Download the .pretty libraries folders found in aUrlList and store them on disk
// in a master folder
bool WIZARD_3DSHAPE_LIBS_DOWNLOADER::downloadGithubLibsFromList( wxArrayString& aUrlList,
                                                     wxString* aErrorMessage )
{
    // Display a progress bar to show the download state
    // The title is updated for each downloaded library.
    // the state will be updated by downloadOneLib() for each file.
    wxProgressDialog pdlg( _( "Downloading 3D libraries" ), wxEmptyString,
                           aUrlList.GetCount(), GetParent(),
                           wxPD_CAN_ABORT | wxPD_APP_MODAL | wxPD_AUTO_HIDE );

    wxString url_base = GetGithubURL();

    // Download libs:
    for( unsigned ii = 0; ii < aUrlList.GetCount(); ii++ )
    {
        wxString& libsrc_name = aUrlList[ii];

        // Extract the lib name from the full URL:
        wxString url = GetGithubURL() + wxT( "/" ) + libsrc_name;
        wxFileName fn( libsrc_name );
        // Set our local path
        fn.SetPath( getDownloadDir() );
        wxString libdst_name = fn.GetFullPath();

        // Display the name of the library to download in the wxProgressDialog
        pdlg.SetTitle( wxString::Format( wxT("%s [%d/%d]" ),
                       libsrc_name.AfterLast( '/' ).GetData(),
                       ii + 1, aUrlList.GetCount() ) );

        if( !wxDirExists( libdst_name ) )
            wxMkdir( libdst_name );

        if( !downloadOneLib( url, libdst_name, &pdlg, aErrorMessage ) )
            return false;
    }

    return true;
}
void WIZARD_FPLIB_TABLE::updateLibraries()
{
    // No need to update, the review list is ready
    if( m_listCtrlReview->GetItemCount() != 0 )
        return;

    switch( GetLibSource() )
    {
    case LOCAL:
    {
        wxArrayString libs;
        m_filePicker->GetPaths( libs );

        // Workaround, when you change filters "/" is automatically selected
        int slash_index = libs.Index( "/", true, true );
        if( slash_index != wxNOT_FOUND )
            libs.RemoveAt( slash_index, 1 );

        m_libraries.reserve( libs.GetCount() );

        for( unsigned int i = 0; i < libs.GetCount(); ++i )
            m_libraries.push_back( libs[i] );
    }
    break;

    case GITHUB:
    {
        wxArrayInt checkedLibs;
        m_checkListGH->GetCheckedItems( checkedLibs );

        m_libraries.reserve( checkedLibs.GetCount() );

        for( unsigned int i = 0; i < checkedLibs.GetCount(); ++i )
            m_libraries.push_back( GetGithubURL() + "/" + m_checkListGH->GetString( checkedLibs[i] ) );
    }
    break;

    default:
        wxASSERT( false );
        break;
    }
}
void WIZARD_FPLIB_TABLE::OnWizardFinished( wxWizardEvent& aEvent )
{
#ifdef BUILD_GITHUB_PLUGIN
    // Shall we download a localy copy of the libraries
    if( GetLibSource() == GITHUB && m_downloadGithub->GetValue() )
    {
        wxString error;
        wxArrayString libs;

        // Prepare a list of libraries to download
        for( std::vector<LIBRARY>::const_iterator it = m_libraries.begin();
                it != m_libraries.end(); ++it )
        {
            wxASSERT( it->GetPluginType() == IO_MGR::GITHUB );

            if( it->GetStatus() != LIBRARY::INVALID )
                libs.Add( it->GetAbsolutePath() );
        }

        if( !downloadGithubLibsFromList( libs, &error ) )
        {
            DisplayError( this, error );
            m_libraries.clear();
        }
        else
        {
            // Now libraries are stored locally, so update the paths to point to the download folder
            for( std::vector<LIBRARY>::iterator it = m_libraries.begin();
                    it != m_libraries.end(); ++it )
            {
                wxString path = it->GetAbsolutePath();
                path.Replace( GetGithubURL(), getDownloadDir() );
                it->setPath( path );
                it->setPluginType( IO_MGR::KICAD );
            }
        }
    }
#endif
}