/
EchowavesController.m
260 lines (216 loc) · 8.31 KB
/
EchowavesController.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
//
// EchowavesController.m
// EchowavesNotifier
//
// Created by Craig Jolicoeur on 12/26/09.
// Copyright 2009 Dr J Enterprises, LLC. All rights reserved.
//
#import "EchowavesController.h"
#import "JSON.h"
#import "Echowaves.h"
#import "UpdatedConvo.h"
#import "ApiKeyController.h"
#import "GrowlNotifier.h"
@implementation EchowavesController
@synthesize updateTimer;
- (id)init {
if (self = [super init]) {
echowaves = [[Echowaves alloc] init];
[self startTimer];
}
return self;
}
- (void)startTimer {
updateTimer = [NSTimer timerWithTimeInterval:_updateInterval target:self selector:@selector(getUpdates) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:updateTimer forMode:NSRunLoopCommonModes];
}
- (void)awakeFromNib {
[statusMenu setAutoenablesItems:NO];
// Create the NSStatusBar and set its length
statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength: NSVariableStatusItemLength] retain];
// Used to detect where our files are
NSBundle *bundle = [NSBundle mainBundle];
// Allocates and loads th eimages into the app to use with NSStatusItem
ewImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"ewBW" ofType:@"png"]];
ewHighlightImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"ewHL" ofType:@"png"]];
// Sets the images in our NSStatusItem
[statusItem setImage:ewImage];
[statusItem setAlternateImage:ewHighlightImage];
[ewImage release];
[ewHighlightImage release];
// Tells the NSStatusItem what menu to load
[statusItem setMenu:statusMenu];
// Sets the tooltip for our item
[statusItem setToolTip:@"Echowaves Notifier"];
// Enables highlighting
[statusItem setHighlightMode:YES];
// Set up growl notifier
growl = [[[GrowlNotifier alloc] init] retain];
// setup ApiKey window stuff
apiWindow = [[ApiKeyController alloc] init];
//[apiWindow window];
if ( _userApiKey ) {
NSLog(@"_userApiKey found: %@", _userApiKey);
[echowaves setEchowavesURI:_userApiKey];
[self getUpdates];
// TODO: set timer for next update
} else {
// no user API Key set in the defaults yet
NSLog(@"No _userApiKey set");
[self enableManualUpdateMenuItem:NO];
[apiWindow showWindow:self];
// TODO: disable timer
}
// add KVO observer for userApiKey
[self observeUserDefault:@"userApiKey"];
}
- (void)enableManualUpdateMenuItem:(BOOL)enabled {
NSMenuItem *manualUpdateItem = [statusMenu itemWithTitle:@"Manually Check for Updates"];
[manualUpdateItem setEnabled:enabled];
}
- (void)observeUserDefault:(NSString *)property {
[[NSUserDefaults standardUserDefaults] addObserver:self
forKeyPath:property
options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld)
context:NULL];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
// do observing here
NSString *newApiKey = [change objectForKey:NSKeyValueChangeNewKey];
NSString *oldApiKey = [change objectForKey:NSKeyValueChangeOldKey];
if ( newApiKey != oldApiKey ) {
NSLog(@"userApiKey changed. old: %@, new: %@", oldApiKey, newApiKey);
if ( newApiKey != [NSNull null] ) {
[echowaves setEchowavesURI:newApiKey];
[self enableManualUpdateMenuItem:YES];
[self getUpdates];
// restart timer
//if ( ![updateTimer isValid] ) [self startTimer];
} else {
[self resetUpdatedConvos];
[self enableManualUpdateMenuItem:NO];
[self updateStatusbarImage:@"ewBW" withCount:0];
// stop timer
//[updateTimer invalidate];
}
}
}
- (void)resetUpdatedConvos {
[echowaves resetUpdatedConvos];
[self reloadMenuItems];
[self updateStatusbarImage:@"ewBW"];
}
- (IBAction)queryEchowavesServer:(id)sender {
[self getUpdates];
}
- (IBAction)updateApiKey:(id)sender {
// 1. Pop window to enter API Key
// 2. Store users API key into the defaults and Echowaves object
[NSApp activateIgnoringOtherApps:YES];
[apiWindow showWindow:self];
}
- (void)reloadMenuItems {
[statusMenu setAutoenablesItems:NO];
// remove all menu items until the first separator is reached
NSArray *itemArray = [statusMenu itemArray];
for (NSMenuItem *item in itemArray) {
if ([item isSeparatorItem]) {
break; // The first separator item marks the end of the item updates
}
[statusMenu removeItem:item];
}
if ( ([echowaves.updatedConvos count] == 0 ) ) {
// no convo updates
NSMenuItem *newItem = [statusMenu insertItemWithTitle:_noNewConvosMessage action:NULL keyEquivalent:@"" atIndex:0];
[newItem setEnabled:NO];
[self updateStatusbarImage:@"ewBW" withCount:0];
} else {
// real convo updates
[growl growlAlert:[NSString stringWithFormat:@"%d new updated convos", [[echowaves updatedConvos] count]] title:@"Echowaves Notifier"];
for (UpdatedConvo *convo in echowaves.updatedConvos) {
NSMenuItem *newItem = [statusMenu insertItemWithTitle:[NSString stringWithFormat:@"%d - %@", convo.newMessagesCount, [convo truncatedName:_menuTextTruncationSize]] action:@selector(convoSelected:) keyEquivalent:@"" atIndex:0];
[newItem setTarget:self];
[newItem setRepresentedObject:convo.ewURI];
}
[self updateStatusbarImage:@"ewColor" withCount:[echowaves.updatedConvos count]];
}
}
- (void)openEchowavesURL:(NSURL *)urlToOpen {
NSLog(@"Opening URL in browser: %@", urlToOpen);
[[NSWorkspace sharedWorkspace] openURL:urlToOpen];
}
- (void)convoSelected:(id)sender {
id convo_id = [sender representedObject];
NSURL *loadUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", _convoBaseURI, convo_id]];
[self openEchowavesURL:loadUrl];
}
- (void)getUpdates {
// NO OP if no API KEY set
if ( !_userApiKey ) {
NSLog(@"No _userApiKey set. Aborting getUpdates process");
return;
}
echowaves.responseData = [[NSMutableData data] retain]; // FIXME: don't I already alloc/init this in Echowaves.m #init method
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:echowaves.echowavesURI]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
NSString *responseString = [[NSString alloc] initWithData:echowaves.responseData encoding:NSUTF8StringEncoding];
[echowaves.responseData release];
if ( [responseString isEqualToString:@"\"Unable to find specified resource.\""] ) {
NSLog(@"Unable to find specified resource.\n");
} else {
[echowaves resetUpdatedConvos];
NSDictionary *dictionary = [responseString JSONValue];
if ( [dictionary count] ) {
for (NSDictionary *subscription in dictionary ) {
UpdatedConvo *convo = [[UpdatedConvo alloc] initWithConvoName:[[subscription objectForKey:@"subscription"] objectForKey:@"convo_name"]
convoURI:[[subscription objectForKey:@"subscription"] objectForKey:@"conversation_id"]
unreadCount:[[[subscription objectForKey:@"subscription"] objectForKey:@"new_messages_count"] integerValue]];
NSLog(@"Adding convo: %@", convo);
[[echowaves updatedConvos] addObject:convo];
[convo release];
}
}
}
[self reloadMenuItems];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[echowaves.responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[echowaves.responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"Connection failed: %@", [error description]);
[echowaves.updatedConvos removeAllObjects];
// TODO: will adding a 'Connection failure' message cause crashes elsewhere?? probably
[echowaves.updatedConvos addObject:@"Connection failure"];
}
- (void)updateStatusbarImage:(NSString *)imagePathName {
NSBundle *bundle = [NSBundle mainBundle];
[statusItem setImage:[[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:imagePathName ofType:@"png"]]];
}
- (void)updateStatusbarImage:(NSString *)imagePathName withCount:(int)count {
if (count == 0) {
[statusItem setTitle:@""];
} else {
[statusItem setTitle:[NSString stringWithFormat:@"%d", count]];
}
[self updateStatusbarImage:imagePathName];
}
- (void)dealloc {
[echowaves release];
[apiWindow release];
[growl release];
if (updateTimer != nil) {
[updateTimer invalidate];
updateTimer = nil;
}
// Release the 2 images we loaded into memory
[ewImage release];
[ewHighlightImage release];
[super dealloc];
}
@end