root/trunk/TypePad.m @ 39

Revision 39, 8.6 kB (checked in by ngerakines, 3 years ago)

* Verified that enable_growl works
* Fixed small bug with userdefaults value

Line 
1//
2//  TypePad.m
3//  TypePad-Uploader
4//
5//  Created by Nicholas Gerakines on 5/1/06.
6//  Copyright 2006 __MyCompanyName__. All rights reserved.
7//
8
9#import "TypePad.h"
10#import "TypePadAccount.h"
11#import "TypePadAtomApi.h"
12
13@implementation TypePad
14
15- (id) init {
16        static TypePad *sharedInstance = nil;
17        if (sharedInstance) {
18        [self autorelease];
19        self = [sharedInstance retain];
20    } else {
21        self = [super init];
22        if (self) {
23            sharedInstance = [self retain];
24                        ntypes = [[NSMutableDictionary alloc] init];
25                        accounts = [[NSMutableDictionary alloc] init];
26                        defaultaccount = [[NSString alloc] init];
27                        // [self preload_values];
28        }
29    }
30    return self;
31}
32
33+ TypePad {
34        static TypePad * sharedInstance = nil;
35        if ( sharedInstance == nil ) {
36                sharedInstance = [[self alloc] init];
37        }
38        return sharedInstance;
39}
40
41/* dealloc */
42- (void) dealloc {
43    [ntypes release];
44    [accounts release];
45        [defaultaccount release];
46       
47    ntypes = nil;
48    accounts = nil;
49        defaultaccount = nil;
50    [super dealloc];
51}
52
53/* class accessors */
54/* actionType */
55- (int) actionType { return actionType; }
56
57/* -setActionType: */
58- (void) setActionType: (int) ActionType {
59    //NSLog(@"in -setActionType, old value of actionType: %i, changed to: %i", actionType, ActionType);
60    actionType = ActionType;
61}
62
63/* ntypes */
64- (NSMutableDictionary *) ntypes { return ntypes; }
65
66/* -setNtypes: */
67- (void) setNtypes: (NSMutableDictionary *) Ntypes {
68    //NSLog(@"in -setNtypes:, old value of ntypes: %@, changed to: %@", ntypes, Ntypes);
69    if (ntypes != Ntypes) {
70        [ntypes autorelease];
71        ntypes = [Ntypes retain];
72    }
73}
74
75/* accounts */
76- (NSMutableDictionary *) accounts { return accounts; }
77
78/* -setAccounts: */
79- (void) setAccounts: (NSMutableDictionary *) Accounts {
80    //NSLog(@"in -setAccounts:, old value of accounts: %@, changed to: %@", accounts, Accounts);
81    if (accounts != Accounts) {
82        [accounts autorelease];
83        accounts = [Accounts retain];
84    }
85}
86
87/* defaultaccount */
88- (NSString *) defaultaccount { return defaultaccount; }
89
90/* -setDefaultaccount: */
91- (void) setDefaultaccount: (NSString *) Defaultaccount {
92    //NSLog(@"in -setDefaultaccount:, old value of defaultaccount: %@, changed to: %@", defaultaccount, Defaultaccount);
93    if (defaultaccount != Defaultaccount) {
94        [defaultaccount autorelease];
95        defaultaccount = [Defaultaccount retain];
96    }
97}
98
99
100/** other stuff **/
101
102- (void) weblog_newpost: (NSString *) text dest:(NSString *) dest {
103        NSLog(@"- (void) weblog_newpost: (NSString *) text - called\n");
104        TypePadAtomAPI *client = [[TypePadAtomAPI alloc] init];
105        [client setAuth:[self defaultaccount] pass:[self defaultAccountPass]];
106        [client setDataarg:text];
107        [client newpost:@"quickpost" args:[NSDictionary dictionaryWithObjectsAndKeys:dest, @"destinationid", nil]];
108        [self appNotify:[NSString stringWithFormat:@"A new post has been created."] title:@"Post created"];
109}
110
111- (void) weblog_upload: (NSArray *) files dest:(NSString *) dest {
112        NSLog(@"- (void) weblog_upload: (NSArray *) files - called\n");
113        NSEnumerator *enumerator = [files objectEnumerator];
114        NSString *path;
115        while( path = [enumerator nextObject] ) {
116                NSLog([NSString stringWithFormat:@"Uploading file %@", path]);
117                TypePadAtomAPI *client = [[[TypePadAtomAPI alloc] init] autorelease];
118                [client setAuth:[self defaultaccount] pass:[self defaultAccountPass]];
119                [client setDataarg:path];
120                [client newpost:@"0" args:[NSDictionary dictionaryWithObjectsAndKeys:dest, @"destinationid", nil]];
121                [self appNotify:[NSString stringWithFormat:@"File '%@' has been uploaded", [client filename]] title:@"File Uploaded"];
122        }
123}
124
125- (void) gallery_upload: (NSArray *)files dest:(NSString *) dest {
126        NSLog(@"- (void) gallery_upload: (NSArray *) files - called\n");
127        NSEnumerator *enumerator = [files objectEnumerator];
128        NSString *path;
129        while( path = [enumerator nextObject] ) {
130                NSLog([NSString stringWithFormat:@"Uploading file %@", path]);
131                TypePadAtomAPI *client = [[[TypePadAtomAPI alloc] init] autorelease];
132                [client setAuth:[self defaultaccount] pass:[self defaultAccountPass]];
133                [client setDataarg:path];
134                [client newpost:@"1" args:[NSDictionary dictionaryWithObjectsAndKeys:dest, @"destinationid", nil]];
135                [self appNotify:[NSString stringWithFormat:@"File '%@' has been uploaded", [client filename]] title:@"File Uploaded"];
136        }
137}
138
139- (void) appNotify: (NSString *) message title: (NSString *) title {
140        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
141        // NSString *usegrowl = [userDefaults objectForKey:@"enable_growl"];
142        if ([userDefaults objectForKey:@"enable_growl"]) {
143                        [GrowlApplicationBridge notifyWithTitle:title description:message notificationName:@"Fence" iconData:nil priority:0 isSticky:NO clickContext:nil];
144        }
145}
146
147- (void) dispatchAction: (NSPasteboard *) pb {
148        actionType = [self handleTypes:pb];
149        switch (actionType) {
150                case 1:
151                        [self handleTypeOne:[ntypes objectForKey:@"stringForType:_NSStringPboardType"]];
152                        break;
153                case 3:
154                        [self handleTypeThree:[pb propertyListForType:@"NSFilenamesPboardType"]];
155                        break;
156                default:
157                        NSLog(@"Something went horribly wrong.\n");
158        }
159}
160
161// text
162- (void) handleTypeOne: (NSString *) text {
163        NSLog(@"- (void) handleTypeOne: (NSString *) text - called\n");
164        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
165        NSString *postloc = [userDefaults objectForKey:@"text destination type"];
166        if ([postloc isEqualToString:@"1"]) {
167                if ([userDefaults objectForKey:@"text destination id"]) {
168                        [self weblog_newpost:text dest:[userDefaults objectForKey:@"text destination id"]];
169                }
170        }
171}
172
173// image
174- (void) handleTypeThree: (NSArray *) list {
175        NSLog(@"- (void) handleTypeThree: (NSArray *) list - called\n");
176        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
177        NSString *postloc = [userDefaults objectForKey:@"image destination type"];
178        if ([postloc isEqualToString:@"1"]) {
179                if ([userDefaults objectForKey:@"image destination id"]) {
180                        [self weblog_upload:list dest:[userDefaults objectForKey:@"image destination id"]];
181                }
182        }
183        if ([postloc isEqualToString:@"2"]) {
184                if ([userDefaults objectForKey:@"image destination id"] ) {
185                        [self gallery_upload:list dest:[userDefaults objectForKey:@"image destination id"]];
186                }
187        }
188}
189
190- (int) handleTypes: (NSPasteboard *) pb {
191        NSLog(@"- (int) handleTypes:(NSPasteboard *) pb - called\n");
192    NSArray *types = [self allowTypes];
193        unsigned int i, count = [types count];
194        int totalcount = 0;
195        id methods[] = {@"stringForType:", @"propertyListForType:"};
196        for (i = 0; i < count; i++) {
197                int m;
198                NSString *type = [types objectAtIndex:i];
199                for (m = 0; m < 2; m++) {
200                        id obj = [pb performSelector:NSSelectorFromString(methods[m]) withObject:type];
201                        if (obj) {
202                                totalcount++;
203                                [ntypes setObject:[obj description] forKey:[NSString stringWithFormat:@"%@_%@",  methods[m], type]];
204                                NSLog(@"%@ - %@ - %@\n",  methods[m], type, [obj description]);
205                        }
206                }
207        }
208        if (totalcount == 1) { return 1; }
209        // NSString *possibleurl = [ntypes objectForKey:@"stringForType:_NSStringPboardType"];
210        // if (possibleurl) { return 2; }
211        return 3;
212}
213
214- (NSArray *) allowTypes {
215        return [NSArray arrayWithObjects:NSURLPboardType, NSStringPboardType, NSFilenamesPboardType, NSHTMLPboardType, nil];
216}
217
218- (void) gotoDonate {
219        [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://blog.socklabs.com/typepad-uploader/"]];
220}
221
222- (BOOL) addAccount: (NSString *) login pass: (NSString *) pass {
223        TypePadAccount *tpuser = [[[TypePadAccount alloc] init] autorelease];
224        [tpuser setUsername:login];
225        [tpuser setPassword:pass];
226        [tpuser discoverWeblogs];
227        [tpuser discoverGalleries];
228        if ([tpuser hasWeblog]) {
229                [accounts setObject:tpuser forKey:login];
230                [self setDefaultaccount:login];
231                return YES;
232        }
233        return NO;
234}
235
236- (void) saveAccounts {
237        NSMutableData *data = [NSMutableData data];
238        NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
239        // TODO -- keep our dataset as bin data, find a better way of doing this
240        // [archiver setOutputFormat:NSPropertyListXMLFormat_v1_0];
241        NSEnumerator *enumerator = [accounts keyEnumerator];
242        id key;
243        while (key = [enumerator nextObject]) {
244                [archiver encodeObject:[accounts objectForKey:key] forKey:key];
245        }
246        [archiver finishEncoding];
247        [archiver release];
248       
249        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
250        [userDefaults setObject:data forKey:@"TypePad Accounts"];
251        [userDefaults synchronize];
252}
253
254- (NSArray *) defaultAccountWeblogs {
255        return [[accounts objectForKey:[self defaultaccount]] weblogs];
256}
257
258- (NSArray *) defaultAccountGalleries {
259        return [[accounts objectForKey:[self defaultaccount]] galleries];
260}
261
262- (NSString *) defaultAccountPass {
263        return [[accounts objectForKey:[self defaultaccount]] password];
264}
265
266@end
Note: See TracBrowser for help on using the browser.