diff --git a/Base.lproj/PrefsWindowController.xib b/Base.lproj/PrefsWindowController.xib index 416e79a..9af8607 100755 --- a/Base.lproj/PrefsWindowController.xib +++ b/Base.lproj/PrefsWindowController.xib @@ -1,8 +1,8 @@ - + - + @@ -13,14 +13,14 @@ - + - - + + - + @@ -46,6 +46,11 @@ + + + + + @@ -57,6 +62,7 @@ + @@ -64,11 +70,12 @@ - + + diff --git a/Classes/OCRManager.swift b/Classes/OCRManager.swift new file mode 100644 index 0000000..5f1d5c3 --- /dev/null +++ b/Classes/OCRManager.swift @@ -0,0 +1,153 @@ +// +// OCRManager.swift +// Subler +// +// Created by Damiano Galassi on 30/05/2019. +// + +import Foundation +import MP42Foundation + +final class OCRLanguage { + + enum Status { + case available + case downloading + case downloaded + } + + private(set) var status: Status + + let langCode: String + let name: String + + init(langCode: String, name: String) { + self.status = .available + self.langCode = langCode + self.name = name + } + + var displayName: String { + get { + return OCRLanguage.langManager.localizedLang(forExtendedTag: langCode) + } + } + + fileprivate func checkIfDownloaded(_ to: URL) { + if (FileManager.default.fileExists(atPath: to.appendingPathComponent(name, isDirectory: false).path)) { + status = .downloaded + } else { + status = .available + } + } + + private var task: URLSessionDownloadTask? + + fileprivate func startDownload(from remoteURL: URL, to localURL: URL, completionHandler: @escaping (Bool) -> Void) -> URLSessionTask? { + let sessionConfig = URLSessionConfiguration.default + let session = URLSession(configuration: sessionConfig) + let request = URLRequest(url: remoteURL.appendingPathComponent(name)) + + let task = session.downloadTask(with: request) { (tempLocalUrl, response, error) in + if let tempLocalUrl = tempLocalUrl, error == nil { + // Success +// if let statusCode = (response as? HTTPURLResponse)?.statusCode { +// print("Success: \(statusCode)") +// } + + do { + try FileManager.default.copyItem(at: tempLocalUrl, to: localURL.appendingPathComponent(self.name, isDirectory: false)) + self.status = .downloaded + self.task = nil + completionHandler(true) + } catch { + self.status = . available + self.task = nil + completionHandler(false) +// print("error writing file \(localURL) : \(writeError)") + } + + } else { + self.status = .available + self.task = nil + completionHandler(false) +// print("Failure: \(String(describing: error?.localizedDescription))") + } + } + + self.task = task + self.status = .downloading + + task.resume() + return task + } + + fileprivate func cancel() { + guard let task = task else { return } + task.cancel() + } + + fileprivate func remove(_ from: URL) throws { + try FileManager.default.removeItem(at: from.appendingPathComponent(name, isDirectory: false)) + status = .available + } + + private static let langManager = MP42Languages.defaultManager +} + +final class OCRManager { + static let shared = OCRManager() + + static let updateNotification = Notification.Name(rawValue: "OCRManagerUpdatedNotification") + + let languages: [OCRLanguage] = [OCRLanguage(langCode: "af", name: "afr.traineddata"), + OCRLanguage(langCode: "am", name: "amh.traineddata"), + OCRLanguage(langCode: "ar", name: "ara.traineddata"), + OCRLanguage(langCode: "as", name: "asm.traineddata"), + OCRLanguage(langCode: "it", name: "ita.traineddata")] + + private init() { + try? load() + } + + // MARK: management + + private func postNotification() { + NotificationCenter.default.post(name: OCRManager.updateNotification, object: self) + } + + private func destinationURL() -> URL? { + return FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first?.appendingPathComponent("Subler", isDirectory: true).appendingPathComponent("tessdata", isDirectory: true) + } + + private func baseURL() -> URL? { + return URL(string: "https://github.com/tesseract-ocr/tessdata_fast/raw/master/") + } + + func download(item: OCRLanguage) { + if let baseURL = baseURL(), let destURL = destinationURL() { + _ = item.startDownload(from: baseURL, to: destURL, completionHandler: { _ in self.postNotification() }) + postNotification() + } + } + + func cancelDownload(item: OCRLanguage) { + item.cancel() + } + + func removeDownload(item: OCRLanguage) throws { + if let destURL = destinationURL() { + try item.remove(destURL) + postNotification() + } + } + + // MARK: read/write + + private func load() throws { + if let destinationURL = destinationURL() { + languages.forEach { $0.checkIfDownloaded(destinationURL) } + } + } + +} diff --git a/Classes/OCRPrefsViewController.swift b/Classes/OCRPrefsViewController.swift new file mode 100644 index 0000000..96dfd36 --- /dev/null +++ b/Classes/OCRPrefsViewController.swift @@ -0,0 +1,97 @@ +// +// OCRPrefsViewController.swift +// Subler +// +// Created by Damiano Galassi on 30/05/2019. +// + +import Cocoa + +class OCRTableCellView : NSTableCellView { + + var item: OCRLanguage? { + didSet { + guard let item = item else { return } + + textField?.stringValue = item.displayName + + switch item.status { + case .available: + actionButton.title = NSLocalizedString("Download", comment: "") + case .downloading: + actionButton.title = NSLocalizedString("Cancel Download", comment: "") + case .downloaded: + actionButton.title = NSLocalizedString("Remove", comment: "") + } + } + } + + @IBOutlet var actionButton: NSButton! + + @IBAction func buttonPressed(_ sender: Any) { + guard let item = item else { return } + + switch item.status { + case .available: + OCRManager.shared.download(item: item) + case .downloading: + OCRManager.shared.cancelDownload(item: item) + case .downloaded: + try! OCRManager.shared.removeDownload(item: item) + } + } + +} + +class OCRPrefsViewController: NSViewController, NSTableViewDataSource, NSTableViewDelegate { + + private let manager = OCRManager.shared + + @IBOutlet var tableView: NSTableView! + var observer: Any? + + init() { + super.init(nibName: nil, bundle: nil) + self.title = NSLocalizedString("OCR", comment: "") + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + override var nibName: NSNib.Name? { + return "OCRPrefsViewController" + } + + override func viewDidLoad() { + super.viewDidLoad() + + tableView.reloadData() + observer = NotificationCenter.default.addObserver(forName: OCRManager.updateNotification, + object: nil, + queue: OperationQueue.main) { [weak self] notification in + guard let s = self else { return } + s.tableView.reloadData() + } + } + + // MARK: Table View + + func numberOfRows(in tableView: NSTableView) -> Int { + return manager.languages.count + } + + private let languageItemColumn = NSUserInterfaceItemIdentifier(rawValue: "languageItem") + + func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? { + let item = manager.languages[row] + + if let cell = tableView.makeView(withIdentifier: languageItemColumn, owner: self) as? OCRTableCellView { + cell.item = item + return cell + } + + return nil + } + +} diff --git a/Classes/OCRPrefsViewController.xib b/Classes/OCRPrefsViewController.xib new file mode 100644 index 0000000..c0c5f8e --- /dev/null +++ b/Classes/OCRPrefsViewController.xib @@ -0,0 +1,115 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Classes/PrefsWindowController.swift b/Classes/PrefsWindowController.swift index 73c3def..7bf0136 100644 --- a/Classes/PrefsWindowController.swift +++ b/Classes/PrefsWindowController.swift @@ -88,12 +88,14 @@ class PrefsWindowController: NSWindowController, NSWindowDelegate { lazy var metadataController: MetadataPrefsViewController = { return MetadataPrefsViewController() }() lazy var presetController: PresetPrefsViewController = { return PresetPrefsViewController() }() lazy var outputController: OutputPrefsViewController = { return OutputPrefsViewController() }() + lazy var ocrController: OCRPrefsViewController = { return OCRPrefsViewController() }() lazy var advancedController: AdvancedPrefsViewController = { return AdvancedPrefsViewController() }() let general: NSToolbarItem.Identifier = NSToolbarItem.Identifier("TOOLBAR_GENERAL") let metadata: NSToolbarItem.Identifier = NSToolbarItem.Identifier("TOOLBAR_METADATA") let presets: NSToolbarItem.Identifier = NSToolbarItem.Identifier("TOOLBAR_SETS") let output: NSToolbarItem.Identifier = NSToolbarItem.Identifier("TOOLBAR_OUTPUT") + let ocr: NSToolbarItem.Identifier = NSToolbarItem.Identifier("TOOLBAR_OCR") let advanced: NSToolbarItem.Identifier = NSToolbarItem.Identifier("TOOLBAR_ADVANCED") // MARK: Panel switching @@ -103,6 +105,7 @@ class PrefsWindowController: NSWindowController, NSWindowDelegate { if identifier == metadata { return metadataController.view } if identifier == presets { return presetController.view } if identifier == output { return outputController.view } + if identifier == ocr { return ocrController.view } if identifier == advanced { return advancedController.view } return nil } diff --git a/Subler.xcodeproj/project.pbxproj b/Subler.xcodeproj/project.pbxproj index 37e57c7..afc3548 100644 --- a/Subler.xcodeproj/project.pbxproj +++ b/Subler.xcodeproj/project.pbxproj @@ -61,6 +61,7 @@ A9676A341FB6EAAD00C1E300 /* ItemViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A9676A331FB6EAAD00C1E300 /* ItemViewController.swift */; }; A979B3862025C8BF00468362 /* DocumentWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A979B3842025C8BF00468362 /* DocumentWindowController.swift */; }; A97C3AA5204F18C90007AD38 /* TokenCellView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A97C3AA4204F18C90007AD38 /* TokenCellView.swift */; }; + A97D99DA22A0240C001FA3A7 /* OCRManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = A97D99D922A0240C001FA3A7 /* OCRManager.swift */; }; A97FBE3C21FC7F1C00F1FD35 /* QueueController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A97FBE3B21FC7F1C00F1FD35 /* QueueController.swift */; }; A983E80B22365B5400221BAE /* MovieViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A983E80A22365B5400221BAE /* MovieViewController.swift */; }; A984DB740F52DD4B001A20B2 /* dsa_pub.pem in Resources */ = {isa = PBXBuildFile; fileRef = A984DB730F52DD4B001A20B2 /* dsa_pub.pem */; }; @@ -115,6 +116,8 @@ A9CBAB1E1F29C083001C26B3 /* MetadataImporter.swift in Sources */ = {isa = PBXBuildFile; fileRef = A9CBAB1D1F29C083001C26B3 /* MetadataImporter.swift */; }; A9D4B8671C05BD04007845C4 /* SBChapterSearch.xib in Resources */ = {isa = PBXBuildFile; fileRef = A9D4B8651C05BD04007845C4 /* SBChapterSearch.xib */; }; A9D64527202CA92400BA172B /* SaveOptions.swift in Sources */ = {isa = PBXBuildFile; fileRef = A9D64525202CA92400BA172B /* SaveOptions.swift */; }; + A9DA03E322A0211C0056AE7B /* OCRPrefsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A9DA03E122A0211C0056AE7B /* OCRPrefsViewController.swift */; }; + A9DA03E422A0211C0056AE7B /* OCRPrefsViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = A9DA03E222A0211C0056AE7B /* OCRPrefsViewController.xib */; }; A9DA3B9C1B918D5C00F1E40C /* SBApplication.m in Sources */ = {isa = PBXBuildFile; fileRef = A9DA3B991B918D5C00F1E40C /* SBApplication.m */; }; A9DA3BA31B918D6F00F1E40C /* ExceptionAlert.xib in Resources */ = {isa = PBXBuildFile; fileRef = A9DA3BA11B918D6F00F1E40C /* ExceptionAlert.xib */; }; A9E97F141F274F1100DBA586 /* iTunesStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = A9E97F131F274F1100DBA586 /* iTunesStore.swift */; }; @@ -283,6 +286,7 @@ A96EA9E921FA47520033EC5F /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = ""; }; A979B3842025C8BF00468362 /* DocumentWindowController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = DocumentWindowController.swift; path = Classes/DocumentWindowController.swift; sourceTree = ""; }; A97C3AA4204F18C90007AD38 /* TokenCellView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = TokenCellView.swift; path = Classes/TokenCellView.swift; sourceTree = ""; }; + A97D99D922A0240C001FA3A7 /* OCRManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = OCRManager.swift; path = Classes/OCRManager.swift; sourceTree = ""; }; A97FBE3B21FC7F1C00F1FD35 /* QueueController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = QueueController.swift; path = Classes/QueueController.swift; sourceTree = ""; }; A983E80A22365B5400221BAE /* MovieViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = MovieViewController.swift; path = Classes/PropertyView/MovieViewController.swift; sourceTree = ""; }; A984DB4A0F52D644001A20B2 /* Sparkle.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = Sparkle.framework; sourceTree = ""; }; @@ -351,6 +355,8 @@ A9C73535206B8F9900794673 /* QueuePreferences.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = QueuePreferences.swift; path = Classes/QueuePreferences.swift; sourceTree = ""; }; A9CBAB1D1F29C083001C26B3 /* MetadataImporter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = MetadataImporter.swift; path = Classes/MetadataImporters/MetadataImporter.swift; sourceTree = ""; }; A9D64525202CA92400BA172B /* SaveOptions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = SaveOptions.swift; path = Classes/SaveOptions.swift; sourceTree = ""; }; + A9DA03E122A0211C0056AE7B /* OCRPrefsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = OCRPrefsViewController.swift; path = Classes/OCRPrefsViewController.swift; sourceTree = ""; }; + A9DA03E222A0211C0056AE7B /* OCRPrefsViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = OCRPrefsViewController.xib; path = Classes/OCRPrefsViewController.xib; sourceTree = ""; }; A9DA3B991B918D5C00F1E40C /* SBApplication.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = SBApplication.m; path = Classes/SBApplication.m; sourceTree = ""; }; A9DA3BA01B918D6200F1E40C /* SBApplication.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SBApplication.h; path = Classes/SBApplication.h; sourceTree = ""; }; A9E97F131F274F1100DBA586 /* iTunesStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = iTunesStore.swift; path = Classes/MetadataImporters/iTunesStore.swift; sourceTree = ""; }; @@ -545,6 +551,9 @@ A90CD5C71FD177280070A067 /* OutputPrefsViewController.xib */, A90C3E0A1FD16AF200F64532 /* TokensViewController.swift */, A90CD5C21FD177230070A067 /* TokensViewController.xib */, + A97D99D922A0240C001FA3A7 /* OCRManager.swift */, + A9DA03E122A0211C0056AE7B /* OCRPrefsViewController.swift */, + A9DA03E222A0211C0056AE7B /* OCRPrefsViewController.xib */, A99D23202029CD32002E7F27 /* PrefsWindow.swift */, ); name = Preferences; @@ -842,6 +851,7 @@ A998DABF1BA54CFF004D7969 /* MediaTagsController.xib in Resources */, A98542D720D197D300EB9803 /* ArtworkSelector.xib in Resources */, A9ECA0602029A13800D5F30C /* OffsetViewController.xib in Resources */, + A9DA03E422A0211C0056AE7B /* OCRPrefsViewController.xib in Resources */, A961476713F568210068F161 /* Queue.xib in Resources */, A9BA1AF31DBF7DD800FD0B38 /* Localizable.strings in Resources */, A9ECA0572029A10B00D5F30C /* DocumentWindowController.xib in Resources */, @@ -876,6 +886,8 @@ A98D6BAB21FDC62800F0D886 /* Queue.swift in Sources */, A98542DC20D1A7E500EB9803 /* ArtworkSelectorViewItem.swift in Sources */, A90C3E0C1FD16AF200F64532 /* TokensViewController.swift in Sources */, + A97D99DA22A0240C001FA3A7 /* OCRManager.swift in Sources */, + A9DA03E322A0211C0056AE7B /* OCRPrefsViewController.swift in Sources */, A9C09C7C1F99F460007939EE /* ExceptionAlertController.swift in Sources */, A9A91B5D1FB6FBB700250D93 /* ChapterViewController.swift in Sources */, A9676A301FB6E85300C1E300 /* LanguageTransformer.swift in Sources */,