cog/Application/AppController.m

553 lines
18 KiB
Matlab
Raw Normal View History

2005-06-02 18:16:43 +00:00
#import "AppController.h"
#import "FileTreeController.h"
#import "FileTreeViewController.h"
#import "FileTreeOutlineView.h"
#import "PlaybackController.h"
#import "PlaylistController.h"
#import "PlaylistView.h"
#import "PlaylistEntry.h"
#import "PlaylistLoader.h"
#import "OpenURLPanel.h"
2008-02-11 09:20:52 +00:00
#import "SpotlightWindowController.h"
#import "StringToURLTransformer.h"
#import "FontSizetoLineHeightTransformer.h"
#import "Cog-Swift.h"
#import "PathNode.h"
#import <CogAudio/Status.h>
2005-06-02 18:16:43 +00:00
#import "Logging.h"
#import "MiniModeMenuTitleTransformer.h"
#import "DualWindow.h"
#import <MASShortcut/Shortcut.h>
#import "Shortcuts.h"
2005-06-02 18:16:43 +00:00
@implementation AppController
+ (void)initialize
{
// Register transformers
NSValueTransformer *stringToURLTransformer = [[StringToURLTransformer alloc] init];
[NSValueTransformer setValueTransformer:stringToURLTransformer
forName:@"StringToURLTransformer"];
NSValueTransformer *fontSizetoLineHeightTransformer =
[[FontSizetoLineHeightTransformer alloc] init];
[NSValueTransformer setValueTransformer:fontSizetoLineHeightTransformer
forName:@"FontSizetoLineHeightTransformer"];
NSValueTransformer *miniModeMenuTitleTransformer = [[MiniModeMenuTitleTransformer alloc] init];
[NSValueTransformer setValueTransformer:miniModeMenuTitleTransformer
forName:@"MiniModeMenuTitleTransformer"];
NSValueTransformer *playbackStatusToHiddenTransformer = [[PlaybackStatusToHiddenTransformer alloc] init];
[NSValueTransformer setValueTransformer:playbackStatusToHiddenTransformer forName:@"PlaybackStatusToHiddenTransformer"];
}
- (id)init
{
self = [super init];
if (self)
{
[self initDefaults];
2007-11-01 01:53:52 +00:00
queue = [[NSOperationQueue alloc]init];
}
return self;
}
2006-09-02 16:09:20 +00:00
- (IBAction)openFiles:(id)sender
2005-06-02 18:16:43 +00:00
{
NSOpenPanel *p;
p = [NSOpenPanel openPanel];
[p setAllowedFileTypes:[playlistLoader acceptableFileTypes]];
2005-06-02 18:16:43 +00:00
[p setCanChooseDirectories:YES];
[p setAllowsMultipleSelection:YES];
[p setResolvesAliases:YES];
2005-06-02 18:16:43 +00:00
[p beginSheetModalForWindow:mainWindow completionHandler:^(NSInteger result) {
if ( result == NSModalResponseOK ) {
2018-06-28 10:59:59 +00:00
[self->playlistLoader willInsertURLs:[p URLs] origin:URLOriginInternal];
[self->playlistLoader didInsertURLs:[self->playlistLoader addURLs:[p URLs] sort:YES] origin:URLOriginInternal];
} else {
[p close];
}
}];
}
- (IBAction)savePlaylist:(id)sender
{
NSSavePanel *p;
p = [NSSavePanel savePanel];
2005-06-02 18:16:43 +00:00
[p beginSheetModalForWindow:mainWindow completionHandler:^(NSInteger result) {
if ( result == NSModalResponseOK ) {
2018-06-28 10:59:59 +00:00
[self->playlistLoader save:[[p URL] path]];
} else {
[p close];
}
}];
}
- (IBAction)openURL:(id)sender
{
OpenURLPanel *p;
p = [OpenURLPanel openURLPanel];
[p beginSheetWithWindow:mainWindow delegate:self didEndSelector:@selector(openURLPanelDidEnd:returnCode:contextInfo:) contextInfo:nil];
}
- (void)openURLPanelDidEnd:(OpenURLPanel *)panel returnCode:(int)returnCode contextInfo:(void *)contextInfo
{
if (returnCode == NSModalResponseOK)
{
[playlistLoader willInsertURLs:[NSArray arrayWithObject:[panel url]] origin:URLOriginInternal];
[playlistLoader didInsertURLs:[playlistLoader addURLs:[NSArray arrayWithObject:[panel url]] sort:NO] origin:URLOriginInternal];
}
}
2005-06-02 18:16:43 +00:00
- (IBAction)delEntries:(id)sender
{
[playlistController remove:self];
}
- (PlaylistEntry *)currentEntry
{
return [playlistController currentEntry];
}
- (BOOL)application:(NSApplication *)sender delegateHandlesKey:(NSString *)key
{
return [key isEqualToString:@"currentEntry"] || [key isEqualToString:@"play"];
2005-06-02 18:16:43 +00:00
}
- (void)awakeFromNib
{
[[totalTimeField cell] setBackgroundStyle:NSBackgroundStyleRaised];
2008-01-23 03:03:46 +00:00
[[playbackButtons cell] setToolTip:NSLocalizedString(@"PlayButtonTooltip", @"") forSegment: 1];
[[playbackButtons cell] setToolTip:NSLocalizedString(@"PrevButtonTooltip", @"") forSegment: 0];
[[playbackButtons cell] setToolTip:NSLocalizedString(@"NextButtonTooltip", @"") forSegment: 2];
2005-07-11 20:27:47 +00:00
[infoButton setToolTip:NSLocalizedString(@"InfoButtonTooltip", @"")];
[shuffleButton setToolTip:NSLocalizedString(@"ShuffleButtonTooltip", @"")];
[repeatButton setToolTip:NSLocalizedString(@"RepeatButtonTooltip", @"")];
[randomizeButton setToolTip:NSLocalizedString(@"RandomizeButtonTooltip", @"")];
[fileButton setToolTip:NSLocalizedString(@"FileButtonTooltip", @"")];
2006-09-02 16:09:20 +00:00
[self registerHotKeys];
(void) [spotlightWindowController init];
[[playlistController undoManager] disableUndoRegistration];
NSString *basePath = [@"~/Library/Application Support/Cog/" stringByExpandingTildeInPath];
NSString *oldFilename = @"Default.m3u";
NSString *newFilename = @"Default.xml";
if ([[NSFileManager defaultManager] fileExistsAtPath:[basePath stringByAppendingPathComponent:newFilename]])
{
[playlistLoader addURL:[NSURL fileURLWithPath:[basePath stringByAppendingPathComponent:newFilename]]];
}
else
{
[playlistLoader addURL:[NSURL fileURLWithPath:[basePath stringByAppendingPathComponent:oldFilename]]];
}
[[playlistController undoManager] enableUndoRegistration];
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"resumePlaybackOnStartup"])
{
int lastStatus = (int) [[NSUserDefaults standardUserDefaults] integerForKey:@"lastPlaybackStatus"];
int lastIndex = (int) [[NSUserDefaults standardUserDefaults] integerForKey:@"lastTrackPlaying"];
if (lastStatus != kCogStatusStopped && lastIndex >= 0)
{
[playbackController playEntryAtIndex:lastIndex startPaused:(lastStatus == kCogStatusPaused)];
[playbackController seek:[NSNumber numberWithDouble:[[NSUserDefaults standardUserDefaults] floatForKey:@"lastTrackPosition"]]];
}
}
// Restore mini mode
[self setMiniMode:[[NSUserDefaults standardUserDefaults] boolForKey:@"miniMode"]];
// We need file tree view to restore its state here
// so attempt to access file tree view controller's root view
// to force it to read nib and create file tree view for us
//
// TODO: there probably is a more elegant way to do all this
// but i'm too stupid/tired to figure it out now
[fileTreeViewController view];
FileTreeOutlineView* outlineView = [fileTreeViewController outlineView];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(nodeExpanded:) name:NSOutlineViewItemDidExpandNotification object:outlineView];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(nodeCollapsed:) name:NSOutlineViewItemDidCollapseNotification object:outlineView];
NSArray *expandedNodesArray = [[NSUserDefaults standardUserDefaults] valueForKey:@"fileTreeViewExpandedNodes"];
if (expandedNodesArray)
{
expandedNodes = [[NSMutableSet alloc] initWithArray:expandedNodesArray];
}
else
{
expandedNodes = [[NSMutableSet alloc] init];
}
DLog(@"Nodes to expand: %@", [expandedNodes description]);
DLog(@"Num of rows: %ld", [outlineView numberOfRows]);
if (!outlineView)
{
DLog(@"outlineView is NULL!");
}
[outlineView reloadData];
for (NSInteger i=0; i<[outlineView numberOfRows]; i++)
{
PathNode *pn = [outlineView itemAtRow:i];
NSString *str = [[pn URL] absoluteString];
if ([expandedNodes containsObject:str])
{
[outlineView expandItem:pn];
}
}
}
- (void)nodeExpanded:(NSNotification*)notification
{
PathNode* node = [[notification userInfo] objectForKey:@"NSObject"];
NSString* url = [[node URL] absoluteString];
[expandedNodes addObject:url];
}
- (void)nodeCollapsed:(NSNotification*)notification
{
PathNode* node = [[notification userInfo] objectForKey:@"NSObject"];
NSString* url = [[node URL] absoluteString];
[expandedNodes removeObject:url];
2005-06-02 18:16:43 +00:00
}
- (void)applicationWillTerminate:(NSNotification *)aNotification
{
int currentStatus = [playbackController playbackStatus];
int lastTrackPlaying = -1;
double lastTrackPosition = 0;
if (currentStatus == kCogStatusStopping)
currentStatus = kCogStatusStopped;
[[NSUserDefaults standardUserDefaults] setInteger:currentStatus forKey:@"lastPlaybackStatus"];
if (currentStatus != kCogStatusStopped)
{
PlaylistEntry * pe = [playlistController currentEntry];
lastTrackPlaying = [pe index];
lastTrackPosition = [pe currentPosition];
}
[[NSUserDefaults standardUserDefaults] setInteger:lastTrackPlaying forKey:@"lastTrackPlaying"];
[[NSUserDefaults standardUserDefaults] setDouble:lastTrackPosition forKey:@"lastTrackPosition"];
2007-02-26 05:07:18 +00:00
[playbackController stop:self];
2005-06-02 18:16:43 +00:00
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *folder = @"~/Library/Application Support/Cog/";
folder = [folder stringByExpandingTildeInPath];
if ([fileManager fileExistsAtPath: folder] == NO)
{
[fileManager createDirectoryAtPath: folder withIntermediateDirectories:NO attributes:nil error:nil];
2005-06-02 18:16:43 +00:00
}
NSString * fileName = @"Default.xml";
[playlistLoader saveXml:[folder stringByAppendingPathComponent: fileName]];
fileName = @"Default.m3u";
NSError *error;
[[NSFileManager defaultManager] removeItemAtPath:[folder stringByAppendingPathComponent:fileName] error:&error];
DLog(@"Saving expanded nodes: %@", [expandedNodes description]);
[[NSUserDefaults standardUserDefaults] setValue:[expandedNodes allObjects] forKey:@"fileTreeViewExpandedNodes"];
2005-06-02 18:16:43 +00:00
}
- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag
{
if (flag == NO)
[mainWindow makeKeyAndOrderFront:self]; // TODO: do we really need this? We never close the main window.
for(NSWindow* win in [NSApp windows]) // Maximizing all windows
if([win isMiniaturized])
[win deminiaturize:self];
2005-06-02 18:16:43 +00:00
return NO;
}
- (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename
{
NSArray* urls = [NSArray arrayWithObject:[NSURL fileURLWithPath:filename]];
2009-02-28 22:22:33 +00:00
[playlistLoader willInsertURLs:urls origin:URLOriginExternal];
[playlistLoader didInsertURLs:[playlistLoader addURLs:urls sort:NO] origin:URLOriginExternal];
2005-06-02 18:16:43 +00:00
return YES;
}
- (void)application:(NSApplication *)theApplication openFiles:(NSArray *)filenames
{
//Need to convert to urls
NSMutableArray *urls = [NSMutableArray array];
for (NSString *filename in filenames)
{
[urls addObject:[NSURL fileURLWithPath:filename]];
}
2009-02-28 22:22:33 +00:00
[playlistLoader willInsertURLs:urls origin:URLOriginExternal];
[playlistLoader didInsertURLs:[playlistLoader addURLs:urls sort:YES] origin:URLOriginExternal];
2005-06-02 18:16:43 +00:00
[theApplication replyToOpenOrPrint:NSApplicationDelegateReplySuccess];
}
2020-11-25 00:03:49 +00:00
- (IBAction)openLiberapayPage:(id)sender
2006-04-04 01:08:21 +00:00
{
2020-11-25 00:03:49 +00:00
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"https://liberapay.com/kode54"]];
}
2020-11-25 00:03:49 +00:00
- (IBAction)openPaypalPage:(id)sender
2020-05-02 07:53:08 +00:00
{
2020-11-25 00:03:49 +00:00
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"https://www.paypal.com/paypalme/kode54"]];
2020-05-02 07:53:08 +00:00
}
2020-11-25 00:03:49 +00:00
- (IBAction)openBitcoinPage:(id)sender
{
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"https://kode54.net/donateBitcoin"]];
}
- (IBAction)openPatreonPage:(id)sender
{
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"https://patreon.com/kode54"]];
}
- (IBAction)openKofiPage:(id)sender
{
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"https://ko-fi.com/kode54"]];
}
- (IBAction)feedback:(id)sender
{
NSString *version = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];
NSString *url = @"https://kode54.net/contact";
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", url, version]]];
2006-04-04 01:08:21 +00:00
}
2006-09-02 16:09:20 +00:00
- (void)initDefaults
{
NSMutableDictionary *userDefaultsValuesDict = [NSMutableDictionary dictionary];
// Font defaults
float fFontSize = [NSFont systemFontSizeForControlSize:NSControlSizeSmall];
NSNumber *fontSize = [NSNumber numberWithFloat:fFontSize];
[userDefaultsValuesDict setObject:fontSize forKey:@"fontSize"];
2006-09-10 21:27:20 +00:00
2021-01-25 03:23:48 +00:00
NSString *feedURLdefault = @"https://f.losno.co/cog/mercury.xml";
[userDefaultsValuesDict setObject:feedURLdefault forKey:@"SUFeedURL"];
[userDefaultsValuesDict setObject:@"clearAndPlay" forKey:@"openingFilesBehavior"];
[userDefaultsValuesDict setObject:@"enqueue" forKey:@"openingFilesAlteredBehavior"];
[userDefaultsValuesDict setObject:@"albumGainWithPeak" forKey:@"volumeScaling"];
[userDefaultsValuesDict setObject:@"cubic" forKey:@"resampling"];
[userDefaultsValuesDict setObject:[NSNumber numberWithInteger:kCogStatusStopped] forKey:@"lastPlaybackStatus"];
[userDefaultsValuesDict setObject:[NSNumber numberWithInteger:-1] forKey:@"lastTrackPlaying"];
[userDefaultsValuesDict setObject:[NSNumber numberWithDouble:0] forKey:@"lastTrackPosition"];
2014-12-04 05:36:55 +00:00
[userDefaultsValuesDict setObject:@"dls appl" forKey:@"midi.plugin"];
[userDefaultsValuesDict setObject:@"default" forKey:@"midi.flavor"];
[userDefaultsValuesDict setObject:[NSNumber numberWithBool:NO] forKey:@"resumePlaybackOnStartup"];
2006-09-02 16:09:20 +00:00
//Register and sync defaults
[[NSUserDefaults standardUserDefaults] registerDefaults:userDefaultsValuesDict];
[[NSUserDefaults standardUserDefaults] synchronize];
2021-01-25 03:23:48 +00:00
//And if the existing feed URL is broken due to my ineptitude with the above defaults, fix it
2021-01-25 03:23:48 +00:00
NSSet<NSString *> *brokenFeedURLs = [NSSet setWithObjects:
@"https://kode54.net/cog/stable.xml",
@"https://kode54.net/cog/mercury.xml"
@"https://www.kode54.net/cog/mercury.xml",
nil];
NSString *feedURL = [[NSUserDefaults standardUserDefaults] stringForKey:@"SUFeedURL"];
if ([brokenFeedURLs containsObject:feedURL]) {
[[NSUserDefaults standardUserDefaults] setValue:feedURLdefault forKey:@"SUFeedURL"];
2021-01-25 03:23:48 +00:00
}
2006-09-02 16:09:20 +00:00
}
/* Unassign previous handler first, so dealloc can unregister it from the global map before the new instances are assigned */
2006-09-10 21:27:20 +00:00
- (void)registerHotKeys
2006-09-02 16:09:20 +00:00
{
MASShortcutBinder *binder = [MASShortcutBinder sharedBinder];
[binder bindShortcutWithDefaultsKey:CogPlayShortcutKey toAction:^{
[self clickPlay];
}];
[binder bindShortcutWithDefaultsKey:CogNextShortcutKey toAction:^{
[self clickNext];
}];
[binder bindShortcutWithDefaultsKey:CogPrevShortcutKey toAction:^{
[self clickPrev];
}];
[binder bindShortcutWithDefaultsKey:CogSpamShortcutKey toAction:^{
[self clickSpam];
}];
2006-09-02 16:09:20 +00:00
}
- (void)windowDidEnterFullScreen:(NSNotification *)notification
{
DLog(@"Entering fullscreen");
if (nil == nowPlaying)
{
nowPlaying = [[NowPlayingBarController alloc] init];
NSView *contentView = [mainWindow contentView];
NSRect contentRect = [contentView frame];
const NSSize windowSize = [contentView convertSize:[mainWindow frame].size fromView: nil];
[contentView addSubview: [nowPlaying view]];
[NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) {
[context setDuration:0.25];
2018-06-28 10:59:59 +00:00
NSRect nowPlayingFrame = [[self->nowPlaying view] frame];
nowPlayingFrame.size.width = windowSize.width;
2018-06-28 10:59:59 +00:00
[[self->nowPlaying view] setFrame: nowPlayingFrame];
[[self->nowPlaying view] setFrameOrigin: NSMakePoint(0.0, NSMaxY(contentRect) - nowPlayingFrame.size.height)];
2018-06-28 10:59:59 +00:00
NSRect mainViewFrame = [self->mainView frame];
mainViewFrame.size.height -= nowPlayingFrame.size.height;
2018-06-28 10:59:59 +00:00
[[self->mainView animator] setFrame:mainViewFrame];
} completionHandler:^{
}];
[[nowPlaying text] bind:@"value" toObject:currentEntryController withKeyPath:@"content.display" options:nil];
}
}
- (void)windowDidExitFullScreen:(NSNotification *)notification
{
DLog(@"Exiting fullscreen");
if (nowPlaying)
{
NSRect nowPlayingFrame = [[nowPlaying view] frame];
NSRect mainViewFrame = [mainView frame];
mainViewFrame.size.height += nowPlayingFrame.size.height;
//[mainView setFrame:mainViewFrame];
// [mainView setFrameOrigin:NSMakePoint(0.0, 0.0)];
[NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) {
[context setDuration:0.25];
2018-06-28 10:59:59 +00:00
[[self->mainView animator] setFrame:mainViewFrame];
} completionHandler:^{
2018-06-28 10:59:59 +00:00
[[self->nowPlaying view] removeFromSuperview];
self->nowPlaying = nil;
}];
}
}
2006-09-02 16:09:20 +00:00
- (void)clickPlay
{
2008-01-23 03:03:46 +00:00
[playbackController playPauseResume:self];
2006-09-02 16:09:20 +00:00
}
- (void)clickPause
{
[playbackController pause:self];
}
- (void)clickStop
{
[playbackController stop:self];
}
2006-09-02 16:09:20 +00:00
- (void)clickPrev
{
2008-01-23 03:03:46 +00:00
[playbackController prev:nil];
2006-09-02 16:09:20 +00:00
}
- (void)clickNext
{
2008-01-23 03:03:46 +00:00
[playbackController next:nil];
2006-09-02 16:09:20 +00:00
}
- (void)clickSpam
{
[playbackController spam];
}
- (void)clickSeek:(NSTimeInterval)position
{
[playbackController seek:self toTime:position];
}
- (void)changeFontSize:(float)size
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
float fCurrentSize = [defaults floatForKey:@"fontSize"];
NSNumber *newSize = [NSNumber numberWithFloat:(fCurrentSize + size)];
[defaults setObject:newSize forKey:@"fontSize"];
}
- (IBAction)increaseFontSize:(id)sender
{
[self changeFontSize:1];
}
- (IBAction)decreaseFontSize:(id)sender
{
[self changeFontSize:-1];
}
- (IBAction)toggleMiniMode:(id)sender
{
[self setMiniMode:(!miniMode)];
}
- (BOOL)miniMode
{
return miniMode;
}
- (void)setMiniMode:(BOOL)newMiniMode
{
miniMode = newMiniMode;
[[NSUserDefaults standardUserDefaults] setBool:miniMode forKey:@"miniMode"];
NSWindow *windowToShow = miniMode ? miniWindow : mainWindow;
NSWindow *windowToHide = miniMode ? mainWindow : miniWindow;
[windowToHide close];
[windowToShow makeKeyAndOrderFront:self];
}
2005-06-02 18:16:43 +00:00
@end