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 T* ret = nullptr;
107 size_t typeHash = typeid( T ).hash_code();
108
109 if( m_app_settings_cache.count( typeHash ) )
110 ret = static_cast<T*>( m_app_settings_cache.at( typeHash ) );
111
112 if( ret )
113 return ret;
114
115#if defined(__clang__)
116 auto it = std::find_if( m_settings.begin(), m_settings.end(),
117 [&]( const std::unique_ptr<JSON_SETTINGS>& aSettings )
118 {
119 return aSettings->GetFilename() == aFilename;
120 } );
121#else
122 auto it = std::find_if( m_settings.begin(), m_settings.end(),
123 []( const std::unique_ptr<JSON_SETTINGS>& aSettings )
124 {
125 return dynamic_cast<T*>( aSettings.get() );
126 } );
127#endif
128
129 if( it != m_settings.end() )
130 {
131 ret = dynamic_cast<T*>( it->get() );
132 }
133 else
134 {
135 throw std::runtime_error( "Tried to GetAppSettings before registering" );
136 }
137
138 m_app_settings_cache[typeHash] = ret;
139
140 return ret;
141 }
142
151 COLOR_SETTINGS* GetColorSettings( const wxString& aName = "user" );
152
153 std::vector<COLOR_SETTINGS*> GetColorSettingsList()
154 {
155 std::vector<COLOR_SETTINGS*> ret;
156
157 for( const std::pair<const wxString, COLOR_SETTINGS*>& entry : m_color_settings )
158 ret.push_back( entry.second );
159
160 std::sort( ret.begin(), ret.end(), []( COLOR_SETTINGS* a, COLOR_SETTINGS* b )
161 {
162 return a->GetName() < b->GetName();
163 } );
164
165 return ret;
166 }
167
177 void SaveColorSettings( COLOR_SETTINGS* aSettings, const std::string& aNamespace = "" );
178
185 COLOR_SETTINGS* AddNewColorSettings( const wxString& aFilename );
186
195 COLOR_SETTINGS* GetMigratedColorSettings();
196
202 COMMON_SETTINGS* GetCommonSettings() const { return m_common_settings; }
203
210 wxString GetPathForSettingsFile( JSON_SETTINGS* aSettings );
211
225 bool MigrateIfNeeded();
226
232 void SetMigrationSource( const wxString& aSource ) { m_migration_source = aSource; }
233
234 void SetMigrateLibraryTables( bool aMigrate = true ) { m_migrateLibraryTables = aMigrate; }
235
246 bool GetPreviousVersionPaths( std::vector<wxString>* aName = nullptr );
247
251 void ReloadColorSettings();
252
260 bool LoadProject( const wxString& aFullPath, bool aSetActive = true );
261
269 bool UnloadProject( PROJECT* aProject, bool aSave = true );
270
278 bool IsProjectOpen() const;
279
285 bool IsProjectOpenNotDummy() const;
286
292 PROJECT& Prj() const;
293
300 PROJECT* GetProject( const wxString& aFullPath ) const;
301
305 std::vector<wxString> GetOpenProjects() const;
306
314 bool SaveProject( const wxString& aFullPath = wxEmptyString, PROJECT* aProject = nullptr );
315
325 void SaveProjectAs( const wxString& aFullPath, PROJECT* aProject = nullptr );
326
335 void SaveProjectCopy( const wxString& aFullPath, PROJECT* aProject = nullptr );
336
340 wxString GetProjectBackupsPath() const;
341
350 bool BackupProject( REPORTER& aReporter, wxFileName& aTarget ) const;
351
358 bool TriggerBackupIfNeeded( REPORTER& aReporter ) const;
359
369 static bool IsSettingsPathValid( const wxString& aPath );
370
375 static wxString GetColorSettingsPath();
376
383 static std::string GetSettingsVersion();
384
388 static wxString GetUserSettingsPath();
389
390private:
391 JSON_SETTINGS* registerSettings( JSON_SETTINGS* aSettings, bool aLoadNow = true );
392
398 static int compareVersions( const std::string& aFirst, const std::string& aSecond );
399
408 static bool extractVersion( const std::string& aVersionString, int* aMajor = nullptr,
409 int* aMinor = nullptr );
410
417 COLOR_SETTINGS* loadColorSettingsByName( const wxString& aName );
418
419 COLOR_SETTINGS* registerColorSettings( const wxString& aFilename, bool aAbsolutePath = false );
420
421 void loadAllColorSettings();
422
429 bool loadProjectFile( PROJECT& aProject );
430
438 bool unloadProjectFile( PROJECT* aProject, bool aSave );
439
441 void registerBuiltinColorSettings();
442
443private:
444
447
450
451 std::vector<std::unique_ptr<JSON_SETTINGS>> m_settings;
452
453 std::unordered_map<wxString, COLOR_SETTINGS*> m_color_settings;
454
456 std::unordered_map<size_t, JSON_SETTINGS*> m_app_settings_cache;
457
458 // Convenience shortcut
460
462
465
467 bool m_ok;
468
470 std::vector<std::unique_ptr<PROJECT>> m_projects_list;
471
473 std::map<wxString, PROJECT*> m_projects;
474
476 std::map<wxString, PROJECT_FILE*> m_project_files;
477
479 std::unique_ptr<LOCKFILE> m_project_lock;
480
481 static wxString backupDateTimeFormat;
482};
483
484#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.
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