Audacity 3.2.0
FileHistory.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 FileHistory.cpp
6
7 Leland Lucius
8
9*******************************************************************//*******************************************************************/
15
16
17#include "FileHistory.h"
18
19#include <wx/defs.h>
20#include <wx/menu.h>
21
22#include "Internat.h"
23#include "Prefs.h"
24
25#include "BasicSettings.h"
26
27#include <mutex>
28
29FileHistory::FileHistory(size_t maxfiles, wxWindowID base)
30{
31 mMaxFiles = maxfiles;
32 mIDBase = base;
33}
34
36{
37}
38
40{
41 // TODO - read the number of files to store in history from preferences
42 static FileHistory history{
44 static std::once_flag flag;
45 std::call_once( flag, [&]{
46 history.Load(*gPrefs, wxT("RecentFiles"));
47 });
48
49 return history;
50}
51
52// File history management
53void FileHistory::AddFileToHistory(const FilePath & file, bool update)
54{
55 // Needed to transition from wxFileHistory to FileHistory since there
56 // can be empty history "slots".
57 if (file.empty()) {
58 return;
59 }
60
61#if defined(__WXMSW__)
62 int i = mHistory.Index(file, false);
63#else
64 int i = mHistory.Index(file, true);
65#endif
66
67 if (i != wxNOT_FOUND) {
68 mHistory.erase( mHistory.begin() + i );
69 }
70
71 if (mMaxFiles > 0 && mMaxFiles == mHistory.size()) {
72 mHistory.erase( mHistory.end() - 1 );
73 }
74
75 mHistory.insert(mHistory.begin(), file);
76
77 if (update)
79}
80
81void FileHistory::Remove( size_t i )
82{
83 wxASSERT(i < mHistory.size());
84
85 if (i < mHistory.size()) {
86 mHistory.erase( mHistory.begin() + i );
87
89 }
90}
91
93{
94 mHistory.clear();
95
97}
98
99void FileHistory::UseMenu(wxMenu *menu)
100{
101 Compress();
102
103 auto end = mMenus.end();
104 auto iter = std::find(mMenus.begin(), end, menu);
105 auto found = (iter != end);
106
107 if (!found)
108 mMenus.push_back(menu);
109 else {
110 wxASSERT(false);
111 }
112
113 NotifyMenu( menu );
114}
115
117{
118 mHistory.clear();
119 mGroup = group.empty()
120 ? wxString{ "RecentFiles" }
121 : group;
122
123 const auto localGroup = settings.BeginGroup(mGroup);
124 for(const auto& key : settings.GetChildKeys())
125 AddFileToHistory(settings.Read(key), false);
126
127 NotifyMenus();
128}
129
131{
132 auto group = settings.BeginGroup(mGroup);
133 settings.Remove({});
134
135 // Stored in reverse order
136 int n = mHistory.size() - 1;
137 for (size_t i = 1; i <= mHistory.size(); i++) {
138 settings.Write(wxString::Format(wxT("file%02d"), (int)i), mHistory[n--]);
139 }
140 settings.Flush();
141}
142
144{
145 Compress();
146 for (auto pMenu : mMenus)
147 if (pMenu)
148 NotifyMenu(pMenu);
149 Save(*gPrefs);
150}
151
152void FileHistory::NotifyMenu(wxMenu *menu)
153{
154 wxMenuItemList items = menu->GetMenuItems();
155 for (auto end = items.end(), iter = items.begin(); iter != end;)
156 menu->Destroy(*iter++);
157
158 for (size_t i = 0; i < mHistory.size(); i++) {
159 wxString item = mHistory[i];
160 item.Replace( "&", "&&" );
161 menu->Append(mIDBase + 1 + i,item);
162 }
163
164 if (mHistory.size() > 0) {
165 menu->AppendSeparator();
166 }
167 menu->Append(mIDBase, _("&Clear"));
168 menu->Enable(mIDBase, mHistory.size() > 0);
169}
170
172{
173 // Clear up expired weak pointers
174 auto end = mMenus.end();
175 mMenus.erase(
176 std::remove_if( mMenus.begin(), end,
177 [](wxWeakRef<wxMenu> &pMenu){ return !pMenu; } ),
178 end
179 );
180}
181
wxT("CloseDown"))
#define _(s)
Definition: Internat.h:73
static const AudacityProject::AttachedObjects::RegisteredFactory key
audacity::BasicSettings * gPrefs
Definition: Prefs.cpp:68
wxString FilePath
Definition: Project.h:21
static Settings & settings()
Definition: TrackInfo.cpp:51
static std::once_flag flag
Similar to wxFileHistory, but customized to our needs.
Definition: FileHistory.h:30
void NotifyMenu(wxMenu *menu)
wxString mGroup
Definition: FileHistory.h:78
FilePaths mHistory
Definition: FileHistory.h:76
virtual ~FileHistory()
Definition: FileHistory.cpp:35
void Compress()
void Load(audacity::BasicSettings &config, const wxString &group=wxEmptyString)
FileHistory(size_t maxfiles=12, wxWindowID idbase=wxID_FILE)
Definition: FileHistory.cpp:29
void Clear()
Definition: FileHistory.cpp:92
void AddFileToHistory(const FilePath &file, bool update)
Definition: FileHistory.cpp:53
wxWindowID mIDBase
Definition: FileHistory.h:73
void Remove(size_t i)
Definition: FileHistory.cpp:81
void NotifyMenus()
void UseMenu(wxMenu *menu)
Definition: FileHistory.cpp:99
size_t mMaxFiles
Definition: FileHistory.h:72
const_iterator end() const
Definition: FileHistory.h:61
void Save(audacity::BasicSettings &config)
std::vector< wxWeakRef< wxMenu > > mMenus
Definition: FileHistory.h:75
static FileHistory & Global()
Definition: FileHistory.cpp:39
Base class for objects that provide facility to store data persistently, and access it with string ke...
Definition: BasicSettings.h:31
iterator insert(const_iterator pos, std::initializer_list< T > items)