wxTreeListItem SFTPTreeView::DoAddFolder(const wxTreeListItem& parent, const wxString& path) { try { m_sftp->CreateDir(path); SFTPAttribute::Ptr_t attr = m_sftp->Stat(path); // Update the UI MyClientData* newCd = new MyClientData(path); newCd->SetIsFolder(true); newCd->SetInitialized(false); wxTreeListItem child = m_treeListCtrl->AppendItem( parent, newCd->GetFullName(), m_bmpLoader.GetMimeImageId(FileExtManager::TypeFolder), wxNOT_FOUND, newCd); if(IsTypeColumnShown()) { m_treeListCtrl->SetItemText(child, GetTypeColumnIndex(), attr->GetTypeAsString()); } if(IsSizeColumnShown()) { m_treeListCtrl->SetItemText(child, GetSizeColumnIndex(), wxString() << attr->GetSize()); } m_treeListCtrl->AppendItem(child, "<dummy>"); m_treeListCtrl->SetSortColumn(0); return child; } catch(clException& e) { ::wxMessageBox(e.What(), "SFTP", wxICON_ERROR | wxOK | wxCENTER); } return wxTreeListItem(); }
wxTreeListItem SFTPTreeView::DoAddFile(const wxTreeListItem& parent, const wxString& path) { try { m_sftp->Write("", path); SFTPAttribute::Ptr_t attr = m_sftp->Stat(path); // Update the UI MyClientData* newFile = new MyClientData(path); newFile->SetIsFolder(false); newFile->SetInitialized(false); wxTreeListItem child = m_treeListCtrl->AppendItem( parent, newFile->GetFullName(), m_bmpLoader.GetMimeImageId(FileExtManager::GetType(path, FileExtManager::TypeText)), wxNOT_FOUND, newFile); m_treeListCtrl->SetItemText(child, 1, attr->GetTypeAsString()); m_treeListCtrl->SetItemText(child, 2, wxString() << attr->GetSize()); m_treeListCtrl->SetSortColumn(0); return child; } catch(clException& e) { ::wxMessageBox(e.What(), "SFTP", wxICON_ERROR | wxOK | wxCENTER); } return wxTreeListItem(); }
void clSFTP::Read(const wxString& remotePath, wxMemoryBuffer& buffer) throw(clException) { if(!m_sftp) { throw clException("SFTP is not initialized"); } sftp_file file = sftp_open(m_sftp, remotePath.mb_str(wxConvUTF8).data(), O_RDONLY, 0); if(file == NULL) { throw clException(wxString() << _("Failed to open remote file: ") << remotePath << ". " << ssh_get_error(m_ssh->GetSession()), sftp_get_error(m_sftp)); } SFTPAttribute::Ptr_t fileAttr = Stat(remotePath); if(!fileAttr) { throw clException(wxString() << _("Could not stat file:") << remotePath << ". " << ssh_get_error(m_ssh->GetSession()), sftp_get_error(m_sftp)); } wxInt64 fileSize = fileAttr->GetSize(); if(fileSize == 0) return; // Allocate buffer for the file content char pBuffer[65536]; // buffer // Read the entire file content wxInt64 bytesLeft = fileSize; wxInt64 bytesRead = 0; while(bytesLeft > 0) { wxInt64 nbytes = sftp_read(file, pBuffer, sizeof(pBuffer)); bytesRead += nbytes; bytesLeft -= nbytes; buffer.AppendData(pBuffer, nbytes); } if(bytesRead != fileSize) { sftp_close(file); buffer.Clear(); throw clException(wxString() << _("Could not read file:") << remotePath << ". " << ssh_get_error(m_ssh->GetSession()), sftp_get_error(m_sftp)); } sftp_close(file); }
bool SFTPTreeView::DoExpandItem(const wxTreeListItem& item) { MyClientData* cd = GetItemData(item); CHECK_PTR_RET_FALSE(cd); // already initialized this folder before? if(cd->IsInitialized()) { return true; } // get list of files and populate the tree SFTPAttribute::List_t attributes; try { attributes = m_sftp->List(cd->GetFullPath(), clSFTP::SFTP_BROWSE_FILES | clSFTP::SFTP_BROWSE_FOLDERS); } catch(clException& e) { ::wxMessageBox(e.What(), "SFTP", wxOK | wxICON_ERROR | wxCENTER, EventNotifier::Get()->TopFrame()); return false; } // Remove the dummy item and replace it with real items wxTreeListItem dummyItem = m_treeListCtrl->GetFirstChild(item); m_treeListCtrl->DeleteItem(dummyItem); cd->SetInitialized(true); int nNumOfRealChildren = 0; SFTPAttribute::List_t::iterator iter = attributes.begin(); for(; iter != attributes.end(); ++iter) { SFTPAttribute::Ptr_t attr = (*iter); if(attr->GetName() == "." || attr->GetName() == "..") continue; ++nNumOfRealChildren; // determine the icon index int imgIdx = wxNOT_FOUND; if(attr->IsFolder()) { imgIdx = m_bmpLoader.GetMimeImageId(FileExtManager::TypeFolder); } else { imgIdx = m_bmpLoader.GetMimeImageId(attr->GetName()); } if(imgIdx == wxNOT_FOUND) { imgIdx = m_bmpLoader.GetMimeImageId(FileExtManager::TypeText); } wxString path; path << cd->GetFullPath() << "/" << attr->GetName(); while(path.Replace("//", "/")) { } MyClientData* childClientData = new MyClientData(path); childClientData->SetIsFolder(attr->IsFolder()); wxTreeListItem child = m_treeListCtrl->AppendItem(item, attr->GetName(), imgIdx, imgIdx, childClientData); if(IsTypeColumnShown()) { m_treeListCtrl->SetItemText(child, GetTypeColumnIndex(), attr->GetTypeAsString()); } if(IsSizeColumnShown()) { m_treeListCtrl->SetItemText(child, GetSizeColumnIndex(), wxString() << attr->GetSize()); } // if its type folder, add a fake child item if(attr->IsFolder()) { m_treeListCtrl->AppendItem(child, "<dummy>"); } } return nNumOfRealChildren > 0; }