Skip to content

Commit 407fe8f

Browse files
Save As refactor (#217)
Save as now discards unsaved changes to current project Unsaved changes only written to project under new name Adds filesystem function Delete(Path&) Adds optional argument to PersistencyService::Save(const char* name) Save temp file Copy temp file to new location Delete temp file Bump CHANGELOG #212 * Guard against forbidden paths Call ACTION_SAVE_AS NewProjectDialog with "root:" as path Change default currentPath to "root:" (fix regression caused in #215) Disallow accidental root folder access in FileSystem::Descend #219
1 parent e701a4d commit 407fe8f

7 files changed

Lines changed: 38 additions & 18 deletions

File tree

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
Fixes:
1010
Add 64 bit soundfont support (#211)
1111
Skip randomly generated project name if a directory with that name already exists (#175)
12+
Save Song As" saves to current as well as new project (#212)
1213

1314
1.5.0-bacon3
1415
Contributions:

sources/Application/Persistency/PersistencyService.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
PersistencyService::PersistencyService():Service(MAKE_FOURCC('S','V','P','S')) {
88
} ;
99

10-
void PersistencyService::Save() {
10+
void PersistencyService::Save(const char *name) {
1111

12-
Path filename("project:lgptsav.dat") ;
12+
Path filename(name);
1313

14-
TiXmlDocument doc(filename.GetPath()) ;
15-
TiXmlElement first("LITTLEGPTRACKER") ;
14+
TiXmlDocument doc(filename.GetPath());
15+
TiXmlElement first("LITTLEGPTRACKER") ;
1616
TiXmlNode *node=doc.InsertEndChild(first) ;
1717

1818
// Loop on all registered service
@@ -25,7 +25,7 @@ void PersistencyService::Save() {
2525
} ;
2626

2727
doc.SaveFile() ;
28-
} ;
28+
};
2929

3030
bool PersistencyService::Load() {
3131

sources/Application/Persistency/PersistencyService.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
class PersistencyService: public Service,public T_Singleton<PersistencyService> {
99
public:
1010
PersistencyService() ;
11-
void Save() ;
12-
bool Load() ;
11+
void Save(const char *name = "project:lgptsav.dat");
12+
bool Load() ;
1313
} ;
1414

1515
class PersistencyDocument: public TiXmlDocument {

sources/Application/Views/ModalDialogs/NewProjectDialog.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
class NewProjectDialog:public ModalView {
1111
public:
12-
NewProjectDialog(View &view, Path currentPath = "");
12+
NewProjectDialog(View &view, Path currentPath = "root:");
1313
virtual ~NewProjectDialog();
1414

1515
virtual void DrawView();

sources/Application/Views/ProjectView.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ static void SaveAsProjectCallback(View &v,ModalView &dialog) {
3838
Path path_dstprjdir = Path(str_dstprjdir);
3939
Path path_dstsmpdir = Path(str_dstsmpdir);
4040

41-
Path path_srclgptdatsav = path_srcprjdir.GetPath() + "lgptsav.dat";
42-
Path path_dstlgptdatsav = path_dstprjdir.GetPath() + "/lgptsav.dat";
41+
Path path_srclgptdatsav = path_srcprjdir.GetPath() + "lgptsav_tmp.dat";
42+
Path path_dstlgptdatsav = path_dstprjdir.GetPath() + "/lgptsav.dat";
4343

4444
if (path_dstprjdir.Exists()) {
4545
Trace::Log("ProjectView", "Dst Dir '%s' Exist == true",
@@ -55,10 +55,13 @@ static void SaveAsProjectCallback(View &v,ModalView &dialog) {
5555
return;
5656
};
5757

58-
FSS.Copy(path_srclgptdatsav,path_dstlgptdatsav);
58+
if (FSS.Copy(path_srclgptdatsav, path_dstlgptdatsav) > -1) {
59+
FSS.Delete(path_srclgptdatsav);
60+
}
5961

60-
I_Dir *idir_srcsmpdir=FileSystem::GetInstance()->Open(path_srcsmpdir.GetPath().c_str());
61-
if (idir_srcsmpdir) {
62+
I_Dir *idir_srcsmpdir =
63+
FileSystem::GetInstance()->Open(path_srcsmpdir.GetPath().c_str());
64+
if (idir_srcsmpdir) {
6265
idir_srcsmpdir->GetContent("*");
6366
idir_srcsmpdir->Sort();
6467
IteratorPtr<Path>it(idir_srcsmpdir->GetIterator());
@@ -270,8 +273,8 @@ void ProjectView::Update(Observable &,I_ObservableData *data) {
270273
}
271274
case ACTION_SAVE_AS: {
272275
PersistencyService *service = PersistencyService::GetInstance();
273-
service->Save();
274-
NewProjectDialog *mb = new NewProjectDialog(*this);
276+
service->Save("project:lgptsav_tmp.dat");
277+
NewProjectDialog *mb = new NewProjectDialog(*this, "root:");
275278
DoModal(mb, SaveAsProjectCallback);
276279
break;
277280
}

sources/System/FileSystem/FileSystem.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,8 @@ std::string Path::GetCanonicalPath() {
7676
Path Path::Descend(const std::string& leaf)
7777
{
7878
std::string currentPath = GetPath();
79-
if (currentPath[currentPath.size()-1] != '/')
80-
{
81-
currentPath += "/";
79+
if (!currentPath.empty() && currentPath[currentPath.size() - 1] != '/') {
80+
currentPath += "/";
8281
}
8382
return Path(currentPath+leaf);
8483
}
@@ -215,4 +214,20 @@ int FileSystemService::Copy(const Path &src,const Path &dst)
215214
isrc->Close();
216215
idst->Close();
217216
return nbwrite;
217+
}
218+
219+
int FileSystemService::Delete(const Path &path) {
220+
int result = -1;
221+
std::string pathString = path.GetPath();
222+
FileSystem * fs = FileSystem::GetInstance();
223+
224+
if (fs->GetFileType(pathString.c_str()) != FT_UNKNOWN) {
225+
fs->Delete(pathString.c_str());
226+
result += 1;
227+
Trace::Log("FileSystemService"," Delete %s ", pathString.c_str());
228+
} else {
229+
Trace::Log("FS Delete","path does not exist: %s", pathString.c_str());
230+
}
231+
232+
return result;
218233
}

sources/System/FileSystem/FileSystem.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ class FileSystem: public T_Factory<FileSystem> {
111111
class FileSystemService {
112112
public:
113113
int Copy(const Path &src,const Path &dst);
114+
int Delete(const Path &path);
114115
};
115116

116117
#endif

0 commit comments

Comments
 (0)