KiCad PCB EDA Suite
Loading...
Searching...
No Matches
settings_manager.h
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2020 Jon Evans <[email protected]>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#ifndef _SETTINGS_MANAGER_H
22#define _SETTINGS_MANAGER_H
23
24#include <algorithm>
25#include <mutex>
26#include <typeinfo>
27#include <core/wx_stl_compat.h> // for wxString hash
29
30class COLOR_SETTINGS;
31class COMMON_SETTINGS;
32class KIWAY;
33class PROJECT;
34class PROJECT_FILE;
35class REPORTER;
36class wxSingleInstanceChecker;
37class wxFileName;
38class LOCKFILE;
39
40
42#define PROJECT_BACKUPS_DIR_SUFFIX wxT( "-backups" )
43
44
46{
47public:
48 SETTINGS_MANAGER( bool aHeadless = false );
49
51
55 bool IsOK() { return m_ok; }
56
62 void SetKiway( KIWAY* aKiway ) { m_kiway = aKiway; }
63
70 template<typename T>
71 T* RegisterSettings( T* aSettings, bool aLoadNow = true )
72 {
73 return static_cast<T*>( registerSettings( aSettings, aLoadNow ) );
74 }
75
76 void Load();
77
78 void Load( JSON_SETTINGS* aSettings );
79
80 void Save();
81
82 void Save( JSON_SETTINGS* aSettings );
83
89 void FlushAndRelease( JSON_SETTINGS* aSettings, bool aSave = true );
90
103 template<typename T>
104 T* GetAppSettings( const wxString& aFilename )
105 {
106 std::lock_guard lock( m_app_settings_mutex );
107
108 T* ret = nullptr;
109 size_t typeHash = typeid( T ).hash_code();
110
111 if( m_app_settings_cache.count( typeHash ) )
112 ret = static_cast<T*>( m_app_settings_cache.at( typeHash ) );
113
114 if( ret )
115 return ret;
116
117#if defined(__clang__)
118 auto it = std::find_if( m_settings.begin(), m_settings.end(),
119 [&]( const std::unique_ptr<JSON_SETTINGS>& aSettings )
120 {
121 return aSettings->GetFilename() == aFilename;
122 } );
123#else
124 auto it = std::find_if( m_settings.begin(), m_settings.end(),
125 []( const std::unique_ptr<JSON_SETTINGS>& aSettings )
126 {
127 return dynamic_cast<T*>( aSettings.get() );
128 } );
129#endif
130
131 if( it != m_settings.end() )
132 {
133 // Do NOT use dynamic_cast here. CLang will think it's the wrong class across
134 // compile boundaries and return nullptr.
135 ret = static_cast<T*>( it->get() );
136 }
137 else
138 {
139 throw std::runtime_error( "Tried to GetAppSettings before registering" );
140 }
141
142 m_app_settings_cache[typeHash] = ret;
143
144 return ret;
145 }
146
155 COLOR_SETTINGS* GetColorSettings( const wxString& aName = "user" );
156
157 std::vector<COLOR_SETTINGS*> GetColorSettingsList()
158 {
159 std::vector<COLOR_SETTINGS*> ret;
160
161 for( const std::pair<const wxString, COLOR_SETTINGS*>& entry : m_color_settings )
162 ret.push_back( entry.second );
163
164 std::sort( ret.begin(), ret.end(), []( COLOR_SETTINGS* a, COLOR_SETTINGS* b )
165 {
166 return a->GetName() < b->GetName();
167 } );
168
169 return ret;
170 }
171
181 void SaveColorSettings( COLOR_SETTINGS* aSettings, const std::string& aNamespace = "" );
182
189 COLOR_SETTINGS* AddNewColorSettings( const wxString& aFilename );
190
199 COLOR_SETTINGS* GetMigratedColorSettings();
200
206 COMMON_SETTINGS* GetCommonSettings() const { return m_common_settings; }
207
214 wxString GetPathForSettingsFile( JSON_SETTINGS* aSettings );
215
229 bool MigrateIfNeeded();
230
236 void SetMigrationSource( const wxString& aSource ) { m_migration_source = aSource; }
237
238 void SetMigrateLibraryTables( bool aMigrate = true ) { m_migrateLibraryTables = aMigrate; }
239
250 bool GetPreviousVersionPaths( std::vector<wxString>* aName = nullptr );
251
255 void ReloadColorSettings();
256
264 bool LoadProject( const wxString& aFullPath, bool aSetActive = true );
265
273 bool UnloadProject( PROJECT* aProject, bool aSave = true );
274
282 bool IsProjectOpen() const;
283
289 bool IsProjectOpenNotDummy() const;
290
296 PROJECT& Prj() const;
297
304 PROJECT* GetProject( const wxString& aFullPath ) const;
305
309 std::vector<wxString> GetOpenProjects() const;
310
318 bool SaveProject( const wxString& aFullPath = wxEmptyString, PROJECT* aProject = nullptr );
319
329 void SaveProjectAs( const wxString& aFullPath, PROJECT* aProject = nullptr );
330
339 void SaveProjectCopy( const wxString& aFullPath, PROJECT* aProject = nullptr );
340
344 wxString GetProjectBackupsPath() const;
345
354 bool BackupProject( REPORTER& aReporter, wxFileName& aTarget ) const;
355
362 bool TriggerBackupIfNeeded( REPORTER& aReporter ) const;
363
373 static bool IsSettingsPathValid( const wxString& aPath );
374
379 static wxString GetColorSettingsPath();
380
387 static std::string GetSettingsVersion();
388
392 static wxString GetUserSettingsPath();
393
394private:
395 JSON_SETTINGS* registerSettings( JSON_SETTINGS* aSettings, bool aLoadNow = true );
396
402 static int compareVersions( const std::string& aFirst, const std::string& aSecond );
403
412 static bool extractVersion( const std::string& aVersionString, int* aMajor = nullptr,
413 int* aMinor = nullptr );
414
421 COLOR_SETTINGS* loadColorSettingsByName( const wxString& aName );
422
423 COLOR_SETTINGS* registerColorSettings( const wxString& aFilename, bool aAbsolutePath = false );
424
425 void loadAllColorSettings();
426
433 bool loadProjectFile( PROJECT& aProject );
434
442 bool unloadProjectFile( PROJECT* aProject, bool aSave );
443
445 void registerBuiltinColorSettings();
446
447private:
448
451
454
455 std::vector<std::unique_ptr<JSON_SETTINGS>> m_settings;
456
457 std::unordered_map<wxString, COLOR_SETTINGS*> m_color_settings;
458
460 std::unordered_map<size_t, JSON_SETTINGS*> m_app_settings_cache;
462
463 // Convenience shortcut
465
467
470
472 bool m_ok;
473
475 std::vector<std::unique_ptr<PROJECT>> m_projects_list;
476
478 std::map<wxString, PROJECT*> m_projects;
479
481 std::map<wxString, PROJECT_FILE*> m_project_files;
482
484 std::unique_ptr<LOCKFILE> m_project_lock;
485
486 static wxString backupDateTimeFormat;
487};
488
489#endif
KICAD_PLUGIN_EXPORT SCENEGRAPH * Load(char const *aFileName)
Read a model file and creates a generic display structure.
Color settings are a bit different than most of the settings objects in that there can be more than o...
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition: kiway.h:285
The backing store for a PROJECT, in JSON format.
Definition: project_file.h:72
Container for project specific data.
Definition: project.h:64
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:72
std::unique_ptr< LOCKFILE > m_project_lock
Lock for loaded project (expand to multiple once we support MDI).
void SetMigrateLibraryTables(bool aMigrate=true)
T * RegisterSettings(T *aSettings, bool aLoadNow=true)
Take ownership of the pointer passed in.
wxString m_migration_source
std::map< wxString, PROJECT * > m_projects
Loaded projects, mapped according to project full name.
bool m_headless
True if running outside a UI context.
COMMON_SETTINGS * GetCommonSettings() const
Retrieve the common settings shared by all applications.
void SetMigrationSource(const wxString &aSource)
Helper for DIALOG_MIGRATE_SETTINGS to specify a source for migration.
std::mutex m_app_settings_mutex
T * GetAppSettings(const wxString &aFilename)
Return a handle to the a given settings by type.
std::map< wxString, PROJECT_FILE * > m_project_files
Loaded project files, mapped according to project full name.
std::unordered_map< wxString, COLOR_SETTINGS * > m_color_settings
void SetKiway(KIWAY *aKiway)
Associate this setting manager with the given Kiway.
std::vector< COLOR_SETTINGS * > GetColorSettingsList()
std::vector< std::unique_ptr< PROJECT > > m_projects_list
Loaded projects (ownership here).
std::vector< std::unique_ptr< JSON_SETTINGS > > m_settings
bool m_migrateLibraryTables
If true, the symbol and footprint library tables will be migrated from the previous version.
std::unordered_map< size_t, JSON_SETTINGS * > m_app_settings_cache
Cache for app settings.
bool m_ok
True if settings loaded successfully at construction.
static wxString backupDateTimeFormat
COMMON_SETTINGS * m_common_settings
KIWAY * m_kiway
The kiway this settings manager interacts with.
PROJECT & Prj()
Definition: kicad.cpp:597
#define KICOMMON_API
Definition: kicommon.h:28