-
Notifications
You must be signed in to change notification settings - Fork 0
/
PushoverDetailPane.m
123 lines (96 loc) · 3.57 KB
/
PushoverDetailPane.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
#import "PushoverPlugin.h"
#import "PushoverDetailPane.h"
#import "NSAttributedString+Hyperlink.h"
#import "JSONKit.h"
static NSMutableDictionary *cachedSoundList;
@implementation PushoverDetailPane
- (NSString *)nibName {
return @"Pushover";
}
- (void)configureForActionDetails:(NSDictionary *)inDetails
listObject:(AIListObject *)inObject
{
NSString *key = [inDetails objectForKey:KEY_PUSHOVER_KEY];
[pushover_key setStringValue:(key ? key : @"")];
NSString *device = [inDetails objectForKey:KEY_PUSHOVER_DEVICE_NAME];
[pushover_device_name setStringValue:(device ? device : @"")];
when_away = [(NSNumber *)[inDetails objectForKey:KEY_ONLY_WHEN_AWAY]
boolValue];
[only_when_away setState:when_away];
when_locked = [(NSNumber *)[inDetails objectForKey:KEY_ONLY_WHEN_LOCKED]
boolValue];
[only_when_locked setState:when_locked];
[pushover_link_label setAllowsEditingTextAttributes: YES];
[pushover_link_label setSelectable: YES];
NSMutableAttributedString* string = [[NSMutableAttributedString alloc]
initWithString:@"Find your user key or create an account at "];
NSURL* url = [NSURL URLWithString:@"https://pushover.net/"];
[string appendAttributedString:[NSAttributedString
hyperlinkFromString:@"https://pushover.net/" withURL:url]];
[pushover_link_label setAttributedStringValue: string];
[sound removeAllItems];
[sound addItemsWithTitles:[[[PushoverDetailPane soundList] allValues]
sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]];
[sound insertItemWithTitle:@"(Device default sound)" atIndex:0];
NSString *sel_sound = [inDetails objectForKey:KEY_PUSHOVER_SOUND];
if (sel_sound != nil) {
/* map sound name to description */
NSString *v = [[PushoverDetailPane soundList] valueForKey:sel_sound];
if (v != nil)
[sound selectItemWithTitle:v];
}
[super configureForActionDetails:inDetails listObject:inObject];
}
- (IBAction)awayClicked:(id)sender
{
when_away = !when_away;
}
- (IBAction)lockedClicked:(id)sender
{
when_locked = !when_locked;
}
- (NSDictionary *)actionDetails
{
NSString *sel_sound = @"";
/* find key in soundList for selected sound description */
if ([sound selectedItem] != nil) {
NSString *sound_t = [[sound selectedItem] title];
NSArray *v = [[PushoverDetailPane soundList] allKeysForObject:sound_t];
if (v != nil && [v count] > 0)
sel_sound = [v objectAtIndex:0];
}
return [NSDictionary dictionaryWithObjectsAndKeys:
[pushover_key stringValue], KEY_PUSHOVER_KEY,
[pushover_device_name stringValue], KEY_PUSHOVER_DEVICE_NAME,
[[NSNumber alloc] initWithBool:when_away], KEY_ONLY_WHEN_AWAY,
[[NSNumber alloc] initWithBool:when_locked], KEY_ONLY_WHEN_LOCKED,
sel_sound, KEY_PUSHOVER_SOUND,
nil];
}
+ (void)fetchSoundList
{
NSURLResponse *response;
NSError *error;
cachedSoundList = [[NSMutableDictionary alloc] initWithCapacity:1];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL
URLWithString:[NSString stringWithFormat:@"%s?token=%s",
PUSHOVER_API_SOUNDS_URL, PUSHOVER_API_TOKEN]]];
NSData *d = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response error:&error];
JSONDecoder *decoder = [[JSONDecoder alloc]
initWithParseOptions:JKParseOptionNone];
NSDictionary *json = [decoder parseJSONData:d];
if (json != nil) {
NSDictionary *s = [json objectForKey:@"sounds"];
if (s != nil)
[cachedSoundList setDictionary:[NSDictionary
dictionaryWithObjects:[s allValues] forKeys:[s allKeys]]];
}
}
+ (NSDictionary *)soundList
{
if (cachedSoundList == nil || [cachedSoundList count] == 0)
[PushoverDetailPane fetchSoundList];
return cachedSoundList;
}
@end