forked from audacity/audacity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirectoriesPrefs.cpp
More file actions
464 lines (382 loc) · 12.1 KB
/
Copy pathDirectoriesPrefs.cpp
File metadata and controls
464 lines (382 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/**********************************************************************
Audacity: A Digital Audio Editor
DirectoriesPrefs.cpp
Joshua Haberman
James Crook
*******************************************************************//**
\class DirectoriesPrefs
\brief A PrefsPanel used to select directories.
*//*******************************************************************/
#include "../Audacity.h"
#include "DirectoriesPrefs.h"
#include <math.h>
#include <wx/defs.h>
#include <wx/intl.h>
#include <wx/log.h>
#include <wx/stattext.h>
#include <wx/textctrl.h>
#include <wx/button.h>
#include <wx/dirdlg.h>
#include <wx/event.h>
#include <wx/filefn.h>
#include <wx/filename.h>
#include <wx/utils.h>
#include "../Prefs.h"
#include "../ShuttleGui.h"
#include "../TempDirectory.h"
#include "../widgets/AudacityMessageBox.h"
#include "../widgets/ReadOnlyText.h"
#include "../widgets/wxTextCtrlWrapper.h"
using namespace FileNames;
using namespace TempDirectory;
class FilesystemValidator : public wxValidator
{
public:
FilesystemValidator(const TranslatableString &message)
: wxValidator()
{
mMessage = message;
}
virtual wxObject* Clone() const wxOVERRIDE
{
return safenew FilesystemValidator(mMessage);
}
virtual bool Validate(wxWindow* WXUNUSED(parent)) wxOVERRIDE
{
wxTextCtrl* tc = wxDynamicCast(GetWindow(), wxTextCtrl);
if (!tc) {
return true;
}
if (FATFilesystemDenied(tc->GetValue(), mMessage)) {
return false;
}
return true;
}
virtual bool TransferToWindow() wxOVERRIDE
{
return true;
}
virtual bool TransferFromWindow() wxOVERRIDE
{
return true;
}
void OnChar(wxKeyEvent &evt)
{
evt.Skip();
wxTextCtrl* tc = wxDynamicCast(GetWindow(), wxTextCtrl);
if (!tc) {
return;
}
auto keycode = evt.GetUnicodeKey();
if (keycode < WXK_SPACE || keycode == WXK_DELETE) {
return;
}
wxString path = tc->GetValue();
path.insert(tc->GetInsertionPoint(), keycode);
if (FATFilesystemDenied(path, mMessage)) {
evt.Skip(false);
return;
}
}
TranslatableString mMessage;
wxDECLARE_EVENT_TABLE();
};
wxBEGIN_EVENT_TABLE(FilesystemValidator, wxValidator)
EVT_CHAR(FilesystemValidator::OnChar)
wxEND_EVENT_TABLE()
enum
{
TempTextID = 1000,
TempButtonID,
TextsStart = 1010,
OpenTextID,
SaveTextID,
ImportTextID,
ExportTextID,
MacrosTextID,
TextsEnd,
ButtonsStart = 1020,
OpenButtonID,
SaveButtonID,
ImportButtonID,
ExportButtonID,
MacrosButtonID,
ButtonsEnd
};
BEGIN_EVENT_TABLE(DirectoriesPrefs, PrefsPanel)
EVT_TEXT(TempTextID, DirectoriesPrefs::OnTempText)
EVT_BUTTON(TempButtonID, DirectoriesPrefs::OnTempBrowse)
EVT_COMMAND_RANGE(ButtonsStart, ButtonsEnd, wxEVT_BUTTON, DirectoriesPrefs::OnBrowse)
END_EVENT_TABLE()
DirectoriesPrefs::DirectoriesPrefs(wxWindow * parent, wxWindowID winid)
/* i18n-hint: Directories, also called directories, in computer file systems */
: PrefsPanel(parent, winid, XO("Directories")),
mFreeSpace(NULL),
mTempText(NULL)
{
Populate();
}
DirectoriesPrefs::~DirectoriesPrefs()
{
}
ComponentInterfaceSymbol DirectoriesPrefs::GetSymbol()
{
return DIRECTORIES_PREFS_PLUGIN_SYMBOL;
}
TranslatableString DirectoriesPrefs::GetDescription()
{
return XO("Preferences for Directories");
}
wxString DirectoriesPrefs::HelpPageName()
{
return "Directories_Preferences";
}
/// Creates the dialog and its contents.
void DirectoriesPrefs::Populate()
{
//------------------------- Main section --------------------
// Now construct the GUI itself.
// Use 'eIsCreatingFromPrefs' so that the GUI is
// initialised with values from gPrefs.
ShuttleGui S(this, eIsCreatingFromPrefs);
PopulateOrExchange(S);
// ----------------------- End of main section --------------
wxCommandEvent e;
OnTempText(e);
}
void DirectoriesPrefs::PopulateOrExchange(ShuttleGui &S)
{
S.SetBorder(2);
S.StartScroller();
S.StartStatic(XO("Default directories"));
{
S.AddSpace(1);
S.AddFixedText(XO("Leave a field empty to go to the last directory used for that operation.\n"
"Fill in a field to always go to that directory for that operation."), false, 450);
S.AddSpace(5);
S.StartMultiColumn(3, wxEXPAND);
{
S.SetStretchyCol(1);
S.Id(OpenTextID);
mOpenText = S.TieTextBox(XXO("O&pen:"),
{PreferenceKey(Operation::Open, PathType::User),
wxT("")},
30);
S.Id(OpenButtonID).AddButton(XXO("&Browse..."));
S.Id(SaveTextID);
mSaveText = S.TieTextBox(XXO("S&ave:"),
{PreferenceKey(Operation::Save, PathType::User),
wxT("")},
30);
if( mSaveText )
mSaveText->SetValidator(FilesystemValidator(XO("Projects cannot be saved to FAT drives.")));
S.Id(SaveButtonID).AddButton(XXO("B&rowse..."));
S.Id(ImportTextID);
mImportText = S.TieTextBox(XXO("&Import:"),
{PreferenceKey(Operation::Import, PathType::User),
wxT("")},
30);
S.Id(ImportButtonID).AddButton(XXO("Br&owse..."));
S.Id(ExportTextID);
mExportText = S.TieTextBox(XXO("&Export:"),
{PreferenceKey(Operation::Export, PathType::User),
wxT("")},
30);
S.Id(ExportButtonID).AddButton(XXO("Bro&wse..."));
S.Id(MacrosTextID);
mMacrosText = S.TieTextBox(XXO("&Macro output:"),
{PreferenceKey(Operation::MacrosOut, PathType::User),
wxT("")},
30);
S.Id(MacrosButtonID).AddButton(XXO("Bro&wse..."));
}
S.EndMultiColumn();
}
S.EndStatic();
S.StartStatic(XO("Temporary files directory"));
{
S.StartMultiColumn(3, wxEXPAND);
{
S.SetStretchyCol(1);
S.Id(TempTextID);
mTempText = S.TieTextBox(XXO("&Location:"),
{PreferenceKey(Operation::Temp, PathType::_None),
wxT("")},
30);
if( mTempText )
mTempText->SetValidator(FilesystemValidator(XO("Temporary files directory cannot be on a FAT drive.")));
S.Id(TempButtonID).AddButton(XXO("Brow&se..."));
mFreeSpace = S
.AddReadOnlyText(XXO("&Free Space:"), "");
}
S.EndMultiColumn();
}
S.EndStatic();
S.EndScroller();
}
void DirectoriesPrefs::OnTempBrowse(wxCommandEvent &evt)
{
wxString oldTemp = gPrefs->Read(PreferenceKey(Operation::Open, PathType::_None),
DefaultTempDir());
// Because we went through InitTemp() during initialisation,
// the old temp directory name in prefs should already be OK. Just in case there is
// some way we hadn't thought of for it to be not OK,
// we avoid prompting with it in that case and use the suggested default instead.
if (!IsTempDirectoryNameOK(oldTemp))
{
oldTemp = DefaultTempDir();
}
wxDirDialogWrapper dlog(this,
XO("Choose a location to place the temporary directory"),
oldTemp);
int retval = dlog.ShowModal();
if (retval != wxID_CANCEL && !dlog.GetPath().empty())
{
wxFileName tmpDirPath;
tmpDirPath.AssignDir(dlog.GetPath());
if (FATFilesystemDenied(tmpDirPath.GetFullPath(),
XO("Temporary files directory cannot be on a FAT drive."))) {
return;
}
// Append an "audacity_temp" directory to this path if necessary (the
// default, the existing pref (as stored in the control), and any path
// ending in a directory with the same name as what we'd add should be OK
// already)
wxString newDirName;
#if defined(__WXMAC__)
newDirName = wxT("SessionData");
#elif defined(__WXMSW__)
// Clearing Bug 1271 residual issue. Let's NOT have temp in the name.
newDirName = wxT("SessionData");
#else
newDirName = wxT(".audacity_temp");
#endif
auto dirsInPath = tmpDirPath.GetDirs();
// If the default temp dir or user's pref dir don't end in '/' they cause
// wxFileName's == operator to construct a wxFileName representing a file
// (that doesn't exist) -- hence the constructor calls
if (tmpDirPath != wxFileName(DefaultTempDir(), wxT("")) &&
tmpDirPath != wxFileName(mTempText->GetValue(), wxT("")) &&
(dirsInPath.size() == 0 ||
dirsInPath[dirsInPath.size()-1] != newDirName))
{
tmpDirPath.AppendDir(newDirName);
}
mTempText->SetValue(tmpDirPath.GetPath(wxPATH_GET_VOLUME|wxPATH_GET_SEPARATOR));
OnTempText(evt);
}
}
void DirectoriesPrefs::OnTempText(wxCommandEvent & WXUNUSED(evt))
{
TranslatableString label;
if (mTempText && mFreeSpace)
{
FilePath path = mTempText->GetValue();
wxLongLong space;
wxGetDiskSpace(path, NULL, &space);
label = wxDirExists(path)
? Internat::FormatSize(space)
: XO("unavailable - above location doesn't exist");
mFreeSpace->SetValue(label.Translation());
}
}
void DirectoriesPrefs::OnBrowse(wxCommandEvent &evt)
{
long id = evt.GetId() - ButtonsStart;
wxTextCtrl *tc = (wxTextCtrl *) FindWindow(id + TextsStart);
wxString location = tc->GetValue();
wxDirDialogWrapper dlog(this,
XO("Choose a location"),
location);
int retval = dlog.ShowModal();
if (retval == wxID_CANCEL)
{
return;
}
if (evt.GetId() == SaveButtonID)
{
if (FATFilesystemDenied(dlog.GetPath(),
XO("Projects cannot be saved to FAT drives.")))
{
return;
}
}
tc->SetValue(dlog.GetPath());
}
bool DirectoriesPrefs::Validate()
{
wxFileName Temp;
Temp.SetPath(mTempText->GetValue());
wxString path{Temp.GetPath()};
if( !IsTempDirectoryNameOK( path ) ) {
AudacityMessageBox(
XO("Directory %s is not suitable (at risk of being cleaned out)")
.Format( path ),
XO("Error"),
wxOK | wxICON_ERROR);
return false;
}
if (!Temp.DirExists()) {
int ans = AudacityMessageBox(
XO("Directory %s does not exist. Create it?")
.Format( path ),
XO("New Temporary Directory"),
wxYES_NO | wxCENTRE | wxICON_EXCLAMATION);
if (ans != wxYES) {
return false;
}
if (!Temp.Mkdir(0755, wxPATH_MKDIR_FULL)) {
/* wxWidgets throws up a decent looking dialog */
return false;
}
}
else {
/* If the directory already exists, make sure it is writable */
wxLogNull logNo;
Temp.AppendDir(wxT("canicreate"));
path = Temp.GetPath();
if (!Temp.Mkdir(0755)) {
AudacityMessageBox(
XO("Directory %s is not writable")
.Format( path ),
XO("Error"),
wxOK | wxICON_ERROR);
return false;
}
Temp.Rmdir();
Temp.RemoveLastDir();
}
wxFileName oldDir;
oldDir.SetPath(TempDir());
if (Temp != oldDir) {
AudacityMessageBox(
XO(
"Changes to temporary directory will not take effect until Audacity is restarted"),
XO("Temp Directory Update"),
wxOK | wxCENTRE | wxICON_INFORMATION);
}
return true;
}
bool DirectoriesPrefs::Commit()
{
ShuttleGui S(this, eIsSavingToPrefs);
PopulateOrExchange(S);
return true;
}
PrefsPanel::Factory
DirectoriesPrefsFactory()
{
return [](wxWindow *parent, wxWindowID winid, AudacityProject *)
{
wxASSERT(parent); // to justify safenew
return safenew DirectoriesPrefs(parent, winid);
};
}
namespace
{
PrefsPanel::Registration sAttachment
{
"Directories",
DirectoriesPrefsFactory()
};
};