Renaming files sort of works.

This commit is contained in:
David Given
2022-09-07 00:02:23 +02:00
parent c1fefb7e13
commit 99ca175e9a
9 changed files with 130 additions and 37 deletions

View File

@@ -60,7 +60,7 @@ public:
uint32_t capabilities() const
{
return OP_GETFSDATA | OP_CREATE | OP_LIST | OP_GETFILE | OP_PUTFILE |
OP_GETDIRENT | OP_DELETE;
OP_GETDIRENT | OP_DELETE | OP_MOVE;
}
std::map<std::string, std::string> getMetadata() override
@@ -208,6 +208,29 @@ public:
throw CannotWriteException();
}
void moveFile(const Path& oldPath, const Path& newPath) override
{
AdfMount m(this);
if ((oldPath.size() == 0) || (newPath.size() == 0))
throw BadPathException();
auto* vol = m.mount();
changeDirButOne(vol, oldPath);
auto oldDir = vol->curDirPtr;
changeDirButOne(vol, newPath);
auto newDir = vol->curDirPtr;
int res = adfRenameEntry(vol,
oldDir,
(char*)oldPath.back().c_str(),
newDir,
(char*)newPath.back().c_str());
if (res != RC_OK)
throw CannotWriteException();
}
private:
std::shared_ptr<Dirent> toDirent(struct Entry* entry, const Path& container)
{

View File

@@ -39,7 +39,7 @@ public:
uint32_t capabilities() const
{
return OP_GETFSDATA | OP_CREATE | OP_LIST | OP_GETFILE | OP_PUTFILE |
OP_GETDIRENT;
OP_GETDIRENT | OP_MOVE;
}
std::map<std::string, std::string> getMetadata() override
@@ -188,6 +188,15 @@ public:
throwError(res);
}
void moveFile(const Path& oldPath, const Path& newPath) override
{
mount();
auto oldPathStr = oldPath.to_str();
auto newPathStr = newPath.to_str();
FRESULT res = f_rename(oldPathStr.c_str(), newPathStr.c_str());
throwError(res);
}
private:
std::shared_ptr<Dirent> toDirent(FILINFO& filinfo, const Path& parent)
{

View File

@@ -27,7 +27,7 @@ public:
uint32_t capabilities() const
{
return OP_GETFSDATA | OP_CREATE | OP_LIST | OP_GETFILE | OP_PUTFILE |
OP_GETDIRENT;
OP_GETDIRENT | OP_MOVE;
}
std::map<std::string, std::string> getMetadata() override
@@ -169,6 +169,18 @@ public:
throw CannotWriteException();
}
void moveFile(const Path& oldPath, const Path& newPath) override
{
HfsMount m(this);
if (oldPath.empty() || newPath.empty())
throw BadPathException();
auto oldPathStr = ":" + oldPath.to_str(":");
auto newPathStr = ":" + newPath.to_str(":");
if (!hfs_rename(_vol, oldPathStr.c_str(), newPathStr.c_str()))
throw CannotWriteException();
}
private:
std::shared_ptr<Dirent> toDirent(hfsdirent& de, const Path& parent)
{

View File

@@ -129,7 +129,19 @@ public:
const wxDataViewItem& item,
unsigned column) override
{
fmt::print("{}\n", __LINE__);
auto node = Find(item);
if (!node || node->stub)
return false;
if ((column == 0) && (value.GetType() == "wxDataViewIconText"))
{
wxDataViewIconText dvit;
dvit << value;
node->newname = dvit.GetText();
return true;
}
return false;
}
unsigned GetChildren(const wxDataViewItem& item,

View File

@@ -16,6 +16,8 @@ public:
bool stub = false;
std::shared_ptr<Dirent> dirent;
std::map<std::string, std::shared_ptr<FilesystemNode>> children;
std::string newname; /* used for inline renames */
};
class FilesystemModel : public wxDataViewModel

View File

