Audacity  3.0.3
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 
27 FileHistory::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
51 void 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)
76  NotifyMenus();
77 }
78 
79 void FileHistory::Remove( size_t i )
80 {
81  wxASSERT(i < mHistory.size());
82 
83  if (i < mHistory.size()) {
84  mHistory.erase( mHistory.begin() + i );
85 
86  NotifyMenus();
87  }
88 }
89 
91 {
92  mHistory.clear();
93 
94  NotifyMenus();
95 }
96 
97 void 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 
114 void 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 
136 void 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 
162 void 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 
FileHistory::ID_RECENT_FIRST
@ ID_RECENT_FIRST
Definition: FileHistory.h:36
flag
static std::once_flag flag
Definition: WaveformView.cpp:1119
gPrefs
FileConfig * gPrefs
Definition: Prefs.cpp:70
FileHistory::Clear
void Clear()
Definition: FileHistory.cpp:90
FileHistory::FileHistory
FileHistory(size_t maxfiles=12, wxWindowID idbase=wxID_FILE)
Definition: FileHistory.cpp:27
FileHistory::~FileHistory
virtual ~FileHistory()
Definition: FileHistory.cpp:33
FileHistory
Similar to wxFileHistory, but customized to our needs.
Definition: FileHistory.h:26
FileHistory::ID_RECENT_CLEAR
@ ID_RECENT_CLEAR
Definition: FileHistory.h:35
FileHistory::NotifyMenus
void NotifyMenus()
Definition: FileHistory.cpp:153
FileHistory::Global
static FileHistory & Global()
Definition: FileHistory.cpp:37
FilePath
wxString FilePath
Definition: Project.h:20
FileHistory::Compress
void Compress()
Definition: FileHistory.cpp:181
wxArrayStringEx::insert
iterator insert(const_iterator pos, std::initializer_list< T > items)
Definition: wxArrayStringEx.h:58
Internat.h
FileHistory::mMenus
std::vector< wxWeakRef< wxMenu > > mMenus
Definition: FileHistory.h:71
FileHistory.h
FileHistory::end
const_iterator end() const
Definition: FileHistory.h:57
_
#define _(s)
Definition: Internat.h:75
FileHistory::UseMenu
void UseMenu(wxMenu *menu)
Definition: FileHistory.cpp:97
FileHistory::mIDBase
wxWindowID mIDBase
Definition: FileHistory.h:69
FileHistory::mGroup
wxString mGroup
Definition: FileHistory.h:74
FileHistory::Remove
void Remove(size_t i)
Definition: FileHistory.cpp:79
Prefs.h
FileHistory::Load
void Load(wxConfigBase &config, const wxString &group=wxEmptyString)
Definition: FileHistory.cpp:114
FileHistory::mHistory
FilePaths mHistory
Definition: FileHistory.h:72
FileHistory::ID_RECENT_LAST
@ ID_RECENT_LAST
Definition: FileHistory.h:37
FileHistory::Save
void Save(wxConfigBase &config)
Definition: FileHistory.cpp:136
FileHistory::AddFileToHistory
void AddFileToHistory(const FilePath &file, bool update)
Definition: FileHistory.cpp:51
FileHistory::mMaxFiles
size_t mMaxFiles
Definition: FileHistory.h:68
FileHistory::NotifyMenu
void NotifyMenu(wxMenu *menu)
Definition: FileHistory.cpp:162