root/trunk/TypePad.m @ 41

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

* Put into place the real icons that Wiley sent over
* Fixed the prefs on login
* Removed the TypePadSuper class
* General code cleanup

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