File: ModuleSettings.cpp

package info (click to toggle)
audacity 3.7.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 134,800 kB
  • sloc: cpp: 366,277; ansic: 198,323; lisp: 7,761; sh: 3,414; python: 1,501; xml: 1,385; perl: 854; makefile: 125
file content (161 lines) | stat: -rw-r--r-- 4,425 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**********************************************************************

  Audacity: A Digital Audio Editor

  @file ModuleSettings.cpp

  Paul Licameli split from ModulePrefs.cpp

**********************************************************************/

#include "ModuleSettings.h"

#include "Prefs.h"

#include <unordered_set>
#include <wx/filename.h>

#include "MemoryX.h"

class ModuleSettingsResetHandler final : public PreferencesResetHandler
{
   using KeyValueStorage = std::vector<std::pair<wxString, wxString>>;
   std::optional<KeyValueStorage> mStorage;
public:

   ~ModuleSettingsResetHandler() override
   {
      assert(!mStorage.has_value());
   }

   void OnSettingResetBegin() override
   {
      assert(!mStorage.has_value());

      static const wxString modulePrefsGroups[] = {
         "/ModulePath/",
         "/Module/",
         "/ModuleDateTime/"
      };
      KeyValueStorage storage;
      for(const auto& group : modulePrefsGroups)
      {
         if(!gPrefs->HasGroup(group))
            continue;

         const auto groupScope = gPrefs->BeginGroup(group);
         for(const auto& key : gPrefs->GetChildKeys())
         {
            wxString value;
            if(gPrefs->Read(key, &value))
               storage.emplace_back(group + key, value);
         }
      }
      mStorage = std::move(storage);
   }

   void OnSettingResetEnd() override
   {
      if(!mStorage.has_value())
         return;
      const auto Do = finally([=]{ mStorage = std::nullopt; });
      for(const auto& [key, value] : *mStorage)
         gPrefs->Write(key, value);
   }
};

static PreferencesResetHandler::Registration<ModuleSettingsResetHandler> preserveModuleSettings;

static const std::unordered_set<wxString> &autoEnabledModules()
{
   // Add names to this list, of modules that are expected to ship with Audacity
   // or widely used third-party modules to enable automatically.
   static std::unordered_set<wxString> modules{
      "mod-ogg",
      "mod-flac",
      "mod-mp2",
      "mod-wavpack",
      "mod-mp3",
      "mod-mpg123",
      "mod-pcm",
      "mod-ffmpeg",
      "mod-cl",
      "mod-lof",
      "mod-aup",
      "mod-opus",
      "mod-midi-import-export",
      "mod-cloud-audiocom",
      "mod-musehub-ui",
      "mod-openvino",
   };
   return modules;
}

// static function that tells us about a module.
int ModuleSettings::GetModuleStatus(const FilePath &fname)
{
   // Default status is NEW module, and we will ask once.
   int iStatus = kModuleNew;

   wxFileName FileName( fname );
   wxString ShortName = FileName.GetName().Lower();

   wxString PathPref = wxString( wxT("/ModulePath/") ) + ShortName;
   wxString StatusPref = wxString( wxT("/Module/") ) + ShortName;
   wxString DateTimePref = wxString( wxT("/ModuleDateTime/") ) + ShortName;

   if( gPrefs->Exists(StatusPref) )
   {
      // Update module path in case it was changed
      gPrefs->Write( PathPref, fname );

      gPrefs->Read( StatusPref, &iStatus, static_cast<int>(kModuleNew) );

      wxDateTime DateTime = FileName.GetModificationTime();
      wxDateTime OldDateTime;
      OldDateTime.ParseISOCombined( gPrefs->Read( DateTimePref, wxEmptyString ) );

      // Some platforms return milliseconds, some do not...level the playing field
      DateTime.SetMillisecond( 0 );
      OldDateTime.SetMillisecond( 0 );

      // fix up a bad status or reset for newer module
      if( iStatus > kModuleNew || !OldDateTime.IsEqualTo( DateTime ) )
      {
         iStatus=kModuleNew;
      }
   }
   else
   {
      // Remove previously saved since it's no longer valid
      gPrefs->DeleteEntry( PathPref );
      gPrefs->DeleteEntry( StatusPref );
      gPrefs->DeleteEntry( DateTimePref );
   }

   if (iStatus == kModuleNew) {
      if (autoEnabledModules().count(ShortName))
         iStatus = kModuleEnabled;
   }

   return iStatus;
}

void ModuleSettings::SetModuleStatus(const FilePath &fname, int iStatus)
{
   wxFileName FileName( fname );
   wxDateTime DateTime = FileName.GetModificationTime();
   wxString ShortName = FileName.GetName().Lower();

   wxString PrefName = wxString( wxT("/Module/") ) + ShortName;
   gPrefs->Write( PrefName, iStatus );

   PrefName = wxString( wxT("/ModulePath/") ) + ShortName;
   gPrefs->Write( PrefName, fname );

   PrefName = wxString( wxT("/ModuleDateTime/") ) + ShortName;
   gPrefs->Write( PrefName, DateTime.FormatISOCombined() );

   gPrefs->Flush();
}