Audacity 3.2.0
ModuleSettings.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 @file ModuleSettings.cpp
6
7 Paul Licameli split from ModulePrefs.cpp
8
9**********************************************************************/
10
11#include "ModuleSettings.h"
12
13#include "Prefs.h"
14
15#include <unordered_set>
16#include <wx/filename.h>
17
18#include "MemoryX.h"
19
21{
22 using KeyValueStorage = std::vector<std::pair<wxString, wxString>>;
23 std::optional<KeyValueStorage> mStorage;
24public:
25
27 {
28 assert(!mStorage.has_value());
29 }
30
31 void OnSettingResetBegin() override
32 {
33 assert(!mStorage.has_value());
34
35 static const wxString modulePrefsGroups[] = {
36 "/ModulePath/",
37 "/Module/",
38 "/ModuleDateTime/"
39 };
40 KeyValueStorage storage;
41 for(const auto& group : modulePrefsGroups)
42 {
43 if(!gPrefs->HasGroup(group))
44 continue;
45
46 const auto groupScope = gPrefs->BeginGroup(group);
47 for(const auto& key : gPrefs->GetChildKeys())
48 {
49 wxString value;
50 if(gPrefs->Read(key, &value))
51 storage.emplace_back(group + key, value);
52 }
53 }
54 mStorage = std::move(storage);
55 }
56
57 void OnSettingResetEnd() override
58 {
59 if(!mStorage.has_value())
60 return;
61 const auto Do = finally([=]{ mStorage = std::nullopt; });
62 for(const auto& [key, value] : *mStorage)
63 gPrefs->Write(key, value);
64 }
65};
66
68
69static const std::unordered_set<wxString> &autoEnabledModules()
70{
71 // Add names to this list, of modules that are expected to ship
72 // with Audacity and enable automatically.
73 static std::unordered_set<wxString> modules{
74 "mod-ogg",
75 "mod-flac",
76 "mod-mp2",
77 "mod-wavpack",
78 "mod-mp3",
79 "mod-mpg123",
80 "mod-pcm",
81 "mod-ffmpeg",
82 "mod-cl",
83 "mod-lof",
84 "mod-aup",
85 "mod-opus",
86 "mod-midi-import-export",
87 };
88 return modules;
89}
90
91// static function that tells us about a module.
93{
94 // Default status is NEW module, and we will ask once.
95 int iStatus = kModuleNew;
96
97 wxFileName FileName( fname );
98 wxString ShortName = FileName.GetName().Lower();
99
100 wxString PathPref = wxString( wxT("/ModulePath/") ) + ShortName;
101 wxString StatusPref = wxString( wxT("/Module/") ) + ShortName;
102 wxString DateTimePref = wxString( wxT("/ModuleDateTime/") ) + ShortName;
103
104 wxString ModulePath = gPrefs->Read( PathPref, wxEmptyString );
105 if( ModulePath.IsSameAs( fname ) )
106 {
107 gPrefs->Read( StatusPref, &iStatus, static_cast<int>(kModuleNew) );
108
109 wxDateTime DateTime = FileName.GetModificationTime();
110 wxDateTime OldDateTime;
111 OldDateTime.ParseISOCombined( gPrefs->Read( DateTimePref, wxEmptyString ) );
112
113 // Some platforms return milliseconds, some do not...level the playing field
114 DateTime.SetMillisecond( 0 );
115 OldDateTime.SetMillisecond( 0 );
116
117 // fix up a bad status or reset for newer module
118 if( iStatus > kModuleNew || !OldDateTime.IsEqualTo( DateTime ) )
119 {
120 iStatus=kModuleNew;
121 }
122 }
123 else
124 {
125 // Remove previously saved since it's no longer valid
126 gPrefs->DeleteEntry( PathPref );
127 gPrefs->DeleteEntry( StatusPref );
128 gPrefs->DeleteEntry( DateTimePref );
129 }
130
131 if (iStatus == kModuleNew) {
132 if (autoEnabledModules().count(ShortName))
133 iStatus = kModuleEnabled;
134 }
135
136 return iStatus;
137}
138
139void ModuleSettings::SetModuleStatus(const FilePath &fname, int iStatus)
140{
141 wxFileName FileName( fname );
142 wxDateTime DateTime = FileName.GetModificationTime();
143 wxString ShortName = FileName.GetName().Lower();
144
145 wxString PrefName = wxString( wxT("/Module/") ) + ShortName;
146 gPrefs->Write( PrefName, iStatus );
147
148 PrefName = wxString( wxT("/ModulePath/") ) + ShortName;
149 gPrefs->Write( PrefName, fname );
150
151 PrefName = wxString( wxT("/ModuleDateTime/") ) + ShortName;
152 gPrefs->Write( PrefName, DateTime.FormatISOCombined() );
153
154 gPrefs->Flush();
155}
156
wxT("CloseDown"))
static const std::unordered_set< wxString > & autoEnabledModules()
static PreferencesResetHandler::Registration< ModuleSettingsResetHandler > preserveModuleSettings
@ kModuleNew
@ kModuleEnabled
static const AudacityProject::AttachedObjects::RegisteredFactory key
audacity::BasicSettings * gPrefs
Definition: Prefs.cpp:68
wxString FilePath
Definition: Project.h:21
std::optional< KeyValueStorage > mStorage
std::vector< std::pair< wxString, wxString > > KeyValueStorage
void OnSettingResetBegin() override
Happens before preferences reset.
void OnSettingResetEnd() override
Happens after preferences reset.
Allows custom logic for preferences reset event.
Definition: Prefs.h:563
virtual bool Flush() noexcept=0
virtual bool HasGroup(const wxString &key) const =0
Checks whether specified group exists relative to the current group.
GroupScope BeginGroup(const wxString &prefix)
Appends a prefix to the current group or sets a new absolute path. Group that was set as current befo...
virtual wxArrayString GetChildKeys() const =0
Returns all child keys within the current group.
virtual bool Write(const wxString &key, bool value)=0
bool DeleteEntry(const wxString &key)
Deletes specified entry if exists.
virtual bool Read(const wxString &key, bool *value) const =0
MODULE_MANAGER_API void SetModuleStatus(const FilePath &fname, int iStatus)
MODULE_MANAGER_API int GetModuleStatus(const FilePath &fname)
Performs single-time global handler registration.
Definition: Prefs.h:570