00001
00002
00003
00004
00005
00006
00007
00008
00009 #import "DropIO.h"
00010 #import "UIImage+cropSquare.h"
00011
00012 #define THUMB_SIZE 32.0
00013
00014 @implementation DropIOAsset
00015
00016 @synthesize properties;
00017 @synthesize drop;
00018
00019 static NSDateFormatter* utcFormatter = nil;
00020
00021 + (DropIOAsset*) assetWithProperties:(NSMutableDictionary*)propDict
00022 {
00023 DropIOAsset* asset = nil;
00024 NSString* type = [propDict valueForKey:kDropIOParamKey_Type];
00025 if ([type isEqualToString:kDropIOAssetType_Note]) asset = [[DropIONote alloc] init];
00026 else
00027 if ([type isEqualToString:kDropIOAssetType_Link]) asset = [[DropIOLink alloc] init];
00028 else
00029 if ([type isEqualToString:kDropIOAssetType_Document]) asset = [[DropIODocument alloc] init];
00030 else
00031 if ([type isEqualToString:kDropIOAssetType_Image]) asset = [[DropIOImage alloc] init];
00032 else
00033 if ([type isEqualToString:kDropIOAssetType_Audio]) asset = [[DropIOAudio alloc] init];
00034 else
00035 if ([type isEqualToString:kDropIOAssetType_Movie]) asset = [[DropIOMovie alloc] init];
00036 else
00037 asset = [[DropIOAsset alloc] init];
00038
00039 asset.properties = propDict;
00040
00041 return [asset autorelease];
00042 }
00043
00044 - (id) init
00045 {
00046 if ((self = [super init]) != nil)
00047 {
00048 properties = [[NSMutableDictionary alloc] initWithCapacity:10];
00049 }
00050 return self;
00051 }
00052
00053 - (void) dealloc
00054 {
00055 [properties release];
00056
00057 [super dealloc];
00058 }
00059
00060 - (void) update
00061 {
00062 [DropIO setLastError:nil];
00063
00064 if (drop != nil)
00065 {
00066
00067 NSString* token = [drop bestTokenIncludingPassword:NO];
00068
00069 NSMutableDictionary* params = [NSMutableDictionary dictionaryWithCapacity:8];
00070
00071
00072 [params setObject:kDropIOParamValue_Format_XML forKey:kDropIOParamKey_Format];
00073 [params setObject:kDropIOParamValue_Version_Latest forKey:kDropIOParamKey_Version];
00074 [params setObject:[DropIO APIKey] forKey:kDropIOParamKey_APIKey];
00075 [params setObject:token forKey:kDropIOParamKey_Token];
00076
00077
00078 if (self.title != nil)
00079 [params setObject:self.title
00080 forKey:kDropIOParamKey_Title];
00081
00082 if (self.description != nil)
00083 [params setObject:self.description
00084 forKey:kDropIOParamKey_Description];
00085
00086 if ([self.type isEqualToString:kDropIOAssetType_Link])
00087 {
00088 if (((DropIOLink*)self).url != nil)
00089 [params setObject:[((DropIOLink*)self).url absoluteString]
00090 forKey:kDropIOParamKey_Url];
00091 }
00092 if ([self.type isEqualToString:kDropIOAssetType_Note])
00093 {
00094 if (((DropIONote*)self).contents != nil)
00095 [params setObject:((DropIONote*)self).contents
00096 forKey:kDropIOParamKey_Contents];
00097 }
00098
00099 [drop assetOperation:@"PUT"
00100 atUrl:[NSString stringWithFormat:kDropIOUpdateAssetUrlFormat, drop.name, self.name]
00101 withParameters:params];
00102 }
00103 }
00104
00105 - (void) delete
00106 {
00107 [DropIO setLastError:nil];
00108
00109 if (drop != nil)
00110 {
00111
00112 NSString* token = [drop bestTokenIncludingPassword:NO];
00113
00114 NSMutableDictionary* params = [NSMutableDictionary dictionaryWithCapacity:4];
00115
00116
00117 [params setObject:kDropIOParamValue_Format_XML forKey:kDropIOParamKey_Format];
00118 [params setObject:kDropIOParamValue_Version_Latest forKey:kDropIOParamKey_Version];
00119 [params setObject:[DropIO APIKey] forKey:kDropIOParamKey_APIKey];
00120 [params setObject:token forKey:kDropIOParamKey_Token];
00121
00122 [drop assetOperation:@"DELETE"
00123 atUrl:[NSString stringWithFormat:kDropIOUpdateAssetUrlFormat, drop.name, self.name]
00124 withParameters:params];
00125
00126 if ([DropIO lastError] != nil)
00127 {
00128
00129 if ([[DropIO lastError] code] == kDropIOErrorCode_AssetDeleted)
00130 {
00131 [DropIO setLastError:nil];
00132 [self setValue:[NSNull null] forKey:kDropIOParamKey_AssetAPIUrl];
00133
00134 DropIODrop* parentDrop = self.drop;
00135 self.drop = nil;
00136
00137 NSInteger maxAssets = [parentDrop.assetCount integerValue];
00138 parentDrop.assetCount = [NSNumber numberWithInteger:--maxAssets];
00139
00140 [parentDrop.assets removeObject:self];
00141 }
00142 }
00143 }
00144 }
00145
00146 - (void) sendWithParameters:(NSDictionary*)params
00147 {
00148 [drop assetOperation:@"POST"
00149 atUrl:[NSString stringWithFormat:kDropIOSendAssetUrlFormat, drop.name, self.name]
00150 withParameters:params];
00151
00152 if (([DropIO lastError] != nil) && ([[DropIO lastError] code] == kDropIOErrorCode_SendToSuccess))
00153 {
00154 [DropIO setLastError:nil];
00155 }
00156 }
00157
00158 - (void) sendToEmail:(NSArray*)emailAddressArray message:(NSString*)optionalMessageText
00159 {
00160 [DropIO setLastError:nil];
00161
00162 NSMutableString* emailText = [[NSMutableString alloc] init];
00163 for (NSString* addr in emailAddressArray)
00164 {
00165 if ([emailText length] > 0)
00166 [emailText appendString:@","];
00167 [emailText appendString:addr];
00168 }
00169
00170 NSString* token = [drop bestTokenIncludingPassword:YES];
00171
00172 NSMutableDictionary* params = [NSMutableDictionary dictionaryWithCapacity:7];
00173
00174
00175 [params setObject:kDropIOParamValue_Format_XML forKey:kDropIOParamKey_Format];
00176 [params setObject:kDropIOParamValue_Version_Latest forKey:kDropIOParamKey_Version];
00177 [params setObject:[DropIO APIKey] forKey:kDropIOParamKey_APIKey];
00178 [params setObject:token forKey:kDropIOParamKey_Token];
00179 [params setObject:kDropIOParamValue_Medium_Email forKey:kDropIOParamKey_Medium];
00180 [params setObject:emailText forKey:kDropIOParamKey_Emails];
00181 if (optionalMessageText != nil)
00182 [params setObject:optionalMessageText forKey:kDropIOParamKey_Message];
00183
00184 [self sendWithParameters:params];
00185
00186 [emailText release];
00187 }
00188
00189 - (void) sendToDropNamed:(NSString*)dropName
00190 {
00191 [DropIO setLastError:nil];
00192
00193 NSString* token = [drop bestTokenIncludingPassword:YES];
00194
00195 NSMutableDictionary* params = [NSMutableDictionary dictionaryWithCapacity:6];
00196
00197
00198 [params setObject:kDropIOParamValue_Format_XML forKey:kDropIOParamKey_Format];
00199 [params setObject:kDropIOParamValue_Version_Latest forKey:kDropIOParamKey_Version];
00200 [params setObject:[DropIO APIKey] forKey:kDropIOParamKey_APIKey];
00201 [params setObject:token forKey:kDropIOParamKey_Token];
00202 [params setObject:kDropIOParamValue_Medium_Drop forKey:kDropIOParamKey_Medium];
00203 [params setObject:dropName forKey:kDropIOParamKey_DropName];
00204
00205 [self sendWithParameters:params];
00206 }
00207
00208 - (void) sendToFax:(NSString*)faxNumber
00209 {
00210 [DropIO setLastError:nil];
00211
00212 NSString* token = [drop bestTokenIncludingPassword:YES];
00213
00214 NSMutableDictionary* params = [NSMutableDictionary dictionaryWithCapacity:6];
00215
00216
00217 [params setObject:kDropIOParamValue_Format_XML forKey:kDropIOParamKey_Format];
00218 [params setObject:kDropIOParamValue_Version_Latest forKey:kDropIOParamKey_Version];
00219 [params setObject:[DropIO APIKey] forKey:kDropIOParamKey_APIKey];
00220 [params setObject:token forKey:kDropIOParamKey_Token];
00221 [params setObject:kDropIOParamValue_Medium_Fax forKey:kDropIOParamKey_Medium];
00222 [params setObject:faxNumber forKey:kDropIOParamKey_FaxNumber];
00223
00224 [self sendWithParameters:params];
00225 }
00226
00227 - (NSString*) name
00228 {
00229 return [properties valueForKey:@"name"];
00230 }
00231
00232 - (void) setName:(NSString*)value
00233 {
00234 [properties setValue:value forKey:@"name"];
00235 }
00236
00237 - (NSString*) type
00238 {
00239 return [properties valueForKey:@"type"];
00240 }
00241
00242 - (void) setType:(NSString*)value
00243 {
00244 [properties setValue:value forKey:@"type"];
00245 }
00246
00247 - (NSString*) title
00248 {
00249 return [properties valueForKey:@"title"];
00250 }
00251
00252 - (void) setTitle:(NSString*)value
00253 {
00254 [properties setValue:value forKey:@"title"];
00255 }
00256
00257 - (NSString*) description
00258 {
00259 return [properties valueForKey:@"description"];
00260 }
00261
00262 - (void) setDescription:(NSString*)value
00263 {
00264 [properties setValue:value forKey:@"description"];
00265 }
00266
00267 - (NSNumber*) filesize
00268 {
00269 NSString* valStr = [properties valueForKey:@"filesize"];
00270 if (valStr == nil)
00271 return nil;
00272
00273 return [NSNumber numberWithInteger:[valStr integerValue]];
00274 }
00275
00276 - (void) setFilesize:(NSNumber*)value
00277 {
00278 [properties setValue:[value stringValue] forKey:@"filesize"];
00279 }
00280
00281 - (NSDate*) createdAt
00282 {
00283 NSString* valStr = [properties valueForKey:@"createdAt"];
00284 if (valStr == nil)
00285 return nil;
00286
00287 if (utcFormatter == nil)
00288 {
00289 utcFormatter = [[NSDateFormatter alloc] init];
00290 [utcFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss 'UTC'"];
00291 }
00292 NSDate* createdAtDate = [utcFormatter dateFromString:valStr];
00293
00294 return createdAtDate;
00295 }
00296
00297 - (void) setCreatedAt:(NSDate*)value
00298 {
00299 if (utcFormatter == nil)
00300 {
00301 utcFormatter = [[NSDateFormatter alloc] init];
00302 [utcFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss 'UTC'"];
00303 }
00304 NSString* strValue = [utcFormatter stringFromDate:value];
00305 [properties setValue:strValue forKey:@"createdAt"];
00306 }
00307
00308 - (NSString*) status
00309 {
00310 return [properties valueForKey:@"status"];
00311 }
00312
00313 - (void) setStatus:(NSString*)value
00314 {
00315 [properties setValue:value forKey:@"status"];
00316 }
00317
00318 - (NSURL*) hiddenUrl
00319 {
00320 NSString* valStr = [properties valueForKey:@"hiddenUrl"];
00321 if (valStr == nil)
00322 return nil;
00323
00324 return [NSURL URLWithString:valStr];
00325 }
00326
00327 - (void) setHiddenUrl:(NSURL*)value
00328 {
00329 [properties setValue:[value absoluteString] forKey:@"hiddenUrl"];
00330 }
00331
00332 - (UIImage*) icon
00333 {
00334 return [UIImage imageNamed:[NSString stringWithFormat:@"%@.png", [self type]]];
00335 }
00336
00337
00338
00339 - (void)setValue:(id)value
00340 forUndefinedKey:(NSString *)key
00341 {
00342 [properties setValue:value forKey:key];
00343 }
00344
00345 @end
00346
00347 #pragma mark -
00348 #pragma mark DropIOAsset Subclasses
00349
00350 #pragma mark -
00351
00352
00353
00354
00355 @implementation DropIONote
00356
00357 - (NSString*) contents
00358 {
00359 return [properties valueForKey:@"contents"];
00360 }
00361
00362 - (void) setContents:(NSString*)value
00363 {
00364 [properties setValue:value forKey:@"contents"];
00365 }
00366
00367 @end
00368
00369 #pragma mark -
00370
00371
00372
00373
00374 @implementation DropIOLink
00375
00376 - (NSURL*) url
00377 {
00378 NSString* valStr = [properties valueForKey:@"url"];
00379 if (valStr == nil)
00380 return nil;
00381
00382 return [NSURL URLWithString:valStr];
00383 }
00384
00385 - (void) setUrl:(NSURL*)value
00386 {
00387 [properties setValue:[value absoluteString] forKey:@"url"];
00388 }
00389
00390 - (UIImage*) icon
00391 {
00392 NSString* imageName = nil;
00393
00394 NSString* urlStr = (NSString*)[properties valueForKey:@"url"];
00395 if (([urlStr rangeOfString:@"http://drop.io/"].location != NSNotFound)
00396 || ([urlStr rangeOfString:@"http://www.drop.io/"].location != NSNotFound))
00397 {
00398
00399
00400 if ([urlStr rangeOfString:@"drop.io/faq"].location == NSNotFound)
00401 imageName = @"drop.png";
00402 else
00403 imageName = @"link.png";
00404 }
00405 else
00406 imageName = @"link.png";
00407 return [UIImage imageNamed:imageName];
00408 }
00409
00410 @end
00411
00412 #pragma mark -
00413
00414
00415
00416
00417 @implementation DropIOImage
00418
00419 - (void) dealloc
00420 {
00421 [thumbImage release];
00422 [super dealloc];
00423 }
00424
00425 - (NSURL*) thumbnail
00426 {
00427 NSString* valStr = [properties valueForKey:@"thumbnail"];
00428 if (valStr == nil)
00429 return nil;
00430
00431 return [NSURL URLWithString:valStr];
00432 }
00433
00434 - (void) setThumbnail:(NSURL*)value
00435 {
00436 [properties setValue:[value absoluteString] forKey:@"thumbnail"];
00437 }
00438
00439 - (NSURL*) file
00440 {
00441 NSString* valStr = [properties valueForKey:@"file"];
00442 if (valStr == nil)
00443 return nil;
00444
00445 return [NSURL URLWithString:valStr];
00446 }
00447
00448 - (void) setFile:(NSURL*)value
00449 {
00450 [properties setValue:[value absoluteString] forKey:@"file"];
00451 }
00452
00453 - (NSURL*) converted
00454 {
00455 NSString* valStr = [properties valueForKey:@"converted"];
00456 if (valStr == nil)
00457 return nil;
00458
00459 return [NSURL URLWithString:valStr];
00460 }
00461
00462 - (void) setConverted:(NSURL*)value
00463 {
00464 [properties setValue:[value absoluteString] forKey:@"converted"];
00465 }
00466
00467 - (NSNumber*) height
00468 {
00469 NSString* valStr = [properties valueForKey:@"height"];
00470 if (valStr == nil)
00471 return nil;
00472
00473 return [NSNumber numberWithInteger:[valStr integerValue]];
00474 }
00475
00476 - (void) setHeight:(NSNumber*)value
00477 {
00478 [properties setValue:[value stringValue] forKey:@"height"];
00479 }
00480
00481 - (NSNumber*) width
00482 {
00483 return [NSNumber numberWithInteger:[[properties valueForKey:@"width"] integerValue]];
00484 }
00485
00486 - (void) setWidth:(NSNumber*)value
00487 {
00488 [properties setValue:[value stringValue] forKey:@"width"];
00489 }
00490
00491 - (void) loadThumbnailImage:(NSURL*)thumbURL
00492 {
00493 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
00494 thumbImage = [[UIImage imageWithData:[NSData dataWithContentsOfURL:thumbURL]] retain];
00495 [self performSelector:@selector(cropThumbnailImage) onThread:[NSThread mainThread] withObject:nil waitUntilDone:NO];
00496 [pool release];
00497 }
00498
00499 - (void) cropThumbnailImage
00500 {
00501 UIImage* croppedImage = [thumbImage cropSquare:THUMB_SIZE];
00502 [thumbImage release];
00503 thumbImage = [croppedImage retain];
00504
00505 if ((loadedTarget != nil) && (loadedSel != nil))
00506 {
00507 [loadedTarget performSelector:loadedSel];
00508
00509 [loadedTarget release];
00510 loadedTarget = nil;
00511 loadedSel = nil;
00512 }
00513 }
00514
00515 - (UIImage*) icon
00516 {
00517 NSURL* thumbURL = [self thumbnail];
00518 if (thumbURL != nil)
00519 {
00520 if (thumbImage != nil)
00521 return thumbImage;
00522
00523 [self performSelectorInBackground:@selector(loadThumbnailImage:) withObject:thumbURL];
00524 }
00525
00526 return [super icon];
00527 }
00528
00529 - (BOOL) isThumbnailLoaded
00530 {
00531 return (thumbImage != nil);
00532 }
00533
00534 - (void) whenThumbnailIsLoadedPerformSelector:(SEL)selector target:(id)obj
00535 {
00536 loadedTarget = [obj retain];
00537 loadedSel = selector;
00538 }
00539
00540 @end
00541
00542 #pragma mark -
00543
00544
00545
00546
00547 @implementation DropIODocument
00548 - (NSURL*) file
00549 {
00550 NSString* valStr = [properties valueForKey:@"file"];
00551 if (valStr == nil)
00552 return nil;
00553
00554 return [NSURL URLWithString:valStr];
00555 }
00556
00557 - (void) setFile:(NSURL*)value
00558 {
00559 [properties setValue:[value absoluteString] forKey:@"file"];
00560 }
00561
00562 - (NSURL*) converted
00563 {
00564 NSString* valStr = [properties valueForKey:@"converted"];
00565 if (valStr == nil)
00566 return nil;
00567
00568 return [NSURL URLWithString:valStr];
00569 }
00570
00571 - (void) setConverted:(NSURL*)value
00572 {
00573 [properties setValue:[value absoluteString] forKey:@"converted"];
00574 }
00575
00576 - (NSNumber*) pages
00577 {
00578 NSString* valStr = [properties valueForKey:@"pages"];
00579 if (valStr == nil)
00580 return nil;
00581
00582 return [NSNumber numberWithInteger:[valStr integerValue]];
00583 }
00584
00585 - (void) setPages:(NSNumber*)value
00586 {
00587 [properties setValue:[value stringValue] forKey:@"pages"];
00588 }
00589
00590 @end
00591
00592 #pragma mark -
00593
00594
00595
00596
00597 @implementation DropIOAudio
00598 - (NSURL*) file
00599 {
00600 NSString* valStr = [properties valueForKey:@"file"];
00601 if (valStr == nil)
00602 return nil;
00603
00604 return [NSURL URLWithString:valStr];
00605 }
00606
00607 - (void) setFile:(NSURL*)value
00608 {
00609 [properties setValue:[value absoluteString] forKey:@"file"];
00610 }
00611
00612 - (NSURL*) converted
00613 {
00614 NSString* valStr = [properties valueForKey:@"converted"];
00615 if (valStr == nil)
00616 return nil;
00617
00618 return [NSURL URLWithString:valStr];
00619 }
00620
00621 - (void) setConverted:(NSURL*)value
00622 {
00623 [properties setValue:[value absoluteString] forKey:@"converted"];
00624 }
00625
00626 - (NSNumber*) duration
00627 {
00628 NSString* valStr = [properties valueForKey:@"duration"];
00629 if (valStr == nil)
00630 return nil;
00631
00632 return [NSNumber numberWithInteger:[valStr integerValue]];
00633 }
00634
00635 - (void) setDuration:(NSNumber*)value
00636 {
00637 [properties setValue:[value stringValue] forKey:@"duration"];
00638 }
00639
00640 - (NSString*) artist
00641 {
00642 return [properties valueForKey:@"artist"];
00643 }
00644
00645 - (void) setArtist:(NSString*)value
00646 {
00647 [properties setValue:value forKey:@"artist"];
00648 }
00649
00650 - (NSString*) trackTitle
00651 {
00652 return [properties valueForKey:@"trackTitle"];
00653 }
00654
00655 - (void) setTrackTitle:(NSString*)value
00656 {
00657 [properties setValue:value forKey:@"trackTitle"];
00658 }
00659
00660 @end
00661
00662 #pragma mark -
00663
00664
00665
00666
00667 @implementation DropIOMovie
00668
00669 - (void) dealloc
00670 {
00671 [thumbImage release];
00672 [super dealloc];
00673 }
00674
00675 - (NSURL*) thumbnail
00676 {
00677 NSString* valStr = [properties valueForKey:@"thumbnail"];
00678 if (valStr == nil)
00679 return nil;
00680
00681 return [NSURL URLWithString:valStr];
00682 }
00683
00684 - (void) setThumbnail:(NSURL*)value
00685 {
00686 [properties setValue:[value absoluteString] forKey:@"thumbnail"];
00687 }
00688
00689 - (NSURL*) file
00690 {
00691 NSString* valStr = [properties valueForKey:@"file"];
00692 if (valStr == nil)
00693 return nil;
00694
00695 return [NSURL URLWithString:valStr];
00696 }
00697
00698 - (void) setFile:(NSURL*)value
00699 {
00700 [properties setValue:[value absoluteString] forKey:@"file"];
00701 }
00702
00703 - (NSURL*) converted
00704 {
00705 NSString* valStr = [properties valueForKey:@"converted"];
00706 if (valStr == nil)
00707 return nil;
00708
00709 return [NSURL URLWithString:valStr];
00710 }
00711
00712 - (void) setConverted:(NSURL*)value
00713 {
00714 [properties setValue:[value absoluteString] forKey:@"converted"];
00715 }
00716
00717 - (NSNumber*) duration
00718 {
00719 NSString* valStr = [properties valueForKey:@"duration"];
00720 if (valStr == nil)
00721 return nil;
00722
00723 return [NSNumber numberWithInteger:[valStr integerValue]];
00724 }
00725
00726 - (void) setDuration:(NSNumber*)value
00727 {
00728 [properties setValue:[value stringValue] forKey:@"duration"];
00729 }
00730
00731 - (void) loadThumbnailImage:(NSURL*)thumbURL
00732 {
00733 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
00734 thumbImage = [[UIImage imageWithData:[NSData dataWithContentsOfURL:thumbURL]] retain];
00735 [self performSelector:@selector(cropThumbnailImage) onThread:[NSThread mainThread] withObject:nil waitUntilDone:NO];
00736 [pool release];
00737 }
00738
00739 - (void) cropThumbnailImage
00740 {
00741 UIImage* croppedImage = [thumbImage cropSquare:THUMB_SIZE];
00742 [thumbImage release];
00743 thumbImage = [croppedImage retain];
00744
00745 if ((loadedTarget != nil) && (loadedSel != nil))
00746 {
00747 [loadedTarget performSelector:loadedSel];
00748
00749 [loadedTarget release];
00750 loadedTarget = nil;
00751 loadedSel = nil;
00752 }
00753 }
00754
00755 - (UIImage*) icon
00756 {
00757 NSURL* thumbURL = [self thumbnail];
00758 if (thumbURL != nil)
00759 {
00760 if (thumbImage != nil)
00761 return thumbImage;
00762
00763 [self performSelectorInBackground:@selector(loadThumbnailImage:) withObject:thumbURL];
00764 }
00765
00766 return [super icon];
00767 }
00768
00769 - (BOOL) isThumbnailLoaded
00770 {
00771 return (thumbImage != nil);
00772 }
00773
00774 - (void) whenThumbnailIsLoadedPerformSelector:(SEL)selector target:(id)obj
00775 {
00776 loadedTarget = [obj retain];
00777 loadedSel = selector;
00778 }
00779 @end