From 6da5e2cdd04327b1b452d7f426642b520cf8a747 Mon Sep 17 00:00:00 2001 From: Moses McKnight Date: Fri, 5 Sep 2014 17:12:38 -0400 Subject: [PATCH] Configuration file consolidation patch from Moses McKnight. * Create GetNewConfig() and GetKicadConfigPath() to unify configuration file creation and location. * Move Windows configuration out of the registry into configuration files. * Move Linux configuration files from $HOME to $HOME/.config/kicad to eliminate configuration file pollution in the users $HOME folder. * Fix a bug in the configuration file where the Eeschema hot keys are saved. --- bitmap2component/bitmap2cmp_gui.cpp | 26 +++++------ common/bin_mod.cpp | 5 +-- common/common.cpp | 58 ++++++++++++++++++++++++- common/fp_lib_table.cpp | 9 +--- common/hotkeys_basic.cpp | 12 ++--- common/pgm_base.cpp | 2 +- include/common.h | 22 ++++++++++ pcb_calculator/pcb_calculator.h | 6 +-- pcb_calculator/pcb_calculator_frame.cpp | 3 +- pcbnew/pcbnew.cpp | 6 +-- 10 files changed, 110 insertions(+), 39 deletions(-) diff --git a/bitmap2component/bitmap2cmp_gui.cpp b/bitmap2component/bitmap2cmp_gui.cpp index 4e0e06983f..e0de783ad8 100644 --- a/bitmap2component/bitmap2cmp_gui.cpp +++ b/bitmap2component/bitmap2cmp_gui.cpp @@ -65,19 +65,19 @@ extern int bitmap2component( potrace_bitmap_t* aPotrace_bitmap, FILE* aOutfile, class BM2CMP_FRAME : public BM2CMP_FRAME_BASE { private: - wxImage m_Pict_Image; - wxBitmap m_Pict_Bitmap; - wxImage m_Greyscale_Image; - wxBitmap m_Greyscale_Bitmap; - wxImage m_NB_Image; - wxBitmap m_BN_Bitmap; - wxSize m_imageDPI; // The initial image resolution. When unknown, + wxImage m_Pict_Image; + wxBitmap m_Pict_Bitmap; + wxImage m_Greyscale_Image; + wxBitmap m_Greyscale_Bitmap; + wxImage m_NB_Image; + wxBitmap m_BN_Bitmap; + wxSize m_imageDPI; // The initial image resolution. When unknown, // set to DEFAULT_DPI x DEFAULT_DPI per Inch - wxString m_BitmapFileName; - wxString m_ConvertedFileName; - wxSize m_frameSize; - wxPoint m_framePos; - wxConfig* m_config; + wxString m_BitmapFileName; + wxString m_ConvertedFileName; + wxSize m_frameSize; + wxPoint m_framePos; + wxConfigBase* m_config; public: BM2CMP_FRAME( KIWAY* aKiway, wxWindow* aParent ); @@ -147,7 +147,7 @@ BM2CMP_FRAME::BM2CMP_FRAME( KIWAY* aKiway, wxWindow* aParent ) : SetKiway( this, aKiway ); int tmp; - m_config = new wxConfig(); + m_config = GetNewConfig( Pgm().App().GetAppName() ); m_config->Read( KEYWORD_FRAME_POSX, & m_framePos.x, -1 ); m_config->Read( KEYWORD_FRAME_POSY, & m_framePos.y, -1 ); m_config->Read( KEYWORD_FRAME_SIZEX, & m_frameSize.x, -1 ); diff --git a/common/bin_mod.cpp b/common/bin_mod.cpp index afce71336d..ca16c60fde 100644 --- a/common/bin_mod.cpp +++ b/common/bin_mod.cpp @@ -1,8 +1,7 @@ - -#include #include #include +#include BIN_MOD::BIN_MOD( const char* aName ) : @@ -15,7 +14,7 @@ BIN_MOD::BIN_MOD( const char* aName ) : void BIN_MOD::Init() { // do an OS specific wxConfig instantiation, using the bin_mod (EXE/DLL/DSO) name. - m_config = new wxConfig( wxString::FromUTF8( m_name ) ); + m_config = GetNewConfig( wxString::FromUTF8( m_name ) ); m_history.Load( *m_config ); diff --git a/common/common.cpp b/common/common.cpp index 3a0d9551de..88916764f0 100644 --- a/common/common.cpp +++ b/common/common.cpp @@ -39,6 +39,9 @@ #include #include +#include +#include +#include /** @@ -280,8 +283,8 @@ double RoundTo0( double x, double precision ) wxString FormatDateLong( const wxDateTime &aDate ) { - /* GetInfo was introduced only on wx 2.9; for portability reason an - * hardcoded format is used on wx 2.8 */ + // GetInfo was introduced only on wx 2.9; for portability reason an + // hardcoded format is used on wx 2.8 #if wxCHECK_VERSION( 2, 9, 0 ) return aDate.Format( wxLocale::GetInfo( wxLOCALE_LONG_DATE_FMT ) ); #else @@ -289,3 +292,54 @@ wxString FormatDateLong( const wxDateTime &aDate ) #endif } + +wxConfigBase* GetNewConfig( const wxString& aProgName ) +{ + wxConfigBase* cfg = 0; + wxFileName configname; + configname.AssignDir( GetKicadConfigPath() ); + configname.SetFullName( aProgName ); + + cfg = new wxFileConfig( wxT( "" ), wxT( "" ), configname.GetFullPath() ); + return cfg; +} + + +wxString GetKicadConfigPath() +{ + wxFileName cfgpath; + + // From the wxWidgets wxStandardPaths::GetUserConfigDir() help: + // Unix: ~ (the home directory) + // Windows: "C:\Documents and Settings\username\Application Data" + // Mac: ~/Library/Preferences + cfgpath.AssignDir( wxStandardPaths::Get().GetUserConfigDir() ); + +#if !defined( __WINDOWS__ ) && !defined( __WXMAC__ ) + wxString envstr; + + if( !wxGetEnv( wxT( "XDG_CONFIG_HOME" ), &envstr ) || envstr.IsEmpty() ) + { + // XDG_CONFIG_HOME is not set, so use the fallback + cfgpath.AppendDir( wxT( ".config" ) ); + } + else + { + // Override the assignment above with XDG_CONFIG_HOME + cfgpath.AssignDir( envstr ); + } +#endif + + cfgpath.AppendDir( wxT( "kicad" ) ); + +#if !wxCHECK_VERSION( 2, 9, 0 ) + #define wxS_DIR_DEFAULT 0777 +#endif + + if( !cfgpath.DirExists() ) + { + cfgpath.Mkdir( wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ); + } + + return cfgpath.GetPath(); +} diff --git a/common/fp_lib_table.cpp b/common/fp_lib_table.cpp index 2b5a47aa9c..420693da4d 100644 --- a/common/fp_lib_table.cpp +++ b/common/fp_lib_table.cpp @@ -733,14 +733,7 @@ wxString FP_LIB_TABLE::GetGlobalTableFileName() { wxFileName fn; - // This is possibly problematic with an uncertain wxApp title, which is now - // the case. We'll need a better technique soon. - fn.SetPath( wxStandardPaths::Get().GetUserConfigDir() ); - -#if defined( __WINDOWS__ ) - fn.AppendDir( wxT( "kicad" ) ); -#endif - + fn.SetPath( GetKicadConfigPath() ); fn.SetName( global_tbl_name ); return fn.GetFullPath(); diff --git a/common/hotkeys_basic.cpp b/common/hotkeys_basic.cpp index 273a8240a9..e8d0532ac5 100644 --- a/common/hotkeys_basic.cpp +++ b/common/hotkeys_basic.cpp @@ -533,8 +533,9 @@ int EDA_BASE_FRAME::WriteHotkeyConfig( struct EDA_HOTKEY_CONFIG* aDescList, } else { - wxConfig config( m_FrameName ); - config.Write( HOTKEYS_CONFIG_KEY, msg ); + wxConfigBase* config = GetNewConfig( m_FrameName ); + config->Write( HOTKEYS_CONFIG_KEY, msg ); + delete config; } return 1; @@ -575,16 +576,17 @@ int EDA_BASE_FRAME::ReadHotkeyConfigFile( const wxString& aFilename, void ReadHotkeyConfig( const wxString& Appname, struct EDA_HOTKEY_CONFIG* aDescList ) { - wxConfig config( Appname ); + wxConfigBase* config = GetNewConfig( Appname ); - if( !config.HasEntry( HOTKEYS_CONFIG_KEY ) ) + if( !config->HasEntry( HOTKEYS_CONFIG_KEY ) ) { // assume defaults are ok return; } wxString data; - config.Read( HOTKEYS_CONFIG_KEY, &data ); + config->Read( HOTKEYS_CONFIG_KEY, &data ); + delete config; ParseHotkeyConfig( data, aDescList ); } diff --git a/common/pgm_base.cpp b/common/pgm_base.cpp index e521e3ce7d..4f7a87ec38 100644 --- a/common/pgm_base.cpp +++ b/common/pgm_base.cpp @@ -405,7 +405,7 @@ bool PGM_BASE::initPgm() SetLanguagePath(); // OS specific instantiation of wxConfigBase derivative: - m_common_settings = new wxConfig( KICAD_COMMON ); + m_common_settings = GetNewConfig( KICAD_COMMON ); ReadPdfBrowserInfos(); // needs m_common_settings diff --git a/include/common.h b/include/common.h index 87fc92209a..d62ba148c0 100644 --- a/include/common.h +++ b/include/common.h @@ -612,5 +612,27 @@ wxString SearchHelpFileFullPath( const SEARCH_STACK& aSearchStack, const wxStrin /// Put aPriorityPath in front of all paths in the value of aEnvVar. const wxString PrePendPath( const wxString& aEnvVar, const wxString& aPriorityPath ); +/** + * Function GetNewConfig + * + * Use this function instead of creating a new wxConfig so we can put config files in + * a more proper place for each platform. This is generally $HOME/.config/kicad/ in Linux + * according to the FreeDesktop specification at + * http://standards.freedesktop.org/basedir-spec/basedir-spec-0.6.html + * The config object created here should be destroyed by the caller. + * + * @param aProgName is the name of the program calling this function - can be obtained by + * calling Pgm().App().GetAppName(). This will be the actual file name of the config file. + * @return A pointer to a new wxConfigBase derived object is returned. The caller is in charge + * of deleting it. + */ +wxConfigBase* GetNewConfig( const wxString& aProgName ); + +/** + * Function GetKicadConfigPath + * @return A wxString containing the config path for Kicad + */ +wxString GetKicadConfigPath(); + #endif // INCLUDE__COMMON_H_ diff --git a/pcb_calculator/pcb_calculator.h b/pcb_calculator/pcb_calculator.h index 0c3633a5ca..74ddb44703 100644 --- a/pcb_calculator/pcb_calculator.h +++ b/pcb_calculator/pcb_calculator.h @@ -26,9 +26,9 @@ private: bool m_RegulatorListChanged; // set to true when m_RegulatorList // was modified, and the corresponging file // must be rewritten - wxSize m_FrameSize; - wxPoint m_FramePos; - wxConfig * m_Config; + wxSize m_FrameSize; + wxPoint m_FramePos; + wxConfigBase* m_Config; enum TRANSLINE_TYPE_ID m_currTransLineType; TRANSLINE * m_currTransLine; // a pointer to the active transline // List of translines: ordered like in dialog menu list diff --git a/pcb_calculator/pcb_calculator_frame.cpp b/pcb_calculator/pcb_calculator_frame.cpp index 3abd3601f2..fdf2513861 100644 --- a/pcb_calculator/pcb_calculator_frame.cpp +++ b/pcb_calculator/pcb_calculator_frame.cpp @@ -24,6 +24,7 @@ #include #include +#include #include #include #include @@ -61,7 +62,7 @@ PCB_CALCULATOR_FRAME::PCB_CALCULATOR_FRAME( KIWAY* aKiway, wxWindow* aParent ) : m_currTransLineType = DEFAULT_TYPE; m_currAttenuator = NULL; m_RegulatorListChanged = false; - m_Config = new wxConfig(); + m_Config = GetNewConfig( Pgm().App().GetAppName() ); // Populate transline list ordered like in dialog menu list const static TRANSLINE_TYPE_ID tltype_list[8] = diff --git a/pcbnew/pcbnew.cpp b/pcbnew/pcbnew.cpp index db578ec9ce..9c68633ef8 100644 --- a/pcbnew/pcbnew.cpp +++ b/pcbnew/pcbnew.cpp @@ -256,7 +256,7 @@ static bool scriptingSetup() // (and remove the fixed paths from /scripting/kicadplugins.i) // wizard plugins are stored in kicad/bin/plugins. - // so add this path to python scripting defualt search paths + // so add this path to python scripting default search paths // which are ( [KICAD_PATH] is an environment variable to define) // [KICAD_PATH]/scripting/plugins // Add this default search path: @@ -336,11 +336,11 @@ bool IFACE::OnKifaceStart( PGM_BASE* aProgram, int aCtlBits ) "You have run Pcbnew for the first time using the " "new footprint library table method for finding " "footprints. Pcbnew has either copied the default " - "table or created an empty table in your home " + "table or created an empty table in the kicad configuration " "folder. You must first configure the library " "table to include all footprint libraries not " "included with KiCad. See the \"Footprint Library " - "Table\" section of the CvPcb documentation for " + "Table\" section of the CvPcb or Pcbnew documentation for " "more information." ) ); } }