From b592a4169f3ac2b46b37ff7eb2f39a2aff54893b Mon Sep 17 00:00:00 2001
From: Jon Evans <jon@craftyjon.com>
Date: Thu, 18 Jun 2020 23:23:03 -0400
Subject: [PATCH] Update project template system for new format

---
 common/project.cpp                        | 96 -----------------------
 include/project.h                         | 11 ---
 kicad/import_project.cpp                  |  4 +-
 kicad/kicad_manager_frame.cpp             | 33 +++++---
 kicad/project_template.cpp                |  7 +-
 kicad/tools/kicad_manager_control.cpp     |  5 +-
 kicad/tree_project_frame.cpp              |  2 +-
 pcbnew/dialogs/dialog_import_settings.cpp |  2 +-
 template/CMakeLists.txt                   |  2 +-
 template/kicad.kicad_pro                  | 61 ++++++++++++++
 template/kicad.pro                        | 34 --------
 11 files changed, 95 insertions(+), 162 deletions(-)
 create mode 100644 template/kicad.kicad_pro
 delete mode 100644 template/kicad.pro

diff --git a/common/project.cpp b/common/project.cpp
index ca3d940a39..117f430a44 100644
--- a/common/project.cpp
+++ b/common/project.cpp
@@ -267,102 +267,6 @@ void PROJECT::SetElem( ELEM_T aIndex, _ELEM* aElem )
 }
 
 
-static bool copy_pro_file_template( const SEARCH_STACK& aSearchS, const wxString& aDestination )
-{
-    if( aDestination.IsEmpty() )
-    {
-        wxLogTrace( tracePathsAndFiles, "%s: destination is empty.", __func__ );
-        return false;
-    }
-
-    wxString templateFile = wxT( "kicad." ) + LegacyProjectFileExtension;
-
-    wxString kicad_pro_template = aSearchS.FindValidPath( templateFile );
-
-    if( !kicad_pro_template )
-    {
-        wxLogTrace( tracePathsAndFiles, "%s: template file '%s' not found using search paths.",
-                    __func__, TO_UTF8( templateFile ) );
-
-        wxFileName  templ( wxStandardPaths::Get().GetDocumentsDir(),
-                            wxT( "kicad" ), LegacyProjectFileExtension );
-
-        if( !templ.IsFileReadable() )
-        {
-            wxString msg = wxString::Format( _(
-                    "Unable to find \"%s\" template config file." ),
-                    GetChars( templateFile ) );
-
-            DisplayErrorMessage( nullptr, _( "Error copying project file template" ), msg );
-
-            return false;
-        }
-
-        kicad_pro_template = templ.GetFullPath();
-    }
-
-    wxLogTrace( tracePathsAndFiles, "%s: using template file '%s' as project file.",
-                __func__, TO_UTF8( kicad_pro_template ) );
-
-    // Verify aDestination can be created. if this is not the case, wxCopyFile
-    // will generate a crappy log error message, and we *do not want* this kind
-    // of stupid message
-    wxFileName fn( aDestination );
-    bool success = true;
-
-    if( fn.IsOk() && fn.IsDirWritable() )
-        success = wxCopyFile( kicad_pro_template, aDestination );
-    else
-    {
-        wxLogMessage( _( "Cannot create prj file \"%s\" (Directory not writable)" ),
-                      GetChars( aDestination) );
-        success = false;
-    }
-
-    return success;
-}
-
-
-wxConfigBase* PROJECT::configCreate( const SEARCH_STACK& aSList,
-        const wxString& aGroupName, const wxString& aProjectFileName )
-{
-    wxConfigBase*   cfg = 0;
-    wxString        cur_pro_fn = !aProjectFileName ? GetProjectFullName() : aProjectFileName;
-
-    wxFileName filename( cur_pro_fn );
-
-    if( filename.GetExt() == ProjectFileExtension )
-    {
-        filename.SetExt( LegacyProjectFileExtension );
-        cur_pro_fn = filename.GetFullPath();
-    }
-
-    // If we do not have a project name or specified name, choose an empty file to store the
-    // temporary configuration data in.
-    if( cur_pro_fn.IsEmpty() )
-        cur_pro_fn = wxFileName::CreateTempFileName( GetProjectPath() );
-
-    if( wxFileName( cur_pro_fn ).IsFileReadable() )
-    {
-        // Note: currently, aGroupName is not used.
-        // Previoulsy, the version of aGroupName was tested, but it
-        // was useless, and if the version is important,
-        // this is not the right place here, because configCreate does know anything
-        // about info stored in this config file.
-        cfg = new wxFileConfig( wxEmptyString, wxEmptyString, cur_pro_fn, wxEmptyString );
-        return cfg;
-    }
-
-    // No suitable pro file was found, either does not exist, or not readable.
-    // Use the template kicad.pro file.  Find it by using caller's SEARCH_STACK.
-    copy_pro_file_template( aSList, cur_pro_fn );
-
-    cfg = new wxFileConfig( wxEmptyString, wxEmptyString, cur_pro_fn, wxEmptyString );
-
-    return cfg;
-}
-
-
 const wxString PROJECT::AbsolutePath( const wxString& aFileName ) const
 {
     wxFileName fn = aFileName;
diff --git a/include/project.h b/include/project.h
index 3e81735f50..9f282904bd 100644
--- a/include/project.h
+++ b/include/project.h
@@ -316,17 +316,6 @@ private:
         m_localSettings = aSettings;
     }
 
