blob: a4095f9f28f13e46153e19947e493d695a999728 (
plain)
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
|
//
// Notice.m
// MacGap
//
// Created by Christian Sullivan on 7/26/12.
// Copyright (c) 2012 Twitter. All rights reserved.
//
#import "Notice.h"
#import "JSEventHelper.h"
@implementation Notice
- (id) initWithWebView:(WebView*)view
{
if(self = [super init]) {
self.webView = view;
[[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:self];
}
return self;
}
- (void) notify:(NSDictionary *)message {
NSUserNotification *notification = [[NSUserNotification alloc] init];
[notification setTitle:[message valueForKey:@"title"]];
[notification setInformativeText:[message valueForKey:@"content"]];
[notification setDeliveryDate:[NSDate dateWithTimeInterval:0 sinceDate:[NSDate date]]];
BOOL playSound = true; // optional parameter, false only when {sound: false}
@try {
NSNumber *s = [message valueForKey:@"sound"];
if ([[s className] isEqual: @"__NSCFBoolean"]) {
playSound = [s boolValue];
}
}
@catch (NSException *exception) {
}
if (playSound) {
[notification setSoundName:NSUserNotificationDefaultSoundName];
}
NSString *id = @""; // optional, needed for close
@try {
id = [message valueForKey:@"id"];
}
@catch (NSException *exception) {
}
[notification setUserInfo:[NSDictionary dictionaryWithObjectsAndKeys:id, @"id", nil]];
NSUserNotificationCenter *center = [NSUserNotificationCenter defaultUserNotificationCenter];
[center scheduleNotification:notification];
}
// close all notifications with id == notificationId or close all notifications if notificationId == "*"
- (void) close:(NSString*)notificationId {
NSUserNotificationCenter *center = [NSUserNotificationCenter defaultUserNotificationCenter];
for(NSUserNotification * deliveredNote in center.deliveredNotifications) {
if ([notificationId isEqualToString:@"*"] || [deliveredNote.userInfo[@"id"] isEqualToString:notificationId]) {
[center removeDeliveredNotification: deliveredNote];
}
}
}
+ (BOOL) available {
if ([NSUserNotificationCenter respondsToSelector:@selector(defaultUserNotificationCenter)])
return YES;
return NO;
}
- (void) userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification
{
NSString *notificationId = [notification.userInfo valueForKey:@"id"];
[JSEventHelper triggerEvent:@"macgap.notify.activated" forDetail:notificationId forWebView:self.webView];
}
#pragma mark WebScripting Protocol
+ (BOOL) isSelectorExcludedFromWebScript:(SEL)selector
{
BOOL result = YES;
if (selector == @selector(notify:))
result = NO;
if (selector == @selector(close:))
result = NO;
return result;
}
+ (NSString*) webScriptNameForSelector:(SEL)selector
{
id result = nil;
if (selector == @selector(notify:)) {
result = @"notify";
}
if (selector == @selector(close:)) {
result = @"close";
}
return result;
}
// right now exclude all properties (eg keys)
+ (BOOL) isKeyExcludedFromWebScript:(const char*)name
{
return YES;
}
@end
|