KiCad PCB EDA Suite
Loading...
Searching...
No Matches
database_lib_settings.cpp File Reference
#include <core/kicad_algo.h>
#include <json_common.h>
#include <algorithm>
#include <set>
#include <database/database_lib_settings.h>
#include <kiplatform/secrets.h>
#include <settings/json_settings_internals.h>
#include <settings/parameters.h>
#include <string_utils.h>
#include <wildcards_and_files_ext.h>
#include <wx/filename.h>

Go to the source code of this file.

Functions

template<typename T>
static void patchKey (nlohmann::json &aObj, const std::string &aKey, const T &aValue, const T &aParsed, bool aRequired)
 Writes aValue to aObj[aKey] only if it differs from aParsed, the value the raw key was read as.
 
static void normalizeScalarParents (JSON_SETTINGS_INTERNALS &aInternals)
 Nested params cannot store into a non-object parent, so replace a malformed one before storing.
 
std::unique_ptr< DATABASE_LIB_SETTINGS > LoadDatabaseLibSettingsReadOnly (const wxString &aPath, bool *aLoaded)
 Loads a database library settings file without allowing the load itself (e.g.
 
wxString DefaultDatabaseLibFileName (const wxString &aLibName)
 
static wxString passwordService (const DATABASE_SOURCE &aSource)
 
DATABASE_SECRET_LOOKUP DefaultDatabaseSecretLookup ()
 The lookup used when none is injected, which must never prompt to unlock the keyring.
 
bool IsNoPromptSecretLookup (const DATABASE_SECRET_LOOKUP &aLookup)
 
static bool hasKeyringLogin (const DATABASE_SOURCE &aSource)
 
bool GetDatabasePassword (const DATABASE_SOURCE &aSource, std::string &aPassword, const DATABASE_SECRET_LOOKUP &aLookup)
 DSN passwords that are not saved in the (often shared) library file live in the system keyring, keyed by DSN and user name so every library on the same login shares one entry.
 
void ResolveDatabasePassword (DATABASE_SOURCE &aSource, const DATABASE_SECRET_LOOKUP &aLookup)
 Fills in the source password from the keyring, but only when the library file did not supply one.
 
bool StoreDatabasePassword (const DATABASE_SOURCE &aSource, const std::string &aPassword)
 Stores the password for the source's DSN login in the system keyring, or removes the entry when the password is empty.
 

Variables

const int dblibSchemaVersion = 1
 
static constexpr const char * c_scalarParents [] = { "/source", "/cache" }
 Objects that hold scalar settings rather than library tables.
 

Function Documentation

◆ DefaultDatabaseLibFileName()

wxString DefaultDatabaseLibFileName ( const wxString & aLibName)
Returns
a file name without extension for a new settings file, derived from the library name the user typed, or a generic name when that leaves nothing usable

Definition at line 431 of file database_lib_settings.cpp.

References name, and ReplaceIllegalFileNameChars().

Referenced by DIALOG_DATABASE_LIB_SETTINGS::chooseSavePath().

◆ DefaultDatabaseSecretLookup()

DATABASE_SECRET_LOOKUP DefaultDatabaseSecretLookup ( )

The lookup used when none is injected, which must never prompt to unlock the keyring.

Definition at line 451 of file database_lib_settings.cpp.

References KIPLATFORM::SECRETS::GetSecretNoPrompt().

Referenced by GetDatabasePassword().

◆ GetDatabasePassword()

bool GetDatabasePassword ( const DATABASE_SOURCE & aSource,
std::string & aPassword,
const DATABASE_SECRET_LOOKUP & aLookup = nullptr )

DSN passwords that are not saved in the (often shared) library file live in the system keyring, keyed by DSN and user name so every library on the same login shares one entry.

Parameters
aLookupreplaces the system keyring when set, for tests.
Returns
false when the source has no DSN login or the keyring holds no password for it

Definition at line 474 of file database_lib_settings.cpp.

References DefaultDatabaseSecretLookup(), hasKeyringLogin(), passwordService(), and DATABASE_SOURCE::username.

Referenced by ResolveDatabasePassword(), and DIALOG_DATABASE_LIB_SETTINGS::TransferDataToWindow().

◆ hasKeyringLogin()

static bool hasKeyringLogin ( const DATABASE_SOURCE & aSource)
static

◆ IsNoPromptSecretLookup()

bool IsNoPromptSecretLookup ( const DATABASE_SECRET_LOOKUP & aLookup)
Returns
true when aLookup is the system keyring lookup that never prompts to unlock it.

Definition at line 457 of file database_lib_settings.cpp.

References KIPLATFORM::SECRETS::GetSecretNoPrompt().

◆ LoadDatabaseLibSettingsReadOnly()

std::unique_ptr< DATABASE_LIB_SETTINGS > LoadDatabaseLibSettingsReadOnly ( const wxString & aPath,
bool * aLoaded = nullptr )

Loads a database library settings file without allowing the load itself (e.g.

a schema migration) to write back to disk. Callers must SetReadOnly( false ) around an explicit save.

Parameters
aLoadedis set to false when the file exists but could not be read; saving such settings would replace the file with defaults

Definition at line 411 of file database_lib_settings.cpp.

References FILEEXT::DatabaseLibraryFileExtension, and path.

Referenced by BOOST_AUTO_TEST_CASE().

◆ normalizeScalarParents()

static void normalizeScalarParents ( JSON_SETTINGS_INTERNALS & aInternals)
static

Nested params cannot store into a non-object parent, so replace a malformed one before storing.

Definition at line 316 of file database_lib_settings.cpp.

References c_scalarParents.

Referenced by DATABASE_LIB_SETTINGS::Store().

◆ passwordService()

static wxString passwordService ( const DATABASE_SOURCE & aSource)
static

Definition at line 445 of file database_lib_settings.cpp.

References DATABASE_SOURCE::dsn.

Referenced by GetDatabasePassword(), and StoreDatabasePassword().

◆ patchKey()

template<typename T>
static void patchKey ( nlohmann::json & aObj,
const std::string & aKey,
const T & aValue,
const T & aParsed,
bool aRequired )
static

Writes aValue to aObj[aKey] only if it differs from aParsed, the value the raw key was read as.

An unchanged key keeps its raw form (or stays absent) unless it is required and missing.

Definition at line 76 of file database_lib_settings.cpp.

References T.

Referenced by DATABASE_LIB_SETTINGS::fieldToJson(), and DATABASE_LIB_SETTINGS::tableToJson().

◆ ResolveDatabasePassword()

void ResolveDatabasePassword ( DATABASE_SOURCE & aSource,
const DATABASE_SECRET_LOOKUP & aLookup )

Fills in the source password from the keyring, but only when the library file did not supply one.

Definition at line 491 of file database_lib_settings.cpp.

References GetDatabasePassword(), and DATABASE_SOURCE::password.

Referenced by DATABASE_LIB_SETTINGS::ResolvePassword().

◆ StoreDatabasePassword()

bool StoreDatabasePassword ( const DATABASE_SOURCE & aSource,
const std::string & aPassword )

Stores the password for the source's DSN login in the system keyring, or removes the entry when the password is empty.

Definition at line 498 of file database_lib_settings.cpp.

References KIPLATFORM::SECRETS::DeleteSecret(), hasKeyringLogin(), passwordService(), KIPLATFORM::SECRETS::StoreSecret(), and DATABASE_SOURCE::username.

Referenced by DIALOG_DATABASE_LIB_SETTINGS::TransferDataFromWindow().

Variable Documentation

◆ c_scalarParents

const char* c_scalarParents[] = { "/source", "/cache" }
staticconstexpr

Objects that hold scalar settings rather than library tables.

Definition at line 312 of file database_lib_settings.cpp.

Referenced by normalizeScalarParents(), and DATABASE_LIB_SETTINGS::Store().

◆ dblibSchemaVersion

const int dblibSchemaVersion = 1