-    /**
-     * Function configCreate
-     * loads a *.pro file and returns a wxConfigBase.
-     *
-     * @param aSList is the KIFACE or PGM's SEARCH_STACK
-     * @param aGroupName is the default config file subset to use.
-     * @param aProjectFileName is the *.pro file to open.
-     */
-    wxConfigBase* configCreate( const SEARCH_STACK& aSList,
-            const wxString& aGroupName, const wxString& aProjectFileName = wxEmptyString );
-
     /**
      * Return the full path and file name of the project specific library table \a aLibTableName..
      */
diff --git a/kicad/import_project.cpp b/kicad/import_project.cpp
index 4d75fe595f..366f5b9329 100644
--- a/kicad/import_project.cpp
+++ b/kicad/import_project.cpp
@@ -77,7 +77,7 @@ void KICAD_MANAGER_FRAME::OnImportEagleFiles( wxCommandEvent& event )
 
     wxFileName pro = sch;
 
-    pro.SetExt( LegacyProjectFileExtension );
+    pro.SetExt( ProjectFileExtension );
 
     wxString protitle = _( "KiCad Project Destination" );
 
@@ -115,7 +115,7 @@ void KICAD_MANAGER_FRAME::OnImportEagleFiles( wxCommandEvent& event )
     }
 
     wxFileName pcb( sch );
-    pro.SetExt( LegacyProjectFileExtension );         // enforce extension
+    pro.SetExt( ProjectFileExtension );
     pcb.SetExt( LegacyPcbFileExtension );       // enforce extension
 
     if( !pro.IsAbsolute() )
diff --git a/kicad/kicad_manager_frame.cpp b/kicad/kicad_manager_frame.cpp
index ec85a294c0..b3cf982f27 100644
--- a/kicad/kicad_manager_frame.cpp
+++ b/kicad/kicad_manager_frame.cpp
@@ -370,20 +370,33 @@ void KICAD_MANAGER_FRAME::CreateNewProject( const wxFileName& aProjectFileName )
     // Init project filename.  This clears all elements from the project object.
     SetProjectFileName( aProjectFileName.GetFullPath() );
 
-    // Copy kicad.pro file from template folder.
+    // If the project is legacy, convert it
     if( !aProjectFileName.FileExists() )
     {
-        // TODO(JE) PROJECT provide in new format
-        wxString srcFileName = sys_search().FindValidPath( "kicad.pro" );
+        wxFileName legacyPro( aProjectFileName );
+        legacyPro.SetExt( LegacyProjectFileExtension );
 
-        wxFileName destFileName( aProjectFileName );
-        destFileName.SetExt( LegacyProjectFileExtension );
-
-        // Create a minimal project (.pro) file if the template project file could not be copied.
-        if( !wxFileName::FileExists( srcFileName )
-            || !wxCopyFile( srcFileName, destFileName.GetFullPath() ) )
+        if( legacyPro.FileExists() )
         {
-            Pgm().GetSettingsManager().SaveProject();
+            GetSettingsManager()->LoadProject( legacyPro.GetFullPath() );
+            GetSettingsManager()->SaveProject();
+
+            wxRemoveFile( legacyPro.GetFullPath() );
+        }
+        else
+        {
+            // Copy template project file from template folder.
+            wxString srcFileName = sys_search().FindValidPath( "kicad.kicad_pro" );
+
+            wxFileName destFileName( aProjectFileName );
+            destFileName.SetExt( ProjectFileExtension );
+
+            // Create a minimal project file if the template project file could not be copied
+            if( !wxFileName::FileExists( srcFileName )
+                || !wxCopyFile( srcFileName, destFileName.GetFullPath() ) )
+            {
+                Pgm().GetSettingsManager().SaveProject();
+            }
         }
     }
 
