File loading and saving checks

This commit is contained in:
Tim Warren 2015-04-17 16:55:48 -04:00
parent 3415733eff
commit 34ccd73350
7 changed files with 173 additions and 41 deletions

View File

@ -16,8 +16,8 @@ WX_RES = $(shell wx-config --rescomp)
LDLIBS = $(TARGET) LDLIBS = $(TARGET)
DEV_CXXFLAGS = -g -Wall -Wextra DEV_CXXFLAGS = -g -Wall -Wextra -DDEBUG
CXXFLAGS = -Os -s CXXFLAGS = -Os -DNDEBUG
TEST_SRC = $(wildcard tests/*.cpp) TEST_SRC = $(wildcard tests/*.cpp)
TESTS = $(patsubst %.cpp,%,$(TEST_SRC)) TESTS = $(patsubst %.cpp,%,$(TEST_SRC))

View File

@ -15,9 +15,7 @@ A Cross-platform Code Editor
Required packages: Required packages:
* build-essential * build-essential
* libwxgtk3.0-0
* libwxgtk3.0-dev * libwxgtk3.0-dev
* libssh2-1
* libssh2-1-dev * libssh2-1-dev
Optional: Optional:

View File

@ -10,6 +10,12 @@ const wxString APP_NAME = "Tyro";
const wxString APP_VENDOR = "Aviat Ion"; const wxString APP_VENDOR = "Aviat Ion";
const wxString APP_VERSION = "0.0.1"; const wxString APP_VERSION = "0.0.1";
// Some boilerplate text
const wxString TYRO_SAVE_ERROR = "Failed to save the file. Maybe you lack the permissions.";
const wxString TYRO_SAVE_ERROR_CAPTION = "Saving Failed";
const wxString TYRO_OPEN_ERROR = "Failed to open the file. Check that it exists, and that you have read permissions.";
const wxString TYRO_OPEN_ERROR_CAPTION = "Open Failed";
// EditPane file extension to lexer mapping // EditPane file extension to lexer mapping
typedef pair<string, int> StringConstMapData; typedef pair<string, int> StringConstMapData;
typedef map<string, int> StringConstMap; typedef map<string, int> StringConstMap;

View File

@ -29,16 +29,21 @@ EditPane::~EditPane()
} }
/** /**
* Encapsulate lexer selection when opening a file * Handle the highlighting config for the
* * selected file
*
* @param wxString filePath * @param wxString filePath
* @return bool * @return void
*/ */
bool EditPane::LoadAndHighlight(wxString filePath) void EditPane::Highlight(wxString filePath)
{ {
fileName = filePath; this->fileName.Assign(filePath);
string lang = this->GetLangByFile();
wxLogDebug("Highlighting method");
// Get the configuration name for the selected language
string lang = this->GetLangByFile();
this->StyleClearAll(); this->StyleClearAll();
// Font setup // Font setup
@ -147,29 +152,58 @@ bool EditPane::LoadAndHighlight(wxString filePath)
else else
{ {
string typeMap[] = {"null", "int", "unsigned int", "double", "string", "boolean", "array", "object"}; string typeMap[] = {"null", "int", "unsigned int", "double", "string", "boolean", "array", "object"};
stringstream output;
cerr << "current lang is:" << lang << endl; output << "current lang is:" << lang << endl;
cerr << "keywords array is not an array" << endl; output << "keywords array is not an array" << endl;
cerr << "keyword array is a " << typeMap[keywords_array.type()] << endl; output << "keyword array is a " << typeMap[keywords_array.type()] << endl;
wxLogDebug(output.str().c_str());
} }
return this->LoadFile(filePath);
} }
/**
* Check file path and open the selected file
*
* @param wxString filePath
* @return bool
*/
bool EditPane::Load(wxString filePath)
{
this->fileName.Assign(filePath);
if (this->FileReadable())
{
this->Highlight(filePath);
bool didLoad = this->LoadFile(filePath);
// @TODO Toggle controls based on write permission
return didLoad;
}
return false;
}
/**
* Determine the format of the current file by
* matching its extension against the patterns
* in the configuration files
*
* @return string
*/
string EditPane::GetLangByFile() string EditPane::GetLangByFile()
{ {
JsonValue langList = config->GetRoot(); JsonValue langList = config->GetRoot();
JsonValue::iterator it; JsonValue::iterator it;
wxFileName fname(this->fileName); wxString curr_file = this->fileName.GetFullName();
wxString curr_file = fname.GetFullName();
// Loop through each language to find a matching file pattern // Loop through each language to find a matching file pattern
for (it = langList.begin(); it != langList.end(); ++it) for (it = langList.begin(); it != langList.end(); ++it)
{ {
string lang = it.key().asString(); string lang = it.key().asString();
// Parse the file pattern // Parse the file pattern
wxString file_pattern((*it)["file_pattern"].asString()); wxString file_pattern((*it)["file_pattern"].asString());
@ -195,7 +229,9 @@ string EditPane::GetLangByFile()
bool EditPane::SaveFile() bool EditPane::SaveFile()
{ {
if ( ! this->fileName) wxString fname;
if ( ! this->fileName.IsOk())
{ {
wxFileDialog dlg ( wxFileDialog dlg (
this, this,
@ -207,24 +243,88 @@ bool EditPane::SaveFile()
); );
if (dlg.ShowModal() != wxID_OK) return false; if (dlg.ShowModal() != wxID_OK) return false;
this->fileName = dlg.GetPath(); fname = dlg.GetPath();
}
else
{
fname = this->fileName.GetFullPath();
} }
return this->SaveFile(this->fileName); const wxString cfname(fname);
return this->SaveFile(cfname);
} }
bool EditPane::SaveFile(const wxString &filename) bool EditPane::SaveFile(const wxString &filename)
{ {
if ( ! this->IsModified()) return true; if ( ! this->IsModified()) return true;
this->SetSavePoint(); this->fileName.Assign(filename);
return wxStyledTextCtrl::SaveFile(filename); // Check file permissions
if (this->FileWritable())
{
this->SetSavePoint();
return wxStyledTextCtrl::SaveFile(filename);
}
return false;
} }
bool EditPane::IsModified() bool EditPane::IsModified()
{ {
return (GetModify() && !GetReadOnly()); return this->GetModify();
}
/**
* Check that the current file can be opened
*
* @return bool
*/
bool EditPane::FileReadable()
{
if (
this->fileName.IsOk() &&
this->fileName.Exists() &&
this->fileName.IsFileReadable()
) return true;
// Hmm...well, let's give an error
wxMessageDialog errDlg(
this,
TYRO_OPEN_ERROR,
TYRO_OPEN_ERROR_CAPTION,
wxOK | wxICON_ERROR | wxCENTER
);
errDlg.ShowModal();
return false;
}
/**
* Check that the current file can be saved
*
* @return bool
*/
bool EditPane::FileWritable()
{
// Lets see...can we write somewhere?
if (
this->fileName.IsOk() &&
((this->fileName.Exists() && this->fileName.IsFileWritable()) ||
(( ! this->fileName.Exists()) && this->fileName.IsDirWritable()))
) return true;
// Hmm...well, let's give an error
wxMessageDialog errDlg(
this,
TYRO_SAVE_ERROR,
TYRO_SAVE_ERROR_CAPTION,
wxOK | wxICON_ERROR | wxCENTER
);
errDlg.ShowModal();
return false;
} }
void EditPane::BindEvents() void EditPane::BindEvents()

View File

@ -21,9 +21,10 @@ public:
wxVSCROLL wxVSCROLL
); );
~EditPane(); ~EditPane();
wxString fileName; wxFileName fileName;
string GetLangByFile(); string GetLangByFile();
bool LoadAndHighlight(wxString filePath); bool Load(wxString filePath);
void Highlight(wxString filePath);
bool SaveFile(); bool SaveFile();
bool SaveFile(const wxString &filename); bool SaveFile(const wxString &filename);
bool IsModified(); bool IsModified();
@ -36,6 +37,8 @@ private:
MARGIN_FOLD, MARGIN_FOLD,
MARGIN_LINE_NUMBERS MARGIN_LINE_NUMBERS
}; };
bool FileReadable();
bool FileWritable();
void BindEvents(); void BindEvents();
void OnMarginClick(wxStyledTextEvent &event); void OnMarginClick(wxStyledTextEvent &event);
}; };

