Archived
1
This repository has been archived on 2020-06-04. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
tagtunes/TagTunes/Preference Controllers.swift
Kim Wittenburg 257a7811d6 Implemented censored names preference
Added case sensitivity preference
Integer tags are not clearable anymore
2015-09-14 00:43:01 +02:00

143 lines
5.2 KiB
Swift

//
// PreferencesTabViewController.swift
// Harmony
//
// Created by Kim Wittenburg on 26.01.15.
// Copyright (c) 2015 Das Code Kollektiv. All rights reserved.
//
import Cocoa
internal class GeneralPreferencesViewController: NSViewController {
// MARK: IBOutlets
@IBOutlet internal weak var artworkPathControl: NSPathControl!
@IBOutlet internal weak var chooseArtworkButton: NSButton!
override internal func viewDidLoad() {
super.viewDidLoad()
artworkPathControl.URL = Preferences.sharedPreferences.artworkTarget
saveArtworkStateChanged(self)
}
@IBAction internal func saveArtworkStateChanged(sender: AnyObject) {
artworkPathControl.enabled = Preferences.sharedPreferences.saveArtwork
chooseArtworkButton.enabled = Preferences.sharedPreferences.saveArtwork
}
@IBAction internal func chooseArtworkPath(sender: AnyObject) {
let openPanel = NSOpenPanel()
openPanel.canChooseDirectories = true
openPanel.canChooseFiles = false
openPanel.canCreateDirectories = true
openPanel.prompt = NSLocalizedString("Choose…", comment: "Button title prompting the user to choose a directory")
openPanel.beginSheetModalForWindow(view.window!) {
result in
if result == NSModalResponseOK {
Preferences.sharedPreferences.artworkTarget = openPanel.URL!.filePathURL!
self.artworkPathControl.URL = openPanel.URL
}
}
}
}
internal class TagsPreferencesViewController: NSViewController, NSTableViewDataSource, NSTableViewDelegate, NSMenuDelegate {
// MARK: Types
private struct TableViewConstants {
static let textTableCellViewIdentifier = "textCell"
static let popupTableCellViewIdentifier = "popupCell"
static let tagTableColumnIdentifier = "tagColumn"
static let savingBehaviorTableColumnIdentifier = "savingBehaviorColumn"
}
// MARK: Properties
@IBOutlet weak var tableView: NSTableView!
// MARK: Table View
internal func numberOfRowsInTableView(tableView: NSTableView) -> Int {
return Track.Tag.allTags.count
}
func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView? {
let tag = Track.Tag.allTags[row]
if tableColumn?.identifier == TableViewConstants.tagTableColumnIdentifier {
let view = tableView.makeViewWithIdentifier(TableViewConstants.textTableCellViewIdentifier, owner: nil) as? NSTableCellView
view?.textField?.stringValue = tag.localizedName
return view
} else if tableColumn?.identifier == TableViewConstants.savingBehaviorTableColumnIdentifier {
let popupButton = tableView.makeViewWithIdentifier(TableViewConstants.popupTableCellViewIdentifier, owner: nil) as? NSPopUpButton
popupButton?.removeAllItems()
if tag.isReturnedBySearchAPI {
popupButton?.addItemWithTitle(NSLocalizedString("Save", comment: "Menu item title for a tag that is going to be saved"))
}
if tag.clearable {
popupButton?.addItemWithTitle(NSLocalizedString("Clear", comment: "Menu item title for a tag that is going to be cleared"))
}
popupButton?.addItemWithTitle(NSLocalizedString("Ignore", comment: "Menu item title for a tag that is not going to be saved"))
var selectedIndex: Int
switch Preferences.sharedPreferences.tagSavingBehaviors[tag]! {
case .Save:
selectedIndex = 0
case .Clear:
selectedIndex = 1
if !tag.isReturnedBySearchAPI {
--selectedIndex
}
case .Ignore:
selectedIndex = 2
if !tag.isReturnedBySearchAPI {
--selectedIndex
}
if !tag.clearable {
--selectedIndex
}
}
popupButton?.selectItemAtIndex(selectedIndex)
return popupButton
}
return nil
}
@IBAction private func savingBehaviorChanged(sender: NSPopUpButton) {
let tag = Track.Tag.allTags[tableView.rowForView(sender)]
let selectedIndex = sender.indexOfItem(sender.selectedItem!)
var savingBehavior = Preferences.sharedPreferences.tagSavingBehaviors[tag]!
switch selectedIndex {
case 0:
if tag.isReturnedBySearchAPI {
savingBehavior = .Save
} else if tag.clearable {
savingBehavior = .Clear
} else {
savingBehavior = .Ignore
}
case 1:
if tag.isReturnedBySearchAPI {
if tag.clearable {
savingBehavior = .Clear
} else {
savingBehavior = .Ignore
}
}
savingBehavior = .Ignore
case 2:
savingBehavior = .Ignore
default:
break
}
Preferences.sharedPreferences.tagSavingBehaviors[tag] = savingBehavior
}
}