@@ -325,7 +325,7 @@ MainWindowGen::MainWindowGen( wxWindow* parent, wxWindowID id, const wxString& t
fgSizer23->Add( browserToolbar, 0, wxEXPAND, 5 );
browserTree = new wxDataViewCtrl( browsePanel, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxDV_SINGLE );
m_dataViewColumn1 = browserTree->AppendIconTextColumn( wxT("Filename"), 0, wxDATAVIEW_CELL_INERT, 250, static_cast<wxAlignment>(wxALIGN_LEFT), wxDATAVIEW_COL_RESIZABLE|wxDATAVIEW_COL_SORTABLE );
m_dataViewColumn1 = browserTree->AppendIconTextColumn( wxT("Filename"), 0, wxDATAVIEW_CELL_EDITABLE, 250, static_cast<wxAlignment>(wxALIGN_LEFT), wxDATAVIEW_COL_RESIZABLE|wxDATAVIEW_COL_SORTABLE );
m_dataViewColumn2 = browserTree->AppendTextColumn( wxT("Size"), 1, wxDATAVIEW_CELL_INERT, 100, static_cast<wxAlignment>(wxALIGN_RIGHT), wxDATAVIEW_COL_RESIZABLE );
m_dataViewColumn3 = browserTree->AppendTextColumn( wxT("Mode"), 2, wxDATAVIEW_CELL_INERT, -1, static_cast<wxAlignment>(wxALIGN_LEFT), wxDATAVIEW_COL_RESIZABLE );
fgSizer23->Add( browserTree, 0, wxALL|wxEXPAND, 5 );
@@ -400,6 +400,7 @@ MainWindowGen::MainWindowGen( wxWindow* parent, wxWindowID id, const wxString& t
browserMoreMenu->Bind(wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( MainWindowGen::OnBrowserRenameMenuItem ), this, browserRenameMenuItem->GetId());
browserMoreMenu->Bind(wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( MainWindowGen::OnBrowserDeleteMenuItem ), this, browserDeleteMenuItem->GetId());
this->Connect( browserFormatTool->GetId(), wxEVT_COMMAND_TOOL_CLICKED, wxCommandEventHandler( MainWindowGen::OnBrowserFormatButton ) );
browserTree->Connect( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE, wxDataViewEventHandler( MainWindowGen::OnBrowserFilenameChanged ), NULL, this );
browserTree->Connect( wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING, wxDataViewEventHandler( MainWindowGen::OnBrowserDirectoryExpanding ), NULL, this );
browserTree->Connect( wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED, wxDataViewEventHandler( MainWindowGen::OnBrowserSelectionChanged ), NULL, this );
browserDiscardButton->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( MainWindowGen::OnBrowserDiscardButton ), NULL, this );
@@ -432,6 +433,7 @@ MainWindowGen::~MainWindowGen()
this->Disconnect( browserViewTool->GetId(), wxEVT_COMMAND_TOOL_CLICKED, wxCommandEventHandler( MainWindowGen::OnBrowserViewButton ) );
this->Disconnect( browserSaveTool->GetId(), wxEVT_COMMAND_TOOL_CLICKED, wxCommandEventHandler( MainWindowGen::OnBrowserSaveButton ) );
this->Disconnect( browserFormatTool->GetId(), wxEVT_COMMAND_TOOL_CLICKED, wxCommandEventHandler( MainWindowGen::OnBrowserFormatButton ) );
browserTree->Disconnect( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE, wxDataViewEventHandler( MainWindowGen::OnBrowserFilenameChanged ), NULL, this );
browserTree->Disconnect( wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING, wxDataViewEventHandler( MainWindowGen::OnBrowserDirectoryExpanding ), NULL, this );
browserTree->Disconnect( wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED, wxDataViewEventHandler( MainWindowGen::OnBrowserSelectionChanged ), NULL, this );
browserDiscardButton->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( MainWindowGen::OnBrowserDiscardButton ), NULL, this );

View File

