Skip to content

Commit

Permalink
Merge pull request #157 from kishikawakatsumi/switchtree
Browse files Browse the repository at this point in the history
To be able to use different sessions for each share.
  • Loading branch information
kishikawakatsumi authored Nov 12, 2024
2 parents 0e9a675 + ee7efe7 commit fcdfa62
Show file tree
Hide file tree
Showing 17 changed files with 317 additions and 143 deletions.
2 changes: 1 addition & 1 deletion Examples/FileBrowser/FileBrowser (macOS)/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}

let filesViewController = FilesViewController.instantiate(
client: foregroundViewController.client,
accessor: foregroundViewController.treeAccessor,
serverNode: foregroundViewController.serverNode,
share: foregroundViewController.share,
path: foregroundViewController.path,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="32700.99.1234" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="foH-3f-sT7">
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="23094" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="foH-3f-sT7">
<dependencies>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="22689"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="23094"/>
<capability name="Image references" minToolsVersion="12.0"/>
<capability name="NSView safe area layout guides" minToolsVersion="12.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
Expand Down Expand Up @@ -327,7 +327,7 @@ CA
<rect key="frame" x="11" y="0.0" width="237" height="17"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<textField verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="CcC-HU-6xV">
<textField focusRingType="none" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="CcC-HU-6xV">
<rect key="frame" x="0.0" y="2" width="237" height="14"/>
<textFieldCell key="cell" lineBreakMode="truncatingTail" sendsActionOnEndEditing="YES" title="Header Cell" id="CAT-WF-SnN">
<font key="font" metaFont="systemMedium" size="11"/>
Expand Down Expand Up @@ -356,7 +356,7 @@ CA
<stackView distribution="fill" orientation="horizontal" alignment="centerY" horizontalStackHuggingPriority="249.99998474121094" verticalStackHuggingPriority="249.99998474121094" detachesHiddenViews="YES" translatesAutoresizingMaskIntoConstraints="NO" id="6bo-mI-F6h">
<rect key="frame" x="28" y="1" width="209" height="16"/>
<subviews>
<textField horizontalHuggingPriority="249" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="RqO-Me-YMn">
<textField focusRingType="none" horizontalHuggingPriority="249" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="RqO-Me-YMn">
<rect key="frame" x="-2" y="0.0" width="192" height="16"/>
<textFieldCell key="cell" lineBreakMode="truncatingTail" sendsActionOnEndEditing="YES" title="Table View Cell" id="jdy-sX-ayR">
<font key="font" metaFont="system"/>
Expand Down
47 changes: 22 additions & 25 deletions Examples/FileBrowser/FileBrowser (macOS)/DirectoryStructure.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ import SMBClient
class DirectoryStructure {
private let server: String
private let path: String
private let treeAccessor: TreeAccessor

private var tree = Tree<FileNode>()
private var viewTree = Tree<FileNode>()

private var searchText = ""
private var sortDescriptor = NSSortDescriptor(key: "NameColumn", ascending: true)

private let client: SMBClient

var useCache = false {
didSet {
if !useCache {
Expand All @@ -23,10 +22,10 @@ class DirectoryStructure {
}
private var cache = [FileNode: [FileNode]]()

init(server: String, path: String, client: SMBClient) {
init(server: String, path: String, accessor: TreeAccessor) {
self.server = server
self.path = path
self.client = client
treeAccessor = accessor
}

func viewTree(_ tree: Tree<FileNode>) -> Tree<FileNode> {
Expand All @@ -46,18 +45,18 @@ class DirectoryStructure {
return viewTree
}

func reload() async {
let nodes = await listDirectory(path: path, parent: nil)
func reload() async throws {
let nodes = try await listDirectory(path: path, parent: nil)
tree.nodes = nodes

viewTree = viewTree(tree)
}

func reload(directory path: String, _ outlineView: NSOutlineView) async {
func reload(directory path: String, _ outlineView: NSOutlineView) async throws {
if let fileNode = node(ID(path)) {
await expand(fileNode, outlineView)
try await expand(fileNode, outlineView)
} else {
let nodes = await listDirectory(path: path, parent: nil)
let nodes = try await listDirectory(path: path, parent: nil)

tree.nodes = Array(
Set(nodes)
Expand All @@ -71,10 +70,10 @@ class DirectoryStructure {
}
}

func expand(_ fileNode: FileNode, _ outlineView: NSOutlineView) async {
func expand(_ fileNode: FileNode, _ outlineView: NSOutlineView) async throws {
let path = resolvePath(fileNode)

let nodes = await listDirectory(path: path, parent: fileNode)
let nodes = try await listDirectory(path: path, parent: fileNode)
let children = children(of: fileNode)

let (deleted, inserted) = nodeDelta(oldNodes: children, newNodes: nodes)
Expand Down Expand Up @@ -144,6 +143,10 @@ class DirectoryStructure {
viewTree.nodes.count
}

func availableSpace() async throws -> UInt64 {
return try await treeAccessor.availableSpace()
}

func rootNodes() -> [FileNode] {
viewTree.rootNodes()
}
Expand Down Expand Up @@ -201,22 +204,16 @@ class DirectoryStructure {
return false
}

private func listDirectory(path: String, parent: FileNode?) async -> [FileNode] {
do {
let files = try await client.listDirectory(path: path)
.filter { $0.name != "." && $0.name != ".." && !$0.isHidden }
.sorted { $0.name.localizedStandardCompare($1.name) == .orderedAscending }
let nodes = files
.map { FileNode(path: join(path, $0.name), file: $0, parent: parent?.id) }
private func listDirectory(path: String, parent: FileNode?) async throws -> [FileNode] {
let files = try await treeAccessor.listDirectory(path: path)
.filter { $0.name != "." && $0.name != ".." && !$0.isHidden }
.sorted { $0.name.localizedStandardCompare($1.name) == .orderedAscending }
let nodes = files
.map { FileNode(path: join(path, $0.name), file: $0, parent: parent?.id) }

DataRepository.shared.set(join(server, path), nodes: nodes)
DataRepository.shared.set(join(server, path), nodes: nodes)

return nodes
} catch {
NSAlert(error: error).runModal()
}

return DataRepository.shared.nodes(join(server, path)) ?? []
return nodes
}

private func nodeDelta(oldNodes: [FileNode], newNodes: [FileNode]) -> (deleted: [Int], inserted: [Int]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,23 @@ private let storyboardID = "DocumentWindowController"

class DocumentWindowController: NSWindowController, NSWindowDelegate {
private let path: String
private let client: SMBClient
private let fileReader: FileReader

private let server: HTTPServer
private let port: UInt16
private var task: Task<(), any Error>?
private let semaphore = Semaphore(value: 1)

static func instantiate(path: String, client: SMBClient) -> Self {
static func instantiate(path: String, accessor: TreeAccessor) -> Self {
let storyboard = NSStoryboard(name: storyboardID, bundle: nil)
return storyboard.instantiateController(identifier: storyboardID) { (coder) in
Self(coder: coder, path: path, client: client)
Self(coder: coder, path: path, accessor: accessor)
}
}

required init?(coder: NSCoder, path: String, client: SMBClient) {
required init?(coder: NSCoder, path: String, accessor: TreeAccessor) {
self.path = path
self.client = client
fileReader = client.fileReader(path: path)
fileReader = accessor.fileReader(path: path)

port = UInt16(42000 + NSApp.windows.count)
server = HTTPServer(port: port, logger: .disabled)
Expand Down
12 changes: 6 additions & 6 deletions Examples/FileBrowser/FileBrowser (macOS)/FileTransfer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ class FileUpload: FileTransfer {
let id: UUID
private let source: URL
private let destination: String
private let client: SMBClient
private let treeAccessor: TreeAccessor

init(source: URL, destination: String, client: SMBClient) {
init(source: URL, destination: String, accessor: TreeAccessor) {
id = UUID()
self.source = source
self.destination = destination
self.client = client
treeAccessor = accessor

displayName = source.lastPathComponent
state = .queued
Expand All @@ -60,7 +60,7 @@ class FileUpload: FileTransfer {
state = .started(transferProgress)
progressHandler(state)

try await client.upload(localPath: source, remotePath: destination) { (completedFiles, fileBeingTransferred, bytesSent) in
try await treeAccessor.upload(localPath: source, remotePath: destination) { (completedFiles, fileBeingTransferred, bytesSent) in
transferProgress = .directory(completedFiles: completedFiles, fileBeingTransferred: fileBeingTransferred, bytesSent: bytesSent)
state = .started(transferProgress)
progressHandler(state)
Expand All @@ -78,7 +78,7 @@ class FileUpload: FileTransfer {
state = .started(transferProgress)
progressHandler(state)

try await client.upload(fileHandle: fileHandle, path: destination) { (progress) in
try await treeAccessor.upload(fileHandle: fileHandle, path: destination) { (progress) in
transferProgress = .file(progress: progress, numberOfBytes: numberOfBytes)
state = .started(.file(progress: progress, numberOfBytes: numberOfBytes))
progressHandler(state)
Expand All @@ -98,7 +98,7 @@ class FileUpload: FileTransfer {
name: Self.didFinish,
object: self,
userInfo: [
FileUploadUserInfoKey.share: client.share ?? "",
FileUploadUserInfoKey.share: treeAccessor.share ?? "",
FileUploadUserInfoKey.path: destination,
]
)
Expand Down
Loading

0 comments on commit fcdfa62

Please sign in to comment.