-
Notifications
You must be signed in to change notification settings - Fork 2
/
Wordpress.m
298 lines (248 loc) · 7.94 KB
/
Wordpress.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// 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 "Wordpress.h"
@implementation Wordpress
static Wordpress *sharedWordpressManager = nil;
# pragma mark Singleton
+ (Wordpress*)sharedManager
{
@synchronized(self) {
if (sharedWordpressManager == nil) {
[[self alloc] init]; // assignment not done here
}
}
return sharedWordpressManager;
}
+ (id)allocWithZone:(NSZone *)zone
{
@synchronized(self) {
if (sharedWordpressManager == nil) {
sharedWordpressManager = [super allocWithZone:zone];
return sharedWordpressManager; // assignment and return on first allocation
}
}
return nil; //on subsequent allocation attempts return nil
}
- (id)copyWithZone:(NSZone *)zone
{
return self;
}
- (id)retain
{
return self;
}
- (unsigned)retainCount
{
return UINT_MAX; //denotes an object that cannot be released
}
- (void)release
{
//do nothing
}
- (id)autorelease
{
return self;
}
#pragma mark -
- (id)init
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updatePreferences:) name:@"preferencesUpdated" object:nil];
[self updatePreferences];
return [super init];
}
#pragma mark Wordpress
- (void)updatePreferences
{
username = [[NSUserDefaults standardUserDefaults] objectForKey:@"wordpressUsername"];
NSString *urlString = [[NSUserDefaults standardUserDefaults] objectForKey:@"wordpressURL"];
if(url){
[url release];
url = nil;
}
@try {
url = [NSURL URLWithString:@"./xmlrpc.php" relativeToURL:[NSURL URLWithString:urlString]];
[url retain];
} @catch ( NSException *e ) {}
if(url == nil){
[self setErrorMessage:@"Invalid URL." withFaultCode:WP_ERROR_URL];
}
EMKeychainProxy *keychainProxy = [EMKeychainProxy sharedProxy];
EMGenericKeychainItem *keychain = [keychainProxy genericKeychainItemForService:@"WordpressNotifier" withUsername:username];
if(keychain){
password = [keychain password];
}
ZLog(@"updated Wordpress object prefs");
[[NSNotificationCenter defaultCenter] postNotificationName:@"wordpressFinishedUpdatingPreferences" object:self];
}
- (void)updatePreferences: (NSNotification *)notification
{
[self updatePreferences];
}
- (NSString *)lastErrorMessage
{
return lastErrorMessage;
}
- (NSInteger)faultCode
{
return faultCode;
}
- (void)setErrorMessage:(NSString *)message
{
lastErrorMessage = message;
faultCode = WP_ERROR_UNKNOWN;
}
- (void)setErrorMessage:(NSString *)message withFaultCode:(NSInteger)code
{
lastErrorMessage = message;
faultCode = code;
}
- (void)notifyError
{
NSLog(@"Wordpress Notifier Error: '%@' : %i", lastErrorMessage, faultCode);
[[NSNotificationCenter defaultCenter] postNotificationName:@"wordpressError" object:self];
}
- (void)processReponse:(NSObject *)object
{
BOOL isError = NO;
if([object isKindOfClass:[NSArray class]]){
for (NSObject *comment in (NSArray *)object) {
if(![comment isKindOfClass:[NSDictionary class]]){
isError = YES;
}
}
}else{
isError = YES;
}
if(isError == NO){
[self setComments:(NSArray *)object];
[[NSNotificationCenter defaultCenter] postNotificationName:@"commentsUpdated" object:self];
if(previousComments){
[self checkCommentsForNewComments];
}
}else{
[self setErrorMessage:@"Unknown Schema Error"];
[self setComments:nil];
[self notifyError];
}
}
- (void)setComments:(NSArray *)_comments
{
[previousComments release];
previousComments = comments;
[_comments retain];
comments = _comments;
}
- (void)checkCommentsForNewComments
{
NSInteger newCommentsCount = 0;
for (NSDictionary *comment in comments) {
BOOL foundComment = NO;
for (NSDictionary *previousComment in previousComments) {
if([[previousComment objectForKey:@"content"] isEqual:[comment objectForKey:@"content"]]){
foundComment = YES;
}
}
if(foundComment == NO){
newCommentsCount++;
}
}
newComments = newCommentsCount;
if(newCommentsCount > 0){
// DEBUG
ZLog(@"New comments: %i", newCommentsCount);
[[NSNotificationCenter defaultCenter] postNotificationName:@"newComments" object:self];
}
}
- (NSArray *)comments
{
return comments;
}
- (NSArray *)previousComments
{
return previousComments;
}
- (NSInteger)newComments{
return newComments;
}
#pragma mark XMLRPC
- (void)checkComments
{
XMLRPCRequest *request = [[XMLRPCRequest alloc] initWithHost: url];
[request setMethod: @"wp.getComments" withParameters: [NSArray arrayWithObjects: [NSNumber numberWithInt:0], username, password, [NSDictionary dictionaryWithObjectsAndKeys: @"hold", @"status", nil], nil]];
[request setUserAgent: @"wpnotifier"];
XMLRPCConnection *connection = [[XMLRPCConnection alloc] initWithXMLRPCRequest: request delegate: self];
if (connection == nil) {
[self setErrorMessage:@"Unknown XML-RPC error."];
[self notifyError];
}else{
//[connection retain];
}
}
- (void)connection: (XMLRPCConnection *)connection didReceiveResponse:(XMLRPCResponse *)response forMethod: (NSString *)method {
BOOL isFault = NO;
ZLog(@"Response Method: %@", method);
if (response != nil) {
if ([response isFault]) {
switch ([[response faultCode] intValue])
{
case WP_ERROR_BAD_LOGIN:
[self setErrorMessage: @"Bad username/password combination." withFaultCode:WP_ERROR_BAD_LOGIN];
break;
case WP_ERROR_UNSUPPORTED:
[self setErrorMessage: @"Wordpress version unsupported. Please upgrade to Wordpress 2.7." withFaultCode:WP_ERROR_UNSUPPORTED];
break;
case WP_ERROR_XMLRPC:
[self setErrorMessage: @"Wordpress XML-RPC support has not enabled (See Help)." withFaultCode:WP_ERROR_XMLRPC];
break;
default:
[self setErrorMessage: @"Unknown error."];
break;
}
isFault = YES;
[self notifyError];
ELog(@"Response source: %@", [response responseSourceXML]);
//NSLog(@"Fault code: %@", [response faultCode]);
} else {
if(isFault == NO){
[self processReponse: [response responseObject]];
}
ZLog(@"Response object: %@", [response responseObject]);
}
// could be useful for debugging
//NSLog(@"Response source: %@", [response responseSourceXML]);
} else {
[self setErrorMessage: @"Unknown Error: Unable to parse XML response."];
ELog(@"Text: %S",[response responseSourceXML ]);
[self notifyError];
//NSLog(@"Unable to parse response.");
}
[response release];
[connection release];
}
- (void)connection: (XMLRPCConnection *)connection didFailWithError: (NSError *)error forMethod: (NSString *)method{
// NSLog(@"Error Code: %i", [error code]);
if([error code] == 404){
if([[[NSUserDefaults standardUserDefaults] stringForKey:@"wordpressURL"] isEqual:@"http://www.example.com"])
{
[self setErrorMessage: @"Preferences not set." withFaultCode:WP_ERROR_PREFS_NOT_SET];
}else{
[self setErrorMessage: @"Wordpress URL is incorrect. HTTP 404 page not found error." withFaultCode:WP_ERROR_404];
}
}else{
[self setErrorMessage:[error localizedDescription] withFaultCode:WP_ERROR_CONNECTION];
}
[self notifyError];
[connection release];
}
@end