Audacity  2.2.2
DirectoriesPrefs.cpp
Go to the documentation of this file.
1 /**********************************************************************
2 
3  Audacity: A Digital Audio Editor
4 
5  DirectoriesPrefs.cpp
6 
7  Joshua Haberman
8  James Crook
9 
10 
11 *******************************************************************//*******************************************************************/
17 
18 #include "../Audacity.h"
19 
20 #include <math.h>
21 
22 #include <wx/defs.h>
23 #include <wx/intl.h>
24 #include <wx/log.h>
25 #include <wx/stattext.h>
26 #include <wx/textctrl.h>
27 #include <wx/button.h>
28 #include <wx/dirdlg.h>
29 #include <wx/event.h>
30 #include <wx/filefn.h>
31 #include <wx/filename.h>
32 #include <wx/utils.h>
33 
34 #include "../Prefs.h"
35 #include "../AudacityApp.h"
36 #include "../Internat.h"
37 #include "../ShuttleGui.h"
38 #include "../widgets/ErrorDialog.h"
39 #include "DirectoriesPrefs.h"
40 
41 enum {
42  TempDirID = 1000,
44 };
45 
46 BEGIN_EVENT_TABLE(DirectoriesPrefs, PrefsPanel)
47  EVT_TEXT(TempDirID, DirectoriesPrefs::UpdateFreeSpace)
48  EVT_BUTTON(ChooseButtonID, DirectoriesPrefs::OnChooseTempDir)
50 
51 DirectoriesPrefs::DirectoriesPrefs(wxWindow * parent, wxWindowID winid)
52 /* i18n-hint: Directories, also called folders, in computer file systems */
53 : PrefsPanel(parent, winid, _("Directories")),
54  mFreeSpace(NULL),
55  mTempDir(NULL)
56 {
57  Populate();
58 }
59 
61 {
62 }
63 
66 {
67  //------------------------- Main section --------------------
68  // Now construct the GUI itself.
69  // Use 'eIsCreatingFromPrefs' so that the GUI is
70  // initialised with values from gPrefs.
73  // ----------------------- End of main section --------------
74 
75  wxCommandEvent e;
76  UpdateFreeSpace(e);
77 }
78 
80 {
81  S.SetBorder(2);
82  S.StartScroller();
83 
84  S.StartStatic(_("Temporary files directory"));
85  {
86  S.StartMultiColumn(2, wxEXPAND);
87  {
88  S.SetStretchyCol(1);
89 
90  S.Id(TempDirID);
91  mTempDir = S.TieTextBox(_("&Location:"),
92  wxT("/Directories/TempDir"),
93  wxT(""),
94  30);
95  }
96  S.EndMultiColumn();
97  S.StartHorizontalLay(wxEXPAND);
98  {
99  S.Prop(0).AddFixedText(_("Free Space:"));
100  mFreeSpace = S.Prop(0).AddVariableText( {} );
101  S.Prop(10).AddSpace( 10 );
102  S.Id(ChooseButtonID).Prop(0).AddButton(_("C&hoose..."));
103  }
104 
105  }
106  S.EndStatic();
107 
108 #ifdef DEPRECATED_AUDIO_CACHE
109  // See http://bugzilla.audacityteam.org/show_bug.cgi?id=545.
110  S.StartStatic(_("Audio cache"));
111  {
112  S.TieCheckBox(_("Play and/or record using &RAM (useful for slow drives)"),
113  wxT("/Directories/CacheBlockFiles"),
114  false);
115 
116  S.StartTwoColumn();
117  {
118  S.TieNumericTextBox(_("Mi&nimum Free Memory (MB):"),
119  wxT("/Directories/CacheLowMem"),
120  16,
121  9);
122  }
123  S.EndTwoColumn();
124 
125  S.AddVariableText(_("If the available system memory falls below this value, audio will no longer\nbe cached in memory and will be written to disk."))->Wrap(600);
126  }
127  S.EndStatic();
128 #endif // DEPRECATED_AUDIO_CACHE
129  S.EndScroller();
130 
131 }
132 
133 void DirectoriesPrefs::OnChooseTempDir(wxCommandEvent & e)
134 {
135  wxString oldTempDir = gPrefs->Read(wxT("/Directories/TempDir"), wxGetApp().defaultTempDir);
136 
137  // Because we went through InitTempDir() during initialisation,
138  // the old temp directory name in prefs should already be OK. Just in case there is
139  // some way we hadn't thought of for it to be not OK,
140  // we avoid prompting with it in that case and use the suggested default instead.
141  if( !AudacityApp::IsTempDirectoryNameOK( oldTempDir ) )
142  oldTempDir = wxGetApp().defaultTempDir;
143 
144  wxDirDialogWrapper dlog(this,
145  _("Choose a location to place the temporary directory"),
146  oldTempDir );
147  int retval = dlog.ShowModal();
148  if (retval != wxID_CANCEL && dlog.GetPath() != wxT("")) {
149  wxFileName tmpDirPath;
150  tmpDirPath.AssignDir(dlog.GetPath());
151 
152  // Append an "audacity_temp" directory to this path if necessary (the
153  // default, the existing pref (as stored in the control), and any path
154  // ending in a directory with the same name as what we'd add should be OK
155  // already)
156  wxString newDirName;
157 #if defined(__WXMAC__)
158  newDirName = wxT("SessionData");
159 #elif defined(__WXMSW__)
160  // Clearing Bug 1271 residual issue. Let's NOT have temp in the name.
161  newDirName = wxT("SessionData");
162 #else
163  newDirName = wxT(".audacity_temp");
164 #endif
165  wxArrayString dirsInPath = tmpDirPath.GetDirs();
166 
167  // If the default temp dir or user's pref dir don't end in '/' they cause
168  // wxFileName's == operator to construct a wxFileName representing a file
169  // (that doesn't exist) -- hence the constructor calls
170  if (tmpDirPath != wxFileName(wxGetApp().defaultTempDir, wxT("")) &&
171  tmpDirPath != wxFileName(mTempDir->GetValue(), wxT("")) &&
172  (dirsInPath.GetCount() == 0 ||
173  dirsInPath[dirsInPath.GetCount()-1] != newDirName))
174  {
175  tmpDirPath.AppendDir(newDirName);
176  }
177 
178  mTempDir->SetValue(tmpDirPath.GetPath(wxPATH_GET_VOLUME|wxPATH_GET_SEPARATOR));
179  UpdateFreeSpace(e);
180  }
181 }
182 
183 void DirectoriesPrefs::UpdateFreeSpace(wxCommandEvent & WXUNUSED(event))
184 {
185  wxString tempDir;
186  wxString label;
187 
188  if (mTempDir != NULL) {
189  tempDir = mTempDir->GetValue();
190  }
191 
192  if (wxDirExists(tempDir)) {
193  wxLongLong space;
194  wxGetDiskSpace(tempDir, NULL, &space);
195  label = Internat::FormatSize(space);
196  }
197  else {
198  label = _("unavailable - above location doesn't exist");
199  }
200 
201  if( mFreeSpace != NULL ) {
202  mFreeSpace->SetLabel(label);
203  mFreeSpace->SetName(label); // fix for bug 577 (NVDA/Narrator screen readers do not read static text in dialogs)
204  }
205 }
206 
208 {
209  wxFileName tempDir;
210  tempDir.SetPath(mTempDir->GetValue());
211 
212  wxString path{tempDir.GetPath()};
213  if( !AudacityApp::IsTempDirectoryNameOK( path ) ) {
215  wxString::Format(_("Directory %s is not suitable (at risk of being cleaned out)"),
216  path),
217  _("Error"),
218  wxOK | wxICON_ERROR);
219  return false;
220  }
221  if (!tempDir.DirExists()) {
222  int ans = AudacityMessageBox(
223  wxString::Format(_("Directory %s does not exist. Create it?"),
224  path),
225  _("New Temporary Directory"),
226  wxYES_NO | wxCENTRE | wxICON_EXCLAMATION);
227 
228  if (ans != wxYES) {
229  return false;
230  }
231 
232  if (!tempDir.Mkdir(0755, wxPATH_MKDIR_FULL)) {
233  /* wxWidgets throws up a decent looking dialog */
234  return false;
235  }
236  }
237  else {
238  /* If the directory already exists, make sure it is writable */
239  wxLogNull logNo;
240  tempDir.AppendDir(wxT("canicreate"));
241  path = tempDir.GetPath();
242  if (!tempDir.Mkdir(0755)) {
244  wxString::Format(_("Directory %s is not writable"),
245  path),
246  _("Error"),
247  wxOK | wxICON_ERROR);
248  return false;
249  }
250  tempDir.Rmdir();
251  tempDir.RemoveLastDir();
252  }
253 
254  wxFileName oldDir;
255  oldDir.SetPath(gPrefs->Read(wxT("/Directories/TempDir")));
256  if (tempDir != oldDir) {
258  _("Changes to temporary directory will not take effect until Audacity is restarted"),
259  _("Temp Directory Update"),
260  wxOK | wxCENTRE | wxICON_INFORMATION);
261  }
262 
263  return true;
264 }
265 
267 {
268  ShuttleGui S(this, eIsSavingToPrefs);
270 
271  return true;
272 }
273 
275 {
276  return "Directories_Preferences";
277 }
278 
279 PrefsPanel *DirectoriesPrefsFactory::operator () (wxWindow *parent, wxWindowID winid)
280 {
281  wxASSERT(parent); // to justify safenew
282  return safenew DirectoriesPrefs(parent, winid);
283 }
static wxString FormatSize(wxLongLong size)
Convert a number to a string while formatting it in bytes, KB, MB, GB.
Definition: Internat.cpp:191
AudacityPrefs * gPrefs
Definition: Prefs.cpp:73
Derived from ShuttleGuiBase, an Audacity specific class for shuttling data to and from GUI...
Definition: ShuttleGui.h:409
void PopulateOrExchange(ShuttleGui &S) override
void EndMultiColumn()
wxString label
Definition: Tags.cpp:727
bool Validate() override
A PrefsPanel used to select directories.
int AudacityMessageBox(const wxString &message, const wxString &caption=AudacityMessageBoxCaptionStr(), long style=wxOK|wxCENTRE, wxWindow *parent=NULL, int x=wxDefaultCoord, int y=wxDefaultCoord)
Definition: ErrorDialog.h:92
void EndScroller()
Definition: ShuttleGui.cpp:828
wxString HelpPageName() override
#define safenew
Definition: Audacity.h:230
wxStaticText * mFreeSpace
static bool IsTempDirectoryNameOK(const wxString &Name)
wxScrolledWindow * StartScroller(int iStyle=0)
Definition: ShuttleGui.cpp:795
void StartTwoColumn()
Definition: ShuttleGui.h:136
void Populate()
Creates the dialog and its contents.
void StartHorizontalLay(int PositionFlags=wxALIGN_CENTRE, int iProp=1)
void StartMultiColumn(int nCols, int PositionFlags=wxALIGN_LEFT)
ShuttleGui & Id(int id)
wxTextCtrl * TieNumericTextBox(const wxString &Prompt, WrappedType &WrappedRef, const int nChars)
void AddFixedText(const wxString &Str, bool bCenter=false)
Definition: ShuttleGui.cpp:397
void UpdateFreeSpace(wxCommandEvent &e)
EVT_BUTTON(wxID_NO, DependencyDialog::OnNo) EVT_BUTTON(wxID_YES
wxTextCtrl * mTempDir
Used within the PrefsDialog, classes derived from this class include AudioIOPrefs, BatchPrefs, DirectoriesPrefs, FileFormatPrefs, GUIPrefs, KeyConfigPrefs, MousePrefs, QualityPrefs, SpectrumPrefs and ThemePrefs.
Definition: PrefsPanel.h:45
_("Move Track &Down")+wxT("\t")+(GetActiveProject() -> GetCommandManager() ->GetKeyFromName(wxT("TrackMoveDown")).Raw()), OnMoveTrack) POPUP_MENU_ITEM(OnMoveTopID, _("Move Track to &Top")+wxT("\t")+(GetActiveProject() ->GetCommandManager() ->GetKeyFromName(wxT("TrackMoveTop")).Raw()), OnMoveTrack) POPUP_MENU_ITEM(OnMoveBottomID, _("Move Track to &Bottom")+wxT("\t")+(GetActiveProject() ->GetCommandManager() ->GetKeyFromName(wxT("TrackMoveBottom")).Raw()), OnMoveTrack)#define SET_TRACK_NAME_PLUGIN_SYMBOLclass SetTrackNameCommand:public AudacityCommand
void OnChooseTempDir(wxCommandEvent &e)
wxCheckBox * TieCheckBox(const wxString &Prompt, WrappedType &WrappedRef)
wxStaticText * AddVariableText(const wxString &Str, bool bCenter=false, int PositionFlags=0)
Definition: ShuttleGui.cpp:414
wxStaticBox * StartStatic(const wxString &Str, int iProp=0)
Definition: ShuttleGui.cpp:763
ShuttleGui & Prop(int iProp)
Definition: ShuttleGui.h:418
AudacityApp & wxGetApp()
END_EVENT_TABLE()
wxSizerItem * AddSpace(int width, int height)
wxString defaultTempDir
Default temp directory.
Definition: AudacityApp.h:141
void SetBorder(int Border)
Definition: ShuttleGui.h:286
void EndTwoColumn()
Definition: ShuttleGui.h:137
bool Commit() override
wxButton * AddButton(const wxString &Text, int PositionFlags=wxALIGN_CENTRE)
Definition: ShuttleGui.cpp:341
void SetStretchyCol(int i)
Used to modify an already placed FlexGridSizer to make a column stretchy.
Definition: ShuttleGui.cpp:203
wxTextCtrl * TieTextBox(const wxString &Prompt, WrappedType &WrappedRef, const int nChars)
PrefsPanel * operator()(wxWindow *parent, wxWindowID winid) override