Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 173 lines (121 sloc) 4.37 KB
/*
* MPDclient - Music Player Daemon (MPD) remote for Mac OS
* Copyright (C) 2019 Old-Geek
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#import "StreamEdit.h"
#import "StreamValues.h"
#import "LogoRadio.h"
@implementation StreamEdit
Stream *streamToEdit;
NSString *newLogo = nil;
unsigned int streamEditPosition;
BOOL addRadio = NO;
- (id)initWithStream:(Stream *)stream {
if (self = [super initWithWindowNibName:@"StreamEdit"]) {
streamToEdit = stream;
[self.window makeKeyAndOrderFront:nil];
[self.window setLevel:NSFloatingWindowLevel];
}
return self;
}
- (void)awakeFromNib {
if (streamToEdit == nil) {
addRadio = YES;
} else {
[nameField setStringValue:streamToEdit.name];
[urlField setStringValue:streamToEdit.url];
NSImage *image = [StreamValues getLogo:streamToEdit.url];
logoButton.image = [image squareDownSize:streameditlogosize];
[self setShadow];
streamEditPosition = streamToEdit.position;
}
}
- (void)setShadow {
NSShadow *shadow = [[NSShadow alloc] init];
[shadow setShadowBlurRadius:2.0f];
[shadow setShadowOffset:CGSizeMake(0.0f, 0.0f)];
NSColor *color = shadowColor;
[shadow setShadowColor:color];
[logoButton setShadow:shadow];
}
- (IBAction)save:(id)sender {
NSString *name = [nameField stringValue];
NSString *url = [urlField stringValue];
if ([name containsString:@"@"]) {
labelName.textColor = errorColor;
return;
} else {
labelName.textColor = [NSColor labelColor];
}
[self add:name url:url];
[self setLabel:name url:url];
[self boxClosed];
}
- (void)add:(NSString *)name url:(NSString *)url {
if (url.length > 7 && name.length > 0 && [[url substringToIndex:4] isEqualToString:@"http"]) {
MPDStream *mpdStream = [MPDStream alloc];
if (!addRadio)
[mpdStream deleteStream:streamEditPosition];
[mpdStream addStream:[NSString stringWithFormat:@"%@#%@", url, name]];
if (newLogo != nil)
[LogoRadio saveLogoFromURL:newLogo radioURL:url];
[[MPDDelegate sMPDDelegate]streamUpdateTrigger];
[self close];
}
}
- (void)setLabel:(NSString *)name url:(NSString *)url {
if (name.length > 0)
labelName.textColor = [NSColor labelColor];
else
labelName.textColor = errorColor;
if ( url.length > 7 && [ [url substringToIndex:4] isEqualToString:@"http"] )
labelURL.textColor = [NSColor labelColor];
else
labelURL.textColor = errorColor;
}
- (IBAction)cancel:(id)sender {
[self close];
[self boxClosed];
}
- (IBAction)chooseLogo:(id)sender {
[self openFiles];
}
- (void)boxClosed {
[[NSNotificationCenter defaultCenter] postNotificationName:@"streamListener" object:nil userInfo:nil];
}
- (void)openFiles {
NSArray *fileTypes = @[@"jpg", @"jpeg", @"png"];
NSOpenPanel * panel = [NSOpenPanel openPanel];
[panel setAllowsMultipleSelection:NO];
[panel setCanChooseFiles:YES];
[panel setFloatingPanel:NO];
[panel setAllowedFileTypes:fileTypes];
[panel setLevel:NSFloatingWindowLevel];
[self.window setLevel:NSNormalWindowLevel];
[panel beginWithCompletionHandler:^(NSInteger result){
if (result == NSModalResponseOK) {
newLogo = [NSString stringWithFormat:@"%@", [panel URLs][0]];
[self setNewLogo:[panel URLs][0]];
}
[self.window setLevel:NSFloatingWindowLevel];
}];
}
- (void)setNewLogo:(NSURL *)fileURL {
NSImage *image = [[NSImage alloc] initWithContentsOfURL:fileURL];
logoButton.image = [image squareDownSize:streameditlogosize];
}
@end
You can’t perform that action at this time.