forked from omnypro/perpetual
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppDelegate.m
266 lines (218 loc) · 9.44 KB
/
AppDelegate.m
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
//
// AppDelegate.m
// Perpetual
//
// Created by Kalle Persson on 2/14/12.
// Copyright (c) 2012 Revyver, Inc. All rights reserved.
//
#import "AppDelegate.h"
#import "INAppStoreWindow.h"
#import <CoreAudio/CoreAudio.h>
#import <QTKit/QTKit.h>
#import <WebKit/WebKit.h>
NSString *const AppDelegateHTMLImagePlaceholder = @"{{ image_url }}";
@implementation AppDelegate
@synthesize window = _window;
@synthesize startSlider = _startSlider;
@synthesize endSlider = _endSlider;
@synthesize currentTimeLabel = _currentTimeLabel;
@synthesize currentTimeBar = _currentTimeBar;
@synthesize playButton = _playButton;
@synthesize currentTrackLabel = _currentTrackLabel;
@synthesize loopCountLabel = _loopCountLabel;
@synthesize loopCountStepper = _loopCountStepper;
@synthesize coverWebView = _coverWebView;
@synthesize loopCount = _loopCount;
@synthesize loopInfiniteCount = _loopInfiniteCount;
@synthesize timeScale = _timeScale;
@synthesize startTime = _startTime;
@synthesize endTime = _endTime;
@synthesize currentTime = _currentTime;
@synthesize music = _music;
@synthesize paused = _paused;
- (void)setTheLoopCount:(NSUInteger)theLoopCount
{
// Sets the property and updates the label.
[self setLoopCount:theLoopCount];
if ([self loopCount] < [self loopInfiniteCount]) {
[self.loopCountLabel setStringValue:[NSString stringWithFormat:@"x%d",self.loopCount]];
}
else {
[self.loopCountLabel setStringValue:@"∞"];
}
// Finally update the stepper so it's synchronized.
[self.loopCountStepper setIntegerValue:[self loopCount]];
}
- (void)awakeFromNib
{
// Customize INAppStoreWindow.
[[self window] setTitleBarHeight:40.0];
[[self window] setTrafficLightButtonsLeftMargin:7.0];
// Set us up as the delegate of the webView for relevant events.
[[self coverWebView] setUIDelegate:self];
[[self coverWebView] setFrameLoadDelegate:self];
[[self coverWebView] setEditingDelegate:self];
NSURL *htmlFileURL = [[NSBundle mainBundle] URLForResource:@"cover" withExtension:@"html"];
NSError *err = nil;
NSMutableString *html = [NSMutableString stringWithContentsOfURL:htmlFileURL encoding:NSUTF8StringEncoding error:&err];
if (html == nil) {
// Do something with the error.
NSLog(@"%@", err);
return;
}
[html replaceOccurrencesOfString:AppDelegateHTMLImagePlaceholder withString:@"blah" options:0 range:NSMakeRange(0, html.length)];
[self.coverWebView.mainFrame loadHTMLString:html baseURL:[[NSBundle mainBundle] resourceURL]];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// - NOOP -
// Implements a very crude NSSegmentControl, used to switch between the album view
// of the track currently opened and the listening statistics for that track.
NSView *titleBarView = [[self window] titleBarView];
NSSize switcherSize = NSMakeSize(100.f, 30.f);
NSRect switcherFrame = NSMakeRect(NSMidX([titleBarView bounds]) - (switcherSize.width / 2.f), NSMidY([titleBarView bounds]) - (switcherSize.height / 2.f), switcherSize.width, switcherSize.height);
NSSegmentedControl *switcher = [[NSSegmentedControl alloc] initWithFrame:switcherFrame];
[switcher setSegmentCount:2];
[switcher setSegmentStyle:NSSegmentStyleTexturedRounded];
[switcher setLabel:@"Music" forSegment:0];
[switcher setLabel:@"Statistics" forSegment:1];
[switcher setSelectedSegment:0];
[switcher setEnabled:FALSE forSegment:1]; // Disables the statistics segment.
[[switcher cell] setTrackingMode:NSSegmentSwitchTrackingSelectOne];
[titleBarView addSubview:switcher];
// Basic implementation of the default loop count.
// Infinity = 31 until further notice.
[self setLoopInfiniteCount:31];
[self setTheLoopCount:10];
[[self loopCountStepper] setMaxValue:(double)[self loopInfiniteCount]];
}
- (void)checkTime:(NSTimer*)theTimer
{
self.currentTime = [self.music currentTime];
if (self.currentTime.timeValue >= self.endTime.timeValue && self.startTime.timeValue < self.endTime.timeValue && [self loopCount] > 0){
if ([self loopCount] < [self loopInfiniteCount]) {
// [self loopInfiniteCount] is the magic infinite number.
[self setTheLoopCount:[self loopCount]-1];
}
[self.music setCurrentTime:self.startTime];
}
NSCalendar *sysCalendar = [NSCalendar currentCalendar];
NSDate *date1 = [[NSDate alloc] init];
NSDate *date2 = [[NSDate alloc] initWithTimeInterval:self.currentTime.timeValue/self.timeScale sinceDate:date1];
unsigned int unitFlags = NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *conversionInfo = [sysCalendar components:unitFlags fromDate:date1 toDate:date2 options:0];
[self.currentTimeLabel setStringValue:[NSString stringWithFormat:@"%02d:%02d",[conversionInfo minute],[conversionInfo second]]];
[self.currentTimeBar setFloatValue:(float)self.currentTime.timeValue];
}
- (void)loadMusic:(NSURL *) fileURL
{
// Load the track from URL.
NSError *err = nil;
self.music = [[QTMovie alloc] initWithURL:fileURL error:&err];
if (self.music == nil) {
// TODO: Error handling.
NSLog(@"%@", err);
return;
}
//Really needed anymore?
self.paused = YES;
// Find and set slider max values.
QTTime maxTime = [self.music duration];
self.timeScale = [self.music duration].timeScale;
float maxValue = (float)maxTime.timeValue;
self.startTime = QTMakeTime(0.0, self.timeScale);
self.endTime = maxTime;
[self.currentTimeBar setMaxValue:maxValue];
[self.startSlider setMaxValue:maxValue];
[self.startSlider setFloatValue:0.0];
[self.endSlider setMaxValue:maxValue];
[self.endSlider setFloatValue:maxValue];
[self.startSlider setNumberOfTickMarks:(int) maxValue/self.timeScale];
[self.endSlider setNumberOfTickMarks:(int) maxValue/self.timeScale];
// Set title and artist labels from.
NSString * trackTitle = @"Unknown title";
NSString * trackArtist = @"Unknown artist";
NSArray * mdFormatsArray = [self.music availableMetadataFormats];
for (int i=0;i<[mdFormatsArray count];i++) {
NSArray * mdArray = [self.music metadataForFormat:[mdFormatsArray objectAtIndex:i]];
// Fixme: find out why we need to replace @ with ©.
NSArray * titleMetadataItems = [QTMetadataItem metadataItemsFromArray:mdArray withKey:[QTMetadataiTunesMetadataKeySongName stringByReplacingOccurrencesOfString:@"@" withString:@"©"] keySpace:nil];
if ([titleMetadataItems count] > 0) {
trackTitle = [[titleMetadataItems objectAtIndex:0] stringValue];
}
// Fixme: find out why we need to replace @ with ©.
NSArray * artistMetadataItems = [QTMetadataItem metadataItemsFromArray:mdArray withKey:[QTMetadataiTunesMetadataKeyArtist stringByReplacingOccurrencesOfString:@"@" withString:@"©"] keySpace:nil];
if ([artistMetadataItems count] > 0) {
trackArtist = [[artistMetadataItems objectAtIndex:0] stringValue];
}
}
[self.currentTrackLabel setStringValue:[NSString stringWithFormat:@"%@\n%@",trackTitle,trackArtist]];
// Start loop and play track.
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(checkTime:) userInfo:nil repeats:YES];
}
- (BOOL)application:(NSApplication *)sender openFile:(NSString *)filename
{
[self.music stop];
NSURL *fileURL = [NSURL fileURLWithPath:filename];
if (fileURL == nil)
return NO; //make me smarter
[self loadMusic:fileURL];
return YES;
}
#pragma mark IBAction Methods
- (IBAction)startSliderSet:(id)sender
{
if ([self.startSlider doubleValue] < (float)self.endTime.timeValue) {
self.startTime = QTMakeTime((long)[self.startSlider doubleValue], self.timeScale);
}
else {
[self.startSlider setFloatValue:(float)self.startTime.timeValue];
}
}
- (IBAction)endSliderSet:(id)sender
{
if ([self.endSlider doubleValue] > (float)self.startTime.timeValue) {
self.endTime = QTMakeTime((long)[self.endSlider doubleValue], self.timeScale);
}
else {
[self.endSlider setFloatValue:(float)self.endTime.timeValue];
}
}
- (IBAction)currentTimeBarSet:(id)sender
{
NSTimeInterval ct = [self.currentTimeBar doubleValue];
[self.music setCurrentTime:QTMakeTime((long)ct, self.timeScale)];
}
- (IBAction)playButtonClick:(id)sender
{
if (!self.paused) {
[self.music stop];
self.paused = YES;
}
else {
[self.music play];
self.paused = NO;
}
}
- (IBAction)loopStepperStep:(id)sender
{
[self setTheLoopCount:[self.loopCountStepper intValue]];
}
#pragma mark WebView Delegate Methods
- (NSUInteger)webView:(WebView *)webView dragDestinationActionMaskForDraggingInfo:(id<NSDraggingInfo>)draggingInfo
{
return WebDragDestinationActionNone; // We shouldn't be able to drag things into the webView.
}
- (NSUInteger)webView:(WebView *)webView dragSourceActionMaskForPoint:(NSPoint)point
{
return WebDragSourceActionNone; // We shouldn't be able to drag the artwork out of the webView.
}
- (NSArray *)webView:(WebView *)sender contextMenuItemsForElement:(NSDictionary *)element defaultMenuItems:(NSArray *)defaultMenuItems
{
return nil; // Disable the webView's contextual menu.
}
- (BOOL)webView:(WebView *)webView shouldChangeSelectedDOMRange:(DOMRange *)currentRange toDOMRange:(DOMRange *)proposedRange affinity:(NSSelectionAffinity)selectionAffinity stillSelecting:(BOOL)flag
{
return NO; // Prevent the selection of content.
}
@end