root/trunk/Vox.m @ 39

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

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

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