Skip to content

Commit

Permalink
Added standard Notification Center notifications and preferences wind…
Browse files Browse the repository at this point in the history
…ow from kbhomes/google-music-mac repo.
  • Loading branch information
JamesFator committed Feb 25, 2014
1 parent 1a0fe1e commit 7d63031
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 431 deletions.
29 changes: 12 additions & 17 deletions Google Music.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
objects = {

/* Begin PBXBuildFile section */
1A519DE81853B1A00069FF17 /* Flash Player.plugin in CopyFiles */ = {isa = PBXBuildFile; fileRef = 1A519DE61853B1990069FF17 /* Flash Player.plugin */; };
1A1C923018BC603C00E4F481 /* js in Resources */ = {isa = PBXBuildFile; fileRef = 1A1C922F18BC603C00E4F481 /* js */; };
1A1C923218BC60B800E4F481 /* LICENSE_ANWAR.md in Resources */ = {isa = PBXBuildFile; fileRef = 1A1C923118BC60B800E4F481 /* LICENSE_ANWAR.md */; };
1A1C923418BC66B700E4F481 /* Preferences.plist in Resources */ = {isa = PBXBuildFile; fileRef = 1A1C923318BC66B700E4F481 /* Preferences.plist */; };
1A68DAD81803B588004CBA25 /* CustomWebView.m in Sources */ = {isa = PBXBuildFile; fileRef = 1A68DAD71803B588004CBA25 /* CustomWebView.m */; };
1A7B684017453E40001E509E /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1A7B683F17453E40001E509E /* Cocoa.framework */; };
1A7B684A17453E40001E509E /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 1A7B684817453E40001E509E /* InfoPlist.strings */; };
Expand All @@ -19,21 +21,10 @@
1A7B68811745ABBD001E509E /* Google MusicIcon.icns in Resources */ = {isa = PBXBuildFile; fileRef = 1A7B68801745ABBD001E509E /* Google MusicIcon.icns */; };
/* End PBXBuildFile section */

