// // P4DepotArea.swift // DocHub // // Created by Tristan Juricek on 6/24/14. // Copyright (c) 2014 Perforce. All rights reserved. // import Foundation class P4DepotArea : NSObject, Area { var p4Library: P4Library var p4Depot: P4Depot init(p4Library p4Lib:P4Library, p4Depot p4Dep:P4Depot) { p4Library = p4Lib p4Depot = p4Dep super.init() // TODO I think I hit a bug with the compiler which causes me to avoid // using lazy initialization here children = loadChildren() files = loadFiles() sorted = sortItems() } var name:String { return p4Depot.name } var library: Library { return p4Library } var children: Directory[] = [] func loadChildren() -> Directory[] { var depotDirs = Array() let depotPath = "\(p4Depot.depotPath)/*" p4Library.withConnection({ (api:P4ClientApi) -> () in let (err, dirs) = api.dirs(depotPath) if let e = err { NSException(name: "DirectoryListFailed", reason: e.localizedDescription, userInfo: nil).raise() } for p4dir in dirs { depotDirs.append(P4DepotDir(depotArea:self, dir:p4dir)) } }) return depotDirs } var files: File[] = [] func loadFiles() -> File[] { var depotFiles = Array() let depotPath = "\(p4Depot.depotPath)/*" p4Library.withConnection({ (api:P4ClientApi) -> () in let (err, p4Files) = api.files(depotPath) if let e = err { NSException(name: "FileListFailed", reason: e.localizedDescription, userInfo: nil).raise() } for p4file in p4Files { let file = P4DepotFile(depotArea:self, p4File:p4file) depotFiles.append(file) } }) return depotFiles } var sorted:Namable[] = [] func sortItems() -> Namable[] { var namables = Array() for child in children { namables.append(child) } for file in files { namables.append(file) } let newArr : Array = sort(namables, { (n1, n2) -> Bool in return n1.name.compare(n2.name) < 0 }) return newArr } func itemAt(index:Int) -> Namable { return sorted[index] } }