/
MainWindowController.swift
309 lines (244 loc) · 12.1 KB
/
MainWindowController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
//
// MainWindowController.swift
// Subtitlr
//
// Created by Lucija Frković on 11/10/15.
// Copyright © 2015 Lucija Frković. All rights reserved.
//
import Cocoa
class MainWindowController: NSWindowController, NSTableViewDataSource, NSTableViewDelegate {
@IBOutlet weak var dropzone: NSView!
@IBOutlet weak var tableView: NSTableView!
@IBOutlet weak var tableContainer: NSView!
@IBOutlet weak var containerView: NSView!
@IBOutlet weak var dropzoneImage: NSImageView!
let popover = NSPopover()
var showDropzone = true
var videos = [Video]()
override var windowNibName: String {
return "MainWindowController"
}
override func windowDidLoad() {
super.windowDidLoad()
self.window!.registerForDraggedTypes([NSFilenamesPboardType])
//color the container view white, so the dropzone gets white as well
containerView.wantsLayer = true
containerView.layer?.backgroundColor = NSColor.whiteColor().CGColor
//unregister NSImageView in dropzone view from receiving dragging operation
//otherwise it interferes with dragging files to the whole dropzone area
dropzoneImage.unregisterDraggedTypes()
tableView.setDelegate(self)
//bind popover to its controller
popover.contentViewController = PopoverController()
popover.behavior = NSPopoverBehavior.Transient
}
func draggingEntered(sender: NSDraggingInfo) -> NSDragOperation {
return .Copy
}
func performDragOperation(sender: NSDraggingInfo) -> Bool {
if let board = sender.draggingPasteboard().propertyListForType("NSFilenamesPboardType") as? NSArray {
if let filePaths = board as? [String] {
//geting the path of dropped file
for path in filePaths {
//only videos are allowed
if Constants.fileTypes.contains((path as NSString).pathExtension.lowercaseString){
videos.append(Video(path: path))
}
}
if videos.count > 0 {
checkAndSwitchToTableView()
tableView.reloadData()
}
return true
}
}
return false
}
//Switches to table view if dropzone is active
func checkAndSwitchToTableView() {
if showDropzone {
showDropzone = false
//remove dropzone and show table with files
containerView.animator().replaceSubview(dropzone, with: tableContainer)
tableContainer!.hidden = false
}
}
func concludeDragOperation(sender: NSDraggingInfo) {
getSubtitlesForDroppedFiles()
}
//counters to help with displaying proper notification info
var videoFoundCount = 0
var videoNotFoundCount = 0
var videoNotSearchedCount = 0
func getSubtitlesForDroppedFiles(){
for (index, video) in videos.enumerate() {
if video.subStatus == Video.SubtitleStauts.NotSearched {
videoNotSearchedCount += 1
// Subtitle search and download is done in a separate thread on a lower priority than
// table reload. Otherwise, the application visually gets "stuck" until all subs are downloaded
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
video.downloadSubtitle()
dispatch_async(dispatch_get_main_queue()){
if video.subStatus == Video.SubtitleStauts.Found {
self.videoFoundCount += 1
}
else if video.subStatus == Video.SubtitleStauts.NotFound {
self.videoNotFoundCount += 1
}
//refresh only the first (status) and third(finder icon/link to manual search) columns
//of the table
let columns = NSMutableIndexSet()
columns.addIndex(0)
columns.addIndex(2)
self.tableView.reloadDataForRowIndexes(NSIndexSet(index: index), columnIndexes: columns)
//if all videos are searched for, display notification
if ((self.videoFoundCount + self.videoNotFoundCount) == self.videoNotSearchedCount){
self.notifyUser()
self.resetSearchResultCounters()
}
}
})
}
}
}
func resetSearchResultCounters(){
videoNotSearchedCount = 0
videoNotFoundCount = 0
videoFoundCount = 0
}
func notifyUser(){
let notify = NSUserNotification()
notify.title = "Finished searching for subtitles"
notify.subtitle = "Subtitles found for \(videoFoundCount) of \(videoNotSearchedCount) videos"
NSUserNotificationCenter.defaultUserNotificationCenter().deliverNotification(notify)
}
func numberOfRowsInTableView(tableView: NSTableView) -> Int {
return videos.count
}
//prevents row selection
func tableView(tableView: NSTableView, selectionIndexesForProposedSelection proposedSelectionIndexes: NSIndexSet) -> NSIndexSet {
return NSIndexSet();
}
//Resolve table column values
func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView? {
//middle column showing video path, middle of text is truncated to fit the cell
if tableColumn?.identifier == "fileName" {
var fileName: NSTextField? = tableView.makeViewWithIdentifier("fileName", owner: self) as? NSTextField
if fileName == nil {
fileName = NSTextField(frame: NSRect())
fileName!.identifier = "fileName"
fileName?.lineBreakMode = NSLineBreakMode.ByTruncatingMiddle
}
fileName?.stringValue = videos[row].fileName
fileName?.bezeled = false
fileName?.bordered = false
fileName?.selectable = false
fileName?.backgroundColor = NSColor.clearColor()
return fileName
}
else if tableColumn?.identifier == "findSubtitle" {
var findSubtitle: NSButton? = tableView.makeViewWithIdentifier("finder", owner: self) as? NSButton
if findSubtitle == nil {
findSubtitle = NSButton(frame: NSRect())
findSubtitle!.identifier = "findSubtitle"
}
findSubtitle?.bordered = false
findSubtitle?.setButtonType(NSButtonType.MomentaryChangeButton)
findSubtitle?.bezelStyle = NSBezelStyle.RoundedDisclosureBezelStyle
findSubtitle?.imagePosition = NSCellImagePosition.ImageOnly
findSubtitle?.target = self
findSubtitle?.tag = row
if videos[row].subStatus == Video.SubtitleStauts.Found {
findSubtitle?.image = NSImage(named: NSImageNameRevealFreestandingTemplate)
findSubtitle?.toolTip = "Show subtitle in Finder"
findSubtitle?.action = "revealInFinder:"
return findSubtitle
}
//show direct search by filename on opensubtitles
else if videos[row].subStatus == Video.SubtitleStauts.NotFound {
findSubtitle?.image = NSImage(named: NSImageNameShareTemplate)
findSubtitle?.bordered = false
findSubtitle?.toolTip = "Search in browser on OpenSubtitles.org"
findSubtitle?.action = "searchOpenSubs:"
return findSubtitle
}
}
else if tableColumn?.identifier == "status" {
if (videos[row].subStatus == Video.SubtitleStauts.NotSearched) {
var spinner: NSProgressIndicator? = tableView.makeViewWithIdentifier("status", owner: self) as? NSProgressIndicator
if spinner == nil {
spinner = NSProgressIndicator(frame: NSRect())
spinner!.identifier = "status"
}
spinner?.style = NSProgressIndicatorStyle.SpinningStyle
spinner?.controlSize = NSControlSize.SmallControlSize
spinner?.startAnimation(nil)
spinner?.bezeled = false
spinner?.toolTip = "Searching subtitles..."
return spinner
}
else {
var status: NSTextField? = tableView.makeViewWithIdentifier("status", owner: self) as? NSTextField
if status == nil {
status = NSTextField(frame: NSRect())
status!.identifier = "status"
}
status?.stringValue = videos[row].subStatus == Video.SubtitleStauts.Found ? "✓" : "x"
status?.font = videos[row].subStatus == Video.SubtitleStauts.Found ? NSFont(name: "Devanagari MT", size: 12): NSFont(name: "Zapf Dingbats", size: 12)
status?.alignment = NSTextAlignment.Center
status?.bezeled = false
status?.bordered = false
status?.selectable = false
status?.backgroundColor = NSColor.clearColor()
status?.toolTip = videos[row].subStatus == Video.SubtitleStauts.Found ? "Subtitle was found and downloaded to video location.": "Subtitle was not found."
return status
}
}
return nil
}
/* Show subtitle in finder.
Finder icon is assigned a tag value corresponding to matching file's array index
*/
func revealInFinder(sender: NSButton){
let url = NSURL.fileURLWithPath(videos[sender.tag].subtitle!.path)
NSWorkspace.sharedWorkspace().activateFileViewerSelectingURLs([url])
}
func searchOpenSubs(sender: NSButton){
let lang = NSUserDefaults.standardUserDefaults().valueForKey(Constants.Keys.subtitleLanguage) as! String
let urlString = "http://www.opensubtitles.org/en/search2/sublanguageid-\(lang)/moviename-\(videos[sender.tag].fileName)"
let safeUrl = urlString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!
let url = NSURL(string: safeUrl)
NSWorkspace.sharedWorkspace().openURL(url!)
}
//opens or closes settings popover
@IBAction func togglePopover(sender: NSButton) {
if popover.shown {
popover.close()
}
else {
popover.showRelativeToRect( sender.bounds, ofView: sender, preferredEdge: NSRectEdge(rawValue: 3)!)
}
}
@IBAction func selectVideoFilesFromFinder(sender: NSButton) {
sender.enabled = false
let openPanel = NSOpenPanel()
openPanel.allowsMultipleSelection = true
openPanel.allowedFileTypes = Constants.fileTypes
openPanel.allowsOtherFileTypes = false
openPanel.canChooseFiles = true
openPanel.canChooseDirectories = false
openPanel.canCreateDirectories = false
openPanel.allowsConcurrentViewDrawing = false
openPanel.beginWithCompletionHandler{result -> Void in
if result == NSFileHandlingPanelOKButton {
for file in openPanel.URLs {
self.videos.append(Video(path: file.path!))
self.checkAndSwitchToTableView()
}
self.tableView.reloadData()
self.getSubtitlesForDroppedFiles()
}
sender.enabled = true
}
}
}