forked from Sneeds-Feed-and-Seed/sneedacity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoRecoveryDialog.cpp
508 lines (430 loc) · 12.6 KB
/
AutoRecoveryDialog.cpp
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/**********************************************************************
Sneedacity: A Digital Audio Editor
AutoRecoveryDialog.cpp
Paul Licameli split from AutoRecovery.cpp
**********************************************************************/
#include "AutoRecoveryDialog.h"
#include "ActiveProjects.h"
#include "ProjectManager.h"
#include "ProjectFileIO.h"
#include "ProjectFileManager.h"
#include "ShuttleGui.h"
#include "TempDirectory.h"
#include "widgets/SneedacityMessageBox.h"
#include "widgets/wxPanelWrapper.h"
#include <wx/dir.h>
#include <wx/evtloop.h>
#include <wx/filefn.h>
#include <wx/filename.h>
#include <wx/listctrl.h>
enum {
ID_QUIT_SNEEDACITY = 10000,
ID_DISCARD_SELECTED,
ID_RECOVER_SELECTED,
ID_SKIP,
ID_FILE_LIST
};
class AutoRecoveryDialog final : public wxDialogWrapper
{
public:
explicit AutoRecoveryDialog(SneedacityProject *proj);
bool HasRecoverables() const;
FilePaths GetRecoverables();
private:
void PopulateOrExchange(ShuttleGui &S);
void PopulateList();
bool HaveChecked();
void OnQuitSneedacity(wxCommandEvent &evt);
void OnDiscardSelected(wxCommandEvent &evt);
void OnRecoverSelected(wxCommandEvent &evt);
void OnSkip(wxCommandEvent &evt);
void OnColumnClicked(wxListEvent &evt);
void OnItemActivated(wxListEvent &evt);
void OnListKeyDown(wxKeyEvent &evt);
FilePaths mFiles;
wxListCtrl *mFileList;
SneedacityProject *mProject;
public:
DECLARE_EVENT_TABLE()
};
BEGIN_EVENT_TABLE(AutoRecoveryDialog, wxDialogWrapper)
EVT_BUTTON(ID_QUIT_SNEEDACITY, AutoRecoveryDialog::OnQuitSneedacity)
EVT_BUTTON(ID_DISCARD_SELECTED, AutoRecoveryDialog::OnDiscardSelected)
EVT_BUTTON(ID_RECOVER_SELECTED, AutoRecoveryDialog::OnRecoverSelected)
EVT_BUTTON(ID_SKIP, AutoRecoveryDialog::OnSkip)
EVT_LIST_COL_CLICK(ID_FILE_LIST, AutoRecoveryDialog::OnColumnClicked)
EVT_LIST_ITEM_ACTIVATED(ID_FILE_LIST, AutoRecoveryDialog::OnItemActivated)
END_EVENT_TABLE()
AutoRecoveryDialog::AutoRecoveryDialog(SneedacityProject *project)
: wxDialogWrapper(nullptr, wxID_ANY, XO("Automatic Crash Recovery"),
wxDefaultPosition, wxDefaultSize,
(wxDEFAULT_DIALOG_STYLE & (~wxCLOSE_BOX)) | wxRESIZE_BORDER), // no close box
mProject(project)
{
SetName();
ShuttleGui S(this, eIsCreating);
PopulateOrExchange(S);
}
bool AutoRecoveryDialog::HasRecoverables() const
{
return mFiles.size() > 0;
}
FilePaths AutoRecoveryDialog::GetRecoverables()
{
return mFiles;
}
void AutoRecoveryDialog::PopulateOrExchange(ShuttleGui &S)
{
S.SetBorder(5);
S.StartVerticalLay(wxEXPAND, 1);
{
S.AddFixedText(
XO("The following projects were not saved properly the last time Sneedacity was run and "
"can be automatically recovered.\n\n"
"After recovery, save the projects to ensure changes are written to disk."),
false,
500);
S.StartStatic(XO("Recoverable &projects"), 1);
{
mFileList = S.Id(ID_FILE_LIST)
.ConnectRoot(wxEVT_KEY_DOWN, &AutoRecoveryDialog::OnListKeyDown)
.AddListControlReportMode(
{
/*i18n-hint: (verb). It instruct the user to select items.*/
XO("Select"),
/*i18n-hint: (noun). It's the name of the project to recover.*/
XO("Name")
});
mFileList->EnableCheckBoxes();
PopulateList();
}
S.EndStatic();
S.StartHorizontalLay(wxALIGN_CENTRE, 0);
{
S.Id(ID_QUIT_SNEEDACITY).AddButton(XXO("&Quit Sneedacity"));
S.Id(ID_DISCARD_SELECTED).AddButton(XXO("&Discard Selected"));
S.Id(ID_RECOVER_SELECTED).AddButton(XXO("&Recover Selected"), wxALIGN_CENTRE, true);
S.Id(ID_SKIP).AddButton(XXO("&Skip"));
SetAffirmativeId(ID_RECOVER_SELECTED);
SetEscapeId(ID_SKIP);
}
S.EndHorizontalLay();
}
S.EndVerticalLay();
Layout();
Fit();
SetMinSize(GetSize());
// Sometimes it centers on wxGTK and sometimes it doesn't.
// Yielding before centering seems to be a good workaround,
// but will leave to implement on a rainy day.
Center();
}
void AutoRecoveryDialog::PopulateList()
{
wxString tempdir = TempDirectory::TempDir();
wxString pattern = wxT("*.") + FileNames::UnsavedProjectExtension();
FilePaths files;
wxDir::GetAllFiles(tempdir, &files, pattern, wxDIR_FILES);
FilePaths active = ActiveProjects::GetAll();
for (auto file : active)
{
wxFileName fn = file;
if (fn.FileExists())
{
FilePath fullPath = fn.GetFullPath();
if (files.Index(fullPath) == wxNOT_FOUND)
{
files.push_back(fullPath);
}
}
else
{
ActiveProjects::Remove(file);
}
}
FilePath activeFile;
if (mProject)
{
auto &projectFileIO = ProjectFileIO::Get(*mProject);
activeFile = projectFileIO.GetFileName();
}
mFiles.clear();
mFileList->DeleteAllItems();
long item = 0;
for (auto file : files)
{
wxFileName fn = file;
if (fn != activeFile)
{
mFiles.push_back(fn.GetFullPath());
mFileList->InsertItem(item, wxT(""));
mFileList->SetItem(item, 1, fn.GetName());
mFileList->CheckItem(item, true);
item++;
}
}
mFileList->SetMinSize(mFileList->GetBestSize());
mFileList->SetColumnWidth(0, wxLIST_AUTOSIZE_USEHEADER);
mFileList->SetColumnWidth(1, 500);
if (item)
{
mFileList->SetItemState(0,
wxLIST_STATE_FOCUSED | wxLIST_STATE_SELECTED,
wxLIST_STATE_FOCUSED | wxLIST_STATE_SELECTED);
mFileList->SetFocus();
}
}
bool AutoRecoveryDialog::HaveChecked()
{
long item = -1;
while (true)
{
item = mFileList->GetNextItem(item, wxLIST_NEXT_ALL, wxLIST_STATE_DONTCARE);
if (item == wxNOT_FOUND)
{
break;
}
if (mFileList->IsItemChecked(item))
{
return true;
}
}
SneedacityMessageBox(XO("No projects selected"), XO("Automatic Crash Recovery"));
return false;
}
void AutoRecoveryDialog::OnQuitSneedacity(wxCommandEvent &WXUNUSED(evt))
{
EndModal(ID_QUIT_SNEEDACITY);
}
void AutoRecoveryDialog::OnDiscardSelected(wxCommandEvent &WXUNUSED(evt))
{
if (!HaveChecked())
{
return;
}
long item = -1;
bool selectedTemporary = false;
while (!selectedTemporary)
{
item = mFileList->GetNextItem(item, wxLIST_NEXT_ALL, wxLIST_STATE_DONTCARE);
if (item == wxNOT_FOUND)
break;
if (!mFileList->IsItemChecked(item))
continue;
FilePath fileName = mFiles[item];
wxFileName file(fileName);
if (file.GetExt().IsSameAs(FileNames::UnsavedProjectExtension()))
selectedTemporary = true;
}
// Don't give this warning message if all of the checked items are
// previously saved, non-temporary projects.
if (selectedTemporary) {
int ret = SneedacityMessageBox(
XO("Are you sure you want to discard the selected projects?\n\n"
"Choosing \"Yes\" permanently deletes the selected projects immediately."),
XO("Automatic Crash Recovery"),
wxICON_QUESTION | wxYES_NO | wxNO_DEFAULT, this);
if (ret == wxNO)
return;
}
item = -1;
FilePaths files;
while (true)
{
item = mFileList->GetNextItem(item, wxLIST_NEXT_ALL, wxLIST_STATE_DONTCARE);
if (item == wxNOT_FOUND)
{
break;
}
if (!mFileList->IsItemChecked(item))
{
// Keep in list
files.push_back(mFiles[item]);
continue;
}
FilePath fileName = mFiles[item];
// Only remove it from disk if it appears to be a temporary file.
wxFileName file(fileName);
if (file.GetExt().IsSameAs(FileNames::UnsavedProjectExtension()))
{
file.SetFullName(wxT(""));
wxFileName temp(TempDirectory::TempDir(), wxT(""));
if (file == temp)
ProjectFileIO::RemoveProject(fileName);
}
else
// Don't remove from disk, but do (later) open the database
// of this saved file, and discard edits
files.push_back(fileName);
// Forget all about it
ActiveProjects::Remove(fileName);
}
PopulateList();
mFiles = files;
if (mFileList->GetItemCount() == 0)
{
EndModal(ID_DISCARD_SELECTED);
}
}
void AutoRecoveryDialog::OnRecoverSelected(wxCommandEvent &WXUNUSED(evt))
{
if (!HaveChecked())
{
return;
}
FilePaths files;
bool selected = false;
long item = -1;
while (true)
{
item = mFileList->GetNextItem(item, wxLIST_NEXT_ALL, wxLIST_STATE_DONTCARE);
if (item == wxNOT_FOUND)
{
if (!selected)
{
SneedacityMessageBox(XO("No projects selected"), XO("Automatic Crash Recovery"));
}
break;
}
selected = true;
if (!mFileList->IsItemChecked(item))
{
continue;
}
files.push_back(mFiles[item]);
}
mFiles = files;
EndModal(ID_RECOVER_SELECTED);
}
void AutoRecoveryDialog::OnSkip(wxCommandEvent &WXUNUSED(evt))
{
EndModal(ID_SKIP);
}
void AutoRecoveryDialog::OnColumnClicked(wxListEvent &evt)
{
if (evt.GetColumn() != 0)
{
return;
}
long item = -1;
while (true)
{
item = mFileList->GetNextItem(item, wxLIST_NEXT_ALL, wxLIST_STATE_DONTCARE);
if (item == wxNOT_FOUND)
{
break;
}
mFileList->CheckItem(item, !mFileList->IsItemChecked(item));
}
}
void AutoRecoveryDialog::OnItemActivated(wxListEvent &evt)
{
long item = evt.GetIndex();
mFileList->CheckItem(item, !mFileList->IsItemChecked(item));
}
void AutoRecoveryDialog::OnListKeyDown(wxKeyEvent &evt)
{
switch (evt.GetKeyCode())
{
case WXK_SPACE:
{
bool selected = false;
long item = -1;
while (true)
{
item = mFileList->GetNextItem(item, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
if (item == wxNOT_FOUND)
{
break;
}
mFileList->CheckItem(item, !mFileList->IsItemChecked(item));
}
}
break;
case WXK_RETURN:
// Don't know why wxListCtrls prevent default dialog action,
// but they do, so handle it.
EmulateButtonClickIfPresent(GetAffirmativeId());
break;
default:
evt.Skip();
break;
}
}
////////////////////////////////////////////////////////////////////////////
static bool RecoverAllProjects(const FilePaths &files,
SneedacityProject *&pproj)
{
// Open a project window for each auto save file
wxString filename;
bool result = true;
for (auto &file: files)
{
SneedacityProject *proj = nullptr;
// Reuse any existing project window, which will be the empty project
// created at application startup
std::swap(proj, pproj);
// Open project.
if (ProjectManager::OpenProject(proj, file, false, true) == nullptr)
{
result = false;
}
}
return result;
}
static void DiscardAllProjects(const FilePaths &files)
{
// Open and close each file, invisibly, removing its Autosave blob
for (auto &file: files)
ProjectFileManager::DiscardAutosave(file);
}
bool ShowAutoRecoveryDialogIfNeeded(SneedacityProject *&pproj, bool *didRecoverAnything)
{
if (didRecoverAnything)
{
*didRecoverAnything = false;
}
bool success = true;
// Under wxGTK3, the auto recovery dialog will not get
// the focus since the project window hasn't been allowed
// to completely initialize.
//
// Yielding seems to allow the initialization to complete.
//
// Additionally, it also corrects a sizing issue in the dialog
// related to wxWidgets bug:
//
// http://trac.wxwidgets.org/ticket/16440
//
// This must be done before "dlg" is declared.
wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI);
AutoRecoveryDialog dialog(pproj);
if (dialog.HasRecoverables())
{
int ret = dialog.ShowModal();
switch (ret)
{
case ID_SKIP:
success = true;
break;
case ID_DISCARD_SELECTED:
DiscardAllProjects(dialog.GetRecoverables());
success = true;
break;
case ID_RECOVER_SELECTED:
success = RecoverAllProjects(dialog.GetRecoverables(), pproj);
if (success)
{
if (didRecoverAnything)
{
*didRecoverAnything = true;
}
}
break;
default:
// This includes ID_QUIT_SNEEDACITY
return false;
}
}
return success;
}