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 <mutex>
26
27FileHistory::FileHistory(size_t maxfiles, wxWindowID base)
28{
29 mMaxFiles = maxfiles;
30 mIDBase = base;
31}
32
34{
35}
36
38{
39 // TODO - read the number of files to store in history from preferences
40 static FileHistory history{
42 static std::once_flag flag;
43 std::call_once( flag, [&]{
44 history.Load(*gPrefs, wxT("RecentFiles"));
45 });
46
47 return history;
48}
49
50// File history management
51void FileHistory::AddFileToHistory(const FilePath & file, bool update)
52{
53 // Needed to transition from wxFileHistory to FileHistory since there
54 // can be empty history "slots".
55 if (file.empty()) {
56 return;
57 }
58
59#if defined(__WXMSW__)
60 int i = mHistory.Index(file, false);
61#else
62 int i = mHistory.Index(file, true);
63#endif
64
65 if (i != wxNOT_FOUND) {
66 mHistory.erase( mHistory.begin() + i );
67 }
68
69 if (mMaxFiles > 0 && mMaxFiles == mHistory.size()) {
70 mHistory.erase( mHistory.end() - 1 );
71 }
72
73 mHistory.insert(mHistory.begin(), file);
74
75 if (update)
77}
78
79void FileHistory::Remove( size_t i )
80{
81 wxASSERT(i < mHistory.size());
82
83 if (i < mHistory.size()) {
84 mHistory.erase( mHistory.begin() + i );
85
87 }
88}
89
91{
92 mHistory.clear();
93
95}
96
97void FileHistory::UseMenu(wxMenu *menu)
98{
99 Compress();
100
101 auto end = mMenus.end();
102 auto iter = std::find(mMenus.begin(), end, menu);
103 auto found = (iter != end);
104
105 if (!found)
106 mMenus.push_back(menu);
107 else {
108 wxASSERT(false);
109 }
110
111 NotifyMenu( menu );
112}
113
114void FileHistory::Load(wxConfigBase & config, const wxString & group)
115{
116 mHistory.clear();
117 mGroup = group.empty()
118 ? wxString{ "RecentFiles" }
119 : group;
120
121 config.SetPath(mGroup);
122
123 wxString file;
124 long ndx;
125 bool got = config.GetFirstEntry(file, ndx);
126 while (got) {
127 AddFileToHistory(config.Read(file), false);
128 got = config.GetNextEntry(file, ndx);
129 }
130
131 config.SetPath(wxT(".."));
132
133 NotifyMenus();
134}
135
136void FileHistory::Save(wxConfigBase & config)
137{
138 config.SetPath(wxT(""));
139 config.DeleteGroup(mGroup);
140 config.SetPath(mGroup);
141
142 // Stored in reverse order
143 int n = mHistory.size() - 1;
144 for (size_t i = 1; i <= mHistory.size(); i++) {
145 config.Write(wxString::Format(wxT("file%02d"), (int)i), mHistory[n--]);
146 }
147
148 config.SetPath(wxT(""));
149
150 config.Flush();
151}
152
154{
155 Compress();
156 for (auto pMenu : mMenus)
157 if (pMenu)
158 NotifyMenu(pMenu);
159 Save(*gPrefs);
160}
161
162void FileHistory::NotifyMenu(wxMenu *menu)
163{
164 wxMenuItemList items = menu->GetMenuItems();
165 for (auto end = items.end(), iter = items.begin(); iter != end;)
166 menu->Destroy(*iter++);
167
168 for (size_t i = 0; i < mHistory.size(); i++) {
169 wxString item = mHistory[i];
170 item.Replace( "&", "&&" );
171 menu->Append(mIDBase + 1 + i,item);
172 }
173
174 if (mHistory.size() > 0) {
175 menu->AppendSeparator();
176 }
177 menu->Append(mIDBase, _("&Clear"));
178 menu->Enable(mIDBase, mHistory.size() > 0);
179}
180
182{
183 // Clear up expired weak pointers
184 auto end = mMenus.end();
185 mMenus.erase(
186 std::remove_if( mMenus.begin(), end,
187 [](wxWeakRef<wxMenu> &pMenu){ return !pMenu; } ),
188 end
189 );
190}
191
#define _(s)
Definition: Internat.h:75
FileConfig * gPrefs
Definition: Prefs.cpp:71
wxString FilePath
Definition: Project.h:20
static std::once_flag flag
Similar to wxFileHistory, but customized to our needs.
Definition: FileHistory.h:26
void NotifyMenu(wxMenu *menu)
wxString mGroup
Definition: FileHistory.h:74
FilePaths mHistory
Definition: FileHistory.h:72
virtual ~FileHistory()
Definition: FileHistory.cpp:33
void Load(wxConfigBase &config, const wxString &group=wxEmptyString)
void Compress()
FileHistory(size_t maxfiles=12, wxWindowID idbase=wxID_FILE)
Definition: FileHistory.cpp:27
void Clear()
Definition: FileHistory.cpp:90
void AddFileToHistory(const FilePath &file, bool update)
Definition: FileHistory.cpp:51
wxWindowID mIDBase
Definition: FileHistory.h:69
void Remove(size_t i)
Definition: FileHistory.cpp:79
void NotifyMenus()
void UseMenu(wxMenu *menu)
Definition: FileHistory.cpp:97
void Save(wxConfigBase &config)
size_t mMaxFiles
Definition: FileHistory.h:68
const_iterator end() const
Definition: FileHistory.h:57
std::vector< wxWeakRef< wxMenu > > mMenus
Definition: FileHistory.h:71
static FileHistory & Global()
Definition: FileHistory.cpp:37
iterator insert(const_iterator pos, std::initializer_list< T > items)