/* Begin PBXCopyFilesBuildPhase section */
1A67E21B1853ACF500EA5F38 /* CopyFiles */ = {
isa = PBXCopyFilesBuildPhase;
buildActionMask = 2147483647;
dstPath = "";
dstSubfolderSpec = 13;
files = (
1A519DE81853B1A00069FF17 /* Flash Player.plugin in CopyFiles */,
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
1A519DE61853B1990069FF17 /* Flash Player.plugin */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = "Flash Player.plugin"; path = "GoogleMusic/Flash Player.plugin"; sourceTree = "<group>"; };
1A1C922F18BC603C00E4F481 /* js */ = {isa = PBXFileReference; lastKnownFileType = folder; path = js; sourceTree = "<group>"; };
1A1C923118BC60B800E4F481 /* LICENSE_ANWAR.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = LICENSE_ANWAR.md; sourceTree = "<group>"; };
1A1C923318BC66B700E4F481 /* Preferences.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Preferences.plist; sourceTree = "<group>"; };
1A68DAD61803B588004CBA25 /* CustomWebView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CustomWebView.h; sourceTree = "<group>"; };
1A68DAD71803B588004CBA25 /* CustomWebView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CustomWebView.m; sourceTree = "<group>"; };
1A7B683C17453E40001E509E /* Google Music.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Google Music.app"; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -87,7 +78,6 @@
1A7B683E17453E40001E509E /* Frameworks */ = {
isa = PBXGroup;
children = (
1A519DE61853B1990069FF17 /* Flash Player.plugin */,
1A7B687317453E4C001E509E /* WebKit.framework */,
1A7B683F17453E40001E509E /* Cocoa.framework */,
1A7B684117453E40001E509E /* Other Frameworks */,
Expand All @@ -108,6 +98,7 @@
1A7B684517453E40001E509E /* GoogleMusic */ = {
isa = PBXGroup;
children = (
1A1C922F18BC603C00E4F481 /* js */,
1A7B685117453E40001E509E /* AppDelegate.h */,
1A7B685217453E40001E509E /* AppDelegate.m */,
1A7B685417453E40001E509E /* MainMenu.xib */,
Expand All @@ -126,6 +117,8 @@
1A7B684B17453E40001E509E /* main.m */,
1A7B684D17453E40001E509E /* Google Music-Prefix.pch */,
1A7B684E17453E40001E509E /* Credits.rtf */,
1A1C923118BC60B800E4F481 /* LICENSE_ANWAR.md */,
1A1C923318BC66B700E4F481 /* Preferences.plist */,
);
name = "Supporting Files";
sourceTree = "<group>";
Expand All @@ -140,7 +133,6 @@
1A7B683817453E40001E509E /* Sources */,
1A7B683917453E40001E509E /* Frameworks */,
1A7B683A17453E40001E509E /* Resources */,
1A67E21B1853ACF500EA5F38 /* CopyFiles */,
);
buildRules = (
);
Expand Down Expand Up @@ -182,7 +174,10 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
1A1C923018BC603C00E4F481 /* js in Resources */,
1A1C923418BC66B700E4F481 /* Preferences.plist in Resources */,
1A7B684A17453E40001E509E /* InfoPlist.strings in Resources */,
1A1C923218BC60B800E4F481 /* LICENSE_ANWAR.md in Resources */,
1A7B685017453E40001E509E /* Credits.rtf in Resources */,
1A7B685617453E40001E509E /* MainMenu.xib in Resources */,
1A7B68811745ABBD001E509E /* Google MusicIcon.icns in Resources */,
Expand Down
1 change: 1 addition & 0 deletions GoogleMusic/AppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

@property (assign) IBOutlet NSWindow *window;
@property (nonatomic, retain) IBOutlet CustomWebView *webView;
@property (assign) NSUserDefaults *defaults;

- (void) playPause;
- (void) forwardAction;
Expand Down
96 changes: 93 additions & 3 deletions GoogleMusic/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,26 @@ @implementation AppDelegate
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
{ return YES; }

/**
* Set defaults.
*/
+ (void)initialize
{
// Register default preferences.
NSString *prefsPath = [[NSBundle mainBundle] pathForResource:@"Preferences" ofType:@"plist"];
NSDictionary *prefs = [NSDictionary dictionaryWithContentsOfFile:prefsPath];

[[NSUserDefaults standardUserDefaults] registerDefaults:prefs];
}

/**
* Application finished launching, we will register the event tap callback.
*/
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Load the user preferences.
_defaults = [NSUserDefaults standardUserDefaults];

// Add an event tap to intercept the system defined media key events
eventTap = CGEventTapCreate(kCGSessionEventTap,
kCGHeadInsertEventTap,
Expand All @@ -32,17 +47,18 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
fprintf(stderr, "failed to create event tap\n");
exit(1);
}
//Create a run loop source.
// Create a run loop source.
eventPortSource = CFMachPortCreateRunLoopSource( kCFAllocatorDefault, eventTap, 0 );

//Enable the event tap.
// Enable the event tap.
CGEventTapEnable(eventTap, true);

// Let's do this in a separate thread so that a slow app doesn't lag the event tap
[NSThread detachNewThreadSelector:@selector(eventTapThread) toTarget:self withObject:nil];

// Load the main page
[webView setAppDelegate:self];
[webView setFrameLoadDelegate:self];
[[webView preferences] setPlugInsEnabled:YES];
NSURL *url = [NSURL URLWithString:@"https://play.google.com/music"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
Expand Down Expand Up @@ -117,7 +133,7 @@ static CGEventRef event_tap_callback(CGEventTapProxy proxy,
return event;
}

#pragma mark - Play Actions
# pragma mark - Play Actions

/**
* playPause toggles the playing status for the app
Expand Down Expand Up @@ -149,4 +165,78 @@ - (void)backAction
CFRelease(keyDownEvent);
}

# pragma mark - Web Level

/**
* didFinishLoadForFrame is called when the Web Frame finished loading.
* We take this time to execute the main.js file
*/
- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
{
[self evaluateJavaScriptFile:@"main"];
[[sender windowScriptObject] setValue:self forKey:@"googleMusicApp"];
}

/**
* evaluateJavaScriptFile will load the JS file and execute it in the webView.
*/
- (void)evaluateJavaScriptFile:(NSString *)name
{
NSString *file = [NSString stringWithFormat:@"js/%@", name];
NSString *path = [[NSBundle mainBundle] pathForResource:file ofType:@"js"];
NSString *js = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];

[webView stringByEvaluatingJavaScriptFromString:js];
}

/**
* notifySong is called when the song is updated. We use this to either
* bring up a standard OSX notification or a 3rd party notification.
*/
- (void)notifySong:(NSString *)title withArtist:(NSString *)artist
album:(NSString *)album art:(NSString *)art
{
if ([_defaults boolForKey:@"notifications.enabled"]) {
NSUserNotification *notif = [[NSUserNotification alloc] init];
notif.title = title;
notif.informativeText = [NSString stringWithFormat:@"%@%@", artist, album];

// Try to load the album art if possible.
if (art) {
NSURL *url = [NSURL URLWithString:art];
NSImage *image = [[NSImage alloc] initWithContentsOfURL:url];

notif.contentImage = image;
}

// Remove the previous notifications in order to make this notification appear immediately.
[[NSUserNotificationCenter defaultUserNotificationCenter] removeAllDeliveredNotifications];

// Deliver the notification.
[[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notif];
}
}

/**
* webScriptNameForSelector will help the JS components access the notify selector.
*/
+ (NSString*)webScriptNameForSelector:(SEL)sel
{
if (sel == @selector(notifySong:withArtist:album:art:))
return @"notifySong";

return nil;
}

/**
* isSelectorExcludedFromWebScript will prevent notify from being excluded from script.
*/
+ (BOOL)isSelectorExcludedFromWebScript:(SEL)sel
{
if (sel == @selector(notifySong:withArtist:album:art:))
return NO;

return YES;
}

@end
2 changes: 1 addition & 1 deletion GoogleMusic/Google Music-Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.3</string>
<string>1.2.1</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
21 changes: 21 additions & 0 deletions GoogleMusic/LICENSE_ANWAR.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)
---------------------

Copyright (c) 2014 Sajid Anwar

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8 changes: 8 additions & 0 deletions GoogleMusic/Preferences.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>notifications.enabled</key>
<true/>
</dict>
</plist>
Loading

0 comments on commit 7d63031

Please sign in to comment.