| 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 | if (actionType == 3) { |
|---|
| 133 | [self handleTypeThree:[pb propertyListForType:@"NSFilenamesPboardType"]]; |
|---|
| 134 | } |
|---|
| 135 | if (actionType == 1) { |
|---|
| 136 | [self handleTypeOne:[ntypes objectForKey:@"stringForType:_NSStringPboardType"]]; |
|---|
| 137 | } |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | // text |
|---|
| 141 | - (void) handleTypeOne: (NSString *) text { |
|---|
| 142 | // NSLog(@"- (void) handleTypeOne: (NSString *) text - called\n"); |
|---|
| 143 | NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; |
|---|
| 144 | NSString *postloc = [userDefaults objectForKey:@"text destination type"]; |
|---|
| 145 | if ([postloc isEqualToString:@"1"]) { |
|---|
| 146 | if ([userDefaults objectForKey:@"vox text destination id"]) { |
|---|
| 147 | VoxAtomApi *client = [[VoxAtomApi alloc] init]; |
|---|
| 148 | [client setAuth:[self defaultaccount] pass:[self defaultAccountPass]]; |
|---|
| 149 | [client setDataarg:text]; |
|---|
| 150 | [client newpost:@"quickpost" args:[NSDictionary dictionaryWithObjectsAndKeys:[userDefaults objectForKey:@"vox text destination id"], @"destinationid", nil]]; |
|---|
| 151 | [self appNotify:[NSString stringWithFormat:@"A new post has been created."] title:@"Post created"]; |
|---|
| 152 | } |
|---|
| 153 | } |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | // image |
|---|
| 157 | - (void) handleTypeThree: (NSArray *) list { |
|---|
| 158 | // NSLog(@"- (void) handleTypeThree: (NSArray *) list - called\n"); |
|---|
| 159 | NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; |
|---|
| 160 | NSString *postloc = [userDefaults objectForKey:@"vox image destination type"]; |
|---|
| 161 | if ([postloc isEqualToString:@"1"]) { |
|---|
| 162 | if ([userDefaults objectForKey:@"vox image destination id"]) { |
|---|
| 163 | NSEnumerator *enumerator = [list objectEnumerator]; |
|---|
| 164 | NSString *path; |
|---|
| 165 | while( path = [enumerator nextObject] ) { |
|---|
| 166 | // NSLog([NSString stringWithFormat:@"Uploading file %@", path]); |
|---|
| 167 | VoxAtomApi *client = [[[VoxAtomApi alloc] init] autorelease]; |
|---|
| 168 | [client setAuth:[self defaultaccount] pass:[self defaultAccountPass]]; |
|---|
| 169 | [client setDataarg:path]; |
|---|
| 170 | [client newpost:@"collection" args:[NSDictionary dictionaryWithObjectsAndKeys:[userDefaults objectForKey:@"vox image destination id"], @"destinationid", nil]]; |
|---|
| 171 | [self appNotify:[NSString stringWithFormat:@"File '%@' has been uploaded", [client filename]] title:@"File Uploaded"]; |
|---|
| 172 | } |
|---|
| 173 | } |
|---|
| 174 | } |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | - (int) handleTypes: (NSPasteboard *) pb { |
|---|
| 178 | // NSLog(@"- (int) handleTypes:(NSPasteboard *) pb - called\n"); |
|---|
| 179 | NSArray *types = [self allowTypes]; |
|---|
| 180 | unsigned int i, count = [types count]; |
|---|
| 181 | int totalcount = 0; |
|---|
| 182 | id methods[] = {@"stringForType:", @"propertyListForType:"}; |
|---|
| 183 | for (i = 0; i < count; i++) { |
|---|
| 184 | int m; |
|---|
| 185 | NSString *type = [types objectAtIndex:i]; |
|---|
| 186 | for (m = 0; m < 2; m++) { |
|---|
| 187 | id obj = [pb performSelector:NSSelectorFromString(methods[m]) withObject:type]; |
|---|
| 188 | if (obj) { |
|---|
| 189 | totalcount++; |
|---|
| 190 | [ntypes setObject:[obj description] forKey:[NSString stringWithFormat:@"%@_%@", methods[m], type]]; |
|---|
| 191 | // NSLog(@"%@ - %@ - %@\n", methods[m], type, [obj description]); |
|---|
| 192 | } |
|---|
| 193 | } |
|---|
| 194 | } |
|---|
| 195 | if (totalcount == 1) { return 1; } |
|---|
| 196 | return 3; |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | - (NSArray *) allowTypes { |
|---|
| 200 | return [NSArray arrayWithObjects:NSURLPboardType, NSStringPboardType, NSFilenamesPboardType, NSHTMLPboardType, nil]; |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | - (NSArray *) defaultAccountCollections { |
|---|
| 204 | return [[accounts objectForKey:[self defaultaccount]] myCollections]; |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | - (NSString *) defaultAccountPass { |
|---|
| 208 | return [[accounts objectForKey:[self defaultaccount]] password]; |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | @end |
|---|