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 
52 /* i18n-hint: Directories, also called folders, in computer file systems */
53 : PrefsPanel(parent, _("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(3, 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  S.Id(ChooseButtonID);
96  S.AddButton(_("C&hoose..."));
97 
98  S.AddFixedText(_("Free Space:"));
99  mFreeSpace = S.AddVariableText( {} );
100  }
101  S.EndMultiColumn();
102  }
103  S.EndStatic();
104 
105 #ifdef DEPRECATED_AUDIO_CACHE
106  // See http://bugzilla.audacityteam.org/show_bug.cgi?id=545.
107  S.StartStatic(_("Audio cache"));
108  {
109  S.TieCheckBox(_("Play and/or record using &RAM (useful for slow drives)"),
110  wxT("/Directories/CacheBlockFiles"),
111  false);
112 
113  S.StartTwoColumn();
114  {
115  S.TieNumericTextBox(_("Mi&nimum Free Memory (MB):"),
116  wxT("/Directories/CacheLowMem"),
117  16,
118  9);
119  }
120  S.EndTwoColumn();
121 
122  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);
123  }
124  S.EndStatic();
125 #endif // DEPRECATED_AUDIO_CACHE
126  S.EndScroller();
127 
128 }
129 
130 void DirectoriesPrefs::OnChooseTempDir(wxCommandEvent & e)
131 {
132  wxString oldTempDir = gPrefs->Read(wxT("/Directories/TempDir"), wxGetApp().defaultTempDir);
133 
134  // Because we went through InitTempDir() during initialisation,
135  // the old temp directory name in prefs should already be OK. Just in case there is
136  // some way we hadn't thought of for it to be not OK,
137  // we avoid prompting with it in that case and use the suggested default instead.
138  if( !AudacityApp::IsTempDirectoryNameOK( oldTempDir ) )
139  oldTempDir = wxGetApp().defaultTempDir;
140 
141  wxDirDialogWrapper dlog(this,
142  _("Choose a location to place the temporary directory"),
143  oldTempDir );
144  int retval = dlog.ShowModal();
145  if (retval != wxID_CANCEL && dlog.GetPath() != wxT("")) {
146  wxFileName tmpDirPath;
147  tmpDirPath.AssignDir(dlog.GetPath());
148 
149  // Append an "audacity_temp" directory to this path if necessary (the
150  // default, the existing pref (as stored in the control), and any path
151  // ending in a directory with the same name as what we'd add should be OK
152  // already)
153  wxString newDirName;
154 #if defined(__WXMAC__)
155  newDirName = wxT("SessionData");
156 #elif defined(__WXMSW__)
157  // Clearing Bug 1271 residual issue. Let's NOT have temp in the name.
158  newDirName = wxT("SessionData");
159 #else
160  newDirName = wxT(".audacity_temp");
161 #endif
162  wxArrayString dirsInPath = tmpDirPath.GetDirs();
163 
164  // If the default temp dir or user's pref dir don't end in '/' they cause
165  // wxFileName's == operator to construct a wxFileName representing a file
166  // (that doesn't exist) -- hence the constructor calls
167  if (tmpDirPath != wxFileName(wxGetApp().defaultTempDir, wxT("")) &&
168  tmpDirPath != wxFileName(mTempDir->GetValue(), wxT("")) &&
169  (dirsInPath.GetCount() == 0 ||
170  dirsInPath[dirsInPath.GetCount()-1] != newDirName))
171  {
172  tmpDirPath.AppendDir(newDirName);
173  }
174 
175  mTempDir->SetValue(tmpDirPath.GetPath(wxPATH_GET_VOLUME|wxPATH_GET_SEPARATOR));
176  UpdateFreeSpace(e);
177  }
178 }
179 
180 void DirectoriesPrefs::UpdateFreeSpace(wxCommandEvent & WXUNUSED(event))
181 {
182  wxString tempDir;
183  wxString label;
184 
185  if (mTempDir != NULL) {
186  tempDir = mTempDir->GetValue();
187  }
188 
189  if (wxDirExists(tempDir)) {
190  wxLongLong space;
191  wxGetDiskSpace(tempDir, NULL, &space);
192  label = Internat::FormatSize(space);
193  }
194  else {
195  label = _("unavailable - above location doesn't exist");
196  }
197 
198  if( mFreeSpace != NULL ) {
199  mFreeSpace->SetLabel(label);
200  mFreeSpace->SetName(label); // fix for bug 577 (NVDA/Narrator screen readers do not read static text in dialogs)
201  }
202 }
203 
205 {
206  wxFileName tempDir;
207  tempDir.SetPath(mTempDir->GetValue());
208 
209  wxString path{tempDir.GetPath()};
210  if( !AudacityApp::IsTempDirectoryNameOK( path ) ) {
212  wxString::Format(_("Directory %s is not suitable (at risk of being cleaned out)"),
213  path),
214  _("Error"),
215  wxOK | wxICON_ERROR);
216  return false;
217  }
218  if (!tempDir.DirExists()) {
219  int ans = AudacityMessageBox(
220  wxString::Format(_("Directory %s does not exist. Create it?"),
221  path),
222  _("New Temporary Directory"),
223  wxYES_NO | wxCENTRE | wxICON_EXCLAMATION);
224 
225  if (ans != wxYES) {
226  return false;
227  }
228 
229  if (!tempDir.Mkdir(0755, wxPATH_MKDIR_FULL)) {
230  /* wxWidgets throws up a decent looking dialog */
231  return false;
232  }
233  }
234  else {
235  /* If the directory already exists, make sure it is writable */
236  wxLogNull logNo;
237  tempDir.AppendDir(wxT("canicreate"));
238  path = tempDir.GetPath();
239  if (!tempDir.Mkdir(0755)) {
241  wxString::Format(_("Directory %s is not writable"),
242  path),
243  _("Error"),
244  wxOK | wxICON_ERROR);
245  return false;
246  }
247  tempDir.Rmdir();
248  tempDir.RemoveLastDir();
249  }
250 
251  wxFileName oldDir;
252  oldDir.SetPath(gPrefs->Read(wxT("/Directories/TempDir")));
253  if (tempDir != oldDir) {
255  _("Changes to temporary directory will not take effect until Audacity is restarted"),
256  _("Temp Directory Update"),
257  wxOK | wxCENTRE | wxICON_INFORMATION);
258  }
259 
260  return true;
261 }
262 
264 {
265  ShuttleGui S(this, eIsSavingToPrefs);
267 
268  return true;
269 }
270 
272 {
273  return "Directories_Preferences";
274 }
275 
277 {
278  wxASSERT(parent); // to justify safenew
279  return safenew DirectoriesPrefs(parent);
280 }
static wxString FormatSize(wxLongLong size)
Convert a number to a string while formatting it in bytes, KB, MB, GB.
Definition: Internat.cpp:190
Derived from ShuttleGuiBase, an Audacity specific class for shuttling data to and from GUI...
Definition: ShuttleGui.h:366
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 PopulateOrExchange(ShuttleGui &S)
void EndScroller()
Definition: ShuttleGui.cpp:770
PrefsPanel * Create(wxWindow *parent) override
wxString HelpPageName() override
#define safenew
Definition: Audacity.h:223
wxStaticText * mFreeSpace
static bool IsTempDirectoryNameOK(const wxString &Name)
wxFileConfig * gPrefs
Definition: Prefs.cpp:72
wxScrolledWindow * StartScroller(int iStyle=0)
Definition: ShuttleGui.cpp:733
void StartTwoColumn()
Definition: ShuttleGui.h:125
void Populate()
Creates the dialog and its contents.
void StartMultiColumn(int nCols, int PositionFlags=wxALIGN_LEFT)
Definition: ShuttleGui.cpp:998
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:356
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:43
_("Move Track &Down")+wxT("\t")+(GetActiveProject() -> GetCommandManager() ->GetKeyFromName(wxT("TrackMoveDown"))), OnMoveTrack) POPUP_MENU_ITEM(OnMoveTopID, _("Move Track to &Top")+wxT("\t")+(GetActiveProject() ->GetCommandManager() ->GetKeyFromName(wxT("TrackMoveTop"))), OnMoveTrack) POPUP_MENU_ITEM(OnMoveBottomID, _("Move Track to &Bottom")+wxT("\t")+(GetActiveProject() ->GetCommandManager() ->GetKeyFromName(wxT("TrackMoveBottom"))), OnMoveTrack) void TrackMenuTable::OnSetName(wxCommandEvent &)
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:373
wxStaticBox * StartStatic(const wxString &Str, int iProp=0)
Definition: ShuttleGui.cpp:701
AudacityApp & wxGetApp()
END_EVENT_TABLE()
wxString defaultTempDir
Default temp directory.
Definition: AudacityApp.h:141
void SetBorder(int Border)
Definition: ShuttleGui.h:251
void EndTwoColumn()
Definition: ShuttleGui.h:126
bool Commit() override
wxButton * AddButton(const wxString &Text, int PositionFlags=wxALIGN_CENTRE)
Definition: ShuttleGui.cpp:301
void SetStretchyCol(int i)
Used to modify an already placed FlexGridSizer to make a column stretchy.
Definition: ShuttleGui.cpp:192
wxTextCtrl * TieTextBox(const wxString &Prompt, WrappedType &WrappedRef, const int nChars)