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 "mod-cloud-audiocom"
88 };
89 return modules;
90}
91
92// static function that tells us about a module.
94{
95 // Default status is NEW module, and we will ask once.
96 int iStatus = kModuleNew;
97
98 wxFileName FileName( fname );
99 wxString ShortName = FileName.GetName().Lower();
100
101 wxString PathPref = wxString( wxT("/ModulePath/") ) + ShortName;
102 wxString StatusPref = wxString( wxT("/Module/") ) + ShortName;
103 wxString DateTimePref = wxString( wxT("/ModuleDateTime/") ) + ShortName;
104
105 wxString ModulePath = gPrefs->Read( PathPref, wxEmptyString );
106 if( ModulePath.IsSameAs( fname ) )
107 {
108 gPrefs->Read( StatusPref, &iStatus, static_cast<int>(kModuleNew) );
109
110 wxDateTime DateTime = FileName.GetModificationTime();
111 wxDateTime OldDateTime;
112 OldDateTime.ParseISOCombined( gPrefs->Read( DateTimePref, wxEmptyString ) );
113
114 // Some platforms return milliseconds, some do not...level the playing field
115 DateTime.SetMillisecond( 0 );
116 OldDateTime.SetMillisecond( 0 );
117
118 // fix up a bad status or reset for newer module
119 if( iStatus > kModuleNew || !OldDateTime.IsEqualTo( DateTime ) )
120 {
121 iStatus=kModuleNew;
122 }
123 }
124 else
125 {
126 // Remove previously saved since it's no longer valid
127 gPrefs->DeleteEntry( PathPref );
128 gPrefs->DeleteEntry( StatusPref );
129 gPrefs->DeleteEntry( DateTimePref );
130 }
131
132 if (iStatus == kModuleNew) {
133 if (autoEnabledModules().count(ShortName))
134 iStatus = kModuleEnabled;
135 }
136
137 return iStatus;
138}
139
140void ModuleSettings::SetModuleStatus(const FilePath &fname, int iStatus)
141{
142 wxFileName FileName( fname );
143 wxDateTime DateTime = FileName.GetModificationTime();
144 wxString ShortName = FileName.GetName().Lower();
145
146 wxString PrefName = wxString( wxT("/Module/") ) + ShortName;
147 gPrefs->Write( PrefName, iStatus );
148
149 PrefName = wxString( wxT("/ModulePath/") ) + ShortName;
150 gPrefs->Write( PrefName, fname );
151
152 PrefName = wxString( wxT("/ModuleDateTime/") ) + ShortName;
153 gPrefs->Write( PrefName, DateTime.FormatISOCombined() );
154
155 gPrefs->Flush();
156}
157
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