@@ -2505,6 +2505,7 @@
<property name="window_extra_style"></property>
<property name="window_name"></property>
<property name="window_style"></property>
<event name="OnDataViewCtrlItemEditingDone">OnBrowserFilenameChanged</event>
<event name="OnDataViewCtrlItemExpanding">OnBrowserDirectoryExpanding</event>
<event name="OnDataViewCtrlSelectionChanged">OnBrowserSelectionChanged</event>
<object class="dataViewColumn" expanded="1">
@@ -2512,7 +2513,7 @@
<property name="ellipsize"></property>
<property name="flags">wxDATAVIEW_COL_RESIZABLE|wxDATAVIEW_COL_SORTABLE</property>
<property name="label">Filename</property>
<property name="mode">wxDATAVIEW_CELL_INERT</property>
<property name="mode">wxDATAVIEW_CELL_EDITABLE</property>
<property name="model_column">0</property>
<property name="name">m_dataViewColumn1</property>
<property name="permission">protected</property>

View File

@@ -132,6 +132,7 @@ class MainWindowGen : public wxFrame
virtual void OnBrowserRenameMenuItem( wxCommandEvent& event ) { event.Skip(); }
virtual void OnBrowserDeleteMenuItem( wxCommandEvent& event ) { event.Skip(); }
virtual void OnBrowserFormatButton( wxCommandEvent& event ) { event.Skip(); }
virtual void OnBrowserFilenameChanged( wxDataViewEvent& event ) { event.Skip(); }
virtual void OnBrowserDirectoryExpanding( wxDataViewEvent& event ) { event.Skip(); }
virtual void OnBrowserSelectionChanged( wxDataViewEvent& event ) { event.Skip(); }
virtual void OnBrowserDiscardButton( wxCommandEvent& event ) { event.Skip(); }

View File

@@ -578,6 +578,35 @@ public:
});
}
/* Called from worker thread only! */
Path ResolveFileConflicts_WT(Path path)
{
do
{
try
{
_filesystem->getDirent(path);
}
catch (const FileNotFoundException& e)
{
break;
}
runOnUiThread(
[&]()
{
FileConflictDialog d(this, wxID_ANY);
d.oldNameText->SetValue(path.to_str());
d.newNameText->SetValue(path.to_str());
if (d.ShowModal() == wxID_OK)
path = Path(d.newNameText->GetValue().ToStdString());
else
path = Path("");
});
} while (!path.empty());
return path;
}
void OnBrowserAddMenuItem(wxCommandEvent&) override
{
Path dirPath;
@@ -616,37 +645,7 @@ public:
QueueBrowserOperation(
[this, path, localPath, item]() mutable
{
/* Check that the file exists. */
for (;;)
{
try
{
_filesystem->getDirent(path);
}
catch (const FileNotFoundException& e)
{
break;
}
runOnUiThread(
[&]()
{
FileConflictDialog d(this, wxID_ANY);
d.oldNameText->SetValue(path.to_str());
d.newNameText->SetValue(path.to_str());
if (d.ShowModal() == wxID_OK)
path = Path(
d.newNameText->GetValue().ToStdString());
else
path = Path("");
});
if (path.empty())
return;
}
/* Now actually write the data. */
path = ResolveFileConflicts_WT(path);
auto bytes = Bytes::readFromFile(localPath);
_filesystem->putFile(path, bytes);
@@ -701,6 +700,38 @@ public:
});
}
void OnBrowserFilenameChanged(wxDataViewEvent& event)
{
if (!(_filesystem->capabilities() & Filesystem::OP_MOVE))
return;
auto node = _filesystemModel->Find(event.GetItem());
if (!node)
return;
if (node->newname.empty())
return;
QueueBrowserOperation(
[this, node]() mutable
{
auto oldpath = node->dirent->path;
auto newpath = oldpath.parent();
newpath.push_back(node->newname);
newpath = ResolveFileConflicts_WT(newpath);
_filesystem->moveFile(oldpath, newpath);
auto dirent = _filesystem->getDirent(newpath);
runOnUiThread(
[&]()
{
_filesystemModel->Delete(oldpath);
_filesystemModel->Add(dirent);
});
});
}
void OnBrowserCommitButton(wxCommandEvent&) override
{
QueueBrowserOperation(