diff --git a/kicad/project_template.cpp b/kicad/project_template.cpp
index 5ded1c8c6c..d70acb5c16 100644
--- a/kicad/project_template.cpp
+++ b/kicad/project_template.cpp
@@ -2,7 +2,7 @@
  * This program source code file is part of KiCad, a free EDA CAD application.
  *
  * Copyright (C) 2012 Brian Sidebotham <brian.sidebotham@gmail.com>
- * Copyright (C) 1992-2019 KiCad Developers, see AUTHORS.txt for contributors.
+ * Copyright (C) 1992-2020 KiCad Developers, see AUTHORS.txt for contributors.
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -29,6 +29,7 @@
 #include <wx/wfstream.h>
 #include <wx/log.h>
 
+#include <wildcards_and_files_ext.h>
 #include "project_template.h"
 
 
@@ -128,7 +129,7 @@ size_t PROJECT_TEMPLATE::GetDestinationFiles( const wxFileName& aNewProjectPath,
 
     for( wxFileName& file : srcFiles )
     {
-        if( file.GetExt() == wxT( "pro" ) )
+        if( file.GetExt() == ProjectFileExtension || file.GetExt() == LegacyProjectFileExtension )
         {
             basename = file.GetName();
             projectCount++;
@@ -174,7 +175,7 @@ bool PROJECT_TEMPLATE::CreateProject( wxFileName& aNewProjectPath, wxString* aEr
 
     for( wxFileName& file : srcFiles )
     {
-        if( file.GetExt() == wxT( "pro" ) )
+        if( file.GetExt() == ProjectFileExtension || file.GetExt() == LegacyProjectFileExtension )
         {
             basename = file.GetName();
             projectCount++;
diff --git a/kicad/tools/kicad_manager_control.cpp b/kicad/tools/kicad_manager_control.cpp
index f87a123113..cd37457bda 100644
--- a/kicad/tools/kicad_manager_control.cpp
+++ b/kicad/tools/kicad_manager_control.cpp
@@ -192,11 +192,10 @@ int KICAD_MANAGER_CONTROL::NewFromTemplate( const TOOL_EVENT& aEvent )
 
     // wxFileName automatically extracts an extension.  But if it isn't
     // a .pro extension, we should keep it as part of the filename
-    if( !fn.GetExt().IsEmpty()
-        && fn.GetExt().ToStdString() != LegacyProjectFileExtension )
+    if( !fn.GetExt().IsEmpty() && fn.GetExt().ToStdString() != ProjectFileExtension )
         fn.SetName( fn.GetName() + wxT( "." ) + fn.GetExt() );
 
-    fn.SetExt( LegacyProjectFileExtension );     // enforce extension
+    fn.SetExt( ProjectFileExtension );
 
     if( !fn.IsAbsolute() )
         fn.MakeAbsolute();
diff --git a/kicad/tree_project_frame.cpp b/kicad/tree_project_frame.cpp
index 1856536648..d32aad898a 100644
--- a/kicad/tree_project_frame.cpp
+++ b/kicad/tree_project_frame.cpp
@@ -509,7 +509,7 @@ void TREE_PROJECT_FRAME::ReCreateTreePrj()
         fn.Clear();
         fn.SetPath( wxStandardPaths::Get().GetDocumentsDir() );
         fn.SetName( NAMELESS_PROJECT );
-        fn.SetExt( LegacyProjectFileExtension );
+        fn.SetExt( ProjectFileExtension );
     }
 
     bool prjOpened = fn.FileExists();
diff --git a/pcbnew/dialogs/dialog_import_settings.cpp b/pcbnew/dialogs/dialog_import_settings.cpp
index 3cc2bbe9fd..97b265299d 100644
--- a/pcbnew/dialogs/dialog_import_settings.cpp
+++ b/pcbnew/dialogs/dialog_import_settings.cpp
@@ -56,7 +56,7 @@ bool DIALOG_IMPORT_SETTINGS::TransferDataToWindow()
 void DIALOG_IMPORT_SETTINGS::OnBrowseClicked( wxCommandEvent& event )
 {
     wxFileName fn = m_frame->GetBoard()->GetFileName();
-    fn.SetExt( LegacyProjectFileExtension );
+    fn.SetExt( ProjectFileExtension );
 
     wxFileDialog dlg( this, _( "Import Settings From" ), fn.GetPath(), fn.GetFullName(),
                       PcbFileWildcard(), wxFD_OPEN | wxFD_FILE_MUST_EXIST | wxFD_CHANGE_DIR );
diff --git a/template/CMakeLists.txt b/template/CMakeLists.txt
index 49f3b2678a..d04471bc41 100644
--- a/template/CMakeLists.txt
+++ b/template/CMakeLists.txt
@@ -1,5 +1,5 @@
 install( FILES
-    kicad.pro
+    kicad.kicad_pro
 
     DESTINATION ${KICAD_TEMPLATE}
     COMPONENT resources
diff --git a/template/kicad.kicad_pro b/template/kicad.kicad_pro
new file mode 100644
index 0000000000..3310037592
--- /dev/null
+++ b/template/kicad.kicad_pro
@@ -0,0 +1,61 @@
+{
+  "board": {
+    "design_settings": {
+      "defaults": {
+        "board_outline_line_width": 0.1,
+        "copper_line_width": 0.2,
+        "copper_text_size_h": 1.5,
+        "copper_text_size_v": 1.5,
+        "copper_text_thickness": 0.3,
+        "other_line_width": 0.15,
+        "silk_line_width": 0.15,
+        "silk_text_size_h": 1.0,
+        "silk_text_size_v": 1.0,
+        "silk_text_thickness": 0.15
+      },
+      "diff_pair_dimensions": [],
+      "drc_exclusions": [],
+      "rules": {
+        "min_copper_edge_clearance": 0.0,
+        "solder_mask_clearance": 0.0,
+        "solder_mask_min_width": 0.0
+      },
+      "track_widths": [],
+      "via_dimensions": []
+    }
+  },
+  "boards": [],
+  "libraries": {
+    "pinned_footprint_libs": [],
+    "pinned_symbol_libs": []
+  },
+  "meta": {
+    "filename": "kicad.kicad_pro",
+    "version": 1
+  },
+  "net_settings": {
+    "classes": [
+      {
+        "clearance": 0.2,
+        "diff_pair_gap": 0.25,
+        "diff_pair_via_gap": 0.25,
+        "diff_pair_width": 0.2,
+        "microvia_diameter": 0.3,
+        "microvia_drill": 0.1,
+        "name": "Default",
+        "nets": [],
+        "track_width": 0.25,
+        "via_diameter": 0.8,
+        "via_drill": 0.4
+      }
+    ],
+    "meta": {
+      "version": 0
+    }
+  },
+  "pcbnew": {
+    "page_layout_descr_file": ""
+  },
+  "sheets": [],
+  "text_variables": {}
+}
diff --git a/template/kicad.pro b/template/kicad.pro
deleted file mode 100644
index 5cd098305b..0000000000
--- a/template/kicad.pro
+++ /dev/null
@@ -1,34 +0,0 @@
-update=05/04/2019 20:44:53
-version=1
-last_client=kicad
-[general]
-version=1
-RootSch=
-BoardNm=
-[pcbnew]
-version=1
-LastNetListRead=
-UseCmpFile=1
-PadDrill=0.600000000000
-PadDrillOvalY=0.600000000000
-PadSizeH=1.500000000000
-PadSizeV=1.500000000000
-PcbTextSizeV=1.500000000000
-PcbTextSizeH=1.500000000000
-PcbTextThickness=0.300000000000
-ModuleTextSizeV=1.000000000000
-ModuleTextSizeH=1.000000000000
-ModuleTextSizeThickness=0.150000000000
-SolderMaskClearance=0.000000000000
-SolderMaskMinWidth=0.000000000000
-DrawSegmentWidth=0.200000000000
-BoardOutlineThickness=0.100000000000
-ModuleOutlineThickness=0.150000000000
-CopperEdgeClearance=0.000000000000
-[cvpcb]
-version=1
-NetIExt=net
-[eeschema]
-version=1
-LibDir=
-[eeschema/libraries]