-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAppController.m
275 lines (219 loc) · 8.06 KB
/
AppController.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
267
268
269
270
271
272
273
274
275
// Wordpress Notifier for Mac (wpnotifier) - Mac OS X Status Bar notifications for Wordpress Blogs.
// Copyright (C) 2010 Paul William
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#import "AppController.h"
#import "StatusIconImage.h"
#import "Wordpress.h"
#import <Growl/Growl.h>
@implementation AppController
#pragma mark main
-(id)init
{
[[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys: @"http://www.example.com", @"wordpressURL", [NSNumber numberWithInt:2], @"pollInterval", @"admin", @"wordpressUsername", [NSNumber numberWithBool:YES], @"growl", nil]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(wordpressError:) name:@"wordpressError" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(commentsUpdated:) name:@"commentsUpdated" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(wordpressFinishedUpdatingPreferences:) name:@"wordpressFinishedUpdatingPreferences" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(newComments:) name:@"newComments" object:nil];
return [super init];
}
- (void)awakeFromNib{
[NSApp setMainMenu:systemMenu];
statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength: NSVariableStatusItemLength];
[statusItem retain];
[statusItem setHighlightMode:YES];
[statusItem setMenu:statusMenu];
[[NSURLCache sharedURLCache] setMemoryCapacity:0];
[[NSURLCache sharedURLCache] setDiskCapacity:0];
// fixes growl bug
[GrowlApplicationBridge setGrowlDelegate:@""];
NSBundle *bundle = [NSBundle mainBundle];
greyImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"wordpress_grey" ofType:@"png"]];
blueImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"wordpress_blue" ofType:@"png"]];
redImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"wordpress_red" ofType:@"png"]];
wordpress = [Wordpress sharedManager];
[self setGreyIcon];
[self setupTimer];
if([[[NSUserDefaults standardUserDefaults] stringForKey:@"wordpressURL"] isEqual:@"http://www.example.com"])
{
[self showPreferences:self];
}
[self checkComments];
}
- (void)checkComments
{
[wordpress checkComments];
}
- (void)wordpressError: (NSNotification *)notification{
NSLog(@"Wordpress Error Notification");
[self clearMenu];
NSString *title = [NSString stringWithFormat:@"Error : %@ (%i)", [wordpress lastErrorMessage], [wordpress faultCode]];
NSMenuItem *menuItem = [[NSMenuItem alloc] initWithTitle:title action:nil keyEquivalent:@""];
[menuItem setTarget:self];
[menuItem setEnabled:YES];
[statusMenu insertItem:menuItem atIndex:3];
[menuItem release];
menuItem = nil;
[self setRedIcon];
}
- (void)commentsUpdated: (NSNotification *)notification{
ZLog(@"Comments updated");
[self clearMenu];
for (NSDictionary *comment in [wordpress comments]) {
NSString *content = [comment objectForKey:@"content"];
if([content length] > 30){
content = [NSString stringWithFormat:@"%@ ...", [content substringToIndex:30]];
}
NSString *title = [NSString stringWithFormat:@"%@ : %@", [comment objectForKey:@"author"], content];
NSMenuItem *menuItem = [[NSMenuItem alloc] initWithTitle:title action:nil keyEquivalent:@""];
[menuItem setTarget:self];
[menuItem setEnabled:YES];
[statusMenu insertItem:menuItem atIndex:3];
[menuItem release];
menuItem = nil;
title = nil;
}
if([[wordpress comments] count] == 0){
NSMenuItem *menuItem = [[NSMenuItem alloc] initWithTitle:@"No unapproved comments." action:nil keyEquivalent:@""];
[menuItem setTarget:self];
[menuItem setEnabled:YES];
[statusMenu insertItem:menuItem atIndex:3];
[menuItem release];
menuItem = nil;
[self setGreyIcon];
}else{
[self setBlueIconWithCommentCount:[[wordpress comments] count]];
}
}
- (void)newComments: (NSNotification *)notification
{
NSString *growlTitle;
NSString *growlDescription;
if([wordpress newComments] > 1){
growlTitle = @"New Comments";
growlDescription = [NSString stringWithFormat:@"%i new comments.", [wordpress newComments]];
}else{
growlTitle = @"New Comment";
growlDescription = @"One new comment.";
}
if([[NSUserDefaults standardUserDefaults] integerForKey:@"growl"] == YES){
[GrowlApplicationBridge
notifyWithTitle:growlTitle
description:growlDescription
notificationName:@"New Comment"
iconData:nil
priority:0
isSticky:NO
clickContext:nil];
}
}
- (void)clearMenu
{
int passedSeparator = 0;
for (NSMenuItem *item in [statusMenu itemArray]) {
if([item isSeparatorItem]){
passedSeparator++;
}else if(passedSeparator == 1){
[statusMenu removeItem:item];
}
}
}
- (IBAction)checkComments:(id)sender
{
[self checkComments];
}
#pragma mark status
- (void)setGreyIcon
{
[self setIcon: greyImage];
}
- (void)setRedIcon
{
[self setIcon:redImage];
}
- (void)setBlueIconWithCommentCount:(int)count
{
[self setBlueIconWithString:[[NSNumber numberWithInt:count] stringValue]];
}
- (void)setBlueIconWithString:(NSString *)text;
{
NSImage *newStatusImage = [StatusIconImage makeIconWIthImage:blueImage string:text];
[newStatusImage retain];
[self setIcon: newStatusImage];
}
- (void)setIcon:(NSImage *)image
{
NSImage *oldImage = [statusItem image];
if(![oldImage isEqual:image]){
[statusItem setImage:image];
if(![oldImage isEqual:greyImage] && ![oldImage isEqual:redImage]){
[oldImage release];
}
}
}
#pragma mark prefs
- (IBAction)showPreferences:(id)sender
{
// Is preferenceController nil?
if (!preferenceController) {
preferenceController = [[PreferenceController alloc] init];
}
// NSLog(@"Showing %@", preferenceController);
[NSApp activateIgnoringOtherApps:YES];
[preferenceController showWindow:self];
}
- (void)wordpressFinishedUpdatingPreferences: (NSNotification *)notification
{
//DEBUG
ZLog(@"AppController Prefs Updated");
[self setupTimer];
[self checkComments];
}
#pragma mark misc
- (IBAction)showAboutWindow:(id)sender
{
[NSApp activateIgnoringOtherApps:YES];
[NSApp orderFrontStandardAboutPanel:self];
}
- (IBAction)quit:(id)sender
{
[NSApp terminate:sender];
}
- (IBAction)openComments:(id)sender
{
@try {
NSString *urlString = [[NSUserDefaults standardUserDefaults] objectForKey:@"wordpressURL"];
if([urlString length] != 0){
NSURL *url = [NSURL URLWithString:@"wp-admin/edit-comments.php" relativeToURL:[NSURL URLWithString:urlString]];
[[NSWorkspace sharedWorkspace] openURL:url];
}
} @catch ( NSException *e ) {}
}
- (IBAction)openHelp:(id)sender
{
NSURL *url = [NSURL URLWithString:@"http://paulwilliam.github.com/Wordpress-Notifier/help.php.html"];
[[NSWorkspace sharedWorkspace] openURL:url];
}
- (void)setupTimer
{
int timerInterval = [[NSUserDefaults standardUserDefaults] integerForKey:@"pollInterval"]*60;
if([timer timeInterval] != timerInterval){
ZLog(@"New Timer : %i", timerInterval );
[timer release];
timer = [[NSTimer scheduledTimerWithTimeInterval: timerInterval
target: self
selector: @selector(checkComments:)
userInfo: nil
repeats: YES] retain];
}
}
@end