Skip to content

Commit

Permalink
Add save as (#12)
Browse files Browse the repository at this point in the history
Save As option in Project page
  • Loading branch information
djdiskmachine authored Jan 15, 2023
1 parent 1f40ee0 commit 2c86d5b
Show file tree
Hide file tree
Showing 9 changed files with 170 additions and 17 deletions.
18 changes: 17 additions & 1 deletion sources/Application/AppWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ AppWindow::AppWindow(I_GUIWindowImp &imp):GUIWindow(imp) {
_mixerView=0 ;
_grooveView=0 ;
_closeProject=0 ;
_loadAfterSaveAsProject=0 ;
_lastA=0 ;
_lastB=0 ;
_mask=0 ;
Expand Down Expand Up @@ -263,10 +264,11 @@ void AppWindow::Flush() {
} ;

void AppWindow::LoadProject(const Path &p) {

Trace::Log("LoadProject","%s\n", p.GetPath().c_str());
_root=p ;

_closeProject=false ;
_loadAfterSaveAsProject=false;

PersistencyService *persist=PersistencyService::GetInstance() ;

Expand Down Expand Up @@ -449,6 +451,11 @@ bool AppWindow::onEvent(GUIEvent &event) {
CloseProject() ;
_isDirty=true ;
}
if(_loadAfterSaveAsProject) {
CloseProject();
_isDirty=true;
LoadProject(_newProjectToLoad);
}
#ifdef _SHOW_GP2X_
Redraw() ;
#else
Expand Down Expand Up @@ -534,6 +541,15 @@ void AppWindow::Update(Observable &o,I_ObservableData *d) {
LoadProject(name) ;
break ;
} */

case VET_SAVEAS_PROJECT:
{
char *name=(char*)ve->GetData() ;
_loadAfterSaveAsProject=true;
strcpy(_newProjectToLoad,name);
break ;
}

case VET_QUIT_PROJECT:
{
// defer event to after we got out of the view
Expand Down
2 changes: 2 additions & 0 deletions sources/Application/AppWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,13 @@ class AppWindow:public GUIWindow,I_Observer,Status {

bool _isDirty ;
bool _closeProject ;
bool _loadAfterSaveAsProject ;
bool _shouldQuit ;
unsigned short _mask ;
unsigned long _lastA ;
unsigned long _lastB ;
char _statusLine[80] ;
char _newProjectToLoad[80];
unsigned char _charScreen[1200] ;
unsigned char _charScreenProp[1200] ;
unsigned char _preScreen[1200] ;
Expand Down
4 changes: 2 additions & 2 deletions sources/Application/Views/BaseClasses/ViewEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

enum ViewEventType {
VET_SWITCH_VIEW,
VET_PLAYER_POSITION_UPDATE,
VET_LIST_SELECT,
VET_PLAYER_POSITION_UPDATE,
VET_SAVEAS_PROJECT,
VET_QUIT_PROJECT,
VET_UPDATE,
VET_QUIT_APP
Expand Down
12 changes: 6 additions & 6 deletions sources/Application/Views/ModalDialogs/SelectProjectDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,15 +234,15 @@ Path SelectProjectDialog::GetSelection() {
Result SelectProjectDialog::OnNewProject(std::string &name) {

Path path = currentPath_.Descend(name);
Trace::Log("TMP","creating project at %s",path.GetPath().c_str());
selection_=path ;
Trace::Log("TMP","creating project at %s",path.GetPath().c_str());
selection_ = path ;
Result result = FileSystem::GetInstance()->MakeDir(path.GetPath().c_str()) ;
RETURN_IF_FAILED_MESSAGE(result,"Failed to create project dir");
RETURN_IF_FAILED(result, ("Failed to create project dir for '%s", path.GetPath().c_str()));

path= path.Descend("samples");
Trace::Log("TMP","creating samples dir at %s",path.GetPath().c_str());
path = path.Descend("samples");
Trace::Log("TMP","creating samples dir at %s",path.GetPath().c_str());
result = FileSystem::GetInstance()->MakeDir(path.GetPath().c_str()) ;
RETURN_IF_FAILED_MESSAGE(result,"Failed to create samples dir");
RETURN_IF_FAILED(result, ("Failed to create samples dir for '%s'", path.GetPath().c_str()));

EndModal(1) ;
return Result::NoError;
Expand Down
105 changes: 98 additions & 7 deletions sources/Application/Views/ProjectView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,80 @@
#include "System/System/System.h"
#include "Services/Midi/MidiService.h"
#include "Application/Views/ModalDialogs/MessageBox.h"

#define ACTION_PURGE MAKE_FOURCC('P','U','R','G')
#define ACTION_SAVE MAKE_FOURCC('S','A','V','E')
#define ACTION_LOAD MAKE_FOURCC('L','O','A','D')
#define ACTION_QUIT MAKE_FOURCC('Q','U','I','T')
#include "Application/Views/ModalDialogs/NewProjectDialog.h"
#include "Application/Views/ModalDialogs/SelectProjectDialog.h"

#define ACTION_PURGE MAKE_FOURCC('P','U','R','G')
#define ACTION_SAVE MAKE_FOURCC('S','A','V','E')
#define ACTION_SAVE_AS MAKE_FOURCC('S','V','A','S')
#define ACTION_LOAD MAKE_FOURCC('L','O','A','D')
#define ACTION_QUIT MAKE_FOURCC('Q','U','I','T')
#define ACTION_PURGE_INSTRUMENT MAKE_FOURCC('P','R','G','I')
#define ACTION_TEMPO_CHANGED MAKE_FOURCC('T','E','M','P')
#define ACTION_TEMPO_CHANGED MAKE_FOURCC('T','E','M','P')

static void SaveAsProjectCallback(View &v,ModalView &dialog) {

FileSystemService FSS;
NewProjectDialog &npd=(NewProjectDialog &)dialog;

if (dialog.GetReturnCode()>0) {
std::string str_dstprjdir;
std::string str_dstsmpdir;
#ifdef PLATFORM_PSP
str_dstprjdir = npd.GetName();
str_dstsmpdir = npd.GetName() + "/samples/";
#else
std::string up = "../";
str_dstprjdir = up + npd.GetName();
str_dstsmpdir = up + npd.GetName() + "/samples/";
#endif

Path path_dstprjdir = Path(str_dstprjdir);
Path path_dstsmpdir = Path(str_dstsmpdir);
Path path_srcprjdir("project:");
Path path_srcsmpdir("project:samples");

Path path_srclgptdatsav = path_srcprjdir.GetPath() + "/lgptsav.dat";
Path path_dstlgptdatsav = path_dstprjdir.GetPath() + "/lgptsav.dat";

if (path_dstprjdir.Exists()) { Trace::Log("ProjectView", "Dst Dir '%s' Exist == true",
path_dstprjdir.GetPath().c_str()); }
else {
Result result = FileSystem::GetInstance()->MakeDir(path_dstprjdir.GetPath().c_str());
if (result.Failed()) {
Trace::Log("ProjectView", "Failed to create dir '%s'", path_dstprjdir.GetPath().c_str());
return;
};

result = FileSystem::GetInstance()->MakeDir(path_dstsmpdir.GetPath().c_str()) ;

if (result.Failed()) {
Trace::Log("ProjectView", "Failed to create sample dir '%s'", path_dstprjdir.GetPath().c_str());
return;
};

FSS.Copy(path_srclgptdatsav,path_dstlgptdatsav);

I_Dir *idir_srcsmpdir=FileSystem::GetInstance()->Open(path_srcsmpdir.GetPath().c_str());
if (idir_srcsmpdir) {
idir_srcsmpdir->GetContent("*");
idir_srcsmpdir->Sort();
IteratorPtr<Path>it(idir_srcsmpdir->GetIterator());
for (it->Begin();!it->IsDone();it->Next()) {
Path &current=it->CurrentItem();
if (current.IsFile())
{
Path dstfile = Path((str_dstsmpdir+current.GetName()).c_str());
Path srcfile = Path(current.GetPath());
FSS.Copy(srcfile.GetPath(),dstfile.GetPath());
}
}
}

((ProjectView &)v).OnSaveAsProject((char*)str_dstprjdir.c_str());
}
}
}

static void LoadCallback(View &v,ModalView &dialog) {
if (dialog.GetReturnCode()==MBL_YES) {
Expand Down Expand Up @@ -75,10 +142,15 @@ ProjectView::ProjectView(GUIWindow &w,ViewData *data):FieldView(w,data) {
a1->AddObserver(*this) ;
T_SimpleList<UIField>::Insert(a1) ;

position._y+=1 ;
a1=new UIActionField("Save Song As",ACTION_SAVE_AS,position) ;
a1->AddObserver(*this) ;
T_SimpleList<UIField>::Insert(a1) ;

v=project_->FindVariable(VAR_MIDIDEVICE) ;
NAssert(v) ;
position._y+=2 ;
UIIntVarField *f3=new UIIntVarField(position,*v,"midi: %s",0,MidiService::GetInstance()->Size(),1,1) ;
UIIntVarField *f3=new UIIntVarField(position,*v,"MIDI: %s",0,MidiService::GetInstance()->Size(),1,1) ;
T_SimpleList<UIField>::Insert(f3) ;

position._y+=2 ;
Expand Down Expand Up @@ -169,6 +241,19 @@ void ProjectView::Update(Observable &,I_ObservableData *data) {
DoModal(mb) ;
}
break ;
case ACTION_SAVE_AS:
if (!player->IsRunning()) {
PersistencyService *service=PersistencyService::GetInstance() ;
service->Save() ;
NewProjectDialog *mb=new NewProjectDialog(*this) ;
DoModal(mb,SaveAsProjectCallback) ;

} else {
MessageBox *mb=new MessageBox(*this,"Not while playing",MBBF_OK) ;
DoModal(mb) ;
}
break ;

case ACTION_LOAD:
{
if (!player->IsRunning()) {
Expand Down Expand Up @@ -211,6 +296,12 @@ void ProjectView::OnLoadProject() {
NotifyObservers(&ve) ;
} ;

void ProjectView::OnSaveAsProject(char * data) {
ViewEvent ve(VET_SAVEAS_PROJECT,data) ;
SetChanged();
NotifyObservers(&ve) ;
} ;

void ProjectView::OnQuit() {
ViewEvent ve(VET_QUIT_APP) ;
SetChanged();
Expand Down
1 change: 1 addition & 0 deletions sources/Application/Views/ProjectView.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class ProjectView: public FieldView,public I_Observer {
void Update(Observable &,I_ObservableData *) ;

void OnLoadProject() ;
void OnSaveAsProject(char * data) ;
void OnPurgeInstruments(bool removeFromDisk) ;
void OnQuit() ;

Expand Down
3 changes: 2 additions & 1 deletion sources/System/Errors/Result.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Result {
Result &operator=(const Result &) ;

static Result NoError ;

protected:
Result() ;
private:
Expand All @@ -29,5 +30,5 @@ class Result {
mutable Result *child_ ;
} ;

#define RETURN_IF_FAILED_MESSAGE(r,m) if (r.Failed()) { return Result(r,m) ; }
#define RETURN_IF_FAILED(r,m) if (r.Failed()) {Result(r,m);}
#define LOG_IF_FAILED(r,m) if (r.Failed()) { Trace::Log(m) ; Trace::Log(r.GetDescription().c_str()); }
37 changes: 37 additions & 0 deletions sources/System/FileSystem/FileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,40 @@ void Path::Alias::SetAliasName(const char *alias) {
void Path::Alias::SetPath(const char *path) {
path_=path ;
} ;

int FileSystemService::Copy(const Path &src,const Path &dst)
{
int bufsize=4096;
char buffer[bufsize];
int count=0;
int nbwrite=-1;

FileSystem * fs=FileSystem::GetInstance() ;
I_File * isrc=fs->Open(src.GetPath().c_str(),"r");
I_File * idst=fs->Open(dst.GetPath().c_str(),"w");

Trace::Log("FS","FileSystemService::Copy %s to %s",
src.GetPath().c_str(), dst.GetPath().c_str());
if (isrc)
Trace::Log("FS","src open ok");
else {
Trace::Log("FS","src open fail");
return nbwrite;
}
if (idst)
Trace::Log("FS","dst open ok");
else {
Trace::Log("FS","dst open fail");
return nbwrite;
}

while (count=isrc->Read(buffer,sizeof(char),bufsize))
{
idst->Write(buffer,sizeof(char),count);
nbwrite++;
}

isrc->Close();
idst->Close();
return nbwrite;
}
5 changes: 5 additions & 0 deletions sources/System/FileSystem/FileSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,9 @@ class FileSystem: public T_Factory<FileSystem> {

#define FS_FOPEN(a,b) FileSystem::GetInstance()->Open(a,b)

class FileSystemService {
public:
int Copy(const Path &src,const Path &dst);
};

#endif

0 comments on commit 2c86d5b

Please sign in to comment.