View File

@ -24,7 +24,7 @@ MainFrame::MainFrame(wxFrame *frame, const wxString& title)
base_sizer->Add(notebook, 1, wxEXPAND | wxALL, 5); base_sizer->Add(notebook, 1, wxEXPAND | wxALL, 5);
base_sizer->SetContainingWindow(this); base_sizer->SetContainingWindow(this);
base_sizer->SetMinSize(600,400); base_sizer->SetMinSize(800,600);
this->DisableEditControls(); this->DisableEditControls();
this->BindEvents(); this->BindEvents();
@ -222,7 +222,39 @@ void MainFrame::OnSave(wxCommandEvent &WXUNUSED(event))
void MainFrame::OnSaveAs(wxCommandEvent &WXUNUSED(event)) void MainFrame::OnSaveAs(wxCommandEvent &WXUNUSED(event))
{ {
// @TODO Implement OnSaveAs EditPane *editor = notebook->GetCurrentEditor();
// If the file hasn't been changed, just return
if ( ! editor->IsModified()) return;
wxFileDialog dlg(
this,
"Save as...",
wxEmptyString,
wxEmptyString,
TYRO_FILE_SAVE_WILDCARDS,
wxFD_SAVE | wxFD_OVERWRITE_PROMPT
);
// Return if the file isn't to be saved
if (dlg.ShowModal() != wxID_OK) return;
wxString filePath = dlg.GetPath();
// Save the file
if(editor->SaveFile(filePath))
{
wxFileName fileName(filePath);
const wxString fullPath = filePath;
const wxString caption= fileName.GetFullName();
// Update the name of the tab
notebook->SetPageToolTip(notebook->GetSelection(), fullPath);
notebook->SetPageText(notebook->GetSelection(), caption);
// Update the editor highlighting
editor->Highlight(filePath);
}
} }
void MainFrame::OnQuit(wxCommandEvent &WXUNUSED(event)) void MainFrame::OnQuit(wxCommandEvent &WXUNUSED(event))

View File

@ -38,22 +38,15 @@ void TabContainer::AddTab(wxString filePath)
wxString caption= fileName.GetFullName(); wxString caption= fileName.GetFullName();
EditPane *editor = new EditPane(this, wxID_ANY); EditPane *editor = new EditPane(this, wxID_ANY);
bool loaded_file = editor->LoadAndHighlight(filePath); if (editor->Load(filePath))
if (loaded_file)
{ {
wxLogDebug("File should be properly loaded.");
this->AddPage(editor, caption, true); this->AddPage(editor, caption, true);
return;
} }
else
{ wxLogDebug("Failed to load file!?");
wxMessageDialog err(
this,
_T("Failed to open the specified file. Do you have permission to open it?"),
_T("Could not open file."),
wxOK|wxCENTER|wxICON_WARNING
);
err.ShowModal();
}
} }
EditPane *TabContainer::GetCurrentEditor() EditPane *TabContainer::GetCurrentEditor()