forked from orta/tumblrclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApplicationController.m
229 lines (189 loc) · 8.97 KB
/
ApplicationController.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
//
// ApplicationController.m
// Tumblor
//
// Created by orta on 19/07/2008.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import "ApplicationController.h"
#import "NSURL_UrlFromFSRef.h"
#import <iMediaBrowser/iMediaBrowser.h>
#import "MMICrashReporterBridge.h"
@implementation ApplicationController
- (void) awakeFromNib{
[self registerDefaults];
[[NSAppleEventManager sharedAppleEventManager] setEventHandler: self
andSelector: @selector (recievedRSSEvent: withReplyEvent:)
forEventClass: EditDataItemAppleEventClass
andEventID: EditDataItemAppleEventID];
[[NSAppleEventManager sharedAppleEventManager] setEventHandler: self
andSelector: @selector (post: withReplyEvent:)
forEventClass: TumblrPostClass
andEventID: TumblrPostEventID];
[[NSAppleEventManager sharedAppleEventManager] setEventHandler: self
andSelector: @selector (postFile: withReplyEvent:)
forEventClass: TumblrPostClass
andEventID: TumblrPostFileEventID];
[[NSAppleEventManager sharedAppleEventManager] setEventHandler: self
andSelector: @selector (send: withReplyEvent:)
forEventClass: TumblrPostClass
andEventID: TumblrPostSendID];
[[NSAppleEventManager sharedAppleEventManager] setEventHandler:self
andSelector:@selector(gotIncomingURL: withReplyEvent:)
forEventClass:kInternetEventClass
andEventID:kAEGetURL];
log(@"added events");
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setiMediaDelegate) name:@"ORAniMediaViewActivated" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(postSucceeded) name:@"ORPostedSuccessfully" object:nil];
}
- (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename{
log(@"application open file = %@", filename);
NSString *extension = [filename pathExtension];
if ([extension caseInsensitiveCompare:@"qsscpt"] == NSOrderedSame ||
[extension caseInsensitiveCompare:@"scpt"] == NSOrderedSame ) {
[TumblrQSHandler dealWithPath: filename];
return YES;
}
[tumblrController importFileContentWithPath:filename andDescription:@""];
return YES;
}
- (IBAction)bugReport:(id)sender{
[MMICrashReporterBridge reportBug];
}
- (void) gotIncomingURL:(NSAppleEventDescriptor*) event withReplyEvent:(NSAppleEventDescriptor *) reply{
NSString *incomingURL = [[event descriptorForKeyword: '----'] stringValue];
incomingURL = [incomingURL urlUnEncodeValue];
NSArray *fields = [incomingURL componentsSeparatedByString:@"&"];
NSEnumerator *e = [fields objectEnumerator];
NSString *field = @"";
NSString *url = @"";
NSString *title = @"";
int type = 99;
NSString *description = @"";
while ((field = [e nextObject])) {
NSArray *keyvalue = [field componentsSeparatedByString:@"="];
NSString *key = [keyvalue objectAtIndex:0] ;
NSString *value = [keyvalue objectAtIndex:1];
log(@"key = '%@' - value = '%@'", key, value);
if([key caseInsensitiveCompare:@"Tumblg:url"] == NSOrderedSame){
url = value;
}
else if ([key caseInsensitiveCompare:@"title"] == NSOrderedSame) {
title = value;
}
else if ([key caseInsensitiveCompare:@"description"] == NSOrderedSame) {
description = value;
}
else if ([key caseInsensitiveCompare:@"type"] == NSOrderedSame) {
type = [value intValue];
}else{
//incase of & signs in URL
NSLog(@"don't know this key %@", key);
url = [url stringByAppendingString:value];
}
}
[tumblrController importTextContentWithBody:description title:title summary:@"" link:url source:title asType:type];
}
- (void) recievedRSSEvent: (NSAppleEventDescriptor *) event
withReplyEvent: (NSAppleEventDescriptor *) reply {
NSAppleEventDescriptor *recordDescriptor = [event descriptorForKeyword: keyDirectObject];
NSString *title = [[recordDescriptor descriptorForKeyword: DataItemTitle] stringValue];
NSString *body = [[recordDescriptor descriptorForKeyword: DataItemDescription] stringValue];
NSString *summary = [[recordDescriptor descriptorForKeyword: DataItemSummary] stringValue];
NSString *link = [[recordDescriptor descriptorForKeyword: DataItemLink] stringValue];
// NSString *permalink = [[recordDescriptor descriptorForKeyword: DataItemPermalink] stringValue];
NSString *sourceName = [[recordDescriptor descriptorForKeyword: DataItemSourceName] stringValue];
//NSString *sourceHomeURL = [[recordDescriptor descriptorForKeyword: DataItemSourceHomeURL] stringValue];
[tumblrController importTextContentWithBody:body title:title summary:summary link:link source:sourceName asType:99];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification{
[self setiMediaDelegate];
if([tumblrController loggedIn] == true){
[[NSNotificationCenter defaultCenter] postNotificationName:@"ORUserLoggedIn" object:@""];
}
}
+ (NSString *)applicationSupportDirectory {
return [[[NSHomeDirectory() stringByAppendingPathComponent:@"Library"] stringByAppendingPathComponent:@"Application Support"] stringByAppendingPathComponent:@"Tumblg"];
}
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication{
return YES;
}
// from applescript post "x" as type "y"
-(void) post:(NSAppleEventDescriptor *) event
withReplyEvent: (NSAppleEventDescriptor *) reply{
NSString *text = [[event descriptorForKeyword: PostText] stringValue];
NSString* type = [[event descriptorForKeyword: PostType] stringValue];
int postType = 99;
log(@"post text = %@ - type = %i", text, type);
if([type caseInsensitiveCompare:@"text"] == NSOrderedSame){
postType = 0;
}
else if ([type caseInsensitiveCompare:@"quote"] == NSOrderedSame) {
postType = 2;
}
else if ([type caseInsensitiveCompare:@"chat"] == NSOrderedSame) {
postType = 3;
}
else if ([type caseInsensitiveCompare:@"link"] == NSOrderedSame) {
postType = 4;
}
[tumblrController importTextContentWithBody:text title:@"" summary:text link:text source:@"" asType:postType];
}
// from applescript post file "x" with description "y"
-(void) postFile:(NSAppleEventDescriptor *) event
withReplyEvent: (NSAppleEventDescriptor *) reply{
log(@"file posted event = %@", event);
NSString *description = [[event descriptorForKeyword: PostImageDescription] stringValue];
// Based on Adium's NSAppleEventDescriptor+NDAppleScriptObject
id theURL = nil;
OSAError theError;
unsigned int theSize;
Handle theAliasHandle;
FSRef theTarget;
Boolean theWasChanged;
theSize = (unsigned int)AEGetDescDataSize([[event descriptorForKeyword: '----'] aeDesc]);
theAliasHandle = NewHandle( theSize );
HLock(theAliasHandle);
theError = AEGetDescData([[event descriptorForKeyword: '----'] aeDesc], *theAliasHandle, theSize);
HUnlock(theAliasHandle);
if( theError == noErr && FSResolveAlias( NULL, (AliasHandle)theAliasHandle, &theTarget, &theWasChanged ) == noErr ){
theURL = [NSURL URLWithFSRef:&theTarget];
}
DisposeHandle(theAliasHandle);
[tumblrController importFileContentWithPath:[theURL path] andDescription:description];
}
-(void) send:(NSAppleEventDescriptor *) event withReplyEvent: (NSAppleEventDescriptor *) reply{
log(@"send event recieved = %@", event);
[postController post:self];
}
- (BOOL)application:(NSApplication *)sender delegateHandlesKey:(NSString *)key {
log(@"%@ - delegateHandlesKey", key);
return YES;
}
- (void) registerDefaults{
log(@"registering defaults");
NSDictionary* defaults = [NSDictionary dictionaryWithObjectsAndKeys:
@"1", @"storePasswordInKeychain",
@"1", @"superfluousVisualEffects",
nil];
NSUserDefaults * defs = [NSUserDefaults standardUserDefaults];
[defs registerDefaults: defaults];
[defs synchronize];
}
- (void) setiMediaDelegate{
NSLog(@"delegating");
[[iMediaConfiguration sharedConfiguration] setDelegate:tumblrController];
}
- (IBAction) aboutApp:(id)sender{
[NSBundle loadNibNamed:@"About" owner:self];
}
- (void)postSucceeded{
if ( [[[NSUserDefaults standardUserDefaults] objectForKey:@"openBlogAfterPost"] boolValue] ){
NSLog(@"TODO: open blog");
// [tumblrController openCurrentBlog];
}
if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"openBlogAfterPost"] boolValue] ){
NSLog(@"TODO: close app");
}
}
@end